Update: Translations from eints
[openttd-github.git] / src / industry.h
blob6c23927104b235e69b007d28bd28cae34b58669c
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 */
8 /** @file industry.h Base of all industries. */
10 #ifndef INDUSTRY_H
11 #define INDUSTRY_H
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.
28 /**
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
40 /**
41 * Flags to control/override the behaviour of an industry.
42 * These flags are controlled by game scripts.
44 enum IndustryControlFlags : uint8_t {
45 /** No flags in effect */
46 INDCTL_NONE = 0,
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,
51 /**
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;
65 /**
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 ProducedCargoes = std::vector<ProducedCargo>;
94 using AcceptedCargoes = std::vector<AcceptedCargo>;
96 TileArea location; ///< Location of the industry
97 Town *town; ///< Nearest town
98 Station *neutral_station; ///< Associated neutral station
99 ProducedCargoes produced; ///< produced cargo slots
100 AcceptedCargoes accepted; ///< accepted cargo slots
101 uint8_t 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 uint8_t 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 uint8_t 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) {}
128 ~Industry();
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 * Safely get a produced cargo slot, or an empty data if the slot does not exist.
144 * @param slot produced cargo slot to retrieve.
145 * @return the real slot, or an empty slot.
147 inline const ProducedCargo &GetProduced(size_t slot) const
149 static const ProducedCargo empty{INVALID_CARGO, 0, 0, {}};
150 return slot < this->produced.size() ? this->produced[slot] : empty;
154 * Safely get an accepted cargo slot, or an empty data if the slot does not exist.
155 * @param slot accepted cargo slot to retrieve.
156 * @return the real slot, or an empty slot.
158 inline const AcceptedCargo &GetAccepted(size_t slot) const
160 static const AcceptedCargo empty{INVALID_CARGO, 0, {}};
161 return slot < this->accepted.size() ? this->accepted[slot] : empty;
165 * Get produced cargo slot for a specific cargo type.
166 * @param cargo CargoID to find.
167 * @return Iterator pointing to produced cargo slot if it exists, or the end iterator.
169 inline ProducedCargoes::iterator GetCargoProduced(CargoID cargo)
171 if (!IsValidCargoID(cargo)) return std::end(this->produced);
172 return std::find_if(std::begin(this->produced), std::end(this->produced), [&cargo](const auto &p) { return p.cargo == cargo; });
176 * Get produced cargo slot for a specific cargo type (const-variant).
177 * @param cargo CargoID to find.
178 * @return Iterator pointing to produced cargo slot if it exists, or the end iterator.
180 inline ProducedCargoes::const_iterator GetCargoProduced(CargoID cargo) const
182 if (!IsValidCargoID(cargo)) return std::end(this->produced);
183 return std::find_if(std::begin(this->produced), std::end(this->produced), [&cargo](const auto &p) { return p.cargo == cargo; });
187 * Get accepted cargo slot for a specific cargo type.
188 * @param cargo CargoID to find.
189 * @return Iterator pointing to accepted cargo slot if it exists, or the end iterator.
191 inline AcceptedCargoes::iterator GetCargoAccepted(CargoID cargo)
193 if (!IsValidCargoID(cargo)) return std::end(this->accepted);
194 return std::find_if(std::begin(this->accepted), std::end(this->accepted), [&cargo](const auto &a) { return a.cargo == cargo; });
198 * Get accepted cargo slot for a specific cargo type (const-variant).
199 * @param cargo CargoID to find.
200 * @return Iterator pointing to accepted cargo slot if it exists, or the end iterator.
202 inline AcceptedCargoes::const_iterator GetCargoAccepted(CargoID cargo) const
204 if (!IsValidCargoID(cargo)) return std::end(this->accepted);
205 return std::find_if(std::begin(this->accepted), std::end(this->accepted), [&cargo](const auto &a) { return a.cargo == cargo; });
209 * Test if this industry accepts any cargo.
210 * @return true iff the industry accepts any cargo.
212 bool IsCargoAccepted() const { return std::any_of(std::begin(this->accepted), std::end(this->accepted), [](const auto &a) { return IsValidCargoID(a.cargo); }); }
215 * Test if this industry produces any cargo.
216 * @return true iff the industry produces any cargo.
218 bool IsCargoProduced() const { return std::any_of(std::begin(this->produced), std::end(this->produced), [](const auto &p) { return IsValidCargoID(p.cargo); }); }
221 * Test if this industry accepts a specific cargo.
222 * @param cargo Cargo type to test.
223 * @return true iff the industry accepts the given cargo type.
225 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; }); }
228 * Test if this industry produces a specific cargo.
229 * @param cargo Cargo type to test.
230 * @return true iff the industry produces the given cargo types.
232 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; }); }
235 * Get the industry of the given tile
236 * @param tile the tile to get the industry from
237 * @pre IsTileType(t, MP_INDUSTRY)
238 * @return the industry
240 static inline Industry *GetByTile(TileIndex tile)
242 return Industry::Get(GetIndustryIndex(tile));
245 static Industry *GetRandom();
246 static void PostDestructor(size_t index);
249 * Increment the count of industries for this type.
250 * @param type IndustryType to increment
251 * @pre type < NUM_INDUSTRYTYPES
253 static inline void IncIndustryTypeCount(IndustryType type)
255 assert(type < NUM_INDUSTRYTYPES);
256 counts[type]++;
260 * Decrement the count of industries for this type.
261 * @param type IndustryType to decrement
262 * @pre type < NUM_INDUSTRYTYPES
264 static inline void DecIndustryTypeCount(IndustryType type)
266 assert(type < NUM_INDUSTRYTYPES);
267 counts[type]--;
271 * Get the count of industries for this type.
272 * @param type IndustryType to query
273 * @pre type < NUM_INDUSTRYTYPES
275 static inline uint16_t GetIndustryTypeCount(IndustryType type)
277 assert(type < NUM_INDUSTRYTYPES);
278 return counts[type];
281 /** Resets industry counts. */
282 static inline void ResetIndustryCounts()
284 memset(&counts, 0, sizeof(counts));
287 inline const std::string &GetCachedName() const
289 if (this->cached_name.empty()) this->FillCachedName();
290 return this->cached_name;
293 private:
294 void FillCachedName() const;
296 protected:
297 static uint16_t counts[NUM_INDUSTRYTYPES]; ///< Number of industries per type ingame
300 void ClearAllIndustryCachedNames();
302 void PlantRandomFarmField(const Industry *i);
304 void ReleaseDisastersTargetingIndustry(IndustryID);
306 bool IsTileForestIndustry(TileIndex tile);
308 /** Data for managing the number of industries of a single industry type. */
309 struct IndustryTypeBuildData {
310 uint32_t probability; ///< Relative probability of building this industry.
311 uint8_t min_number; ///< Smallest number of industries that should exist (either \c 0 or \c 1).
312 uint16_t target_count; ///< Desired number of industries of this type.
313 uint16_t max_wait; ///< Starting number of turns to wait (copied to #wait_count).
314 uint16_t wait_count; ///< Number of turns to wait before trying to build again.
316 void Reset();
318 bool GetIndustryTypeData(IndustryType it);
322 * Data for managing the number and type of industries in the game.
324 struct IndustryBuildData {
325 IndustryTypeBuildData builddata[NUM_INDUSTRYTYPES]; ///< Industry build data for every industry type.
326 uint32_t wanted_inds; ///< Number of wanted industries (bits 31-16), and a fraction (bits 15-0).
328 void Reset();
330 void SetupTargetCount();
331 void TryBuildNewIndustry();
333 void EconomyMonthlyLoop();
336 extern IndustryBuildData _industry_builder;
339 /** Special values for the industry list window for the data parameter of #InvalidateWindowData. */
340 enum IndustryDirectoryInvalidateWindowData {
341 IDIWD_FORCE_REBUILD,
342 IDIWD_PRODUCTION_CHANGE,
343 IDIWD_FORCE_RESORT,
346 void TrimIndustryAcceptedProduced(Industry *ind);
348 #endif /* INDUSTRY_H */