4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file industry.h Base of all industries. */
15 #include "newgrf_storage.h"
16 #include "subsidy_type.h"
17 #include "industrytype.h"
18 #include "map/tilearea.h"
19 #include "map/tileset.h"
22 * Production level maximum, minimum and default values.
23 * It is not a value been really used in order to change, but rather an indicator
24 * of how the industry is behaving.
26 enum ProductionLevels
{
27 PRODLEVEL_CLOSURE
= 0x00, ///< signal set to actually close the industry
28 PRODLEVEL_MINIMUM
= 0x04, ///< below this level, the industry is set to be closing
29 PRODLEVEL_DEFAULT
= 0x10, ///< default level set when the industry is created
30 PRODLEVEL_MAXIMUM
= 0x80, ///< the industry is running at full speed
33 /* Forward declaration. */
34 inline TileIndex
get_industry_tile (const Industry
*);
37 * Defines the internal data of a functional industry.
39 struct Industry
: PooledItem
<Industry
, IndustryID
, 64, 64000>, TileSetObject
<Industry
, get_industry_tile
> {
40 TileArea location
; ///< Location of the industry
41 Town
*town
; ///< Nearest town
42 CargoID produced_cargo
[2]; ///< 2 production cargo slots
43 uint16 produced_cargo_waiting
[2]; ///< amount of cargo produced per cargo
44 uint16 incoming_cargo_waiting
[3]; ///< incoming cargo waiting to be processed
45 byte production_rate
[2]; ///< production rate for each cargo
46 byte prod_level
; ///< general production level
47 CargoID accepts_cargo
[3]; ///< 3 input cargo slots
48 uint16 this_month_production
[2]; ///< stats of this month's production per cargo
49 uint16 this_month_transported
[2]; ///< stats of this month's transport per cargo
50 byte last_month_pct_transported
[2]; ///< percentage transported per cargo in the last full month
51 uint16 last_month_production
[2]; ///< total units produced per cargo in the last full month
52 uint16 last_month_transported
[2]; ///< total units transported per cargo in the last full month
53 uint16 counter
; ///< used for animation and/or production (if available cargo)
55 IndustryType type
; ///< type of industry.
56 OwnerByte owner
; ///< owner of the industry. Which SHOULD always be (imho) OWNER_NONE
57 byte random_colour
; ///< randomized colour of the industry, for display purpose
58 Year last_prod_year
; ///< last year of production
59 byte was_cargo_delivered
; ///< flag that indicate this has been the closest industry chosen for cargo delivery by a station. see DeliverGoodsToIndustry
61 PartOfSubsidyByte part_of_subsidy
; ///< NOSAVE: is this industry a source/destination of a subsidy?
63 OwnerByte founder
; ///< Founder of the industry
64 Date construction_date
; ///< Date of the construction of the industry
65 uint8 construction_type
; ///< Way the industry was constructed (@see IndustryConstructionType)
66 Date last_cargo_accepted_at
; ///< Last day cargo was accepted by this industry
67 byte selected_layout
; ///< Which tile layout was used when creating the industry
69 byte random_triggers
; ///< Triggers for the random
70 uint16 random
; ///< Random value used for randomisation of all kinds of things
72 PersistentStorage
*psa
; ///< Persistent storage for NewGRF industries.
74 Industry() : location() {}
76 Industry(TileIndex tile
) : location(tile
, 0, 0)
83 void RecomputeProductionMultipliers();
86 * Check if a given tile belongs to this industry.
87 * @param tile The tile to check.
88 * @return True if the tile is part of this industry.
90 inline bool TileBelongsToIndustry(TileIndex tile
) const
92 return IsIndustryTile(tile
) && GetIndustryIndex(tile
) == this->index
;
96 * Get the industry of the given tile
97 * @param tile the tile to get the industry from
98 * @pre IsIndustryTile(t)
99 * @return the industry
101 static inline Industry
*GetByTile(TileIndex tile
)
103 return Industry::Get(GetIndustryIndex(tile
));
106 static Industry
*GetRandom();
107 static void PostDestructor(size_t index
);
110 * Increment the count of industries for this type.
111 * @param type IndustryType to increment
112 * @pre type < NUM_INDUSTRYTYPES
114 static inline void IncIndustryTypeCount(IndustryType type
)
116 assert(type
< NUM_INDUSTRYTYPES
);
121 * Decrement the count of industries for this type.
122 * @param type IndustryType to decrement
123 * @pre type < NUM_INDUSTRYTYPES
125 static inline void DecIndustryTypeCount(IndustryType type
)
127 assert(type
< NUM_INDUSTRYTYPES
);
132 * Get the count of industries for this type.
133 * @param type IndustryType to query
134 * @pre type < NUM_INDUSTRYTYPES
136 static inline uint16
GetIndustryTypeCount(IndustryType type
)
138 assert(type
< NUM_INDUSTRYTYPES
);
142 /** Resets industry counts. */
143 static inline void ResetIndustryCounts()
145 memset(&counts
, 0, sizeof(counts
));
149 static uint16 counts
[NUM_INDUSTRYTYPES
]; ///< Number of industries per type ingame
152 /** Get the tile where an industry is located. */
153 inline TileIndex
get_industry_tile (const Industry
*i
)
155 return i
->location
.tile
;
158 void PlantRandomFarmField(const Industry
*i
);
160 void ReleaseDisastersTargetingIndustry(IndustryID
);
162 bool IsTileForestIndustry(TileIndex tile
);
164 #define FOR_ALL_INDUSTRIES_FROM(var, start) FOR_ALL_ITEMS_FROM(Industry, industry_index, var, start)
165 #define FOR_ALL_INDUSTRIES(var) FOR_ALL_INDUSTRIES_FROM(var, 0)
167 /** Data for managing the number of industries of a single industry type. */
168 struct IndustryTypeBuildData
{
169 uint32 probability
; ///< Relative probability of building this industry.
170 byte min_number
; ///< Smallest number of industries that should exist (either \c 0 or \c 1).
171 uint16 target_count
; ///< Desired number of industries of this type.
172 uint16 max_wait
; ///< Starting number of turns to wait (copied to #wait_count).
173 uint16 wait_count
; ///< Number of turns to wait before trying to build again.
177 bool GetIndustryTypeData(IndustryType it
);
181 * Data for managing the number and type of industries in the game.
183 struct IndustryBuildData
{
184 IndustryTypeBuildData builddata
[NUM_INDUSTRYTYPES
]; ///< Industry build data for every industry type.
185 uint32 wanted_inds
; ///< Number of wanted industries (bits 31-16), and a fraction (bits 15-0).
189 void SetupTargetCount();
190 void TryBuildNewIndustry();
195 extern IndustryBuildData _industry_builder
;
197 #endif /* INDUSTRY_H */