OpenTTD Source  1.10.3
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 
27 #include "table/strings.h"
28 
29 #include "safeguards.h"
30 
31 SubsidyPool _subsidy_pool("Subsidy");
33 
34 
38 void Subsidy::AwardTo(CompanyID company)
39 {
40  assert(!this->IsAwarded());
41 
42  this->awarded = company;
43  this->remaining = SUBSIDY_CONTRACT_MONTHS;
44 
46  SetDParam(0, company);
47  GetString(company_name, STR_COMPANY_NAME, lastof(company_name));
48 
49  char *cn = stredup(company_name);
50 
51  /* Add a news item */
52  Pair reftype = SetupSubsidyDecodeParam(this, false);
53  InjectDParam(1);
54 
55  SetDParamStr(0, cn);
57  STR_NEWS_SERVICE_SUBSIDY_AWARDED_HALF + _settings_game.difficulty.subsidy_multiplier,
59  (NewsReferenceType)reftype.a, this->src, (NewsReferenceType)reftype.b, this->dst,
60  cn
61  );
62  AI::BroadcastNewEvent(new ScriptEventSubsidyAwarded(this->index));
63  Game::NewEvent(new ScriptEventSubsidyAwarded(this->index));
64 
66 }
67 
74 Pair SetupSubsidyDecodeParam(const Subsidy *s, bool mode)
75 {
76  NewsReferenceType reftype1 = NR_NONE;
77  NewsReferenceType reftype2 = NR_NONE;
78 
79  /* if mode is false, use the singular form */
80  const CargoSpec *cs = CargoSpec::Get(s->cargo_type);
81  SetDParam(0, mode ? cs->name : cs->name_single);
82 
83  switch (s->src_type) {
84  case ST_INDUSTRY:
85  reftype1 = NR_INDUSTRY;
86  SetDParam(1, STR_INDUSTRY_NAME);
87  break;
88  case ST_TOWN:
89  reftype1 = NR_TOWN;
90  SetDParam(1, STR_TOWN_NAME);
91  break;
92  default: NOT_REACHED();
93  }
94  SetDParam(2, s->src);
95 
96  switch (s->dst_type) {
97  case ST_INDUSTRY:
98  reftype2 = NR_INDUSTRY;
99  SetDParam(4, STR_INDUSTRY_NAME);
100  break;
101  case ST_TOWN:
102  reftype2 = NR_TOWN;
103  SetDParam(4, STR_TOWN_NAME);
104  break;
105  default: NOT_REACHED();
106  }
107  SetDParam(5, s->dst);
108 
109  Pair p;
110  p.a = reftype1;
111  p.b = reftype2;
112  return p;
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 
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 
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) {
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;
220 
221  Pair reftype = SetupSubsidyDecodeParam(s, false);
222  AddNewsItem(STR_NEWS_SERVICE_SUBSIDY_OFFERED, NT_SUBSIDIES, NF_NORMAL, (NewsReferenceType)reftype.a, s->src, (NewsReferenceType)reftype.b, s->dst);
225  AI::BroadcastNewEvent(new ScriptEventSubsidyOffer(s->index));
226  Game::NewEvent(new ScriptEventSubsidyOffer(s->index));
227 
229 }
230 
245 CommandCost CmdCreateSubsidy(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *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 
292 {
293  if (!Subsidy::CanAllocateItem()) return false;
294 
295  const Town *src = Town::GetRandom();
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 
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  CargoArray town_cargo_produced = GetProductionAroundTiles(src_town->xy, 1, 1, SUBSIDY_TOWN_CARGO_RADIUS);
332 
333  /* Passenger subsidies are not handled here. */
334  town_cargo_produced[CT_PASSENGERS] = 0;
335 
336  uint8 cargo_count = 0;
337  for (CargoID i = 0; i < NUM_CARGO; i++) {
338  if (town_cargo_produced[i] > 0) cargo_count++;
339  }
340 
341  /* No cargo produced at all? */
342  if (cargo_count == 0) return false;
343 
344  /* Choose a random cargo that is produced in the town. */
345  uint8 cargo_number = RandomRange(cargo_count);
346  CargoID cid;
347  for (cid = 0; cid < NUM_CARGO; cid++) {
348  if (town_cargo_produced[cid] > 0) {
349  if (cargo_number == 0) break;
350  cargo_number--;
351  }
352  }
353 
354  /* Avoid using invalid NewGRF cargoes. */
355  if (!CargoSpec::Get(cid)->IsValid() ||
356  _settings_game.linkgraph.GetDistributionType(cid) != DT_MANUAL) {
357  return false;
358  }
359 
360  /* Quit if the percentage transported is large enough. */
361  if (src_town->GetPercentTransported(cid) > SUBSIDY_MAX_PCT_TRANSPORTED) return false;
362 
363  SourceID src = src_town->index;
364 
365  return FindSubsidyCargoDestination(cid, src_type, src);
366 }
367 
373 {
374  if (!Subsidy::CanAllocateItem()) return false;
375 
376  SourceType src_type = ST_INDUSTRY;
377 
378  /* Select a random industry. */
379  const Industry *src_ind = Industry::GetRandom();
380  if (src_ind == nullptr) return false;
381 
382  uint trans, total;
383 
384  CargoID cid;
385 
386  /* Randomize cargo type */
387  int num_cargos = 0;
388  uint cargo_index;
389  for (cargo_index = 0; cargo_index < lengthof(src_ind->produced_cargo); cargo_index++) {
390  if (src_ind->produced_cargo[cargo_index] != CT_INVALID) num_cargos++;
391  }
392  if (num_cargos == 0) return false; // industry produces nothing
393  int cargo_num = RandomRange(num_cargos) + 1;
394  for (cargo_index = 0; cargo_index < lengthof(src_ind->produced_cargo); cargo_index++) {
395  if (src_ind->produced_cargo[cargo_index] != CT_INVALID) cargo_num--;
396  if (cargo_num == 0) break;
397  }
398  assert(cargo_num == 0); // indicates loop didn't break as intended
399  cid = src_ind->produced_cargo[cargo_index];
400  trans = src_ind->last_month_pct_transported[cargo_index];
401  total = src_ind->last_month_production[cargo_index];
402 
403  /* Quit if no production in this industry
404  * or if the pct transported is already large enough
405  * or if the cargo is automatically distributed */
406  if (total == 0 || trans > SUBSIDY_MAX_PCT_TRANSPORTED ||
407  cid == CT_INVALID ||
408  _settings_game.linkgraph.GetDistributionType(cid) != DT_MANUAL) {
409  return false;
410  }
411 
412  SourceID src = src_ind->index;
413 
414  return FindSubsidyCargoDestination(cid, src_type, src);
415 }
416 
425 {
426  /* Choose a random destination. */
427  SourceType dst_type = Chance16(1, 2) ? ST_TOWN : ST_INDUSTRY;
428 
429  SourceID dst;
430  switch (dst_type) {
431  case ST_TOWN: {
432  /* Select a random town. */
433  const Town *dst_town = Town::GetRandom();
434  CargoArray town_cargo_accepted = GetAcceptanceAroundTiles(dst_town->xy, 1, 1, SUBSIDY_TOWN_CARGO_RADIUS);
435 
436  /* Check if the town can accept this cargo. */
437  if (town_cargo_accepted[cid] < 8) return false;
438 
439  dst = dst_town->index;
440  break;
441  }
442 
443  case ST_INDUSTRY: {
444  /* Select a random industry. */
445  const Industry *dst_ind = Industry::GetRandom();
446  if (dst_ind == nullptr) return false;
447 
448  /* The industry must accept the cargo */
449  bool valid = std::find(dst_ind->accepts_cargo, endof(dst_ind->accepts_cargo), cid) != endof(dst_ind->accepts_cargo);
450  if (!valid) return false;
451 
452  dst = dst_ind->index;
453  break;
454  }
455 
456  default: NOT_REACHED();
457  }
458 
459  /* Check that the source and the destination are not the same. */
460  if (src_type == dst_type && src == dst) return false;
461 
462  /* Check distance between source and destination. */
463  if (!CheckSubsidyDistance(src_type, src, dst_type, dst)) return false;
464 
465  /* Avoid duplicate subsidies. */
466  if (CheckSubsidyDuplicate(cid, src_type, src, dst_type, dst)) return false;
467 
468  CreateSubsidy(cid, src_type, src, dst_type, dst);
469 
470  return true;
471 }
472 
475 {
476  bool modified = false;
477 
478  for (Subsidy *s : Subsidy::Iterate()) {
479  if (--s->remaining == 0) {
480  if (!s->IsAwarded()) {
481  Pair reftype = SetupSubsidyDecodeParam(s, true);
482  AddNewsItem(STR_NEWS_OFFER_OF_SUBSIDY_EXPIRED, NT_SUBSIDIES, NF_NORMAL, (NewsReferenceType)reftype.a, s->src, (NewsReferenceType)reftype.b, s->dst);
483  AI::BroadcastNewEvent(new ScriptEventSubsidyOfferExpired(s->index));
484  Game::NewEvent(new ScriptEventSubsidyOfferExpired(s->index));
485  } else {
486  if (s->awarded == _local_company) {
487  Pair reftype = SetupSubsidyDecodeParam(s, true);
488  AddNewsItem(STR_NEWS_SUBSIDY_WITHDRAWN_SERVICE, NT_SUBSIDIES, NF_NORMAL, (NewsReferenceType)reftype.a, s->src, (NewsReferenceType)reftype.b, s->dst);
489  }
490  AI::BroadcastNewEvent(new ScriptEventSubsidyExpired(s->index));
491  Game::NewEvent(new ScriptEventSubsidyExpired(s->index));
492  }
493  delete s;
494  modified = true;
495  }
496  }
497 
498  if (modified) {
504  /* Return early if there are no manually distributed cargoes and if we
505  * don't need to invalidate the subsidies window. */
506  return;
507  }
508 
509  bool passenger_subsidy = false;
510  bool town_subsidy = false;
511  bool industry_subsidy = false;
512 
513  int random_chance = RandomRange(16);
514 
515  if (random_chance < 2 && _settings_game.linkgraph.distribution_pax == DT_MANUAL) {
516  /* There is a 1/8 chance each month of generating a passenger subsidy. */
517  int n = 1000;
518 
519  do {
520  passenger_subsidy = FindSubsidyPassengerRoute();
521  } while (!passenger_subsidy && n--);
522  } else if (random_chance == 2) {
523  /* Cargo subsidies with a town as a source have a 1/16 chance. */
524  int n = 1000;
525 
526  do {
527  town_subsidy = FindSubsidyTownCargoRoute();
528  } while (!town_subsidy && n--);
529  } else if (random_chance == 3) {
530  /* Cargo subsidies with an industry as a source have a 1/16 chance. */
531  int n = 1000;
532 
533  do {
534  industry_subsidy = FindSubsidyIndustryCargoRoute();
535  } while (!industry_subsidy && n--);
536  }
537 
538  modified |= passenger_subsidy || town_subsidy || industry_subsidy;
539 
540  if (modified) InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
541 }
542 
552 bool CheckSubsidised(CargoID cargo_type, CompanyID company, SourceType src_type, SourceID src, const Station *st)
553 {
554  /* If the source isn't subsidised, don't continue */
555  if (src == INVALID_SOURCE) return false;
556  switch (src_type) {
557  case ST_INDUSTRY:
558  if (!(Industry::Get(src)->part_of_subsidy & POS_SRC)) return false;
559  break;
560  case ST_TOWN:
561  if (!(Town::Get(src)->cache.part_of_subsidy & POS_SRC)) return false;
562  break;
563  default: return false;
564  }
565 
566  /* Remember all towns near this station (at least one house in its catchment radius)
567  * which are destination of subsidised path. Do that only if needed */
568  std::vector<const Town *> towns_near;
569  if (!st->rect.IsEmpty()) {
570  for (const Subsidy *s : Subsidy::Iterate()) {
571  /* Don't create the cache if there is no applicable subsidy with town as destination */
572  if (s->dst_type != ST_TOWN) continue;
573  if (s->cargo_type != cargo_type || s->src_type != src_type || s->src != src) continue;
574  if (s->IsAwarded() && s->awarded != company) continue;
575 
577  for (TileIndex tile = it; tile != INVALID_TILE; tile = ++it) {
578  if (!IsTileType(tile, MP_HOUSE)) continue;
579  const Town *t = Town::GetByTile(tile);
580  if (t->cache.part_of_subsidy & POS_DST) include(towns_near, t);
581  }
582  break;
583  }
584  }
585 
586  bool subsidised = false;
587 
588  /* Check if there's a (new) subsidy that applies. There can be more subsidies triggered by this delivery!
589  * Think about the case that subsidies are A->B and A->C and station has both B and C in its catchment area */
590  for (Subsidy *s : Subsidy::Iterate()) {
591  if (s->cargo_type == cargo_type && s->src_type == src_type && s->src == src && (!s->IsAwarded() || s->awarded == company)) {
592  switch (s->dst_type) {
593  case ST_INDUSTRY:
594  for (Industry *ind : st->industries_near) {
595  if (s->dst == ind->index) {
596  assert(ind->part_of_subsidy & POS_DST);
597  subsidised = true;
598  if (!s->IsAwarded()) s->AwardTo(company);
599  }
600  }
601  break;
602  case ST_TOWN:
603  for (const Town *tp : towns_near) {
604  if (s->dst == tp->index) {
605  assert(tp->cache.part_of_subsidy & POS_DST);
606  subsidised = true;
607  if (!s->IsAwarded()) s->AwardTo(company);
608  }
609  }
610  break;
611  default:
612  NOT_REACHED();
613  }
614  }
615  }
616 
617  return subsidised;
618 }
Functions related to OTTD&#39;s strings.
SourceType
Types of cargo source and destination.
Definition: cargo_type.h:146
Owner
Enum for all companies/owners.
Definition: company_type.h:18
Iterator to iterate over all tiles belonging to a bitmaptilearea.
Definition: bitmap_type.h:107
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
Source/destination is a town.
Definition: cargo_type.h:148
News about subsidies (announcements, expirations, acceptance)
Definition: news_type.h:35
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:80
static void NewEvent(class ScriptEvent *event)
Queue a new event for a Game Script.
Definition: game_core.cpp:141
static const uint SUBSIDY_CONTRACT_MONTHS
Duration of subsidy after awarding.
Definition: subsidy_base.h:55
Pair SetupSubsidyDecodeParam(const Subsidy *s, bool mode)
Setup the string parameters for printing the subsidy at the screen, and compute the news reference fo...
Definition: subsidy.cpp:74
PartOfSubsidy part_of_subsidy
Is this town a source/destination of a subsidy?
Definition: town.h:44
CompanyID awarded
Subsidy is awarded to this company; INVALID_COMPANY if it&#39;s not awarded to anyone.
Definition: subsidy_base.h:25
SourceID src
Index of source. Either TownID or IndustryID.
Definition: subsidy_base.h:28
static const uint SUBSIDY_OFFER_MONTHS
Constants related to subsidies.
Definition: subsidy_base.h:54
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:329
SourceType dst_type
Destination of subsidised path (ST_INDUSTRY or ST_TOWN)
Definition: subsidy_base.h:27
CargoArray GetAcceptanceAroundTiles(TileIndex tile, int w, int h, int rad, CargoTypes *always_accepted)
Get the acceptance of cargoes around the tile in 1/8.
void SubsidyMonthlyLoop()
Perform the monthly update of open subsidies, and try to create a new one.
Definition: subsidy.cpp:474
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:23
Maximal number of cargo types in a game.
Definition: cargo_type.h:64
SourceID dst
Index of destination. Either TownID or IndustryID.
Definition: subsidy_base.h:29
Specification of a cargo type.
Definition: cargotype.h:55
bool FindSubsidyCargoDestination(CargoID cid, SourceType src_type, SourceID src)
Tries to find a suitable destination for the given source and cargo.
Definition: subsidy.cpp:424
StationRect rect
NOSAVE: Station spread out rectangle maintained by StationRect::xxx() functions.
Defines the internal data of a functional industry.
Definition: industry.h:40
DifficultySettings difficulty
settings related to the difficulty
static void BroadcastNewEvent(ScriptEvent *event, CompanyID skip_company=MAX_COMPANIES)
Broadcast a new event to all active AIs.
Definition: ai_core.cpp:259
Tindex index
Index of this pool item.
Definition: pool_type.hpp:227
StringID name_single
Name of a single entity of this type of cargo.
Definition: cargotype.h:71
Manual distribution. No link graph calculations are run.
static const uint SUBSIDY_MAX_PCT_TRANSPORTED
Subsidy will be created only for towns/industries with less % transported.
Definition: subsidy_base.h:58
byte subsidy_multiplier
amount of subsidy
Definition: settings_type.h:62
DistributionType distribution_default
distribution type for all other goods
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:48
Common return value for all commands.
Definition: command_type.h:23
static Industry * GetRandom()
Return a random valid industry.
uint32 population
Current population of people.
Definition: town.h:42
bool FindSubsidyTownCargoRoute()
Tries to create a cargo subsidy with a town as source.
Definition: subsidy.cpp:321
void DeleteSubsidyWith(SourceType type, SourceID index)
Delete the subsidies associated with a given cargo source type and id.
Definition: subsidy.cpp:148
StringID name
Name of this type of cargo.
Definition: cargotype.h:70
static uint32 RandomRange(uint32 limit)
Pick a random number between 0 and limit - 1, inclusive.
Definition: random_func.hpp:81
static const SourceID INVALID_SOURCE
Invalid/unknown index of source.
Definition: cargo_type.h:153
bit 1 set -> town/industry is destination of subsidised path
Definition: subsidy_type.h:19
Pseudo random number generator.
static const uint SUBSIDY_CARGO_MIN_POPULATION
Min. population of destination town for cargo route.
Definition: subsidy_base.h:57
Invalid cargo type.
Definition: cargo_type.h:68
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
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:279
Functions related to low-level strings.
Some methods of Pool are placed here in order to reduce compilation time and binary size...
static const int MAX_CHAR_LENGTH
Max. length of UTF-8 encoded unicode character.
Definition: strings_type.h:18
uint8 valid
Bits indicating what variable is valid (for each bit, 0 is invalid, 1 is valid).
DistributionType distribution_mail
distribution type for mail
TileIndex xy
town center tile
Definition: town.h:51
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
PartOfSubsidy part_of_subsidy
NOSAVE: is this industry a source/destination of a subsidy?
Definition: industry.h:63
CommandCost CmdCreateSubsidy(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Create a new subsidy.
Definition: subsidy.cpp:245
bool FindSubsidyPassengerRoute()
Tries to create a passenger subsidy between two towns.
Definition: subsidy.cpp:291
IndustryList industries_near
Cached list of industries near the station that can accept cargo,.
Definition: station_base.h:482
DoCommandFlag
List of flags for a command.
Definition: command_type.h:344
Definition of base types and functions in a cross-platform compatible way.
static const uint MAX_LENGTH_COMPANY_NAME_CHARS
The maximum length of a company name in characters including &#39;\0&#39;.
Definition: company_type.h:40
A number of safeguards to prevent using unsafe methods.
static Town * GetRandom()
Return a random valid town.
Definition: town_cmd.cpp:181
Empty reference.
Definition: news_type.h:50
static const uint SUBSIDY_MAX_DISTANCE
Max. length of subsidised route (DistanceManhattan)
Definition: subsidy_base.h:59
uint16 last_month_production[INDUSTRY_NUM_OUTPUTS]
total units produced per cargo in the last full month
Definition: industry.h:53
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:60
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:136
CargoID produced_cargo[INDUSTRY_NUM_OUTPUTS]
16 production cargo slots
Definition: industry.h:44
Normal news item. (Newspaper with text only)
Definition: news_type.h:78
Subsidies list; Window numbers:
Definition: window_type.h:253
nothing
Definition: subsidy_type.h:17
CargoID accepts_cargo[INDUSTRY_NUM_INPUTS]
16 input cargo slots
Definition: industry.h:49
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:40
bool FindSubsidyIndustryCargoRoute()
Tries to create a cargo subsidy with an industry as source.
Definition: subsidy.cpp:372
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:552
SubsidyPool _subsidy_pool("Subsidy")
Pool for the subsidies.
CargoID cargo_type
Cargo type involved in this subsidy, CT_INVALID for invalid subsidy.
Definition: subsidy_base.h:23
DistributionType distribution_armoured
distribution type for armoured cargo class
Base class for all pools.
Definition: pool_type.hpp:81
Struct about subsidies, offered and awarded.
Definition: subsidy_base.h:22
static bool Chance16(const uint a, const uint b)
Flips a coin with given probability.
bit 0 set -> town/industry is source of subsidised path
Definition: subsidy_type.h:18
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don&#39;t get linker errors.
Definition: pool_func.hpp:224
CargoArray GetProductionAroundTiles(TileIndex tile, int w, int h, int rad)
Get the cargo types being produced around the tile (in a rectangle).
execute the given command
Definition: command_type.h:346
Functions related to companies.
An invalid company.
Definition: company_type.h:30
Class for storing amounts of cargo.
Definition: cargo_type.h:81
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:378
Subsidy base class.
void InjectDParam(uint amount)
Shift the string parameters in the global string parameter array by amount positions, making room at the beginning.
Definition: strings.cpp:288
DistributionType distribution_pax
distribution type for passengers
Reference town. Scroll to town when clicking on the news.
Definition: news_type.h:55
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo ID.
Definition: cargotype.h:117
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
Definition: map.cpp:157
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...
uint16 SourceID
Contains either industry ID, town ID or company ID (or INVALID_SOURCE)
Definition: cargo_type.h:152
void RebuildSubsidisedSourceAndDestinationCache()
Perform a full rebuild of the subsidies cache.
Definition: subsidy.cpp:131
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
SourceType src_type
Source of subsidised path (ST_INDUSTRY or ST_TOWN)
Definition: subsidy_base.h:26
BitmapTileArea catchment_tiles
NOSAVE: Set of individual tiles covered by catchment area.
Definition: station_base.h:470
TownCache cache
Container for all cacheable data.
Definition: town.h:53
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:384
Town data structure.
Definition: town.h:50
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function() ...
Definition: pool_type.hpp:299
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
Base functions for all Games.
Functions related to commands.
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
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:45
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:318
NewsReferenceType
References to objects in news.
Definition: news_type.h:49
Base of all industries.
Base functions for all AIs.
void AddNewsItem(StringID string, NewsType type, NewsFlag flags, NewsReferenceType reftype1=NR_NONE, uint32 ref1=UINT32_MAX, NewsReferenceType reftype2=NR_NONE, uint32 ref2=UINT32_MAX, void *free_data=nullptr)
Add a new newsitem to be shown.
Definition: news_gui.cpp:800
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:83
Base of the town class.
Reference industry. Scroll to industry when clicking on the news. Delete news when industry is delete...
Definition: news_type.h:54
A house by a town.
Definition: tile_type.h:44
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
PartOfSubsidy
What part of a subsidy is something?
Definition: subsidy_type.h:16
byte remaining
Remaining months when this subsidy is valid.
Definition: subsidy_base.h:24
Window functions not directly related to making/drawing windows.
byte last_month_pct_transported[INDUSTRY_NUM_OUTPUTS]
percentage transported per cargo in the last full month
Definition: industry.h:52
static const uint SUBSIDY_PAX_MIN_POPULATION
Min. population of towns for subsidised pax route.
Definition: subsidy_base.h:56
Functions related to news.
Base classes/functions for stations.
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:44
The object is owned by a superuser / goal script.
Definition: company_type.h:27
Station data structure.
Definition: station_base.h:450
Functions related to subsidies.
LinkGraphSettings linkgraph
settings for link graph calculations
A pair of two integers.
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:3319
Source/destination is an industry.
Definition: cargo_type.h:147
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:199