OpenTTD Source: src/industry.h Source File

OpenTTD Source 12.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"
19
20
21 typedef Pool<Industry, IndustryID, 64, 64000> IndustryPool;
22 extern IndustryPool _industry_pool;
23
29 enum ProductionLevels {
30 PRODLEVEL_CLOSURE = 0x00,
31 PRODLEVEL_MINIMUM = 0x04,
32 PRODLEVEL_DEFAULT = 0x10,
33 PRODLEVEL_MAXIMUM = 0x80,
34 };
35
36 enum class IndustryAction : byte {
37 SetControlFlags = 0,
38 SetExclusiveSupplier = 1,
39 SetExclusiveConsumer = 2,
40 SetText = 3,
41 };
42
47 enum IndustryControlFlags : byte {
49 INDCTL_NONE = 0,
51 INDCTL_NO_PRODUCTION_DECREASE = 1 << 0,
53 INDCTL_NO_PRODUCTION_INCREASE = 1 << 1,
57 INDCTL_NO_CLOSURE = 1 << 2,
59 INDCTL_MASK = INDCTL_NO_PRODUCTION_DECREASE | INDCTL_NO_PRODUCTION_INCREASE | INDCTL_NO_CLOSURE,
60 };
61 DECLARE_ENUM_AS_BIT_SET(IndustryControlFlags);
62
66 struct Industry : IndustryPool::PoolItem<&_industry_pool> {
67 TileArea location;
68 Town *town;
69 Station *neutral_station;
70 CargoID produced_cargo[INDUSTRY_NUM_OUTPUTS];
71 uint16 produced_cargo_waiting[INDUSTRY_NUM_OUTPUTS];
72 uint16 incoming_cargo_waiting[INDUSTRY_NUM_INPUTS];
73 byte production_rate[INDUSTRY_NUM_OUTPUTS];
74 byte prod_level;
75 CargoID accepts_cargo[INDUSTRY_NUM_INPUTS];
76 uint16 this_month_production[INDUSTRY_NUM_OUTPUTS];
77 uint16 this_month_transported[INDUSTRY_NUM_OUTPUTS];
78 byte last_month_pct_transported[INDUSTRY_NUM_OUTPUTS];
79 uint16 last_month_production[INDUSTRY_NUM_OUTPUTS];
80 uint16 last_month_transported[INDUSTRY_NUM_OUTPUTS];
81 uint16 counter;
82
83 IndustryType type;
84 Owner owner;
85 byte random_colour;
86 Year last_prod_year;
87 byte was_cargo_delivered;
88 IndustryControlFlags ctlflags;
89
90 PartOfSubsidy part_of_subsidy;
91 StationList stations_near;
92 mutable std::string cached_name;
93
94 Owner founder;
95 Date construction_date;
96 uint8 construction_type;
97 Date last_cargo_accepted_at[INDUSTRY_NUM_INPUTS];
98 byte selected_layout;
99 Owner exclusive_supplier;
100 Owner exclusive_consumer;
101 std::string text;
102
103 uint16 random;
104
105 PersistentStorage *psa;
106
107 Industry(TileIndex tile = INVALID_TILE) : location(tile, 0, 0) {}
108 ~Industry();
109
110 void RecomputeProductionMultipliers();
111
117 inline bool TileBelongsToIndustry(TileIndex tile) const
118 {
119 return IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == this->index;
120 }
121
122 inline int GetCargoProducedIndex(CargoID cargo) const
123 {
124 if (cargo == CT_INVALID) return -1;
125 const CargoID *pos = std::find(this->produced_cargo, endof(this->produced_cargo), cargo);
126 if (pos == endof(this->produced_cargo)) return -1;
127 return pos - this->produced_cargo;
128 }
129
130 inline int GetCargoAcceptedIndex(CargoID cargo) const
131 {
132 if (cargo == CT_INVALID) return -1;
133 const CargoID *pos = std::find(this->accepts_cargo, endof(this->accepts_cargo), cargo);
134 if (pos == endof(this->accepts_cargo)) return -1;
135 return pos - this->accepts_cargo;
136 }
137
144 static inline Industry *GetByTile(TileIndex tile)
145 {
146 return Industry::Get(GetIndustryIndex(tile));
147 }
148
149 static Industry *GetRandom();
150 static void PostDestructor(size_t index);
151
157 static inline void IncIndustryTypeCount(IndustryType type)
158 {
159 assert(type < NUM_INDUSTRYTYPES);
160 counts[type]++;
161 }
162
168 static inline void DecIndustryTypeCount(IndustryType type)
169 {
170 assert(type < NUM_INDUSTRYTYPES);
171 counts[type]--;
172 }
173
179 static inline uint16 GetIndustryTypeCount(IndustryType type)
180 {
181 assert(type < NUM_INDUSTRYTYPES);
182 return counts[type];
183 }
184
186 static inline void ResetIndustryCounts()
187 {
188 memset(&counts, 0, sizeof(counts));
189 }
190
191 inline const char *GetCachedName() const
192 {
193 if (this->cached_name.empty()) this->FillCachedName();
194 return this->cached_name.c_str();
195 }
196
197 private:
198 void FillCachedName() const;
199
200 protected:
201 static uint16 counts[NUM_INDUSTRYTYPES];
202 };
203
204 void ClearAllIndustryCachedNames();
205
206 void PlantRandomFarmField(const Industry *i);
207
208 void ReleaseDisastersTargetingIndustry(IndustryID);
209
210 bool IsTileForestIndustry(TileIndex tile);
211
213 struct IndustryTypeBuildData {
214 uint32 probability;
215 byte min_number;
216 uint16 target_count;
217 uint16 max_wait;
218 uint16 wait_count;
219
220 void Reset();
221
222 bool GetIndustryTypeData(IndustryType it);
223 };
224
228 struct IndustryBuildData {
229 IndustryTypeBuildData builddata[NUM_INDUSTRYTYPES];
230 uint32 wanted_inds;
231
232 void Reset();
233
234 void SetupTargetCount();
235 void TryBuildNewIndustry();
236
237 void MonthlyLoop();
238 };
239
240 extern IndustryBuildData _industry_builder;
241
242
244 enum IndustryDirectoryInvalidateWindowData {
245 IDIWD_FORCE_REBUILD,
246 IDIWD_PRODUCTION_CHANGE,
247 IDIWD_FORCE_RESORT,
248 };
249
250 #endif /* INDUSTRY_H */
PRODLEVEL_MINIMUM
@ PRODLEVEL_MINIMUM
below this level, the industry is set to be closing
Definition: industry.h:31
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:83
Industry::owner
Owner owner
owner of the industry. Which SHOULD always be (imho) OWNER_NONE
Definition: industry.h:84
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
Industry::this_month_production
uint16 this_month_production[INDUSTRY_NUM_OUTPUTS]
stats of this month's production per cargo
Definition: industry.h:76
INDUSTRY_NUM_OUTPUTS
static const int INDUSTRY_NUM_OUTPUTS
Number of cargo types an industry can produce.
Definition: industry_type.h:39
Pool::PoolItem<&_industry_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:337
Industry::part_of_subsidy
PartOfSubsidy part_of_subsidy
NOSAVE: is this industry a source/destination of a subsidy?
Definition: industry.h:90
INDCTL_NONE
@ INDCTL_NONE
No flags in effect.
Definition: industry.h:49
INDCTL_NO_CLOSURE
@ INDCTL_NO_CLOSURE
Industry can not close regardless of production level or time since last delivery.
Definition: industry.h:57
IndustryBuildData::builddata
IndustryTypeBuildData builddata[NUM_INDUSTRYTYPES]
Industry build data for every industry type.
Definition: industry.h:229
Industry::this_month_transported
uint16 this_month_transported[INDUSTRY_NUM_OUTPUTS]
stats of this month's transport per cargo
Definition: industry.h:77
Station
Station data structure.
Definition: station_base.h:447
INDUSTRY_NUM_INPUTS
static const int INDUSTRY_NUM_INPUTS
Number of cargo types an industry can accept.
Definition: industry_type.h:38
PRODLEVEL_CLOSURE
@ PRODLEVEL_CLOSURE
signal set to actually close the industry
Definition: industry.h:30
Industry::last_month_transported
uint16 last_month_transported[INDUSTRY_NUM_OUTPUTS]
total units transported per cargo in the last full month
Definition: industry.h:80
IndustryAction::SetText
@ SetText
Set additional text.
Pool::PoolItem<&_industry_pool >::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:235
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:87
Industry::produced_cargo_waiting
uint16 produced_cargo_waiting[INDUSTRY_NUM_OUTPUTS]
amount of cargo produced per cargo
Definition: industry.h:71
Industry::last_month_production
uint16 last_month_production[INDUSTRY_NUM_OUTPUTS]
total units produced per cargo in the last full month
Definition: industry.h:79
subsidy_type.h
Year
int32 Year
Type for the year, note: 0 based, i.e. starts at the year 0.
Definition: date_type.h:18
IndustryAction::SetExclusiveConsumer
@ SetExclusiveConsumer
Set exclusive consumer.
Industry::RecomputeProductionMultipliers
void RecomputeProductionMultipliers()
Recompute production_rate for current prod_level.
Definition: industry_cmd.cpp:2378
Industry::construction_type
uint8 construction_type
Way the industry was constructed (.
Definition: industry.h:96
MP_INDUSTRY
@ MP_INDUSTRY
Part of an industry.
Definition: tile_type.h:54
Industry::construction_date
Date construction_date
Date of the construction of the industry.
Definition: industry.h:95
Industry::exclusive_consumer
Owner exclusive_consumer
Which company has exclusive rights to take cargo (INVALID_OWNER = anyone)
Definition: industry.h:100
PRODLEVEL_DEFAULT
@ PRODLEVEL_DEFAULT
default level set when the industry is created
Definition: industry.h:32
Industry
Defines the internal data of a functional industry.
Definition: industry.h:66
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
Industry::ctlflags
IndustryControlFlags ctlflags
flags overriding standard behaviours
Definition: industry.h:88
Industry::neutral_station
Station * neutral_station
Associated neutral station.
Definition: industry.h:69
industry_map.h
IndustryAction
IndustryAction
Definition: industry.h:36
Industry::GetRandom
static Industry * GetRandom()
Return a random valid industry.
Definition: industry_cmd.cpp:218
IndustryAction::SetExclusiveSupplier
@ SetExclusiveSupplier
Set exclusive supplier.
Industry::PostDestructor
static void PostDestructor(size_t index)
Invalidating some stuff after removing item from the pool.
Definition: industry_cmd.cpp:208
IndustryTypeBuildData::wait_count
uint16 wait_count
Number of turns to wait before trying to build again.
Definition: industry.h:218
Industry::stations_near
StationList stations_near
NOSAVE: List of nearby stations.
Definition: industry.h:91
Industry::random
uint16 random
Random value used for randomisation of all kinds of things.
Definition: industry.h:103
Industry::location
TileArea location
Location of the industry.
Definition: industry.h:67
IndustryAction::SetControlFlags
@ SetControlFlags
Set IndustryControlFlags.
IndustryTypeBuildData::Reset
void Reset()
Reset the entry.
Definition: industry_cmd.cpp:2266
Industry::cached_name
std::string cached_name
NOSAVE: Cache of the resolved name of the industry.
Definition: industry.h:92
Industry::GetByTile
static Industry * GetByTile(TileIndex tile)
Get the industry of the given tile.
Definition: industry.h:144
Industry::exclusive_supplier
Owner exclusive_supplier
Which company has exclusive rights to deliver cargo (INVALID_OWNER = anyone)
Definition: industry.h:99
Industry::type
IndustryType type
type of industry.
Definition: industry.h:83
IndustryTypeBuildData::max_wait
uint16 max_wait
Starting number of turns to wait (copied to wait_count).
Definition: industry.h:217
IndustryBuildData
Data for managing the number and type of industries in the game.
Definition: industry.h:228
PersistentStorage
Class for pooled persistent storage of data.
Definition: newgrf_storage.h:221
Industry::produced_cargo
CargoID produced_cargo[INDUSTRY_NUM_OUTPUTS]
16 production cargo slots
Definition: industry.h:70
Industry::incoming_cargo_waiting
uint16 incoming_cargo_waiting[INDUSTRY_NUM_INPUTS]
incoming cargo waiting to be processed
Definition: industry.h:72
Date
int32 Date
The type to store our dates in.
Definition: date_type.h:14
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:168
IndustryBuildData::MonthlyLoop
void MonthlyLoop()
Monthly update of industry build data.
Definition: industry_cmd.cpp:2286
PRODLEVEL_MAXIMUM
@ PRODLEVEL_MAXIMUM
the industry is running at full speed
Definition: industry.h:33
Industry::ResetIndustryCounts
static void ResetIndustryCounts()
Resets industry counts.
Definition: industry.h:186
IndustryBuildData::SetupTargetCount
void SetupTargetCount()
Decide how many industries of each type are needed.
Definition: industry_cmd.cpp:2421
IndustryDirectoryInvalidateWindowData
IndustryDirectoryInvalidateWindowData
Special values for the industry list window for the data parameter of InvalidateWindowData.
Definition: industry.h:244
IndustryBuildData::TryBuildNewIndustry
void TryBuildNewIndustry()
Try to create a random industry, during gameplay.
Definition: industry_cmd.cpp:2466
Industry::text
std::string text
General text with additional information.
Definition: industry.h:101
StationList
std::set< Station *, StationCompare > StationList
List of stations.
Definition: station_type.h:94
IsTileType
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
IsTileForestIndustry
bool IsTileForestIndustry(TileIndex tile)
Check whether the tile is a forest.
Definition: industry_cmd.cpp:962
Industry::last_month_pct_transported
byte last_month_pct_transported[INDUSTRY_NUM_OUTPUTS]
percentage transported per cargo in the last full month
Definition: industry.h:78
Industry::town
Town * town
Nearest town.
Definition: industry.h:68
IndustryBuildData::Reset
void Reset()
Completely reset the industry build data.
Definition: industry_cmd.cpp:2276
tilearea_type.h
IndustryBuildData::wanted_inds
uint32 wanted_inds
Number of wanted industries (bits 31-16), and a fraction (bits 15-0).
Definition: industry.h:230
station_base.h
Industry::selected_layout
byte selected_layout
Which tile layout was used when creating the industry.
Definition: industry.h:98
Pool
Base class for all pools.
Definition: pool_type.hpp:81
Industry::GetIndustryTypeCount
static uint16 GetIndustryTypeCount(IndustryType type)
Get the count of industries for this type.
Definition: industry.h:179
Industry::founder
Owner founder
Founder of the industry.
Definition: industry.h:94
Industry::IncIndustryTypeCount
static void IncIndustryTypeCount(IndustryType type)
Increment the count of industries for this type.
Definition: industry.h:157
endof
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:386
GetIndustryIndex
static IndustryID GetIndustryIndex(TileIndex t)
Get the industry ID of the given tile.
Definition: industry_map.h:63
industrytype.h
Industry::last_cargo_accepted_at
Date last_cargo_accepted_at[INDUSTRY_NUM_INPUTS]
Last day each cargo type was accepted by this industry.
Definition: industry.h:97
Industry::last_prod_year
Year last_prod_year
last year of production
Definition: industry.h:86
IndustryTypeBuildData::min_number
byte min_number
Smallest number of industries that should exist (either 0 or 1).
Definition: industry.h:215
IndustryTypeBuildData
Data for managing the number of industries of a single industry type.
Definition: industry.h:213
ProductionLevels
ProductionLevels
Production level maximum, minimum and default values.
Definition: industry.h:29
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::random_colour
byte random_colour
randomized colour of the industry, for display purpose
Definition: industry.h:85
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
INVALID_TILE
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:88
INDCTL_NO_PRODUCTION_DECREASE
@ INDCTL_NO_PRODUCTION_DECREASE
When industry production change is evaluated, rolls to decrease are ignored.
Definition: industry.h:51
IndustryTypeBuildData::probability
uint32 probability
Relative probability of building this industry.
Definition: industry.h:214
_industry_builder
IndustryBuildData _industry_builder
In-game manager of industries.
Definition: industry_cmd.cpp:65
Industry::accepts_cargo
CargoID accepts_cargo[INDUSTRY_NUM_INPUTS]
16 input cargo slots
Definition: industry.h:75
INDCTL_MASK
@ INDCTL_MASK
Mask of all flags set.
Definition: industry.h:59
Industry::prod_level
byte prod_level
general production level
Definition: industry.h:74
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:938
Industry::counts
static uint16 counts[NUM_INDUSTRYTYPES]
Number of industries per type ingame.
Definition: industry.h:201
Industry::psa
PersistentStorage * psa
Persistent storage for NewGRF industries.
Definition: industry.h:105
INDCTL_NO_PRODUCTION_INCREASE
@ INDCTL_NO_PRODUCTION_INCREASE
When industry production change is evaluated, rolls to increase are ignored.
Definition: industry.h:53
IndustryControlFlags
IndustryControlFlags
Flags to control/override the behaviour of an industry.
Definition: industry.h:47
CT_INVALID
@ CT_INVALID
Invalid cargo type.
Definition: cargo_type.h:69
newgrf_storage.h
Pool::PoolItem
Base class for all PoolItems.
Definition: pool_type.hpp:234
Industry::production_rate
byte production_rate[INDUSTRY_NUM_OUTPUTS]
production rate for each cargo
Definition: industry.h:73
IndustryTypeBuildData::target_count
uint16 target_count
Desired number of industries of this type.
Definition: industry.h:216
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:2410
Industry::counter
uint16 counter
used for animation and/or production (if available cargo)
Definition: industry.h:81
Industry::TileBelongsToIndustry
bool TileBelongsToIndustry(TileIndex tile) const
Check if a given tile belongs to this industry.
Definition: industry.h:117