summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/subsidy.cpp59
-rw-r--r--src/subsidy_base.h3
2 files changed, 39 insertions, 23 deletions
diff --git a/src/subsidy.cpp b/src/subsidy.cpp
index 04f0df118..1d183cc9a 100644
--- a/src/subsidy.cpp
+++ b/src/subsidy.cpp
@@ -14,12 +14,47 @@
#include "strings_func.h"
#include "window_func.h"
#include "subsidy_base.h"
+#include "subsidy_func.h"
#include "table/strings.h"
/* static */ Subsidy Subsidy::array[MAX_COMPANIES];
/**
+ * Marks subsidy as awarded, creates news and AI event
+ * @param from source station
+ * @param to destination station
+ * @param company awarded company
+ */
+void Subsidy::AwardTo(StationID from, StationID to, CompanyID company)
+{
+ assert(!this->IsAwarded());
+
+ this->age = 12;
+ this->from = from;
+ this->to = to;
+
+ /* Add a news item */
+ Pair reftype = SetupSubsidyDecodeParam(this, 0);
+ InjectDParam(1);
+
+ char *company_name = MallocT<char>(MAX_LENGTH_COMPANY_NAME_BYTES);
+ SetDParam(0, company);
+ GetString(company_name, STR_COMPANY_NAME, company_name + MAX_LENGTH_COMPANY_NAME_BYTES - 1);
+
+ SetDParamStr(0, company_name);
+ AddNewsItem(
+ STR_NEWS_SERVICE_SUBSIDY_AWARDED_HALF + _settings_game.difficulty.subsidy_multiplier,
+ NS_SUBSIDIES,
+ (NewsReferenceType)reftype.a, this->from, (NewsReferenceType)reftype.b, this->to,
+ company_name
+ );
+ AI::BroadcastNewEvent(new AIEventSubsidyAwarded(this->Index()));
+
+ InvalidateWindow(WC_SUBSIDIES_LIST, 0);
+}
+
+/**
* Allocates one subsidy
* @return pointer to first invalid subsidy, NULL if there is none
*/
@@ -349,29 +384,7 @@ bool CheckSubsidised(const Station *from, const Station *to, CargoID cargo_type,
}
if (DistanceMax(xy, to->xy) > 9) continue;
- /* Found a subsidy, change the values to indicate that it's in use */
- s->age = 12;
- s->from = from->index;
- s->to = to->index;
-
- /* Add a news item */
- Pair reftype = SetupSubsidyDecodeParam(s, 0);
- InjectDParam(1);
-
- char *company_name = MallocT<char>(MAX_LENGTH_COMPANY_NAME_BYTES);
- SetDParam(0, company);
- GetString(company_name, STR_COMPANY_NAME, company_name + MAX_LENGTH_COMPANY_NAME_BYTES - 1);
-
- SetDParamStr(0, company_name);
- AddNewsItem(
- STR_NEWS_SERVICE_SUBSIDY_AWARDED_HALF + _settings_game.difficulty.subsidy_multiplier,
- NS_SUBSIDIES,
- (NewsReferenceType)reftype.a, s->from, (NewsReferenceType)reftype.b, s->to,
- company_name
- );
- AI::BroadcastNewEvent(new AIEventSubsidyAwarded(s->Index()));
-
- InvalidateWindow(WC_SUBSIDIES_LIST, 0);
+ s->AwardTo(from->index, to->index, company);
return true;
}
}
diff --git a/src/subsidy_base.h b/src/subsidy_base.h
index adb03f83b..790be8c6d 100644
--- a/src/subsidy_base.h
+++ b/src/subsidy_base.h
@@ -7,6 +7,7 @@
#include "cargo_type.h"
#include "company_type.h"
+#include "station_type.h"
typedef uint16 SubsidyID; ///< ID of a subsidy
@@ -26,6 +27,8 @@ struct Subsidy {
return this->age >= 12;
}
+ void AwardTo(StationID from, StationID to, CompanyID company);
+
/**
* Determines index of this subsidy
* @return index (in the Subsidy::array array)