OpenTTD Source: src/industry.h Source File

OpenTTD Source 15.0-RC2

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
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"
21#include "timer/timer_game_calendar.h"
22#include "timer/timer_game_economy.h"
23
24
25typedef Pool<Industry, IndustryID, 64> IndustryPool;
26extern IndustryPool _industry_pool;
27
28 static const TimerGameEconomy::Year PROCESSING_INDUSTRY_ABANDONMENT_YEARS{5};
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 */
35 static constexpr uint8_t PRODLEVEL_CLOSURE = 0x00;
36 static constexpr uint8_t PRODLEVEL_MINIMUM = 0x04;
37 static constexpr uint8_t PRODLEVEL_DEFAULT = 0x10;
38 static constexpr uint8_t PRODLEVEL_MAXIMUM = 0x80;
39
44 enum class IndustryControlFlag : uint8_t {
46 NoProductionDecrease = 0,
48 NoProductionIncrease = 1,
52 NoClosure = 2,
54 ExternalProdLevel = 3,
55 End,
56};
57using IndustryControlFlags = EnumBitSet<IndustryControlFlag, uint8_t, IndustryControlFlag::End>;
58
62 struct Industry : IndustryPool::PoolItem<&_industry_pool> {
63 struct ProducedHistory {
64 uint16_t production = 0;
65 uint16_t transported = 0;
66
67 uint8_t PctTransported() const
68 {
69 if (this->production == 0) return 0;
70 return ClampTo<uint8_t>(this->transported * 256 / this->production);
71 }
72 };
73 struct ProducedCargo {
74 CargoType cargo = 0;
75 uint16_t waiting = 0;
76 uint8_t rate = 0;
77 HistoryData<ProducedHistory> history{};
78 };
79
80 struct AcceptedHistory {
81 uint16_t accepted = 0;
82 uint16_t waiting = 0;
83 };
84
85 struct AcceptedCargo {
86 CargoType cargo = 0;
87 uint16_t waiting = 0;
88 uint32_t accumulated_waiting = 0;
89 TimerGameEconomy::Date last_accepted{};
90 std::unique_ptr<HistoryData<AcceptedHistory>> history{};
91
96 inline HistoryData<AcceptedHistory> &GetOrCreateHistory()
97 {
98 if (this->history == nullptr) this->history = std::make_unique<HistoryData<AcceptedHistory>>();
99 return *this->history;
100 }
101 };
102
103 using ProducedCargoes = std::vector<ProducedCargo>;
104 using AcceptedCargoes = std::vector<AcceptedCargo>;
105
106 TileArea location{INVALID_TILE, 0, 0};
107 Town *town = nullptr;
108 Station *neutral_station = nullptr;
109 ValidHistoryMask valid_history = 0;
110 ProducedCargoes produced{};
111 AcceptedCargoes accepted{};
112 uint8_t prod_level = 0;
113 uint16_t counter = 0;
114
115 IndustryType type = 0;
116 Owner owner = INVALID_OWNER;
117 Colours random_colour = COLOUR_BEGIN;
118 TimerGameEconomy::Year last_prod_year{};
119 uint8_t was_cargo_delivered = 0;
120 IndustryControlFlags ctlflags{};
121
122 PartsOfSubsidy part_of_subsidy{};
123 StationList stations_near{};
124 mutable std::string cached_name{};
125
126 Owner founder = INVALID_OWNER;
127 TimerGameCalendar::Date construction_date{};
128 uint8_t construction_type = 0;
129 uint8_t selected_layout = 0;
130 Owner exclusive_supplier = INVALID_OWNER;
131 Owner exclusive_consumer = INVALID_OWNER;
132 EncodedString text{};
133
134 uint16_t random = 0;
135
136 PersistentStorage *psa = nullptr;
137
138 Industry(TileIndex tile = INVALID_TILE) : location(tile, 0, 0) {}
139 ~Industry();
140
141 void RecomputeProductionMultipliers();
142
148 inline bool TileBelongsToIndustry(TileIndex tile) const
149 {
150 return IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == this->index;
151 }
152
158 inline const ProducedCargo &GetProduced(size_t slot) const
159 {
160 static const ProducedCargo empty{INVALID_CARGO, 0, 0, {}};
161 return slot < this->produced.size() ? this->produced[slot] : empty;
162 }
163
169 inline const AcceptedCargo &GetAccepted(size_t slot) const
170 {
171 static const AcceptedCargo empty{INVALID_CARGO, 0, 0, {}, {}};
172 return slot < this->accepted.size() ? this->accepted[slot] : empty;
173 }
174
180 inline ProducedCargoes::iterator GetCargoProduced(CargoType cargo)
181 {
182 if (!IsValidCargoType(cargo)) return std::end(this->produced);
183 return std::ranges::find(this->produced, cargo, &ProducedCargo::cargo);
184 }
185
191 inline ProducedCargoes::const_iterator GetCargoProduced(CargoType cargo) const
192 {
193 if (!IsValidCargoType(cargo)) return std::end(this->produced);
194 return std::ranges::find(this->produced, cargo, &ProducedCargo::cargo);
195 }
196
202 inline AcceptedCargoes::iterator GetCargoAccepted(CargoType cargo)
203 {
204 if (!IsValidCargoType(cargo)) return std::end(this->accepted);
205 return std::ranges::find(this->accepted, cargo, &AcceptedCargo::cargo);
206 }
207
213 inline AcceptedCargoes::const_iterator GetCargoAccepted(CargoType cargo) const
214 {
215 if (!IsValidCargoType(cargo)) return std::end(this->accepted);
216 return std::ranges::find(this->accepted, cargo, &AcceptedCargo::cargo);
217 }
218
223 bool IsCargoAccepted() const { return std::any_of(std::begin(this->accepted), std::end(this->accepted), [](const auto &a) { return IsValidCargoType(a.cargo); }); }
224
229 bool IsCargoProduced() const { return std::any_of(std::begin(this->produced), std::end(this->produced), [](const auto &p) { return IsValidCargoType(p.cargo); }); }
230
236 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; }); }
237
243 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; }); }
244
251 static inline Industry *GetByTile(TileIndex tile)
252 {
253 return Industry::Get(GetIndustryIndex(tile));
254 }
255
256 static Industry *GetRandom();
257 static void PostDestructor(size_t index);
258
264 static inline uint16_t GetIndustryTypeCount(IndustryType type)
265 {
266 assert(type < NUM_INDUSTRYTYPES);
267 return static_cast<uint16_t>(std::size(industries[type]));
268 }
269
270 inline const std::string &GetCachedName() const
271 {
272 if (this->cached_name.empty()) this->FillCachedName();
273 return this->cached_name;
274 }
275
276 static std::array<FlatSet<IndustryID>, NUM_INDUSTRYTYPES> industries;
277
278private:
279 void FillCachedName() const;
280};
281
282void ClearAllIndustryCachedNames();
283
284void PlantRandomFarmField(const Industry *i);
285
286void ReleaseDisastersTargetingIndustry(IndustryID);
287
288bool IsTileForestIndustry(TileIndex tile);
289
291 struct IndustryTypeBuildData {
292 uint32_t probability;
293 uint8_t min_number;
294 uint16_t target_count;
295 uint16_t max_wait;
296 uint16_t wait_count;
297
298 void Reset();
299
300 bool GetIndustryTypeData(IndustryType it);
301};
302
306 struct IndustryBuildData {
307 IndustryTypeBuildData builddata[NUM_INDUSTRYTYPES];
308 uint32_t wanted_inds;
309
310 void Reset();
311
312 void SetupTargetCount();
313 void TryBuildNewIndustry();
314
315 void EconomyMonthlyLoop();
316};
317
318extern IndustryBuildData _industry_builder;
319
320
322 enum IndustryDirectoryInvalidateWindowData : uint8_t {
323 IDIWD_FORCE_REBUILD,
324 IDIWD_PRODUCTION_CHANGE,
325 IDIWD_FORCE_RESORT,
326};
327
328void TrimIndustryAcceptedProduced(Industry *ind);
329
330#endif /* INDUSTRY_H */
CargoType
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:21
IsValidCargoType
bool IsValidCargoType(CargoType cargo)
Test whether cargo type is not INVALID_CARGO.
Definition cargo_type.h:104
EncodedString
Container for an encoded string, created by GetEncodedString.
Definition strings_type.h:98
EnumBitSet< IndustryControlFlag, uint8_t, IndustryControlFlag::End >
CommandType::End
@ End
End marker.
INVALID_OWNER
static constexpr Owner INVALID_OWNER
An invalid owner.
Definition company_type.h:30
flatset_type.hpp
Flat set container implementation.
history_type.hpp
Types for storing historical data.
HistoryData
std::array< T, HISTORY_RECORDS > HistoryData
Container type for storing history data.
Definition history_type.hpp:53
ValidHistoryMask
uint64_t ValidHistoryMask
Mask of valid history records.
Definition history_type.hpp:56
PRODLEVEL_MAXIMUM
static constexpr uint8_t PRODLEVEL_MAXIMUM
the industry is running at full speed
Definition industry.h:38
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:962
PRODLEVEL_DEFAULT
static constexpr uint8_t PRODLEVEL_DEFAULT
default level set when the industry is created
Definition industry.h:37
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:28
IsTileForestIndustry
bool IsTileForestIndustry(TileIndex tile)
Check whether the tile is a forest.
Definition industry_cmd.cpp:978
_industry_builder
IndustryBuildData _industry_builder
In-game manager of industries.
Definition industry_cmd.cpp:73
IndustryDirectoryInvalidateWindowData
IndustryDirectoryInvalidateWindowData
Special values for the industry list window for the data parameter of InvalidateWindowData.
Definition industry.h:322
PRODLEVEL_MINIMUM
static constexpr uint8_t PRODLEVEL_MINIMUM
below this level, the industry is set to be closing
Definition industry.h:36
PRODLEVEL_CLOSURE
static constexpr uint8_t PRODLEVEL_CLOSURE
signal set to actually close the industry
Definition industry.h:35
TrimIndustryAcceptedProduced
void TrimIndustryAcceptedProduced(Industry *ind)
Remove unused industry accepted/produced slots – entries after the last slot with valid cargo.
Definition industry_cmd.cpp:3292
IndustryControlFlag
IndustryControlFlag
Flags to control/override the behaviour of an industry.
Definition industry.h:44
IndustryControlFlag::ExternalProdLevel
@ ExternalProdLevel
Indicates that the production level of the industry is externally controlled.
IndustryControlFlag::NoClosure
@ NoClosure
Industry can not close regardless of production level or time since last delivery.
IndustryControlFlag::NoProductionDecrease
@ NoProductionDecrease
When industry production change is evaluated, rolls to decrease are ignored.
IndustryControlFlag::NoProductionIncrease
@ NoProductionIncrease
When industry production change is evaluated, rolls to increase are ignored.
industry_map.h
Accessors for industries.
GetIndustryIndex
IndustryID GetIndustryIndex(Tile t)
Get the industry ID of the given tile.
Definition industry_map.h:63
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 IT_...
Definition industry_type.h:45
industrytype.h
Industry type specs.
newgrf_storage.h
Functionality related to the temporary and persistent storage arrays for NewGRFs.
station_base.h
Base classes/functions for stations.
StationList
std::set< Station *, StationCompare > StationList
List of stations.
Definition station_type.h:131
IndustryBuildData
Data for managing the number and type of industries in the game.
Definition industry.h:306
IndustryBuildData::Reset
void Reset()
Completely reset the industry build data.
Definition industry_cmd.cpp:2436
IndustryBuildData::EconomyMonthlyLoop
void EconomyMonthlyLoop()
Monthly update of industry build data.
Definition industry_cmd.cpp:2446
IndustryBuildData::wanted_inds
uint32_t wanted_inds
Number of wanted industries (bits 31-16), and a fraction (bits 15-0).
Definition industry.h:308
IndustryBuildData::SetupTargetCount
void SetupTargetCount()
Decide how many industries of each type are needed.
Definition industry_cmd.cpp:2631
IndustryBuildData::TryBuildNewIndustry
void TryBuildNewIndustry()
Try to create a random industry, during gameplay.
Definition industry_cmd.cpp:2676
IndustryBuildData::builddata
IndustryTypeBuildData builddata[NUM_INDUSTRYTYPES]
Industry build data for every industry type.
Definition industry.h:307
IndustryTypeBuildData
Data for managing the number of industries of a single industry type.
Definition industry.h:291
IndustryTypeBuildData::probability
uint32_t probability
Relative probability of building this industry.
Definition industry.h:292
IndustryTypeBuildData::target_count
uint16_t target_count
Desired number of industries of this type.
Definition industry.h:294
IndustryTypeBuildData::min_number
uint8_t min_number
Smallest number of industries that should exist (either 0 or 1).
Definition industry.h:293
IndustryTypeBuildData::Reset
void Reset()
Reset the entry.
Definition industry_cmd.cpp:2426
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:2620
IndustryTypeBuildData::wait_count
uint16_t wait_count
Number of turns to wait before trying to build again.
Definition industry.h:296
IndustryTypeBuildData::max_wait
uint16_t max_wait
Starting number of turns to wait (copied to wait_count).
Definition industry.h:295
Industry::AcceptedCargo
Definition industry.h:85
Industry::AcceptedCargo::accumulated_waiting
uint32_t accumulated_waiting
Accumulated waiting total over the last month, used to calculate average.
Definition industry.h:88
Industry::AcceptedCargo::cargo
CargoType cargo
Cargo type.
Definition industry.h:86
Industry::AcceptedCargo::waiting
uint16_t waiting
Amount of cargo waiting to processed.
Definition industry.h:87
Industry::AcceptedCargo::GetOrCreateHistory
HistoryData< AcceptedHistory > & GetOrCreateHistory()
Get history data, creating it if necessary.
Definition industry.h:96
Industry::AcceptedCargo::history
std::unique_ptr< HistoryData< AcceptedHistory > > history
History of accepted and waiting cargo.
Definition industry.h:90
Industry::AcceptedCargo::last_accepted
TimerGameEconomy::Date last_accepted
Last day cargo was accepted by this industry.
Definition industry.h:89
Industry::AcceptedHistory
Definition industry.h:80
Industry::AcceptedHistory::waiting
uint16_t waiting
Total accepted.
Definition industry.h:82
Industry::ProducedCargo
Definition industry.h:73
Industry::ProducedCargo::waiting
uint16_t waiting
Amount of cargo produced.
Definition industry.h:75
Industry::ProducedCargo::cargo
CargoType cargo
Cargo type.
Definition industry.h:74
Industry::ProducedCargo::history
HistoryData< ProducedHistory > history
History of cargo produced and transported for this month and 24 previous months.
Definition industry.h:77
Industry::ProducedCargo::rate
uint8_t rate
Production rate.
Definition industry.h:76
Industry::ProducedHistory
Definition industry.h:63
Industry::ProducedHistory::transported
uint16_t transported
Total transported.
Definition industry.h:65
Industry::ProducedHistory::production
uint16_t production
Total produced.
Definition industry.h:64
Industry
Defines the internal data of a functional industry.
Definition industry.h:62
Industry::GetRandom
static Industry * GetRandom()
Return a random valid industry.
Definition industry_cmd.cpp:227
Industry::type
IndustryType type
type of industry.
Definition industry.h:115
Industry::exclusive_supplier
Owner exclusive_supplier
Which company has exclusive rights to deliver cargo (INVALID_OWNER = anyone)
Definition industry.h:130
Industry::construction_date
TimerGameCalendar::Date construction_date
Date of the construction of the industry.
Definition industry.h:127
Industry::ctlflags
IndustryControlFlags ctlflags
flags overriding standard behaviours
Definition industry.h:120
Industry::IsCargoAccepted
bool IsCargoAccepted() const
Test if this industry accepts any cargo.
Definition industry.h:223
Industry::psa
PersistentStorage * psa
Persistent storage for NewGRF industries.
Definition industry.h:136
Industry::prod_level
uint8_t prod_level
general production level
Definition industry.h:112
Industry::random_colour
Colours random_colour
randomized colour of the industry, for display purpose
Definition industry.h:117
Industry::RecomputeProductionMultipliers
void RecomputeProductionMultipliers()
Recompute #production_rate for current prod_level.
Definition industry_cmd.cpp:2591
Industry::text
EncodedString text
General text with additional information.
Definition industry.h:132
Industry::GetCargoProduced
ProducedCargoes::iterator GetCargoProduced(CargoType cargo)
Get produced cargo slot for a specific cargo type.
Definition industry.h:180
Industry::construction_type
uint8_t construction_type
Way the industry was constructed (.
Definition industry.h:128
Industry::cached_name
std::string cached_name
NOSAVE: Cache of the resolved name of the industry.
Definition industry.h:124
Industry::GetProduced
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:158
Industry::valid_history
ValidHistoryMask valid_history
Mask of valid history records.
Definition industry.h:109
Industry::last_prod_year
TimerGameEconomy::Year last_prod_year
last economy year of production
Definition industry.h:118
Industry::produced
ProducedCargoes produced
produced cargo slots
Definition industry.h:110
Industry::random
uint16_t random
Random value used for randomisation of all kinds of things.
Definition industry.h:134
Industry::GetCargoAccepted
AcceptedCargoes::const_iterator GetCargoAccepted(CargoType cargo) const
Get accepted cargo slot for a specific cargo type (const-variant).
Definition industry.h:213
Industry::PostDestructor
static void PostDestructor(size_t index)
Invalidating some stuff after removing item from the pool.
Definition industry_cmd.cpp:216
Industry::town
Town * town
Nearest town.
Definition industry.h:107
Industry::founder
Owner founder
Founder of the industry.
Definition industry.h:126
Industry::GetCargoProduced
ProducedCargoes::const_iterator GetCargoProduced(CargoType cargo) const
Get produced cargo slot for a specific cargo type (const-variant).
Definition industry.h:191
Industry::selected_layout
uint8_t selected_layout
Which tile layout was used when creating the industry.
Definition industry.h:129
Industry::accepted
AcceptedCargoes accepted
accepted cargo slots
Definition industry.h:111
Industry::GetIndustryTypeCount
static uint16_t GetIndustryTypeCount(IndustryType type)
Get the count of industries for this type.
Definition industry.h:264
Industry::GetAccepted
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:169
Industry::IsCargoAccepted
bool IsCargoAccepted(CargoType cargo) const
Test if this industry accepts a specific cargo.
Definition industry.h:236
Industry::owner
Owner owner
owner of the industry. Which SHOULD always be (imho) OWNER_NONE
Definition industry.h:116
Industry::GetByTile
static Industry * GetByTile(TileIndex tile)
Get the industry of the given tile.
Definition industry.h:251
Industry::part_of_subsidy
PartsOfSubsidy part_of_subsidy
NOSAVE: is this industry a source/destination of a subsidy?
Definition industry.h:122
Industry::location
TileArea location
Location of the industry.
Definition industry.h:106
Industry::IsCargoProduced
bool IsCargoProduced(CargoType cargo) const
Test if this industry produces a specific cargo.
Definition industry.h:243
Industry::neutral_station
Station * neutral_station
Associated neutral station.
Definition industry.h:108
Industry::stations_near
StationList stations_near
NOSAVE: List of nearby stations.
Definition industry.h:123
Industry::IsCargoProduced
bool IsCargoProduced() const
Test if this industry produces any cargo.
Definition industry.h:229
Industry::exclusive_consumer
Owner exclusive_consumer
Which company has exclusive rights to take cargo (INVALID_OWNER = anyone)
Definition industry.h:131
Industry::counter
uint16_t counter
used for animation and/or production (if available cargo)
Definition industry.h:113
Industry::was_cargo_delivered
uint8_t was_cargo_delivered
flag that indicate this has been the closest industry chosen for cargo delivery by a station....
Definition industry.h:119
Industry::TileBelongsToIndustry
bool TileBelongsToIndustry(TileIndex tile) const
Check if a given tile belongs to this industry.
Definition industry.h:148
Industry::GetCargoAccepted
AcceptedCargoes::iterator GetCargoAccepted(CargoType cargo)
Get accepted cargo slot for a specific cargo type.
Definition industry.h:202
Industry::industries
static std::array< FlatSet< IndustryID >, NUM_INDUSTRYTYPES > industries
List of industries of each type.
Definition industry.h:276
OrthogonalTileArea
Represents the covered area of e.g.
Definition tilearea_type.h:18
PersistentStorage
Class for pooled persistent storage of data.
Definition newgrf_storage.h:199
PoolID< uint8_t, struct CompanyIDTag, 0xF, 0xFF >
Pool::PoolItem
Base class for all PoolItems.
Definition pool_type.hpp:285
Pool::PoolItem<&_industry_pool >::Get
static Titem * Get(auto index)
Returns Titem with given index.
Definition pool_type.hpp:387
Pool::PoolItem<&_industry_pool >::index
Tindex index
Index of this pool item.
Definition pool_type.hpp:286
Pool
Base class for all pools.
Definition pool_type.hpp:133
Station
Station data structure.
Definition station_base.h:500
StrongType::Typedef
Templated helper to make a type-safe 'typedef' representing a single POD value.
Definition strong_typedef_type.hpp:130
Town
Town data structure.
Definition town.h:63
subsidy_type.h
basic types related to subsidies
IsTileType
static bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
INVALID_TILE
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95
MP_INDUSTRY
@ MP_INDUSTRY
Part of an industry.
Definition tile_type.h:56
tilearea_type.h
Type for storing the 'area' of something uses on the map.
timer_game_calendar.h
Definition of the game-calendar-timer.
timer_game_economy.h
Definition of the game-economy-timer.