OpenTTD Source  1.11.2
town_map.h
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #ifndef TOWN_MAP_H
11 #define TOWN_MAP_H
12 
13 #include "road_map.h"
14 #include "house.h"
15 
22 static inline TownID GetTownIndex(TileIndex t)
23 {
24  assert(IsTileType(t, MP_HOUSE) || (IsTileType(t, MP_ROAD) && !IsRoadDepot(t)));
25  return _m[t].m2;
26 }
27 
34 static inline void SetTownIndex(TileIndex t, TownID index)
35 {
36  assert(IsTileType(t, MP_HOUSE) || (IsTileType(t, MP_ROAD) && !IsRoadDepot(t)));
37  _m[t].m2 = index;
38 }
39 
48 {
49  assert(IsTileType(t, MP_HOUSE));
50  return _m[t].m4 | (GB(_m[t].m3, 6, 1) << 8);
51 }
52 
59 static inline HouseID GetHouseType(TileIndex t)
60 {
62 }
63 
70 static inline void SetHouseType(TileIndex t, HouseID house_id)
71 {
72  assert(IsTileType(t, MP_HOUSE));
73  _m[t].m4 = GB(house_id, 0, 8);
74  SB(_m[t].m3, 6, 1, GB(house_id, 8, 1));
75 }
76 
82 static inline bool LiftHasDestination(TileIndex t)
83 {
84  return HasBit(_me[t].m7, 0);
85 }
86 
93 static inline void SetLiftDestination(TileIndex t, byte dest)
94 {
95  SetBit(_me[t].m7, 0);
96  SB(_me[t].m7, 1, 3, dest);
97 }
98 
104 static inline byte GetLiftDestination(TileIndex t)
105 {
106  return GB(_me[t].m7, 1, 3);
107 }
108 
115 static inline void HaltLift(TileIndex t)
116 {
117  SB(_me[t].m7, 0, 4, 0);
118 }
119 
125 static inline byte GetLiftPosition(TileIndex t)
126 {
127  return GB(_me[t].m6, 2, 6);
128 }
129 
135 static inline void SetLiftPosition(TileIndex t, byte pos)
136 {
137  SB(_me[t].m6, 2, 6, pos);
138 }
139 
145 static inline bool IsHouseCompleted(TileIndex t)
146 {
147  assert(IsTileType(t, MP_HOUSE));
148  return HasBit(_m[t].m3, 7);
149 }
150 
156 static inline void SetHouseCompleted(TileIndex t, bool status)
157 {
158  assert(IsTileType(t, MP_HOUSE));
159  SB(_m[t].m3, 7, 1, !!status);
160 }
161 
183 static inline byte GetHouseBuildingStage(TileIndex t)
184 {
185  assert(IsTileType(t, MP_HOUSE));
186  return IsHouseCompleted(t) ? (byte)TOWN_HOUSE_COMPLETED : GB(_m[t].m5, 3, 2);
187 }
188 
195 static inline byte GetHouseConstructionTick(TileIndex t)
196 {
197  assert(IsTileType(t, MP_HOUSE));
198  return IsHouseCompleted(t) ? 0 : GB(_m[t].m5, 0, 3);
199 }
200 
208 static inline void IncHouseConstructionTick(TileIndex t)
209 {
210  assert(IsTileType(t, MP_HOUSE));
211  AB(_m[t].m5, 0, 5, 1);
212 
213  if (GB(_m[t].m5, 3, 2) == TOWN_HOUSE_COMPLETED) {
214  /* House is now completed.
215  * Store the year of construction as well, for newgrf house purpose */
216  SetHouseCompleted(t, true);
217  }
218 }
219 
226 static inline void ResetHouseAge(TileIndex t)
227 {
228  assert(IsTileType(t, MP_HOUSE) && IsHouseCompleted(t));
229  _m[t].m5 = 0;
230 }
231 
237 static inline void IncrementHouseAge(TileIndex t)
238 {
239  assert(IsTileType(t, MP_HOUSE));
240  if (IsHouseCompleted(t) && _m[t].m5 < 0xFF) _m[t].m5++;
241 }
242 
249 static inline Year GetHouseAge(TileIndex t)
250 {
251  assert(IsTileType(t, MP_HOUSE));
252  return IsHouseCompleted(t) ? _m[t].m5 : 0;
253 }
254 
262 static inline void SetHouseRandomBits(TileIndex t, byte random)
263 {
264  assert(IsTileType(t, MP_HOUSE));
265  _m[t].m1 = random;
266 }
267 
275 static inline byte GetHouseRandomBits(TileIndex t)
276 {
277  assert(IsTileType(t, MP_HOUSE));
278  return _m[t].m1;
279 }
280 
288 static inline void SetHouseTriggers(TileIndex t, byte triggers)
289 {
290  assert(IsTileType(t, MP_HOUSE));
291  SB(_m[t].m3, 0, 5, triggers);
292 }
293 
301 static inline byte GetHouseTriggers(TileIndex t)
302 {
303  assert(IsTileType(t, MP_HOUSE));
304  return GB(_m[t].m3, 0, 5);
305 }
306 
313 static inline byte GetHouseProcessingTime(TileIndex t)
314 {
315  assert(IsTileType(t, MP_HOUSE));
316  return GB(_me[t].m6, 2, 6);
317 }
318 
325 static inline void SetHouseProcessingTime(TileIndex t, byte time)
326 {
327  assert(IsTileType(t, MP_HOUSE));
328  SB(_me[t].m6, 2, 6, time);
329 }
330 
336 static inline void DecHouseProcessingTime(TileIndex t)
337 {
338  assert(IsTileType(t, MP_HOUSE));
339  _me[t].m6 -= 1 << 2;
340 }
341 
352 static inline void MakeHouseTile(TileIndex t, TownID tid, byte counter, byte stage, HouseID type, byte random_bits)
353 {
354  assert(IsTileType(t, MP_CLEAR));
355 
356  SetTileType(t, MP_HOUSE);
357  _m[t].m1 = random_bits;
358  _m[t].m2 = tid;
359  _m[t].m3 = 0;
360  SetHouseType(t, type);
362  _m[t].m5 = IsHouseCompleted(t) ? 0 : (stage << 3 | counter);
363  SetAnimationFrame(t, 0);
364  SetHouseProcessingTime(t, HouseSpec::Get(type)->processing_time);
365 }
366 
367 #endif /* TOWN_MAP_H */
MP_CLEAR
@ MP_CLEAR
A tile without any structures, i.e. grass, rocks, farm fields etc.
Definition: tile_type.h:46
MP_HOUSE
@ MP_HOUSE
A house by a town.
Definition: tile_type.h:49
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:83
GetHouseAge
static Year GetHouseAge(TileIndex t)
Get the age of the house.
Definition: town_map.h:249
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
SetTileType
static void SetTileType(TileIndex tile, TileType type)
Set the type of a tile.
Definition: tile_map.h:131
IsHouseCompleted
static bool IsHouseCompleted(TileIndex t)
Get the completion of this house.
Definition: town_map.h:145
GetHouseProcessingTime
static byte GetHouseProcessingTime(TileIndex t)
Get the amount of time remaining before the tile loop processes this tile.
Definition: town_map.h:313
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
_me
TileExtended * _me
Extended Tiles of the map.
Definition: map.cpp:31
Tile::m2
uint16 m2
Primarily used for indices to towns, industries and stations.
Definition: map_type.h:20
Year
int32 Year
Type for the year, note: 0 based, i.e. starts at the year 0.
Definition: date_type.h:18
ResetHouseAge
static void ResetHouseAge(TileIndex t)
Sets the age of the house to zero.
Definition: town_map.h:226
AB
static T AB(T &x, const uint8 s, const uint8 n, const U i)
Add i to n bits of x starting at bit s.
Definition: bitmath_func.hpp:83
Tile::m1
byte m1
Primarily used for ownership information.
Definition: map_type.h:21
MP_ROAD
@ MP_ROAD
A tile with road (or tram tracks)
Definition: tile_type.h:48
GetTownIndex
static TownID GetTownIndex(TileIndex t)
Get the index of which town this house/street is attached to.
Definition: town_map.h:22
IncHouseConstructionTick
static void IncHouseConstructionTick(TileIndex t)
Sets the increment stage of a house It is working with the whole counter + stage 5 bits,...
Definition: town_map.h:208
GetHouseType
static HouseID GetHouseType(TileIndex t)
Get the type of this house, which is an index into the house spec array.
Definition: town_map.h:59
GetLiftPosition
static byte GetLiftPosition(TileIndex t)
Get the position of the lift on this animated house.
Definition: town_map.h:125
TileExtended::m6
byte m6
General purpose.
Definition: map_type.h:34
SB
static T SB(T &x, const uint8 s, const uint8 n, const U d)
Set n bits in x starting at bit s to d.
Definition: bitmath_func.hpp:58
GetLiftDestination
static byte GetLiftDestination(TileIndex t)
Get the current destination for this lift.
Definition: town_map.h:104
HaltLift
static void HaltLift(TileIndex t)
Stop the lift of this animated house from moving.
Definition: town_map.h:115
SetLiftPosition
static void SetLiftPosition(TileIndex t, byte pos)
Set the position of the lift on this animated house.
Definition: town_map.h:135
IsRoadDepot
static bool IsRoadDepot(TileIndex t)
Return whether a tile is a road depot.
Definition: road_map.h:105
GetHouseConstructionTick
static byte GetHouseConstructionTick(TileIndex t)
Gets the construction stage of a house.
Definition: town_map.h:195
house.h
IsTileType
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
road_map.h
SetLiftDestination
static void SetLiftDestination(TileIndex t, byte dest)
Set the new destination of the lift for this animated house, and activate the LiftHasDestination bit.
Definition: town_map.h:93
HouseID
uint16 HouseID
OpenTTD ID of house types.
Definition: house_type.h:13
Tile::m5
byte m5
General purpose.
Definition: map_type.h:24
LiftHasDestination
static bool LiftHasDestination(TileIndex t)
Check if the lift of this animated house has a destination.
Definition: town_map.h:82
SetHouseTriggers
static void SetHouseTriggers(TileIndex t, byte triggers)
Set the activated triggers bits for this house.
Definition: town_map.h:288
GetHouseBuildingStage
static byte GetHouseBuildingStage(TileIndex t)
House Construction Scheme.
Definition: town_map.h:183
SetHouseCompleted
static void SetHouseCompleted(TileIndex t, bool status)
Mark this house as been completed.
Definition: town_map.h:156
SetTownIndex
static void SetTownIndex(TileIndex t, TownID index)
Set the town index for a road or house tile.
Definition: town_map.h:34
GetTranslatedHouseID
static HouseID GetTranslatedHouseID(HouseID hid)
Do HouseID translation for NewGRFs.
Definition: house.h:140
SetBit
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
Definition: bitmath_func.hpp:121
SetHouseRandomBits
static void SetHouseRandomBits(TileIndex t, byte random)
Set the random bits for this house.
Definition: town_map.h:262
GetCleanHouseType
static HouseID GetCleanHouseType(TileIndex t)
Get the type of this house, which is an index into the house spec array without doing any NewGRF rela...
Definition: town_map.h:47
DecHouseProcessingTime
static void DecHouseProcessingTime(TileIndex t)
Decrease the amount of time remaining before the tile loop processes this tile.
Definition: town_map.h:336
SetHouseProcessingTime
static void SetHouseProcessingTime(TileIndex t, byte time)
Set the amount of time remaining before the tile loop processes this tile.
Definition: town_map.h:325
Tile::m3
byte m3
General purpose.
Definition: map_type.h:22
GetHouseTriggers
static byte GetHouseTriggers(TileIndex t)
Get the already activated triggers bits for this house.
Definition: town_map.h:301
SetAnimationFrame
static void SetAnimationFrame(TileIndex t, byte frame)
Set a new animation frame.
Definition: tile_map.h:262
TOWN_HOUSE_COMPLETED
static const byte TOWN_HOUSE_COMPLETED
Simple value that indicates the house has reached the final stage of construction.
Definition: house.h:23
SetHouseType
static void SetHouseType(TileIndex t, HouseID house_id)
Set the house type.
Definition: town_map.h:70
IncrementHouseAge
static void IncrementHouseAge(TileIndex t)
Increments the age of the house.
Definition: town_map.h:237
Tile::m4
byte m4
General purpose.
Definition: map_type.h:23
_m
Tile * _m
Tiles of the map.
Definition: map.cpp:30
MakeHouseTile
static void MakeHouseTile(TileIndex t, TownID tid, byte counter, byte stage, HouseID type, byte random_bits)
Make the tile a house.
Definition: town_map.h:352
GetHouseRandomBits
static byte GetHouseRandomBits(TileIndex t)
Get the random bits for this house.
Definition: town_map.h:275