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 "core/container_func.hpp"
24 #include "game/game.hpp"
25 #include "command_func.h"
26 #include "string_func.h"
28 #include "subsidy_cmd.h"
29 #include "timer/timer.h"
30 #include "timer/timer_game_economy.h"
32 #include "table/strings.h"
34 #include "safeguards.h"
36 SubsidyPool
_subsidy_pool("Subsidy"); ///< Pool for the subsidies.
37 INSTANTIATE_POOL_METHODS(Subsidy
)
40 * Marks subsidy as awarded, creates news and AI event
41 * @param company awarded company
43 void Subsidy::AwardTo(CompanyID company
)
45 assert(!this->IsAwarded());
47 this->awarded
= company
;
48 this->remaining
= _settings_game
.difficulty
.subsidy_duration
* CalendarTime::MONTHS_IN_YEAR
;
50 SetDParam(0, company
);
51 NewsStringData
*company_name
= new NewsStringData(GetString(STR_COMPANY_NAME
));
54 std::pair
<NewsReferenceType
, NewsReferenceType
> reftype
= SetupSubsidyDecodeParam(this, SubsidyDecodeParamType::NewsAwarded
, 1);
56 SetDParamStr(0, company_name
->string
);
58 STR_NEWS_SERVICE_SUBSIDY_AWARDED_HALF
+ _settings_game
.difficulty
.subsidy_multiplier
,
59 NT_SUBSIDIES
, NF_NORMAL
,
60 reftype
.first
, this->src
, reftype
.second
, this->dst
,
63 AI::BroadcastNewEvent(new ScriptEventSubsidyAwarded(this->index
));
64 Game::NewEvent(new ScriptEventSubsidyAwarded(this->index
));
66 InvalidateWindowData(WC_SUBSIDIES_LIST
, 0);
70 * Setup the string parameters for printing the subsidy at the screen, and compute the news reference for the subsidy.
71 * @param s %Subsidy being printed.
72 * @param mode Type of subsidy news message to decide on parameter format.
73 * @param parameter_offset The location/index in the String DParams to start decoding the subsidy's parameters. Defaults to 0.
74 * @return Reference of the subsidy in the news system.
76 std::pair
<NewsReferenceType
, NewsReferenceType
> SetupSubsidyDecodeParam(const Subsidy
*s
, SubsidyDecodeParamType mode
, uint parameter_offset
)
78 NewsReferenceType reftype1
= NR_NONE
;
79 NewsReferenceType reftype2
= NR_NONE
;
81 /* Always use the plural form of the cargo name - trying to decide between plural or singular causes issues for translations */
82 const CargoSpec
*cs
= CargoSpec::Get(s
->cargo_type
);
83 SetDParam(parameter_offset
, cs
->name
);
85 switch (s
->src_type
) {
86 case SourceType::Industry
:
87 reftype1
= NR_INDUSTRY
;
88 SetDParam(parameter_offset
+ 1, STR_INDUSTRY_NAME
);
90 case SourceType::Town
:
92 SetDParam(parameter_offset
+ 1, STR_TOWN_NAME
);
94 default: NOT_REACHED();
96 SetDParam(parameter_offset
+ 2, s
->src
);
98 switch (s
->dst_type
) {
99 case SourceType::Industry
:
100 reftype2
= NR_INDUSTRY
;
101 SetDParam(parameter_offset
+ 4, STR_INDUSTRY_NAME
);
103 case SourceType::Town
:
105 SetDParam(parameter_offset
+ 4, STR_TOWN_NAME
);
107 default: NOT_REACHED();
109 SetDParam(parameter_offset
+ 5, s
->dst
);
111 /* If the subsidy is being offered or awarded, the news item mentions the subsidy duration. */
112 if (mode
== SubsidyDecodeParamType::NewsOffered
|| mode
== SubsidyDecodeParamType::NewsAwarded
) {
113 SetDParam(parameter_offset
+ 7, _settings_game
.difficulty
.subsidy_duration
);
116 return std::pair
<NewsReferenceType
, NewsReferenceType
>(reftype1
, reftype2
);
120 * Sets a flag indicating that given town/industry is part of subsidised route.
121 * @param type is it a town or an industry?
122 * @param index index of town/industry
123 * @param flag flag to set
125 static inline void SetPartOfSubsidyFlag(SourceType type
, SourceID index
, PartOfSubsidy flag
)
128 case SourceType::Industry
: Industry::Get(index
)->part_of_subsidy
|= flag
; return;
129 case SourceType::Town
: Town::Get(index
)->cache
.part_of_subsidy
|= flag
; return;
130 default: NOT_REACHED();
134 /** Perform a full rebuild of the subsidies cache. */
135 void RebuildSubsidisedSourceAndDestinationCache()
137 for (Town
*t
: Town::Iterate()) t
->cache
.part_of_subsidy
= POS_NONE
;
139 for (Industry
*i
: Industry::Iterate()) i
->part_of_subsidy
= POS_NONE
;
141 for (const Subsidy
*s
: Subsidy::Iterate()) {
142 SetPartOfSubsidyFlag(s
->src_type
, s
->src
, POS_SRC
);
143 SetPartOfSubsidyFlag(s
->dst_type
, s
->dst
, POS_DST
);
148 * Delete the subsidies associated with a given cargo source type and id.
149 * @param type Cargo source type of the id.
150 * @param index Id to remove.
152 void DeleteSubsidyWith(SourceType type
, SourceID index
)
156 for (Subsidy
*s
: Subsidy::Iterate()) {
157 if ((s
->src_type
== type
&& s
->src
== index
) || (s
->dst_type
== type
&& s
->dst
== index
)) {
164 InvalidateWindowData(WC_SUBSIDIES_LIST
, 0);
165 RebuildSubsidisedSourceAndDestinationCache();
170 * Check whether a specific subsidy already exists.
171 * @param cargo Cargo type.
172 * @param src_type Type of source of the cargo, affects interpretation of \a src.
173 * @param src Id of the source.
174 * @param dst_type Type of the destination of the cargo, affects interpretation of \a dst.
175 * @param dst Id of the destination.
176 * @return \c true if the subsidy already exists, \c false if not.
178 static bool CheckSubsidyDuplicate(CargoID cargo
, SourceType src_type
, SourceID src
, SourceType dst_type
, SourceID dst
)
180 for (const Subsidy
*s
: Subsidy::Iterate()) {
181 if (s
->cargo_type
== cargo
&&
182 s
->src_type
== src_type
&& s
->src
== src
&&
183 s
->dst_type
== dst_type
&& s
->dst
== dst
) {
191 * Checks if the source and destination of a subsidy are inside the distance limit.
192 * @param src_type Type of \a src.
193 * @param src Index of source.
194 * @param dst_type Type of \a dst.
195 * @param dst Index of destination.
196 * @return True if they are inside the distance limit.
198 static bool CheckSubsidyDistance(SourceType src_type
, SourceID src
, SourceType dst_type
, SourceID dst
)
200 TileIndex tile_src
= (src_type
== SourceType::Town
) ? Town::Get(src
)->xy
: Industry::Get(src
)->location
.tile
;
201 TileIndex tile_dst
= (dst_type
== SourceType::Town
) ? Town::Get(dst
)->xy
: Industry::Get(dst
)->location
.tile
;
203 return (DistanceManhattan(tile_src
, tile_dst
) <= SUBSIDY_MAX_DISTANCE
);
207 * Creates a subsidy with the given parameters.
208 * @param cid Subsidised cargo.
209 * @param src_type Type of \a src.
210 * @param src Index of source.
211 * @param dst_type Type of \a dst.
212 * @param dst Index of destination.
214 void CreateSubsidy(CargoID cid
, SourceType src_type
, SourceID src
, SourceType dst_type
, SourceID dst
)
216 Subsidy
*s
= new Subsidy();
218 s
->src_type
= src_type
;
220 s
->dst_type
= dst_type
;
222 s
->remaining
= SUBSIDY_OFFER_MONTHS
;
223 s
->awarded
= INVALID_COMPANY
;
225 std::pair
<NewsReferenceType
, NewsReferenceType
> reftype
= SetupSubsidyDecodeParam(s
, SubsidyDecodeParamType::NewsOffered
);
226 AddNewsItem(STR_NEWS_SERVICE_SUBSIDY_OFFERED
, NT_SUBSIDIES
, NF_NORMAL
, reftype
.first
, s
->src
, reftype
.second
, s
->dst
);
227 SetPartOfSubsidyFlag(s
->src_type
, s
->src
, POS_SRC
);
228 SetPartOfSubsidyFlag(s
->dst_type
, s
->dst
, POS_DST
);
229 AI::BroadcastNewEvent(new ScriptEventSubsidyOffer(s
->index
));
230 Game::NewEvent(new ScriptEventSubsidyOffer(s
->index
));
232 InvalidateWindowData(WC_SUBSIDIES_LIST
, 0);
236 * Create a new subsidy.
237 * @param flags type of operation
238 * @param cid CargoID of subsidy.
239 * @param src_type SourceType of source.
240 * @param src SourceID of source.
241 * @param dst_type SourceType of destination.
242 * @param dst SourceID of destination.
243 * @return the cost of this operation or an error
245 CommandCost
CmdCreateSubsidy(DoCommandFlag flags
, CargoID cid
, SourceType src_type
, SourceID src
, SourceType dst_type
, SourceID dst
)
247 if (!Subsidy::CanAllocateItem()) return CMD_ERROR
;
249 if (_current_company
!= OWNER_DEITY
) return CMD_ERROR
;
251 if (cid
>= NUM_CARGO
|| !::CargoSpec::Get(cid
)->IsValid()) return CMD_ERROR
;
254 case SourceType::Town
:
255 if (!Town::IsValidID(src
)) return CMD_ERROR
;
257 case SourceType::Industry
:
258 if (!Industry::IsValidID(src
)) return CMD_ERROR
;
264 case SourceType::Town
:
265 if (!Town::IsValidID(dst
)) return CMD_ERROR
;
267 case SourceType::Industry
:
268 if (!Industry::IsValidID(dst
)) return CMD_ERROR
;
274 if (flags
& DC_EXEC
) {
275 CreateSubsidy(cid
, src_type
, src
, dst_type
, dst
);
278 return CommandCost();
282 * Tries to create a passenger subsidy between two towns.
283 * @return True iff the subsidy was created.
285 bool FindSubsidyPassengerRoute()
287 if (!Subsidy::CanAllocateItem()) return false;
289 /* Pick a random TPE_PASSENGER type */
290 uint32_t r
= RandomRange(static_cast<uint
>(CargoSpec::town_production_cargoes
[TPE_PASSENGERS
].size()));
291 CargoID cid
= CargoSpec::town_production_cargoes
[TPE_PASSENGERS
][r
]->Index();
293 const Town
*src
= Town::GetRandom();
294 if (src
->cache
.population
< SUBSIDY_PAX_MIN_POPULATION
||
295 src
->GetPercentTransported(cid
) > SUBSIDY_MAX_PCT_TRANSPORTED
) {
299 const Town
*dst
= Town::GetRandom();
300 if (dst
->cache
.population
< SUBSIDY_PAX_MIN_POPULATION
|| src
== dst
) {
304 if (DistanceManhattan(src
->xy
, dst
->xy
) > SUBSIDY_MAX_DISTANCE
) return false;
305 if (CheckSubsidyDuplicate(cid
, SourceType::Town
, src
->index
, SourceType::Town
, dst
->index
)) return false;
307 CreateSubsidy(cid
, SourceType::Town
, src
->index
, SourceType::Town
, dst
->index
);
312 bool FindSubsidyCargoDestination(CargoID cid
, SourceType src_type
, SourceID src
);
316 * Tries to create a cargo subsidy with a town as source.
317 * @return True iff the subsidy was created.
319 bool FindSubsidyTownCargoRoute()
321 if (!Subsidy::CanAllocateItem()) return false;
323 SourceType src_type
= SourceType::Town
;
325 /* Select a random town. */
326 const Town
*src_town
= Town::GetRandom();
327 if (src_town
->cache
.population
< SUBSIDY_CARGO_MIN_POPULATION
) return false;
329 /* Calculate the produced cargo of houses around town center. */
330 CargoArray town_cargo_produced
{};
331 TileArea ta
= TileArea(src_town
->xy
, 1, 1).Expand(SUBSIDY_TOWN_CARGO_RADIUS
);
332 for (TileIndex tile
: ta
) {
333 if (IsTileType(tile
, MP_HOUSE
)) {
334 AddProducedCargo(tile
, town_cargo_produced
);
338 /* Passenger subsidies are not handled here. */
339 for (const CargoSpec
*cs
: CargoSpec::town_production_cargoes
[TPE_PASSENGERS
]) {
340 town_cargo_produced
[cs
->Index()] = 0;
343 uint8_t cargo_count
= town_cargo_produced
.GetCount();
345 /* No cargo produced at all? */
346 if (cargo_count
== 0) return false;
348 /* Choose a random cargo that is produced in the town. */
349 uint8_t cargo_number
= RandomRange(cargo_count
);
351 for (cid
= 0; cid
< NUM_CARGO
; cid
++) {
352 if (town_cargo_produced
[cid
] > 0) {
353 if (cargo_number
== 0) break;
358 /* Avoid using invalid NewGRF cargoes. */
359 if (!CargoSpec::Get(cid
)->IsValid() ||
360 _settings_game
.linkgraph
.GetDistributionType(cid
) != DT_MANUAL
) {
364 /* Quit if the percentage transported is large enough. */
365 if (src_town
->GetPercentTransported(cid
) > SUBSIDY_MAX_PCT_TRANSPORTED
) return false;
367 SourceID src
= src_town
->index
;
369 return FindSubsidyCargoDestination(cid
, src_type
, src
);
373 * Tries to create a cargo subsidy with an industry as source.
374 * @return True iff the subsidy was created.
376 bool FindSubsidyIndustryCargoRoute()
378 if (!Subsidy::CanAllocateItem()) return false;
380 SourceType src_type
= SourceType::Industry
;
382 /* Select a random industry. */
383 const Industry
*src_ind
= Industry::GetRandom();
384 if (src_ind
== nullptr) return false;
390 /* Randomize cargo type */
391 int num_cargos
= std::count_if(std::begin(src_ind
->produced
), std::end(src_ind
->produced
), [](const auto &p
) { return IsValidCargoID(p
.cargo
); });
392 if (num_cargos
== 0) return false; // industry produces nothing
393 int cargo_num
= RandomRange(num_cargos
) + 1;
395 auto it
= std::begin(src_ind
->produced
);
396 for (/* nothing */; it
!= std::end(src_ind
->produced
); ++it
) {
397 if (IsValidCargoID(it
->cargo
)) cargo_num
--;
398 if (cargo_num
== 0) break;
400 assert(it
!= std::end(src_ind
->produced
)); // indicates loop didn't end as intended
403 trans
= it
->history
[LAST_MONTH
].PctTransported();
404 total
= it
->history
[LAST_MONTH
].production
;
406 /* Quit if no production in this industry
407 * or if the pct transported is already large enough
408 * or if the cargo is automatically distributed */
409 if (total
== 0 || trans
> SUBSIDY_MAX_PCT_TRANSPORTED
||
410 !IsValidCargoID(cid
) ||
411 _settings_game
.linkgraph
.GetDistributionType(cid
) != DT_MANUAL
) {
415 SourceID src
= src_ind
->index
;
417 return FindSubsidyCargoDestination(cid
, src_type
, src
);
421 * Tries to find a suitable destination for the given source and cargo.
422 * @param cid Subsidized cargo.
423 * @param src_type Type of \a src.
424 * @param src Index of source.
425 * @return True iff the subsidy was created.
427 bool FindSubsidyCargoDestination(CargoID cid
, SourceType src_type
, SourceID src
)
429 /* Choose a random destination. */
430 SourceType dst_type
= Chance16(1, 2) ? SourceType::Town
: SourceType::Industry
;
434 case SourceType::Town
: {
435 /* Select a random town. */
436 const Town
*dst_town
= Town::GetRandom();
438 /* Calculate cargo acceptance of houses around town center. */
439 CargoArray town_cargo_accepted
{};
440 TileArea ta
= TileArea(dst_town
->xy
, 1, 1).Expand(SUBSIDY_TOWN_CARGO_RADIUS
);
441 for (TileIndex tile
: ta
) {
442 if (IsTileType(tile
, MP_HOUSE
)) {
443 AddAcceptedCargo(tile
, town_cargo_accepted
, nullptr);
447 /* Check if the town can accept this cargo. */
448 if (town_cargo_accepted
[cid
] < 8) return false;
450 dst
= dst_town
->index
;
454 case SourceType::Industry
: {
455 /* Select a random industry. */
456 const Industry
*dst_ind
= Industry::GetRandom();
457 if (dst_ind
== nullptr) return false;
459 /* The industry must accept the cargo */
460 if (!dst_ind
->IsCargoAccepted(cid
)) return false;
462 dst
= dst_ind
->index
;
466 default: NOT_REACHED();
469 /* Check that the source and the destination are not the same. */
470 if (src_type
== dst_type
&& src
== dst
) return false;
472 /* Check distance between source and destination. */
473 if (!CheckSubsidyDistance(src_type
, src
, dst_type
, dst
)) return false;
475 /* Avoid duplicate subsidies. */
476 if (CheckSubsidyDuplicate(cid
, src_type
, src
, dst_type
, dst
)) return false;
478 CreateSubsidy(cid
, src_type
, src
, dst_type
, dst
);
483 /** Perform the economy monthly update of open subsidies, and try to create a new one. */
484 static IntervalTimer
<TimerGameEconomy
> _economy_subsidies_monthly({TimerGameEconomy::MONTH
, TimerGameEconomy::Priority::SUBSIDY
}, [](auto)
486 bool modified
= false;
488 for (Subsidy
*s
: Subsidy::Iterate()) {
489 if (--s
->remaining
== 0) {
490 if (!s
->IsAwarded()) {
491 std::pair
<NewsReferenceType
, NewsReferenceType
> reftype
= SetupSubsidyDecodeParam(s
, SubsidyDecodeParamType::NewsWithdrawn
);
492 AddNewsItem(STR_NEWS_OFFER_OF_SUBSIDY_EXPIRED
, NT_SUBSIDIES
, NF_NORMAL
, reftype
.first
, s
->src
, reftype
.second
, s
->dst
);
493 AI::BroadcastNewEvent(new ScriptEventSubsidyOfferExpired(s
->index
));
494 Game::NewEvent(new ScriptEventSubsidyOfferExpired(s
->index
));
496 if (s
->awarded
== _local_company
) {
497 std::pair
<NewsReferenceType
, NewsReferenceType
> reftype
= SetupSubsidyDecodeParam(s
, SubsidyDecodeParamType::NewsWithdrawn
);
498 AddNewsItem(STR_NEWS_SUBSIDY_WITHDRAWN_SERVICE
, NT_SUBSIDIES
, NF_NORMAL
, reftype
.first
, s
->src
, reftype
.second
, s
->dst
);
500 AI::BroadcastNewEvent(new ScriptEventSubsidyExpired(s
->index
));
501 Game::NewEvent(new ScriptEventSubsidyExpired(s
->index
));
509 RebuildSubsidisedSourceAndDestinationCache();
510 } else if (_settings_game
.difficulty
.subsidy_duration
== 0) {
511 /* If subsidy duration is set to 0, subsidies are disabled, so bail out. */
513 } else if (_settings_game
.linkgraph
.distribution_pax
!= DT_MANUAL
&&
514 _settings_game
.linkgraph
.distribution_mail
!= DT_MANUAL
&&
515 _settings_game
.linkgraph
.distribution_armoured
!= DT_MANUAL
&&
516 _settings_game
.linkgraph
.distribution_default
!= DT_MANUAL
) {
517 /* Return early if there are no manually distributed cargoes and if we
518 * don't need to invalidate the subsidies window. */
522 bool passenger_subsidy
= false;
523 bool town_subsidy
= false;
524 bool industry_subsidy
= false;
526 int random_chance
= RandomRange(16);
528 if (random_chance
< 2 && _settings_game
.linkgraph
.distribution_pax
== DT_MANUAL
) {
529 /* There is a 1/8 chance each month of generating a passenger subsidy. */
533 passenger_subsidy
= FindSubsidyPassengerRoute();
534 } while (!passenger_subsidy
&& n
--);
535 } else if (random_chance
== 2) {
536 /* Cargo subsidies with a town as a source have a 1/16 chance. */
540 town_subsidy
= FindSubsidyTownCargoRoute();
541 } while (!town_subsidy
&& n
--);
542 } else if (random_chance
== 3) {
543 /* Cargo subsidies with an industry as a source have a 1/16 chance. */
547 industry_subsidy
= FindSubsidyIndustryCargoRoute();
548 } while (!industry_subsidy
&& n
--);
551 modified
|= passenger_subsidy
|| town_subsidy
|| industry_subsidy
;
553 if (modified
) InvalidateWindowData(WC_SUBSIDIES_LIST
, 0);
557 * Tests whether given delivery is subsidised and possibly awards the subsidy to delivering company
558 * @param cargo_type type of cargo
559 * @param company company delivering the cargo
560 * @param src_type type of \a src
561 * @param src index of source
562 * @param st station where the cargo is delivered to
563 * @return is the delivery subsidised?
565 bool CheckSubsidised(CargoID cargo_type
, CompanyID company
, SourceType src_type
, SourceID src
, const Station
*st
)
567 /* If the source isn't subsidised, don't continue */
568 if (src
== INVALID_SOURCE
) return false;
570 case SourceType::Industry
:
571 if (!(Industry::Get(src
)->part_of_subsidy
& POS_SRC
)) return false;
573 case SourceType::Town
:
574 if (!(Town::Get(src
)->cache
.part_of_subsidy
& POS_SRC
)) return false;
576 default: return false;
579 /* Remember all towns near this station (at least one house in its catchment radius)
580 * which are destination of subsidised path. Do that only if needed */
581 std::vector
<const Town
*> towns_near
;
582 if (!st
->rect
.IsEmpty()) {
583 for (const Subsidy
*s
: Subsidy::Iterate()) {
584 /* Don't create the cache if there is no applicable subsidy with town as destination */
585 if (s
->dst_type
!= SourceType::Town
) continue;
586 if (s
->cargo_type
!= cargo_type
|| s
->src_type
!= src_type
|| s
->src
!= src
) continue;
587 if (s
->IsAwarded() && s
->awarded
!= company
) continue;
589 BitmapTileIterator
it(st
->catchment_tiles
);
590 for (TileIndex tile
= it
; tile
!= INVALID_TILE
; tile
= ++it
) {
591 if (!IsTileType(tile
, MP_HOUSE
)) continue;
592 const Town
*t
= Town::GetByTile(tile
);
593 if (t
->cache
.part_of_subsidy
& POS_DST
) include(towns_near
, t
);
599 bool subsidised
= false;
601 /* Check if there's a (new) subsidy that applies. There can be more subsidies triggered by this delivery!
602 * Think about the case that subsidies are A->B and A->C and station has both B and C in its catchment area */
603 for (Subsidy
*s
: Subsidy::Iterate()) {
604 if (s
->cargo_type
== cargo_type
&& s
->src_type
== src_type
&& s
->src
== src
&& (!s
->IsAwarded() || s
->awarded
== company
)) {
605 switch (s
->dst_type
) {
606 case SourceType::Industry
:
607 for (const auto &i
: st
->industries_near
) {
608 if (s
->dst
== i
.industry
->index
) {
609 assert(i
.industry
->part_of_subsidy
& POS_DST
);
611 if (!s
->IsAwarded()) s
->AwardTo(company
);
615 case SourceType::Town
:
616 for (const Town
*tp
: towns_near
) {
617 if (s
->dst
== tp
->index
) {
618 assert(tp
->cache
.part_of_subsidy
& POS_DST
);
620 if (!s
->IsAwarded()) s
->AwardTo(company
);