OpenTTD Source  1.11.2
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 
22 
24  WBL_TYPE_LOCK = 0x1,
26 
28 
33 
36 };
37 
44 };
45 
47 enum WaterClass {
52 };
54 template <> struct EnumPropsT<WaterClass> : MakeEnumPropsT<WaterClass, byte, WATER_CLASS_SEA, WATER_CLASS_INVALID, WATER_CLASS_INVALID, 2> {};
55 
57 enum DepotPart {
60  DEPOT_PART_END
61 };
62 
64 enum LockPart {
68 };
69 
70 bool IsPossibleDockingTile(TileIndex t);
71 
78 {
79  assert(IsTileType(t, MP_WATER));
80 
81  switch (GB(_m[t].m5, WBL_TYPE_BEGIN, WBL_TYPE_COUNT)) {
83  case WBL_TYPE_LOCK: return WATER_TILE_LOCK;
84  case WBL_TYPE_DEPOT: return WATER_TILE_DEPOT;
85  default: NOT_REACHED();
86  }
87 }
88 
95 static inline bool HasTileWaterClass(TileIndex t)
96 {
98 }
99 
107 {
108  assert(HasTileWaterClass(t));
109  return (WaterClass)GB(_m[t].m1, 5, 2);
110 }
111 
118 static inline void SetWaterClass(TileIndex t, WaterClass wc)
119 {
120  assert(HasTileWaterClass(t));
121  SB(_m[t].m1, 5, 2, wc);
122 }
123 
130 static inline bool IsTileOnWater(TileIndex t)
131 {
132  return (GetWaterClass(t) != WATER_CLASS_INVALID);
133 }
134 
141 static inline bool IsWater(TileIndex t)
142 {
143  return GetWaterTileType(t) == WATER_TILE_CLEAR;
144 }
145 
152 static inline bool IsSea(TileIndex t)
153 {
154  return IsWater(t) && GetWaterClass(t) == WATER_CLASS_SEA;
155 }
156 
163 static inline bool IsCanal(TileIndex t)
164 {
165  return IsWater(t) && GetWaterClass(t) == WATER_CLASS_CANAL;
166 }
167 
174 static inline bool IsRiver(TileIndex t)
175 {
176  return IsWater(t) && GetWaterClass(t) == WATER_CLASS_RIVER;
177 }
178 
184 static inline bool IsWaterTile(TileIndex t)
185 {
186  return IsTileType(t, MP_WATER) && IsWater(t);
187 }
188 
195 static inline bool IsCoast(TileIndex t)
196 {
197  return GetWaterTileType(t) == WATER_TILE_COAST;
198 }
199 
205 static inline bool IsCoastTile(TileIndex t)
206 {
207  return (IsTileType(t, MP_WATER) && IsCoast(t)) || (IsTileType(t, MP_TREES) && GetWaterClass(t) != WATER_CLASS_INVALID);
208 }
209 
216 static inline bool IsShipDepot(TileIndex t)
217 {
218  return GetWaterTileType(t) == WATER_TILE_DEPOT;
219 }
220 
226 static inline bool IsShipDepotTile(TileIndex t)
227 {
228  return IsTileType(t, MP_WATER) && IsShipDepot(t);
229 }
230 
238 {
239  assert(IsShipDepotTile(t));
240  return (Axis)GB(_m[t].m5, WBL_DEPOT_AXIS, 1);
241 }
242 
250 {
251  assert(IsShipDepotTile(t));
252  return (DepotPart)GB(_m[t].m5, WBL_DEPOT_PART, 1);
253 }
254 
262 {
264 }
265 
273 {
274  return t + (GetShipDepotPart(t) != DEPOT_PART_NORTH ? -1 : 1) * (GetShipDepotAxis(t) != AXIS_X ? TileDiffXY(0, 1) : TileDiffXY(1, 0));
275 }
276 
284 {
285  assert(IsShipDepot(t));
286  TileIndex tile2 = GetOtherShipDepotTile(t);
287 
288  return t < tile2 ? t : tile2;
289 }
290 
297 static inline bool IsLock(TileIndex t)
298 {
299  return GetWaterTileType(t) == WATER_TILE_LOCK;
300 }
301 
309 {
310  assert(IsLock(t));
312 }
313 
320 static inline byte GetLockPart(TileIndex t)
321 {
322  assert(IsLock(t));
324 }
325 
332 static inline byte GetWaterTileRandomBits(TileIndex t)
333 {
334  assert(IsTileType(t, MP_WATER));
335  return _m[t].m4;
336 }
337 
344 static inline bool HasTileWaterGround(TileIndex t)
345 {
346  return HasTileWaterClass(t) && IsTileOnWater(t) && !IsCoastTile(t);
347 }
348 
355 static inline void SetDockingTile(TileIndex t, bool b)
356 {
358  SB(_m[t].m1, 7, 1, b ? 1 : 0);
359 }
360 
365 static inline bool IsDockingTile(TileIndex t)
366 {
367  return (IsTileType(t, MP_WATER) || IsTileType(t, MP_RAILWAY) || IsTileType(t, MP_STATION) || IsTileType(t, MP_TUNNELBRIDGE)) && HasBit(_m[t].m1, 7);
368 }
369 
370 
375 static inline void MakeShore(TileIndex t)
376 {
377  SetTileType(t, MP_WATER);
380  SetDockingTile(t, false);
381  _m[t].m2 = 0;
382  _m[t].m3 = 0;
383  _m[t].m4 = 0;
385  SB(_me[t].m6, 2, 4, 0);
386  _me[t].m7 = 0;
387 }
388 
396 static inline void MakeWater(TileIndex t, Owner o, WaterClass wc, uint8 random_bits)
397 {
398  SetTileType(t, MP_WATER);
399  SetTileOwner(t, o);
400  SetWaterClass(t, wc);
401  SetDockingTile(t, false);
402  _m[t].m2 = 0;
403  _m[t].m3 = 0;
404  _m[t].m4 = random_bits;
406  SB(_me[t].m6, 2, 4, 0);
407  _me[t].m7 = 0;
408 }
409 
414 static inline void MakeSea(TileIndex t)
415 {
417 }
418 
424 static inline void MakeRiver(TileIndex t, uint8 random_bits)
425 {
426  MakeWater(t, OWNER_WATER, WATER_CLASS_RIVER, random_bits);
427 }
428 
435 static inline void MakeCanal(TileIndex t, Owner o, uint8 random_bits)
436 {
437  assert(o != OWNER_WATER);
438  MakeWater(t, o, WATER_CLASS_CANAL, random_bits);
439 }
440 
450 static inline void MakeShipDepot(TileIndex t, Owner o, DepotID did, DepotPart part, Axis a, WaterClass original_water_class)
451 {
452  SetTileType(t, MP_WATER);
453  SetTileOwner(t, o);
454  SetWaterClass(t, original_water_class);
455  SetDockingTile(t, false);
456  _m[t].m2 = did;
457  _m[t].m3 = 0;
458  _m[t].m4 = 0;
460  SB(_me[t].m6, 2, 4, 0);
461  _me[t].m7 = 0;
462 }
463 
473 static inline void MakeLockTile(TileIndex t, Owner o, LockPart part, DiagDirection dir, WaterClass original_water_class)
474 {
475  SetTileType(t, MP_WATER);
476  SetTileOwner(t, o);
477  SetWaterClass(t, original_water_class);
478  SetDockingTile(t, false);
479  _m[t].m2 = 0;
480  _m[t].m3 = 0;
481  _m[t].m4 = 0;
483  SB(_me[t].m6, 2, 4, 0);
484  _me[t].m7 = 0;
485 }
486 
496 static inline void MakeLock(TileIndex t, Owner o, DiagDirection d, WaterClass wc_lower, WaterClass wc_upper, WaterClass wc_middle)
497 {
498  TileIndexDiff delta = TileOffsByDiagDir(d);
499 
500  /* Keep the current waterclass and owner for the tiles.
501  * It allows to restore them after the lock is deleted */
502  MakeLockTile(t, o, LOCK_PART_MIDDLE, d, wc_middle);
503  MakeLockTile(t - delta, IsWaterTile(t - delta) ? GetTileOwner(t - delta) : o, LOCK_PART_LOWER, d, wc_lower);
504  MakeLockTile(t + delta, IsWaterTile(t + delta) ? GetTileOwner(t + delta) : o, LOCK_PART_UPPER, d, wc_upper);
505 }
506 
507 #endif /* WATER_MAP_H */
WBL_TYPE_COUNT
@ WBL_TYPE_COUNT
Length of the 'type' bitfield.
Definition: water_map.h:21
LOCK_PART_UPPER
@ LOCK_PART_UPPER
Upper part of a lock.
Definition: water_map.h:67
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:83
IsSea
static bool IsSea(TileIndex t)
Is it a sea water tile?
Definition: water_map.h:152
TileOffsByDiagDir
static TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition: map_func.h:341
IsCanal
static bool IsCanal(TileIndex t)
Is it a canal tile?
Definition: water_map.h:163
GetOtherShipDepotTile
static TileIndex GetOtherShipDepotTile(TileIndex t)
Get the other tile of the ship depot.
Definition: water_map.h:272
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
HasTileWaterClass
static bool HasTileWaterClass(TileIndex t)
Checks whether the tile has an waterclass associated.
Definition: water_map.h:95
WBL_TYPE_LOCK
@ WBL_TYPE_LOCK
Lock ('type' bitfield).
Definition: water_map.h:24
SetTileType
static void SetTileType(TileIndex tile, TileType type)
Set the type of a tile.
Definition: tile_map.h:131
GetLockDirection
static DiagDirection GetLockDirection(TileIndex t)
Get the direction of the water lock.
Definition: water_map.h:308
WBL_LOCK_ORIENT_COUNT
@ WBL_LOCK_ORIENT_COUNT
Length of lock orientation bitfield.
Definition: water_map.h:30
MakeSea
static void MakeSea(TileIndex t)
Make a sea tile.
Definition: water_map.h:414
DepotID
uint16 DepotID
Type for the unique identifier of depots.
Definition: depot_type.h:13
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
_me
TileExtended * _me
Extended Tiles of the map.
Definition: map.cpp:31
LockPart
LockPart
Sections of the water lock.
Definition: water_map.h:64
WBL_TYPE_NORMAL
@ WBL_TYPE_NORMAL
Clear water or coast ('type' bitfield).
Definition: water_map.h:23
MP_RAILWAY
@ MP_RAILWAY
A railway.
Definition: tile_type.h:47
Tile::m2
uint16 m2
Primarily used for indices to towns, industries and stations.
Definition: map_type.h:20
WaterClass
WaterClass
classes of water (for WATER_TILE_CLEAR water tile type).
Definition: water_map.h:47
IsCoastTile
static bool IsCoastTile(TileIndex t)
Is it a coast tile.
Definition: water_map.h:205
WBL_LOCK_ORIENT_BEGIN
@ WBL_LOCK_ORIENT_BEGIN
Start of lock orientation bitfield.
Definition: water_map.h:29
IsCoast
static bool IsCoast(TileIndex t)
Is it a coast tile?
Definition: water_map.h:195
MP_INDUSTRY
@ MP_INDUSTRY
Part of an industry.
Definition: tile_type.h:54
WATER_CLASS_RIVER
@ WATER_CLASS_RIVER
River.
Definition: water_map.h:50
WATER_TILE_DEPOT
@ WATER_TILE_DEPOT
Water Depot.
Definition: water_map.h:43
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
GetShipDepotDirection
static DiagDirection GetShipDepotDirection(TileIndex t)
Get the direction of the ship depot.
Definition: water_map.h:261
GetLockPart
static byte GetLockPart(TileIndex t)
Get the part of a lock.
Definition: water_map.h:320
DEPOT_PART_NORTH
@ DEPOT_PART_NORTH
Northern part of a depot.
Definition: water_map.h:58
IsTileOnWater
static bool IsTileOnWater(TileIndex t)
Tests if the tile was built on water.
Definition: water_map.h:130
GetShipDepotNorthTile
static TileIndex GetShipDepotNorthTile(TileIndex t)
Get the most northern tile of a ship depot.
Definition: water_map.h:283
WATER_TILE_COAST
@ WATER_TILE_COAST
Coast.
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:51
EnumPropsT
Informative template class exposing basic enumeration properties used by several other templates belo...
Definition: enum_type.hpp:48
tile_map.h
HasTileWaterGround
static bool HasTileWaterGround(TileIndex t)
Checks whether the tile has water at the ground.
Definition: water_map.h:344
WATER_TILE_CLEAR
@ WATER_TILE_CLEAR
Plain water.
Definition: water_map.h:40
MakeLock
static void MakeLock(TileIndex t, Owner o, DiagDirection d, WaterClass wc_lower, WaterClass wc_upper, WaterClass wc_middle)
Make a water lock.
Definition: water_map.h:496
XYNSToDiagDir
static DiagDirection XYNSToDiagDir(Axis xy, uint ns)
Convert an axis and a flag for north/south into a DiagDirection.
Definition: direction_func.h:261
TileIndexDiff
int32 TileIndexDiff
An offset value between to tiles.
Definition: map_func.h:154
MP_OBJECT
@ MP_OBJECT
Contains objects such as transmitters and owned land.
Definition: tile_type.h:56
SB
static T SB(T &x, const uint8 s, const uint8 n, const U d)
Set n bits in x starting at bit s to d.
Definition: bitmath_func.hpp:58
MP_WATER
@ MP_WATER
Water tile.
Definition: tile_type.h:52
IsShipDepot
static bool IsShipDepot(TileIndex t)
Is it a water tile with a ship depot on it?
Definition: water_map.h:216
LOCK_PART_MIDDLE
@ LOCK_PART_MIDDLE
Middle part of a lock.
Definition: water_map.h:65
WBL_TYPE_DEPOT
@ WBL_TYPE_DEPOT
Depot ('type' bitfield).
Definition: water_map.h:25
SetDockingTile
static void SetDockingTile(TileIndex t, bool b)
Set the docking tile state of a tile.
Definition: water_map.h:355
LOCK_PART_LOWER
@ LOCK_PART_LOWER
Lower part of a lock.
Definition: water_map.h:66
WaterTileTypeBitLayout
WaterTileTypeBitLayout
Bit field layout of m5 for water tiles.
Definition: water_map.h:19
MP_TUNNELBRIDGE
@ MP_TUNNELBRIDGE
Tunnel entry/exit and bridge heads.
Definition: tile_type.h:55
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition: direction_type.h:77
MakeLockTile
static void MakeLockTile(TileIndex t, Owner o, LockPart part, DiagDirection dir, WaterClass original_water_class)
Make a lock section.
Definition: water_map.h:473
WATER_CLASS_CANAL
@ WATER_CLASS_CANAL
Canal.
Definition: water_map.h:49
GetShipDepotPart
static DepotPart GetShipDepotPart(TileIndex t)
Get the part of a ship depot.
Definition: water_map.h:249
IsTileType
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
GetTileOwner
static Owner GetTileOwner(TileIndex tile)
Returns the owner of a tile.
Definition: tile_map.h:178
MP_TREES
@ MP_TREES
Tile got trees.
Definition: tile_type.h:50
WBL_COAST_FLAG
@ WBL_COAST_FLAG
Flag for coast.
Definition: water_map.h:27
DEPOT_PART_SOUTH
@ DEPOT_PART_SOUTH
Southern part of a depot.
Definition: water_map.h:59
Tile::m5
byte m5
General purpose.
Definition: map_type.h:24
WaterTileType
WaterTileType
Available water tile types.
Definition: water_map.h:39
GetWaterClass
static WaterClass GetWaterClass(TileIndex t)
Get the water class at a tile.
Definition: water_map.h:106
GetWaterTileType
static WaterTileType GetWaterTileType(TileIndex t)
Get the water tile type at a tile.
Definition: water_map.h:77
GetWaterTileRandomBits
static byte GetWaterTileRandomBits(TileIndex t)
Get the random bits of the water tile.
Definition: water_map.h:332
MakeShipDepot
static void MakeShipDepot(TileIndex t, Owner o, DepotID did, DepotPart part, Axis a, WaterClass original_water_class)
Make a ship depot section.
Definition: water_map.h:450
IsWaterTile
static bool IsWaterTile(TileIndex t)
Is it a water tile with plain water?
Definition: water_map.h:184
depot_type.h
MakeShore
static void MakeShore(TileIndex t)
Helper function to make a coast tile.
Definition: water_map.h:375
TileDiffXY
static TileIndexDiff TileDiffXY(int x, int y)
Calculates an offset for the given coordinate(-offset).
Definition: map_func.h:179
WBL_LOCK_PART_COUNT
@ WBL_LOCK_PART_COUNT
Length of lock part bitfield.
Definition: water_map.h:32
MP_STATION
@ MP_STATION
A tile of a station.
Definition: tile_type.h:51
IsLock
static bool IsLock(TileIndex t)
Is there a lock on a given water tile?
Definition: water_map.h:297
GetShipDepotAxis
static Axis GetShipDepotAxis(TileIndex t)
Get the axis of the ship depot.
Definition: water_map.h:237
IsRiver
static bool IsRiver(TileIndex t)
Is it a river water tile?
Definition: water_map.h:174
MakeEnumPropsT
Helper template class that makes basic properties of given enumeration type visible from outsize.
Definition: enum_type.hpp:62
IsDockingTile
static bool IsDockingTile(TileIndex t)
Checks whether the tile is marked as a dockling tile.
Definition: water_map.h:365
MakeCanal
static void MakeCanal(TileIndex t, Owner o, uint8 random_bits)
Make a canal tile.
Definition: water_map.h:435
TileExtended::m7
byte m7
Primarily used for newgrf support.
Definition: map_type.h:35
Axis
Axis
Allow incrementing of DiagDirDiff variables.
Definition: direction_type.h:123
SetTileOwner
static void SetTileOwner(TileIndex tile, Owner owner)
Sets the owner of a tile.
Definition: tile_map.h:198
WBL_TYPE_BEGIN
@ WBL_TYPE_BEGIN
Start of the 'type' bitfield.
Definition: water_map.h:20
WBL_DEPOT_AXIS
@ WBL_DEPOT_AXIS
Depot axis flag.
Definition: water_map.h:35
MakeWater
static void MakeWater(TileIndex t, Owner o, WaterClass wc, uint8 random_bits)
Helper function for making a watery tile.
Definition: water_map.h:396
SetWaterClass
static void SetWaterClass(TileIndex t, WaterClass wc)
Set the water class at a tile.
Definition: water_map.h:118
WATER_CLASS_SEA
@ WATER_CLASS_SEA
Sea.
Definition: water_map.h:48
WBL_LOCK_PART_BEGIN
@ WBL_LOCK_PART_BEGIN
Start of lock part bitfield.
Definition: water_map.h:31
IsWater
static bool IsWater(TileIndex t)
Is it a plain water tile?
Definition: water_map.h:141
WATER_TILE_LOCK
@ WATER_TILE_LOCK
Water lock.
Definition: water_map.h:42
Tile::m3
byte m3
General purpose.
Definition: map_type.h:22
IsShipDepotTile
static bool IsShipDepotTile(TileIndex t)
Is it a ship depot tile?
Definition: water_map.h:226
DepotPart
DepotPart
Sections of the water depot.
Definition: water_map.h:57
OWNER_WATER
@ OWNER_WATER
The tile/execution is done by "water".
Definition: company_type.h:26
Tile::m4
byte m4
General purpose.
Definition: map_type.h:23
_m
Tile * _m
Tiles of the map.
Definition: map.cpp:30
WBL_DEPOT_PART
@ WBL_DEPOT_PART
Depot part flag.
Definition: water_map.h:34
MakeRiver
static void MakeRiver(TileIndex t, uint8 random_bits)
Make a river tile.
Definition: water_map.h:424
AXIS_X
@ AXIS_X
The X axis.
Definition: direction_type.h:124