OpenTTD Source: src/town_map.h Source File

OpenTTD Source 15.0-RC2

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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 static_cast<TownID>(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.base();
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 IsHouseProtected(Tile t)
83{
84 assert(IsTileType(t, MP_HOUSE));
85 return HasBit(t.m3(), 5);
86}
87
93 inline 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
104 inline bool LiftHasDestination(Tile t)
105{
106 return HasBit(t.m7(), 0);
107}
108
115 inline void SetLiftDestination(Tile t, uint8_t dest)
116{
117 SetBit(t.m7(), 0);
118 SB(t.m7(), 1, 3, dest);
119}
120
126 inline uint8_t GetLiftDestination(Tile t)
127{
128 return GB(t.m7(), 1, 3);
129}
130
137 inline void HaltLift(Tile t)
138{
139 SB(t.m7(), 0, 4, 0);
140}
141
147 inline uint8_t GetLiftPosition(Tile t)
148{
149 return GB(t.m6(), 2, 6);
150}
151
157 inline void SetLiftPosition(Tile t, uint8_t pos)
158{
159 SB(t.m6(), 2, 6, pos);
160}
161
167 inline bool IsHouseCompleted(Tile t)
168{
169 assert(IsTileType(t, MP_HOUSE));
170 return HasBit(t.m3(), 7);
171}
172
178 inline void SetHouseCompleted(Tile t, bool status)
179{
180 assert(IsTileType(t, MP_HOUSE));
181 SB(t.m3(), 7, 1, !!status);
182}
183
205 inline uint8_t GetHouseBuildingStage(Tile t)
206{
207 assert(IsTileType(t, MP_HOUSE));
208 return IsHouseCompleted(t) ? (uint8_t)TOWN_HOUSE_COMPLETED : GB(t.m5(), 3, 2);
209}
210
217 inline uint8_t GetHouseConstructionTick(Tile t)
218{
219 assert(IsTileType(t, MP_HOUSE));
220 return IsHouseCompleted(t) ? 0 : GB(t.m5(), 0, 3);
221}
222
230 inline void IncHouseConstructionTick(Tile t)
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
248 inline void ResetHouseAge(Tile t)
249{
250 assert(IsTileType(t, MP_HOUSE) && IsHouseCompleted(t));
251 t.m5() = 0;
252}
253
259 inline void IncrementHouseAge(Tile t)
260{
261 assert(IsTileType(t, MP_HOUSE));
262 if (IsHouseCompleted(t) && t.m5() < 0xFF) t.m5()++;
263}
264
271 inline TimerGameCalendar::Year GetHouseAge(Tile t)
272{
273 assert(IsTileType(t, MP_HOUSE));
274 return TimerGameCalendar::Year{IsHouseCompleted(t) ? t.m5() : 0};
275}
276
284 inline void SetHouseRandomBits(Tile t, uint8_t random)
285{
286 assert(IsTileType(t, MP_HOUSE));
287 t.m1() = random;
288}
289
297 inline uint8_t GetHouseRandomBits(Tile t)
298{
299 assert(IsTileType(t, MP_HOUSE));
300 return t.m1();
301}
302
310 inline void SetHouseRandomTriggers(Tile t, HouseRandomTriggers triggers)
311{
312 assert(IsTileType(t, MP_HOUSE));
313 SB(t.m3(), 0, 5, triggers.base());
314}
315
323 inline HouseRandomTriggers GetHouseRandomTriggers(Tile t)
324{
325 assert(IsTileType(t, MP_HOUSE));
326 return static_cast<HouseRandomTriggers>(GB(t.m3(), 0, 5));
327}
328
335 inline uint8_t GetHouseProcessingTime(Tile t)
336{
337 assert(IsTileType(t, MP_HOUSE));
338 return GB(t.m6(), 2, 6);
339}
340
347 inline void SetHouseProcessingTime(Tile t, uint8_t time)
348{
349 assert(IsTileType(t, MP_HOUSE));
350 SB(t.m6(), 2, 6, time);
351}
352
358 inline void DecHouseProcessingTime(Tile t)
359{
360 assert(IsTileType(t, MP_HOUSE));
361 t.m6() -= 1 << 2;
362}
363
375 inline 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
379 SetTileType(t, MP_HOUSE);
380 t.m1() = random_bits;
381 t.m2() = tid.base();
382 t.m3() = 0;
383 SetHouseType(t, type);
384 SetHouseCompleted(t, stage == TOWN_HOUSE_COMPLETED);
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 SB(t.m8(), 12, 4, 0);
390}
391
392#endif /* TOWN_MAP_H */
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
GB
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
SetBit
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
Definition bitmath_func.hpp:121
HasBit
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition bitmath_func.hpp:103
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
BaseBitSet::base
constexpr Tstorage base() const noexcept
Retrieve the raw value behind this bit set.
Definition base_bitset_type.hpp:219
EnumBitSet
Enum-as-bit-set wrapper.
Definition enum_type.hpp:177
Tile
Wrapper class to abstract away the way the tiles are stored.
Definition map_func.h:25
Tile::m5
uint8_t & m5()
General purpose.
Definition map_func.h:161
Tile::m1
uint8_t & m1()
Primarily used for ownership information.
Definition map_func.h:113
Tile::m6
uint8_t & m6()
General purpose.
Definition map_func.h:173
Tile::m7
uint8_t & m7()
Primarily used for newgrf support.
Definition map_func.h:185
Tile::m3
uint8_t & m3()
General purpose.
Definition map_func.h:137
Tile::m8
uint16_t & m8()
General purpose.
Definition map_func.h:197
Tile::m2
uint16_t & m2()
Primarily used for indices to towns, industries and stations.
Definition map_func.h:125
house.h
definition of HouseSpec and accessors
GetTranslatedHouseID
HouseID GetTranslatedHouseID(HouseID hid)
Do HouseID translation for NewGRFs.
Definition house.h:140
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:25
HouseID
uint16_t HouseID
OpenTTD ID of house types.
Definition house_type.h:15
road_map.h
Map accessors for roads.
IsRoadDepot
static bool IsRoadDepot(Tile t)
Return whether a tile is a road depot.
Definition road_map.h:90
HouseSpec::Get
static HouseSpec * Get(size_t house_id)
Get the spec for a house ID.
Definition newgrf_house.cpp:70
PoolID< uint16_t, struct TownIDTag, 64000, 0xFFFF >
StrongType::Typedef
Templated helper to make a type-safe 'typedef' representing a single POD value.
Definition strong_typedef_type.hpp:130
IsTileType
static bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h: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
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:259
HaltLift
void HaltLift(Tile t)
Stop the lift of this animated house from moving.
Definition town_map.h:137
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:248
SetHouseRandomBits
void SetHouseRandomBits(Tile t, uint8_t random)
Set the random bits for this house.
Definition town_map.h:284
MakeHouseTile
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
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:230
GetLiftPosition
uint8_t GetLiftPosition(Tile t)
Get the position of the lift on this animated house.
Definition town_map.h:147
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:297
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:115
GetHouseAge
TimerGameCalendar::Year GetHouseAge(Tile t)
Get the age of the house.
Definition town_map.h:271
SetLiftPosition
void SetLiftPosition(Tile t, uint8_t pos)
Set the position of the lift on this animated house.
Definition town_map.h:157
GetLiftDestination
uint8_t GetLiftDestination(Tile t)
Get the current destination for this lift.
Definition town_map.h:126
DecHouseProcessingTime
void DecHouseProcessingTime(Tile t)
Decrease the amount of time remaining before the tile loop processes this tile.
Definition town_map.h:358
GetHouseBuildingStage
uint8_t GetHouseBuildingStage(Tile t)
House Construction Scheme.
Definition town_map.h:205
SetHouseRandomTriggers
void SetHouseRandomTriggers(Tile t, HouseRandomTriggers triggers)
Set the activated triggers bits for this house.
Definition town_map.h:310
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:178
SetHouseProtected
void SetHouseProtected(Tile t, bool house_protected)
Set a house as protected from removal by towns.
Definition town_map.h:93
GetHouseRandomTriggers
HouseRandomTriggers GetHouseRandomTriggers(Tile t)
Get the already activated triggers bits for this house.
Definition town_map.h:323
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:347
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:104
IsHouseCompleted
bool IsHouseCompleted(Tile t)
Get the completion of this house.
Definition town_map.h:167
IsHouseProtected
bool IsHouseProtected(Tile t)
Check if the house is protected from removal by towns.
Definition town_map.h:82
GetHouseProcessingTime
uint8_t GetHouseProcessingTime(Tile t)
Get the amount of time remaining before the tile loop processes this tile.
Definition town_map.h:335
GetHouseConstructionTick
uint8_t GetHouseConstructionTick(Tile t)
Gets the construction stage of a house.
Definition town_map.h:217