OpenTTD Source 20250312-master-gcdcc6b491d
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
24{
25 assert(IsTileType(t, MP_HOUSE) || (IsTileType(t, MP_ROAD) && !IsRoadDepot(t)));
26 return static_cast<TownID>(t.m2());
27}
28
35inline void SetTownIndex(Tile t, TownID index)
36{
37 assert(IsTileType(t, MP_HOUSE) || (IsTileType(t, MP_ROAD) && !IsRoadDepot(t)));
38 t.m2() = index.base();
39}
40
49{
50 assert(IsTileType(t, MP_HOUSE));
51 return GB(t.m8(), 0, 12);
52}
53
64
71inline void SetHouseType(Tile t, HouseID house_id)
72{
73 assert(IsTileType(t, MP_HOUSE));
74 SB(t.m8(), 0, 12, house_id);
75}
76
82inline bool IsHouseProtected(Tile t)
83{
84 assert(IsTileType(t, MP_HOUSE));
85 return HasBit(t.m3(), 5);
86}
87
93inline void SetHouseProtected(Tile t, bool house_protected)
94{
95 assert(IsTileType(t, MP_HOUSE));
96 SB(t.m3(), 5, 1, house_protected ? 1 : 0);
97}
98
105{
106 return HasBit(t.m7(), 0);
107}
108
115inline void SetLiftDestination(Tile t, uint8_t dest)
116{
117 SetBit(t.m7(), 0);
118 SB(t.m7(), 1, 3, dest);
119}
120
126inline uint8_t GetLiftDestination(Tile t)
127{
128 return GB(t.m7(), 1, 3);
129}
130
137inline void HaltLift(Tile t)
138{
139 SB(t.m7(), 0, 4, 0);
140}
141
147inline uint8_t GetLiftPosition(Tile t)
148{
149 return GB(t.m6(), 2, 6);
150}
151
157inline void SetLiftPosition(Tile t, uint8_t pos)
158{
159 SB(t.m6(), 2, 6, pos);
160}
161
167inline bool IsHouseCompleted(Tile t)
168{
169 assert(IsTileType(t, MP_HOUSE));
170 return HasBit(t.m3(), 7);
171}
172
178inline void SetHouseCompleted(Tile t, bool status)
179{
180 assert(IsTileType(t, MP_HOUSE));
181 SB(t.m3(), 7, 1, !!status);
182}
183
206{
207 assert(IsTileType(t, MP_HOUSE));
208 return IsHouseCompleted(t) ? (uint8_t)TOWN_HOUSE_COMPLETED : GB(t.m5(), 3, 2);
209}
210
218{
219 assert(IsTileType(t, MP_HOUSE));
220 return IsHouseCompleted(t) ? 0 : GB(t.m5(), 0, 3);
221}
222
231{
232 assert(IsTileType(t, MP_HOUSE));
233 AB(t.m5(), 0, 5, 1);
234
235 if (GB(t.m5(), 3, 2) == TOWN_HOUSE_COMPLETED) {
236 /* House is now completed.
237 * Store the year of construction as well, for newgrf house purpose */
238 SetHouseCompleted(t, true);
239 }
240}
241
248inline void ResetHouseAge(Tile t)
249{
250 assert(IsTileType(t, MP_HOUSE) && IsHouseCompleted(t));
251 t.m5() = 0;
252}
253
260{
261 assert(IsTileType(t, MP_HOUSE));
262 if (IsHouseCompleted(t) && t.m5() < 0xFF) t.m5()++;
263}
264
272{
273 assert(IsTileType(t, MP_HOUSE));
274 return TimerGameCalendar::Year{IsHouseCompleted(t) ? t.m5() : 0};
275}
276
284inline void SetHouseRandomBits(Tile t, uint8_t random)
285{
286 assert(IsTileType(t, MP_HOUSE));
287 t.m1() = random;
288}
289
297inline uint8_t GetHouseRandomBits(Tile t)
298{
299 assert(IsTileType(t, MP_HOUSE));
300 return t.m1();
301}
302
310inline void SetHouseTriggers(Tile t, uint8_t triggers)
311{
312 assert(IsTileType(t, MP_HOUSE));
313 SB(t.m3(), 0, 5, triggers);
314}
315
323inline uint8_t GetHouseTriggers(Tile t)
324{
325 assert(IsTileType(t, MP_HOUSE));
326 return GB(t.m3(), 0, 5);
327}
328
336{
337 assert(IsTileType(t, MP_HOUSE));
338 return GB(t.m6(), 2, 6);
339}
340
347inline void SetHouseProcessingTime(Tile t, uint8_t time)
348{
349 assert(IsTileType(t, MP_HOUSE));
350 SB(t.m6(), 2, 6, time);
351}
352
359{
360 assert(IsTileType(t, MP_HOUSE));
361 t.m6() -= 1 << 2;
362}
363
375inline void MakeHouseTile(Tile t, TownID tid, uint8_t counter, uint8_t stage, HouseID type, uint8_t random_bits, bool house_protected)
376{
377 assert(IsTileType(t, MP_CLEAR));
378
380 t.m1() = random_bits;
381 t.m2() = tid.base();
382 t.m3() = 0;
383 SetHouseType(t, type);
385 t.m5() = IsHouseCompleted(t) ? 0 : (stage << 3 | counter);
386 SetHouseProtected(t, house_protected);
387 SetAnimationFrame(t, 0);
388 SetHouseProcessingTime(t, HouseSpec::Get(type)->processing_time);
389}
390
391#endif /* TOWN_MAP_H */
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
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.
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
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.
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Wrapper class to abstract away the way the tiles are stored.
Definition map_func.h:25
debug_inline uint16_t & m8()
General purpose.
Definition map_func.h:197
debug_inline uint16_t & m2()
Primarily used for indices to towns, industries and stations.
Definition map_func.h:125
debug_inline uint8_t & m7()
Primarily used for newgrf support.
Definition map_func.h:185
debug_inline uint8_t & m6()
General purpose.
Definition map_func.h:173
debug_inline uint8_t & m3()
General purpose.
Definition map_func.h:137
debug_inline uint8_t & m1()
Primarily used for ownership information.
Definition map_func.h:113
debug_inline uint8_t & m5()
General purpose.
Definition map_func.h:161
definition of HouseSpec and accessors
HouseID GetTranslatedHouseID(HouseID hid)
Do HouseID translation for NewGRFs.
Definition house.h:135
static const uint8_t TOWN_HOUSE_COMPLETED
Simple value that indicates the house has reached the final stage of construction.
Definition house.h:25
uint16_t HouseID
OpenTTD ID of house types.
Definition house_type.h:13
Map accessors for roads.
static debug_inline bool IsRoadDepot(Tile t)
Return whether a tile is a road depot.
Definition road_map.h:106
static HouseSpec * Get(size_t house_id)
Get the spec for a house ID.
Templated helper to make a type-safe 'typedef' representing a single POD value.
void SetTileType(Tile tile, TileType type)
Set the type of a tile.
Definition tile_map.h:131
void SetAnimationFrame(Tile t, uint8_t frame)
Set a new animation frame.
Definition tile_map.h:262
static debug_inline bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
@ MP_ROAD
A tile with road (or tram tracks)
Definition tile_type.h:50
@ MP_CLEAR
A tile without any structures, i.e. grass, rocks, farm fields etc.
Definition tile_type.h:48
@ MP_HOUSE
A house by a town.
Definition tile_type.h:51
Definition of the game-calendar-timer.
void IncrementHouseAge(Tile t)
Increments the age of the house.
Definition town_map.h:259
void HaltLift(Tile t)
Stop the lift of this animated house from moving.
Definition town_map.h:137
HouseID GetHouseType(Tile t)
Get the type of this house, which is an index into the house spec array.
Definition town_map.h:60
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
void ResetHouseAge(Tile t)
Sets the age of the house to zero.
Definition town_map.h:248
void SetHouseRandomBits(Tile t, uint8_t random)
Set the random bits for this house.
Definition town_map.h:284
void MakeHouseTile(Tile t, TownID tid, uint8_t counter, uint8_t stage, HouseID type, uint8_t random_bits, bool house_protected)
Make the tile a house.
Definition town_map.h:375
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:230
uint8_t GetLiftPosition(Tile t)
Get the position of the lift on this animated house.
Definition town_map.h:147
void SetHouseType(Tile t, HouseID house_id)
Set the house type.
Definition town_map.h:71
uint8_t GetHouseRandomBits(Tile t)
Get the random bits for this house.
Definition town_map.h:297
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:115
void SetHouseTriggers(Tile t, uint8_t triggers)
Set the activated triggers bits for this house.
Definition town_map.h:310
TimerGameCalendar::Year GetHouseAge(Tile t)
Get the age of the house.
Definition town_map.h:271
void SetLiftPosition(Tile t, uint8_t pos)
Set the position of the lift on this animated house.
Definition town_map.h:157
uint8_t GetLiftDestination(Tile t)
Get the current destination for this lift.
Definition town_map.h:126
void DecHouseProcessingTime(Tile t)
Decrease the amount of time remaining before the tile loop processes this tile.
Definition town_map.h:358
uint8_t GetHouseBuildingStage(Tile t)
House Construction Scheme.
Definition town_map.h:205
TownID GetTownIndex(Tile t)
Get the index of which town this house/street is attached to.
Definition town_map.h:23
void SetHouseCompleted(Tile t, bool status)
Mark this house as been completed.
Definition town_map.h:178
void SetHouseProtected(Tile t, bool house_protected)
Set a house as protected from removal by towns.
Definition town_map.h:93
uint8_t GetHouseTriggers(Tile t)
Get the already activated triggers bits for this house.
Definition town_map.h:323
void SetHouseProcessingTime(Tile t, uint8_t time)
Set the amount of time remaining before the tile loop processes this tile.
Definition town_map.h:347
void SetTownIndex(Tile t, TownID index)
Set the town index for a road or house tile.
Definition town_map.h:35
bool LiftHasDestination(Tile t)
Check if the lift of this animated house has a destination.
Definition town_map.h:104
bool IsHouseCompleted(Tile t)
Get the completion of this house.
Definition town_map.h:167
bool IsHouseProtected(Tile t)
Check if the house is protected from removal by towns.
Definition town_map.h:82
uint8_t GetHouseProcessingTime(Tile t)
Get the amount of time remaining before the tile loop processes this tile.
Definition town_map.h:335
uint8_t GetHouseConstructionTick(Tile t)
Gets the construction stage of a house.
Definition town_map.h:217