OpenTTD Source  20240917-master-g9ab0a47812
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"
16 
23 inline TownID GetTownIndex(Tile t)
24 {
25  assert(IsTileType(t, MP_HOUSE) || (IsTileType(t, MP_ROAD) && !IsRoadDepot(t)));
26  return t.m2();
27 }
28 
35 inline void SetTownIndex(Tile t, TownID index)
36 {
37  assert(IsTileType(t, MP_HOUSE) || (IsTileType(t, MP_ROAD) && !IsRoadDepot(t)));
38  t.m2() = index;
39 }
40 
49 {
50  assert(IsTileType(t, MP_HOUSE));
51  return t.m4() | (GB(t.m3(), 6, 1) << 8);
52 }
53 
61 {
63 }
64 
71 inline void SetHouseType(Tile t, HouseID house_id)
72 {
73  assert(IsTileType(t, MP_HOUSE));
74  t.m4() = GB(house_id, 0, 8);
75  SB(t.m3(), 6, 1, GB(house_id, 8, 1));
76 }
77 
83 inline bool LiftHasDestination(Tile t)
84 {
85  return HasBit(t.m7(), 0);
86 }
87 
94 inline void SetLiftDestination(Tile t, uint8_t dest)
95 {
96  SetBit(t.m7(), 0);
97  SB(t.m7(), 1, 3, dest);
98 }
99 
105 inline uint8_t GetLiftDestination(Tile t)
106 {
107  return GB(t.m7(), 1, 3);
108 }
109 
116 inline void HaltLift(Tile t)
117 {
118  SB(t.m7(), 0, 4, 0);
119 }
120 
126 inline uint8_t GetLiftPosition(Tile t)
127 {
128  return GB(t.m6(), 2, 6);
129 }
130 
136 inline void SetLiftPosition(Tile t, uint8_t pos)
137 {
138  SB(t.m6(), 2, 6, pos);
139 }
140 
146 inline bool IsHouseCompleted(Tile t)
147 {
148  assert(IsTileType(t, MP_HOUSE));
149  return HasBit(t.m3(), 7);
150 }
151 
157 inline void SetHouseCompleted(Tile t, bool status)
158 {
159  assert(IsTileType(t, MP_HOUSE));
160  SB(t.m3(), 7, 1, !!status);
161 }
162 
184 inline uint8_t GetHouseBuildingStage(Tile t)
185 {
186  assert(IsTileType(t, MP_HOUSE));
187  return IsHouseCompleted(t) ? (uint8_t)TOWN_HOUSE_COMPLETED : GB(t.m5(), 3, 2);
188 }
189 
197 {
198  assert(IsTileType(t, MP_HOUSE));
199  return IsHouseCompleted(t) ? 0 : GB(t.m5(), 0, 3);
200 }
201 
210 {
211  assert(IsTileType(t, MP_HOUSE));
212  AB(t.m5(), 0, 5, 1);
213 
214  if (GB(t.m5(), 3, 2) == TOWN_HOUSE_COMPLETED) {
215  /* House is now completed.
216  * Store the year of construction as well, for newgrf house purpose */
217  SetHouseCompleted(t, true);
218  }
219 }
220 
227 inline void ResetHouseAge(Tile t)
228 {
229  assert(IsTileType(t, MP_HOUSE) && IsHouseCompleted(t));
230  t.m5() = 0;
231 }
232 
238 inline void IncrementHouseAge(Tile t)
239 {
240  assert(IsTileType(t, MP_HOUSE));
241  if (IsHouseCompleted(t) && t.m5() < 0xFF) t.m5()++;
242 }
243 
251 {
252  assert(IsTileType(t, MP_HOUSE));
253  return IsHouseCompleted(t) ? t.m5() : 0;
254 }
255 
263 inline void SetHouseRandomBits(Tile t, uint8_t random)
264 {
265  assert(IsTileType(t, MP_HOUSE));
266  t.m1() = random;
267 }
268 
276 inline uint8_t GetHouseRandomBits(Tile t)
277 {
278  assert(IsTileType(t, MP_HOUSE));
279  return t.m1();
280 }
281 
289 inline void SetHouseTriggers(Tile t, uint8_t triggers)
290 {
291  assert(IsTileType(t, MP_HOUSE));
292  SB(t.m3(), 0, 5, triggers);
293 }
294 
302 inline uint8_t GetHouseTriggers(Tile t)
303 {
304  assert(IsTileType(t, MP_HOUSE));
305  return GB(t.m3(), 0, 5);
306 }
307 
314 inline uint8_t GetHouseProcessingTime(Tile t)
315 {
316  assert(IsTileType(t, MP_HOUSE));
317  return GB(t.m6(), 2, 6);
318 }
319 
326 inline void SetHouseProcessingTime(Tile t, uint8_t time)
327 {
328  assert(IsTileType(t, MP_HOUSE));
329  SB(t.m6(), 2, 6, time);
330 }
331 
338 {
339  assert(IsTileType(t, MP_HOUSE));
340  t.m6() -= 1 << 2;
341 }
342 
353 inline void MakeHouseTile(Tile t, TownID tid, uint8_t counter, uint8_t stage, HouseID type, uint8_t random_bits)
354 {
355  assert(IsTileType(t, MP_CLEAR));
356 
357  SetTileType(t, MP_HOUSE);
358  t.m1() = random_bits;
359  t.m2() = tid;
360  t.m3() = 0;
361  SetHouseType(t, type);
363  t.m5() = IsHouseCompleted(t) ? 0 : (stage << 3 | counter);
364  SetAnimationFrame(t, 0);
365  SetHouseProcessingTime(t, HouseSpec::Get(type)->processing_time);
366 }
367 
368 #endif /* TOWN_MAP_H */
GetHouseRandomBits
uint8_t GetHouseRandomBits(Tile t)
Get the random bits for this house.
Definition: town_map.h:276
MP_CLEAR
@ MP_CLEAR
A tile without any structures, i.e. grass, rocks, farm fields etc.
Definition: tile_type.h:48
MP_HOUSE
@ MP_HOUSE
A house by a town.
Definition: tile_type.h:51
ResetHouseAge
void ResetHouseAge(Tile t)
Sets the age of the house to zero.
Definition: town_map.h:227
SetBit
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
Definition: bitmath_func.hpp:121
Tile::m5
debug_inline uint8_t & m5()
General purpose.
Definition: map_func.h:161
GetCleanHouseType
HouseID GetCleanHouseType(Tile 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:48
Tile::m3
debug_inline uint8_t & m3()
General purpose.
Definition: map_func.h:137
SetHouseRandomBits
void SetHouseRandomBits(Tile t, uint8_t random)
Set the random bits for this house.
Definition: town_map.h:263
timer_game_calendar.h
GetHouseBuildingStage
uint8_t GetHouseBuildingStage(Tile t)
House Construction Scheme.
Definition: town_map.h:184
GB
constexpr static debug_inline uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
IncHouseConstructionTick
void IncHouseConstructionTick(Tile t)
Sets the increment stage of a house It is working with the whole counter + stage 5 bits,...
Definition: town_map.h:209
Tile
Wrapper class to abstract away the way the tiles are stored.
Definition: map_func.h:25
SetLiftDestination
void SetLiftDestination(Tile t, uint8_t dest)
Set the new destination of the lift for this animated house, and activate the LiftHasDestination bit.
Definition: town_map.h:94
StrongType::Typedef
Templated helper to make a type-safe 'typedef' representing a single POD value.
Definition: strong_typedef_type.hpp:150
Tile::m4
debug_inline uint8_t & m4()
General purpose.
Definition: map_func.h:149
SetHouseType
void SetHouseType(Tile t, HouseID house_id)
Set the house type.
Definition: town_map.h:71
MP_ROAD
@ MP_ROAD
A tile with road (or tram tracks)
Definition: tile_type.h:50
LiftHasDestination
bool LiftHasDestination(Tile t)
Check if the lift of this animated house has a destination.
Definition: town_map.h:83
Tile::m7
debug_inline uint8_t & m7()
Primarily used for newgrf support.
Definition: map_func.h:185
GetHouseAge
TimerGameCalendar::Year GetHouseAge(Tile t)
Get the age of the house.
Definition: town_map.h:250
GetHouseProcessingTime
uint8_t GetHouseProcessingTime(Tile t)
Get the amount of time remaining before the tile loop processes this tile.
Definition: town_map.h:314
Tile::m2
debug_inline uint16_t & m2()
Primarily used for indices to towns, industries and stations.
Definition: map_func.h:125
AB
constexpr T AB(T &x, const uint8_t s, const uint8_t n, const U i)
Add i to n bits of x starting at bit s.
Definition: bitmath_func.hpp:83
SetHouseProcessingTime
void SetHouseProcessingTime(Tile t, uint8_t time)
Set the amount of time remaining before the tile loop processes this tile.
Definition: town_map.h:326
HaltLift
void HaltLift(Tile t)
Stop the lift of this animated house from moving.
Definition: town_map.h:116
Tile::m6
debug_inline uint8_t & m6()
General purpose.
Definition: map_func.h:173
GetHouseType
HouseID GetHouseType(Tile t)
Get the type of this house, which is an index into the house spec array.
Definition: town_map.h:60
house.h
TOWN_HOUSE_COMPLETED
static const uint8_t TOWN_HOUSE_COMPLETED
Simple value that indicates the house has reached the final stage of construction.
Definition: house.h:23
road_map.h
IncrementHouseAge
void IncrementHouseAge(Tile t)
Increments the age of the house.
Definition: town_map.h:238
SetHouseCompleted
void SetHouseCompleted(Tile t, bool status)
Mark this house as been completed.
Definition: town_map.h:157
HouseSpec::Get
static HouseSpec * Get(size_t house_id)
Get the spec for a house ID.
Definition: newgrf_house.cpp:69
SetHouseTriggers
void SetHouseTriggers(Tile t, uint8_t triggers)
Set the activated triggers bits for this house.
Definition: town_map.h:289
SetTileType
void SetTileType(Tile tile, TileType type)
Set the type of a tile.
Definition: tile_map.h:131
IsHouseCompleted
bool IsHouseCompleted(Tile t)
Get the completion of this house.
Definition: town_map.h:146
HouseID
uint16_t HouseID
OpenTTD ID of house types.
Definition: house_type.h:13
SetTownIndex
void SetTownIndex(Tile t, TownID index)
Set the town index for a road or house tile.
Definition: town_map.h:35
SetLiftPosition
void SetLiftPosition(Tile t, uint8_t pos)
Set the position of the lift on this animated house.
Definition: town_map.h:136
GetLiftPosition
uint8_t GetLiftPosition(Tile t)
Get the position of the lift on this animated house.
Definition: town_map.h:126
SB
constexpr T SB(T &x, const uint8_t s, const uint8_t n, const U d)
Set n bits in x starting at bit s to d.
Definition: bitmath_func.hpp:58
GetHouseTriggers
uint8_t GetHouseTriggers(Tile t)
Get the already activated triggers bits for this house.
Definition: town_map.h:302
IsTileType
static debug_inline bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
SetAnimationFrame
void SetAnimationFrame(Tile t, uint8_t frame)
Set a new animation frame.
Definition: tile_map.h:262
DecHouseProcessingTime
void DecHouseProcessingTime(Tile t)
Decrease the amount of time remaining before the tile loop processes this tile.
Definition: town_map.h:337
GetLiftDestination
uint8_t GetLiftDestination(Tile t)
Get the current destination for this lift.
Definition: town_map.h:105
MakeHouseTile
void MakeHouseTile(Tile t, TownID tid, uint8_t counter, uint8_t stage, HouseID type, uint8_t random_bits)
Make the tile a house.
Definition: town_map.h:353
IsRoadDepot
static debug_inline bool IsRoadDepot(Tile t)
Return whether a tile is a road depot.
Definition: road_map.h:106
GetTranslatedHouseID
HouseID GetTranslatedHouseID(HouseID hid)
Do HouseID translation for NewGRFs.
Definition: house.h:133
GetHouseConstructionTick
uint8_t GetHouseConstructionTick(Tile t)
Gets the construction stage of a house.
Definition: town_map.h:196
Tile::m1
debug_inline uint8_t & m1()
Primarily used for ownership information.
Definition: map_func.h:113
GetTownIndex
TownID GetTownIndex(Tile t)
Get the index of which town this house/street is attached to.
Definition: town_map.h:23
HasBit
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103