Fix old map array tunnel head conversion
[openttd/fttd.git] / src / industry.h
blob945378a481c41890510a9d8a9b4e7a5025844517
1 /* $Id$ */
3 /*
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/>.
8 */
10 /** @file industry.h Base of all industries. */
12 #ifndef INDUSTRY_H
13 #define INDUSTRY_H
15 #include "newgrf_storage.h"
16 #include "subsidy_type.h"
17 #include "industrytype.h"
18 #include "map/tilearea.h"
19 #include "map/tileset.h"
21 /**
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*);
36 /**
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 uint16 random; ///< Random value used for randomisation of all kinds of things
71 PersistentStorage *psa; ///< Persistent storage for NewGRF industries.
73 Industry() : location() {}
75 Industry(TileIndex tile) : location(tile, 0, 0)
77 add_to_tileset();
80 ~Industry();
82 void RecomputeProductionMultipliers();
84 /**
85 * Check if a given tile belongs to this industry.
86 * @param tile The tile to check.
87 * @return True if the tile is part of this industry.
89 inline bool TileBelongsToIndustry(TileIndex tile) const
91 return IsIndustryTile(tile) && GetIndustryIndex(tile) == this->index;
94 /**
95 * Get the industry of the given tile
96 * @param tile the tile to get the industry from
97 * @pre IsIndustryTile(t)
98 * @return the industry
100 static inline Industry *GetByTile(TileIndex tile)
102 return Industry::Get(GetIndustryIndex(tile));
105 static Industry *GetRandom();
106 static void PostDestructor(size_t index);
109 * Increment the count of industries for this type.
110 * @param type IndustryType to increment
111 * @pre type < NUM_INDUSTRYTYPES
113 static inline void IncIndustryTypeCount(IndustryType type)
115 assert(type < NUM_INDUSTRYTYPES);
116 counts[type]++;
120 * Decrement the count of industries for this type.
121 * @param type IndustryType to decrement
122 * @pre type < NUM_INDUSTRYTYPES
124 static inline void DecIndustryTypeCount(IndustryType type)
126 assert(type < NUM_INDUSTRYTYPES);
127 counts[type]--;
131 * Get the count of industries for this type.
132 * @param type IndustryType to query
133 * @pre type < NUM_INDUSTRYTYPES
135 static inline uint16 GetIndustryTypeCount(IndustryType type)
137 assert(type < NUM_INDUSTRYTYPES);
138 return counts[type];
141 /** Resets industry counts. */
142 static inline void ResetIndustryCounts()
144 memset(&counts, 0, sizeof(counts));
147 protected:
148 static uint16 counts[NUM_INDUSTRYTYPES]; ///< Number of industries per type ingame
151 /** Get the tile where an industry is located. */
152 inline TileIndex get_industry_tile (const Industry *i)
154 return i->location.tile;
157 void PlantRandomFarmField(const Industry *i);
159 void ReleaseDisastersTargetingIndustry(IndustryID);
161 bool IsTileForestIndustry(TileIndex tile);
163 void CheckIndustries();
165 #define FOR_ALL_INDUSTRIES_FROM(var, start) FOR_ALL_ITEMS_FROM(Industry, industry_index, var, start)
166 #define FOR_ALL_INDUSTRIES(var) FOR_ALL_INDUSTRIES_FROM(var, 0)
168 /** Data for managing the number of industries of a single industry type. */
169 struct IndustryTypeBuildData {
170 uint32 probability; ///< Relative probability of building this industry.
171 byte min_number; ///< Smallest number of industries that should exist (either \c 0 or \c 1).
172 uint16 target_count; ///< Desired number of industries of this type.
173 uint16 max_wait; ///< Starting number of turns to wait (copied to #wait_count).
174 uint16 wait_count; ///< Number of turns to wait before trying to build again.
176 void Reset();
178 bool GetIndustryTypeData(IndustryType it);
182 * Data for managing the number and type of industries in the game.
184 struct IndustryBuildData {
185 IndustryTypeBuildData builddata[NUM_INDUSTRYTYPES]; ///< Industry build data for every industry type.
186 uint32 wanted_inds; ///< Number of wanted industries (bits 31-16), and a fraction (bits 15-0).
188 void Reset();
190 void SetupTargetCount();
191 void TryBuildNewIndustry();
193 void MonthlyLoop();
196 extern IndustryBuildData _industry_builder;
198 #endif /* INDUSTRY_H */