OpenTTD Source 20260911-master-gee2b2ac12a
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
9
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
31enum class WaterTileType : uint8_t {
32 Clear = 0,
33 Coast = 1,
34 Lock = 2,
35 Depot = 3,
38};
39
41enum class WaterClass : uint8_t {
42 Sea = 0,
43 Canal = 1,
44 River = 2,
45 Invalid = 3,
46};
47
55{
56 return wc < WaterClass::Invalid;
57}
58
60enum class DepotPart : uint8_t {
61 North = 0,
62 South = 1,
64};
65
67enum class LockPart : uint8_t {
68 Middle = 0,
69 Lower = 1,
70 Upper = 2,
72};
74
76
83{
84 assert(IsTileType(t, TileType::Water));
85 return static_cast<WaterTileType>(GB(t.m5(), WBL_TYPE_BEGIN, WBL_TYPE_COUNT));
86}
87
94{
95 assert(IsTileType(t, TileType::Water));
97}
98
109
117{
118 assert(HasTileWaterClass(t));
119 return static_cast<WaterClass>(GB(t.m1(), 5, 2));
120}
121
128inline void SetWaterClass(Tile t, WaterClass wc)
129{
130 assert(HasTileWaterClass(t));
131 SB(t.m1(), 5, 2, to_underlying(wc));
132}
133
140inline bool IsTileOnWater(Tile t)
141{
142 return (GetWaterClass(t) != WaterClass::Invalid);
143}
144
151inline bool IsWater(Tile t)
152{
154}
155
162inline bool IsSea(Tile t)
163{
164 return IsWater(t) && GetWaterClass(t) == WaterClass::Sea;
165}
166
173inline bool IsCanal(Tile t)
174{
175 return IsWater(t) && GetWaterClass(t) == WaterClass::Canal;
176}
177
184inline bool IsRiver(Tile t)
185{
186 return IsWater(t) && GetWaterClass(t) == WaterClass::River;
187}
188
194inline bool IsWaterTile(Tile t)
195{
196 return IsTileType(t, TileType::Water) && IsWater(t);
197}
198
205inline bool IsCoast(Tile t)
206{
208 return wtt == WaterTileType::Coast || wtt == WaterTileType::CoastRocks;
209}
210
216inline bool IsCoastTile(Tile t)
217{
219}
220
227inline bool IsShipDepot(Tile t)
228{
230}
231
237inline bool IsShipDepotTile(Tile t)
238{
239 return IsTileType(t, TileType::Water) && IsShipDepot(t);
240}
241
249{
250 assert(IsShipDepotTile(t));
251 return static_cast<Axis>(GB(t.m5(), WBL_DEPOT_AXIS, 1));
252}
253
261{
262 assert(IsShipDepotTile(t));
263 return static_cast<DepotPart>(GB(t.m5(), WBL_DEPOT_PART, 1));
264}
265
276
287
295{
296 assert(IsShipDepot(t));
298
299 return t < tile2 ? TileIndex(t) : tile2;
300}
301
308inline bool IsLock(Tile t)
309{
311}
312
320{
321 assert(IsLock(t));
323}
324
332{
333 assert(IsLock(t));
334 return static_cast<LockPart>(GB(t.m5(), WBL_LOCK_PART_BEGIN, WBL_LOCK_PART_COUNT));
335}
336
344{
345 assert(IsTileType(t, TileType::Water));
346 return t.m4();
347}
348
357{
358 return HasTileWaterClass(t) && IsTileOnWater(t) && !IsCoastTile(t);
359}
360
367inline void SetDockingTile(Tile t, bool b)
368{
370 AssignBit(t.m1(), 7, b);
371}
372
382
383
389inline void MakeShore(Tile t, bool rocks = false)
390{
394 SetDockingTile(t, false);
395 t.m2() = 0;
396 t.m3() = 0;
397 t.m4() = 0;
398 t.m5() = 0;
400 SB(t.m6(), 2, 6, 0);
401 t.m7() = 0;
402 t.m8() = 0;
403}
404
413inline void MakeWater(Tile t, Owner o, WaterClass wc, uint8_t random_bits, bool rocks = false)
414{
416 SetTileOwner(t, o);
417 SetWaterClass(t, wc);
418 SetDockingTile(t, false);
419 t.m2() = 0;
420 t.m3() = 0;
421 t.m4() = random_bits;
422 t.m5() = 0;
424 SB(t.m6(), 2, 6, 0);
425 t.m7() = 0;
426 t.m8() = 0;
427}
428
434inline void MakeSea(Tile t, bool rocks = false)
435{
437}
438
444inline void MakeRiver(Tile t, uint8_t random_bits)
445{
446 MakeWater(t, OWNER_WATER, WaterClass::River, random_bits);
447}
448
455inline void MakeCanal(Tile t, Owner o, uint8_t random_bits)
456{
457 assert(o != OWNER_WATER);
458 MakeWater(t, o, WaterClass::Canal, random_bits);
459}
460
470inline void MakeShipDepot(Tile t, Owner o, DepotID did, DepotPart part, Axis a, WaterClass original_water_class)
471{
473 SetTileOwner(t, o);
474 SetWaterClass(t, original_water_class);
475 SetDockingTile(t, false);
476 t.m2() = did.base();
477 t.m3() = 0;
478 t.m4() = 0;
481 SB(t.m6(), 2, 6, 0);
482 t.m7() = 0;
483 t.m8() = 0;
484}
485
495inline void MakeLockTile(Tile t, Owner o, LockPart part, DiagDirection dir, WaterClass original_water_class)
496{
498 SetTileOwner(t, o);
499 SetWaterClass(t, original_water_class);
500 SetDockingTile(t, false);
501 t.m2() = 0;
502 t.m3() = 0;
503 t.m4() = 0;
506 SB(t.m6(), 2, 6, 0);
507 t.m7() = 0;
508 t.m8() = 0;
509}
510
520inline void MakeLock(Tile t, Owner o, DiagDirection d, WaterClass wc_lower, WaterClass wc_upper, WaterClass wc_middle)
521{
523 Tile lower_tile = TileIndex(t) - delta;
524 Tile upper_tile = TileIndex(t) + delta;
525
526 /* Keep the current waterclass and owner for the tiles.
527 * It allows to restore them after the lock is deleted */
528 MakeLockTile(t, o, LockPart::Middle, d, wc_middle);
529 MakeLockTile(lower_tile, IsWaterTile(lower_tile) ? GetTileOwner(lower_tile) : o, LockPart::Lower, d, wc_lower);
530 MakeLockTile(upper_tile, IsWaterTile(upper_tile) ? GetTileOwner(upper_tile) : o, LockPart::Upper, d, wc_upper);
531}
532
538inline void SetNonFloodingWaterTile(Tile t, bool b)
539{
540 assert(IsTileType(t, TileType::Water));
541 AssignBit(t.m3(), 0, b);
542}
543
549{
550 assert(IsTileType(t, TileType::Water));
551 return HasBit(t.m3(), 0);
552}
553
554#endif /* WATER_MAP_H */
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.
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Wrapper class to abstract away the way the tiles are stored.
Definition map_func.h:25
uint8_t & m5()
General purpose.
Definition map_func.h:156
uint8_t & m1()
Primarily used for ownership information.
Definition map_func.h:112
uint8_t & m4()
General purpose.
Definition map_func.h:145
uint8_t & m6()
General purpose.
Definition map_func.h:167
uint8_t & m7()
Primarily used for newgrf support.
Definition map_func.h:178
uint8_t & m3()
General purpose.
Definition map_func.h:134
uint16_t & m8()
General purpose.
Definition map_func.h:189
uint16_t & m2()
Primarily used for indices to towns, industries and stations.
Definition map_func.h:123
static constexpr Owner OWNER_WATER
The tile/execution is done by "water".
Header files for depots (not hangars).
PoolID< uint16_t, struct DepotIDTag, 64000, 0xFFFF > DepotID
Type for the unique identifier of depots.
Definition depot_type.h:15
DiagDirection XYNSToDiagDir(Axis xy, uint ns)
Convert an axis and a flag for north/south into a DiagDirection.
Axis
Enumeration for the two axis X and Y.
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:21
#define DECLARE_INCREMENT_DECREMENT_OPERATORS(enum_type)
For some enums it is useful to have pre/post increment/decrement operators.
Definition enum_type.hpp:86
TileIndexDiff TileOffsByAxis(Axis axis)
Convert an Axis to a TileIndexDiff.
Definition map_func.h:559
TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition map_func.h:574
int32_t TileIndexDiff
An offset value between two tiles.
Definition map_type.h:23
@ Invalid
broken savegame (used internally)
Definition saveload.h:435
Map writing/reading functions for tiles.
static bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
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
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:92
@ TunnelBridge
Tunnel entry/exit and bridge heads.
Definition tile_type.h:58
@ Water
Water tile.
Definition tile_type.h:55
@ Station
A tile of a station or airport.
Definition tile_type.h:54
@ Object
Contains objects such as transmitters and owned land.
Definition tile_type.h:59
@ Industry
Part of an industry.
Definition tile_type.h:57
@ Railway
A tile with railway.
Definition tile_type.h:50
@ Trees
Tile with one or more trees.
Definition tile_type.h:53
void SetWaterClass(Tile t, WaterClass wc)
Set the water class at a tile.
Definition water_map.h:128
bool HasTileWaterGround(Tile t)
Checks whether the tile has water at the ground.
Definition water_map.h:356
TileIndex GetShipDepotNorthTile(Tile t)
Get the most northern tile of a ship depot.
Definition water_map.h:294
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:520
void MakeWater(Tile t, Owner o, WaterClass wc, uint8_t random_bits, bool rocks=false)
Helper function for making a watery tile.
Definition water_map.h:413
DepotPart GetShipDepotPart(Tile t)
Get the part of a ship depot.
Definition water_map.h:260
bool IsTileOnWater(Tile t)
Tests if the tile was built on water.
Definition water_map.h:140
bool IsShipDepot(Tile t)
Is it a water tile with a ship depot on it?
Definition water_map.h:227
bool IsValidWaterClass(WaterClass wc)
Checks if a water class is valid.
Definition water_map.h:54
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:184
DiagDirection GetLockDirection(Tile t)
Get the direction of the water lock.
Definition water_map.h:319
static constexpr uint8_t WBL_TYPE_COUNT
Length of the 'type' bitfield.
Definition water_map.h:20
WaterClass
classes of water (for WaterTileType::Clear water tile type).
Definition water_map.h:41
@ River
River.
Definition water_map.h:44
@ Invalid
Used for industry tiles on land (also for oilrig if newgrf says so).
Definition water_map.h:45
@ Canal
Canal.
Definition water_map.h:43
@ Sea
Sea.
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:495
bool HasTileWaterClass(Tile t)
Checks whether the tile has an waterclass associated.
Definition water_map.h:105
bool IsShipDepotTile(Tile t)
Is it a ship depot tile?
Definition water_map.h:237
bool IsCanal(Tile t)
Is it a canal tile?
Definition water_map.h:173
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:205
void MakeRiver(Tile t, uint8_t random_bits)
Make a river tile.
Definition water_map.h:444
WaterTileType GetWaterTileType(Tile t)
Get the water tile type of a tile.
Definition water_map.h:82
void SetNonFloodingWaterTile(Tile t, bool b)
Set the non-flooding water tile state of a tile.
Definition water_map.h:538
void SetWaterTileType(Tile t, WaterTileType type)
Set the water tile type of a tile.
Definition water_map.h:93
WaterClass GetWaterClass(Tile t)
Get the water class at a tile.
Definition water_map.h:116
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:455
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:216
static constexpr uint8_t WBL_DEPOT_PART
Depot part flag.
Definition water_map.h:27
bool IsPossibleDockingTile(Tile t)
Check whether it is feasible that the given tile could be a docking tile.
TileIndex GetOtherShipDepotTile(Tile t)
Get the other tile of the ship depot.
Definition water_map.h:283
void MakeShore(Tile t, bool rocks=false)
Helper function to make a coast tile.
Definition water_map.h:389
DepotPart
Sections of the water depot.
Definition water_map.h:60
@ South
Southern part of a depot.
Definition water_map.h:62
@ North
Northern part of a depot.
Definition water_map.h:61
@ End
End marker.
Definition water_map.h:63
bool IsDockingTile(Tile t)
Checks whether the tile is marked as a dockling tile.
Definition water_map.h:378
WaterTileType
Available water tile types.
Definition water_map.h:31
@ ClearRocks
Rocks on water.
Definition water_map.h:36
@ Coast
Coast.
Definition water_map.h:33
@ CoastRocks
Rocks on coast.
Definition water_map.h:37
@ Depot
Water Depot.
Definition water_map.h:35
@ Lock
Water lock.
Definition water_map.h:34
@ 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:548
void SetDockingTile(Tile t, bool b)
Set the docking tile state of a tile.
Definition water_map.h:367
LockPart
Sections of the water lock.
Definition water_map.h:67
@ Upper
Upper part of a lock.
Definition water_map.h:70
@ Middle
Middle part of a lock.
Definition water_map.h:68
@ Lower
Lower part of a lock.
Definition water_map.h:69
bool IsWater(Tile t)
Is it a plain water tile?
Definition water_map.h:151
bool IsWaterTile(Tile t)
Is it a water tile with plain water?
Definition water_map.h:194
bool IsLock(Tile t)
Is there a lock on a given water tile?
Definition water_map.h:308
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:470
uint8_t GetWaterTileRandomBits(Tile t)
Get the random bits of the water tile.
Definition water_map.h:343
DiagDirection GetShipDepotDirection(Tile t)
Get the direction of the ship depot.
Definition water_map.h:272
bool IsSea(Tile t)
Is it a sea water tile?
Definition water_map.h:162
void MakeSea(Tile t, bool rocks=false)
Make a sea tile.
Definition water_map.h:434
LockPart GetLockPart(Tile t)
Get the part of a lock.
Definition water_map.h:331
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:248