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. */
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
;
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
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.
65 mutable std::string cached_name
; ///< NOSAVE: Cache of the resolved name of the industry
67 Owner founder
; ///< Founder of the industry
68 Date construction_date
; ///< Date of the construction of the industry
69 uint8 construction_type
; ///< Way the industry was constructed (@see IndustryConstructionType)
70 Date last_cargo_accepted_at
[INDUSTRY_NUM_INPUTS
]; ///< Last day each cargo type was accepted by this industry
71 byte selected_layout
; ///< Which tile layout was used when creating the industry
73 uint16 random
; ///< Random value used for randomisation of all kinds of things
75 PersistentStorage
*psa
; ///< Persistent storage for NewGRF industries.
77 Industry(TileIndex tile
= INVALID_TILE
) : location(tile
, 0, 0) {}
80 void RecomputeProductionMultipliers();
83 * Check if a given tile belongs to this industry.
84 * @param tile The tile to check.
85 * @return True if the tile is part of this industry.
87 inline bool TileBelongsToIndustry(TileIndex tile
) const
89 return IsTileType(tile
, MP_INDUSTRY
) && GetIndustryIndex(tile
) == this->index
;
92 inline int GetCargoProducedIndex(CargoID cargo
) const
94 if (cargo
== CT_INVALID
) return -1;
95 const CargoID
*pos
= std::find(this->produced_cargo
, endof(this->produced_cargo
), cargo
);
96 if (pos
== endof(this->produced_cargo
)) return -1;
97 return pos
- this->produced_cargo
;
100 inline int GetCargoAcceptedIndex(CargoID cargo
) const
102 if (cargo
== CT_INVALID
) return -1;
103 const CargoID
*pos
= std::find(this->accepts_cargo
, endof(this->accepts_cargo
), cargo
);
104 if (pos
== endof(this->accepts_cargo
)) return -1;
105 return pos
- this->accepts_cargo
;
109 * Get the industry of the given tile
110 * @param tile the tile to get the industry from
111 * @pre IsTileType(t, MP_INDUSTRY)
112 * @return the industry
114 static inline Industry
*GetByTile(TileIndex tile
)
116 return Industry::Get(GetIndustryIndex(tile
));
119 static Industry
*GetRandom();
120 static void PostDestructor(size_t index
);
123 * Increment the count of industries for this type.
124 * @param type IndustryType to increment
125 * @pre type < NUM_INDUSTRYTYPES
127 static inline void IncIndustryTypeCount(IndustryType type
)
129 assert(type
< NUM_INDUSTRYTYPES
);
134 * Decrement the count of industries for this type.
135 * @param type IndustryType to decrement
136 * @pre type < NUM_INDUSTRYTYPES
138 static inline void DecIndustryTypeCount(IndustryType type
)
140 assert(type
< NUM_INDUSTRYTYPES
);
145 * Get the count of industries for this type.
146 * @param type IndustryType to query
147 * @pre type < NUM_INDUSTRYTYPES
149 static inline uint16
GetIndustryTypeCount(IndustryType type
)
151 assert(type
< NUM_INDUSTRYTYPES
);
155 /** Resets industry counts. */
156 static inline void ResetIndustryCounts()
158 memset(&counts
, 0, sizeof(counts
));
161 inline const char *GetCachedName() const
163 if (this->cached_name
.empty()) this->FillCachedName();
164 return this->cached_name
.c_str();
168 void FillCachedName() const;
171 static uint16 counts
[NUM_INDUSTRYTYPES
]; ///< Number of industries per type ingame
174 void ClearAllIndustryCachedNames();
176 void PlantRandomFarmField(const Industry
*i
);
178 void ReleaseDisastersTargetingIndustry(IndustryID
);
180 bool IsTileForestIndustry(TileIndex tile
);
182 /** Data for managing the number of industries of a single industry type. */
183 struct IndustryTypeBuildData
{
184 uint32 probability
; ///< Relative probability of building this industry.
185 byte min_number
; ///< Smallest number of industries that should exist (either \c 0 or \c 1).
186 uint16 target_count
; ///< Desired number of industries of this type.
187 uint16 max_wait
; ///< Starting number of turns to wait (copied to #wait_count).
188 uint16 wait_count
; ///< Number of turns to wait before trying to build again.
192 bool GetIndustryTypeData(IndustryType it
);
196 * Data for managing the number and type of industries in the game.
198 struct IndustryBuildData
{
199 IndustryTypeBuildData builddata
[NUM_INDUSTRYTYPES
]; ///< Industry build data for every industry type.
200 uint32 wanted_inds
; ///< Number of wanted industries (bits 31-16), and a fraction (bits 15-0).
204 void SetupTargetCount();
205 void TryBuildNewIndustry();
210 extern IndustryBuildData _industry_builder
;
213 /** Special values for the industry list window for the data parameter of #InvalidateWindowData. */
214 enum IndustryDirectoryInvalidateWindowData
{
216 IDIWD_PRODUCTION_CHANGE
,
220 #endif /* INDUSTRY_H */