OpenTTD Source 20260621-master-g720d10536d
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef INDUSTRY_H
11#define INDUSTRY_H
12
13#include "core/flatset_type.hpp"
14#include "misc/history_type.hpp"
15#include "newgrf_storage.h"
16#include "subsidy_type.h"
17#include "industry_map.h"
18#include "industrytype.h"
19#include "tilearea_type.h"
20#include "station_base.h"
23
24
25typedef Pool<Industry, IndustryID, 64> IndustryPool;
26extern IndustryPool _industry_pool;
27
29
30/*
31 * Production level maximum, minimum and default values.
32 * It is not a value been really used in order to change, but rather an indicator
33 * of how the industry is behaving.
34 */
35static constexpr uint8_t PRODLEVEL_CLOSURE = 0x00;
36static constexpr uint8_t PRODLEVEL_MINIMUM = 0x04;
37static constexpr uint8_t PRODLEVEL_DEFAULT = 0x10;
38static constexpr uint8_t PRODLEVEL_MAXIMUM = 0x80;
39
57
60
64struct Industry : IndustryPool::PoolItem<&_industry_pool> {
66 uint16_t production = 0;
67 uint16_t transported = 0;
68
69 uint8_t PctTransported() const
70 {
71 if (this->production == 0) return 0;
72 return ClampTo<uint8_t>(this->transported * 256 / this->production);
73 }
74 };
76 CargoType cargo = INVALID_CARGO;
77 uint16_t waiting = 0;
78 uint8_t rate = 0;
80 };
81
83 uint16_t accepted = 0;
84 uint16_t waiting = 0;
85 };
86
88 CargoType cargo = INVALID_CARGO;
89 uint16_t waiting = 0;
90 uint32_t accumulated_waiting = 0;
92 std::unique_ptr<HistoryData<AcceptedHistory>> history{};
93
99 {
100 if (this->history == nullptr) this->history = std::make_unique<HistoryData<AcceptedHistory>>();
101 return *this->history;
102 }
103 };
104
105 using ProducedCargoes = std::vector<ProducedCargo>;
106 using AcceptedCargoes = std::vector<AcceptedCargo>;
107
109 Town *town = nullptr;
112 ProducedCargoes produced{};
113 AcceptedCargoes accepted{};
114 uint8_t prod_level = 0;
115 uint16_t counter = 0;
116
117 IndustryType type = 0;
123
126 mutable std::string cached_name{};
127
131 uint8_t selected_layout = 0;
135
136 uint16_t random = 0;
137
139
140 Industry(IndustryID index, TileIndex tile = INVALID_TILE) : IndustryPool::PoolItem<&_industry_pool>(index), location(tile, 0, 0) {}
141 ~Industry();
142
144
150 inline bool TileBelongsToIndustry(TileIndex tile) const
151 {
152 return IsTileType(tile, TileType::Industry) && GetIndustryIndex(tile) == this->index;
153 }
154
160 inline const ProducedCargo &GetProduced(size_t slot) const
161 {
162 static const ProducedCargo empty{INVALID_CARGO, 0, 0, {}};
163 return slot < this->produced.size() ? this->produced[slot] : empty;
164 }
165
171 inline const AcceptedCargo &GetAccepted(size_t slot) const
172 {
173 static const AcceptedCargo empty{INVALID_CARGO, 0, 0, {}, {}};
174 return slot < this->accepted.size() ? this->accepted[slot] : empty;
175 }
176
182 inline ProducedCargoes::iterator GetCargoProduced(CargoType cargo)
183 {
184 if (!IsValidCargoType(cargo)) return std::end(this->produced);
185 return std::ranges::find(this->produced, cargo, &ProducedCargo::cargo);
186 }
187
193 inline ProducedCargoes::const_iterator GetCargoProduced(CargoType cargo) const
194 {
195 if (!IsValidCargoType(cargo)) return std::end(this->produced);
196 return std::ranges::find(this->produced, cargo, &ProducedCargo::cargo);
197 }
198
204 inline AcceptedCargoes::iterator GetCargoAccepted(CargoType cargo)
205 {
206 if (!IsValidCargoType(cargo)) return std::end(this->accepted);
207 return std::ranges::find(this->accepted, cargo, &AcceptedCargo::cargo);
208 }
209
215 inline AcceptedCargoes::const_iterator GetCargoAccepted(CargoType cargo) const
216 {
217 if (!IsValidCargoType(cargo)) return std::end(this->accepted);
218 return std::ranges::find(this->accepted, cargo, &AcceptedCargo::cargo);
219 }
220
225 bool IsCargoAccepted() const { return std::any_of(std::begin(this->accepted), std::end(this->accepted), [](const auto &a) { return IsValidCargoType(a.cargo); }); }
226
231 bool IsCargoProduced() const { return std::any_of(std::begin(this->produced), std::end(this->produced), [](const auto &p) { return IsValidCargoType(p.cargo); }); }
232
238 bool IsCargoAccepted(CargoType cargo) const { return std::any_of(std::begin(this->accepted), std::end(this->accepted), [&cargo](const auto &a) { return a.cargo == cargo; }); }
239
245 bool IsCargoProduced(CargoType cargo) const { return std::any_of(std::begin(this->produced), std::end(this->produced), [&cargo](const auto &p) { return p.cargo == cargo; }); }
246
253 static inline Industry *GetByTile(TileIndex tile)
254 {
255 return Industry::Get(GetIndustryIndex(tile));
256 }
257
258 static Industry *GetRandom();
259 static void PostDestructor(size_t index);
260
267 static inline uint16_t GetIndustryTypeCount(IndustryType type)
268 {
269 assert(type < NUM_INDUSTRYTYPES);
270 return static_cast<uint16_t>(std::size(industries[type]));
271 }
272
273 inline const std::string &GetCachedName() const
274 {
275 if (this->cached_name.empty()) this->FillCachedName();
276 return this->cached_name;
277 }
278
279 static std::array<FlatSet<IndustryID>, NUM_INDUSTRYTYPES> industries;
280
281private:
282 void FillCachedName() const;
283};
284
285void ClearAllIndustryCachedNames();
286
287void PlantRandomFarmField(const Industry *i);
288
289void ReleaseDisastersTargetingIndustry(IndustryID);
290
292
295 uint32_t probability;
296 uint8_t min_number;
297 uint16_t target_count;
298 uint16_t max_wait;
299 uint16_t wait_count;
300
301 void Reset();
302
303 bool GetIndustryTypeData(IndustryType it);
304};
305
320
322
323
326 IDIWD_FORCE_REBUILD,
327 IDIWD_PRODUCTION_CHANGE,
328 IDIWD_FORCE_RESORT,
329};
330
332
333#endif /* INDUSTRY_H */
bool IsValidCargoType(CargoType cargo)
Test whether cargo type is not INVALID_CARGO.
Definition cargo_type.h:110
CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:22
Container for an encoded string, created by GetEncodedString.
Enum-as-bit-set wrapper.
StrongType::Typedef< int32_t, struct YearTag< struct Economy >, StrongType::Compare, StrongType::Integer > Year
StrongType::Typedef< int32_t, DateTag< struct Economy >, StrongType::Compare, StrongType::Integer > Date
static constexpr Owner INVALID_OWNER
An invalid owner.
Flat set container implementation.
Colours
One of 16 base colours used for companies and windows/widgets.
Definition gfx_type.h:284
@ Begin
Begin marker.
Definition gfx_type.h:285
Types for storing historical data.
std::array< T, HISTORY_RECORDS > HistoryData
Container type for storing history data.
uint64_t ValidHistoryMask
Mask of valid history records.
static constexpr uint8_t PRODLEVEL_MAXIMUM
the industry is running at full speed
Definition industry.h:38
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:37
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:28
bool IsTileForestIndustry(TileIndex tile)
Check whether the tile is a forest.
IndustryBuildData _industry_builder
In-game manager of industries.
IndustryDirectoryInvalidateWindowData
Special values for the industry list window for the data parameter of InvalidateWindowData.
Definition industry.h:325
EnumBitSet< IndustryControlFlag, uint8_t, IndustryControlFlag::End > IndustryControlFlags
Bitset of IndustryControlFlag elements.
Definition industry.h:59
static constexpr uint8_t PRODLEVEL_MINIMUM
below this level, the industry is set to be closing
Definition industry.h:36
static constexpr uint8_t PRODLEVEL_CLOSURE
signal set to actually close the industry
Definition industry.h:35
void TrimIndustryAcceptedProduced(Industry *ind)
Remove unused industry accepted/produced slots – entries after the last slot with valid cargo.
IndustryControlFlag
Flags to control/override the behaviour of an industry.
Definition industry.h:44
@ End
End marker.
Definition industry.h:55
@ ExternalProdLevel
Indicates that the production level of the industry is externally controlled.
Definition industry.h:54
@ NoClosure
Industry can not close regardless of production level or time since last delivery.
Definition industry.h:52
@ NoProductionDecrease
When industry production change is evaluated, rolls to decrease are ignored.
Definition industry.h:46
@ NoProductionIncrease
When industry production change is evaluated, rolls to increase are ignored.
Definition industry.h:48
Accessors to map for industries.
IndustryID GetIndustryIndex(Tile t)
Get the industry ID of the given tile.
static const IndustryType NUM_INDUSTRYTYPES
total number of industry types, new and old; limited to 240 because we need some special ids like IT_...
Industry type specs.
IndustryConstructionType
How was the industry created.
constexpr To ClampTo(From value)
Clamp the given value down to lie within the requested type.
Functionality related to the temporary and persistent storage arrays for NewGRFs.
Base classes/functions for stations.
std::set< Station *, StationCompare > StationList
List of stations.
Data for managing the number and type of industries in the game.
Definition industry.h:309
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:311
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:310
Data for managing the number of industries of a single industry type.
Definition industry.h:294
uint32_t probability
Relative probability of building this industry.
Definition industry.h:295
uint16_t target_count
Desired number of industries of this type.
Definition industry.h:297
uint8_t min_number
Smallest number of industries that should exist (either 0 or 1).
Definition industry.h:296
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:299
uint16_t max_wait
Starting number of turns to wait (copied to wait_count).
Definition industry.h:298
uint32_t accumulated_waiting
Accumulated waiting total over the last month, used to calculate average.
Definition industry.h:90
CargoType cargo
Cargo type.
Definition industry.h:88
uint16_t waiting
Amount of cargo waiting to processed.
Definition industry.h:89
HistoryData< AcceptedHistory > & GetOrCreateHistory()
Get history data, creating it if necessary.
Definition industry.h:98
std::unique_ptr< HistoryData< AcceptedHistory > > history
History of accepted and waiting cargo.
Definition industry.h:92
TimerGameEconomy::Date last_accepted
Last day cargo was accepted by this industry.
Definition industry.h:91
uint16_t accepted
Total accepted.
Definition industry.h:83
uint16_t waiting
Average waiting.
Definition industry.h:84
uint16_t waiting
Amount of cargo produced.
Definition industry.h:77
CargoType cargo
Cargo type.
Definition industry.h:76
HistoryData< ProducedHistory > history
History of cargo produced and transported for this month and 24 previous months.
Definition industry.h:79
uint8_t rate
Production rate.
Definition industry.h:78
uint16_t transported
Total transported.
Definition industry.h:67
uint16_t production
Total produced.
Definition industry.h:66
Defines the internal data of a functional industry.
Definition industry.h:64
static Industry * GetRandom()
Return a random valid industry.
IndustryType type
type of industry.
Definition industry.h:117
Owner exclusive_supplier
Which company has exclusive rights to deliver cargo (INVALID_OWNER = anyone).
Definition industry.h:132
TimerGameCalendar::Date construction_date
Date of the construction of the industry.
Definition industry.h:129
IndustryControlFlags ctlflags
flags overriding standard behaviours
Definition industry.h:122
bool IsCargoAccepted() const
Test if this industry accepts any cargo.
Definition industry.h:225
PersistentStorage * psa
Persistent storage for NewGRF industries.
Definition industry.h:138
uint8_t prod_level
general production level
Definition industry.h:114
Colours random_colour
randomized colour of the industry, for display purpose
Definition industry.h:119
void RecomputeProductionMultipliers()
Recompute production_rate for current prod_level.
EncodedString text
General text with additional information.
Definition industry.h:134
ProducedCargoes::iterator GetCargoProduced(CargoType cargo)
Get produced cargo slot for a specific cargo type.
Definition industry.h:182
std::string cached_name
NOSAVE: Cache of the resolved name of the industry.
Definition industry.h:126
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:160
ValidHistoryMask valid_history
Mask of valid history records.
Definition industry.h:111
TimerGameEconomy::Year last_prod_year
last economy year of production
Definition industry.h:120
ProducedCargoes produced
produced cargo slots
Definition industry.h:112
uint16_t random
Random value used for randomisation of all kinds of things.
Definition industry.h:136
AcceptedCargoes::const_iterator GetCargoAccepted(CargoType cargo) const
Get accepted cargo slot for a specific cargo type (const-variant).
Definition industry.h:215
static void PostDestructor(size_t index)
Invalidating some stuff after removing item from the pool.
Town * town
Nearest town.
Definition industry.h:109
Owner founder
Founder of the industry.
Definition industry.h:128
ProducedCargoes::const_iterator GetCargoProduced(CargoType cargo) const
Get produced cargo slot for a specific cargo type (const-variant).
Definition industry.h:193
uint8_t selected_layout
Which tile layout was used when creating the industry.
Definition industry.h:131
AcceptedCargoes accepted
accepted cargo slots
Definition industry.h:113
static uint16_t GetIndustryTypeCount(IndustryType type)
Get the count of industries for this type.
Definition industry.h:267
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:171
bool IsCargoAccepted(CargoType cargo) const
Test if this industry accepts a specific cargo.
Definition industry.h:238
Owner owner
owner of the industry. Which SHOULD always be (imho) OWNER_NONE
Definition industry.h:118
static Industry * GetByTile(TileIndex tile)
Get the industry of the given tile.
Definition industry.h:253
PartsOfSubsidy part_of_subsidy
NOSAVE: is this industry a source/destination of a subsidy?
Definition industry.h:124
TileArea location
Location of the industry.
Definition industry.h:108
bool IsCargoProduced(CargoType cargo) const
Test if this industry produces a specific cargo.
Definition industry.h:245
Station * neutral_station
Associated neutral station.
Definition industry.h:110
~Industry()
Remove any reference to this industry from the game.
IndustryConstructionType construction_type
Way the industry was constructed (.
Definition industry.h:130
StationList stations_near
NOSAVE: List of nearby stations.
Definition industry.h:125
bool IsCargoProduced() const
Test if this industry produces any cargo.
Definition industry.h:231
Owner exclusive_consumer
Which company has exclusive rights to take cargo (INVALID_OWNER = anyone).
Definition industry.h:133
uint16_t counter
used for animation and/or production (if available cargo)
Definition industry.h:115
uint8_t was_cargo_delivered
flag that indicate this has been the closest industry chosen for cargo delivery by a station....
Definition industry.h:121
bool TileBelongsToIndustry(TileIndex tile) const
Check if a given tile belongs to this industry.
Definition industry.h:150
AcceptedCargoes::iterator GetCargoAccepted(CargoType cargo)
Get accepted cargo slot for a specific cargo type.
Definition industry.h:204
static std::array< FlatSet< IndustryID >, NUM_INDUSTRYTYPES > industries
List of industries of each type.
Definition industry.h:279
Class for pooled persistent storage of data.
static Industry * Get(auto index)
Base class for all pools.
Station data structure.
Town data structure.
Definition town.h:64
Basic types related to subsidies.
EnumBitSet< PartOfSubsidy, uint8_t > PartsOfSubsidy
Bitset of PartOfSubsidy elements.
static bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:100
@ Industry
Part of an industry.
Definition tile_type.h:57
Type for storing the 'area' of something uses on the map.
OrthogonalTileArea TileArea
Shorthand for the much more common orthogonal tile area.
Definition of the game-calendar-timer.
Definition of the game-economy-timer.