OpenTTD Source  20241108-master-g80f628063a
industry.h
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 #ifndef INDUSTRY_H
11 #define INDUSTRY_H
12 
13 #include "newgrf_storage.h"
14 #include "subsidy_type.h"
15 #include "industry_map.h"
16 #include "industrytype.h"
17 #include "tilearea_type.h"
18 #include "station_base.h"
21 
22 
24 extern IndustryPool _industry_pool;
25 
26 static const TimerGameEconomy::Year PROCESSING_INDUSTRY_ABANDONMENT_YEARS = 5;
27 
28 /*
29  * Production level maximum, minimum and default values.
30  * It is not a value been really used in order to change, but rather an indicator
31  * of how the industry is behaving.
32  */
33 static constexpr uint8_t PRODLEVEL_CLOSURE = 0x00;
34 static constexpr uint8_t PRODLEVEL_MINIMUM = 0x04;
35 static constexpr uint8_t PRODLEVEL_DEFAULT = 0x10;
36 static constexpr uint8_t PRODLEVEL_MAXIMUM = 0x80;
37 
42 enum IndustryControlFlags : uint8_t {
57 };
59 
60 static const int THIS_MONTH = 0;
61 static const int LAST_MONTH = 1;
62 
66 struct Industry : IndustryPool::PoolItem<&_industry_pool> {
67  struct ProducedHistory {
68  uint16_t production;
69  uint16_t transported;
70 
71  uint8_t PctTransported() const
72  {
73  if (this->production == 0) return 0;
74  return ClampTo<uint8_t>(this->transported * 256 / this->production);
75  }
76  };
77 
78  struct ProducedCargo {
80  uint16_t waiting;
81  uint8_t rate;
82  std::array<ProducedHistory, 25> history;
83  };
84 
85  struct AcceptedCargo {
87  uint16_t waiting;
89  };
90 
91  using ProducedCargoes = std::vector<ProducedCargo>;
92  using AcceptedCargoes = std::vector<AcceptedCargo>;
93 
97  ProducedCargoes produced;
98  AcceptedCargoes accepted;
99  uint8_t prod_level;
100  uint16_t counter;
101 
102  IndustryType type;
104  Colours random_colour;
108 
111  mutable std::string cached_name;
112 
116  uint8_t selected_layout;
119  std::string text;
120 
121  uint16_t random;
122 
124 
125  Industry(TileIndex tile = INVALID_TILE) : location(tile, 0, 0) {}
126  ~Industry();
127 
129 
135  inline bool TileBelongsToIndustry(TileIndex tile) const
136  {
137  return IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == this->index;
138  }
139 
145  inline const ProducedCargo &GetProduced(size_t slot) const
146  {
147  static const ProducedCargo empty{INVALID_CARGO, 0, 0, {}};
148  return slot < this->produced.size() ? this->produced[slot] : empty;
149  }
150 
156  inline const AcceptedCargo &GetAccepted(size_t slot) const
157  {
158  static const AcceptedCargo empty{INVALID_CARGO, 0, {}};
159  return slot < this->accepted.size() ? this->accepted[slot] : empty;
160  }
161 
167  inline ProducedCargoes::iterator GetCargoProduced(CargoID cargo)
168  {
169  if (!IsValidCargoID(cargo)) return std::end(this->produced);
170  return std::find_if(std::begin(this->produced), std::end(this->produced), [&cargo](const auto &p) { return p.cargo == cargo; });
171  }
172 
178  inline ProducedCargoes::const_iterator GetCargoProduced(CargoID cargo) const
179  {
180  if (!IsValidCargoID(cargo)) return std::end(this->produced);
181  return std::find_if(std::begin(this->produced), std::end(this->produced), [&cargo](const auto &p) { return p.cargo == cargo; });
182  }
183 
189  inline AcceptedCargoes::iterator GetCargoAccepted(CargoID cargo)
190  {
191  if (!IsValidCargoID(cargo)) return std::end(this->accepted);
192  return std::find_if(std::begin(this->accepted), std::end(this->accepted), [&cargo](const auto &a) { return a.cargo == cargo; });
193  }
194 
200  inline AcceptedCargoes::const_iterator GetCargoAccepted(CargoID cargo) const
201  {
202  if (!IsValidCargoID(cargo)) return std::end(this->accepted);
203  return std::find_if(std::begin(this->accepted), std::end(this->accepted), [&cargo](const auto &a) { return a.cargo == cargo; });
204  }
205 
210  bool IsCargoAccepted() const { return std::any_of(std::begin(this->accepted), std::end(this->accepted), [](const auto &a) { return IsValidCargoID(a.cargo); }); }
211 
216  bool IsCargoProduced() const { return std::any_of(std::begin(this->produced), std::end(this->produced), [](const auto &p) { return IsValidCargoID(p.cargo); }); }
217 
223  bool IsCargoAccepted(CargoID cargo) const { return std::any_of(std::begin(this->accepted), std::end(this->accepted), [&cargo](const auto &a) { return a.cargo == cargo; }); }
224 
230  bool IsCargoProduced(CargoID cargo) const { return std::any_of(std::begin(this->produced), std::end(this->produced), [&cargo](const auto &p) { return p.cargo == cargo; }); }
231 
238  static inline Industry *GetByTile(TileIndex tile)
239  {
240  return Industry::Get(GetIndustryIndex(tile));
241  }
242 
243  static Industry *GetRandom();
244  static void PostDestructor(size_t index);
245 
251  static inline void IncIndustryTypeCount(IndustryType type)
252  {
253  assert(type < NUM_INDUSTRYTYPES);
254  counts[type]++;
255  }
256 
262  static inline void DecIndustryTypeCount(IndustryType type)
263  {
264  assert(type < NUM_INDUSTRYTYPES);
265  counts[type]--;
266  }
267 
273  static inline uint16_t GetIndustryTypeCount(IndustryType type)
274  {
275  assert(type < NUM_INDUSTRYTYPES);
276  return counts[type];
277  }
278 
280  static inline void ResetIndustryCounts()
281  {
282  memset(&counts, 0, sizeof(counts));
283  }
284 
285  inline const std::string &GetCachedName() const
286  {
287  if (this->cached_name.empty()) this->FillCachedName();
288  return this->cached_name;
289  }
290 
291 private:
292  void FillCachedName() const;
293 
294 protected:
295  static uint16_t counts[NUM_INDUSTRYTYPES];
296 };
297 
298 void ClearAllIndustryCachedNames();
299 
300 void PlantRandomFarmField(const Industry *i);
301 
302 void ReleaseDisastersTargetingIndustry(IndustryID);
303 
305 
308  uint32_t probability;
309  uint8_t min_number;
310  uint16_t target_count;
311  uint16_t max_wait;
312  uint16_t wait_count;
313 
314  void Reset();
315 
316  bool GetIndustryTypeData(IndustryType it);
317 };
318 
324  uint32_t wanted_inds;
325 
326  void Reset();
327 
328  void SetupTargetCount();
329  void TryBuildNewIndustry();
330 
331  void EconomyMonthlyLoop();
332 };
333 
335 
336 
339  IDIWD_FORCE_REBUILD,
340  IDIWD_PRODUCTION_CHANGE,
341  IDIWD_FORCE_RESORT,
342 };
343 
345 
346 #endif /* INDUSTRY_H */
uint8_t CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
bool IsValidCargoID(CargoID t)
Test whether cargo type is not INVALID_CARGO.
Definition: cargo_type.h:107
DECLARE_ENUM_AS_BIT_SET(GenderEthnicity) enum CompanyManagerFaceVariable
Bitgroups of the CompanyManagerFace variable.
Owner
Enum for all companies/owners.
Definition: company_type.h:18
IndustryControlFlags
Flags to control/override the behaviour of an industry.
Definition: industry.h:42
@ INDCTL_EXTERNAL_PROD_LEVEL
Indicates that the production level of the industry is externally controlled.
Definition: industry.h:54
@ INDCTL_NONE
No flags in effect.
Definition: industry.h:44
@ INDCTL_NO_CLOSURE
Industry can not close regardless of production level or time since last delivery.
Definition: industry.h:52
@ INDCTL_NO_PRODUCTION_DECREASE
When industry production change is evaluated, rolls to decrease are ignored.
Definition: industry.h:46
@ INDCTL_MASK
Mask of all flags set.
Definition: industry.h:56
@ INDCTL_NO_PRODUCTION_INCREASE
When industry production change is evaluated, rolls to increase are ignored.
Definition: industry.h:48
static constexpr uint8_t PRODLEVEL_MAXIMUM
the industry is running at full speed
Definition: industry.h:36
void ReleaseDisastersTargetingIndustry(IndustryID)
Marks all disasters targeting this industry in such a way they won't call Industry::Get(v->dest_tile)...
static constexpr uint8_t PRODLEVEL_DEFAULT
default level set when the industry is created
Definition: industry.h:35
static const TimerGameEconomy::Year PROCESSING_INDUSTRY_ABANDONMENT_YEARS
If a processing industry doesn't produce for this many consecutive economy years, it may close.
Definition: industry.h:26
IndustryDirectoryInvalidateWindowData
Special values for the industry list window for the data parameter of InvalidateWindowData.
Definition: industry.h:338
bool IsTileForestIndustry(TileIndex tile)
Check whether the tile is a forest.
IndustryBuildData _industry_builder
In-game manager of industries.
static constexpr uint8_t PRODLEVEL_MINIMUM
below this level, the industry is set to be closing
Definition: industry.h:34
static constexpr uint8_t PRODLEVEL_CLOSURE
signal set to actually close the industry
Definition: industry.h:33
void TrimIndustryAcceptedProduced(Industry *ind)
Remove unused industry accepted/produced slots – entries after the last slot with valid cargo.
Accessors for industries.
IndustryID GetIndustryIndex(Tile t)
Get the industry ID of the given tile.
Definition: industry_map.h:63
static const IndustryType NUM_INDUSTRYTYPES
total number of industry types, new and old; limited to 240 because we need some special ids like INV...
Definition: industry_type.h:26
Industry type specs.
Functionality related to the temporary and persistent storage arrays for NewGRFs.
Base classes/functions for stations.
std::set< Station *, StationCompare > StationList
List of stations.
Definition: station_type.h:94
Data for managing the number and type of industries in the game.
Definition: industry.h:322
void Reset()
Completely reset the industry build data.
void EconomyMonthlyLoop()
Monthly update of industry build data.
uint32_t wanted_inds
Number of wanted industries (bits 31-16), and a fraction (bits 15-0).
Definition: industry.h:324
void SetupTargetCount()
Decide how many industries of each type are needed.
void TryBuildNewIndustry()
Try to create a random industry, during gameplay.
IndustryTypeBuildData builddata[NUM_INDUSTRYTYPES]
Industry build data for every industry type.
Definition: industry.h:323
Data for managing the number of industries of a single industry type.
Definition: industry.h:307
uint32_t probability
Relative probability of building this industry.
Definition: industry.h:308
uint16_t target_count
Desired number of industries of this type.
Definition: industry.h:310
uint8_t min_number
Smallest number of industries that should exist (either 0 or 1).
Definition: industry.h:309
void Reset()
Reset the entry.
bool GetIndustryTypeData(IndustryType it)
Set the probability and min_number fields for the industry type it for a running game.
uint16_t wait_count
Number of turns to wait before trying to build again.
Definition: industry.h:312
uint16_t max_wait
Starting number of turns to wait (copied to wait_count).
Definition: industry.h:311
uint16_t waiting
Amount of cargo waiting to processed.
Definition: industry.h:87
CargoID cargo
Cargo type.
Definition: industry.h:86
TimerGameEconomy::Date last_accepted
Last day cargo was accepted by this industry.
Definition: industry.h:88
CargoID cargo
Cargo type.
Definition: industry.h:79
uint16_t waiting
Amount of cargo produced.
Definition: industry.h:80
std::array< ProducedHistory, 25 > history
History of cargo produced and transported for this month and 24 previous months.
Definition: industry.h:82
uint8_t rate
Production rate.
Definition: industry.h:81
uint16_t transported
Total transported.
Definition: industry.h:69
uint16_t production
Total produced.
Definition: industry.h:68
Defines the internal data of a functional industry.
Definition: industry.h:66
static Industry * GetRandom()
Return a random valid industry.
IndustryType type
type of industry.
Definition: industry.h:102
Owner exclusive_supplier
Which company has exclusive rights to deliver cargo (INVALID_OWNER = anyone)
Definition: industry.h:117
TimerGameCalendar::Date construction_date
Date of the construction of the industry.
Definition: industry.h:114
IndustryControlFlags ctlflags
flags overriding standard behaviours
Definition: industry.h:107
bool IsCargoAccepted() const
Test if this industry accepts any cargo.
Definition: industry.h:210
PersistentStorage * psa
Persistent storage for NewGRF industries.
Definition: industry.h:123
bool IsCargoAccepted(CargoID cargo) const
Test if this industry accepts a specific cargo.
Definition: industry.h:223
uint8_t prod_level
general production level
Definition: industry.h:99
Colours random_colour
randomized colour of the industry, for display purpose
Definition: industry.h:104
void RecomputeProductionMultipliers()
Recompute #production_rate for current prod_level.
AcceptedCargoes::iterator GetCargoAccepted(CargoID cargo)
Get accepted cargo slot for a specific cargo type.
Definition: industry.h:189
uint8_t construction_type
Way the industry was constructed (.
Definition: industry.h:115
std::string cached_name
NOSAVE: Cache of the resolved name of the industry.
Definition: industry.h:111
TimerGameEconomy::Year last_prod_year
last economy year of production
Definition: industry.h:105
static Industry * GetByTile(TileIndex tile)
Get the industry of the given tile.
Definition: industry.h:238
ProducedCargoes produced
produced cargo slots
Definition: industry.h:97
uint16_t random
Random value used for randomisation of all kinds of things.
Definition: industry.h:121
static void PostDestructor(size_t index)
Invalidating some stuff after removing item from the pool.
Town * town
Nearest town.
Definition: industry.h:95
Owner founder
Founder of the industry.
Definition: industry.h:113
uint8_t selected_layout
Which tile layout was used when creating the industry.
Definition: industry.h:116
AcceptedCargoes accepted
accepted cargo slots
Definition: industry.h:98
static uint16_t GetIndustryTypeCount(IndustryType type)
Get the count of industries for this type.
Definition: industry.h:273
std::string text
General text with additional information.
Definition: industry.h:119
static void ResetIndustryCounts()
Resets industry counts.
Definition: industry.h:280
Owner owner
owner of the industry. Which SHOULD always be (imho) OWNER_NONE
Definition: industry.h:103
ProducedCargoes::const_iterator GetCargoProduced(CargoID cargo) const
Get produced cargo slot for a specific cargo type (const-variant).
Definition: industry.h:178
static void IncIndustryTypeCount(IndustryType type)
Increment the count of industries for this type.
Definition: industry.h:251
TileArea location
Location of the industry.
Definition: industry.h:94
Station * neutral_station
Associated neutral station.
Definition: industry.h:96
static uint16_t counts[NUM_INDUSTRYTYPES]
Number of industries per type ingame.
Definition: industry.h:295
AcceptedCargoes::const_iterator GetCargoAccepted(CargoID cargo) const
Get accepted cargo slot for a specific cargo type (const-variant).
Definition: industry.h:200
bool IsCargoProduced(CargoID cargo) const
Test if this industry produces a specific cargo.
Definition: industry.h:230
StationList stations_near
NOSAVE: List of nearby stations.
Definition: industry.h:110
bool IsCargoProduced() const
Test if this industry produces any cargo.
Definition: industry.h:216
Owner exclusive_consumer
Which company has exclusive rights to take cargo (INVALID_OWNER = anyone)
Definition: industry.h:118
const ProducedCargo & GetProduced(size_t slot) const
Safely get a produced cargo slot, or an empty data if the slot does not exist.
Definition: industry.h:145
uint16_t counter
used for animation and/or production (if available cargo)
Definition: industry.h:100
ProducedCargoes::iterator GetCargoProduced(CargoID cargo)
Get produced cargo slot for a specific cargo type.
Definition: industry.h:167
uint8_t was_cargo_delivered
flag that indicate this has been the closest industry chosen for cargo delivery by a station....
Definition: industry.h:106
static void DecIndustryTypeCount(IndustryType type)
Decrement the count of industries for this type.
Definition: industry.h:262
bool TileBelongsToIndustry(TileIndex tile) const
Check if a given tile belongs to this industry.
Definition: industry.h:135
const AcceptedCargo & GetAccepted(size_t slot) const
Safely get an accepted cargo slot, or an empty data if the slot does not exist.
Definition: industry.h:156
PartOfSubsidy part_of_subsidy
NOSAVE: is this industry a source/destination of a subsidy?
Definition: industry.h:109
Represents the covered area of e.g.
Definition: tilearea_type.h:18
Class for pooled persistent storage of data.
Base class for all PoolItems.
Definition: pool_type.hpp:237
Tindex index
Index of this pool item.
Definition: pool_type.hpp:238
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:339
Base class for all pools.
Definition: pool_type.hpp:80
Station data structure.
Definition: station_base.h:439
Templated helper to make a type-safe 'typedef' representing a single POD value.
Town data structure.
Definition: town.h:54
basic types related to subsidies
PartOfSubsidy
What part of a subsidy is something?
Definition: subsidy_type.h:16
static debug_inline bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:95
@ MP_INDUSTRY
Part of an industry.
Definition: tile_type.h:56
Type for storing the 'area' of something uses on the map.
Definition of the game-calendar-timer.
Definition of the game-economy-timer.