OpenTTD Source: src/water_map.h Source File

OpenTTD Source 15.3

water_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 WATER_MAP_H
11#define WATER_MAP_H
12
13#include "depot_type.h"
14#include "tile_map.h"
15
19 static constexpr uint8_t WBL_TYPE_BEGIN = 4;
20 static constexpr uint8_t WBL_TYPE_COUNT = 4;
21
22 static constexpr uint8_t WBL_LOCK_ORIENT_BEGIN = 0;
23 static constexpr uint8_t WBL_LOCK_ORIENT_COUNT = 2;
24 static constexpr uint8_t WBL_LOCK_PART_BEGIN = 2;
25 static constexpr uint8_t WBL_LOCK_PART_COUNT = 2;
26
27 static constexpr uint8_t WBL_DEPOT_PART = 0;
28 static constexpr uint8_t WBL_DEPOT_AXIS = 1;
29
31 enum class WaterTileType : uint8_t {
32 Clear = 0,
33 Coast = 1,
34 Lock = 2,
35 Depot = 3,
36};
37
39 enum class WaterClass : uint8_t {
40 Sea = 0,
41 Canal = 1,
42 River = 2,
43 Invalid = 3,
44};
45
52 inline bool IsValidWaterClass(WaterClass wc)
53{
54 return wc < WaterClass::Invalid;
55}
56
58 enum class DepotPart : uint8_t {
59 North = 0,
60 South = 1,
61 End,
62};
63
65 enum class LockPart : uint8_t {
66 Middle = 0,
67 Lower = 1,
68 Upper = 2,
69 End,
70};
71 DECLARE_INCREMENT_DECREMENT_OPERATORS(LockPart);
72
73bool IsPossibleDockingTile(Tile t);
74
80 inline WaterTileType GetWaterTileType(Tile t)
81{
82 assert(IsTileType(t, MP_WATER));
83 return static_cast<WaterTileType>(GB(t.m5(), WBL_TYPE_BEGIN, WBL_TYPE_COUNT));
84}
85
91 inline void SetWaterTileType(Tile t, WaterTileType type)
92{
93 assert(IsTileType(t, MP_WATER));
94 SB(t.m5(), WBL_TYPE_BEGIN, WBL_TYPE_COUNT, to_underlying(type));
95}
96
103 inline bool HasTileWaterClass(Tile t)
104{
105 return IsTileType(t, MP_WATER) || IsTileType(t, MP_STATION) || IsTileType(t, MP_INDUSTRY) || IsTileType(t, MP_OBJECT) || IsTileType(t, MP_TREES);
106}
107
114 inline WaterClass GetWaterClass(Tile t)
115{
116 assert(HasTileWaterClass(t));
117 return static_cast<WaterClass>(GB(t.m1(), 5, 2));
118}
119
126 inline void SetWaterClass(Tile t, WaterClass wc)
127{
128 assert(HasTileWaterClass(t));
129 SB(t.m1(), 5, 2, to_underlying(wc));
130}
131
138 inline bool IsTileOnWater(Tile t)
139{
140 return (GetWaterClass(t) != WaterClass::Invalid);
141}
142
149 inline bool IsWater(Tile t)
150{
151 return GetWaterTileType(t) == WaterTileType::Clear;
152}
153
160 inline bool IsSea(Tile t)
161{
162 return IsWater(t) && GetWaterClass(t) == WaterClass::Sea;
163}
164
171 inline bool IsCanal(Tile t)
172{
173 return IsWater(t) && GetWaterClass(t) == WaterClass::Canal;
174}
175
182 inline bool IsRiver(Tile t)
183{
184 return IsWater(t) && GetWaterClass(t) == WaterClass::River;
185}
186
192 inline bool IsWaterTile(Tile t)
193{
194 return IsTileType(t, MP_WATER) && IsWater(t);
195}
196
203 inline bool IsCoast(Tile t)
204{
205 return GetWaterTileType(t) == WaterTileType::Coast;
206}
207
213 inline bool IsCoastTile(Tile t)
214{
215 return (IsTileType(t, MP_WATER) && IsCoast(t)) || (IsTileType(t, MP_TREES) && GetWaterClass(t) != WaterClass::Invalid);
216}
217
224 inline bool IsShipDepot(Tile t)
225{
226 return GetWaterTileType(t) == WaterTileType::Depot;
227}
228
234 inline bool IsShipDepotTile(Tile t)
235{
236 return IsTileType(t, MP_WATER) && IsShipDepot(t);
237}
238
245 inline Axis GetShipDepotAxis(Tile t)
246{
247 assert(IsShipDepotTile(t));
248 return (Axis)GB(t.m5(), WBL_DEPOT_AXIS, 1);
249}
250
257 inline DepotPart GetShipDepotPart(Tile t)
258{
259 assert(IsShipDepotTile(t));
260 return static_cast<DepotPart>(GB(t.m5(), WBL_DEPOT_PART, 1));
261}
262
269 inline DiagDirection GetShipDepotDirection(Tile t)
270{
271 return XYNSToDiagDir(GetShipDepotAxis(t), to_underlying(GetShipDepotPart(t)));
272}
273
280 inline TileIndex GetOtherShipDepotTile(Tile t)
281{
282 return TileIndex(t) + (GetShipDepotPart(t) != DepotPart::North ? -1 : 1) * TileOffsByAxis(GetShipDepotAxis(t));
283}
284
291 inline TileIndex GetShipDepotNorthTile(Tile t)
292{
293 assert(IsShipDepot(t));
294 TileIndex tile2 = GetOtherShipDepotTile(t);
295
296 return t < tile2 ? TileIndex(t) : tile2;
297}
298
305 inline bool IsLock(Tile t)
306{
307 return GetWaterTileType(t) == WaterTileType::Lock;
308}
309
316 inline DiagDirection GetLockDirection(Tile t)
317{
318 assert(IsLock(t));
319 return (DiagDirection)GB(t.m5(), WBL_LOCK_ORIENT_BEGIN, WBL_LOCK_ORIENT_COUNT);
320}
321
328 inline LockPart GetLockPart(Tile t)
329{
330 assert(IsLock(t));
331 return static_cast<LockPart>(GB(t.m5(), WBL_LOCK_PART_BEGIN, WBL_LOCK_PART_COUNT));
332}
333
340 inline uint8_t GetWaterTileRandomBits(Tile t)
341{
342 assert(IsTileType(t, MP_WATER));
343 return t.m4();
344}
345
352 inline bool HasTileWaterGround(Tile t)
353{
354 return HasTileWaterClass(t) && IsTileOnWater(t) && !IsCoastTile(t);
355}
356
363 inline void SetDockingTile(Tile t, bool b)
364{
365 assert(IsTileType(t, MP_WATER) || IsTileType(t, MP_RAILWAY) || IsTileType(t, MP_STATION) || IsTileType(t, MP_TUNNELBRIDGE));
366 AssignBit(t.m1(), 7, b);
367}
368
373 inline bool IsDockingTile(Tile t)
374{
375 return (IsTileType(t, MP_WATER) || IsTileType(t, MP_RAILWAY) || IsTileType(t, MP_STATION) || IsTileType(t, MP_TUNNELBRIDGE)) && HasBit(t.m1(), 7);
376}
377
378
383 inline void MakeShore(Tile t)
384{
385 SetTileType(t, MP_WATER);
386 SetTileOwner(t, OWNER_WATER);
387 SetWaterClass(t, WaterClass::Sea);
388 SetDockingTile(t, false);
389 t.m2() = 0;
390 t.m3() = 0;
391 t.m4() = 0;
392 t.m5() = 0;
393 SetWaterTileType(t, WaterTileType::Coast);
394 SB(t.m6(), 2, 6, 0);
395 t.m7() = 0;
396 t.m8() = 0;
397}
398
406 inline void MakeWater(Tile t, Owner o, WaterClass wc, uint8_t random_bits)
407{
408 SetTileType(t, MP_WATER);
409 SetTileOwner(t, o);
410 SetWaterClass(t, wc);
411 SetDockingTile(t, false);
412 t.m2() = 0;
413 t.m3() = 0;
414 t.m4() = random_bits;
415 t.m5() = 0;
416 SetWaterTileType(t, WaterTileType::Clear);
417 SB(t.m6(), 2, 6, 0);
418 t.m7() = 0;
419 t.m8() = 0;
420}
421
426 inline void MakeSea(Tile t)
427{
428 MakeWater(t, OWNER_WATER, WaterClass::Sea, 0);
429}
430
436 inline void MakeRiver(Tile t, uint8_t random_bits)
437{
438 MakeWater(t, OWNER_WATER, WaterClass::River, random_bits);
439}
440
447 inline void MakeCanal(Tile t, Owner o, uint8_t random_bits)
448{
449 assert(o != OWNER_WATER);
450 MakeWater(t, o, WaterClass::Canal, random_bits);
451}
452
462 inline void MakeShipDepot(Tile t, Owner o, DepotID did, DepotPart part, Axis a, WaterClass original_water_class)
463{
464 SetTileType(t, MP_WATER);
465 SetTileOwner(t, o);
466 SetWaterClass(t, original_water_class);
467 SetDockingTile(t, false);
468 t.m2() = did.base();
469 t.m3() = 0;
470 t.m4() = 0;
471 t.m5() = to_underlying(part) << WBL_DEPOT_PART | a << WBL_DEPOT_AXIS;
472 SetWaterTileType(t, WaterTileType::Depot);
473 SB(t.m6(), 2, 6, 0);
474 t.m7() = 0;
475 t.m8() = 0;
476}
477
487 inline void MakeLockTile(Tile t, Owner o, LockPart part, DiagDirection dir, WaterClass original_water_class)
488{
489 SetTileType(t, MP_WATER);
490 SetTileOwner(t, o);
491 SetWaterClass(t, original_water_class);
492 SetDockingTile(t, false);
493 t.m2() = 0;
494 t.m3() = 0;
495 t.m4() = 0;
496 t.m5() = to_underlying(part) << WBL_LOCK_PART_BEGIN | dir << WBL_LOCK_ORIENT_BEGIN;
497 SetWaterTileType(t, WaterTileType::Lock);
498 SB(t.m6(), 2, 6, 0);
499 t.m7() = 0;
500 t.m8() = 0;
501}
502
512 inline void MakeLock(Tile t, Owner o, DiagDirection d, WaterClass wc_lower, WaterClass wc_upper, WaterClass wc_middle)
513{
514 TileIndexDiff delta = TileOffsByDiagDir(d);
515 Tile lower_tile = TileIndex(t) - delta;
516 Tile upper_tile = TileIndex(t) + delta;
517
518 /* Keep the current waterclass and owner for the tiles.
519 * It allows to restore them after the lock is deleted */
520 MakeLockTile(t, o, LockPart::Middle, d, wc_middle);
521 MakeLockTile(lower_tile, IsWaterTile(lower_tile) ? GetTileOwner(lower_tile) : o, LockPart::Lower, d, wc_lower);
522 MakeLockTile(upper_tile, IsWaterTile(upper_tile) ? GetTileOwner(upper_tile) : o, LockPart::Upper, d, wc_upper);
523}
524
530 inline void SetNonFloodingWaterTile(Tile t, bool b)
531{
532 assert(IsTileType(t, MP_WATER));
533 AssignBit(t.m3(), 0, b);
534}
539 inline bool IsNonFloodingWaterTile(Tile t)
540{
541 assert(IsTileType(t, MP_WATER));
542 return HasBit(t.m3(), 0);
543}
544
545#endif /* WATER_MAP_H */
AssignBit
constexpr T AssignBit(T &x, const uint8_t y, bool value)
Assigns a bit in a variable.
Definition bitmath_func.hpp:176
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
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
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::m4
uint8_t & m4()
General purpose.
Definition map_func.h:149
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
CommandType::End
@ End
End marker.
OWNER_WATER
static constexpr Owner OWNER_WATER
The tile/execution is done by "water".
Definition company_type.h:27
depot_type.h
Header files for depots (not hangars)
XYNSToDiagDir
DiagDirection XYNSToDiagDir(Axis xy, uint ns)
Convert an axis and a flag for north/south into a DiagDirection.
Definition direction_func.h:278
Axis
Axis
Allow incrementing of DiagDirDiff variables.
Definition direction_type.h:123
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition direction_type.h:78
to_underlying
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23)
Definition enum_type.hpp:17
DECLARE_INCREMENT_DECREMENT_OPERATORS
#define DECLARE_INCREMENT_DECREMENT_OPERATORS(enum_type)
For some enums it is useful to have pre/post increment/decrement operators.
Definition enum_type.hpp:63
TileOffsByAxis
TileIndexDiff TileOffsByAxis(Axis axis)
Convert an Axis to a TileIndexDiff.
Definition map_func.h:568
TileOffsByDiagDir
TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition map_func.h:583
TileIndexDiff
int32_t TileIndexDiff
An offset value between two tiles.
Definition map_type.h:23
Depot
Definition depot_base.h:20
PoolID< uint8_t, struct CompanyIDTag, 0xF, 0xFF >
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
tile_map.h
Map writing/reading functions for tiles.
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
GetTileOwner
Owner GetTileOwner(Tile tile)
Returns the owner of a tile.
Definition tile_map.h:178
SetTileOwner
void SetTileOwner(Tile tile, Owner owner)
Sets the owner of a tile.
Definition tile_map.h:198
TileIndex
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:87
MP_TREES
@ MP_TREES
Tile got trees.
Definition tile_type.h:52
MP_STATION
@ MP_STATION
A tile of a station.
Definition tile_type.h:53
MP_TUNNELBRIDGE
@ MP_TUNNELBRIDGE
Tunnel entry/exit and bridge heads.
Definition tile_type.h:57
MP_WATER
@ MP_WATER
Water tile.
Definition tile_type.h:54
MP_RAILWAY
@ MP_RAILWAY
A railway.
Definition tile_type.h:49
MP_INDUSTRY
@ MP_INDUSTRY
Part of an industry.
Definition tile_type.h:56
MP_OBJECT
@ MP_OBJECT
Contains objects such as transmitters and owned land.
Definition tile_type.h:58
MakeShore
void MakeShore(Tile t)
Helper function to make a coast tile.
Definition water_map.h:383
SetWaterClass
void SetWaterClass(Tile t, WaterClass wc)
Set the water class at a tile.
Definition water_map.h:126
HasTileWaterGround
bool HasTileWaterGround(Tile t)
Checks whether the tile has water at the ground.
Definition water_map.h:352
GetShipDepotNorthTile
TileIndex GetShipDepotNorthTile(Tile t)
Get the most northern tile of a ship depot.
Definition water_map.h:291
MakeLock
void MakeLock(Tile t, Owner o, DiagDirection d, WaterClass wc_lower, WaterClass wc_upper, WaterClass wc_middle)
Make a water lock.
Definition water_map.h:512
MakeWater
void MakeWater(Tile t, Owner o, WaterClass wc, uint8_t random_bits)
Helper function for making a watery tile.
Definition water_map.h:406
GetShipDepotPart
DepotPart GetShipDepotPart(Tile t)
Get the part of a ship depot.
Definition water_map.h:257
IsTileOnWater
bool IsTileOnWater(Tile t)
Tests if the tile was built on water.
Definition water_map.h:138
IsShipDepot
bool IsShipDepot(Tile t)
Is it a water tile with a ship depot on it?
Definition water_map.h:224
IsValidWaterClass
bool IsValidWaterClass(WaterClass wc)
Checks if a water class is valid.
Definition water_map.h:52
WBL_LOCK_PART_BEGIN
static constexpr uint8_t WBL_LOCK_PART_BEGIN
Start of lock part bitfield.
Definition water_map.h:24
IsRiver
bool IsRiver(Tile t)
Is it a river water tile?
Definition water_map.h:182
GetLockDirection
DiagDirection GetLockDirection(Tile t)
Get the direction of the water lock.
Definition water_map.h:316
WBL_TYPE_COUNT
static constexpr uint8_t WBL_TYPE_COUNT
Length of the 'type' bitfield.
Definition water_map.h:20
WaterClass
WaterClass
classes of water (for WaterTileType::Clear water tile type).
Definition water_map.h:39
WaterClass::River
@ River
River.
WaterClass::Invalid
@ Invalid
Used for industry tiles on land (also for oilrig if newgrf says so).
WaterClass::Canal
@ Canal
Canal.
WaterClass::Sea
@ Sea
Sea.
WBL_LOCK_ORIENT_BEGIN
static constexpr uint8_t WBL_LOCK_ORIENT_BEGIN
Start of lock orientation bitfield.
Definition water_map.h:22
MakeLockTile
void MakeLockTile(Tile t, Owner o, LockPart part, DiagDirection dir, WaterClass original_water_class)
Make a lock section.
Definition water_map.h:487
HasTileWaterClass
bool HasTileWaterClass(Tile t)
Checks whether the tile has an waterclass associated.
Definition water_map.h:103
IsShipDepotTile
bool IsShipDepotTile(Tile t)
Is it a ship depot tile?
Definition water_map.h:234
IsCanal
bool IsCanal(Tile t)
Is it a canal tile?
Definition water_map.h:171
WBL_TYPE_BEGIN
static constexpr uint8_t WBL_TYPE_BEGIN
Bit field layout of m5 for water tiles.
Definition water_map.h:19
IsCoast
bool IsCoast(Tile t)
Is it a coast tile?
Definition water_map.h:203
MakeRiver
void MakeRiver(Tile t, uint8_t random_bits)
Make a river tile.
Definition water_map.h:436
GetWaterTileType
WaterTileType GetWaterTileType(Tile t)
Get the water tile type of a tile.
Definition water_map.h:80
SetNonFloodingWaterTile
void SetNonFloodingWaterTile(Tile t, bool b)
Set the non-flooding water tile state of a tile.
Definition water_map.h:530
SetWaterTileType
void SetWaterTileType(Tile t, WaterTileType type)
Set the water tile type of a tile.
Definition water_map.h:91
GetWaterClass
WaterClass GetWaterClass(Tile t)
Get the water class at a tile.
Definition water_map.h:114
WBL_LOCK_ORIENT_COUNT
static constexpr uint8_t WBL_LOCK_ORIENT_COUNT
Length of lock orientation bitfield.
Definition water_map.h:23
MakeCanal
void MakeCanal(Tile t, Owner o, uint8_t random_bits)
Make a canal tile.
Definition water_map.h:447
WBL_DEPOT_AXIS
static constexpr uint8_t WBL_DEPOT_AXIS
Depot axis flag.
Definition water_map.h:28
IsCoastTile
bool IsCoastTile(Tile t)
Is it a coast tile.
Definition water_map.h:213
WBL_DEPOT_PART
static constexpr uint8_t WBL_DEPOT_PART
Depot part flag.
Definition water_map.h:27
GetOtherShipDepotTile
TileIndex GetOtherShipDepotTile(Tile t)
Get the other tile of the ship depot.
Definition water_map.h:280
DepotPart
DepotPart
Sections of the water depot.
Definition water_map.h:58
DepotPart::South
@ South
Southern part of a depot.
DepotPart::North
@ North
Northern part of a depot.
IsDockingTile
bool IsDockingTile(Tile t)
Checks whether the tile is marked as a dockling tile.
Definition water_map.h:373
WaterTileType
WaterTileType
Available water tile types.
Definition water_map.h:31
WaterTileType::Coast
@ Coast
Coast.
WaterTileType::Depot
@ Depot
Water Depot.
WaterTileType::Lock
@ Lock
Water lock.
WaterTileType::Clear
@ Clear
Plain water.
IsNonFloodingWaterTile
bool IsNonFloodingWaterTile(Tile t)
Checks whether the tile is marked as a non-flooding water tile.
Definition water_map.h:539
SetDockingTile
void SetDockingTile(Tile t, bool b)
Set the docking tile state of a tile.
Definition water_map.h:363
LockPart
LockPart
Sections of the water lock.
Definition water_map.h:65
LockPart::Upper
@ Upper
Upper part of a lock.
LockPart::Middle
@ Middle
Middle part of a lock.
LockPart::Lower
@ Lower
Lower part of a lock.
IsWater
bool IsWater(Tile t)
Is it a plain water tile?
Definition water_map.h:149
IsWaterTile
bool IsWaterTile(Tile t)
Is it a water tile with plain water?
Definition water_map.h:192
IsLock
bool IsLock(Tile t)
Is there a lock on a given water tile?
Definition water_map.h:305
MakeShipDepot
void MakeShipDepot(Tile t, Owner o, DepotID did, DepotPart part, Axis a, WaterClass original_water_class)
Make a ship depot section.
Definition water_map.h:462
GetWaterTileRandomBits
uint8_t GetWaterTileRandomBits(Tile t)
Get the random bits of the water tile.
Definition water_map.h:340
MakeSea
void MakeSea(Tile t)
Make a sea tile.
Definition water_map.h:426
GetShipDepotDirection
DiagDirection GetShipDepotDirection(Tile t)
Get the direction of the ship depot.
Definition water_map.h:269
IsSea
bool IsSea(Tile t)
Is it a sea water tile?
Definition water_map.h:160
GetLockPart
LockPart GetLockPart(Tile t)
Get the part of a lock.
Definition water_map.h:328
WBL_LOCK_PART_COUNT
static constexpr uint8_t WBL_LOCK_PART_COUNT
Length of lock part bitfield.
Definition water_map.h:25
GetShipDepotAxis
Axis GetShipDepotAxis(Tile t)
Get the axis of the ship depot.
Definition water_map.h:245