OpenTTD Source: src/water_map.h Source File

OpenTTD Source 15.0-beta1

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 <http://www.gnu.org/licenses/>.
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 WaterTileType {
32 WATER_TILE_CLEAR,
33 WATER_TILE_COAST,
34 WATER_TILE_LOCK,
35 WATER_TILE_DEPOT,
36};
37
39 enum WaterClass : uint8_t {
40 WATER_CLASS_SEA,
41 WATER_CLASS_CANAL,
42 WATER_CLASS_RIVER,
43 WATER_CLASS_INVALID,
44};
45
52 inline bool IsValidWaterClass(WaterClass wc)
53{
54 return wc < WATER_CLASS_INVALID;
55}
56
58 enum DepotPart {
59 DEPOT_PART_NORTH = 0,
60 DEPOT_PART_SOUTH = 1,
61 DEPOT_PART_END
62};
63
65 enum LockPart {
66 LOCK_PART_MIDDLE = 0,
67 LOCK_PART_LOWER = 1,
68 LOCK_PART_UPPER = 2,
69};
70
71bool IsPossibleDockingTile(Tile t);
72
78 inline WaterTileType GetWaterTileType(Tile t)
79{
80 assert(IsTileType(t, MP_WATER));
81 return static_cast<WaterTileType>(GB(t.m5(), WBL_TYPE_BEGIN, WBL_TYPE_COUNT));
82}
83
89 inline void SetWaterTileType(Tile t, WaterTileType type)
90{
91 assert(IsTileType(t, MP_WATER));
92 SB(t.m5(), WBL_TYPE_BEGIN, WBL_TYPE_COUNT, to_underlying(type));
93}
94
101 inline bool HasTileWaterClass(Tile t)
102{
103 return IsTileType(t, MP_WATER) || IsTileType(t, MP_STATION) || IsTileType(t, MP_INDUSTRY) || IsTileType(t, MP_OBJECT) || IsTileType(t, MP_TREES);
104}
105
112 inline WaterClass GetWaterClass(Tile t)
113{
114 assert(HasTileWaterClass(t));
115 return (WaterClass)GB(t.m1(), 5, 2);
116}
117
124 inline void SetWaterClass(Tile t, WaterClass wc)
125{
126 assert(HasTileWaterClass(t));
127 SB(t.m1(), 5, 2, wc);
128}
129
136 inline bool IsTileOnWater(Tile t)
137{
138 return (GetWaterClass(t) != WATER_CLASS_INVALID);
139}
140
147 inline bool IsWater(Tile t)
148{
149 return GetWaterTileType(t) == WATER_TILE_CLEAR;
150}
151
158 inline bool IsSea(Tile t)
159{
160 return IsWater(t) && GetWaterClass(t) == WATER_CLASS_SEA;
161}
162
169 inline bool IsCanal(Tile t)
170{
171 return IsWater(t) && GetWaterClass(t) == WATER_CLASS_CANAL;
172}
173
180 inline bool IsRiver(Tile t)
181{
182 return IsWater(t) && GetWaterClass(t) == WATER_CLASS_RIVER;
183}
184
190 inline bool IsWaterTile(Tile t)
191{
192 return IsTileType(t, MP_WATER) && IsWater(t);
193}
194
201 inline bool IsCoast(Tile t)
202{
203 return GetWaterTileType(t) == WATER_TILE_COAST;
204}
205
211 inline bool IsCoastTile(Tile t)
212{
213 return (IsTileType(t, MP_WATER) && IsCoast(t)) || (IsTileType(t, MP_TREES) && GetWaterClass(t) != WATER_CLASS_INVALID);
214}
215
222 inline bool IsShipDepot(Tile t)
223{
224 return GetWaterTileType(t) == WATER_TILE_DEPOT;
225}
226
232 inline bool IsShipDepotTile(Tile t)
233{
234 return IsTileType(t, MP_WATER) && IsShipDepot(t);
235}
236
243 inline Axis GetShipDepotAxis(Tile t)
244{
245 assert(IsShipDepotTile(t));
246 return (Axis)GB(t.m5(), WBL_DEPOT_AXIS, 1);
247}
248
255 inline DepotPart GetShipDepotPart(Tile t)
256{
257 assert(IsShipDepotTile(t));
258 return (DepotPart)GB(t.m5(), WBL_DEPOT_PART, 1);
259}
260
267 inline DiagDirection GetShipDepotDirection(Tile t)
268{
269 return XYNSToDiagDir(GetShipDepotAxis(t), GetShipDepotPart(t));
270}
271
278 inline TileIndex GetOtherShipDepotTile(Tile t)
279{
280 return TileIndex(t) + (GetShipDepotPart(t) != DEPOT_PART_NORTH ? -1 : 1) * TileOffsByAxis(GetShipDepotAxis(t));
281}
282
289 inline TileIndex GetShipDepotNorthTile(Tile t)
290{
291 assert(IsShipDepot(t));
292 TileIndex tile2 = GetOtherShipDepotTile(t);
293
294 return t < tile2 ? TileIndex(t) : tile2;
295}
296
303 inline bool IsLock(Tile t)
304{
305 return GetWaterTileType(t) == WATER_TILE_LOCK;
306}
307
314 inline DiagDirection GetLockDirection(Tile t)
315{
316 assert(IsLock(t));
317 return (DiagDirection)GB(t.m5(), WBL_LOCK_ORIENT_BEGIN, WBL_LOCK_ORIENT_COUNT);
318}
319
326 inline uint8_t GetLockPart(Tile t)
327{
328 assert(IsLock(t));
329 return GB(t.m5(), WBL_LOCK_PART_BEGIN, WBL_LOCK_PART_COUNT);
330}
331
338 inline uint8_t GetWaterTileRandomBits(Tile t)
339{
340 assert(IsTileType(t, MP_WATER));
341 return t.m4();
342}
343
350 inline bool HasTileWaterGround(Tile t)
351{
352 return HasTileWaterClass(t) && IsTileOnWater(t) && !IsCoastTile(t);
353}
354
361 inline void SetDockingTile(Tile t, bool b)
362{
363 assert(IsTileType(t, MP_WATER) || IsTileType(t, MP_RAILWAY) || IsTileType(t, MP_STATION) || IsTileType(t, MP_TUNNELBRIDGE));
364 AssignBit(t.m1(), 7, b);
365}
366
371 inline bool IsDockingTile(Tile t)
372{
373 return (IsTileType(t, MP_WATER) || IsTileType(t, MP_RAILWAY) || IsTileType(t, MP_STATION) || IsTileType(t, MP_TUNNELBRIDGE)) && HasBit(t.m1(), 7);
374}
375
376
381 inline void MakeShore(Tile t)
382{
383 SetTileType(t, MP_WATER);
384 SetTileOwner(t, OWNER_WATER);
385 SetWaterClass(t, WATER_CLASS_SEA);
386 SetDockingTile(t, false);
387 t.m2() = 0;
388 t.m3() = 0;
389 t.m4() = 0;
390 t.m5() = 0;
391 SetWaterTileType(t, WATER_TILE_COAST);
392 SB(t.m6(), 2, 4, 0);
393 t.m7() = 0;
394}
395
403 inline void MakeWater(Tile t, Owner o, WaterClass wc, uint8_t random_bits)
404{
405 SetTileType(t, MP_WATER);
406 SetTileOwner(t, o);
407 SetWaterClass(t, wc);
408 SetDockingTile(t, false);
409 t.m2() = 0;
410 t.m3() = 0;
411 t.m4() = random_bits;
412 t.m5() = 0;
413 SetWaterTileType(t, WATER_TILE_CLEAR);
414 SB(t.m6(), 2, 4, 0);
415 t.m7() = 0;
416}
417
422 inline void MakeSea(Tile t)
423{
424 MakeWater(t, OWNER_WATER, WATER_CLASS_SEA, 0);
425}
426
432 inline void MakeRiver(Tile t, uint8_t random_bits)
433{
434 MakeWater(t, OWNER_WATER, WATER_CLASS_RIVER, random_bits);
435}
436
443 inline void MakeCanal(Tile t, Owner o, uint8_t random_bits)
444{
445 assert(o != OWNER_WATER);
446 MakeWater(t, o, WATER_CLASS_CANAL, random_bits);
447}
448
458 inline void MakeShipDepot(Tile t, Owner o, DepotID did, DepotPart part, Axis a, WaterClass original_water_class)
459{
460 SetTileType(t, MP_WATER);
461 SetTileOwner(t, o);
462 SetWaterClass(t, original_water_class);
463 SetDockingTile(t, false);
464 t.m2() = did;
465 t.m3() = 0;
466 t.m4() = 0;
467 t.m5() = part << WBL_DEPOT_PART | a << WBL_DEPOT_AXIS;
468 SetWaterTileType(t, WATER_TILE_DEPOT);
469 SB(t.m6(), 2, 4, 0);
470 t.m7() = 0;
471}
472
482 inline void MakeLockTile(Tile t, Owner o, LockPart part, DiagDirection dir, WaterClass original_water_class)
483{
484 SetTileType(t, MP_WATER);
485 SetTileOwner(t, o);
486 SetWaterClass(t, original_water_class);
487 SetDockingTile(t, false);
488 t.m2() = 0;
489 t.m3() = 0;
490 t.m4() = 0;
491 t.m5() = part << WBL_LOCK_PART_BEGIN | dir << WBL_LOCK_ORIENT_BEGIN;
492 SetWaterTileType(t, WATER_TILE_LOCK);
493 SB(t.m6(), 2, 4, 0);
494 t.m7() = 0;
495}
496
506 inline void MakeLock(Tile t, Owner o, DiagDirection d, WaterClass wc_lower, WaterClass wc_upper, WaterClass wc_middle)
507{
508 TileIndexDiff delta = TileOffsByDiagDir(d);
509 Tile lower_tile = TileIndex(t) - delta;
510 Tile upper_tile = TileIndex(t) + delta;
511
512 /* Keep the current waterclass and owner for the tiles.
513 * It allows to restore them after the lock is deleted */
514 MakeLockTile(t, o, LOCK_PART_MIDDLE, d, wc_middle);
515 MakeLockTile(lower_tile, IsWaterTile(lower_tile) ? GetTileOwner(lower_tile) : o, LOCK_PART_LOWER, d, wc_lower);
516 MakeLockTile(upper_tile, IsWaterTile(upper_tile) ? GetTileOwner(upper_tile) : o, LOCK_PART_UPPER, d, wc_upper);
517}
518
524 inline void SetNonFloodingWaterTile(Tile t, bool b)
525{
526 assert(IsTileType(t, MP_WATER));
527 AssignBit(t.m3(), 0, b);
528}
533 inline bool IsNonFloodingWaterTile(Tile t)
534{
535 assert(IsTileType(t, MP_WATER));
536 return HasBit(t.m3(), 0);
537}
538
539#endif /* WATER_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
AssignBit
constexpr T AssignBit(T &x, const uint8_t y, bool value)
Assigns a bit in a variable.
Definition bitmath_func.hpp:200
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
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::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::m4
debug_inline uint8_t & m4()
General purpose.
Definition map_func.h:149
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
Owner
Owner
Enum for all companies/owners.
Definition company_type.h:18
OWNER_WATER
@ OWNER_WATER
The tile/execution is done by "water".
Definition company_type.h:26
depot_type.h
Header files for depots (not hangars)
DepotID
uint16_t DepotID
Type for the unique identifier of depots.
Definition depot_type.h:13
XYNSToDiagDir
DiagDirection XYNSToDiagDir(Axis xy, uint ns)
Convert an axis and a flag for north/south into a DiagDirection.
Definition direction_func.h:261
Axis
Axis
Allow incrementing of DiagDirDiff variables.
Definition direction_type.h:116
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition direction_type.h:73
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:15
TileOffsByAxis
TileIndexDiff TileOffsByAxis(Axis axis)
Convert an Axis to a TileIndexDiff.
Definition map_func.h:552
TileOffsByDiagDir
TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition map_func.h:567
TileIndexDiff
int32_t TileIndexDiff
An offset value between two tiles.
Definition map_type.h:23
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.
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
IsTileType
static debug_inline bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
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:381
SetWaterClass
void SetWaterClass(Tile t, WaterClass wc)
Set the water class at a tile.
Definition water_map.h:124
HasTileWaterGround
bool HasTileWaterGround(Tile t)
Checks whether the tile has water at the ground.
Definition water_map.h:350
GetShipDepotNorthTile
TileIndex GetShipDepotNorthTile(Tile t)
Get the most northern tile of a ship depot.
Definition water_map.h:289
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:506
MakeWater
void MakeWater(Tile t, Owner o, WaterClass wc, uint8_t random_bits)
Helper function for making a watery tile.
Definition water_map.h:403
GetShipDepotPart
DepotPart GetShipDepotPart(Tile t)
Get the part of a ship depot.
Definition water_map.h:255
IsTileOnWater
bool IsTileOnWater(Tile t)
Tests if the tile was built on water.
Definition water_map.h:136
DepotPart
DepotPart
Sections of the water depot.
Definition water_map.h:58
DEPOT_PART_NORTH
@ DEPOT_PART_NORTH
Northern part of a depot.
Definition water_map.h:59
DEPOT_PART_SOUTH
@ DEPOT_PART_SOUTH
Southern part of a depot.
Definition water_map.h:60
IsShipDepot
bool IsShipDepot(Tile t)
Is it a water tile with a ship depot on it?
Definition water_map.h:222
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:180
GetLockDirection
DiagDirection GetLockDirection(Tile t)
Get the direction of the water lock.
Definition water_map.h:314
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 WATER_TILE_CLEAR water tile type).
Definition water_map.h:39
WATER_CLASS_SEA
@ WATER_CLASS_SEA
Sea.
Definition water_map.h:40
WATER_CLASS_CANAL
@ WATER_CLASS_CANAL
Canal.
Definition water_map.h:41
WATER_CLASS_INVALID
@ WATER_CLASS_INVALID
Used for industry tiles on land (also for oilrig if newgrf says so).
Definition water_map.h:43
WATER_CLASS_RIVER
@ WATER_CLASS_RIVER
River.
Definition water_map.h:42
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:482
HasTileWaterClass
bool HasTileWaterClass(Tile t)
Checks whether the tile has an waterclass associated.
Definition water_map.h:101
IsShipDepotTile
bool IsShipDepotTile(Tile t)
Is it a ship depot tile?
Definition water_map.h:232
IsCanal
bool IsCanal(Tile t)
Is it a canal tile?
Definition water_map.h:169
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:201
MakeRiver
void MakeRiver(Tile t, uint8_t random_bits)
Make a river tile.
Definition water_map.h:432
GetWaterTileType
WaterTileType GetWaterTileType(Tile t)
Get the water tile type of a tile.
Definition water_map.h:78
SetNonFloodingWaterTile
void SetNonFloodingWaterTile(Tile t, bool b)
Set the non-flooding water tile state of a tile.
Definition water_map.h:524
SetWaterTileType
void SetWaterTileType(Tile t, WaterTileType type)
Set the water tile type of a tile.
Definition water_map.h:89
WaterTileType
WaterTileType
Available water tile types.
Definition water_map.h:31
WATER_TILE_COAST
@ WATER_TILE_COAST
Coast.
Definition water_map.h:33
WATER_TILE_LOCK
@ WATER_TILE_LOCK
Water lock.
Definition water_map.h:34
WATER_TILE_DEPOT
@ WATER_TILE_DEPOT
Water Depot.
Definition water_map.h:35
WATER_TILE_CLEAR
@ WATER_TILE_CLEAR
Plain water.
Definition water_map.h:32
GetWaterClass
WaterClass GetWaterClass(Tile t)
Get the water class at a tile.
Definition water_map.h:112
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:443
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:211
GetLockPart
uint8_t GetLockPart(Tile t)
Get the part of a lock.
Definition water_map.h:326
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:278
IsDockingTile
bool IsDockingTile(Tile t)
Checks whether the tile is marked as a dockling tile.
Definition water_map.h:371
IsNonFloodingWaterTile
bool IsNonFloodingWaterTile(Tile t)
Checks whether the tile is marked as a non-flooding water tile.
Definition water_map.h:533
SetDockingTile
void SetDockingTile(Tile t, bool b)
Set the docking tile state of a tile.
Definition water_map.h:361
IsWater
bool IsWater(Tile t)
Is it a plain water tile?
Definition water_map.h:147
IsWaterTile
bool IsWaterTile(Tile t)
Is it a water tile with plain water?
Definition water_map.h:190
IsLock
bool IsLock(Tile t)
Is there a lock on a given water tile?
Definition water_map.h:303
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:458
GetWaterTileRandomBits
uint8_t GetWaterTileRandomBits(Tile t)
Get the random bits of the water tile.
Definition water_map.h:338
MakeSea
void MakeSea(Tile t)
Make a sea tile.
Definition water_map.h:422
GetShipDepotDirection
DiagDirection GetShipDepotDirection(Tile t)
Get the direction of the ship depot.
Definition water_map.h:267
IsSea
bool IsSea(Tile t)
Is it a sea water tile?
Definition water_map.h:158
LockPart
LockPart
Sections of the water lock.
Definition water_map.h:65
LOCK_PART_UPPER
@ LOCK_PART_UPPER
Upper part of a lock.
Definition water_map.h:68
LOCK_PART_MIDDLE
@ LOCK_PART_MIDDLE
Middle part of a lock.
Definition water_map.h:66
LOCK_PART_LOWER
@ LOCK_PART_LOWER
Lower part of a lock.
Definition water_map.h:67
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:243