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
= SUBSIDY_CONTRACT_MONTHS
;
46 char company_name
[MAX_LENGTH_COMPANY_NAME_CHARS
* MAX_CHAR_LENGTH
];
47 SetDParam(0, company
);
48 GetString(company_name
, STR_COMPANY_NAME
, lastof(company_name
));
50 char *cn
= stredup(company_name
);
53 Pair reftype
= SetupSubsidyDecodeParam(this, false);
58 STR_NEWS_SERVICE_SUBSIDY_AWARDED_HALF
+ _settings_game
.difficulty
.subsidy_multiplier
,
59 NT_SUBSIDIES
, NF_NORMAL
,
60 (NewsReferenceType
)reftype
.a
, this->src
, (NewsReferenceType
)reftype
.b
, 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 Unit of cargo used, \c true means general name, \c false means singular form.
73 * @return Reference of the subsidy in the news system.
75 Pair
SetupSubsidyDecodeParam(const Subsidy
*s
, bool mode
)
77 NewsReferenceType reftype1
= NR_NONE
;
78 NewsReferenceType reftype2
= NR_NONE
;
80 /* if mode is false, use the singular form */
81 const CargoSpec
*cs
= CargoSpec::Get(s
->cargo_type
);
82 SetDParam(0, mode
? cs
->name
: cs
->name_single
);
84 switch (s
->src_type
) {
86 reftype1
= NR_INDUSTRY
;
87 SetDParam(1, STR_INDUSTRY_NAME
);
91 SetDParam(1, STR_TOWN_NAME
);
93 default: NOT_REACHED();
97 switch (s
->dst_type
) {
99 reftype2
= NR_INDUSTRY
;
100 SetDParam(4, STR_INDUSTRY_NAME
);
104 SetDParam(4, STR_TOWN_NAME
);
106 default: NOT_REACHED();
108 SetDParam(5, s
->dst
);
117 * Sets a flag indicating that given town/industry is part of subsidised route.
118 * @param type is it a town or an industry?
119 * @param index index of town/industry
120 * @param flag flag to set
122 static inline void SetPartOfSubsidyFlag(SourceType type
, SourceID index
, PartOfSubsidy flag
)
125 case ST_INDUSTRY
: Industry::Get(index
)->part_of_subsidy
|= flag
; return;
126 case ST_TOWN
: Town::Get(index
)->cache
.part_of_subsidy
|= flag
; return;
127 default: NOT_REACHED();
131 /** Perform a full rebuild of the subsidies cache. */
132 void RebuildSubsidisedSourceAndDestinationCache()
134 for (Town
*t
: Town::Iterate()) t
->cache
.part_of_subsidy
= POS_NONE
;
136 for (Industry
*i
: Industry::Iterate()) i
->part_of_subsidy
= POS_NONE
;
138 for (const Subsidy
*s
: Subsidy::Iterate()) {
139 SetPartOfSubsidyFlag(s
->src_type
, s
->src
, POS_SRC
);
140 SetPartOfSubsidyFlag(s
->dst_type
, s
->dst
, POS_DST
);
145 * Delete the subsidies associated with a given cargo source type and id.
146 * @param type Cargo source type of the id.
147 * @param index Id to remove.
149 void DeleteSubsidyWith(SourceType type
, SourceID index
)
153 for (Subsidy
*s
: Subsidy::Iterate()) {
154 if ((s
->src_type
== type
&& s
->src
== index
) || (s
->dst_type
== type
&& s
->dst
== index
)) {
161 InvalidateWindowData(WC_SUBSIDIES_LIST
, 0);
162 RebuildSubsidisedSourceAndDestinationCache();
167 * Check whether a specific subsidy already exists.
168 * @param cargo Cargo type.
169 * @param src_type Type of source of the cargo, affects interpretation of \a src.
170 * @param src Id of the source.
171 * @param dst_type Type of the destination of the cargo, affects interpretation of \a dst.
172 * @param dst Id of the destination.
173 * @return \c true if the subsidy already exists, \c false if not.
175 static bool CheckSubsidyDuplicate(CargoID cargo
, SourceType src_type
, SourceID src
, SourceType dst_type
, SourceID dst
)
177 for (const Subsidy
*s
: Subsidy::Iterate()) {
178 if (s
->cargo_type
== cargo
&&
179 s
->src_type
== src_type
&& s
->src
== src
&&
180 s
->dst_type
== dst_type
&& s
->dst
== dst
) {
188 * Checks if the source and destination of a subsidy are inside the distance limit.
189 * @param src_type Type of \a src.
190 * @param src Index of source.
191 * @param dst_type Type of \a dst.
192 * @param dst Index of destination.
193 * @return True if they are inside the distance limit.
195 static bool CheckSubsidyDistance(SourceType src_type
, SourceID src
, SourceType dst_type
, SourceID dst
)
197 TileIndex tile_src
= (src_type
== ST_TOWN
) ? Town::Get(src
)->xy
: Industry::Get(src
)->location
.tile
;
198 TileIndex tile_dst
= (dst_type
== ST_TOWN
) ? Town::Get(dst
)->xy
: Industry::Get(dst
)->location
.tile
;
200 return (DistanceManhattan(tile_src
, tile_dst
) <= SUBSIDY_MAX_DISTANCE
);
204 * Creates a subsidy with the given parameters.
205 * @param cid Subsidised cargo.
206 * @param src_type Type of \a src.
207 * @param src Index of source.
208 * @param dst_type Type of \a dst.
209 * @param dst Index of destination.
211 void CreateSubsidy(CargoID cid
, SourceType src_type
, SourceID src
, SourceType dst_type
, SourceID dst
)
213 Subsidy
*s
= new Subsidy();
215 s
->src_type
= src_type
;
217 s
->dst_type
= dst_type
;
219 s
->remaining
= SUBSIDY_OFFER_MONTHS
;
220 s
->awarded
= INVALID_COMPANY
;
222 Pair reftype
= SetupSubsidyDecodeParam(s
, false);
223 AddNewsItem(STR_NEWS_SERVICE_SUBSIDY_OFFERED
, NT_SUBSIDIES
, NF_NORMAL
, (NewsReferenceType
)reftype
.a
, s
->src
, (NewsReferenceType
)reftype
.b
, s
->dst
);
224 SetPartOfSubsidyFlag(s
->src_type
, s
->src
, POS_SRC
);
225 SetPartOfSubsidyFlag(s
->dst_type
, s
->dst
, POS_DST
);
226 AI::BroadcastNewEvent(new ScriptEventSubsidyOffer(s
->index
));
227 Game::NewEvent(new ScriptEventSubsidyOffer(s
->index
));
229 InvalidateWindowData(WC_SUBSIDIES_LIST
, 0);
233 * Create a new subsidy.
234 * @param tile unused.
235 * @param flags type of operation
236 * @param p1 various bitstuffed elements
237 * - p1 = (bit 0 - 7) - SourceType of source.
238 * - p1 = (bit 8 - 23) - SourceID of source.
239 * - p1 = (bit 24 - 31) - CargoID of subsidy.
240 * @param p2 various bitstuffed elements
241 * - p2 = (bit 0 - 7) - SourceType of destination.
242 * - p2 = (bit 8 - 23) - SourceID of destination.
243 * @param text unused.
244 * @return the cost of this operation or an error
246 CommandCost
CmdCreateSubsidy(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
248 if (!Subsidy::CanAllocateItem()) return CMD_ERROR
;
250 CargoID cid
= GB(p1
, 24, 8);
251 SourceType src_type
= (SourceType
)GB(p1
, 0, 8);
252 SourceID src
= GB(p1
, 8, 16);
253 SourceType dst_type
= (SourceType
)GB(p2
, 0, 8);
254 SourceID dst
= GB(p2
, 8, 16);
256 if (_current_company
!= OWNER_DEITY
) return CMD_ERROR
;
258 if (cid
>= NUM_CARGO
|| !::CargoSpec::Get(cid
)->IsValid()) return CMD_ERROR
;
262 if (!Town::IsValidID(src
)) return CMD_ERROR
;
265 if (!Industry::IsValidID(src
)) return CMD_ERROR
;
272 if (!Town::IsValidID(dst
)) return CMD_ERROR
;
275 if (!Industry::IsValidID(dst
)) return CMD_ERROR
;
281 if (flags
& DC_EXEC
) {
282 CreateSubsidy(cid
, src_type
, src
, dst_type
, dst
);
285 return CommandCost();
289 * Tries to create a passenger subsidy between two towns.
290 * @return True iff the subsidy was created.
292 bool FindSubsidyPassengerRoute()
294 if (!Subsidy::CanAllocateItem()) return false;
296 const Town
*src
= Town::GetRandom();
297 if (src
->cache
.population
< SUBSIDY_PAX_MIN_POPULATION
||
298 src
->GetPercentTransported(CT_PASSENGERS
) > SUBSIDY_MAX_PCT_TRANSPORTED
) {
302 const Town
*dst
= Town::GetRandom();
303 if (dst
->cache
.population
< SUBSIDY_PAX_MIN_POPULATION
|| src
== dst
) {
307 if (DistanceManhattan(src
->xy
, dst
->xy
) > SUBSIDY_MAX_DISTANCE
) return false;
308 if (CheckSubsidyDuplicate(CT_PASSENGERS
, ST_TOWN
, src
->index
, ST_TOWN
, dst
->index
)) return false;
310 CreateSubsidy(CT_PASSENGERS
, ST_TOWN
, src
->index
, ST_TOWN
, dst
->index
);
315 bool FindSubsidyCargoDestination(CargoID cid
, SourceType src_type
, SourceID src
);
319 * Tries to create a cargo subsidy with a town as source.
320 * @return True iff the subsidy was created.
322 bool FindSubsidyTownCargoRoute()
324 if (!Subsidy::CanAllocateItem()) return false;
326 SourceType src_type
= ST_TOWN
;
328 /* Select a random town. */
329 const Town
*src_town
= Town::GetRandom();
330 if (src_town
->cache
.population
< SUBSIDY_CARGO_MIN_POPULATION
) return false;
332 /* Calculate the produced cargo of houses around town center. */
333 CargoArray town_cargo_produced
;
334 TileArea ta
= TileArea(src_town
->xy
, 1, 1).Expand(SUBSIDY_TOWN_CARGO_RADIUS
);
335 TILE_AREA_LOOP(tile
, ta
) {
336 if (IsTileType(tile
, MP_HOUSE
)) {
337 AddProducedCargo(tile
, town_cargo_produced
);
341 /* Passenger subsidies are not handled here. */
342 town_cargo_produced
[CT_PASSENGERS
] = 0;
344 uint8 cargo_count
= 0;
345 for (CargoID i
= 0; i
< NUM_CARGO
; i
++) {
346 if (town_cargo_produced
[i
] > 0) cargo_count
++;
349 /* No cargo produced at all? */
350 if (cargo_count
== 0) return false;
352 /* Choose a random cargo that is produced in the town. */
353 uint8 cargo_number
= RandomRange(cargo_count
);
355 for (cid
= 0; cid
< NUM_CARGO
; cid
++) {
356 if (town_cargo_produced
[cid
] > 0) {
357 if (cargo_number
== 0) break;
362 /* Avoid using invalid NewGRF cargoes. */
363 if (!CargoSpec::Get(cid
)->IsValid() ||
364 _settings_game
.linkgraph
.GetDistributionType(cid
) != DT_MANUAL
) {
368 /* Quit if the percentage transported is large enough. */
369 if (src_town
->GetPercentTransported(cid
) > SUBSIDY_MAX_PCT_TRANSPORTED
) return false;
371 SourceID src
= src_town
->index
;
373 return FindSubsidyCargoDestination(cid
, src_type
, src
);
377 * Tries to create a cargo subsidy with an industry as source.
378 * @return True iff the subsidy was created.
380 bool FindSubsidyIndustryCargoRoute()
382 if (!Subsidy::CanAllocateItem()) return false;
384 SourceType src_type
= ST_INDUSTRY
;
386 /* Select a random industry. */
387 const Industry
*src_ind
= Industry::GetRandom();
388 if (src_ind
== nullptr) return false;
394 /* Randomize cargo type */
397 for (cargo_index
= 0; cargo_index
< lengthof(src_ind
->produced_cargo
); cargo_index
++) {
398 if (src_ind
->produced_cargo
[cargo_index
] != CT_INVALID
) num_cargos
++;
400 if (num_cargos
== 0) return false; // industry produces nothing
401 int cargo_num
= RandomRange(num_cargos
) + 1;
402 for (cargo_index
= 0; cargo_index
< lengthof(src_ind
->produced_cargo
); cargo_index
++) {
403 if (src_ind
->produced_cargo
[cargo_index
] != CT_INVALID
) cargo_num
--;
404 if (cargo_num
== 0) break;
406 assert(cargo_num
== 0); // indicates loop didn't break as intended
407 cid
= src_ind
->produced_cargo
[cargo_index
];
408 trans
= src_ind
->last_month_pct_transported
[cargo_index
];
409 total
= src_ind
->last_month_production
[cargo_index
];
411 /* Quit if no production in this industry
412 * or if the pct transported is already large enough
413 * or if the cargo is automatically distributed */
414 if (total
== 0 || trans
> SUBSIDY_MAX_PCT_TRANSPORTED
||
416 _settings_game
.linkgraph
.GetDistributionType(cid
) != DT_MANUAL
) {
420 SourceID src
= src_ind
->index
;
422 return FindSubsidyCargoDestination(cid
, src_type
, src
);
426 * Tries to find a suitable destination for the given source and cargo.
427 * @param cid Subsidized cargo.
428 * @param src_type Type of \a src.
429 * @param src Index of source.
430 * @return True iff the subsidy was created.
432 bool FindSubsidyCargoDestination(CargoID cid
, SourceType src_type
, SourceID src
)
434 /* Choose a random destination. */
435 SourceType dst_type
= Chance16(1, 2) ? ST_TOWN
: ST_INDUSTRY
;
440 /* Select a random town. */
441 const Town
*dst_town
= Town::GetRandom();
443 /* Calculate cargo acceptance of houses around town center. */
444 CargoArray town_cargo_accepted
;
445 TileArea ta
= TileArea(dst_town
->xy
, 1, 1).Expand(SUBSIDY_TOWN_CARGO_RADIUS
);
446 TILE_AREA_LOOP(tile
, ta
) {
447 if (IsTileType(tile
, MP_HOUSE
)) {
448 AddAcceptedCargo(tile
, town_cargo_accepted
, nullptr);
452 /* Check if the town can accept this cargo. */
453 if (town_cargo_accepted
[cid
] < 8) return false;
455 dst
= dst_town
->index
;
460 /* Select a random industry. */
461 const Industry
*dst_ind
= Industry::GetRandom();
462 if (dst_ind
== nullptr) return false;
464 /* The industry must accept the cargo */
465 bool valid
= std::find(dst_ind
->accepts_cargo
, endof(dst_ind
->accepts_cargo
), cid
) != endof(dst_ind
->accepts_cargo
);
466 if (!valid
) return false;
468 dst
= dst_ind
->index
;
472 default: NOT_REACHED();
475 /* Check that the source and the destination are not the same. */
476 if (src_type
== dst_type
&& src
== dst
) return false;
478 /* Check distance between source and destination. */
479 if (!CheckSubsidyDistance(src_type
, src
, dst_type
, dst
)) return false;
481 /* Avoid duplicate subsidies. */
482 if (CheckSubsidyDuplicate(cid
, src_type
, src
, dst_type
, dst
)) return false;
484 CreateSubsidy(cid
, src_type
, src
, dst_type
, dst
);
489 /** Perform the monthly update of open subsidies, and try to create a new one. */
490 void SubsidyMonthlyLoop()
492 bool modified
= false;
494 for (Subsidy
*s
: Subsidy::Iterate()) {
495 if (--s
->remaining
== 0) {
496 if (!s
->IsAwarded()) {
497 Pair reftype
= SetupSubsidyDecodeParam(s
, true);
498 AddNewsItem(STR_NEWS_OFFER_OF_SUBSIDY_EXPIRED
, NT_SUBSIDIES
, NF_NORMAL
, (NewsReferenceType
)reftype
.a
, s
->src
, (NewsReferenceType
)reftype
.b
, s
->dst
);
499 AI::BroadcastNewEvent(new ScriptEventSubsidyOfferExpired(s
->index
));
500 Game::NewEvent(new ScriptEventSubsidyOfferExpired(s
->index
));
502 if (s
->awarded
== _local_company
) {
503 Pair reftype
= SetupSubsidyDecodeParam(s
, true);
504 AddNewsItem(STR_NEWS_SUBSIDY_WITHDRAWN_SERVICE
, NT_SUBSIDIES
, NF_NORMAL
, (NewsReferenceType
)reftype
.a
, s
->src
, (NewsReferenceType
)reftype
.b
, s
->dst
);
506 AI::BroadcastNewEvent(new ScriptEventSubsidyExpired(s
->index
));
507 Game::NewEvent(new ScriptEventSubsidyExpired(s
->index
));
515 RebuildSubsidisedSourceAndDestinationCache();
516 } else if (_settings_game
.linkgraph
.distribution_pax
!= DT_MANUAL
&&
517 _settings_game
.linkgraph
.distribution_mail
!= DT_MANUAL
&&
518 _settings_game
.linkgraph
.distribution_armoured
!= DT_MANUAL
&&
519 _settings_game
.linkgraph
.distribution_default
!= DT_MANUAL
) {
520 /* Return early if there are no manually distributed cargoes and if we
521 * don't need to invalidate the subsidies window. */
525 bool passenger_subsidy
= false;
526 bool town_subsidy
= false;
527 bool industry_subsidy
= false;
529 int random_chance
= RandomRange(16);
531 if (random_chance
< 2 && _settings_game
.linkgraph
.distribution_pax
== DT_MANUAL
) {
532 /* There is a 1/8 chance each month of generating a passenger subsidy. */
536 passenger_subsidy
= FindSubsidyPassengerRoute();
537 } while (!passenger_subsidy
&& n
--);
538 } else if (random_chance
== 2) {
539 /* Cargo subsidies with a town as a source have a 1/16 chance. */
543 town_subsidy
= FindSubsidyTownCargoRoute();
544 } while (!town_subsidy
&& n
--);
545 } else if (random_chance
== 3) {
546 /* Cargo subsidies with an industry as a source have a 1/16 chance. */
550 industry_subsidy
= FindSubsidyIndustryCargoRoute();
551 } while (!industry_subsidy
&& n
--);
554 modified
|= passenger_subsidy
|| town_subsidy
|| industry_subsidy
;
556 if (modified
) InvalidateWindowData(WC_SUBSIDIES_LIST
, 0);
560 * Tests whether given delivery is subsidised and possibly awards the subsidy to delivering company
561 * @param cargo_type type of cargo
562 * @param company company delivering the cargo
563 * @param src_type type of \a src
564 * @param src index of source
565 * @param st station where the cargo is delivered to
566 * @return is the delivery subsidised?
568 bool CheckSubsidised(CargoID cargo_type
, CompanyID company
, SourceType src_type
, SourceID src
, const Station
*st
)
570 /* If the source isn't subsidised, don't continue */
571 if (src
== INVALID_SOURCE
) return false;
574 if (!(Industry::Get(src
)->part_of_subsidy
& POS_SRC
)) return false;
577 if (!(Town::Get(src
)->cache
.part_of_subsidy
& POS_SRC
)) return false;
579 default: return false;
582 /* Remember all towns near this station (at least one house in its catchment radius)
583 * which are destination of subsidised path. Do that only if needed */
584 std::vector
<const Town
*> towns_near
;
585 if (!st
->rect
.IsEmpty()) {
586 for (const Subsidy
*s
: Subsidy::Iterate()) {
587 /* Don't create the cache if there is no applicable subsidy with town as destination */
588 if (s
->dst_type
!= ST_TOWN
) continue;
589 if (s
->cargo_type
!= cargo_type
|| s
->src_type
!= src_type
|| s
->src
!= src
) continue;
590 if (s
->IsAwarded() && s
->awarded
!= company
) continue;
592 BitmapTileIterator
it(st
->catchment_tiles
);
593 for (TileIndex tile
= it
; tile
!= INVALID_TILE
; tile
= ++it
) {
594 if (!IsTileType(tile
, MP_HOUSE
)) continue;
595 const Town
*t
= Town::GetByTile(tile
);
596 if (t
->cache
.part_of_subsidy
& POS_DST
) include(towns_near
, t
);
602 bool subsidised
= false;
604 /* Check if there's a (new) subsidy that applies. There can be more subsidies triggered by this delivery!
605 * Think about the case that subsidies are A->B and A->C and station has both B and C in its catchment area */
606 for (Subsidy
*s
: Subsidy::Iterate()) {
607 if (s
->cargo_type
== cargo_type
&& s
->src_type
== src_type
&& s
->src
== src
&& (!s
->IsAwarded() || s
->awarded
== company
)) {
608 switch (s
->dst_type
) {
610 for (Industry
*ind
: st
->industries_near
) {
611 if (s
->dst
== ind
->index
) {
612 assert(ind
->part_of_subsidy
& POS_DST
);
614 if (!s
->IsAwarded()) s
->AwardTo(company
);
619 for (const Town
*tp
: towns_near
) {
620 if (s
->dst
== tp
->index
) {
621 assert(tp
->cache
.part_of_subsidy
& POS_DST
);
623 if (!s
->IsAwarded()) s
->AwardTo(company
);