OpenTTD Source 20251213-master-g1091fa6071
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
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,
36};
37
39enum class WaterClass : uint8_t {
40 Sea = 0,
41 Canal = 1,
42 River = 2,
43 Invalid = 3,
44};
45
53{
54 return wc < WaterClass::Invalid;
55}
56
58enum class DepotPart : uint8_t {
59 North = 0,
60 South = 1,
61 End,
62};
63
65enum class LockPart : uint8_t {
66 Middle = 0,
67 Lower = 1,
68 Upper = 2,
69 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 static_cast<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, to_underlying(wc));
130}
131
138inline bool IsTileOnWater(Tile t)
139{
140 return (GetWaterClass(t) != WaterClass::Invalid);
141}
142
149inline bool IsWater(Tile t)
150{
152}
153
160inline bool IsSea(Tile t)
161{
162 return IsWater(t) && GetWaterClass(t) == WaterClass::Sea;
163}
164
171inline bool IsCanal(Tile t)
172{
173 return IsWater(t) && GetWaterClass(t) == WaterClass::Canal;
174}
175
182inline bool IsRiver(Tile t)
183{
184 return IsWater(t) && GetWaterClass(t) == WaterClass::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 static_cast<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, 6, 0);
395 t.m7() = 0;
396 t.m8() = 0;
397}
398
406inline void MakeWater(Tile t, Owner o, WaterClass wc, uint8_t random_bits)
407{
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;
417 SB(t.m6(), 2, 6, 0);
418 t.m7() = 0;
419 t.m8() = 0;
420}
421
426inline void MakeSea(Tile t)
427{
429}
430
436inline void MakeRiver(Tile t, uint8_t random_bits)
437{
438 MakeWater(t, OWNER_WATER, WaterClass::River, random_bits);
439}
440
447inline 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
462inline void MakeShipDepot(Tile t, Owner o, DepotID did, DepotPart part, Axis a, WaterClass original_water_class)
463{
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;
473 SB(t.m6(), 2, 6, 0);
474 t.m7() = 0;
475 t.m8() = 0;
476}
477
487inline void MakeLockTile(Tile t, Owner o, LockPart part, DiagDirection dir, WaterClass original_water_class)
488{
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;
498 SB(t.m6(), 2, 6, 0);
499 t.m7() = 0;
500 t.m8() = 0;
501}
502
512inline void MakeLock(Tile t, Owner o, DiagDirection d, WaterClass wc_lower, WaterClass wc_upper, WaterClass wc_middle)
513{
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
530inline void SetNonFloodingWaterTile(Tile t, bool b)
531{
532 assert(IsTileType(t, MP_WATER));
533 AssignBit(t.m3(), 0, b);
534}
540{
541 assert(IsTileType(t, MP_WATER));
542 return HasBit(t.m3(), 0);
543}
544
545#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:161
uint8_t & m1()
Primarily used for ownership information.
Definition map_func.h:113
uint8_t & m4()
General purpose.
Definition map_func.h:149
uint8_t & m6()
General purpose.
Definition map_func.h:173
uint8_t & m7()
Primarily used for newgrf support.
Definition map_func.h:185
uint8_t & m3()
General purpose.
Definition map_func.h:137
uint16_t & m8()
General purpose.
Definition map_func.h:197
uint16_t & m2()
Primarily used for indices to towns, industries and stations.
Definition map_func.h:125
@ End
End marker.
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:567
TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition map_func.h:582
int32_t TileIndexDiff
An offset value between two tiles.
Definition map_type.h:23
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: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:512
void MakeWater(Tile t, Owner o, WaterClass wc, uint8_t random_bits)
Helper function for making a watery tile.
Definition water_map.h:406
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 WaterTileType::Clear water tile type).
Definition water_map.h:39
@ River
River.
@ Invalid
Used for industry tiles on land (also for oilrig if newgrf says so).
@ Canal
Canal.
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:487
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:436
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:530
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:447
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
@ South
Southern part of a depot.
@ North
Northern part of a depot.
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
@ Depot
Water Depot.
@ Lock
Water lock.
@ Clear
Plain water.
bool IsNonFloodingWaterTile(Tile t)
Checks whether the tile is marked as a non-flooding water tile.
Definition water_map.h:539
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
@ Upper
Upper part of a lock.
@ Middle
Middle part of a lock.
@ Lower
Lower part of a lock.
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:462
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:426
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