OpenTTD
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  CargoTypes town_cargo_produced = src_town->cargo_produced;
332 
333  /* Passenger subsidies are not handled here. */
334  ClrBit(town_cargo_produced, CT_PASSENGERS);
335 
336  /* No cargo produced at all? */
337  if (town_cargo_produced == 0) return false;
338 
339  /* Choose a random cargo that is produced in the town. */
340  uint8 cargo_number = RandomRange(CountBits(town_cargo_produced));
341  CargoID cid;
342  FOR_EACH_SET_CARGO_ID(cid, town_cargo_produced) {
343  if (cargo_number == 0) break;
344  cargo_number--;
345  }
346 
347  /* Avoid using invalid NewGRF cargoes. */
348  if (!CargoSpec::Get(cid)->IsValid() ||
349  _settings_game.linkgraph.GetDistributionType(cid) != DT_MANUAL) {
350  return false;
351  }
352 
353  /* Quit if the percentage transported is large enough. */
354  if (src_town->GetPercentTransported(cid) > SUBSIDY_MAX_PCT_TRANSPORTED) return false;
355 
356  SourceID src = src_town->index;
357 
358  return FindSubsidyCargoDestination(cid, src_type, src);
359 }
360 
366 {
367  if (!Subsidy::CanAllocateItem()) return false;
368 
369  SourceType src_type = ST_INDUSTRY;
370 
371  /* Select a random industry. */
372  const Industry *src_ind = Industry::GetRandom();
373  if (src_ind == nullptr) return false;
374 
375  uint trans, total;
376 
377  CargoID cid;
378 
379  /* Randomize cargo type */
380  int num_cargos = 0;
381  uint cargo_index;
382  for (cargo_index = 0; cargo_index < lengthof(src_ind->produced_cargo); cargo_index++) {
383  if (src_ind->produced_cargo[cargo_index] != CT_INVALID) num_cargos++;
384  }
385  if (num_cargos == 0) return false; // industry produces nothing
386  int cargo_num = RandomRange(num_cargos) + 1;
387  for (cargo_index = 0; cargo_index < lengthof(src_ind->produced_cargo); cargo_index++) {
388  if (src_ind->produced_cargo[cargo_index] != CT_INVALID) cargo_num--;
389  if (cargo_num == 0) break;
390  }
391  assert(cargo_num == 0); // indicates loop didn't break as intended
392  cid = src_ind->produced_cargo[cargo_index];
393  trans = src_ind->last_month_pct_transported[cargo_index];
394  total = src_ind->last_month_production[cargo_index];
395 
396  /* Quit if no production in this industry
397  * or if the pct transported is already large enough
398  * or if the cargo is automatically distributed */
399  if (total == 0 || trans > SUBSIDY_MAX_PCT_TRANSPORTED ||
400  cid == CT_INVALID ||
401  _settings_game.linkgraph.GetDistributionType(cid) != DT_MANUAL) {
402  return false;
403  }
404 
405  SourceID src = src_ind->index;
406 
407  return FindSubsidyCargoDestination(cid, src_type, src);
408 }
409 
418 {
419  /* Choose a random destination. Only consider towns if they can accept the cargo. */
420  SourceType dst_type = (HasBit(_town_cargoes_accepted, cid) && Chance16(1, 2)) ? ST_TOWN : ST_INDUSTRY;
421 
422  SourceID dst;
423  switch (dst_type) {
424  case ST_TOWN: {
425  /* Select a random town. */
426  const Town *dst_town = Town::GetRandom();
427 
428  /* Check if the town can accept this cargo. */
429  if (!HasBit(dst_town->cargo_accepted_total, cid)) return false;
430 
431  dst = dst_town->index;
432  break;
433  }
434 
435  case ST_INDUSTRY: {
436  /* Select a random industry. */
437  const Industry *dst_ind = Industry::GetRandom();
438  if (dst_ind == nullptr) return false;
439 
440  /* The industry must accept the cargo */
441  bool valid = std::find(dst_ind->accepts_cargo, endof(dst_ind->accepts_cargo), cid) != endof(dst_ind->accepts_cargo);
442  if (!valid) return false;
443 
444  dst = dst_ind->index;
445  break;
446  }
447 
448  default: NOT_REACHED();
449  }
450 
451  /* Check that the source and the destination are not the same. */
452  if (src_type == dst_type && src == dst) return false;
453 
454  /* Check distance between source and destination. */
455  if (!CheckSubsidyDistance(src_type, src, dst_type, dst)) return false;
456 
457  /* Avoid duplicate subsidies. */
458  if (CheckSubsidyDuplicate(cid, src_type, src, dst_type, dst)) return false;
459 
460  CreateSubsidy(cid, src_type, src, dst_type, dst);
461 
462  return true;
463 }
464 
467 {
468  bool modified = false;
469 
470  for (Subsidy *s : Subsidy::Iterate()) {
471  if (--s->remaining == 0) {
472  if (!s->IsAwarded()) {
473  Pair reftype = SetupSubsidyDecodeParam(s, true);
474  AddNewsItem(STR_NEWS_OFFER_OF_SUBSIDY_EXPIRED, NT_SUBSIDIES, NF_NORMAL, (NewsReferenceType)reftype.a, s->src, (NewsReferenceType)reftype.b, s->dst);
475  AI::BroadcastNewEvent(new ScriptEventSubsidyOfferExpired(s->index));
476  Game::NewEvent(new ScriptEventSubsidyOfferExpired(s->index));
477  } else {
478  if (s->awarded == _local_company) {
479  Pair reftype = SetupSubsidyDecodeParam(s, true);
480  AddNewsItem(STR_NEWS_SUBSIDY_WITHDRAWN_SERVICE, NT_SUBSIDIES, NF_NORMAL, (NewsReferenceType)reftype.a, s->src, (NewsReferenceType)reftype.b, s->dst);
481  }
482  AI::BroadcastNewEvent(new ScriptEventSubsidyExpired(s->index));
483  Game::NewEvent(new ScriptEventSubsidyExpired(s->index));
484  }
485  delete s;
486  modified = true;
487  }
488  }
489 
490  if (modified) {
496  /* Return early if there are no manually distributed cargoes and if we
497  * don't need to invalidate the subsidies window. */
498  return;
499  }
500 
501  bool passenger_subsidy = false;
502  bool town_subsidy = false;
503  bool industry_subsidy = false;
504 
505  int random_chance = RandomRange(16);
506 
507  if (random_chance < 2 && _settings_game.linkgraph.distribution_pax == DT_MANUAL) {
508  /* There is a 1/8 chance each month of generating a passenger subsidy. */
509  int n = 1000;
510 
511  do {
512  passenger_subsidy = FindSubsidyPassengerRoute();
513  } while (!passenger_subsidy && n--);
514  } else if (random_chance == 2) {
515  /* Cargo subsidies with a town as a source have a 1/16 chance. */
516  int n = 1000;
517 
518  do {
519  town_subsidy = FindSubsidyTownCargoRoute();
520  } while (!town_subsidy && n--);
521  } else if (random_chance == 3) {
522  /* Cargo subsidies with an industry as a source have a 1/16 chance. */
523  int n = 1000;
524 
525  do {
526  industry_subsidy = FindSubsidyIndustryCargoRoute();
527  } while (!industry_subsidy && n--);
528  }
529 
530  modified |= passenger_subsidy || town_subsidy || industry_subsidy;
531 
532  if (modified) InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
533 }
534 
544 bool CheckSubsidised(CargoID cargo_type, CompanyID company, SourceType src_type, SourceID src, const Station *st)
545 {
546  /* If the source isn't subsidised, don't continue */
547  if (src == INVALID_SOURCE) return false;
548  switch (src_type) {
549  case ST_INDUSTRY:
550  if (!(Industry::Get(src)->part_of_subsidy & POS_SRC)) return false;
551  break;
552  case ST_TOWN:
553  if (!(Town::Get(src)->cache.part_of_subsidy & POS_SRC)) return false;
554  break;
555  default: return false;
556  }
557 
558  /* Remember all towns near this station (at least one house in its catchment radius)
559  * which are destination of subsidised path. Do that only if needed */
560  std::vector<const Town *> towns_near;
561  if (!st->rect.IsEmpty()) {
562  for (const Subsidy *s : Subsidy::Iterate()) {
563  /* Don't create the cache if there is no applicable subsidy with town as destination */
564  if (s->dst_type != ST_TOWN) continue;
565  if (s->cargo_type != cargo_type || s->src_type != src_type || s->src != src) continue;
566  if (s->IsAwarded() && s->awarded != company) continue;
567 
569  for (TileIndex tile = it; tile != INVALID_TILE; tile = ++it) {
570  if (!IsTileType(tile, MP_HOUSE)) continue;
571  const Town *t = Town::GetByTile(tile);
572  if (t->cache.part_of_subsidy & POS_DST) include(towns_near, t);
573  }
574  break;
575  }
576  }
577 
578  bool subsidised = false;
579 
580  /* Check if there's a (new) subsidy that applies. There can be more subsidies triggered by this delivery!
581  * Think about the case that subsidies are A->B and A->C and station has both B and C in its catchment area */
582  for (Subsidy *s : Subsidy::Iterate()) {
583  if (s->cargo_type == cargo_type && s->src_type == src_type && s->src == src && (!s->IsAwarded() || s->awarded == company)) {
584  switch (s->dst_type) {
585  case ST_INDUSTRY:
586  for (Industry *ind : st->industries_near) {
587  if (s->dst == ind->index) {
588  assert(ind->part_of_subsidy & POS_DST);
589  subsidised = true;
590  if (!s->IsAwarded()) s->AwardTo(company);
591  }
592  }
593  break;
594  case ST_TOWN:
595  for (const Town *tp : towns_near) {
596  if (s->dst == tp->index) {
597  assert(tp->cache.part_of_subsidy & POS_DST);
598  subsidised = true;
599  if (!s->IsAwarded()) s->AwardTo(company);
600  }
601  }
602  break;
603  default:
604  NOT_REACHED();
605  }
606  }
607  }
608 
609  return subsidised;
610 }
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:79
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:47
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:291
SourceType dst_type
Destination of subsidised path (ST_INDUSTRY or ST_TOWN)
Definition: subsidy_base.h:27
void SubsidyMonthlyLoop()
Perform the monthly update of open subsidies, and try to create a new one.
Definition: subsidy.cpp:466
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
CargoTypes cargo_produced
Bitmap of all cargoes produced by houses in this town.
Definition: town.h:86
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:417
CargoTypes _town_cargoes_accepted
Bitmap of all cargoes accepted by houses.
Definition: town_cmd.cpp:58
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:189
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:45
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:54
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:342
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:182
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
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:365
CargoTypes cargo_accepted_total
NOSAVE: Bitmap of all cargoes accepted by houses in this town.
Definition: town.h:88
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:544
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:82
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
execute the given command
Definition: command_type.h:344
Functions related to companies.
An invalid company.
Definition: company_type.h:30
static T ClrBit(T &x, const uint8 y)
Clears a bit in a variable.
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:340
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:56
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:384
Town data structure.
Definition: town.h:53
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function() ...
Definition: pool_type.hpp:261
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
static uint CountBits(T value)
Counts the number of set bits in a variable.
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:280
NewsReferenceType
References to objects in news.
Definition: news_type.h:49
Base of all industries.
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
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:745
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:3316
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