Fix compiler warnings.
[openttd-joker.git] / src / town.h
blob785f33a5d6b0505e0bb39760dfea0ef264c014a6
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 town.h Base of the town class. */
12 #ifndef TOWN_H
13 #define TOWN_H
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 "openttd.h"
22 #include "table/strings.h"
23 #include "company_func.h"
24 #include <list>
26 template <typename T>
27 struct BuildingCounts {
28 T id_count[NUM_HOUSES];
29 T class_count[HOUSE_CLASS_MAX];
32 typedef TileMatrix<uint32, 4> AcceptanceMatrix;
34 static const uint CUSTOM_TOWN_NUMBER_DIFFICULTY = 4; ///< value for custom town number in difficulty settings
35 static const uint CUSTOM_TOWN_MAX_NUMBER = 5000; ///< this is the maximum number of towns a user can specify in customisation
37 static const uint INVALID_TOWN = 0xFFFF;
39 static const uint TOWN_GROWTH_WINTER = 0xFFFFFFFE; ///< The town only needs this cargo in the winter (any amount)
40 static const uint TOWN_GROWTH_DESERT = 0xFFFFFFFF; ///< The town needs the cargo for growth when on desert (any amount)
41 static const uint16 TOWN_GROW_RATE_CUSTOM = 0x8000; ///< If this mask is applied to Town::growth_rate, the grow_counter will not be calculated by the system (but assumed to be set by scripts)
42 static const uint16 TOWN_GROW_RATE_CUSTOM_NONE = 0xFFFF; ///< Special value for Town::growth_rate to disable town growth.
44 typedef Pool<Town, TownID, 64, 64000> TownPool;
45 extern TownPool _town_pool;
47 /** Data structure with cached data of towns. */
48 struct TownCache {
49 uint32 num_houses; ///< Amount of houses
50 uint32 population; ///< Current population of people
51 ViewportSign sign; ///< Location of name sign, UpdateVirtCoord updates this
52 PartOfSubsidyByte part_of_subsidy; ///< Is this town a source/destination of a subsidy?
53 uint32 squared_town_zone_radius[HZB_END]; ///< UpdateTownRadius updates this given the house count
54 BuildingCounts<uint16> building_counts; ///< The number of each type of building in the town
57 /** Town data structure. */
58 struct Town : TownPool::PoolItem<&_town_pool> {
59 TileIndex xy; ///< town center tile
61 TownCache cache; ///< Container for all cacheable data.
63 /* Town name */
64 uint32 townnamegrfid;
65 uint16 townnametype;
66 uint32 townnameparts;
67 char *name; ///< Custom town name. If NULL, the town was not renamed and uses the generated name.
69 byte flags; ///< See #TownFlags.
71 uint16 noise_reached; ///< level of noise that all the airports are generating
73 CompanyMask statues; ///< which companies have a statue?
75 /* Company ratings. */
76 CompanyMask have_ratings; ///< which companies have a rating
77 uint8 unwanted[MAX_COMPANIES]; ///< how many months companies aren't wanted by towns (bribe)
78 CompanyByte exclusivity; ///< which company has exclusivity
79 uint8 exclusive_counter; ///< months till the exclusivity expires
80 int16 ratings[MAX_COMPANIES]; ///< ratings of each company for this town
81 StringID town_label; ///< Label dependent on _local_company rating.
83 TransportedCargoStat<uint32> supplied[NUM_CARGO]; ///< Cargo statistics about supplied cargo.
84 TransportedCargoStat<uint16> received[NUM_TE]; ///< Cargo statistics about received cargotypes.
85 uint32 goal[NUM_TE]; ///< Amount of cargo required for the town to grow.
87 char *text; ///< General text with additional information.
89 inline byte GetPercentTransported(CargoID cid) const { return this->supplied[cid].old_act * 256 / (this->supplied[cid].old_max + 1); }
91 /* Cargo production and acceptance stats. */
92 uint32 cargo_produced; ///< Bitmap of all cargoes produced by houses in this town.
93 AcceptanceMatrix cargo_accepted; ///< Bitmap of cargoes accepted by houses for each 4*4 map square of the town.
94 uint32 cargo_accepted_total; ///< NOSAVE: Bitmap of all cargoes accepted by houses in this town.
96 int32 time_until_rebuild; ///< date at which we we rebuild a house
98 uint16 grow_counter; ///< counter to count when to grow, value is smaller than or equal to growth_rate
99 uint16 growth_rate; ///< town growth rate
101 byte fund_buildings_months; ///< fund buildings program in action?
102 byte road_build_months; ///< fund road reconstruction in action?
104 bool larger_town; ///< if this is a larger town and should grow more quickly
105 TownLayoutByte layout; ///< town specific road layout
107 std::list<PersistentStorage *> psa_list;
110 * Creates a new town.
111 * @param tile center tile of the town
113 Town(TileIndex tile = INVALID_TILE) : xy(tile) { }
115 /** Destroy the town. */
116 ~Town();
118 void InitializeLayout(TownLayout layout);
120 void UpdateLabel();
123 * Returns the correct town label, based on rating.
125 inline StringID Label() const{
126 if (!(_game_mode == GM_EDITOR) && (_local_company < MAX_COMPANIES)) {
127 return STR_VIEWPORT_TOWN_POP_VERY_POOR_RATING + this->town_label;
128 } else {
129 return _settings_client.gui.population_in_label ? STR_VIEWPORT_TOWN_POP : STR_VIEWPORT_TOWN;
134 * Returns the correct town small label, based on rating.
136 inline StringID SmallLabel() const{
137 if (!(_game_mode == GM_EDITOR) && (_local_company < MAX_COMPANIES)) {
138 return STR_VIEWPORT_TOWN_TINY_VERY_POOR_RATING + this->town_label;
139 } else {
140 return STR_VIEWPORT_TOWN_TINY_WHITE;
145 * Calculate the max town noise.
146 * The value is counted using the population divided by the content of the
147 * entry in town_noise_population corresponding to the town's tolerance.
148 * @return the maximum noise level the town will tolerate.
150 inline uint16 MaxTownNoise() const
152 if (this->cache.population == 0) return 0; // no population? no noise
154 /* 3 is added (the noise of the lowest airport), so the user can at least build a small airfield. */
155 return (this->cache.population / _settings_game.economy.town_noise_population[_settings_game.difficulty.town_council_tolerance]) + 3;
158 void UpdateVirtCoord();
160 static inline Town *GetByTile(TileIndex tile)
162 return Town::Get(GetTownIndex(tile));
165 static Town *GetRandom();
166 static void PostDestructor(size_t index);
169 uint32 GetWorldPopulation();
171 void UpdateAllTownVirtCoords();
172 void ShowTownViewWindow(TownID town);
173 void ExpandTown(Town *t);
176 * Action types that a company must ask permission for to a town authority.
177 * @see CheckforTownRating
179 enum TownRatingCheckType {
180 ROAD_REMOVE = 0, ///< Removal of a road owned by the town.
181 TUNNELBRIDGE_REMOVE = 1, ///< Removal of a tunnel or bridge owned by the towb.
182 TOWN_RATING_CHECK_TYPE_COUNT, ///< Number of town checking action types.
186 * This enum is used in conjunction with town->flags.
187 * IT simply states what bit is used for.
188 * It is pretty unrealistic (IMHO) to only have one church/stadium
189 * per town, NO MATTER the population of it.
190 * And there are 5 more bits available on flags...
192 enum TownFlags {
193 TOWN_IS_GROWING = 0, ///< Conditions for town growth are met. Grow according to Town::growth_rate.
194 TOWN_HAS_CHURCH = 1, ///< There can be only one church by town.
195 TOWN_HAS_STADIUM = 2, ///< There can be only one stadium by town.
198 CommandCost CheckforTownRating(DoCommandFlag flags, Town *t, TownRatingCheckType type);
201 TileIndexDiff GetHouseNorthPart(HouseID &house);
203 Town *CalcClosestTownFromTile(TileIndex tile, uint threshold = UINT_MAX);
205 #define FOR_ALL_TOWNS_FROM(var, start) FOR_ALL_ITEMS_FROM(Town, town_index, var, start)
206 #define FOR_ALL_TOWNS(var) FOR_ALL_TOWNS_FROM(var, 0)
208 void ResetHouses();
210 void ClearTownHouse(Town *t, TileIndex tile);
211 void UpdateTownMaxPass(Town *t);
212 void UpdateTownRadius(Town *t);
213 void UpdateTownCargoes(Town *t);
214 void UpdateTownCargoTotal(Town *t);
215 void UpdateTownCargoBitmap();
216 CommandCost CheckIfAuthorityAllowsNewStation(TileIndex tile, DoCommandFlag flags);
217 Town *ClosestTownFromTile(TileIndex tile, uint threshold);
218 void ChangeTownRating(Town *t, int add, int max, DoCommandFlag flags);
219 HouseZonesBits GetTownRadiusGroup(const Town *t, TileIndex tile);
220 void SetTownRatingTestMode(bool mode);
221 uint GetMaskOfTownActions(int *nump, CompanyID cid, const Town *t);
222 bool GenerateTowns(TownLayout layout);
223 const CargoSpec *FindFirstCargoWithTownEffect(TownEffect effect);
226 /** Town actions of a company. */
227 enum TownActions {
228 TACT_NONE = 0x00, ///< Empty action set.
230 TACT_ADVERTISE_SMALL = 0x01, ///< Small advertising campaign.
231 TACT_ADVERTISE_MEDIUM = 0x02, ///< Medium advertising campaign.
232 TACT_ADVERTISE_LARGE = 0x04, ///< Large advertising campaign.
233 TACT_ROAD_REBUILD = 0x08, ///< Rebuild the roads.
234 TACT_BUILD_STATUE = 0x10, ///< Build a statue.
235 TACT_FUND_BUILDINGS = 0x20, ///< Fund new buildings.
236 TACT_BUY_RIGHTS = 0x40, ///< Buy exclusive transport rights.
237 TACT_BRIBE = 0x80, ///< Try to bribe the council.
239 TACT_COUNT = 8, ///< Number of available town actions.
241 TACT_ADVERTISE = TACT_ADVERTISE_SMALL | TACT_ADVERTISE_MEDIUM | TACT_ADVERTISE_LARGE, ///< All possible advertising actions.
242 TACT_CONSTRUCTION = TACT_ROAD_REBUILD | TACT_BUILD_STATUE | TACT_FUND_BUILDINGS, ///< All possible construction actions.
243 TACT_FUNDS = TACT_BUY_RIGHTS | TACT_BRIBE, ///< All possible funding actions.
244 TACT_ALL = TACT_ADVERTISE | TACT_CONSTRUCTION | TACT_FUNDS, ///< All possible actions.
246 DECLARE_ENUM_AS_BIT_SET(TownActions)
248 extern const byte _town_action_costs[TACT_COUNT];
249 extern TownID _new_town_id;
252 * Set the default name for a depot/waypoint
253 * @tparam T The type/class to make a default name for
254 * @param obj The object/instance we want to find the name for
256 template <class T>
257 void MakeDefaultName(T *obj)
259 /* We only want to set names if it hasn't been set before, or when we're calling from afterload. */
260 assert(obj->name == NULL || obj->town_cn == UINT16_MAX);
262 obj->town = ClosestTownFromTile(obj->xy, UINT_MAX);
264 /* Find first unused number belonging to this town. This can never fail,
265 * as long as there can be at most 65535 waypoints/depots in total.
267 * This does 'n * m' search, but with 32bit 'used' bitmap, it needs at
268 * most 'n * (1 + ceil(m / 32))' steps (n - number of waypoints in pool,
269 * m - number of waypoints near this town).
270 * Usually, it needs only 'n' steps.
272 * If it wasn't using 'used' and 'idx', it would just search for increasing 'next',
273 * but this way it is faster */
275 uint32 used = 0; // bitmap of used waypoint numbers, sliding window with 'next' as base
276 uint32 next = 0; // first number in the bitmap
277 uint32 idx = 0; // index where we will stop
278 uint32 cid = 0; // current index, goes to T::GetPoolSize()-1, then wraps to 0
280 do {
281 T *lobj = T::GetIfValid(cid);
283 /* check only valid waypoints... */
284 if (lobj != NULL && obj != lobj) {
285 /* only objects within the same city and with the same type */
286 if (lobj->town == obj->town && lobj->IsOfType(obj)) {
287 /* if lobj->town_cn < next, uint will overflow to '+inf' */
288 uint i = (uint)lobj->town_cn - next;
290 if (i < 32) {
291 SetBit(used, i); // update bitmap
292 if (i == 0) {
293 /* shift bitmap while the lowest bit is '1';
294 * increase the base of the bitmap too */
295 do {
296 used >>= 1;
297 next++;
298 } while (HasBit(used, 0));
299 /* when we are at 'idx' again at end of the loop and
300 * 'next' hasn't changed, then no object had town_cn == next,
301 * so we can safely use it */
302 idx = cid;
308 cid++;
309 if (cid == T::GetPoolSize()) cid = 0; // wrap to zero...
310 } while (cid != idx);
312 obj->town_cn = (uint16)next; // set index...
315 extern uint32 _town_cargoes_accepted;
317 #endif /* TOWN_H */