OpenTTD Source: src/subsidy.cpp Source File

OpenTTD Source 14.0-beta3

subsidy.cpp
Go to the documentation of this file.
1 /*
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/>.
6 */
7
10 #include "stdafx.h"
11 #include "company_func.h"
12 #include "industry.h"
13 #include "town.h"
14 #include "news_func.h"
15 #include "ai/ai.hpp"
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"
27 #include "tile_cmd.h"
28 #include "subsidy_cmd.h"
29 #include "timer/timer.h"
30 #include "timer/timer_game_economy.h"
31
32 #include "table/strings.h"
33
34 #include "safeguards.h"
35
36 SubsidyPool _subsidy_pool("Subsidy");
37 INSTANTIATE_POOL_METHODS(Subsidy)
38
39
43 void Subsidy::AwardTo(CompanyID company)
44 {
45 assert(!this->IsAwarded());
46
47 this->awarded = company;
48 this->remaining = _settings_game.difficulty.subsidy_duration * CalendarTime::MONTHS_IN_YEAR;
49
50 SetDParam(0, company);
51 NewsStringData *company_name = new NewsStringData(GetString(STR_COMPANY_NAME));
52
53 /* Add a news item */
54 std::pair<NewsReferenceType, NewsReferenceType> reftype = SetupSubsidyDecodeParam(this, SubsidyDecodeParamType::NewsAwarded, 1);
55
56 SetDParamStr(0, company_name->string);
57 AddNewsItem(
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,
61 company_name
62 );
63 AI::BroadcastNewEvent(new ScriptEventSubsidyAwarded(this->index));
64 Game::NewEvent(new ScriptEventSubsidyAwarded(this->index));
65
66 InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
67 }
68
76 std::pair<NewsReferenceType, NewsReferenceType> SetupSubsidyDecodeParam(const Subsidy *s, SubsidyDecodeParamType mode, uint parameter_offset)
77 {
78 NewsReferenceType reftype1 = NR_NONE;
79 NewsReferenceType reftype2 = NR_NONE;
80
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);
84
85 switch (s->src_type) {
86 case SourceType::Industry:
87 reftype1 = NR_INDUSTRY;
88 SetDParam(parameter_offset + 1, STR_INDUSTRY_NAME);
89 break;
90 case SourceType::Town:
91 reftype1 = NR_TOWN;
92 SetDParam(parameter_offset + 1, STR_TOWN_NAME);
93 break;
94 default: NOT_REACHED();
95 }
96 SetDParam(parameter_offset + 2, s->src);
97
98 switch (s->dst_type) {
99 case SourceType::Industry:
100 reftype2 = NR_INDUSTRY;
101 SetDParam(parameter_offset + 4, STR_INDUSTRY_NAME);
102 break;
103 case SourceType::Town:
104 reftype2 = NR_TOWN;
105 SetDParam(parameter_offset + 4, STR_TOWN_NAME);
106 break;
107 default: NOT_REACHED();
108 }
109 SetDParam(parameter_offset + 5, s->dst);
110
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);
114 }
115
116 return std::pair<NewsReferenceType, NewsReferenceType>(reftype1, reftype2);
117 }
118
125 static inline void SetPartOfSubsidyFlag(SourceType type, SourceID index, PartOfSubsidy flag)
126 {
127 switch (type) {
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();
131 }
132 }
133
135 void RebuildSubsidisedSourceAndDestinationCache()
136 {
137 for (Town *t : Town::Iterate()) t->cache.part_of_subsidy = POS_NONE;
138
139 for (Industry *i : Industry::Iterate()) i->part_of_subsidy = POS_NONE;
140
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);
144 }
145 }
146
152 void DeleteSubsidyWith(SourceType type, SourceID index)
153 {
154 bool dirty = false;
155
156 for (Subsidy *s : Subsidy::Iterate()) {
157 if ((s->src_type == type && s->src == index) || (s->dst_type == type && s->dst == index)) {
158 delete s;
159 dirty = true;
160 }
161 }
162
163 if (dirty) {
164 InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
165 RebuildSubsidisedSourceAndDestinationCache();
166 }
167 }
168
178 static bool CheckSubsidyDuplicate(CargoID cargo, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
179 {
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) {
184 return true;
185 }
186 }
187 return false;
188 }
189
198 static bool CheckSubsidyDistance(SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
199 {
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;
202
203 return (DistanceManhattan(tile_src, tile_dst) <= SUBSIDY_MAX_DISTANCE);
204 }
205
214 void CreateSubsidy(CargoID cid, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
215 {
216 Subsidy *s = new Subsidy();
217 s->cargo_type = cid;
218 s->src_type = src_type;
219 s->src = src;
220 s->dst_type = dst_type;
221 s->dst = dst;
222 s->remaining = SUBSIDY_OFFER_MONTHS;
223 s->awarded = INVALID_COMPANY;
224
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));
231
232 InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
233 }
234
245 CommandCost CmdCreateSubsidy(DoCommandFlag flags, CargoID cid, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
246 {
247 if (!Subsidy::CanAllocateItem()) return CMD_ERROR;
248
249 if (_current_company != OWNER_DEITY) return CMD_ERROR;
250
251 if (cid >= NUM_CARGO || !::CargoSpec::Get(cid)->IsValid()) return CMD_ERROR;
252
253 switch (src_type) {
254 case SourceType::Town:
255 if (!Town::IsValidID(src)) return CMD_ERROR;
256 break;
257 case SourceType::Industry:
258 if (!Industry::IsValidID(src)) return CMD_ERROR;
259 break;
260 default:
261 return CMD_ERROR;
262 }
263 switch (dst_type) {
264 case SourceType::Town:
265 if (!Town::IsValidID(dst)) return CMD_ERROR;
266 break;
267 case SourceType::Industry:
268 if (!Industry::IsValidID(dst)) return CMD_ERROR;
269 break;
270 default:
271 return CMD_ERROR;
272 }
273
274 if (flags & DC_EXEC) {
275 CreateSubsidy(cid, src_type, src, dst_type, dst);
276 }
277
278 return CommandCost();
279 }
280
285 bool FindSubsidyPassengerRoute()
286 {
287 if (!Subsidy::CanAllocateItem()) return false;
288
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();
292
293 const Town *src = Town::GetRandom();
294 if (src->cache.population < SUBSIDY_PAX_MIN_POPULATION ||
295 src->GetPercentTransported(cid) > SUBSIDY_MAX_PCT_TRANSPORTED) {
296 return false;
297 }
298
299 const Town *dst = Town::GetRandom();
300 if (dst->cache.population < SUBSIDY_PAX_MIN_POPULATION || src == dst) {
301 return false;
302 }
303
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;
306
307 CreateSubsidy(cid, SourceType::Town, src->index, SourceType::Town, dst->index);
308
309 return true;
310 }
311
312 bool FindSubsidyCargoDestination(CargoID cid, SourceType src_type, SourceID src);
313
314
319 bool FindSubsidyTownCargoRoute()
320 {
321 if (!Subsidy::CanAllocateItem()) return false;
322
323 SourceType src_type = SourceType::Town;
324
325 /* Select a random town. */
326 const Town *src_town = Town::GetRandom();
327 if (src_town->cache.population < SUBSIDY_CARGO_MIN_POPULATION) return false;
328
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);
335 }
336 }
337
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;
341 }
342
343 uint8_t cargo_count = town_cargo_produced.GetCount();
344
345 /* No cargo produced at all? */
346 if (cargo_count == 0) return false;
347
348 /* Choose a random cargo that is produced in the town. */
349 uint8_t cargo_number = RandomRange(cargo_count);
350 CargoID cid;
351 for (cid = 0; cid < NUM_CARGO; cid++) {
352 if (town_cargo_produced[cid] > 0) {
353 if (cargo_number == 0) break;
354 cargo_number--;
355 }
356 }
357
358 /* Avoid using invalid NewGRF cargoes. */
359 if (!CargoSpec::Get(cid)->IsValid() ||
360 _settings_game.linkgraph.GetDistributionType(cid) != DT_MANUAL) {
361 return false;
362 }
363
364 /* Quit if the percentage transported is large enough. */
365 if (src_town->GetPercentTransported(cid) > SUBSIDY_MAX_PCT_TRANSPORTED) return false;
366
367 SourceID src = src_town->index;
368
369 return FindSubsidyCargoDestination(cid, src_type, src);
370 }
371
376 bool FindSubsidyIndustryCargoRoute()
377 {
378 if (!Subsidy::CanAllocateItem()) return false;
379
380 SourceType src_type = SourceType::Industry;
381
382 /* Select a random industry. */
383 const Industry *src_ind = Industry::GetRandom();
384 if (src_ind == nullptr) return false;
385
386 uint trans, total;
387
388 CargoID cid;
389
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;
394
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;
399 }
400 assert(it != std::end(src_ind->produced)); // indicates loop didn't end as intended
401
402 cid = it->cargo;
403 trans = it->history[LAST_MONTH].PctTransported();
404 total = it->history[LAST_MONTH].production;
405
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) {
412 return false;
413 }
414
415 SourceID src = src_ind->index;
416
417 return FindSubsidyCargoDestination(cid, src_type, src);
418 }
419
427 bool FindSubsidyCargoDestination(CargoID cid, SourceType src_type, SourceID src)
428 {
429 /* Choose a random destination. */
430 SourceType dst_type = Chance16(1, 2) ? SourceType::Town : SourceType::Industry;
431
432 SourceID dst;
433 switch (dst_type) {
434 case SourceType::Town: {
435 /* Select a random town. */
436 const Town *dst_town = Town::GetRandom();
437
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);
444 }
445 }
446
447 /* Check if the town can accept this cargo. */
448 if (town_cargo_accepted[cid] < 8) return false;
449
450 dst = dst_town->index;
451 break;
452 }
453
454 case SourceType::Industry: {
455 /* Select a random industry. */
456 const Industry *dst_ind = Industry::GetRandom();
457 if (dst_ind == nullptr) return false;
458
459 /* The industry must accept the cargo */
460 if (!dst_ind->IsCargoAccepted(cid)) return false;
461
462 dst = dst_ind->index;
463 break;
464 }
465
466 default: NOT_REACHED();
467 }
468
469 /* Check that the source and the destination are not the same. */
470 if (src_type == dst_type && src == dst) return false;
471
472 /* Check distance between source and destination. */
473 if (!CheckSubsidyDistance(src_type, src, dst_type, dst)) return false;
474
475 /* Avoid duplicate subsidies. */
476 if (CheckSubsidyDuplicate(cid, src_type, src, dst_type, dst)) return false;
477
478 CreateSubsidy(cid, src_type, src, dst_type, dst);
479
480 return true;
481 }
482
484 static IntervalTimer<TimerGameEconomy> _economy_subsidies_monthly({TimerGameEconomy::MONTH, TimerGameEconomy::Priority::SUBSIDY}, [](auto)
485 {
486 bool modified = false;
487
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));
495 } else {
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);
499 }
500 AI::BroadcastNewEvent(new ScriptEventSubsidyExpired(s->index));
501 Game::NewEvent(new ScriptEventSubsidyExpired(s->index));
502 }
503 delete s;
504 modified = true;
505 }
506 }
507
508 if (modified) {
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. */
512 return;
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. */
519 return;
520 }
521
522 bool passenger_subsidy = false;
523 bool town_subsidy = false;
524 bool industry_subsidy = false;
525
526 int random_chance = RandomRange(16);
527
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. */
530 int n = 1000;
531
532 do {
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. */
537 int n = 1000;
538
539 do {
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. */
544 int n = 1000;
545
546 do {
547 industry_subsidy = FindSubsidyIndustryCargoRoute();
548 } while (!industry_subsidy && n--);
549 }
550
551 modified |= passenger_subsidy || town_subsidy || industry_subsidy;
552
553 if (modified) InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
554 });
555
565 bool CheckSubsidised(CargoID cargo_type, CompanyID company, SourceType src_type, SourceID src, const Station *st)
566 {
567 /* If the source isn't subsidised, don't continue */
568 if (src == INVALID_SOURCE) return false;
569 switch (src_type) {
570 case SourceType::Industry:
571 if (!(Industry::Get(src)->part_of_subsidy & POS_SRC)) return false;
572 break;
573 case SourceType::Town:
574 if (!(Town::Get(src)->cache.part_of_subsidy & POS_SRC)) return false;
575 break;
576 default: return false;
577 }
578
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;
588
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);
594 }
595 break;
596 }
597 }
598
599 bool subsidised = false;
600
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);
610 subsidised = true;
611 if (!s->IsAwarded()) s->AwardTo(company);
612 }
613 }
614 break;
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);
619 subsidised = true;
620 if (!s->IsAwarded()) s->AwardTo(company);
621 }
622 }
623 break;
624 default:
625 NOT_REACHED();
626 }
627 }
628 }
629
630 return subsidised;
631 }
game.hpp
MP_HOUSE
@ MP_HOUSE
A house by a town.
Definition: tile_type.h:51
InvalidateWindowData
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
Mark window data of the window of a given class and specific window number as invalid (in need of re-...
Definition: window.cpp:3200
CheckSubsidyDuplicate
static bool CheckSubsidyDuplicate(CargoID cargo, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
Check whether a specific subsidy already exists.
Definition: subsidy.cpp:178
Pool::PoolItem<&_industry_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:335
Subsidy::remaining
uint16_t remaining
Remaining months when this subsidy is valid.
Definition: subsidy_base.h:24
command_func.h
CMD_ERROR
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:28
SubsidyDecodeParamType::NewsAwarded
@ NewsAwarded
News item for an awarded subsidy.
WC_SUBSIDIES_LIST
@ WC_SUBSIDIES_LIST
Subsidies list; Window numbers:
Definition: window_type.h:260
Station
Station data structure.
Definition: station_base.h:442
BitmapTileIterator
Iterator to iterate over all tiles belonging to a bitmaptilearea.
Definition: bitmap_type.h:106
IntervalTimer
An interval timer will fire every interval, and will continue to fire until it is deleted.
Definition: timer.h:76
SUBSIDY_OFFER_MONTHS
static const uint SUBSIDY_OFFER_MONTHS
Constants related to subsidies.
Definition: subsidy_base.h:54
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:234
CargoSpec::Get
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo ID.
Definition: cargotype.h:131
LinkGraphSettings::distribution_mail
DistributionType distribution_mail
distribution type for mail
Definition: settings_type.h:571
SUBSIDY_MAX_PCT_TRANSPORTED
static const uint SUBSIDY_MAX_PCT_TRANSPORTED
Subsidy will be created only for towns/industries with less % transported.
Definition: subsidy_base.h:57
CargoArray
Class for storing amounts of cargo.
Definition: cargo_type.h:110
DT_MANUAL
@ DT_MANUAL
Manual distribution. No link graph calculations are run.
Definition: linkgraph_type.h:25
SubsidyDecodeParamType::NewsOffered
@ NewsOffered
News item for an offered subsidy.
GameSettings::difficulty
DifficultySettings difficulty
settings related to the difficulty
Definition: settings_type.h:618
INVALID_TILE
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:95
TownCache::part_of_subsidy
PartOfSubsidy part_of_subsidy
Is this town a source/destination of a subsidy?
Definition: town.h:44
Industry::produced
ProducedCargoArray produced
INDUSTRY_NUM_OUTPUTS production cargo slots.
Definition: industry.h:99
Town::xy
TileIndex xy
town center tile
Definition: town.h:51
CargoSpec
Specification of a cargo type.
Definition: cargotype.h:68
town.h
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
Industry
Defines the internal data of a functional industry.
Definition: industry.h:68
Chance16
bool Chance16(const uint a, const uint b)
Flips a coin with given probability.
Definition: random_func.hpp:131
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
DC_EXEC
@ DC_EXEC
execute the given command
Definition: command_type.h:371
SetupSubsidyDecodeParam
std::pair< NewsReferenceType, NewsReferenceType > SetupSubsidyDecodeParam(const Subsidy *s, SubsidyDecodeParamType mode, uint parameter_offset)
Setup the string parameters for printing the subsidy at the screen, and compute the news reference fo...
Definition: subsidy.cpp:76
DoCommandFlag
DoCommandFlag
List of flags for a command.
Definition: command_type.h:369
Town::GetRandom
static Town * GetRandom()
Return a random valid town.
Definition: town_cmd.cpp:191
FindSubsidyTownCargoRoute
bool FindSubsidyTownCargoRoute()
Tries to create a cargo subsidy with a town as source.
Definition: subsidy.cpp:319
RandomRange
static uint32_t RandomRange(uint32_t limit)
Pick a random number between 0 and limit - 1, inclusive.
Definition: random_func.hpp:81
ai.hpp
NR_NONE
@ NR_NONE
Empty reference.
Definition: news_type.h:53
tile_cmd.h
NR_TOWN
@ NR_TOWN
Reference town. Scroll to town when clicking on the news.
Definition: news_type.h:58
include
bool include(Container &container, typename Container::const_reference &item)
Helper function to append an item to a container if it is not already contained.
Definition: container_func.hpp:24
Industry::GetRandom
static Industry * GetRandom()
Return a random valid industry.
Definition: industry_cmd.cpp:221
DistanceManhattan
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
Definition: map.cpp:159
SUBSIDY_CARGO_MIN_POPULATION
static const uint SUBSIDY_CARGO_MIN_POPULATION
Min. population of destination town for cargo route.
Definition: subsidy_base.h:56
Subsidy::cargo_type
CargoID cargo_type
Cargo type involved in this subsidy, INVALID_CARGO for invalid subsidy.
Definition: subsidy_base.h:23
Subsidy
Struct about subsidies, offered and awarded.
Definition: subsidy_base.h:22
DifficultySettings::subsidy_multiplier
byte subsidy_multiplier
payment multiplier for subsidized deliveries
Definition: settings_type.h:108
SUBSIDY_TOWN_CARGO_RADIUS
static const uint SUBSIDY_TOWN_CARGO_RADIUS
Extent of a tile area around town center when scanning for town cargo acceptance and production (6 ~=...
Definition: subsidy_base.h:59
CommandCost
Common return value for all commands.
Definition: command_type.h:23
DifficultySettings::subsidy_duration
uint16_t subsidy_duration
duration of subsidies
Definition: settings_type.h:109
FindSubsidyIndustryCargoRoute
bool FindSubsidyIndustryCargoRoute()
Tries to create a cargo subsidy with an industry as source.
Definition: subsidy.cpp:376
BaseStation::rect
StationRect rect
NOSAVE: Station spread out rectangle maintained by StationRect::xxx() functions.
Definition: base_station_base.h:90
_economy_subsidies_monthly
static IntervalTimer< TimerGameEconomy > _economy_subsidies_monthly({TimerGameEconomy::MONTH, TimerGameEconomy::Priority::SUBSIDY}, [](auto) { bool modified=false;for(Subsidy *s :Subsidy::Iterate()) { if(--s->remaining==0) { if(!s->IsAwarded()) { std::pair< NewsReferenceType, NewsReferenceType > reftype=SetupSubsidyDecodeParam(s, SubsidyDecodeParamType::NewsWithdrawn);AddNewsItem(STR_NEWS_OFFER_OF_SUBSIDY_EXPIRED, NT_SUBSIDIES, NF_NORMAL, reftype.first, s->src, reftype.second, s->dst);AI::BroadcastNewEvent(new ScriptEventSubsidyOfferExpired(s->index));Game::NewEvent(new ScriptEventSubsidyOfferExpired(s->index));} else { if(s->awarded==_local_company) { std::pair< NewsReferenceType, NewsReferenceType > reftype=SetupSubsidyDecodeParam(s, SubsidyDecodeParamType::NewsWithdrawn);AddNewsItem(STR_NEWS_SUBSIDY_WITHDRAWN_SERVICE, NT_SUBSIDIES, NF_NORMAL, reftype.first, s->src, reftype.second, s->dst);} AI::BroadcastNewEvent(new ScriptEventSubsidyExpired(s->index));Game::NewEvent(new ScriptEventSubsidyExpired(s->index));} delete s;modified=true;} } if(modified) { RebuildSubsidisedSourceAndDestinationCache();} else if(_settings_game.difficulty.subsidy_duration==0) { return;} else if(_settings_game.linkgraph.distribution_pax !=DT_MANUAL &&_settings_game.linkgraph.distribution_mail !=DT_MANUAL &&_settings_game.linkgraph.distribution_armoured !=DT_MANUAL &&_settings_game.linkgraph.distribution_default !=DT_MANUAL) { return;} bool passenger_subsidy=false;bool town_subsidy=false;bool industry_subsidy=false;int random_chance=RandomRange(16);if(random_chance< 2 &&_settings_game.linkgraph.distribution_pax==DT_MANUAL) { int n=1000;do { passenger_subsidy=FindSubsidyPassengerRoute();} while(!passenger_subsidy &&n--);} else if(random_chance==2) { int n=1000;do { town_subsidy=FindSubsidyTownCargoRoute();} while(!town_subsidy &&n--);} else if(random_chance==3) { int n=1000;do { industry_subsidy=FindSubsidyIndustryCargoRoute();} while(!industry_subsidy &&n--);} modified|=passenger_subsidy||town_subsidy||industry_subsidy;if(modified) InvalidateWindowData(WC_SUBSIDIES_LIST, 0);})
Perform the economy monthly update of open subsidies, and try to create a new one.
CargoSpec::town_production_cargoes
static std::array< std::vector< const CargoSpec * >, NUM_TPE > town_production_cargoes
List of cargo specs for each Town Product Effect.
Definition: cargotype.h:190
Subsidy::src
SourceID src
Index of source. Either TownID or IndustryID.
Definition: subsidy_base.h:28
NewsReferenceType
NewsReferenceType
References to objects in news.
Definition: news_type.h:52
SetPartOfSubsidyFlag
static void SetPartOfSubsidyFlag(SourceType type, SourceID index, PartOfSubsidy flag)
Sets a flag indicating that given town/industry is part of subsidised route.
Definition: subsidy.cpp:125
OrthogonalTileArea
Represents the covered area of e.g.
Definition: tilearea_type.h:18
SourceID
uint16_t SourceID
Contains either industry ID, town ID or company ID (or INVALID_SOURCE)
Definition: cargo_type.h:139
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:55
NewsStringData
Container for a single string to be passed as NewsAllocatedData.
Definition: news_type.h:150
DeleteSubsidyWith
void DeleteSubsidyWith(SourceType type, SourceID index)
Delete the subsidies associated with a given cargo source type and id.
Definition: subsidy.cpp:152
Game::NewEvent
static void NewEvent(class ScriptEvent *event)
Queue a new event for a Game Script.
Definition: game_core.cpp:147
_local_company
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:49
AI::BroadcastNewEvent
static void BroadcastNewEvent(ScriptEvent *event, CompanyID skip_company=MAX_COMPANIES)
Broadcast a new event to all active AIs.
Definition: ai_core.cpp:260
industry.h
safeguards.h
_subsidy_pool
SubsidyPool _subsidy_pool("Subsidy")
Pool for the subsidies.
timer.h
Subsidy::dst
SourceID dst
Index of destination. Either TownID or IndustryID.
Definition: subsidy_base.h:29
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
GameSettings::linkgraph
LinkGraphSettings linkgraph
settings for link graph calculations
Definition: settings_type.h:629
NT_SUBSIDIES
@ NT_SUBSIDIES
News about subsidies (announcements, expirations, acceptance)
Definition: news_type.h:38
SourceType::Industry
@ Industry
Source/destination is an industry.
subsidy_cmd.h
stdafx.h
NF_NORMAL
@ NF_NORMAL
Normal news item. (Newspaper with text only)
Definition: news_type.h:81
SourceType::Town
@ Town
Source/destination is a town.
SubsidyDecodeParamType::NewsWithdrawn
@ NewsWithdrawn
News item for a subsidy offer withdrawn, or expired subsidy.
TownCache::population
uint32_t population
Current population of people.
Definition: town.h:42
string_func.h
Subsidy::dst_type
SourceType dst_type
Destination of subsidised path (SourceType::Industry or SourceType::Town)
Definition: subsidy_base.h:27
Station::industries_near
IndustryList industries_near
Cached list of industries near the station that can accept cargo,.
Definition: station_base.h:474
_current_company
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:50
station_base.h
SourceType
SourceType
Types of cargo source and destination.
Definition: cargo_type.h:133
Pool::PoolItem<&_town_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:384
strings_func.h
Pool
Base class for all pools.
Definition: pool_type.hpp:80
NR_INDUSTRY
@ NR_INDUSTRY
Reference industry. Scroll to industry when clicking on the news. Delete news when industry is delete...
Definition: news_type.h:57
subsidy_func.h
RebuildSubsidisedSourceAndDestinationCache
void RebuildSubsidisedSourceAndDestinationCache()
Perform a full rebuild of the subsidies cache.
Definition: subsidy.cpp:135
TimerGameConst< struct Calendar >::MONTHS_IN_YEAR
static constexpr int MONTHS_IN_YEAR
months per year
Definition: timer_game_common.h:151
SUBSIDY_MAX_DISTANCE
static const uint SUBSIDY_MAX_DISTANCE
Max. length of subsidised route (DistanceManhattan)
Definition: subsidy_base.h:58
SetDParam
void SetDParam(size_t n, uint64_t v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings.cpp:104
CmdCreateSubsidy
CommandCost CmdCreateSubsidy(DoCommandFlag flags, CargoID cid, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
Create a new subsidy.
Definition: subsidy.cpp:245
AddNewsItem
void AddNewsItem(StringID string, NewsType type, NewsFlag flags, NewsReferenceType reftype1=NR_NONE, uint32_t ref1=UINT32_MAX, NewsReferenceType reftype2=NR_NONE, uint32_t ref2=UINT32_MAX, const NewsAllocatedData *data=nullptr)
Add a new newsitem to be shown.
Definition: news_gui.cpp:827
GetString
std::string GetString(StringID string)
Resolve the given StringID into a std::string with all the associated DParam lookups and formatting.
Definition: strings.cpp:327
Town::cache
TownCache cache
Container for all cacheable data.
Definition: town.h:53
Pool::PoolItem<&_subsidy_pool >::CanAllocateItem
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
Definition: pool_type.hpp:305
INVALID_SOURCE
static const SourceID INVALID_SOURCE
Invalid/unknown index of source.
Definition: cargo_type.h:140
POS_NONE
@ POS_NONE
nothing
Definition: subsidy_type.h:17
subsidy_base.h
SetDParamStr
void SetDParamStr(size_t n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:352
CargoSpec::name
StringID name
Name of this type of cargo.
Definition: cargotype.h:85
Station::catchment_tiles
BitmapTileArea catchment_tiles
NOSAVE: Set of individual tiles covered by catchment area.
Definition: station_base.h:462
OWNER_DEITY
@ OWNER_DEITY
The object is owned by a superuser / goal script.
Definition: company_type.h:27
PartOfSubsidy
PartOfSubsidy
What part of a subsidy is something?
Definition: subsidy_type.h:16
container_func.hpp
company_func.h
FindSubsidyCargoDestination
bool FindSubsidyCargoDestination(CargoID cid, SourceType src_type, SourceID src)
Tries to find a suitable destination for the given source and cargo.
Definition: subsidy.cpp:427
INSTANTIATE_POOL_METHODS
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
Definition: pool_func.hpp:225
TileArea
OrthogonalTileArea TileArea
Shorthand for the much more common orthogonal tile area.
Definition: tilearea_type.h:102
window_func.h
Town
Town data structure.
Definition: town.h:50
CreateSubsidy
void CreateSubsidy(CargoID cid, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
Creates a subsidy with the given parameters.
Definition: subsidy.cpp:214
random_func.hpp
INVALID_COMPANY
@ INVALID_COMPANY
An invalid company.
Definition: company_type.h:30
SubsidyDecodeParamType
SubsidyDecodeParamType
Types of subsidy news messages, which determine how the date is printed and whether to use singular o...
Definition: subsidy_base.h:62
NewsStringData::string
std::string string
The string to retain.
Definition: news_type.h:151
IsValidCargoID
bool IsValidCargoID(CargoID t)
Test whether cargo type is not INVALID_CARGO.
Definition: cargo_type.h:103
OrthogonalTileArea::Expand
OrthogonalTileArea & Expand(int rad)
Expand a tile area by rad tiles in each direction, keeping within map bounds.
Definition: tilearea.cpp:123
Industry::IsCargoAccepted
bool IsCargoAccepted() const
Test if this industry accepts any cargo.
Definition: industry.h:179
IsTileType
static debug_inline bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
Pool::PoolItem<&_town_pool >::IsValidID
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:324
LinkGraphSettings::distribution_armoured
DistributionType distribution_armoured
distribution type for armoured cargo class
Definition: settings_type.h:572
TPE_PASSENGERS
@ TPE_PASSENGERS
Cargo behaves passenger-like for production.
Definition: cargotype.h:36
CheckSubsidised
bool CheckSubsidised(CargoID cargo_type, CompanyID company, SourceType src_type, SourceID src, const Station *st)
Tests whether given delivery is subsidised and possibly awards the subsidy to delivering company.
Definition: subsidy.cpp:565
NUM_CARGO
static const CargoID NUM_CARGO
Maximum number of cargo types in a game.
Definition: cargo_type.h:74
pool_func.hpp
Subsidy::src_type
SourceType src_type
Source of subsidised path (SourceType::Industry or SourceType::Town)
Definition: subsidy_base.h:26
Subsidy::awarded
CompanyID awarded
Subsidy is awarded to this company; INVALID_COMPANY if it's not awarded to anyone.
Definition: subsidy_base.h:25
CheckSubsidyDistance
static bool CheckSubsidyDistance(SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
Checks if the source and destination of a subsidy are inside the distance limit.
Definition: subsidy.cpp:198
LinkGraphSettings::distribution_pax
DistributionType distribution_pax
distribution type for passengers
Definition: settings_type.h:570
timer_game_economy.h
POS_DST
@ POS_DST
bit 1 set -> town/industry is destination of subsidised path
Definition: subsidy_type.h:19
LinkGraphSettings::distribution_default
DistributionType distribution_default
distribution type for all other goods
Definition: settings_type.h:573
POS_SRC
@ POS_SRC
bit 0 set -> town/industry is source of subsidised path
Definition: subsidy_type.h:18
SUBSIDY_PAX_MIN_POPULATION
static const uint SUBSIDY_PAX_MIN_POPULATION
Min. population of towns for subsidised pax route.
Definition: subsidy_base.h:55
news_func.h
FindSubsidyPassengerRoute
bool FindSubsidyPassengerRoute()
Tries to create a passenger subsidy between two towns.
Definition: subsidy.cpp:285