OpenTTD
town.h
Go to the documentation of this file.
1 /* $Id: town.h 27105 2015-01-01 21:25:42Z rubidium $ */
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 TOWN_H
13 #define TOWN_H
14 
15 #include "viewport_type.h"
16 #include "town_map.h"
17 #include "subsidy_type.h"
18 #include "newgrf_storage.h"
19 #include "cargotype.h"
20 #include "tilematrix_type.hpp"
21 #include <list>
22 
23 template <typename T>
25  T id_count[NUM_HOUSES];
26  T class_count[HOUSE_CLASS_MAX];
27 };
28 
30 
31 static const uint CUSTOM_TOWN_NUMBER_DIFFICULTY = 4;
32 static const uint CUSTOM_TOWN_MAX_NUMBER = 5000;
33 
34 static const uint INVALID_TOWN = 0xFFFF;
35 
36 static const uint TOWN_GROWTH_WINTER = 0xFFFFFFFE;
37 static const uint TOWN_GROWTH_DESERT = 0xFFFFFFFF;
38 static const uint16 TOWN_GROW_RATE_CUSTOM = 0x8000;
39 static const uint16 TOWN_GROW_RATE_CUSTOM_NONE = 0xFFFF;
40 
42 extern TownPool _town_pool;
43 
45 struct TownCache {
46  uint32 num_houses;
47  uint32 population;
50  uint32 squared_town_zone_radius[HZB_END];
52 };
53 
55 struct Town : TownPool::PoolItem<&_town_pool> {
57 
59 
60  /* Town name */
61  uint32 townnamegrfid;
62  uint16 townnametype;
63  uint32 townnameparts;
64  char *name;
65 
66  byte flags;
67 
68  uint16 noise_reached;
69 
70  CompanyMask statues;
71 
72  /* Company ratings. */
73  CompanyMask have_ratings;
74  uint8 unwanted[MAX_COMPANIES];
77  int16 ratings[MAX_COMPANIES];
78 
81  uint32 goal[NUM_TE];
82 
83  char *text;
84 
85  inline byte GetPercentTransported(CargoID cid) const { return this->supplied[cid].old_act * 256 / (this->supplied[cid].old_max + 1); }
86 
87  /* Cargo production and acceptance stats. */
88  uint32 cargo_produced;
91 
93 
94  uint16 grow_counter;
95  uint16 growth_rate;
96 
99 
100  bool larger_town;
102 
103  std::list<PersistentStorage *> psa_list;
104 
109  Town(TileIndex tile = INVALID_TILE) : xy(tile) { }
110 
112  ~Town();
113 
114  void InitializeLayout(TownLayout layout);
115 
122  inline uint16 MaxTownNoise() const
123  {
124  if (this->cache.population == 0) return 0; // no population? no noise
125 
126  /* 3 is added (the noise of the lowest airport), so the user can at least build a small airfield. */
128  }
129 
130  void UpdateVirtCoord();
131 
132  static inline Town *GetByTile(TileIndex tile)
133  {
134  return Town::Get(GetTownIndex(tile));
135  }
136 
137  static Town *GetRandom();
138  static void PostDestructor(size_t index);
139 };
140 
141 uint32 GetWorldPopulation();
142 
144 void ShowTownViewWindow(TownID town);
145 void ExpandTown(Town *t);
146 
155 };
156 
164 enum TownFlags {
168 };
169 
171 
172 
174 
175 Town *CalcClosestTownFromTile(TileIndex tile, uint threshold = UINT_MAX);
176 
177 #define FOR_ALL_TOWNS_FROM(var, start) FOR_ALL_ITEMS_FROM(Town, town_index, var, start)
178 #define FOR_ALL_TOWNS(var) FOR_ALL_TOWNS_FROM(var, 0)
179 
180 void ResetHouses();
181 
182 void ClearTownHouse(Town *t, TileIndex tile);
183 void UpdateTownMaxPass(Town *t);
184 void UpdateTownRadius(Town *t);
185 void UpdateTownCargoes(Town *t);
186 void UpdateTownCargoTotal(Town *t);
187 void UpdateTownCargoBitmap();
189 Town *ClosestTownFromTile(TileIndex tile, uint threshold);
190 void ChangeTownRating(Town *t, int add, int max, DoCommandFlag flags);
191 HouseZonesBits GetTownRadiusGroup(const Town *t, TileIndex tile);
192 void SetTownRatingTestMode(bool mode);
193 uint GetMaskOfTownActions(int *nump, CompanyID cid, const Town *t);
194 bool GenerateTowns(TownLayout layout);
196 
197 
200  TACT_NONE = 0x00,
201 
209  TACT_BRIBE = 0x80,
210 
212 
217 };
219 
220 extern const byte _town_action_costs[TACT_COUNT];
221 extern TownID _new_town_id;
222 
228 template <class T>
229 void MakeDefaultName(T *obj)
230 {
231  /* We only want to set names if it hasn't been set before, or when we're calling from afterload. */
232  assert(obj->name == NULL || obj->town_cn == UINT16_MAX);
233 
234  obj->town = ClosestTownFromTile(obj->xy, UINT_MAX);
235 
236  /* Find first unused number belonging to this town. This can never fail,
237  * as long as there can be at most 65535 waypoints/depots in total.
238  *
239  * This does 'n * m' search, but with 32bit 'used' bitmap, it needs at
240  * most 'n * (1 + ceil(m / 32))' steps (n - number of waypoints in pool,
241  * m - number of waypoints near this town).
242  * Usually, it needs only 'n' steps.
243  *
244  * If it wasn't using 'used' and 'idx', it would just search for increasing 'next',
245  * but this way it is faster */
246 
247  uint32 used = 0; // bitmap of used waypoint numbers, sliding window with 'next' as base
248  uint32 next = 0; // first number in the bitmap
249  uint32 idx = 0; // index where we will stop
250  uint32 cid = 0; // current index, goes to T::GetPoolSize()-1, then wraps to 0
251 
252  do {
253  T *lobj = T::GetIfValid(cid);
254 
255  /* check only valid waypoints... */
256  if (lobj != NULL && obj != lobj) {
257  /* only objects within the same city and with the same type */
258  if (lobj->town == obj->town && lobj->IsOfType(obj)) {
259  /* if lobj->town_cn < next, uint will overflow to '+inf' */
260  uint i = (uint)lobj->town_cn - next;
261 
262  if (i < 32) {
263  SetBit(used, i); // update bitmap
264  if (i == 0) {
265  /* shift bitmap while the lowest bit is '1';
266  * increase the base of the bitmap too */
267  do {
268  used >>= 1;
269  next++;
270  } while (HasBit(used, 0));
271  /* when we are at 'idx' again at end of the loop and
272  * 'next' hasn't changed, then no object had town_cn == next,
273  * so we can safely use it */
274  idx = cid;
275  }
276  }
277  }
278  }
279 
280  cid++;
281  if (cid == T::GetPoolSize()) cid = 0; // wrap to zero...
282  } while (cid != idx);
283 
284  obj->town_cn = (uint16)next; // set index...
285 }
286 
287 extern uint32 _town_cargoes_accepted;
288 
289 #endif /* TOWN_H */
static const uint TOWN_GROWTH_WINTER
The town only needs this cargo in the winter (any amount)
Definition: town.h:36
TileIndexDiff GetHouseNorthPart(HouseID &house)
Determines if a given HouseID is part of a multitile house.
Definition: town_cmd.cpp:2415
AcceptanceMatrix cargo_accepted
Bitmap of cargoes accepted by houses for each 4*4 map square of the town.
Definition: town.h:89
DECLARE_ENUM_AS_BIT_SET(GenderEthnicity) enum CompanyManagerFaceVariable
Bitgroups of the CompanyManagerFace variable.
There can be only one stadium by town.
Definition: town.h:167
TownRatingCheckType
Action types that a company must ask permission for to a town authority.
Definition: town.h:151
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:77
void UpdateTownCargoes(Town *t)
Update cargo acceptance for the complete town.
Definition: town_cmd.cpp:749
CompanyMask statues
which companies have a statue?
Definition: town.h:70
basic types related to subsidies
static const uint CUSTOM_TOWN_MAX_NUMBER
this is the maximum number of towns a user can specify in customisation
Definition: town.h:32
void UpdateTownCargoTotal(Town *t)
Update the total cargo acceptance of the whole town.
Definition: town_cmd.cpp:701
EconomySettings economy
settings to change the economy
int32 TileIndexDiff
An offset value between to tiles.
Definition: map_func.h:156
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:246
byte fund_buildings_months
fund buildings program in action?
Definition: town.h:97
CompanyByte exclusivity
which company has exclusivity
Definition: town.h:75
Functionality related to the temporary and persistent storage arrays for NewGRFs. ...
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
byte flags
See TownFlags.
Definition: town.h:66
Specification of a cargo type.
Definition: cargotype.h:56
Medium advertising campaign.
Definition: town.h:203
Build a statue.
Definition: town.h:206
Amount of town effects.
Definition: cargotype.h:34
Town(TileIndex tile=INVALID_TILE)
Creates a new town.
Definition: town.h:109
void UpdateAllTownVirtCoords()
Update the virtual coords needed to draw the town sign for all towns.
Definition: town_cmd.cpp:387
DifficultySettings difficulty
settings related to the difficulty
uint16 time_until_rebuild
time until we rebuild a house
Definition: town.h:92
void SetTownRatingTestMode(bool mode)
Switch the town rating to test-mode, to allow commands to be tested without affecting current ratings...
Definition: town_cmd.cpp:3280
Template for storing a value per area of the map.
uint32 cargo_produced
Bitmap of all cargoes produced by houses in this town.
Definition: town.h:88
uint32 cargo_accepted_total
NOSAVE: Bitmap of all cargoes accepted by houses in this town.
Definition: town.h:90
Common return value for all commands.
Definition: command_type.h:25
bool GenerateTowns(TownLayout layout)
This function will generate a certain amount of towns, with a certain layout It can be called from th...
Definition: town_cmd.cpp:1949
uint32 GetWorldPopulation()
Determines the world population Basically, count population of all towns, one by one.
Definition: town_cmd.cpp:415
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:26
uint16 HouseID
OpenTTD ID of house types.
Definition: house_type.h:15
uint32 population
Current population of people.
Definition: town.h:47
uint16 noise_reached
level of noise that all the airports are generating
Definition: town.h:68
const byte _town_action_costs[TACT_COUNT]
Factor in the cost of each town action.
Definition: town_cmd.cpp:2750
void UpdateTownCargoBitmap()
Updates the bitmap of all cargoes accepted by houses.
Definition: town_cmd.cpp:768
static const HouseID NUM_HOUSES
Total number of houses.
Definition: house.h:31
Rebuild the roads.
Definition: town.h:205
Types related to viewports.
Large advertising campaign.
Definition: town.h:204
TownActions
Town actions of a company.
Definition: town.h:199
Town * ClosestTownFromTile(TileIndex tile, uint threshold)
Return the town closest (in distance or ownership) to a given tile, within a given threshold...
Definition: town_cmd.cpp:3239
TownEffect
Town growth effect when delivering cargo.
Definition: cargotype.h:25
TileIndex xy
town center tile
Definition: town.h:56
void MakeDefaultName(T *obj)
Set the default name for a depot/waypoint.
Definition: town.h:229
ViewportSign sign
Location of name sign, UpdateVirtCoord updates this.
Definition: town.h:48
Fund new buildings.
Definition: town.h:207
static TownID GetTownIndex(TileIndex t)
Get the index of which town this house/street is attached to.
Definition: town_map.h:24
All possible advertising actions.
Definition: town.h:213
DoCommandFlag
List of flags for a command.
Definition: command_type.h:340
All possible construction actions.
Definition: town.h:214
static const uint16 TOWN_GROW_RATE_CUSTOM
If this mask is applied to Town::growth_rate, the grow_counter will not be calculated by the system (...
Definition: town.h:38
uint16 town_noise_population[3]
population to base decision on noise evaluation (
Location information about a sign as seen on the viewport.
Definition: viewport_type.h:48
BuildingCounts< uint16 > building_counts
The number of each type of building in the town.
Definition: town.h:51
Number of available town actions.
Definition: town.h:211
const CargoSpec * FindFirstCargoWithTownEffect(TownEffect effect)
Determines the first cargo with a certain town effect.
Definition: town_cmd.cpp:2510
All possible funding actions.
Definition: town.h:215
uint32 _town_cargoes_accepted
Bitmap of all cargoes accepted by houses.
Definition: town_cmd.cpp:56
Small advertising campaign.
Definition: town.h:202
Accessors for towns.
HouseZonesBits GetTownRadiusGroup(const Town *t, TileIndex tile)
Returns the bit corresponding to the town zone of the specified tile.
Definition: town_cmd.cpp:1998
Base class for all PoolItems.
Definition: pool_type.hpp:146
Maximum number of companies.
Definition: company_type.h:25
uint16 MaxTownNoise() const
Calculate the max town noise.
Definition: town.h:122
Empty action set.
Definition: town.h:200
Number of town checking action types.
Definition: town.h:154
Base class for all pools.
Definition: pool_type.hpp:83
Removal of a tunnel or bridge owned by the towb.
Definition: town.h:153
Maximal number of cargo types in a game.
Definition: cargo_type.h:66
Town * CalcClosestTownFromTile(TileIndex tile, uint threshold=UINT_MAX)
Return the town closest to the given tile within threshold.
Definition: town_cmd.cpp:3214
char * text
General text with additional information.
Definition: town.h:83
static const uint CUSTOM_TOWN_NUMBER_DIFFICULTY
value for custom town number in difficulty settings
Definition: town.h:31
static const uint TOWN_GROWTH_DESERT
The town needs the cargo for growth when on desert (any amount)
Definition: town.h:37
uint16 growth_rate
town growth rate
Definition: town.h:95
CommandCost CheckIfAuthorityAllowsNewStation(TileIndex tile, DoCommandFlag flags)
Checks whether the local authority allows construction of a new station (rail, road, airport, dock) on the given tile.
Definition: town_cmd.cpp:3193
Tstorage old_max
Maximum amount last month.
Definition: town_type.h:113
There can be only one church by town.
Definition: town.h:166
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
static const uint HOUSE_CLASS_MAX
There can only be as many classes as there are new houses, plus one for NO_CLASS, as the original hou...
Definition: house.h:38
Buy exclusive transport rights.
Definition: town.h:208
static const uint16 TOWN_GROW_RATE_CUSTOM_NONE
Special value for Town::growth_rate to disable town growth.
Definition: town.h:39
byte town_council_tolerance
minimum required town ratings to be allowed to demolish stuff
Definition: settings_type.h:71
Removal of a road owned by the town.
Definition: town.h:152
uint GetMaskOfTownActions(int *nump, CompanyID cid, const Town *t)
Get a list of available actions to do at a town.
Definition: town_cmd.cpp:2989
TownFlags
This enum is used in conjunction with town->flags.
Definition: town.h:164
TownLayout
Town Layouts.
Definition: town_type.h:82
All possible actions.
Definition: town.h:216
TownCache cache
Container for all cacheable data.
Definition: town.h:58
Town data structure.
Definition: town.h:55
uint8 exclusive_counter
months till the exclusivity expires
Definition: town.h:76
Types/functions related to cargoes.
bool larger_town
if this is a larger town and should grow more quickly
Definition: town.h:100
char * name
Custom town name. If NULL, the town was not renamed and uses the generated name.
Definition: town.h:64
Data structure with cached data of towns.
Definition: town.h:45
TownLayoutByte layout
town specific road layout
Definition: town.h:101
PartOfSubsidyByte part_of_subsidy
Is this town a source/destination of a subsidy?
Definition: town.h:49
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Try to bribe the council.
Definition: town.h:209
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:85
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
Owner
Enum for all companies/owners.
Definition: company_type.h:20
uint32 num_houses
Amount of houses.
Definition: town.h:46
Conditions for town growth are met. Grow according to Town::growth_rate.
Definition: town.h:165
byte road_build_months
fund road reconstruction in action?
Definition: town.h:98
uint16 grow_counter
counter to count when to grow, value is smaller than or equal to growth_rate
Definition: town.h:94
CommandCost CheckforTownRating(DoCommandFlag flags, Town *t, TownRatingCheckType type)
Does the town authority allow the (destructive) action of the current company?
Definition: town_cmd.cpp:3355
Tstorage old_act
Actually transported last month.
Definition: town_type.h:115
void ChangeTownRating(Town *t, int add, int max, DoCommandFlag flags)
Changes town rating of the current company.
Definition: town_cmd.cpp:3318
CompanyMask have_ratings
which companies have a rating
Definition: town.h:73