Change: Only resort town directory window on population change if necessary
[openttd-github.git] / src / industry.h
blob932104ff458616a9c1a03b73ed5ca6553658091c
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 <algorithm>
14 #include "newgrf_storage.h"
15 #include "subsidy_type.h"
16 #include "industry_map.h"
17 #include "industrytype.h"
18 #include "tilearea_type.h"
19 #include "station_base.h"
22 typedef Pool<Industry, IndustryID, 64, 64000> IndustryPool;
23 extern IndustryPool _industry_pool;
25 /**
26 * Production level maximum, minimum and default values.
27 * It is not a value been really used in order to change, but rather an indicator
28 * of how the industry is behaving.
30 enum ProductionLevels {
31 PRODLEVEL_CLOSURE = 0x00, ///< signal set to actually close the industry
32 PRODLEVEL_MINIMUM = 0x04, ///< below this level, the industry is set to be closing
33 PRODLEVEL_DEFAULT = 0x10, ///< default level set when the industry is created
34 PRODLEVEL_MAXIMUM = 0x80, ///< the industry is running at full speed
37 /**
38 * Defines the internal data of a functional industry.
40 struct Industry : IndustryPool::PoolItem<&_industry_pool> {
41 TileArea location; ///< Location of the industry
42 Town *town; ///< Nearest town
43 Station *neutral_station; ///< Associated neutral station
44 CargoID produced_cargo[INDUSTRY_NUM_OUTPUTS]; ///< 16 production cargo slots
45 uint16 produced_cargo_waiting[INDUSTRY_NUM_OUTPUTS]; ///< amount of cargo produced per cargo
46 uint16 incoming_cargo_waiting[INDUSTRY_NUM_INPUTS]; ///< incoming cargo waiting to be processed
47 byte production_rate[INDUSTRY_NUM_OUTPUTS]; ///< production rate for each cargo
48 byte prod_level; ///< general production level
49 CargoID accepts_cargo[INDUSTRY_NUM_INPUTS]; ///< 16 input cargo slots
50 uint16 this_month_production[INDUSTRY_NUM_OUTPUTS]; ///< stats of this month's production per cargo
51 uint16 this_month_transported[INDUSTRY_NUM_OUTPUTS]; ///< stats of this month's transport per cargo
52 byte last_month_pct_transported[INDUSTRY_NUM_OUTPUTS]; ///< percentage transported per cargo in the last full month
53 uint16 last_month_production[INDUSTRY_NUM_OUTPUTS]; ///< total units produced per cargo in the last full month
54 uint16 last_month_transported[INDUSTRY_NUM_OUTPUTS]; ///< total units transported per cargo in the last full month
55 uint16 counter; ///< used for animation and/or production (if available cargo)
57 IndustryType type; ///< type of industry.
58 Owner owner; ///< owner of the industry. Which SHOULD always be (imho) OWNER_NONE
59 byte random_colour; ///< randomized colour of the industry, for display purpose
60 Year last_prod_year; ///< last year of production
61 byte was_cargo_delivered; ///< flag that indicate this has been the closest industry chosen for cargo delivery by a station. see DeliverGoodsToIndustry
63 PartOfSubsidy part_of_subsidy; ///< NOSAVE: is this industry a source/destination of a subsidy?
64 StationList stations_near; ///< NOSAVE: List of nearby stations.
66 Owner founder; ///< Founder of the industry
67 Date construction_date; ///< Date of the construction of the industry
68 uint8 construction_type; ///< Way the industry was constructed (@see IndustryConstructionType)
69 Date last_cargo_accepted_at[INDUSTRY_NUM_INPUTS]; ///< Last day each cargo type was accepted by this industry
70 byte selected_layout; ///< Which tile layout was used when creating the industry
72 uint16 random; ///< Random value used for randomisation of all kinds of things
74 PersistentStorage *psa; ///< Persistent storage for NewGRF industries.
76 Industry(TileIndex tile = INVALID_TILE) : location(tile, 0, 0) {}
77 ~Industry();
79 void RecomputeProductionMultipliers();
81 /**
82 * Check if a given tile belongs to this industry.
83 * @param tile The tile to check.
84 * @return True if the tile is part of this industry.
86 inline bool TileBelongsToIndustry(TileIndex tile) const
88 return IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == this->index;
91 inline int GetCargoProducedIndex(CargoID cargo) const
93 if (cargo == CT_INVALID) return -1;
94 const CargoID *pos = std::find(this->produced_cargo, endof(this->produced_cargo), cargo);
95 if (pos == endof(this->produced_cargo)) return -1;
96 return pos - this->produced_cargo;
99 inline int GetCargoAcceptedIndex(CargoID cargo) const
101 if (cargo == CT_INVALID) return -1;
102 const CargoID *pos = std::find(this->accepts_cargo, endof(this->accepts_cargo), cargo);
103 if (pos == endof(this->accepts_cargo)) return -1;
104 return pos - this->accepts_cargo;
108 * Get the industry of the given tile
109 * @param tile the tile to get the industry from
110 * @pre IsTileType(t, MP_INDUSTRY)
111 * @return the industry
113 static inline Industry *GetByTile(TileIndex tile)
115 return Industry::Get(GetIndustryIndex(tile));
118 static Industry *GetRandom();
119 static void PostDestructor(size_t index);
122 * Increment the count of industries for this type.
123 * @param type IndustryType to increment
124 * @pre type < NUM_INDUSTRYTYPES
126 static inline void IncIndustryTypeCount(IndustryType type)
128 assert(type < NUM_INDUSTRYTYPES);
129 counts[type]++;
133 * Decrement the count of industries for this type.
134 * @param type IndustryType to decrement
135 * @pre type < NUM_INDUSTRYTYPES
137 static inline void DecIndustryTypeCount(IndustryType type)
139 assert(type < NUM_INDUSTRYTYPES);
140 counts[type]--;
144 * Get the count of industries for this type.
145 * @param type IndustryType to query
146 * @pre type < NUM_INDUSTRYTYPES
148 static inline uint16 GetIndustryTypeCount(IndustryType type)
150 assert(type < NUM_INDUSTRYTYPES);
151 return counts[type];
154 /** Resets industry counts. */
155 static inline void ResetIndustryCounts()
157 memset(&counts, 0, sizeof(counts));
160 protected:
161 static uint16 counts[NUM_INDUSTRYTYPES]; ///< Number of industries per type ingame
164 void PlantRandomFarmField(const Industry *i);
166 void ReleaseDisastersTargetingIndustry(IndustryID);
168 bool IsTileForestIndustry(TileIndex tile);
170 /** Data for managing the number of industries of a single industry type. */
171 struct IndustryTypeBuildData {
172 uint32 probability; ///< Relative probability of building this industry.
173 byte min_number; ///< Smallest number of industries that should exist (either \c 0 or \c 1).
174 uint16 target_count; ///< Desired number of industries of this type.
175 uint16 max_wait; ///< Starting number of turns to wait (copied to #wait_count).
176 uint16 wait_count; ///< Number of turns to wait before trying to build again.
178 void Reset();
180 bool GetIndustryTypeData(IndustryType it);
184 * Data for managing the number and type of industries in the game.
186 struct IndustryBuildData {
187 IndustryTypeBuildData builddata[NUM_INDUSTRYTYPES]; ///< Industry build data for every industry type.
188 uint32 wanted_inds; ///< Number of wanted industries (bits 31-16), and a fraction (bits 15-0).
190 void Reset();
192 void SetupTargetCount();
193 void TryBuildNewIndustry();
195 void MonthlyLoop();
198 extern IndustryBuildData _industry_builder;
200 #endif /* INDUSTRY_H */