OpenTTD
industry.h
Go to the documentation of this file.
1 /* $Id$ */
2 
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  */
9 
12 #ifndef INDUSTRY_H
13 #define INDUSTRY_H
14 
15 #include <algorithm>
16 #include "newgrf_storage.h"
17 #include "subsidy_type.h"
18 #include "industry_map.h"
19 #include "industrytype.h"
20 #include "tilearea_type.h"
21 
22 
24 extern IndustryPool _industry_pool;
25 
36 };
37 
41 struct Industry : IndustryPool::PoolItem<&_industry_pool> {
48  byte prod_level;
55  uint16 counter;
56 
57  IndustryType type;
62 
64 
70 
71  uint16 random;
72 
74 
75  Industry(TileIndex tile = INVALID_TILE) : location(tile, 0, 0) {}
76  ~Industry();
77 
79 
85  inline bool TileBelongsToIndustry(TileIndex tile) const
86  {
87  return IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == this->index;
88  }
89 
90  inline int GetCargoProducedIndex(CargoID cargo) const
91  {
92  if (cargo == CT_INVALID) return -1;
93  const CargoID *pos = std::find(this->produced_cargo, endof(this->produced_cargo), cargo);
94  if (pos == endof(this->produced_cargo)) return -1;
95  return pos - this->produced_cargo;
96  }
97 
98  inline int GetCargoAcceptedIndex(CargoID cargo) const
99  {
100  if (cargo == CT_INVALID) return -1;
101  const CargoID *pos = std::find(this->accepts_cargo, endof(this->accepts_cargo), cargo);
102  if (pos == endof(this->accepts_cargo)) return -1;
103  return pos - this->accepts_cargo;
104  }
105 
112  static inline Industry *GetByTile(TileIndex tile)
113  {
114  return Industry::Get(GetIndustryIndex(tile));
115  }
116 
117  static Industry *GetRandom();
118  static void PostDestructor(size_t index);
119 
125  static inline void IncIndustryTypeCount(IndustryType type)
126  {
127  assert(type < NUM_INDUSTRYTYPES);
128  counts[type]++;
129  }
130 
136  static inline void DecIndustryTypeCount(IndustryType type)
137  {
138  assert(type < NUM_INDUSTRYTYPES);
139  counts[type]--;
140  }
141 
147  static inline uint16 GetIndustryTypeCount(IndustryType type)
148  {
149  assert(type < NUM_INDUSTRYTYPES);
150  return counts[type];
151  }
152 
154  static inline void ResetIndustryCounts()
155  {
156  memset(&counts, 0, sizeof(counts));
157  }
158 
159 protected:
160  static uint16 counts[NUM_INDUSTRYTYPES];
161 };
162 
163 void PlantRandomFarmField(const Industry *i);
164 
165 void ReleaseDisastersTargetingIndustry(IndustryID);
166 
168 
169 #define FOR_ALL_INDUSTRIES_FROM(var, start) FOR_ALL_ITEMS_FROM(Industry, industry_index, var, start)
170 #define FOR_ALL_INDUSTRIES(var) FOR_ALL_INDUSTRIES_FROM(var, 0)
171 
174  uint32 probability;
175  byte min_number;
176  uint16 target_count;
177  uint16 max_wait;
178  uint16 wait_count;
179 
180  void Reset();
181 
182  bool GetIndustryTypeData(IndustryType it);
183 };
184 
190  uint32 wanted_inds;
191 
192  void Reset();
193 
194  void SetupTargetCount();
195  void TryBuildNewIndustry();
196 
197  void MonthlyLoop();
198 };
199 
201 
202 #endif /* INDUSTRY_H */
static void ResetIndustryCounts()
Resets industry counts.
Definition: industry.h:154
void ReleaseDisastersTargetingIndustry(IndustryID)
Marks all disasters targeting this industry in such a way they won&#39;t call Industry::Get(v->dest_tile)...
byte production_rate[INDUSTRY_NUM_OUTPUTS]
production rate for each cargo
Definition: industry.h:47
ProductionLevels
Production level maximum, minimum and default values.
Definition: industry.h:31
uint16 max_wait
Starting number of turns to wait (copied to wait_count).
Definition: industry.h:177
basic types related to subsidies
static const int INDUSTRY_NUM_INPUTS
Number of cargo types an industry can accept.
Definition: industry_type.h:40
IndustryBuildData _industry_builder
In-game manager of industries.
Part of an industry.
Definition: tile_type.h:51
uint16 counter
used for animation and/or production (if available cargo)
Definition: industry.h:55
below this level, the industry is set to be closing
Definition: industry.h:33
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:246
OwnerByte founder
Founder of the industry.
Definition: industry.h:65
Functionality related to the temporary and persistent storage arrays for NewGRFs. ...
Town * town
Nearest town.
Definition: industry.h:43
byte selected_layout
Which tile layout was used when creating the industry.
Definition: industry.h:69
uint8 construction_type
Way the industry was constructed (.
Definition: industry.h:67
default level set when the industry is created
Definition: industry.h:34
signal set to actually close the industry
Definition: industry.h:32
Defines the internal data of a functional industry.
Definition: industry.h:41
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
Industry type specs.
Date last_cargo_accepted_at[INDUSTRY_NUM_INPUTS]
Last day each cargo type was accepted by this industry.
Definition: industry.h:68
byte was_cargo_delivered
flag that indicate this has been the closest industry chosen for cargo delivery by a station...
Definition: industry.h:61
uint16 random
Random value used for randomisation of all kinds of things.
Definition: industry.h:71
static Industry * GetRandom()
Return a random valid industry.
int32 Year
Type for the year, note: 0 based, i.e. starts at the year 0.
Definition: date_type.h:20
Accessors for industries.
the industry is running at full speed
Definition: industry.h:35
static const int INDUSTRY_NUM_OUTPUTS
Number of cargo types an industry can produce.
Definition: industry_type.h:41
byte random_colour
randomized colour of the industry, for display purpose
Definition: industry.h:59
Date construction_date
Date of the construction of the industry.
Definition: industry.h:66
Data for managing the number and type of industries in the game.
Definition: industry.h:188
Invalid cargo type.
Definition: cargo_type.h:70
uint16 this_month_production[INDUSTRY_NUM_OUTPUTS]
stats of this month&#39;s production per cargo
Definition: industry.h:50
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:152
PersistentStorage * psa
Persistent storage for NewGRF industries.
Definition: industry.h:73
PartOfSubsidyByte part_of_subsidy
NOSAVE: is this industry a source/destination of a subsidy?
Definition: industry.h:63
bool IsTileForestIndustry(TileIndex tile)
Check whether the tile is a forest.
uint16 this_month_transported[INDUSTRY_NUM_OUTPUTS]
stats of this month&#39;s transport per cargo
Definition: industry.h:51
Year last_prod_year
last year of production
Definition: industry.h:60
IndustryType type
type of industry.
Definition: industry.h:57
TileArea location
Location of the industry.
Definition: industry.h:42
uint16 last_month_production[INDUSTRY_NUM_OUTPUTS]
total units produced per cargo in the last full month
Definition: industry.h:53
Represents the covered area of e.g.
Definition: tilearea_type.h:18
CargoID produced_cargo[INDUSTRY_NUM_OUTPUTS]
16 production cargo slots
Definition: industry.h:44
Class for pooled persistent storage of data.
static uint16 GetIndustryTypeCount(IndustryType type)
Get the count of industries for this type.
Definition: industry.h:147
CargoID accepts_cargo[INDUSTRY_NUM_INPUTS]
16 input cargo slots
Definition: industry.h:49
uint16 incoming_cargo_waiting[INDUSTRY_NUM_INPUTS]
incoming cargo waiting to be processed
Definition: industry.h:46
byte prod_level
general production level
Definition: industry.h:48
Data for managing the number of industries of a single industry type.
Definition: industry.h:173
Base class for all PoolItems.
Definition: pool_type.hpp:146
uint16 wait_count
Number of turns to wait before trying to build again.
Definition: industry.h:178
static void IncIndustryTypeCount(IndustryType type)
Increment the count of industries for this type.
Definition: industry.h:125
Base class for all pools.
Definition: pool_type.hpp:83
static void PostDestructor(size_t index)
Invalidating some stuff after removing item from the pool.
uint16 produced_cargo_waiting[INDUSTRY_NUM_OUTPUTS]
amount of cargo produced per cargo
Definition: industry.h:45
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
void RecomputeProductionMultipliers()
Recompute production_rate for current prod_level.
static const IndustryType NUM_INDUSTRYTYPES
total number of industry types, new and old; limited to 240 because we need some special ids like INV...
Definition: industry_type.h:28
static IndustryID GetIndustryIndex(TileIndex t)
Get the industry ID of the given tile.
Definition: industry_map.h:65
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:412
static void DecIndustryTypeCount(IndustryType type)
Decrement the count of industries for this type.
Definition: industry.h:136
Town data structure.
Definition: town.h:55
Type for storing the &#39;area&#39; of something uses on the map.
uint32 probability
Relative probability of building this industry.
Definition: industry.h:174
static uint16 counts[NUM_INDUSTRYTYPES]
Number of industries per type ingame.
Definition: industry.h:160
uint32 wanted_inds
Number of wanted industries (bits 31-16), and a fraction (bits 15-0).
Definition: industry.h:190
uint16 last_month_transported[INDUSTRY_NUM_OUTPUTS]
total units transported per cargo in the last full month
Definition: industry.h:54
int32 Date
The type to store our dates in.
Definition: date_type.h:16
byte min_number
Smallest number of industries that should exist (either 0 or 1).
Definition: industry.h:175
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:85
bool TileBelongsToIndustry(TileIndex tile) const
Check if a given tile belongs to this industry.
Definition: industry.h:85
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
static Industry * GetByTile(TileIndex tile)
Get the industry of the given tile.
Definition: industry.h:112
uint16 target_count
Desired number of industries of this type.
Definition: industry.h:176
byte last_month_pct_transported[INDUSTRY_NUM_OUTPUTS]
percentage transported per cargo in the last full month
Definition: industry.h:52
OwnerByte owner
owner of the industry. Which SHOULD always be (imho) OWNER_NONE
Definition: industry.h:58