OpenTTD Source  14.0-beta1
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 
38 };
39 
44 enum IndustryControlFlags : byte {
59 };
61 
62 static const int THIS_MONTH = 0;
63 static const int LAST_MONTH = 1;
64 
68 struct Industry : IndustryPool::PoolItem<&_industry_pool> {
69  struct ProducedHistory {
70  uint16_t production;
71  uint16_t transported;
72 
73  uint8_t PctTransported() const
74  {
75  if (this->production == 0) return 0;
76  return ClampTo<uint8_t>(this->transported * 256 / this->production);
77  }
78  };
79 
80  struct ProducedCargo {
82  uint16_t waiting;
83  uint8_t rate;
84  std::array<ProducedHistory, 2> history;
85  };
86 
87  struct AcceptedCargo {
89  uint16_t waiting;
90  TimerGameEconomy::Date last_accepted;
91  };
92 
93  using ProducedCargoArray = std::array<ProducedCargo, INDUSTRY_NUM_OUTPUTS>;
94  using AcceptedCargoArray = std::array<AcceptedCargo, INDUSTRY_NUM_INPUTS>;
95 
99  ProducedCargoArray produced;
100  AcceptedCargoArray accepted;
101  byte prod_level;
102  uint16_t counter;
103 
104  IndustryType type;
106  Colours random_colour;
110 
113  mutable std::string cached_name;
114 
121  std::string text;
122 
123  uint16_t random;
124 
126 
127  Industry(TileIndex tile = INVALID_TILE) : location(tile, 0, 0) {}
128  ~Industry();
129 
131 
137  inline bool TileBelongsToIndustry(TileIndex tile) const
138  {
139  return IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == this->index;
140  }
141 
147  inline ProducedCargoArray::iterator GetCargoProduced(CargoID cargo)
148  {
149  if (!IsValidCargoID(cargo)) return std::end(this->produced);
150  return std::find_if(std::begin(this->produced), std::end(this->produced), [&cargo](const auto &p) { return p.cargo == cargo; });
151  }
152 
158  inline ProducedCargoArray::const_iterator GetCargoProduced(CargoID cargo) const
159  {
160  if (!IsValidCargoID(cargo)) return std::end(this->produced);
161  return std::find_if(std::begin(this->produced), std::end(this->produced), [&cargo](const auto &p) { return p.cargo == cargo; });
162  }
163 
169  inline AcceptedCargoArray::iterator GetCargoAccepted(CargoID cargo)
170  {
171  if (!IsValidCargoID(cargo)) return std::end(this->accepted);
172  return std::find_if(std::begin(this->accepted), std::end(this->accepted), [&cargo](const auto &a) { return a.cargo == cargo; });
173  }
174 
179  bool IsCargoAccepted() const { return std::any_of(std::begin(this->accepted), std::end(this->accepted), [](const auto &a) { return IsValidCargoID(a.cargo); }); }
180 
185  bool IsCargoProduced() const { return std::any_of(std::begin(this->produced), std::end(this->produced), [](const auto &p) { return IsValidCargoID(p.cargo); }); }
186 
192  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; }); }
193 
199  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; }); }
200 
207  static inline Industry *GetByTile(TileIndex tile)
208  {
209  return Industry::Get(GetIndustryIndex(tile));
210  }
211 
212  static Industry *GetRandom();
213  static void PostDestructor(size_t index);
214 
220  static inline void IncIndustryTypeCount(IndustryType type)
221  {
222  assert(type < NUM_INDUSTRYTYPES);
223  counts[type]++;
224  }
225 
231  static inline void DecIndustryTypeCount(IndustryType type)
232  {
233  assert(type < NUM_INDUSTRYTYPES);
234  counts[type]--;
235  }
236 
242  static inline uint16_t GetIndustryTypeCount(IndustryType type)
243  {
244  assert(type < NUM_INDUSTRYTYPES);
245  return counts[type];
246  }
247 
249  static inline void ResetIndustryCounts()
250  {
251  memset(&counts, 0, sizeof(counts));
252  }
253 
254  inline const std::string &GetCachedName() const
255  {
256  if (this->cached_name.empty()) this->FillCachedName();
257  return this->cached_name;
258  }
259 
260 private:
261  void FillCachedName() const;
262 
263 protected:
264  static uint16_t counts[NUM_INDUSTRYTYPES];
265 };
266 
267 void ClearAllIndustryCachedNames();
268 
269 void PlantRandomFarmField(const Industry *i);
270 
271 void ReleaseDisastersTargetingIndustry(IndustryID);
272 
274 
277  uint32_t probability;
278  byte min_number;
279  uint16_t target_count;
280  uint16_t max_wait;
281  uint16_t wait_count;
282 
283  void Reset();
284 
285  bool GetIndustryTypeData(IndustryType it);
286 };
287 
293  uint32_t wanted_inds;
294 
295  void Reset();
296 
297  void SetupTargetCount();
298  void TryBuildNewIndustry();
299 
300  void EconomyMonthlyLoop();
301 };
302 
304 
305 
308  IDIWD_FORCE_REBUILD,
309  IDIWD_PRODUCTION_CHANGE,
310  IDIWD_FORCE_RESORT,
311 };
312 
313 #endif /* INDUSTRY_H */
Industry::IsCargoAccepted
bool IsCargoAccepted(CargoID cargo) const
Test if this industry accepts a specific cargo.
Definition: industry.h:192
Industry::AcceptedCargo::waiting
uint16_t waiting
Amount of cargo waiting to processed.
Definition: industry.h:89
PRODLEVEL_MINIMUM
@ PRODLEVEL_MINIMUM
below this level, the industry is set to be closing
Definition: industry.h:35
Industry::owner
Owner owner
owner of the industry. Which SHOULD always be (imho) OWNER_NONE
Definition: industry.h:105
NUM_INDUSTRYTYPES
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
IndustryBuildData::wanted_inds
uint32_t wanted_inds
Number of wanted industries (bits 31-16), and a fraction (bits 15-0).
Definition: industry.h:293
Pool::PoolItem<&_industry_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:335
Industry::ProducedHistory::transported
uint16_t transported
Total transported.
Definition: industry.h:71
Industry::part_of_subsidy
PartOfSubsidy part_of_subsidy
NOSAVE: is this industry a source/destination of a subsidy?
Definition: industry.h:111
INDCTL_NONE
@ INDCTL_NONE
No flags in effect.
Definition: industry.h:46
Industry::counts
static uint16_t counts[NUM_INDUSTRYTYPES]
Number of industries per type ingame.
Definition: industry.h:264
INDCTL_NO_CLOSURE
@ INDCTL_NO_CLOSURE
Industry can not close regardless of production level or time since last delivery.
Definition: industry.h:54
IndustryBuildData::builddata
IndustryTypeBuildData builddata[NUM_INDUSTRYTYPES]
Industry build data for every industry type.
Definition: industry.h:292
IndustryTypeBuildData::max_wait
uint16_t max_wait
Starting number of turns to wait (copied to wait_count).
Definition: industry.h:280
timer_game_calendar.h
Industry::ProducedCargo::history
std::array< ProducedHistory, 2 > history
History of cargo produced and transported.
Definition: industry.h:84
Station
Station data structure.
Definition: station_base.h:442
PRODLEVEL_CLOSURE
@ PRODLEVEL_CLOSURE
signal set to actually close the industry
Definition: industry.h:34
IndustryBuildData::EconomyMonthlyLoop
void EconomyMonthlyLoop()
Monthly update of industry build data.
Definition: industry_cmd.cpp:2422
Industry::ProducedCargo::cargo
CargoID cargo
Cargo type.
Definition: industry.h:81
Industry::ProducedHistory::production
uint16_t production
Total produced.
Definition: industry.h:70
Pool::PoolItem<&_industry_pool >::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:234
Industry::was_cargo_delivered
byte was_cargo_delivered
flag that indicate this has been the closest industry chosen for cargo delivery by a station....
Definition: industry.h:108
Industry::IsCargoProduced
bool IsCargoProduced(CargoID cargo) const
Test if this industry produces a specific cargo.
Definition: industry.h:199
INVALID_TILE
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:95
subsidy_type.h
Industry::AcceptedCargo::cargo
CargoID cargo
Cargo type.
Definition: industry.h:88
Industry::produced
ProducedCargoArray produced
INDUSTRY_NUM_OUTPUTS production cargo slots.
Definition: industry.h:99
Industry::RecomputeProductionMultipliers
void RecomputeProductionMultipliers()
Recompute #production_rate for current prod_level.
Definition: industry_cmd.cpp:2508
Industry::GetCargoProduced
ProducedCargoArray::iterator GetCargoProduced(CargoID cargo)
Get produced cargo slot for a specific cargo type.
Definition: industry.h:147
MP_INDUSTRY
@ MP_INDUSTRY
Part of an industry.
Definition: tile_type.h:56
Industry::AcceptedCargo::last_accepted
TimerGameEconomy::Date last_accepted
Last day cargo was accepted by this industry.
Definition: industry.h:90
StrongType::Typedef
Templated helper to make a type-safe 'typedef' representing a single POD value.
Definition: strong_typedef_type.hpp:150
Industry::construction_type
uint8_t construction_type
Way the industry was constructed (.
Definition: industry.h:117
Industry::exclusive_consumer
Owner exclusive_consumer
Which company has exclusive rights to take cargo (INVALID_OWNER = anyone)
Definition: industry.h:120
PRODLEVEL_DEFAULT
@ PRODLEVEL_DEFAULT
default level set when the industry is created
Definition: industry.h:36
Industry
Defines the internal data of a functional industry.
Definition: industry.h:68
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
Industry::ctlflags
IndustryControlFlags ctlflags
flags overriding standard behaviours
Definition: industry.h:109
Industry::neutral_station
Station * neutral_station
Associated neutral station.
Definition: industry.h:98
industry_map.h
Industry::GetRandom
static Industry * GetRandom()
Return a random valid industry.
Definition: industry_cmd.cpp:221
Industry::ProducedCargo::waiting
uint16_t waiting
Amount of cargo produced.
Definition: industry.h:82
Industry::PostDestructor
static void PostDestructor(size_t index)
Invalidating some stuff after removing item from the pool.
Definition: industry_cmd.cpp:210
Industry::stations_near
StationList stations_near
NOSAVE: List of nearby stations.
Definition: industry.h:112
Industry::location
TileArea location
Location of the industry.
Definition: industry.h:96
IndustryTypeBuildData::Reset
void Reset()
Reset the entry.
Definition: industry_cmd.cpp:2402
Industry::cached_name
std::string cached_name
NOSAVE: Cache of the resolved name of the industry.
Definition: industry.h:113
Industry::GetByTile
static Industry * GetByTile(TileIndex tile)
Get the industry of the given tile.
Definition: industry.h:207
Industry::exclusive_supplier
Owner exclusive_supplier
Which company has exclusive rights to deliver cargo (INVALID_OWNER = anyone)
Definition: industry.h:119
Industry::type
IndustryType type
type of industry.
Definition: industry.h:104
IndustryBuildData
Data for managing the number and type of industries in the game.
Definition: industry.h:291
PersistentStorage
Class for pooled persistent storage of data.
Definition: newgrf_storage.h:199
OrthogonalTileArea
Represents the covered area of e.g.
Definition: tilearea_type.h:18
Industry::DecIndustryTypeCount
static void DecIndustryTypeCount(IndustryType type)
Decrement the count of industries for this type.
Definition: industry.h:231
Industry::accepted
AcceptedCargoArray accepted
INDUSTRY_NUM_INPUTS input cargo slots.
Definition: industry.h:100
IndustryTypeBuildData::probability
uint32_t probability
Relative probability of building this industry.
Definition: industry.h:277
IndustryTypeBuildData::wait_count
uint16_t wait_count
Number of turns to wait before trying to build again.
Definition: industry.h:281
PRODLEVEL_MAXIMUM
@ PRODLEVEL_MAXIMUM
the industry is running at full speed
Definition: industry.h:37
GetIndustryIndex
IndustryID GetIndustryIndex(Tile t)
Get the industry ID of the given tile.
Definition: industry_map.h:63
Industry::ResetIndustryCounts
static void ResetIndustryCounts()
Resets industry counts.
Definition: industry.h:249
IndustryBuildData::SetupTargetCount
void SetupTargetCount()
Decide how many industries of each type are needed.
Definition: industry_cmd.cpp:2548
IndustryDirectoryInvalidateWindowData
IndustryDirectoryInvalidateWindowData
Special values for the industry list window for the data parameter of InvalidateWindowData.
Definition: industry.h:307
Industry::AcceptedCargo
Definition: industry.h:87
IndustryBuildData::TryBuildNewIndustry
void TryBuildNewIndustry()
Try to create a random industry, during gameplay.
Definition: industry_cmd.cpp:2593
PROCESSING_INDUSTRY_ABANDONMENT_YEARS
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
Industry::text
std::string text
General text with additional information.
Definition: industry.h:121
StationList
std::set< Station *, StationCompare > StationList
List of stations.
Definition: station_type.h:94
Industry::ProducedCargo
Definition: industry.h:80
Industry::random_colour
Colours random_colour
randomized colour of the industry, for display purpose
Definition: industry.h:106
IsTileForestIndustry
bool IsTileForestIndustry(TileIndex tile)
Check whether the tile is a forest.
Definition: industry_cmd.cpp:974
Industry::town
Town * town
Nearest town.
Definition: industry.h:97
IndustryBuildData::Reset
void Reset()
Completely reset the industry build data.
Definition: industry_cmd.cpp:2412
tilearea_type.h
station_base.h
Industry::selected_layout
byte selected_layout
Which tile layout was used when creating the industry.
Definition: industry.h:118
Pool
Base class for all pools.
Definition: pool_type.hpp:80
Industry::counter
uint16_t counter
used for animation and/or production (if available cargo)
Definition: industry.h:102
Industry::ProducedHistory
Definition: industry.h:69
Industry::founder
Owner founder
Founder of the industry.
Definition: industry.h:115
Industry::IncIndustryTypeCount
static void IncIndustryTypeCount(IndustryType type)
Increment the count of industries for this type.
Definition: industry.h:220
Industry::GetCargoAccepted
AcceptedCargoArray::iterator GetCargoAccepted(CargoID cargo)
Get accepted cargo slot for a specific cargo type.
Definition: industry.h:169
industrytype.h
Industry::GetCargoProduced
ProducedCargoArray::const_iterator GetCargoProduced(CargoID cargo) const
Get produced cargo slot for a specific cargo type (const-variant).
Definition: industry.h:158
Industry::GetIndustryTypeCount
static uint16_t GetIndustryTypeCount(IndustryType type)
Get the count of industries for this type.
Definition: industry.h:242
IndustryTypeBuildData::min_number
byte min_number
Smallest number of industries that should exist (either 0 or 1).
Definition: industry.h:278
IndustryTypeBuildData
Data for managing the number of industries of a single industry type.
Definition: industry.h:276
ProductionLevels
ProductionLevels
Production level maximum, minimum and default values.
Definition: industry.h:33
PartOfSubsidy
PartOfSubsidy
What part of a subsidy is something?
Definition: subsidy_type.h:16
DECLARE_ENUM_AS_BIT_SET
DECLARE_ENUM_AS_BIT_SET(GenderEthnicity) enum CompanyManagerFaceVariable
Bitgroups of the CompanyManagerFace variable.
Definition: company_manager_face.h:29
Industry::IsCargoProduced
bool IsCargoProduced() const
Test if this industry produces any cargo.
Definition: industry.h:185
Town
Town data structure.
Definition: town.h:50
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
INDCTL_NO_PRODUCTION_DECREASE
@ INDCTL_NO_PRODUCTION_DECREASE
When industry production change is evaluated, rolls to decrease are ignored.
Definition: industry.h:48
IsValidCargoID
bool IsValidCargoID(CargoID t)
Test whether cargo type is not INVALID_CARGO.
Definition: cargo_type.h:90
Industry::last_prod_year
TimerGameEconomy::Year last_prod_year
last economy year of production
Definition: industry.h:107
_industry_builder
IndustryBuildData _industry_builder
In-game manager of industries.
Definition: industry_cmd.cpp:70
Industry::IsCargoAccepted
bool IsCargoAccepted() const
Test if this industry accepts any cargo.
Definition: industry.h:179
IsTileType
static debug_inline bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
IndustryTypeBuildData::target_count
uint16_t target_count
Desired number of industries of this type.
Definition: industry.h:279
INDCTL_MASK
@ INDCTL_MASK
Mask of all flags set.
Definition: industry.h:58
Industry::prod_level
byte prod_level
general production level
Definition: industry.h:101
ReleaseDisastersTargetingIndustry
void ReleaseDisastersTargetingIndustry(IndustryID)
Marks all disasters targeting this industry in such a way they won't call Industry::Get(v->dest_tile)...
Definition: disaster_vehicle.cpp:958
Industry::construction_date
TimerGameCalendar::Date construction_date
Date of the construction of the industry.
Definition: industry.h:116
Industry::psa
PersistentStorage * psa
Persistent storage for NewGRF industries.
Definition: industry.h:125
INDCTL_NO_PRODUCTION_INCREASE
@ INDCTL_NO_PRODUCTION_INCREASE
When industry production change is evaluated, rolls to increase are ignored.
Definition: industry.h:50
IndustryControlFlags
IndustryControlFlags
Flags to control/override the behaviour of an industry.
Definition: industry.h:44
Industry::ProducedCargo::rate
uint8_t rate
Production rate.
Definition: industry.h:83
newgrf_storage.h
Pool::PoolItem
Base class for all PoolItems.
Definition: pool_type.hpp:233
INDCTL_EXTERNAL_PROD_LEVEL
@ INDCTL_EXTERNAL_PROD_LEVEL
Indicates that the production level of the industry is externally controlled.
Definition: industry.h:56
IndustryTypeBuildData::GetIndustryTypeData
bool GetIndustryTypeData(IndustryType it)
Set the probability and min_number fields for the industry type it for a running game.
Definition: industry_cmd.cpp:2537
timer_game_economy.h
Industry::random
uint16_t random
Random value used for randomisation of all kinds of things.
Definition: industry.h:123
Industry::TileBelongsToIndustry
bool TileBelongsToIndustry(TileIndex tile) const
Check if a given tile belongs to this industry.
Definition: industry.h:137