OpenTTD Source: src/town_map.h Source File

OpenTTD Source 15.0-beta1

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#include "timer/timer_game_calendar.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
48 inline HouseID GetCleanHouseType(Tile t)
49{
50 assert(IsTileType(t, MP_HOUSE));
51 return GB(t.m8(), 0, 12);
52}
53
60 inline HouseID GetHouseType(Tile t)
61{
62 return GetTranslatedHouseID(GetCleanHouseType(t));
63}
64
71 inline void SetHouseType(Tile t, HouseID house_id)
72{
73 assert(IsTileType(t, MP_HOUSE));
74 SB(t.m8(), 0, 12, house_id);
75}
76
82 inline bool LiftHasDestination(Tile t)
83{
84 return HasBit(t.m7(), 0);
85}
86
93 inline void SetLiftDestination(Tile t, uint8_t dest)
94{
95 SetBit(t.m7(), 0);
96 SB(t.m7(), 1, 3, dest);
97}
98
104 inline uint8_t GetLiftDestination(Tile t)
105{
106 return GB(t.m7(), 1, 3);
107}
108
115 inline void HaltLift(Tile t)
116{
117 SB(t.m7(), 0, 4, 0);
118}
119
125 inline uint8_t GetLiftPosition(Tile t)
126{
127 return GB(t.m6(), 2, 6);
128}
129
135 inline void SetLiftPosition(Tile t, uint8_t pos)
136{
137 SB(t.m6(), 2, 6, pos);
138}
139
145 inline bool IsHouseCompleted(Tile t)
146{
147 assert(IsTileType(t, MP_HOUSE));
148 return HasBit(t.m3(), 7);
149}
150
156 inline void SetHouseCompleted(Tile t, bool status)
157{
158 assert(IsTileType(t, MP_HOUSE));
159 SB(t.m3(), 7, 1, !!status);
160}
161
183 inline uint8_t GetHouseBuildingStage(Tile t)
184{
185 assert(IsTileType(t, MP_HOUSE));
186 return IsHouseCompleted(t) ? (uint8_t)TOWN_HOUSE_COMPLETED : GB(t.m5(), 3, 2);
187}
188
195 inline uint8_t GetHouseConstructionTick(Tile t)
196{
197 assert(IsTileType(t, MP_HOUSE));
198 return IsHouseCompleted(t) ? 0 : GB(t.m5(), 0, 3);
199}
200
208 inline void IncHouseConstructionTick(Tile t)
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
226 inline void ResetHouseAge(Tile t)
227{
228 assert(IsTileType(t, MP_HOUSE) && IsHouseCompleted(t));
229 t.m5() = 0;
230}
231
237 inline void IncrementHouseAge(Tile t)
238{
239 assert(IsTileType(t, MP_HOUSE));
240 if (IsHouseCompleted(t) && t.m5() < 0xFF) t.m5()++;
241}
242
249 inline TimerGameCalendar::Year GetHouseAge(Tile t)
250{
251 assert(IsTileType(t, MP_HOUSE));
252 return IsHouseCompleted(t) ? t.m5() : 0;
253}
254
262 inline void SetHouseRandomBits(Tile t, uint8_t random)
263{
264 assert(IsTileType(t, MP_HOUSE));
265 t.m1() = random;
266}
267
275 inline uint8_t GetHouseRandomBits(Tile t)
276{
277 assert(IsTileType(t, MP_HOUSE));
278 return t.m1();
279}
280
288 inline void SetHouseTriggers(Tile t, uint8_t triggers)
289{
290 assert(IsTileType(t, MP_HOUSE));
291 SB(t.m3(), 0, 5, triggers);
292}
293
301 inline uint8_t GetHouseTriggers(Tile t)
302{
303 assert(IsTileType(t, MP_HOUSE));
304 return GB(t.m3(), 0, 5);
305}
306
313 inline uint8_t GetHouseProcessingTime(Tile t)
314{
315 assert(IsTileType(t, MP_HOUSE));
316 return GB(t.m6(), 2, 6);
317}
318
325 inline void SetHouseProcessingTime(Tile t, uint8_t time)
326{
327 assert(IsTileType(t, MP_HOUSE));
328 SB(t.m6(), 2, 6, time);
329}
330
336 inline void DecHouseProcessingTime(Tile t)
337{
338 assert(IsTileType(t, MP_HOUSE));
339 t.m6() -= 1 << 2;
340}
341
352 inline 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
356 SetTileType(t, MP_HOUSE);
357 t.m1() = random_bits;
358 t.m2() = tid;
359 t.m3() = 0;
360 SetHouseType(t, type);
361 SetHouseCompleted(t, stage == TOWN_HOUSE_COMPLETED);
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 */
HasBit
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition bitmath_func.hpp:103
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
SetBit
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
Definition bitmath_func.hpp:121
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
GB
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.
Definition bitmath_func.hpp:32
Tile
Wrapper class to abstract away the way the tiles are stored.
Definition map_func.h:25
Tile::m8
debug_inline uint16_t & m8()
General purpose.
Definition map_func.h:197
Tile::m2
debug_inline uint16_t & m2()
Primarily used for indices to towns, industries and stations.
Definition map_func.h:125
Tile::m7
debug_inline uint8_t & m7()
Primarily used for newgrf support.
Definition map_func.h:185
Tile::m6
debug_inline uint8_t & m6()
General purpose.
Definition map_func.h:173
Tile::m3
debug_inline uint8_t & m3()
General purpose.
Definition map_func.h:137
Tile::m1
debug_inline uint8_t & m1()
Primarily used for ownership information.
Definition map_func.h:113
Tile::m5
debug_inline uint8_t & m5()
General purpose.
Definition map_func.h:161
house.h
definition of HouseSpec and accessors
GetTranslatedHouseID
HouseID GetTranslatedHouseID(HouseID hid)
Do HouseID translation for NewGRFs.
Definition house.h:134
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
HouseID
uint16_t HouseID
OpenTTD ID of house types.
Definition house_type.h:13
road_map.h
Map accessors for roads.
IsRoadDepot
static debug_inline bool IsRoadDepot(Tile t)
Return whether a tile is a road depot.
Definition road_map.h:106
HouseSpec::Get
static HouseSpec * Get(size_t house_id)
Get the spec for a house ID.
Definition newgrf_house.cpp:69
StrongType::Typedef
Templated helper to make a type-safe 'typedef' representing a single POD value.
Definition strong_typedef_type.hpp:150
SetTileType
void SetTileType(Tile tile, TileType type)
Set the type of a tile.
Definition tile_map.h:131
SetAnimationFrame
void SetAnimationFrame(Tile t, uint8_t frame)
Set a new animation frame.
Definition tile_map.h:262
IsTileType
static debug_inline bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
MP_ROAD
@ MP_ROAD
A tile with road (or tram tracks)
Definition tile_type.h:50
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
timer_game_calendar.h
Definition of the game-calendar-timer.
IncrementHouseAge
void IncrementHouseAge(Tile t)
Increments the age of the house.
Definition town_map.h:237
HaltLift
void HaltLift(Tile t)
Stop the lift of this animated house from moving.
Definition town_map.h:115
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
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
ResetHouseAge
void ResetHouseAge(Tile t)
Sets the age of the house to zero.
Definition town_map.h:226
SetHouseRandomBits
void SetHouseRandomBits(Tile t, uint8_t random)
Set the random bits for this house.
Definition town_map.h:262
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:208
GetLiftPosition
uint8_t GetLiftPosition(Tile t)
Get the position of the lift on this animated house.
Definition town_map.h:125
SetHouseType
void SetHouseType(Tile t, HouseID house_id)
Set the house type.
Definition town_map.h:71
GetHouseRandomBits
uint8_t GetHouseRandomBits(Tile t)
Get the random bits for this house.
Definition town_map.h:275
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:93
SetHouseTriggers
void SetHouseTriggers(Tile t, uint8_t triggers)
Set the activated triggers bits for this house.
Definition town_map.h:288
GetHouseAge
TimerGameCalendar::Year GetHouseAge(Tile t)
Get the age of the house.
Definition town_map.h:249
SetLiftPosition
void SetLiftPosition(Tile t, uint8_t pos)
Set the position of the lift on this animated house.
Definition town_map.h:135
GetLiftDestination
uint8_t GetLiftDestination(Tile t)
Get the current destination for this lift.
Definition town_map.h:104
DecHouseProcessingTime
void DecHouseProcessingTime(Tile t)
Decrease the amount of time remaining before the tile loop processes this tile.
Definition town_map.h:336
GetHouseBuildingStage
uint8_t GetHouseBuildingStage(Tile t)
House Construction Scheme.
Definition town_map.h:183
GetTownIndex
TownID GetTownIndex(Tile t)
Get the index of which town this house/street is attached to.
Definition town_map.h:23
SetHouseCompleted
void SetHouseCompleted(Tile t, bool status)
Mark this house as been completed.
Definition town_map.h:156
GetHouseTriggers
uint8_t GetHouseTriggers(Tile t)
Get the already activated triggers bits for this house.
Definition town_map.h:301
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:325
SetTownIndex
void SetTownIndex(Tile t, TownID index)
Set the town index for a road or house tile.
Definition town_map.h:35
LiftHasDestination
bool LiftHasDestination(Tile t)
Check if the lift of this animated house has a destination.
Definition town_map.h:82
IsHouseCompleted
bool IsHouseCompleted(Tile t)
Get the completion of this house.
Definition town_map.h:145
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:352
GetHouseProcessingTime
uint8_t GetHouseProcessingTime(Tile t)
Get the amount of time remaining before the tile loop processes this tile.
Definition town_map.h:313
GetHouseConstructionTick
uint8_t GetHouseConstructionTick(Tile t)
Gets the construction stage of a house.
Definition town_map.h:195