(svn r27724) -Cleanup: Remove pointless usage of IsOpenTTDBaseGRF. System GRFs are...
[openttd.git] / src / industry.h
blob5e5d0468803d993b2c3d40eb9bda21200635af99
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 "industry_map.h"
18 #include "tilearea_type.h"
21 typedef Pool<Industry, IndustryID, 64, 64000> IndustryPool;
22 extern IndustryPool _industry_pool;
24 /**
25 * Production level maximum, minimum and default values.
26 * It is not a value been really used in order to change, but rather an indicator
27 * of how the industry is behaving.
29 enum ProductionLevels {
30 PRODLEVEL_CLOSURE = 0x00, ///< signal set to actually close the industry
31 PRODLEVEL_MINIMUM = 0x04, ///< below this level, the industry is set to be closing
32 PRODLEVEL_DEFAULT = 0x10, ///< default level set when the industry is created
33 PRODLEVEL_MAXIMUM = 0x80, ///< the industry is running at full speed
36 /**
37 * Defines the internal data of a functional industry.
39 struct Industry : IndustryPool::PoolItem<&_industry_pool> {
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(TileIndex tile = INVALID_TILE) : location(tile, 0, 0) {}
75 ~Industry();
77 void RecomputeProductionMultipliers();
79 /**
80 * Check if a given tile belongs to this industry.
81 * @param tile The tile to check.
82 * @return True if the tile is part of this industry.
84 inline bool TileBelongsToIndustry(TileIndex tile) const
86 return IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == this->index;
89 /**
90 * Get the industry of the given tile
91 * @param tile the tile to get the industry from
92 * @pre IsTileType(t, MP_INDUSTRY)
93 * @return the industry
95 static inline Industry *GetByTile(TileIndex tile)
97 return Industry::Get(GetIndustryIndex(tile));
100 static Industry *GetRandom();
101 static void PostDestructor(size_t index);
104 * Increment the count of industries for this type.
105 * @param type IndustryType to increment
106 * @pre type < NUM_INDUSTRYTYPES
108 static inline void IncIndustryTypeCount(IndustryType type)
110 assert(type < NUM_INDUSTRYTYPES);
111 counts[type]++;
115 * Decrement the count of industries for this type.
116 * @param type IndustryType to decrement
117 * @pre type < NUM_INDUSTRYTYPES
119 static inline void DecIndustryTypeCount(IndustryType type)
121 assert(type < NUM_INDUSTRYTYPES);
122 counts[type]--;
126 * Get the count of industries for this type.
127 * @param type IndustryType to query
128 * @pre type < NUM_INDUSTRYTYPES
130 static inline uint16 GetIndustryTypeCount(IndustryType type)
132 assert(type < NUM_INDUSTRYTYPES);
133 return counts[type];
136 /** Resets industry counts. */
137 static inline void ResetIndustryCounts()
139 memset(&counts, 0, sizeof(counts));
142 protected:
143 static uint16 counts[NUM_INDUSTRYTYPES]; ///< Number of industries per type ingame
146 void PlantRandomFarmField(const Industry *i);
148 void ReleaseDisastersTargetingIndustry(IndustryID);
150 bool IsTileForestIndustry(TileIndex tile);
152 #define FOR_ALL_INDUSTRIES_FROM(var, start) FOR_ALL_ITEMS_FROM(Industry, industry_index, var, start)
153 #define FOR_ALL_INDUSTRIES(var) FOR_ALL_INDUSTRIES_FROM(var, 0)
155 /** Data for managing the number of industries of a single industry type. */
156 struct IndustryTypeBuildData {
157 uint32 probability; ///< Relative probability of building this industry.
158 byte min_number; ///< Smallest number of industries that should exist (either \c 0 or \c 1).
159 uint16 target_count; ///< Desired number of industries of this type.
160 uint16 max_wait; ///< Starting number of turns to wait (copied to #wait_count).
161 uint16 wait_count; ///< Number of turns to wait before trying to build again.
163 void Reset();
165 bool GetIndustryTypeData(IndustryType it);
169 * Data for managing the number and type of industries in the game.
171 struct IndustryBuildData {
172 IndustryTypeBuildData builddata[NUM_INDUSTRYTYPES]; ///< Industry build data for every industry type.
173 uint32 wanted_inds; ///< Number of wanted industries (bits 31-16), and a fraction (bits 15-0).
175 void Reset();
177 void SetupTargetCount();
178 void TryBuildNewIndustry();
180 void MonthlyLoop();
183 extern IndustryBuildData _industry_builder;
185 #endif /* INDUSTRY_H */