Update: Translations from eints
[openttd-github.git] / src / industry.h
blobcd29dcff7fc5605ad138020beac1f4d86a190926
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.
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 static constexpr uint8_t PRODLEVEL_CLOSURE = 0x00; ///< signal set to actually close the industry
34 static constexpr uint8_t PRODLEVEL_MINIMUM = 0x04; ///< below this level, the industry is set to be closing
35 static constexpr uint8_t PRODLEVEL_DEFAULT = 0x10; ///< default level set when the industry is created
36 static constexpr uint8_t PRODLEVEL_MAXIMUM = 0x80; ///< the industry is running at full speed
38 /**
39 * Flags to control/override the behaviour of an industry.
40 * These flags are controlled by game scripts.
42 enum IndustryControlFlags : uint8_t {
43 /** No flags in effect */
44 INDCTL_NONE = 0,
45 /** When industry production change is evaluated, rolls to decrease are ignored. */
46 INDCTL_NO_PRODUCTION_DECREASE = 1 << 0,
47 /** When industry production change is evaluated, rolls to increase are ignored. */
48 INDCTL_NO_PRODUCTION_INCREASE = 1 << 1,
49 /**
50 * Industry can not close regardless of production level or time since last delivery.
51 * This does not prevent a closure already announced. */
52 INDCTL_NO_CLOSURE = 1 << 2,
53 /** Indicates that the production level of the industry is externally controlled. */
54 INDCTL_EXTERNAL_PROD_LEVEL = 1 << 3,
55 /** Mask of all flags set */
56 INDCTL_MASK = INDCTL_NO_PRODUCTION_DECREASE | INDCTL_NO_PRODUCTION_INCREASE | INDCTL_NO_CLOSURE | INDCTL_EXTERNAL_PROD_LEVEL,
58 DECLARE_ENUM_AS_BIT_SET(IndustryControlFlags);
60 static const int THIS_MONTH = 0;
61 static const int LAST_MONTH = 1;
63 /**
64 * Defines the internal data of a functional industry.
66 struct Industry : IndustryPool::PoolItem<&_industry_pool> {
67 struct ProducedHistory {
68 uint16_t production; ///< Total produced
69 uint16_t transported; ///< Total transported
71 uint8_t PctTransported() const
73 if (this->production == 0) return 0;
74 return ClampTo<uint8_t>(this->transported * 256 / this->production);
78 struct ProducedCargo {
79 CargoID cargo; ///< Cargo type
80 uint16_t waiting; ///< Amount of cargo produced
81 uint8_t rate; ///< Production rate
82 std::array<ProducedHistory, 25> history; ///< History of cargo produced and transported for this month and 24 previous months
85 struct AcceptedCargo {
86 CargoID cargo; ///< Cargo type
87 uint16_t waiting; ///< Amount of cargo waiting to processed
88 TimerGameEconomy::Date last_accepted; ///< Last day cargo was accepted by this industry
91 using ProducedCargoes = std::vector<ProducedCargo>;
92 using AcceptedCargoes = std::vector<AcceptedCargo>;
94 TileArea location; ///< Location of the industry
95 Town *town; ///< Nearest town
96 Station *neutral_station; ///< Associated neutral station
97 ProducedCargoes produced; ///< produced cargo slots
98 AcceptedCargoes accepted; ///< accepted cargo slots
99 uint8_t prod_level; ///< general production level
100 uint16_t counter; ///< used for animation and/or production (if available cargo)
102 IndustryType type; ///< type of industry.
103 Owner owner; ///< owner of the industry. Which SHOULD always be (imho) OWNER_NONE
104 Colours random_colour; ///< randomized colour of the industry, for display purpose
105 TimerGameEconomy::Year last_prod_year; ///< last economy year of production
106 uint8_t was_cargo_delivered; ///< flag that indicate this has been the closest industry chosen for cargo delivery by a station. see DeliverGoodsToIndustry
107 IndustryControlFlags ctlflags; ///< flags overriding standard behaviours
109 PartOfSubsidy part_of_subsidy; ///< NOSAVE: is this industry a source/destination of a subsidy?
110 StationList stations_near; ///< NOSAVE: List of nearby stations.
111 mutable std::string cached_name; ///< NOSAVE: Cache of the resolved name of the industry
113 Owner founder; ///< Founder of the industry
114 TimerGameCalendar::Date construction_date; ///< Date of the construction of the industry
115 uint8_t construction_type; ///< Way the industry was constructed (@see IndustryConstructionType)
116 uint8_t selected_layout; ///< Which tile layout was used when creating the industry
117 Owner exclusive_supplier; ///< Which company has exclusive rights to deliver cargo (INVALID_OWNER = anyone)
118 Owner exclusive_consumer; ///< Which company has exclusive rights to take cargo (INVALID_OWNER = anyone)
119 std::string text; ///< General text with additional information.
121 uint16_t random; ///< Random value used for randomisation of all kinds of things
123 PersistentStorage *psa; ///< Persistent storage for NewGRF industries.
125 Industry(TileIndex tile = INVALID_TILE) : location(tile, 0, 0) {}
126 ~Industry();
128 void RecomputeProductionMultipliers();
131 * Check if a given tile belongs to this industry.
132 * @param tile The tile to check.
133 * @return True if the tile is part of this industry.
135 inline bool TileBelongsToIndustry(TileIndex tile) const
137 return IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == this->index;
141 * Safely get a produced cargo slot, or an empty data if the slot does not exist.
142 * @param slot produced cargo slot to retrieve.
143 * @return the real slot, or an empty slot.
145 inline const ProducedCargo &GetProduced(size_t slot) const
147 static const ProducedCargo empty{INVALID_CARGO, 0, 0, {}};
148 return slot < this->produced.size() ? this->produced[slot] : empty;
152 * Safely get an accepted cargo slot, or an empty data if the slot does not exist.
153 * @param slot accepted cargo slot to retrieve.
154 * @return the real slot, or an empty slot.
156 inline const AcceptedCargo &GetAccepted(size_t slot) const
158 static const AcceptedCargo empty{INVALID_CARGO, 0, {}};
159 return slot < this->accepted.size() ? this->accepted[slot] : empty;
163 * Get produced cargo slot for a specific cargo type.
164 * @param cargo CargoID to find.
165 * @return Iterator pointing to produced cargo slot if it exists, or the end iterator.
167 inline ProducedCargoes::iterator GetCargoProduced(CargoID cargo)
169 if (!IsValidCargoID(cargo)) return std::end(this->produced);
170 return std::find_if(std::begin(this->produced), std::end(this->produced), [&cargo](const auto &p) { return p.cargo == cargo; });
174 * Get produced cargo slot for a specific cargo type (const-variant).
175 * @param cargo CargoID to find.
176 * @return Iterator pointing to produced cargo slot if it exists, or the end iterator.
178 inline ProducedCargoes::const_iterator GetCargoProduced(CargoID cargo) const
180 if (!IsValidCargoID(cargo)) return std::end(this->produced);
181 return std::find_if(std::begin(this->produced), std::end(this->produced), [&cargo](const auto &p) { return p.cargo == cargo; });
185 * Get accepted cargo slot for a specific cargo type.
186 * @param cargo CargoID to find.
187 * @return Iterator pointing to accepted cargo slot if it exists, or the end iterator.
189 inline AcceptedCargoes::iterator GetCargoAccepted(CargoID cargo)
191 if (!IsValidCargoID(cargo)) return std::end(this->accepted);
192 return std::find_if(std::begin(this->accepted), std::end(this->accepted), [&cargo](const auto &a) { return a.cargo == cargo; });
196 * Get accepted cargo slot for a specific cargo type (const-variant).
197 * @param cargo CargoID to find.
198 * @return Iterator pointing to accepted cargo slot if it exists, or the end iterator.
200 inline AcceptedCargoes::const_iterator GetCargoAccepted(CargoID cargo) const
202 if (!IsValidCargoID(cargo)) return std::end(this->accepted);
203 return std::find_if(std::begin(this->accepted), std::end(this->accepted), [&cargo](const auto &a) { return a.cargo == cargo; });
207 * Test if this industry accepts any cargo.
208 * @return true iff the industry accepts any cargo.
210 bool IsCargoAccepted() const { return std::any_of(std::begin(this->accepted), std::end(this->accepted), [](const auto &a) { return IsValidCargoID(a.cargo); }); }
213 * Test if this industry produces any cargo.
214 * @return true iff the industry produces any cargo.
216 bool IsCargoProduced() const { return std::any_of(std::begin(this->produced), std::end(this->produced), [](const auto &p) { return IsValidCargoID(p.cargo); }); }
219 * Test if this industry accepts a specific cargo.
220 * @param cargo Cargo type to test.
221 * @return true iff the industry accepts the given cargo type.
223 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; }); }
226 * Test if this industry produces a specific cargo.
227 * @param cargo Cargo type to test.
228 * @return true iff the industry produces the given cargo types.
230 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; }); }
233 * Get the industry of the given tile
234 * @param tile the tile to get the industry from
235 * @pre IsTileType(t, MP_INDUSTRY)
236 * @return the industry
238 static inline Industry *GetByTile(TileIndex tile)
240 return Industry::Get(GetIndustryIndex(tile));
243 static Industry *GetRandom();
244 static void PostDestructor(size_t index);
247 * Increment the count of industries for this type.
248 * @param type IndustryType to increment
249 * @pre type < NUM_INDUSTRYTYPES
251 static inline void IncIndustryTypeCount(IndustryType type)
253 assert(type < NUM_INDUSTRYTYPES);
254 counts[type]++;
258 * Decrement the count of industries for this type.
259 * @param type IndustryType to decrement
260 * @pre type < NUM_INDUSTRYTYPES
262 static inline void DecIndustryTypeCount(IndustryType type)
264 assert(type < NUM_INDUSTRYTYPES);
265 counts[type]--;
269 * Get the count of industries for this type.
270 * @param type IndustryType to query
271 * @pre type < NUM_INDUSTRYTYPES
273 static inline uint16_t GetIndustryTypeCount(IndustryType type)
275 assert(type < NUM_INDUSTRYTYPES);
276 return counts[type];
279 /** Resets industry counts. */
280 static inline void ResetIndustryCounts()
282 memset(&counts, 0, sizeof(counts));
285 inline const std::string &GetCachedName() const
287 if (this->cached_name.empty()) this->FillCachedName();
288 return this->cached_name;
291 private:
292 void FillCachedName() const;
294 protected:
295 static uint16_t counts[NUM_INDUSTRYTYPES]; ///< Number of industries per type ingame
298 void ClearAllIndustryCachedNames();
300 void PlantRandomFarmField(const Industry *i);
302 void ReleaseDisastersTargetingIndustry(IndustryID);
304 bool IsTileForestIndustry(TileIndex tile);
306 /** Data for managing the number of industries of a single industry type. */
307 struct IndustryTypeBuildData {
308 uint32_t probability; ///< Relative probability of building this industry.
309 uint8_t min_number; ///< Smallest number of industries that should exist (either \c 0 or \c 1).
310 uint16_t target_count; ///< Desired number of industries of this type.
311 uint16_t max_wait; ///< Starting number of turns to wait (copied to #wait_count).
312 uint16_t wait_count; ///< Number of turns to wait before trying to build again.
314 void Reset();
316 bool GetIndustryTypeData(IndustryType it);
320 * Data for managing the number and type of industries in the game.
322 struct IndustryBuildData {
323 IndustryTypeBuildData builddata[NUM_INDUSTRYTYPES]; ///< Industry build data for every industry type.
324 uint32_t wanted_inds; ///< Number of wanted industries (bits 31-16), and a fraction (bits 15-0).
326 void Reset();
328 void SetupTargetCount();
329 void TryBuildNewIndustry();
331 void EconomyMonthlyLoop();
334 extern IndustryBuildData _industry_builder;
337 /** Special values for the industry list window for the data parameter of #InvalidateWindowData. */
338 enum IndustryDirectoryInvalidateWindowData {
339 IDIWD_FORCE_REBUILD,
340 IDIWD_PRODUCTION_CHANGE,
341 IDIWD_FORCE_RESORT,
344 void TrimIndustryAcceptedProduced(Industry *ind);
346 #endif /* INDUSTRY_H */