OpenTTD Source 20241224-master-gf74b0cf984
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
23inline TownID GetTownIndex(Tile t)
24{
25 assert(IsTileType(t, MP_HOUSE) || (IsTileType(t, MP_ROAD) && !IsRoadDepot(t)));
26 return 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;
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
83{
84 return HasBit(t.m7(), 0);
85}
86
93inline void SetLiftDestination(Tile t, uint8_t dest)
94{
95 SetBit(t.m7(), 0);
96 SB(t.m7(), 1, 3, dest);
97}
98
104inline uint8_t GetLiftDestination(Tile t)
105{
106 return GB(t.m7(), 1, 3);
107}
108
115inline void HaltLift(Tile t)
116{
117 SB(t.m7(), 0, 4, 0);
118}
119
125inline uint8_t GetLiftPosition(Tile t)
126{
127 return GB(t.m6(), 2, 6);
128}
129
135inline void SetLiftPosition(Tile t, uint8_t pos)
136{
137 SB(t.m6(), 2, 6, pos);
138}
139
145inline bool IsHouseCompleted(Tile t)
146{
147 assert(IsTileType(t, MP_HOUSE));
148 return HasBit(t.m3(), 7);
149}
150
156inline void SetHouseCompleted(Tile t, bool status)
157{
158 assert(IsTileType(t, MP_HOUSE));
159 SB(t.m3(), 7, 1, !!status);
160}
161
184{
185 assert(IsTileType(t, MP_HOUSE));
186 return IsHouseCompleted(t) ? (uint8_t)TOWN_HOUSE_COMPLETED : GB(t.m5(), 3, 2);
187}
188
196{
197 assert(IsTileType(t, MP_HOUSE));
198 return IsHouseCompleted(t) ? 0 : GB(t.m5(), 0, 3);
199}
200
209{
210 assert(IsTileType(t, MP_HOUSE));
211 AB(t.m5(), 0, 5, 1);
212
213 if (GB(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
226inline void ResetHouseAge(Tile t)
227{
228 assert(IsTileType(t, MP_HOUSE) && IsHouseCompleted(t));
229 t.m5() = 0;
230}
231
238{
239 assert(IsTileType(t, MP_HOUSE));
240 if (IsHouseCompleted(t) && t.m5() < 0xFF) t.m5()++;
241}
242
250{
251 assert(IsTileType(t, MP_HOUSE));
252 return IsHouseCompleted(t) ? t.m5() : 0;
253}
254
262inline void SetHouseRandomBits(Tile t, uint8_t random)
263{
264 assert(IsTileType(t, MP_HOUSE));
265 t.m1() = random;
266}
267
275inline uint8_t GetHouseRandomBits(Tile t)
276{
277 assert(IsTileType(t, MP_HOUSE));
278 return t.m1();
279}
280
288inline void SetHouseTriggers(Tile t, uint8_t triggers)
289{
290 assert(IsTileType(t, MP_HOUSE));
291 SB(t.m3(), 0, 5, triggers);
292}
293
301inline uint8_t GetHouseTriggers(Tile t)
302{
303 assert(IsTileType(t, MP_HOUSE));
304 return GB(t.m3(), 0, 5);
305}
306
314{
315 assert(IsTileType(t, MP_HOUSE));
316 return GB(t.m6(), 2, 6);
317}
318
325inline void SetHouseProcessingTime(Tile t, uint8_t time)
326{
327 assert(IsTileType(t, MP_HOUSE));
328 SB(t.m6(), 2, 6, time);
329}
330
337{
338 assert(IsTileType(t, MP_HOUSE));
339 t.m6() -= 1 << 2;
340}
341
352inline void MakeHouseTile(Tile t, TownID tid, uint8_t counter, uint8_t stage, HouseID type, uint8_t random_bits)
353{
354 assert(IsTileType(t, MP_CLEAR));
355
357 t.m1() = random_bits;
358 t.m2() = tid;
359 t.m3() = 0;
360 SetHouseType(t, type);
362 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 */
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:134
static const uint8_t TOWN_HOUSE_COMPLETED
Simple value that indicates the house has reached the final stage of construction.
Definition house.h:23
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:237
void HaltLift(Tile t)
Stop the lift of this animated house from moving.
Definition town_map.h:115
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:226
void SetHouseRandomBits(Tile t, uint8_t random)
Set the random bits for this house.
Definition town_map.h:262
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:208
uint8_t GetLiftPosition(Tile t)
Get the position of the lift on this animated house.
Definition town_map.h:125
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:275
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:93
void SetHouseTriggers(Tile t, uint8_t triggers)
Set the activated triggers bits for this house.
Definition town_map.h:288
TimerGameCalendar::Year GetHouseAge(Tile t)
Get the age of the house.
Definition town_map.h:249
void SetLiftPosition(Tile t, uint8_t pos)
Set the position of the lift on this animated house.
Definition town_map.h:135
uint8_t GetLiftDestination(Tile t)
Get the current destination for this lift.
Definition town_map.h:104
void DecHouseProcessingTime(Tile t)
Decrease the amount of time remaining before the tile loop processes this tile.
Definition town_map.h:336
uint8_t GetHouseBuildingStage(Tile t)
House Construction Scheme.
Definition town_map.h:183
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:156
uint8_t GetHouseTriggers(Tile t)
Get the already activated triggers bits for this house.
Definition town_map.h:301
void SetHouseProcessingTime(Tile t, uint8_t time)
Set the amount of time remaining before the tile loop processes this tile.
Definition town_map.h:325
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:82
bool IsHouseCompleted(Tile t)
Get the completion of this house.
Definition town_map.h:145
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:352
uint8_t GetHouseProcessingTime(Tile t)
Get the amount of time remaining before the tile loop processes this tile.
Definition town_map.h:313
uint8_t GetHouseConstructionTick(Tile t)
Gets the construction stage of a house.
Definition town_map.h:195