OpenTTD Source: src/subsidy.cpp Source File

OpenTTD Source 12.2

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 "game/game.hpp"
24 #include "command_func.h"
25 #include "string_func.h"
26 #include "tile_cmd.h"
27
28 #include "table/strings.h"
29
30 #include "safeguards.h"
31
32 SubsidyPool _subsidy_pool("Subsidy");
33 INSTANTIATE_POOL_METHODS(Subsidy)
34
35
39 void Subsidy::AwardTo(CompanyID company)
40 {
41 assert(!this->IsAwarded());
42
43 this->awarded = company;
44 this->remaining = _settings_game.difficulty.subsidy_duration * MONTHS_IN_YEAR;
45
46 SetDParam(0, company);
47 NewsStringData *company_name = new NewsStringData(GetString(STR_COMPANY_NAME));
48
49 /* Add a news item */
50 std::pair<NewsReferenceType, NewsReferenceType> reftype = SetupSubsidyDecodeParam(this, SubsidyDecodeParamType::NewsAwarded, 1);
51
52 SetDParamStr(0, company_name->string);
53 AddNewsItem(
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,
57 company_name
58 );
59 AI::BroadcastNewEvent(new ScriptEventSubsidyAwarded(this->index));
60 Game::NewEvent(new ScriptEventSubsidyAwarded(this->index));
61
62 InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
63 }
64
72 std::pair<NewsReferenceType, NewsReferenceType> SetupSubsidyDecodeParam(const Subsidy *s, SubsidyDecodeParamType mode, uint parameter_offset)
73 {
74 NewsReferenceType reftype1 = NR_NONE;
75 NewsReferenceType reftype2 = NR_NONE;
76
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);
80
81 switch (s->src_type) {
82 case ST_INDUSTRY:
83 reftype1 = NR_INDUSTRY;
84 SetDParam(parameter_offset + 1, STR_INDUSTRY_NAME);
85 break;
86 case ST_TOWN:
87 reftype1 = NR_TOWN;
88 SetDParam(parameter_offset + 1, STR_TOWN_NAME);
89 break;
90 default: NOT_REACHED();
91 }
92 SetDParam(parameter_offset + 2, s->src);
93
94 switch (s->dst_type) {
95 case ST_INDUSTRY:
96 reftype2 = NR_INDUSTRY;
97 SetDParam(parameter_offset + 4, STR_INDUSTRY_NAME);
98 break;
99 case ST_TOWN:
100 reftype2 = NR_TOWN;
101 SetDParam(parameter_offset + 4, STR_TOWN_NAME);
102 break;
103 default: NOT_REACHED();
104 }
105 SetDParam(parameter_offset + 5, s->dst);
106
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);
110 }
111
112 return std::pair<NewsReferenceType, NewsReferenceType>(reftype1, reftype2);
113 }
114
121 static inline void SetPartOfSubsidyFlag(SourceType type, SourceID index, PartOfSubsidy flag)
122 {
123 switch (type) {
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();
127 }
128 }
129
131 void RebuildSubsidisedSourceAndDestinationCache()
132 {
133 for (Town *t : Town::Iterate()) t->cache.part_of_subsidy = POS_NONE;
134
135 for (Industry *i : Industry::Iterate()) i->part_of_subsidy = POS_NONE;
136
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);
140 }
141 }
142
148 void DeleteSubsidyWith(SourceType type, SourceID index)
149 {
150 bool dirty = false;
151
152 for (Subsidy *s : Subsidy::Iterate()) {
153 if ((s->src_type == type && s->src == index) || (s->dst_type == type && s->dst == index)) {
154 delete s;
155 dirty = true;
156 }
157 }
158
159 if (dirty) {
160 InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
161 RebuildSubsidisedSourceAndDestinationCache();
162 }
163 }
164
174 static bool CheckSubsidyDuplicate(CargoID cargo, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
175 {
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) {
180 return true;
181 }
182 }
183 return false;
184 }
185
194 static bool CheckSubsidyDistance(SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
195 {
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;
198
199 return (DistanceManhattan(tile_src, tile_dst) <= SUBSIDY_MAX_DISTANCE);
200 }
201
210 void CreateSubsidy(CargoID cid, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
211 {
212 Subsidy *s = new Subsidy();
213 s->cargo_type = cid;
214 s->src_type = src_type;
215 s->src = src;
216 s->dst_type = dst_type;
217 s->dst = dst;
218 s->remaining = SUBSIDY_OFFER_MONTHS;
219 s->awarded = INVALID_COMPANY;
220
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));
227
228 InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
229 }
230
245 CommandCost CmdCreateSubsidy(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const std::string &text)
246 {
247 if (!Subsidy::CanAllocateItem()) return CMD_ERROR;
248
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);
254
255 if (_current_company != OWNER_DEITY) return CMD_ERROR;
256
257 if (cid >= NUM_CARGO || !::CargoSpec::Get(cid)->IsValid()) return CMD_ERROR;
258
259 switch (src_type) {
260 case ST_TOWN:
261 if (!Town::IsValidID(src)) return CMD_ERROR;
262 break;
263 case ST_INDUSTRY:
264 if (!Industry::IsValidID(src)) return CMD_ERROR;
265 break;
266 default:
267 return CMD_ERROR;
268 }
269 switch (dst_type) {
270 case ST_TOWN:
271 if (!Town::IsValidID(dst)) return CMD_ERROR;
272 break;
273 case ST_INDUSTRY:
274 if (!Industry::IsValidID(dst)) return CMD_ERROR;
275 break;
276 default:
277 return CMD_ERROR;
278 }
279
280 if (flags & DC_EXEC) {
281 CreateSubsidy(cid, src_type, src, dst_type, dst);
282 }
283
284 return CommandCost();
285 }
286
291 bool FindSubsidyPassengerRoute()
292 {
293 if (!Subsidy::CanAllocateItem()) return false;
294
295 const Town *src = Town::GetRandom();
296 if (src->cache.population < SUBSIDY_PAX_MIN_POPULATION ||
297 src->GetPercentTransported(CT_PASSENGERS) > SUBSIDY_MAX_PCT_TRANSPORTED) {
298 return false;
299 }
300
301 const Town *dst = Town::GetRandom();
302 if (dst->cache.population < SUBSIDY_PAX_MIN_POPULATION || src == dst) {
303 return false;
304 }
305
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;
308
309 CreateSubsidy(CT_PASSENGERS, ST_TOWN, src->index, ST_TOWN, dst->index);
310
311 return true;
312 }
313
314 bool FindSubsidyCargoDestination(CargoID cid, SourceType src_type, SourceID src);
315
316
321 bool FindSubsidyTownCargoRoute()
322 {
323 if (!Subsidy::CanAllocateItem()) return false;
324
325 SourceType src_type = ST_TOWN;
326
327 /* Select a random town. */
328 const Town *src_town = Town::GetRandom();
329 if (src_town->cache.population < SUBSIDY_CARGO_MIN_POPULATION) return false;
330
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);
337 }
338 }
339
340 /* Passenger subsidies are not handled here. */
341 town_cargo_produced[CT_PASSENGERS] = 0;
342
343 uint8 cargo_count = 0;
344 for (CargoID i = 0; i < NUM_CARGO; i++) {
345 if (town_cargo_produced[i] > 0) cargo_count++;
346 }
347
348 /* No cargo produced at all? */
349 if (cargo_count == 0) return false;
350
351 /* Choose a random cargo that is produced in the town. */
352 uint8 cargo_number = RandomRange(cargo_count);
353 CargoID cid;
354 for (cid = 0; cid < NUM_CARGO; cid++) {
355 if (town_cargo_produced[cid] > 0) {
356 if (cargo_number == 0) break;
357 cargo_number--;
358 }
359 }
360
361 /* Avoid using invalid NewGRF cargoes. */
362 if (!CargoSpec::Get(cid)->IsValid() ||
363 _settings_game.linkgraph.GetDistributionType(cid) != DT_MANUAL) {
364 return false;
365 }
366
367 /* Quit if the percentage transported is large enough. */
368 if (src_town->GetPercentTransported(cid) > SUBSIDY_MAX_PCT_TRANSPORTED) return false;
369
370 SourceID src = src_town->index;
371
372 return FindSubsidyCargoDestination(cid, src_type, src);
373 }
374
379 bool FindSubsidyIndustryCargoRoute()
380 {
381 if (!Subsidy::CanAllocateItem()) return false;
382
383 SourceType src_type = ST_INDUSTRY;
384
385 /* Select a random industry. */
386 const Industry *src_ind = Industry::GetRandom();
387 if (src_ind == nullptr) return false;
388
389 uint trans, total;
390
391 CargoID cid;
392
393 /* Randomize cargo type */
394 int num_cargos = 0;
395 uint cargo_index;
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++;
398 }
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;
404 }
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];
409
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 ||
414 cid == CT_INVALID ||
415 _settings_game.linkgraph.GetDistributionType(cid) != DT_MANUAL) {
416 return false;
417 }
418
419 SourceID src = src_ind->index;
420
421 return FindSubsidyCargoDestination(cid, src_type, src);
422 }
423
431 bool FindSubsidyCargoDestination(CargoID cid, SourceType src_type, SourceID src)
432 {
433 /* Choose a random destination. */
434 SourceType dst_type = Chance16(1, 2) ? ST_TOWN : ST_INDUSTRY;
435
436 SourceID dst;
437 switch (dst_type) {
438 case ST_TOWN: {
439 /* Select a random town. */
440 const Town *dst_town = Town::GetRandom();
441
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);
448 }
449 }
450
451 /* Check if the town can accept this cargo. */
452 if (town_cargo_accepted[cid] < 8) return false;
453
454 dst = dst_town->index;
455 break;
456 }
457
458 case ST_INDUSTRY: {
459 /* Select a random industry. */
460 const Industry *dst_ind = Industry::GetRandom();
461 if (dst_ind == nullptr) return false;
462
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;
466
467 dst = dst_ind->index;
468 break;
469 }
470
471 default: NOT_REACHED();
472 }
473
474 /* Check that the source and the destination are not the same. */
475 if (src_type == dst_type && src == dst) return false;
476
477 /* Check distance between source and destination. */
478 if (!CheckSubsidyDistance(src_type, src, dst_type, dst)) return false;
479
480 /* Avoid duplicate subsidies. */
481 if (CheckSubsidyDuplicate(cid, src_type, src, dst_type, dst)) return false;
482
483 CreateSubsidy(cid, src_type, src, dst_type, dst);
484
485 return true;
486 }
487
489 void SubsidyMonthlyLoop()
490 {
491 bool modified = false;
492
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));
500 } else {
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);
504 }
505 AI::BroadcastNewEvent(new ScriptEventSubsidyExpired(s->index));
506 Game::NewEvent(new ScriptEventSubsidyExpired(s->index));
507 }
508 delete s;
509 modified = true;
510 }
511 }
512
513 if (modified) {
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. */
517 return;
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. */
524 return;
525 }
526
527 bool passenger_subsidy = false;
528 bool town_subsidy = false;
529 bool industry_subsidy = false;
530
531 int random_chance = RandomRange(16);
532
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. */
535 int n = 1000;
536
537 do {
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. */
542 int n = 1000;
543
544 do {
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. */
549 int n = 1000;
550
551 do {
552 industry_subsidy = FindSubsidyIndustryCargoRoute();
553 } while (!industry_subsidy && n--);
554 }
555
556 modified |= passenger_subsidy || town_subsidy || industry_subsidy;
557
558 if (modified) InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
559 }
560
570 bool CheckSubsidised(CargoID cargo_type, CompanyID company, SourceType src_type, SourceID src, const Station *st)
571 {
572 /* If the source isn't subsidised, don't continue */
573 if (src == INVALID_SOURCE) return false;
574 switch (src_type) {
575 case ST_INDUSTRY:
576 if (!(Industry::Get(src)->part_of_subsidy & POS_SRC)) return false;
577 break;
578 case ST_TOWN:
579 if (!(Town::Get(src)->cache.part_of_subsidy & POS_SRC)) return false;
580 break;
581 default: return false;
582 }
583
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;
593
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);
599 }
600 break;
601 }
602 }
603
604 bool subsidised = false;
605
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) {
611 case ST_INDUSTRY:
612 for (Industry *ind : st->industries_near) {
613 if (s->dst == ind->index) {
614 assert(ind->part_of_subsidy & POS_DST);
615 subsidised = true;
616 if (!s->IsAwarded()) s->AwardTo(company);
617 }
618 }
619 break;
620 case ST_TOWN:
621 for (const Town *tp : towns_near) {
622 if (s->dst == tp->index) {
623 assert(tp->cache.part_of_subsidy & POS_DST);
624 subsidised = true;
625 if (!s->IsAwarded()) s->AwardTo(company);
626 }
627 }
628 break;
629 default:
630 NOT_REACHED();
631 }
632 }
633 }
634
635 return subsidised;
636 }
game.hpp
Subsidy::remaining
uint16 remaining
Remaining months when this subsidy is valid.
Definition: subsidy_base.h:24
MP_HOUSE
@ MP_HOUSE
A house by a town.
Definition: tile_type.h:49
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:83
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:3234
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:174
Chance16
static bool Chance16(const uint a, const uint b)
Flips a coin with given probability.
Definition: random_func.hpp:131
Pool::PoolItem<&_industry_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:337
Industry::part_of_subsidy
PartOfSubsidy part_of_subsidy
NOSAVE: is this industry a source/destination of a subsidy?
Definition: industry.h:90
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
DifficultySettings::subsidy_duration
uint16 subsidy_duration
duration of subsidies
Definition: settings_type.h:85
command_func.h
CMD_ERROR
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:23
NT_SUBSIDIES
@ NT_SUBSIDIES
News about subsidies (announcements, expirations, acceptance)
Definition: news_type.h:35
SubsidyDecodeParamType::NewsAwarded
@ NewsAwarded
News item for an awarded subsidy.
WC_SUBSIDIES_LIST
@ WC_SUBSIDIES_LIST
Subsidies list; Window numbers:
Definition: window_type.h:252
Station
Station data structure.
Definition: station_base.h:447
BitmapTileIterator
Iterator to iterate over all tiles belonging to a bitmaptilearea.
Definition: bitmap_type.h:107
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:235
CargoSpec::Get
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo ID.
Definition: cargotype.h:119
LinkGraphSettings::distribution_mail
DistributionType distribution_mail
distribution type for mail
Definition: settings_type.h:530
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:82
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:576
Industry::last_month_production
uint16 last_month_production[INDUSTRY_NUM_OUTPUTS]
total units produced per cargo in the last full month
Definition: industry.h:79
AddNewsItem
void AddNewsItem(StringID string, NewsType type, NewsFlag flags, NewsReferenceType reftype1=NR_NONE, uint32 ref1=UINT32_MAX, NewsReferenceType reftype2=NR_NONE, uint32 ref2=UINT32_MAX, const NewsAllocatedData *data=nullptr)
Add a new newsitem to be shown.
Definition: news_gui.cpp:807
include
bool include(std::vector< T > &vec, const T &item)
Helper function to append an item to a vector if it is not already contained Consider using std::set,...
Definition: smallvec_type.hpp:27
valid
uint8 valid
Bits indicating what variable is valid (for each bit, 0 is invalid, 1 is valid).
Definition: newgrf_station.cpp:248
TownCache::part_of_subsidy
PartOfSubsidy part_of_subsidy
Is this town a source/destination of a subsidy?
Definition: town.h:44
Town::xy
TileIndex xy
town center tile
Definition: town.h:51
CargoSpec
Specification of a cargo type.
Definition: cargotype.h:57
town.h
RandomRange
static uint32 RandomRange(uint32 limit)
Pick a random number between 0 and limit - 1, inclusive.
Definition: random_func.hpp:81
Industry
Defines the internal data of a functional industry.
Definition: industry.h:66
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
DC_EXEC
@ DC_EXEC
execute the given command
Definition: command_type.h:348
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:72
SetDParam
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:196
DoCommandFlag
DoCommandFlag
List of flags for a command.
Definition: command_type.h:346
Town::GetRandom
static Town * GetRandom()
Return a random valid town.
Definition: town_cmd.cpp:184
FindSubsidyTownCargoRoute
bool FindSubsidyTownCargoRoute()
Tries to create a cargo subsidy with a town as source.
Definition: subsidy.cpp:321
ST_TOWN
@ ST_TOWN
Source/destination is a town.
Definition: cargo_type.h:149
ai.hpp
tile_cmd.h
CmdCreateSubsidy
CommandCost CmdCreateSubsidy(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const std::string &text)
Create a new subsidy.
Definition: subsidy.cpp:245
Industry::GetRandom
static Industry * GetRandom()
Return a random valid industry.
Definition: industry_cmd.cpp:218
DistanceManhattan
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
Definition: map.cpp:157
TownCache::population
uint32 population
Current population of people.
Definition: town.h:42
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, CT_INVALID 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:84
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
SourceID
uint16 SourceID
Contains either industry ID, town ID or company ID (or INVALID_SOURCE)
Definition: cargo_type.h:153
FindSubsidyIndustryCargoRoute
bool FindSubsidyIndustryCargoRoute()
Tries to create a cargo subsidy with an industry as source.
Definition: subsidy.cpp:379
BaseStation::rect
StationRect rect
NOSAVE: Station spread out rectangle maintained by StationRect::xxx() functions.
Definition: base_station_base.h:76
Subsidy::src
SourceID src
Index of source. Either TownID or IndustryID.
Definition: subsidy_base.h:28
Industry::produced_cargo
CargoID produced_cargo[INDUSTRY_NUM_OUTPUTS]
16 production cargo slots
Definition: industry.h:70
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:121
ST_INDUSTRY
@ ST_INDUSTRY
Source/destination is an industry.
Definition: cargo_type.h:148
OrthogonalTileArea
Represents the covered area of e.g.
Definition: tilearea_type.h:18
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:53
NewsStringData
Container for a single string to be passed as NewsAllocatedData.
Definition: news_type.h:146
DeleteSubsidyWith
void DeleteSubsidyWith(SourceType type, SourceID index)
Delete the subsidies associated with a given cargo source type and id.
Definition: subsidy.cpp:148
Game::NewEvent
static void NewEvent(class ScriptEvent *event)
Queue a new event for a Game Script.
Definition: game_core.cpp:141
_local_company
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:46
AI::BroadcastNewEvent
static void BroadcastNewEvent(ScriptEvent *event, CompanyID skip_company=MAX_COMPANIES)
Broadcast a new event to all active AIs.
Definition: ai_core.cpp:259
industry.h
safeguards.h
_subsidy_pool
SubsidyPool _subsidy_pool("Subsidy")
Pool for the subsidies.
Subsidy::dst
SourceID dst
Index of destination. Either TownID or IndustryID.
Definition: subsidy_base.h:29
MONTHS_IN_YEAR
static const int MONTHS_IN_YEAR
months per year
Definition: date_type.h:31
GameSettings::linkgraph
LinkGraphSettings linkgraph
settings for link graph calculations
Definition: settings_type.h:587
stdafx.h
NR_NONE
@ NR_NONE
Empty reference.
Definition: news_type.h:50
NF_NORMAL
@ NF_NORMAL
Normal news item. (Newspaper with text only)
Definition: news_type.h:78
IsTileType
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
SubsidyDecodeParamType::NewsWithdrawn
@ NewsWithdrawn
News item for a subsidy offer withdrawn, or expired subsidy.
Industry::last_month_pct_transported
byte last_month_pct_transported[INDUSTRY_NUM_OUTPUTS]
percentage transported per cargo in the last full month
Definition: industry.h:78
NewsReferenceType
NewsReferenceType
References to objects in news.
Definition: news_type.h:49
string_func.h
NR_TOWN
@ NR_TOWN
Reference town. Scroll to town when clicking on the news.
Definition: news_type.h:55
Subsidy::dst_type
SourceType dst_type
Destination of subsidised path (ST_INDUSTRY or ST_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:479
_current_company
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
station_base.h
SourceType
SourceType
Types of cargo source and destination.
Definition: cargo_type.h:147
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:386
strings_func.h
Pool
Base class for all pools.
Definition: pool_type.hpp:81
subsidy_func.h
RebuildSubsidisedSourceAndDestinationCache
void RebuildSubsidisedSourceAndDestinationCache()
Perform a full rebuild of the subsidies cache.
Definition: subsidy.cpp:131
SUBSIDY_MAX_DISTANCE
static const uint SUBSIDY_MAX_DISTANCE
Max. length of subsidised route (DistanceManhattan)
Definition: subsidy_base.h:58
endof
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:386
NUM_CARGO
@ NUM_CARGO
Maximal number of cargo types in a game.
Definition: cargo_type.h:65
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:307
INVALID_SOURCE
static const SourceID INVALID_SOURCE
Invalid/unknown index of source.
Definition: cargo_type.h:154
POS_NONE
@ POS_NONE
nothing
Definition: subsidy_type.h:17
subsidy_base.h
CargoSpec::name
StringID name
Name of this type of cargo.
Definition: cargotype.h:72
Station::catchment_tiles
BitmapTileArea catchment_tiles
NOSAVE: Set of individual tiles covered by catchment area.
Definition: station_base.h:467
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
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:431
INSTANTIATE_POOL_METHODS
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
Definition: pool_func.hpp:224
TileArea
OrthogonalTileArea TileArea
Shorthand for the much more common orthogonal tile area.
Definition: tilearea_type.h:102
window_func.h
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:378
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:210
random_func.hpp
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
INVALID_TILE
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:88
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:147
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::accepts_cargo
CargoID accepts_cargo[INDUSTRY_NUM_INPUTS]
16 input cargo slots
Definition: industry.h:75
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:326
LinkGraphSettings::distribution_armoured
DistributionType distribution_armoured
distribution type for armoured cargo class
Definition: settings_type.h:531
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:570
pool_func.hpp
CT_INVALID
@ CT_INVALID
Invalid cargo type.
Definition: cargo_type.h:69
Subsidy::src_type
SourceType src_type
Source of subsidised path (ST_INDUSTRY or ST_TOWN)
Definition: subsidy_base.h:26
NR_INDUSTRY
@ NR_INDUSTRY
Reference industry. Scroll to industry when clicking on the news. Delete news when industry is delete...
Definition: news_type.h:54
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:194
LinkGraphSettings::distribution_pax
DistributionType distribution_pax
distribution type for passengers
Definition: settings_type.h:529
SetDParamStr
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:296
SubsidyMonthlyLoop
void SubsidyMonthlyLoop()
Perform the monthly update of open subsidies, and try to create a new one.
Definition: subsidy.cpp:489
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:532
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:291