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/>.
8 /** @file industry.h Base of all industries. */
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 #include "timer/timer_game_calendar.h"
20 #include "timer/timer_game_economy.h"
23 typedef Pool
<Industry
, IndustryID
, 64, 64000> IndustryPool
;
24 extern IndustryPool _industry_pool
;
26 static const TimerGameEconomy::Year PROCESSING_INDUSTRY_ABANDONMENT_YEARS
= 5; ///< If a processing industry doesn't produce for this many consecutive economy years, it may close.
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.
33 enum ProductionLevels
{
34 PRODLEVEL_CLOSURE
= 0x00, ///< signal set to actually close the industry
35 PRODLEVEL_MINIMUM
= 0x04, ///< below this level, the industry is set to be closing
36 PRODLEVEL_DEFAULT
= 0x10, ///< default level set when the industry is created
37 PRODLEVEL_MAXIMUM
= 0x80, ///< the industry is running at full speed
41 * Flags to control/override the behaviour of an industry.
42 * These flags are controlled by game scripts.
44 enum IndustryControlFlags
: byte
{
45 /** No flags in effect */
47 /** When industry production change is evaluated, rolls to decrease are ignored. */
48 INDCTL_NO_PRODUCTION_DECREASE
= 1 << 0,
49 /** When industry production change is evaluated, rolls to increase are ignored. */
50 INDCTL_NO_PRODUCTION_INCREASE
= 1 << 1,
52 * Industry can not close regardless of production level or time since last delivery.
53 * This does not prevent a closure already announced. */
54 INDCTL_NO_CLOSURE
= 1 << 2,
55 /** Indicates that the production level of the industry is externally controlled. */
56 INDCTL_EXTERNAL_PROD_LEVEL
= 1 << 3,
57 /** Mask of all flags set */
58 INDCTL_MASK
= INDCTL_NO_PRODUCTION_DECREASE
| INDCTL_NO_PRODUCTION_INCREASE
| INDCTL_NO_CLOSURE
| INDCTL_EXTERNAL_PROD_LEVEL
,
60 DECLARE_ENUM_AS_BIT_SET(IndustryControlFlags
);
62 static const int THIS_MONTH
= 0;
63 static const int LAST_MONTH
= 1;
66 * Defines the internal data of a functional industry.
68 struct Industry
: IndustryPool::PoolItem
<&_industry_pool
> {
69 struct ProducedHistory
{
70 uint16_t production
; ///< Total produced
71 uint16_t transported
; ///< Total transported
73 uint8_t PctTransported() const
75 if (this->production
== 0) return 0;
76 return ClampTo
<uint8_t>(this->transported
* 256 / this->production
);
80 struct ProducedCargo
{
81 CargoID cargo
; ///< Cargo type
82 uint16_t waiting
; ///< Amount of cargo produced
83 uint8_t rate
; ///< Production rate
84 std::array
<ProducedHistory
, 2> history
; ///< History of cargo produced and transported
87 struct AcceptedCargo
{
88 CargoID cargo
; ///< Cargo type
89 uint16_t waiting
; ///< Amount of cargo waiting to processed
90 TimerGameEconomy::Date last_accepted
; ///< Last day cargo was accepted by this industry
93 using ProducedCargoArray
= std::array
<ProducedCargo
, INDUSTRY_NUM_OUTPUTS
>;
94 using AcceptedCargoArray
= std::array
<AcceptedCargo
, INDUSTRY_NUM_INPUTS
>;
96 TileArea location
; ///< Location of the industry
97 Town
*town
; ///< Nearest town
98 Station
*neutral_station
; ///< Associated neutral station
99 ProducedCargoArray produced
; ///< INDUSTRY_NUM_OUTPUTS production cargo slots
100 AcceptedCargoArray accepted
; ///< INDUSTRY_NUM_INPUTS input cargo slots
101 byte prod_level
; ///< general production level
102 uint16_t counter
; ///< used for animation and/or production (if available cargo)
104 IndustryType type
; ///< type of industry.
105 Owner owner
; ///< owner of the industry. Which SHOULD always be (imho) OWNER_NONE
106 Colours random_colour
; ///< randomized colour of the industry, for display purpose
107 TimerGameEconomy::Year last_prod_year
; ///< last economy year of production
108 byte was_cargo_delivered
; ///< flag that indicate this has been the closest industry chosen for cargo delivery by a station. see DeliverGoodsToIndustry
109 IndustryControlFlags ctlflags
; ///< flags overriding standard behaviours
111 PartOfSubsidy part_of_subsidy
; ///< NOSAVE: is this industry a source/destination of a subsidy?
112 StationList stations_near
; ///< NOSAVE: List of nearby stations.
113 mutable std::string cached_name
; ///< NOSAVE: Cache of the resolved name of the industry
115 Owner founder
; ///< Founder of the industry
116 TimerGameCalendar::Date construction_date
; ///< Date of the construction of the industry
117 uint8_t construction_type
; ///< Way the industry was constructed (@see IndustryConstructionType)
118 byte selected_layout
; ///< Which tile layout was used when creating the industry
119 Owner exclusive_supplier
; ///< Which company has exclusive rights to deliver cargo (INVALID_OWNER = anyone)
120 Owner exclusive_consumer
; ///< Which company has exclusive rights to take cargo (INVALID_OWNER = anyone)
121 std::string text
; ///< General text with additional information.
123 uint16_t random
; ///< Random value used for randomisation of all kinds of things
125 PersistentStorage
*psa
; ///< Persistent storage for NewGRF industries.
127 Industry(TileIndex tile
= INVALID_TILE
) : location(tile
, 0, 0) {}
130 void RecomputeProductionMultipliers();
133 * Check if a given tile belongs to this industry.
134 * @param tile The tile to check.
135 * @return True if the tile is part of this industry.
137 inline bool TileBelongsToIndustry(TileIndex tile
) const
139 return IsTileType(tile
, MP_INDUSTRY
) && GetIndustryIndex(tile
) == this->index
;
143 * Get produced cargo slot for a specific cargo type.
144 * @param cargo CargoID to find.
145 * @return Iterator pointing to produced cargo slot if it exists, or the end iterator.
147 inline ProducedCargoArray::iterator
GetCargoProduced(CargoID cargo
)
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
; });
154 * Get produced cargo slot for a specific cargo type (const-variant).
155 * @param cargo CargoID to find.
156 * @return Iterator pointing to produced cargo slot if it exists, or the end iterator.
158 inline ProducedCargoArray::const_iterator
GetCargoProduced(CargoID cargo
) const
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
; });
165 * Get accepted cargo slot for a specific cargo type.
166 * @param cargo CargoID to find.
167 * @return Iterator pointing to accepted cargo slot if it exists, or the end iterator.
169 inline AcceptedCargoArray::iterator
GetCargoAccepted(CargoID cargo
)
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
; });
176 * Test if this industry accepts any cargo.
177 * @return true iff the industry accepts any cargo.
179 bool IsCargoAccepted() const { return std::any_of(std::begin(this->accepted
), std::end(this->accepted
), [](const auto &a
) { return IsValidCargoID(a
.cargo
); }); }
182 * Test if this industry produces any cargo.
183 * @return true iff the industry produces any cargo.
185 bool IsCargoProduced() const { return std::any_of(std::begin(this->produced
), std::end(this->produced
), [](const auto &p
) { return IsValidCargoID(p
.cargo
); }); }
188 * Test if this industry accepts a specific cargo.
189 * @param cargo Cargo type to test.
190 * @return true iff the industry accepts the given cargo type.
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
; }); }
195 * Test if this industry produces a specific cargo.
196 * @param cargo Cargo type to test.
197 * @return true iff the industry produces the given cargo types.
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
; }); }
202 * Get the industry of the given tile
203 * @param tile the tile to get the industry from
204 * @pre IsTileType(t, MP_INDUSTRY)
205 * @return the industry
207 static inline Industry
*GetByTile(TileIndex tile
)
209 return Industry::Get(GetIndustryIndex(tile
));
212 static Industry
*GetRandom();
213 static void PostDestructor(size_t index
);
216 * Increment the count of industries for this type.
217 * @param type IndustryType to increment
218 * @pre type < NUM_INDUSTRYTYPES
220 static inline void IncIndustryTypeCount(IndustryType type
)
222 assert(type
< NUM_INDUSTRYTYPES
);
227 * Decrement the count of industries for this type.
228 * @param type IndustryType to decrement
229 * @pre type < NUM_INDUSTRYTYPES
231 static inline void DecIndustryTypeCount(IndustryType type
)
233 assert(type
< NUM_INDUSTRYTYPES
);
238 * Get the count of industries for this type.
239 * @param type IndustryType to query
240 * @pre type < NUM_INDUSTRYTYPES
242 static inline uint16_t GetIndustryTypeCount(IndustryType type
)
244 assert(type
< NUM_INDUSTRYTYPES
);
248 /** Resets industry counts. */
249 static inline void ResetIndustryCounts()
251 memset(&counts
, 0, sizeof(counts
));
254 inline const std::string
&GetCachedName() const
256 if (this->cached_name
.empty()) this->FillCachedName();
257 return this->cached_name
;
261 void FillCachedName() const;
264 static uint16_t counts
[NUM_INDUSTRYTYPES
]; ///< Number of industries per type ingame
267 void ClearAllIndustryCachedNames();
269 void PlantRandomFarmField(const Industry
*i
);
271 void ReleaseDisastersTargetingIndustry(IndustryID
);
273 bool IsTileForestIndustry(TileIndex tile
);
275 /** Data for managing the number of industries of a single industry type. */
276 struct IndustryTypeBuildData
{
277 uint32_t probability
; ///< Relative probability of building this industry.
278 byte min_number
; ///< Smallest number of industries that should exist (either \c 0 or \c 1).
279 uint16_t target_count
; ///< Desired number of industries of this type.
280 uint16_t max_wait
; ///< Starting number of turns to wait (copied to #wait_count).
281 uint16_t wait_count
; ///< Number of turns to wait before trying to build again.
285 bool GetIndustryTypeData(IndustryType it
);
289 * Data for managing the number and type of industries in the game.
291 struct IndustryBuildData
{
292 IndustryTypeBuildData builddata
[NUM_INDUSTRYTYPES
]; ///< Industry build data for every industry type.
293 uint32_t wanted_inds
; ///< Number of wanted industries (bits 31-16), and a fraction (bits 15-0).
297 void SetupTargetCount();
298 void TryBuildNewIndustry();
300 void EconomyMonthlyLoop();
303 extern IndustryBuildData _industry_builder
;
306 /** Special values for the industry list window for the data parameter of #InvalidateWindowData. */
307 enum IndustryDirectoryInvalidateWindowData
{
309 IDIWD_PRODUCTION_CHANGE
,
313 #endif /* INDUSTRY_H */