OpenTTD
subsidy.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8  */
9 
12 #include "stdafx.h"
13 #include "company_func.h"
14 #include "industry.h"
15 #include "town.h"
16 #include "news_func.h"
17 #include "ai/ai.hpp"
18 #include "station_base.h"
19 #include "strings_func.h"
20 #include "window_func.h"
21 #include "subsidy_base.h"
22 #include "subsidy_func.h"
23 #include "core/pool_func.hpp"
24 #include "core/random_func.hpp"
25 #include "game/game.hpp"
26 #include "command_func.h"
27 #include "string_func.h"
28 
29 #include "table/strings.h"
30 
31 #include "safeguards.h"
32 
33 SubsidyPool _subsidy_pool("Subsidy");
35 
36 
40 void Subsidy::AwardTo(CompanyID company)
41 {
42  assert(!this->IsAwarded());
43 
44  this->awarded = company;
45  this->remaining = SUBSIDY_CONTRACT_MONTHS;
46 
48  SetDParam(0, company);
49  GetString(company_name, STR_COMPANY_NAME, lastof(company_name));
50 
51  char *cn = stredup(company_name);
52 
53  /* Add a news item */
54  Pair reftype = SetupSubsidyDecodeParam(this, false);
55  InjectDParam(1);
56 
57  SetDParamStr(0, cn);
59  STR_NEWS_SERVICE_SUBSIDY_AWARDED_HALF + _settings_game.difficulty.subsidy_multiplier,
61  (NewsReferenceType)reftype.a, this->src, (NewsReferenceType)reftype.b, this->dst,
62  cn
63  );
64  AI::BroadcastNewEvent(new ScriptEventSubsidyAwarded(this->index));
65  Game::NewEvent(new ScriptEventSubsidyAwarded(this->index));
66 
68 }
69 
76 Pair SetupSubsidyDecodeParam(const Subsidy *s, bool mode)
77 {
78  NewsReferenceType reftype1 = NR_NONE;
79  NewsReferenceType reftype2 = NR_NONE;
80 
81  /* if mode is false, use the singular form */
82  const CargoSpec *cs = CargoSpec::Get(s->cargo_type);
83  SetDParam(0, mode ? cs->name : cs->name_single);
84 
85  switch (s->src_type) {
86  case ST_INDUSTRY:
87  reftype1 = NR_INDUSTRY;
88  SetDParam(1, STR_INDUSTRY_NAME);
89  break;
90  case ST_TOWN:
91  reftype1 = NR_TOWN;
92  SetDParam(1, STR_TOWN_NAME);
93  break;
94  default: NOT_REACHED();
95  }
96  SetDParam(2, s->src);
97 
98  switch (s->dst_type) {
99  case ST_INDUSTRY:
100  reftype2 = NR_INDUSTRY;
101  SetDParam(4, STR_INDUSTRY_NAME);
102  break;
103  case ST_TOWN:
104  reftype2 = NR_TOWN;
105  SetDParam(4, STR_TOWN_NAME);
106  break;
107  default: NOT_REACHED();
108  }
109  SetDParam(5, s->dst);
110 
111  Pair p;
112  p.a = reftype1;
113  p.b = reftype2;
114  return p;
115 }
116 
123 static inline void SetPartOfSubsidyFlag(SourceType type, SourceID index, PartOfSubsidy flag)
124 {
125  switch (type) {
126  case ST_INDUSTRY: Industry::Get(index)->part_of_subsidy |= flag; return;
127  case ST_TOWN: Town::Get(index)->cache.part_of_subsidy |= flag; return;
128  default: NOT_REACHED();
129  }
130 }
131 
134 {
135  Town *t;
136  FOR_ALL_TOWNS(t) t->cache.part_of_subsidy = POS_NONE;
137 
138  Industry *i;
139  FOR_ALL_INDUSTRIES(i) i->part_of_subsidy = POS_NONE;
140 
141  const Subsidy *s;
142  FOR_ALL_SUBSIDIES(s) {
145  }
146 }
147 
154 {
155  bool dirty = false;
156 
157  Subsidy *s;
158  FOR_ALL_SUBSIDIES(s) {
159  if ((s->src_type == type && s->src == index) || (s->dst_type == type && s->dst == index)) {
160  delete s;
161  dirty = true;
162  }
163  }
164 
165  if (dirty) {
168  }
169 }
170 
180 static bool CheckSubsidyDuplicate(CargoID cargo, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
181 {
182  const Subsidy *s;
183  FOR_ALL_SUBSIDIES(s) {
184  if (s->cargo_type == cargo &&
185  s->src_type == src_type && s->src == src &&
186  s->dst_type == dst_type && s->dst == dst) {
187  return true;
188  }
189  }
190  return false;
191 }
192 
201 static bool CheckSubsidyDistance(SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
202 {
203  TileIndex tile_src = (src_type == ST_TOWN) ? Town::Get(src)->xy : Industry::Get(src)->location.tile;
204  TileIndex tile_dst = (dst_type == ST_TOWN) ? Town::Get(dst)->xy : Industry::Get(dst)->location.tile;
205 
206  return (DistanceManhattan(tile_src, tile_dst) <= SUBSIDY_MAX_DISTANCE);
207 }
208 
217 void CreateSubsidy(CargoID cid, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
218 {
219  Subsidy *s = new Subsidy();
220  s->cargo_type = cid;
221  s->src_type = src_type;
222  s->src = src;
223  s->dst_type = dst_type;
224  s->dst = dst;
227 
228  Pair reftype = SetupSubsidyDecodeParam(s, false);
229  AddNewsItem(STR_NEWS_SERVICE_SUBSIDY_OFFERED, NT_SUBSIDIES, NF_NORMAL, (NewsReferenceType)reftype.a, s->src, (NewsReferenceType)reftype.b, s->dst);
232  AI::BroadcastNewEvent(new ScriptEventSubsidyOffer(s->index));
233  Game::NewEvent(new ScriptEventSubsidyOffer(s->index));
234 
236 }
237 
252 CommandCost CmdCreateSubsidy(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
253 {
254  if (!Subsidy::CanAllocateItem()) return CMD_ERROR;
255 
256  CargoID cid = GB(p1, 24, 8);
257  SourceType src_type = (SourceType)GB(p1, 0, 8);
258  SourceID src = GB(p1, 8, 16);
259  SourceType dst_type = (SourceType)GB(p2, 0, 8);
260  SourceID dst = GB(p2, 8, 16);
261 
262  if (_current_company != OWNER_DEITY) return CMD_ERROR;
263 
264  if (cid >= NUM_CARGO || !::CargoSpec::Get(cid)->IsValid()) return CMD_ERROR;
265 
266  switch (src_type) {
267  case ST_TOWN:
268  if (!Town::IsValidID(src)) return CMD_ERROR;
269  break;
270  case ST_INDUSTRY:
271  if (!Industry::IsValidID(src)) return CMD_ERROR;
272  break;
273  default:
274  return CMD_ERROR;
275  }
276  switch (dst_type) {
277  case ST_TOWN:
278  if (!Town::IsValidID(dst)) return CMD_ERROR;
279  break;
280  case ST_INDUSTRY:
281  if (!Industry::IsValidID(dst)) return CMD_ERROR;
282  break;
283  default:
284  return CMD_ERROR;
285  }
286 
287  if (flags & DC_EXEC) {
288  CreateSubsidy(cid, src_type, src, dst_type, dst);
289  }
290 
291  return CommandCost();
292 }
293 
299 {
300  if (!Subsidy::CanAllocateItem()) return false;
301 
302  const Town *src = Town::GetRandom();
304  src->GetPercentTransported(CT_PASSENGERS) > SUBSIDY_MAX_PCT_TRANSPORTED) {
305  return false;
306  }
307 
308  const Town *dst = Town::GetRandom();
309  if (dst->cache.population < SUBSIDY_PAX_MIN_POPULATION || src == dst) {
310  return false;
311  }
312 
313  if (DistanceManhattan(src->xy, dst->xy) > SUBSIDY_MAX_DISTANCE) return false;
314  if (CheckSubsidyDuplicate(CT_PASSENGERS, ST_TOWN, src->index, ST_TOWN, dst->index)) return false;
315 
316  CreateSubsidy(CT_PASSENGERS, ST_TOWN, src->index, ST_TOWN, dst->index);
317 
318  return true;
319 }
320 
321 bool FindSubsidyCargoDestination(CargoID cid, SourceType src_type, SourceID src);
322 
323 
329 {
330  if (!Subsidy::CanAllocateItem()) return false;
331 
332  SourceType src_type = ST_TOWN;
333 
334  /* Select a random town. */
335  const Town *src_town = Town::GetRandom();
336  if (src_town->cache.population < SUBSIDY_CARGO_MIN_POPULATION) return false;
337 
338  CargoTypes town_cargo_produced = src_town->cargo_produced;
339 
340  /* Passenger subsidies are not handled here. */
341  ClrBit(town_cargo_produced, CT_PASSENGERS);
342 
343  /* No cargo produced at all? */
344  if (town_cargo_produced == 0) return false;
345 
346  /* Choose a random cargo that is produced in the town. */
347  uint8 cargo_number = RandomRange(CountBits(town_cargo_produced));
348  CargoID cid;
349  FOR_EACH_SET_CARGO_ID(cid, town_cargo_produced) {
350  if (cargo_number == 0) break;
351  cargo_number--;
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 == NULL) 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. Only consider towns if they can accept the cargo. */
427  SourceType dst_type = (HasBit(_town_cargoes_accepted, cid) && 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 
435  /* Check if the town can accept this cargo. */
436  if (!HasBit(dst_town->cargo_accepted_total, cid)) return false;
437 
438  dst = dst_town->index;
439  break;
440  }
441 
442  case ST_INDUSTRY: {
443  /* Select a random industry. */
444  const Industry *dst_ind = Industry::GetRandom();
445  if (dst_ind == NULL) return false;
446 
447  /* The industry must accept the cargo */
448  bool valid = std::find(dst_ind->accepts_cargo, endof(dst_ind->accepts_cargo), cid) != endof(dst_ind->accepts_cargo);
449  if (!valid) return false;
450 
451  dst = dst_ind->index;
452  break;
453  }
454 
455  default: NOT_REACHED();
456  }
457 
458  /* Check that the source and the destination are not the same. */
459  if (src_type == dst_type && src == dst) return false;
460 
461  /* Check distance between source and destination. */
462  if (!CheckSubsidyDistance(src_type, src, dst_type, dst)) return false;
463 
464  /* Avoid duplicate subsidies. */
465  if (CheckSubsidyDuplicate(cid, src_type, src, dst_type, dst)) return false;
466 
467  CreateSubsidy(cid, src_type, src, dst_type, dst);
468 
469  return true;
470 }
471 
474 {
475  bool modified = false;
476 
477  Subsidy *s;
478  FOR_ALL_SUBSIDIES(s) {
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  SmallVector<const Town *, 2> towns_near;
569  if (!st->rect.IsEmpty()) {
570  Subsidy *s;
571  FOR_ALL_SUBSIDIES(s) {
572  /* Don't create the cache if there is no applicable subsidy with town as destination */
573  if (s->dst_type != ST_TOWN) continue;
574  if (s->cargo_type != cargo_type || s->src_type != src_type || s->src != src) continue;
575  if (s->IsAwarded() && s->awarded != company) continue;
576 
577  Rect rect = st->GetCatchmentRect();
578 
579  for (int y = rect.top; y <= rect.bottom; y++) {
580  for (int x = rect.left; x <= rect.right; x++) {
581  TileIndex tile = TileXY(x, y);
582  if (!IsTileType(tile, MP_HOUSE)) continue;
583  const Town *t = Town::GetByTile(tile);
584  if (t->cache.part_of_subsidy & POS_DST) towns_near.Include(t);
585  }
586  }
587  break;
588  }
589  }
590 
591  bool subsidised = false;
592 
593  /* Check if there's a (new) subsidy that applies. There can be more subsidies triggered by this delivery!
594  * Think about the case that subsidies are A->B and A->C and station has both B and C in its catchment area */
595  Subsidy *s;
596  FOR_ALL_SUBSIDIES(s) {
597  if (s->cargo_type == cargo_type && s->src_type == src_type && s->src == src && (!s->IsAwarded() || s->awarded == company)) {
598  switch (s->dst_type) {
599  case ST_INDUSTRY:
600  for (const Industry * const *ip = st->industries_near.Begin(); ip != st->industries_near.End(); ip++) {
601  if (s->dst == (*ip)->index) {
602  assert((*ip)->part_of_subsidy & POS_DST);
603  subsidised = true;
604  if (!s->IsAwarded()) s->AwardTo(company);
605  }
606  }
607  break;
608  case ST_TOWN:
609  for (const Town * const *tp = towns_near.Begin(); tp != towns_near.End(); tp++) {
610  if (s->dst == (*tp)->index) {
611  assert((*tp)->cache.part_of_subsidy & POS_DST);
612  subsidised = true;
613  if (!s->IsAwarded()) s->AwardTo(company);
614  }
615  }
616  break;
617  default:
618  NOT_REACHED();
619  }
620  }
621  }
622 
623  return subsidised;
624 }
Functions related to OTTD&#39;s strings.
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:180
News about subsidies (announcements, expirations, acceptance)
Definition: news_type.h:37
Source/destination is a town.
Definition: cargo_type.h:150
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:77
static void NewEvent(class ScriptEvent *event)
Queue a new event for a Game Script.
Definition: game_core.cpp:143
static const uint SUBSIDY_CONTRACT_MONTHS
Duration of subsidy after awarding.
Definition: subsidy_base.h:57
IndustryVector industries_near
Cached list of industries near the station that can accept cargo,.
Definition: station_base.h:475
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:76
Rect GetCatchmentRect() const
Determines catchment rectangle of this station.
Definition: station.cpp:291
SourceID src
Index of source. Either TownID or IndustryID.
Definition: subsidy_base.h:30
static const uint SUBSIDY_OFFER_MONTHS
Constants related to subsidies.
Definition: subsidy_base.h:56
bool IsAwarded() const
Tests whether this subsidy has been awarded to someone.
Definition: subsidy_base.h:47
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:246
void SubsidyMonthlyLoop()
Perform the monthly update of open subsidies, and try to create a new one.
Definition: subsidy.cpp:473
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:25
Maximal number of cargo types in a game.
Definition: cargo_type.h:66
SourceID dst
Index of destination. Either TownID or IndustryID.
Definition: subsidy_base.h:31
CargoTypes cargo_produced
Bitmap of all cargoes produced by houses in this town.
Definition: town.h:88
void AwardTo(CompanyID company)
Marks subsidy as awarded, creates news and AI event.
Definition: subsidy.cpp:40
Specification of a cargo type.
Definition: cargotype.h:56
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
DistributionTypeByte distribution_mail
distribution type for mail
CargoTypes _town_cargoes_accepted
Bitmap of all cargoes accepted by houses.
Definition: town_cmd.cpp:56
Manual distribution. No link graph calculations are run.
StationRect rect
NOSAVE: Station spread out rectangle maintained by StationRect::xxx() functions.
Defines the internal data of a functional industry.
Definition: industry.h:41
const T * Begin() const
Get the pointer to the first item (const)
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:263
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
StringID name_single
Name of a single entity of this type of cargo.
Definition: cargotype.h:72
static const uint SUBSIDY_MAX_PCT_TRANSPORTED
Subsidy will be created only for towns/industries with less % transported.
Definition: subsidy_base.h:60
SourceTypeByte src_type
Source of subsidised path (ST_INDUSTRY or ST_TOWN)
Definition: subsidy_base.h:28
byte subsidy_multiplier
amount of subsidy
Definition: settings_type.h:64
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
Simple vector template class.
Common return value for all commands.
Definition: command_type.h:25
static Industry * GetRandom()
Return a random valid industry.
uint32 population
Current population of people.
Definition: town.h:47
const T * End() const
Get the pointer behind the last valid item (const)
bool FindSubsidyTownCargoRoute()
Tries to create a cargo subsidy with a town as source.
Definition: subsidy.cpp:328
void DeleteSubsidyWith(SourceType type, SourceID index)
Delete the subsidies associated with a given cargo source type and id.
Definition: subsidy.cpp:153
StringID name
Name of this type of cargo.
Definition: cargotype.h:71
static uint32 RandomRange(uint32 limit)
Pick a random number between 0 and limit - 1, inclusive.
Definition: random_func.hpp:83
static const SourceID INVALID_SOURCE
Invalid/unknown index of source.
Definition: cargo_type.h:156
CompanyByte _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:46
Pseudo random number generator.
The object is owned by a superuser / goal script.
Definition: company_type.h:29
static const uint SUBSIDY_CARGO_MIN_POPULATION
Min. population of destination town for cargo route.
Definition: subsidy_base.h:59
Invalid cargo type.
Definition: cargo_type.h:70
void CreateSubsidy(CargoID cid, SourceType src_type, SourceID src, SourceType dst_type, SourceID dst)
Creates a subsidy with the given parameters.
Definition: subsidy.cpp:217
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:282
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:20
uint8 valid
Bits indicating what variable is valid (for each bit, 0 is invalid, 1 is valid).
TileIndex xy
town center tile
Definition: town.h:56
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:152
CommandCost CmdCreateSubsidy(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Create a new subsidy.
Definition: subsidy.cpp:252
PartOfSubsidyByte part_of_subsidy
NOSAVE: is this industry a source/destination of a subsidy?
Definition: industry.h:63
bool FindSubsidyPassengerRoute()
Tries to create a passenger subsidy between two towns.
Definition: subsidy.cpp:298
DoCommandFlag
List of flags for a command.
Definition: command_type.h:343
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:42
A number of safeguards to prevent using unsafe methods.
static Town * GetRandom()
Return a random valid town.
Definition: town_cmd.cpp:145
Empty reference.
Definition: news_type.h:52
static const uint SUBSIDY_MAX_DISTANCE
Max. length of subsidised route (DistanceManhattan)
Definition: subsidy_base.h:61
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:138
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:80
CompanyByte awarded
Subsidy is awarded to this company; INVALID_COMPANY if it&#39;s not awarded to anyone.
Definition: subsidy_base.h:27
Subsidies list; Window numbers:
Definition: window_type.h:255
DistributionTypeByte distribution_pax
distribution type for passengers
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:42
bool FindSubsidyIndustryCargoRoute()
Tries to create a cargo subsidy with an industry as source.
Definition: subsidy.cpp:372
CargoTypes cargo_accepted_total
NOSAVE: Bitmap of all cargoes accepted by houses in this town.
Definition: town.h:90
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:25
Base class for all pools.
Definition: pool_type.hpp:83
Struct about subsidies, offered and awarded.
Definition: subsidy_base.h:24
static bool Chance16(const uint a, const uint b)
Flips a coin with given probability.
SourceType
Types of cargo source and destination.
Definition: cargo_type.h:148
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don&#39;t get linker errors.
Definition: pool_func.hpp:226
bit 1 set -> town/industry is destination of subsidised path
Definition: subsidy_type.h:21
execute the given command
Definition: command_type.h:345
Functions related to companies.
static T ClrBit(T &x, const uint8 y)
Clears a bit in a variable.
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:291
Reference town. Scroll to town when clicking on the news.
Definition: news_type.h:57
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo ID.
Definition: cargotype.h:118
CompanyByte _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
Definition: map.cpp:159
SourceTypeByte dst_type
Destination of subsidised path (ST_INDUSTRY or ST_TOWN)
Definition: subsidy_base.h:29
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=NULL)
Add a new newsitem to be shown.
Definition: news_gui.cpp:659
Source/destination is an industry.
Definition: cargo_type.h:149
uint16 SourceID
Contains either industry ID, town ID or company ID (or INVALID_SOURCE)
Definition: cargo_type.h:155
void RebuildSubsidisedSourceAndDestinationCache()
Perform a full rebuild of the subsidies cache.
Definition: subsidy.cpp:133
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
bool Include(const T &item)
Tests whether a item is present in the vector, and appends it to the end if not.
TownCache cache
Container for all cacheable data.
Definition: town.h:58
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:412
Town data structure.
Definition: town.h:55
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function() ...
Definition: pool_type.hpp:216
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:201
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:123
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-NULL) Titem.
Definition: pool_type.hpp:235
NewsReferenceType
References to objects in news.
Definition: news_type.h:51
Base of all industries.
PartOfSubsidyByte part_of_subsidy
Is this town a source/destination of a subsidy?
Definition: town.h:49
nothing
Definition: subsidy_type.h:19
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Base functions for all AIs.
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:56
Specification of a rectangle with absolute coordinates of all edges.
A house by a town.
Definition: tile_type.h:46
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
Owner
Enum for all companies/owners.
Definition: company_type.h:20
byte remaining
Remaining months when this subsidy is valid.
Definition: subsidy_base.h:26
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:58
Functions related to news.
Base classes/functions for stations.
bit 0 set -> town/industry is source of subsidised path
Definition: subsidy_type.h:20
An invalid company.
Definition: company_type.h:32
DistributionTypeByte distribution_default
distribution type for all other goods
DistributionTypeByte distribution_armoured
distribution type for armoured cargo class
Station data structure.
Definition: station_base.h:446
Functions related to subsidies.
LinkGraphSettings linkgraph
settings for link graph calculations
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition: map_func.h:165
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:3301
PartOfSubsidy
What part of a subsidy is something?
Definition: subsidy_type.h:18
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:201