2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file subsidy.cpp Handling of subsidies. */
11 #include "company_func.h"
14 #include "news_func.h"
16 #include "station_base.h"
17 #include "strings_func.h"
18 #include "window_func.h"
19 #include "subsidy_base.h"
20 #include "subsidy_func.h"
21 #include "core/pool_func.hpp"
22 #include "core/random_func.hpp"
23 #include "game/game.hpp"
24 #include "command_func.h"
25 #include "string_func.h"
28 #include "table/strings.h"
30 #include "safeguards.h"
32 SubsidyPool
_subsidy_pool("Subsidy"); ///< Pool for the subsidies.
33 INSTANTIATE_POOL_METHODS(Subsidy
)
36 * Marks subsidy as awarded, creates news and AI event
37 * @param company awarded company
39 void Subsidy::AwardTo(CompanyID company
)
41 assert(!this->IsAwarded());
43 this->awarded
= company
;
44 this->remaining
= _settings_game
.difficulty
.subsidy_duration
* MONTHS_IN_YEAR
;
46 SetDParam(0, company
);
47 NewsStringData
*company_name
= new NewsStringData(GetString(STR_COMPANY_NAME
));
50 std::pair
<NewsReferenceType
, NewsReferenceType
> reftype
= SetupSubsidyDecodeParam(this, SubsidyDecodeParamType::NewsAwarded
, 1);
52 SetDParamStr(0, company_name
->string
);
54 STR_NEWS_SERVICE_SUBSIDY_AWARDED_HALF
+ _settings_game
.difficulty
.subsidy_multiplier
,
55 NT_SUBSIDIES
, NF_NORMAL
,
56 reftype
.first
, this->src
, reftype
.second
, this->dst
,
59 AI::BroadcastNewEvent(new ScriptEventSubsidyAwarded(this->index
));
60 Game::NewEvent(new ScriptEventSubsidyAwarded(this->index
));
62 InvalidateWindowData(WC_SUBSIDIES_LIST
, 0);
66 * Setup the string parameters for printing the subsidy at the screen, and compute the news reference for the subsidy.
67 * @param s %Subsidy being printed.
68 * @param mode Type of subsidy news message to decide on parameter format.
69 * @param parameter_offset The location/index in the String DParams to start decoding the subsidy's parameters. Defaults to 0.
70 * @return Reference of the subsidy in the news system.
72 std::pair
<NewsReferenceType
, NewsReferenceType
> SetupSubsidyDecodeParam(const Subsidy
*s
, SubsidyDecodeParamType mode
, uint parameter_offset
)
74 NewsReferenceType reftype1
= NR_NONE
;
75 NewsReferenceType reftype2
= NR_NONE
;
77 /* Always use the plural form of the cargo name - trying to decide between plural or singular causes issues for translations */
78 const CargoSpec
*cs
= CargoSpec::Get(s
->cargo_type
);
79 SetDParam(parameter_offset
, cs
->name
);
81 switch (s
->src_type
) {
83 reftype1
= NR_INDUSTRY
;
84 SetDParam(parameter_offset
+ 1, STR_INDUSTRY_NAME
);
88 SetDParam(parameter_offset
+ 1, STR_TOWN_NAME
);
90 default: NOT_REACHED();
92 SetDParam(parameter_offset
+ 2, s
->src
);
94 switch (s
->dst_type
) {
96 reftype2
= NR_INDUSTRY
;
97 SetDParam(parameter_offset
+ 4, STR_INDUSTRY_NAME
);
101 SetDParam(parameter_offset
+ 4, STR_TOWN_NAME
);
103 default: NOT_REACHED();
105 SetDParam(parameter_offset
+ 5, s
->dst
);
107 /* If the subsidy is being offered or awarded, the news item mentions the subsidy duration. */
108 if (mode
== SubsidyDecodeParamType::NewsOffered
|| mode
== SubsidyDecodeParamType::NewsAwarded
) {
109 SetDParam(parameter_offset
+ 7, _settings_game
.difficulty
.subsidy_duration
);
112 return std::pair
<NewsReferenceType
, NewsReferenceType
>(reftype1
, reftype2
);
116 * Sets a flag indicating that given town/industry is part of subsidised route.
117 * @param type is it a town or an industry?
118 * @param index index of town/industry
119 * @param flag flag to set
121 static inline void SetPartOfSubsidyFlag(SourceType type
, SourceID index
, PartOfSubsidy flag
)
124 case ST_INDUSTRY
: Industry::Get(index
)->part_of_subsidy
|= flag
; return;
125 case ST_TOWN
: Town::Get(index
)->cache
.part_of_subsidy
|= flag
; return;
126 default: NOT_REACHED();
130 /** Perform a full rebuild of the subsidies cache. */
131 void RebuildSubsidisedSourceAndDestinationCache()
133 for (Town
*t
: Town::Iterate()) t
->cache
.part_of_subsidy
= POS_NONE
;
135 for (Industry
*i
: Industry::Iterate()) i
->part_of_subsidy
= POS_NONE
;
137 for (const Subsidy
*s
: Subsidy::Iterate()) {
138 SetPartOfSubsidyFlag(s
->src_type
, s
->src
, POS_SRC
);
139 SetPartOfSubsidyFlag(s
->dst_type
, s
->dst
, POS_DST
);
144 * Delete the subsidies associated with a given cargo source type and id.
145 * @param type Cargo source type of the id.
146 * @param index Id to remove.
148 void DeleteSubsidyWith(SourceType type
, SourceID index
)
152 for (Subsidy
*s
: Subsidy::Iterate()) {
153 if ((s
->src_type
== type
&& s
->src
== index
) || (s
->dst_type
== type
&& s
->dst
== index
)) {
160 InvalidateWindowData(WC_SUBSIDIES_LIST
, 0);
161 RebuildSubsidisedSourceAndDestinationCache();
166 * Check whether a specific subsidy already exists.
167 * @param cargo Cargo type.
168 * @param src_type Type of source of the cargo, affects interpretation of \a src.
169 * @param src Id of the source.
170 * @param dst_type Type of the destination of the cargo, affects interpretation of \a dst.
171 * @param dst Id of the destination.
172 * @return \c true if the subsidy already exists, \c false if not.
174 static bool CheckSubsidyDuplicate(CargoID cargo
, SourceType src_type
, SourceID src
, SourceType dst_type
, SourceID dst
)
176 for (const Subsidy
*s
: Subsidy::Iterate()) {
177 if (s
->cargo_type
== cargo
&&
178 s
->src_type
== src_type
&& s
->src
== src
&&
179 s
->dst_type
== dst_type
&& s
->dst
== dst
) {
187 * Checks if the source and destination of a subsidy are inside the distance limit.
188 * @param src_type Type of \a src.
189 * @param src Index of source.
190 * @param dst_type Type of \a dst.
191 * @param dst Index of destination.
192 * @return True if they are inside the distance limit.
194 static bool CheckSubsidyDistance(SourceType src_type
, SourceID src
, SourceType dst_type
, SourceID dst
)
196 TileIndex tile_src
= (src_type
== ST_TOWN
) ? Town::Get(src
)->xy
: Industry::Get(src
)->location
.tile
;
197 TileIndex tile_dst
= (dst_type
== ST_TOWN
) ? Town::Get(dst
)->xy
: Industry::Get(dst
)->location
.tile
;
199 return (DistanceManhattan(tile_src
, tile_dst
) <= SUBSIDY_MAX_DISTANCE
);
203 * Creates a subsidy with the given parameters.
204 * @param cid Subsidised cargo.
205 * @param src_type Type of \a src.
206 * @param src Index of source.
207 * @param dst_type Type of \a dst.
208 * @param dst Index of destination.
210 void CreateSubsidy(CargoID cid
, SourceType src_type
, SourceID src
, SourceType dst_type
, SourceID dst
)
212 Subsidy
*s
= new Subsidy();
214 s
->src_type
= src_type
;
216 s
->dst_type
= dst_type
;
218 s
->remaining
= SUBSIDY_OFFER_MONTHS
;
219 s
->awarded
= INVALID_COMPANY
;
221 std::pair
<NewsReferenceType
, NewsReferenceType
> reftype
= SetupSubsidyDecodeParam(s
, SubsidyDecodeParamType::NewsOffered
);
222 AddNewsItem(STR_NEWS_SERVICE_SUBSIDY_OFFERED
, NT_SUBSIDIES
, NF_NORMAL
, reftype
.first
, s
->src
, reftype
.second
, s
->dst
);
223 SetPartOfSubsidyFlag(s
->src_type
, s
->src
, POS_SRC
);
224 SetPartOfSubsidyFlag(s
->dst_type
, s
->dst
, POS_DST
);
225 AI::BroadcastNewEvent(new ScriptEventSubsidyOffer(s
->index
));
226 Game::NewEvent(new ScriptEventSubsidyOffer(s
->index
));
228 InvalidateWindowData(WC_SUBSIDIES_LIST
, 0);
232 * Create a new subsidy.
233 * @param tile unused.
234 * @param flags type of operation
235 * @param p1 various bitstuffed elements
236 * - p1 = (bit 0 - 7) - SourceType of source.
237 * - p1 = (bit 8 - 23) - SourceID of source.
238 * - p1 = (bit 24 - 31) - CargoID of subsidy.
239 * @param p2 various bitstuffed elements
240 * - p2 = (bit 0 - 7) - SourceType of destination.
241 * - p2 = (bit 8 - 23) - SourceID of destination.
242 * @param text unused.
243 * @return the cost of this operation or an error
245 CommandCost
CmdCreateSubsidy(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const std::string
&text
)
247 if (!Subsidy::CanAllocateItem()) return CMD_ERROR
;
249 CargoID cid
= GB(p1
, 24, 8);
250 SourceType src_type
= (SourceType
)GB(p1
, 0, 8);
251 SourceID src
= GB(p1
, 8, 16);
252 SourceType dst_type
= (SourceType
)GB(p2
, 0, 8);
253 SourceID dst
= GB(p2
, 8, 16);
255 if (_current_company
!= OWNER_DEITY
) return CMD_ERROR
;
257 if (cid
>= NUM_CARGO
|| !::CargoSpec::Get(cid
)->IsValid()) return CMD_ERROR
;
261 if (!Town::IsValidID(src
)) return CMD_ERROR
;
264 if (!Industry::IsValidID(src
)) return CMD_ERROR
;
271 if (!Town::IsValidID(dst
)) return CMD_ERROR
;
274 if (!Industry::IsValidID(dst
)) return CMD_ERROR
;
280 if (flags
& DC_EXEC
) {
281 CreateSubsidy(cid
, src_type
, src
, dst_type
, dst
);
284 return CommandCost();
288 * Tries to create a passenger subsidy between two towns.
289 * @return True iff the subsidy was created.
291 bool FindSubsidyPassengerRoute()
293 if (!Subsidy::CanAllocateItem()) return false;
295 const Town
*src
= Town::GetRandom();
296 if (src
->cache
.population
< SUBSIDY_PAX_MIN_POPULATION
||
297 src
->GetPercentTransported(CT_PASSENGERS
) > SUBSIDY_MAX_PCT_TRANSPORTED
) {
301 const Town
*dst
= Town::GetRandom();
302 if (dst
->cache
.population
< SUBSIDY_PAX_MIN_POPULATION
|| src
== dst
) {
306 if (DistanceManhattan(src
->xy
, dst
->xy
) > SUBSIDY_MAX_DISTANCE
) return false;
307 if (CheckSubsidyDuplicate(CT_PASSENGERS
, ST_TOWN
, src
->index
, ST_TOWN
, dst
->index
)) return false;
309 CreateSubsidy(CT_PASSENGERS
, ST_TOWN
, src
->index
, ST_TOWN
, dst
->index
);
314 bool FindSubsidyCargoDestination(CargoID cid
, SourceType src_type
, SourceID src
);
318 * Tries to create a cargo subsidy with a town as source.
319 * @return True iff the subsidy was created.
321 bool FindSubsidyTownCargoRoute()
323 if (!Subsidy::CanAllocateItem()) return false;
325 SourceType src_type
= ST_TOWN
;
327 /* Select a random town. */
328 const Town
*src_town
= Town::GetRandom();
329 if (src_town
->cache
.population
< SUBSIDY_CARGO_MIN_POPULATION
) return false;
331 /* Calculate the produced cargo of houses around town center. */
332 CargoArray town_cargo_produced
;
333 TileArea ta
= TileArea(src_town
->xy
, 1, 1).Expand(SUBSIDY_TOWN_CARGO_RADIUS
);
334 for (TileIndex tile
: ta
) {
335 if (IsTileType(tile
, MP_HOUSE
)) {
336 AddProducedCargo(tile
, town_cargo_produced
);
340 /* Passenger subsidies are not handled here. */
341 town_cargo_produced
[CT_PASSENGERS
] = 0;
343 uint8 cargo_count
= 0;
344 for (CargoID i
= 0; i
< NUM_CARGO
; i
++) {
345 if (town_cargo_produced
[i
] > 0) cargo_count
++;
348 /* No cargo produced at all? */
349 if (cargo_count
== 0) return false;
351 /* Choose a random cargo that is produced in the town. */
352 uint8 cargo_number
= RandomRange(cargo_count
);
354 for (cid
= 0; cid
< NUM_CARGO
; cid
++) {
355 if (town_cargo_produced
[cid
] > 0) {
356 if (cargo_number
== 0) break;
361 /* Avoid using invalid NewGRF cargoes. */
362 if (!CargoSpec::Get(cid
)->IsValid() ||
363 _settings_game
.linkgraph
.GetDistributionType(cid
) != DT_MANUAL
) {
367 /* Quit if the percentage transported is large enough. */
368 if (src_town
->GetPercentTransported(cid
) > SUBSIDY_MAX_PCT_TRANSPORTED
) return false;
370 SourceID src
= src_town
->index
;
372 return FindSubsidyCargoDestination(cid
, src_type
, src
);
376 * Tries to create a cargo subsidy with an industry as source.
377 * @return True iff the subsidy was created.
379 bool FindSubsidyIndustryCargoRoute()
381 if (!Subsidy::CanAllocateItem()) return false;
383 SourceType src_type
= ST_INDUSTRY
;
385 /* Select a random industry. */
386 const Industry
*src_ind
= Industry::GetRandom();
387 if (src_ind
== nullptr) return false;
393 /* Randomize cargo type */
396 for (cargo_index
= 0; cargo_index
< lengthof(src_ind
->produced_cargo
); cargo_index
++) {
397 if (src_ind
->produced_cargo
[cargo_index
] != CT_INVALID
) num_cargos
++;
399 if (num_cargos
== 0) return false; // industry produces nothing
400 int cargo_num
= RandomRange(num_cargos
) + 1;
401 for (cargo_index
= 0; cargo_index
< lengthof(src_ind
->produced_cargo
); cargo_index
++) {
402 if (src_ind
->produced_cargo
[cargo_index
] != CT_INVALID
) cargo_num
--;
403 if (cargo_num
== 0) break;
405 assert(cargo_num
== 0); // indicates loop didn't break as intended
406 cid
= src_ind
->produced_cargo
[cargo_index
];
407 trans
= src_ind
->last_month_pct_transported
[cargo_index
];
408 total
= src_ind
->last_month_production
[cargo_index
];
410 /* Quit if no production in this industry
411 * or if the pct transported is already large enough
412 * or if the cargo is automatically distributed */
413 if (total
== 0 || trans
> SUBSIDY_MAX_PCT_TRANSPORTED
||
415 _settings_game
.linkgraph
.GetDistributionType(cid
) != DT_MANUAL
) {
419 SourceID src
= src_ind
->index
;
421 return FindSubsidyCargoDestination(cid
, src_type
, src
);
425 * Tries to find a suitable destination for the given source and cargo.
426 * @param cid Subsidized cargo.
427 * @param src_type Type of \a src.
428 * @param src Index of source.
429 * @return True iff the subsidy was created.
431 bool FindSubsidyCargoDestination(CargoID cid
, SourceType src_type
, SourceID src
)
433 /* Choose a random destination. */
434 SourceType dst_type
= Chance16(1, 2) ? ST_TOWN
: ST_INDUSTRY
;
439 /* Select a random town. */
440 const Town
*dst_town
= Town::GetRandom();
442 /* Calculate cargo acceptance of houses around town center. */
443 CargoArray town_cargo_accepted
;
444 TileArea ta
= TileArea(dst_town
->xy
, 1, 1).Expand(SUBSIDY_TOWN_CARGO_RADIUS
);
445 for (TileIndex tile
: ta
) {
446 if (IsTileType(tile
, MP_HOUSE
)) {
447 AddAcceptedCargo(tile
, town_cargo_accepted
, nullptr);
451 /* Check if the town can accept this cargo. */
452 if (town_cargo_accepted
[cid
] < 8) return false;
454 dst
= dst_town
->index
;
459 /* Select a random industry. */
460 const Industry
*dst_ind
= Industry::GetRandom();
461 if (dst_ind
== nullptr) return false;
463 /* The industry must accept the cargo */
464 bool valid
= std::find(dst_ind
->accepts_cargo
, endof(dst_ind
->accepts_cargo
), cid
) != endof(dst_ind
->accepts_cargo
);
465 if (!valid
) return false;
467 dst
= dst_ind
->index
;
471 default: NOT_REACHED();
474 /* Check that the source and the destination are not the same. */
475 if (src_type
== dst_type
&& src
== dst
) return false;
477 /* Check distance between source and destination. */
478 if (!CheckSubsidyDistance(src_type
, src
, dst_type
, dst
)) return false;
480 /* Avoid duplicate subsidies. */
481 if (CheckSubsidyDuplicate(cid
, src_type
, src
, dst_type
, dst
)) return false;
483 CreateSubsidy(cid
, src_type
, src
, dst_type
, dst
);
488 /** Perform the monthly update of open subsidies, and try to create a new one. */
489 void SubsidyMonthlyLoop()
491 bool modified
= false;
493 for (Subsidy
*s
: Subsidy::Iterate()) {
494 if (--s
->remaining
== 0) {
495 if (!s
->IsAwarded()) {
496 std::pair
<NewsReferenceType
, NewsReferenceType
> reftype
= SetupSubsidyDecodeParam(s
, SubsidyDecodeParamType::NewsWithdrawn
);
497 AddNewsItem(STR_NEWS_OFFER_OF_SUBSIDY_EXPIRED
, NT_SUBSIDIES
, NF_NORMAL
, reftype
.first
, s
->src
, reftype
.second
, s
->dst
);
498 AI::BroadcastNewEvent(new ScriptEventSubsidyOfferExpired(s
->index
));
499 Game::NewEvent(new ScriptEventSubsidyOfferExpired(s
->index
));
501 if (s
->awarded
== _local_company
) {
502 std::pair
<NewsReferenceType
, NewsReferenceType
> reftype
= SetupSubsidyDecodeParam(s
, SubsidyDecodeParamType::NewsWithdrawn
);
503 AddNewsItem(STR_NEWS_SUBSIDY_WITHDRAWN_SERVICE
, NT_SUBSIDIES
, NF_NORMAL
, reftype
.first
, s
->src
, reftype
.second
, s
->dst
);
505 AI::BroadcastNewEvent(new ScriptEventSubsidyExpired(s
->index
));
506 Game::NewEvent(new ScriptEventSubsidyExpired(s
->index
));
514 RebuildSubsidisedSourceAndDestinationCache();
515 } else if (_settings_game
.difficulty
.subsidy_duration
== 0) {
516 /* If subsidy duration is set to 0, subsidies are disabled, so bail out. */
518 } else if (_settings_game
.linkgraph
.distribution_pax
!= DT_MANUAL
&&
519 _settings_game
.linkgraph
.distribution_mail
!= DT_MANUAL
&&
520 _settings_game
.linkgraph
.distribution_armoured
!= DT_MANUAL
&&
521 _settings_game
.linkgraph
.distribution_default
!= DT_MANUAL
) {
522 /* Return early if there are no manually distributed cargoes and if we
523 * don't need to invalidate the subsidies window. */
527 bool passenger_subsidy
= false;
528 bool town_subsidy
= false;
529 bool industry_subsidy
= false;
531 int random_chance
= RandomRange(16);
533 if (random_chance
< 2 && _settings_game
.linkgraph
.distribution_pax
== DT_MANUAL
) {
534 /* There is a 1/8 chance each month of generating a passenger subsidy. */
538 passenger_subsidy
= FindSubsidyPassengerRoute();
539 } while (!passenger_subsidy
&& n
--);
540 } else if (random_chance
== 2) {
541 /* Cargo subsidies with a town as a source have a 1/16 chance. */
545 town_subsidy
= FindSubsidyTownCargoRoute();
546 } while (!town_subsidy
&& n
--);
547 } else if (random_chance
== 3) {
548 /* Cargo subsidies with an industry as a source have a 1/16 chance. */
552 industry_subsidy
= FindSubsidyIndustryCargoRoute();
553 } while (!industry_subsidy
&& n
--);
556 modified
|= passenger_subsidy
|| town_subsidy
|| industry_subsidy
;
558 if (modified
) InvalidateWindowData(WC_SUBSIDIES_LIST
, 0);
562 * Tests whether given delivery is subsidised and possibly awards the subsidy to delivering company
563 * @param cargo_type type of cargo
564 * @param company company delivering the cargo
565 * @param src_type type of \a src
566 * @param src index of source
567 * @param st station where the cargo is delivered to
568 * @return is the delivery subsidised?
570 bool CheckSubsidised(CargoID cargo_type
, CompanyID company
, SourceType src_type
, SourceID src
, const Station
*st
)
572 /* If the source isn't subsidised, don't continue */
573 if (src
== INVALID_SOURCE
) return false;
576 if (!(Industry::Get(src
)->part_of_subsidy
& POS_SRC
)) return false;
579 if (!(Town::Get(src
)->cache
.part_of_subsidy
& POS_SRC
)) return false;
581 default: return false;
584 /* Remember all towns near this station (at least one house in its catchment radius)
585 * which are destination of subsidised path. Do that only if needed */
586 std::vector
<const Town
*> towns_near
;
587 if (!st
->rect
.IsEmpty()) {
588 for (const Subsidy
*s
: Subsidy::Iterate()) {
589 /* Don't create the cache if there is no applicable subsidy with town as destination */
590 if (s
->dst_type
!= ST_TOWN
) continue;
591 if (s
->cargo_type
!= cargo_type
|| s
->src_type
!= src_type
|| s
->src
!= src
) continue;
592 if (s
->IsAwarded() && s
->awarded
!= company
) continue;
594 BitmapTileIterator
it(st
->catchment_tiles
);
595 for (TileIndex tile
= it
; tile
!= INVALID_TILE
; tile
= ++it
) {
596 if (!IsTileType(tile
, MP_HOUSE
)) continue;
597 const Town
*t
= Town::GetByTile(tile
);
598 if (t
->cache
.part_of_subsidy
& POS_DST
) include(towns_near
, t
);
604 bool subsidised
= false;
606 /* Check if there's a (new) subsidy that applies. There can be more subsidies triggered by this delivery!
607 * Think about the case that subsidies are A->B and A->C and station has both B and C in its catchment area */
608 for (Subsidy
*s
: Subsidy::Iterate()) {
609 if (s
->cargo_type
== cargo_type
&& s
->src_type
== src_type
&& s
->src
== src
&& (!s
->IsAwarded() || s
->awarded
== company
)) {
610 switch (s
->dst_type
) {
612 for (Industry
*ind
: st
->industries_near
) {
613 if (s
->dst
== ind
->index
) {
614 assert(ind
->part_of_subsidy
& POS_DST
);
616 if (!s
->IsAwarded()) s
->AwardTo(company
);
621 for (const Town
*tp
: towns_near
) {
622 if (s
->dst
== tp
->index
) {
623 assert(tp
->cache
.part_of_subsidy
& POS_DST
);
625 if (!s
->IsAwarded()) s
->AwardTo(company
);