OpenTTD Source 20250924-master-gbec4e71d53
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
19static constexpr uint8_t WBL_TYPE_BEGIN = 4;
20static constexpr uint8_t WBL_TYPE_COUNT = 4;
21
22static constexpr uint8_t WBL_LOCK_ORIENT_BEGIN = 0;
23static constexpr uint8_t WBL_LOCK_ORIENT_COUNT = 2;
24static constexpr uint8_t WBL_LOCK_PART_BEGIN = 2;
25static constexpr uint8_t WBL_LOCK_PART_COUNT = 2;
26
27static constexpr uint8_t WBL_DEPOT_PART = 0;
28static constexpr uint8_t WBL_DEPOT_AXIS = 1;
29
37
45
53{
54 return wc < WATER_CLASS_INVALID;
55}
56
58enum DepotPart : uint8_t {
61 DEPOT_PART_END
62};
63
65enum LockPart : uint8_t {
69 LOCK_PART_END,
70};
72
73bool IsPossibleDockingTile(Tile t);
74
81{
82 assert(IsTileType(t, MP_WATER));
83 return static_cast<WaterTileType>(GB(t.m5(), WBL_TYPE_BEGIN, WBL_TYPE_COUNT));
84}
85
92{
93 assert(IsTileType(t, MP_WATER));
95}
96
104{
106}
107
115{
116 assert(HasTileWaterClass(t));
117 return (WaterClass)GB(t.m1(), 5, 2);
118}
119
126inline void SetWaterClass(Tile t, WaterClass wc)
127{
128 assert(HasTileWaterClass(t));
129 SB(t.m1(), 5, 2, wc);
130}
131
138inline bool IsTileOnWater(Tile t)
139{
140 return (GetWaterClass(t) != WATER_CLASS_INVALID);
141}
142
149inline bool IsWater(Tile t)
150{
152}
153
160inline bool IsSea(Tile t)
161{
162 return IsWater(t) && GetWaterClass(t) == WATER_CLASS_SEA;
163}
164
171inline bool IsCanal(Tile t)
172{
173 return IsWater(t) && GetWaterClass(t) == WATER_CLASS_CANAL;
174}
175
182inline bool IsRiver(Tile t)
183{
184 return IsWater(t) && GetWaterClass(t) == WATER_CLASS_RIVER;
185}
186
192inline bool IsWaterTile(Tile t)
193{
194 return IsTileType(t, MP_WATER) && IsWater(t);
195}
196
203inline bool IsCoast(Tile t)
204{
206}
207
213inline bool IsCoastTile(Tile t)
214{
216}
217
224inline bool IsShipDepot(Tile t)
225{
227}
228
234inline bool IsShipDepotTile(Tile t)
235{
236 return IsTileType(t, MP_WATER) && IsShipDepot(t);
237}
238
246{
247 assert(IsShipDepotTile(t));
248 return (Axis)GB(t.m5(), WBL_DEPOT_AXIS, 1);
249}
250
258{
259 assert(IsShipDepotTile(t));
260 return (DepotPart)GB(t.m5(), WBL_DEPOT_PART, 1);
261}
262
273
284
292{
293 assert(IsShipDepot(t));
295
296 return t < tile2 ? TileIndex(t) : tile2;
297}
298
305inline bool IsLock(Tile t)
306{
308}
309
321
329{
330 assert(IsLock(t));
331 return static_cast<LockPart>(GB(t.m5(), WBL_LOCK_PART_BEGIN, WBL_LOCK_PART_COUNT));
332}
333
341{
342 assert(IsTileType(t, MP_WATER));
343 return t.m4();
344}
345
353{
354 return HasTileWaterClass(t) && IsTileOnWater(t) && !IsCoastTile(t);
355}
356
363inline void SetDockingTile(Tile t, bool b)
364{
366 AssignBit(t.m1(), 7, b);
367}
368
373inline bool IsDockingTile(Tile t)
374{
376}
377
378
383inline void MakeShore(Tile t)
384{
388 SetDockingTile(t, false);
389 t.m2() = 0;
390 t.m3() = 0;
391 t.m4() = 0;
392 t.m5() = 0;
394 SB(t.m6(), 2, 4, 0);
395 t.m7() = 0;
396}
397
405inline void MakeWater(Tile t, Owner o, WaterClass wc, uint8_t random_bits)
406{
408 SetTileOwner(t, o);
409 SetWaterClass(t, wc);
410 SetDockingTile(t, false);
411 t.m2() = 0;
412 t.m3() = 0;
413 t.m4() = random_bits;
414 t.m5() = 0;
416 SB(t.m6(), 2, 4, 0);
417 t.m7() = 0;
418}
419
424inline void MakeSea(Tile t)
425{
427}
428
434inline void MakeRiver(Tile t, uint8_t random_bits)
435{
436 MakeWater(t, OWNER_WATER, WATER_CLASS_RIVER, random_bits);
437}
438
445inline void MakeCanal(Tile t, Owner o, uint8_t random_bits)
446{
447 assert(o != OWNER_WATER);
448 MakeWater(t, o, WATER_CLASS_CANAL, random_bits);
449}
450
460inline void MakeShipDepot(Tile t, Owner o, DepotID did, DepotPart part, Axis a, WaterClass original_water_class)
461{
463 SetTileOwner(t, o);
464 SetWaterClass(t, original_water_class);
465 SetDockingTile(t, false);
466 t.m2() = did.base();
467 t.m3() = 0;
468 t.m4() = 0;
469 t.m5() = part << WBL_DEPOT_PART | a << WBL_DEPOT_AXIS;
471 SB(t.m6(), 2, 4, 0);
472 t.m7() = 0;
473}
474
484inline void MakeLockTile(Tile t, Owner o, LockPart part, DiagDirection dir, WaterClass original_water_class)
485{
487 SetTileOwner(t, o);
488 SetWaterClass(t, original_water_class);
489 SetDockingTile(t, false);
490 t.m2() = 0;
491 t.m3() = 0;
492 t.m4() = 0;
493 t.m5() = part << WBL_LOCK_PART_BEGIN | dir << WBL_LOCK_ORIENT_BEGIN;
495 SB(t.m6(), 2, 4, 0);
496 t.m7() = 0;
497}
498
508inline void MakeLock(Tile t, Owner o, DiagDirection d, WaterClass wc_lower, WaterClass wc_upper, WaterClass wc_middle)
509{
511 Tile lower_tile = TileIndex(t) - delta;
512 Tile upper_tile = TileIndex(t) + delta;
513
514 /* Keep the current waterclass and owner for the tiles.
515 * It allows to restore them after the lock is deleted */
516 MakeLockTile(t, o, LOCK_PART_MIDDLE, d, wc_middle);
517 MakeLockTile(lower_tile, IsWaterTile(lower_tile) ? GetTileOwner(lower_tile) : o, LOCK_PART_LOWER, d, wc_lower);
518 MakeLockTile(upper_tile, IsWaterTile(upper_tile) ? GetTileOwner(upper_tile) : o, LOCK_PART_UPPER, d, wc_upper);
519}
520
526inline void SetNonFloodingWaterTile(Tile t, bool b)
527{
528 assert(IsTileType(t, MP_WATER));
529 AssignBit(t.m3(), 0, b);
530}
536{
537 assert(IsTileType(t, MP_WATER));
538 return HasBit(t.m3(), 0);
539}
540
541#endif /* WATER_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 AssignBit(T &x, const uint8_t y, bool value)
Assigns a bit in a variable.
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.
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 & 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 & m4()
General purpose.
Definition map_func.h:149
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
static constexpr Owner OWNER_WATER
The tile/execution is done by "water".
Header files for depots (not hangars)
DiagDirection XYNSToDiagDir(Axis xy, uint ns)
Convert an axis and a flag for north/south into a DiagDirection.
Axis
Allow incrementing of DiagDirDiff variables.
DiagDirection
Enumeration for diagonal directions.
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
#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
TileIndexDiff TileOffsByAxis(Axis axis)
Convert an Axis to a TileIndexDiff.
Definition map_func.h:554
TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition map_func.h:569
int32_t TileIndexDiff
An offset value between two tiles.
Definition map_type.h:23
Map writing/reading functions for tiles.
void SetTileType(Tile tile, TileType type)
Set the type of a tile.
Definition tile_map.h:131
Owner GetTileOwner(Tile tile)
Returns the owner of a tile.
Definition tile_map.h:178
void SetTileOwner(Tile tile, Owner owner)
Sets the owner of a tile.
Definition tile_map.h:198
static debug_inline bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
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
Tile got trees.
Definition tile_type.h:52
@ MP_STATION
A tile of a station.
Definition tile_type.h:53
@ MP_TUNNELBRIDGE
Tunnel entry/exit and bridge heads.
Definition tile_type.h:57
@ MP_WATER
Water tile.
Definition tile_type.h:54
@ MP_RAILWAY
A railway.
Definition tile_type.h:49
@ MP_INDUSTRY
Part of an industry.
Definition tile_type.h:56
@ MP_OBJECT
Contains objects such as transmitters and owned land.
Definition tile_type.h:58
void MakeShore(Tile t)
Helper function to make a coast tile.
Definition water_map.h:383
void SetWaterClass(Tile t, WaterClass wc)
Set the water class at a tile.
Definition water_map.h:126
bool HasTileWaterGround(Tile t)
Checks whether the tile has water at the ground.
Definition water_map.h:352
TileIndex GetShipDepotNorthTile(Tile t)
Get the most northern tile of a ship depot.
Definition water_map.h:291
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:508
void MakeWater(Tile t, Owner o, WaterClass wc, uint8_t random_bits)
Helper function for making a watery tile.
Definition water_map.h:405
DepotPart GetShipDepotPart(Tile t)
Get the part of a ship depot.
Definition water_map.h:257
bool IsTileOnWater(Tile t)
Tests if the tile was built on water.
Definition water_map.h:138
bool IsShipDepot(Tile t)
Is it a water tile with a ship depot on it?
Definition water_map.h:224
bool IsValidWaterClass(WaterClass wc)
Checks if a water class is valid.
Definition water_map.h:52
static constexpr uint8_t WBL_LOCK_PART_BEGIN
Start of lock part bitfield.
Definition water_map.h:24
bool IsRiver(Tile t)
Is it a river water tile?
Definition water_map.h:182
DiagDirection GetLockDirection(Tile t)
Get the direction of the water lock.
Definition water_map.h:316
static constexpr uint8_t WBL_TYPE_COUNT
Length of the 'type' bitfield.
Definition water_map.h:20
WaterClass
classes of water (for WATER_TILE_CLEAR water tile type).
Definition water_map.h:39
@ WATER_CLASS_SEA
Sea.
Definition water_map.h:40
@ WATER_CLASS_CANAL
Canal.
Definition water_map.h:41
@ WATER_CLASS_INVALID
Used for industry tiles on land (also for oilrig if newgrf says so).
Definition water_map.h:43
@ WATER_CLASS_RIVER
River.
Definition water_map.h:42
static constexpr uint8_t WBL_LOCK_ORIENT_BEGIN
Start of lock orientation bitfield.
Definition water_map.h:22
void MakeLockTile(Tile t, Owner o, LockPart part, DiagDirection dir, WaterClass original_water_class)
Make a lock section.
Definition water_map.h:484
bool HasTileWaterClass(Tile t)
Checks whether the tile has an waterclass associated.
Definition water_map.h:103
bool IsShipDepotTile(Tile t)
Is it a ship depot tile?
Definition water_map.h:234
bool IsCanal(Tile t)
Is it a canal tile?
Definition water_map.h:171
static constexpr uint8_t WBL_TYPE_BEGIN
Bit field layout of m5 for water tiles.
Definition water_map.h:19
bool IsCoast(Tile t)
Is it a coast tile?
Definition water_map.h:203
void MakeRiver(Tile t, uint8_t random_bits)
Make a river tile.
Definition water_map.h:434
WaterTileType GetWaterTileType(Tile t)
Get the water tile type of a tile.
Definition water_map.h:80
void SetNonFloodingWaterTile(Tile t, bool b)
Set the non-flooding water tile state of a tile.
Definition water_map.h:526
void SetWaterTileType(Tile t, WaterTileType type)
Set the water tile type of a tile.
Definition water_map.h:91
WaterClass GetWaterClass(Tile t)
Get the water class at a tile.
Definition water_map.h:114
static constexpr uint8_t WBL_LOCK_ORIENT_COUNT
Length of lock orientation bitfield.
Definition water_map.h:23
void MakeCanal(Tile t, Owner o, uint8_t random_bits)
Make a canal tile.
Definition water_map.h:445
static constexpr uint8_t WBL_DEPOT_AXIS
Depot axis flag.
Definition water_map.h:28
bool IsCoastTile(Tile t)
Is it a coast tile.
Definition water_map.h:213
static constexpr uint8_t WBL_DEPOT_PART
Depot part flag.
Definition water_map.h:27
TileIndex GetOtherShipDepotTile(Tile t)
Get the other tile of the ship depot.
Definition water_map.h:280
DepotPart
Sections of the water depot.
Definition water_map.h:58
@ DEPOT_PART_NORTH
Northern part of a depot.
Definition water_map.h:59
@ DEPOT_PART_SOUTH
Southern part of a depot.
Definition water_map.h:60
bool IsDockingTile(Tile t)
Checks whether the tile is marked as a dockling tile.
Definition water_map.h:373
WaterTileType
Available water tile types.
Definition water_map.h:31
@ WATER_TILE_COAST
Coast.
Definition water_map.h:33
@ WATER_TILE_LOCK
Water lock.
Definition water_map.h:34
@ WATER_TILE_DEPOT
Water Depot.
Definition water_map.h:35
@ WATER_TILE_CLEAR
Plain water.
Definition water_map.h:32
bool IsNonFloodingWaterTile(Tile t)
Checks whether the tile is marked as a non-flooding water tile.
Definition water_map.h:535
void SetDockingTile(Tile t, bool b)
Set the docking tile state of a tile.
Definition water_map.h:363
LockPart
Sections of the water lock.
Definition water_map.h:65
@ LOCK_PART_UPPER
Upper part of a lock.
Definition water_map.h:68
@ LOCK_PART_MIDDLE
Middle part of a lock.
Definition water_map.h:66
@ LOCK_PART_LOWER
Lower part of a lock.
Definition water_map.h:67
bool IsWater(Tile t)
Is it a plain water tile?
Definition water_map.h:149
bool IsWaterTile(Tile t)
Is it a water tile with plain water?
Definition water_map.h:192
bool IsLock(Tile t)
Is there a lock on a given water tile?
Definition water_map.h:305
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:460
uint8_t GetWaterTileRandomBits(Tile t)
Get the random bits of the water tile.
Definition water_map.h:340
void MakeSea(Tile t)
Make a sea tile.
Definition water_map.h:424
DiagDirection GetShipDepotDirection(Tile t)
Get the direction of the ship depot.
Definition water_map.h:269
bool IsSea(Tile t)
Is it a sea water tile?
Definition water_map.h:160
LockPart GetLockPart(Tile t)
Get the part of a lock.
Definition water_map.h:328
static constexpr uint8_t WBL_LOCK_PART_COUNT
Length of lock part bitfield.
Definition water_map.h:25
Axis GetShipDepotAxis(Tile t)
Get the axis of the ship depot.
Definition water_map.h:245