OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

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>
24 struct BuildingCounts {
25 T id_count[NUM_HOUSES];
26 T class_count[HOUSE_CLASS_MAX];
27 };
28
29 typedef TileMatrix<uint32, 4> AcceptanceMatrix;
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
41 typedef Pool<Town, TownID, 64, 64000> TownPool;
42 extern TownPool _town_pool;
43
45 struct TownCache {
46 uint32 num_houses;
47 uint32 population;
48 ViewportSign sign;
49 PartOfSubsidyByte part_of_subsidy;
50 uint32 squared_town_zone_radius[HZB_END];
51 BuildingCounts<uint16> building_counts;
52 };
53
55 struct Town : TownPool::PoolItem<&_town_pool> {
56 TileIndex xy;
57
58 TownCache cache;
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];
75 CompanyByte exclusivity;
76 uint8 exclusive_counter;
77 int16 ratings[MAX_COMPANIES];
78
79 TransportedCargoStat<uint32> supplied[NUM_CARGO];
80 TransportedCargoStat<uint16> received[NUM_TE];
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;
89 AcceptanceMatrix cargo_accepted;
90 uint32 cargo_accepted_total;
91
92 uint16 time_until_rebuild;
93
94 uint16 grow_counter;
95 uint16 growth_rate;
96
97 byte fund_buildings_months;
98 byte road_build_months;
99
100 bool larger_town;
101 TownLayoutByte layout;
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. */
127 return (this->cache.population / _settings_game.economy.town_noise_population[_settings_game.difficulty.town_council_tolerance]) + 3;
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
143 void UpdateAllTownVirtCoords();
144 void ShowTownViewWindow(TownID town);
145 void ExpandTown(Town *t);
146
151 enum TownRatingCheckType {
152 ROAD_REMOVE = 0,
153 TUNNELBRIDGE_REMOVE = 1,
154 TOWN_RATING_CHECK_TYPE_COUNT,
155 };
156
164 enum TownFlags {
165 TOWN_IS_GROWING = 0,
166 TOWN_HAS_CHURCH = 1,
167 TOWN_HAS_STADIUM = 2,
168 };
169
170 CommandCost CheckforTownRating(DoCommandFlag flags, Town *t, TownRatingCheckType type);
171
172
173 TileIndexDiff GetHouseNorthPart(HouseID &house);
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();
188 CommandCost CheckIfAuthorityAllowsNewStation(TileIndex tile, DoCommandFlag flags);
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);
195 const CargoSpec *FindFirstCargoWithTownEffect(TownEffect effect);
196
197
199 enum TownActions {
200 TACT_NONE = 0x00,
201
202 TACT_ADVERTISE_SMALL = 0x01,
203 TACT_ADVERTISE_MEDIUM = 0x02,
204 TACT_ADVERTISE_LARGE = 0x04,
205 TACT_ROAD_REBUILD = 0x08,
206 TACT_BUILD_STATUE = 0x10,
207 TACT_FUND_BUILDINGS = 0x20,
208 TACT_BUY_RIGHTS = 0x40,
209 TACT_BRIBE = 0x80,
210
211 TACT_COUNT = 8,
212
213 TACT_ADVERTISE = TACT_ADVERTISE_SMALL | TACT_ADVERTISE_MEDIUM | TACT_ADVERTISE_LARGE,
214 TACT_CONSTRUCTION = TACT_ROAD_REBUILD | TACT_BUILD_STATUE | TACT_FUND_BUILDINGS,
215 TACT_FUNDS = TACT_BUY_RIGHTS | TACT_BRIBE,
216 TACT_ALL = TACT_ADVERTISE | TACT_CONSTRUCTION | TACT_FUNDS,
217 };
218 DECLARE_ENUM_AS_BIT_SET(TownActions)
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 */