OpenTTD
water_map.h
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #ifndef WATER_MAP_H
13 #define WATER_MAP_H
14 
15 #include "depot_type.h"
16 #include "tile_map.h"
17 
24 
26  WBL_TYPE_LOCK = 0x1,
28 
30 
35 
38 };
39 
46 };
47 
49 enum WaterClass {
54 };
56 template <> struct EnumPropsT<WaterClass> : MakeEnumPropsT<WaterClass, byte, WATER_CLASS_SEA, WATER_CLASS_INVALID, WATER_CLASS_INVALID, 2> {};
57 
59 enum DepotPart {
62  DEPOT_PART_END
63 };
64 
66 enum LockPart {
70 };
71 
72 bool IsPossibleDockingTile(TileIndex t);
73 
80 {
81  assert(IsTileType(t, MP_WATER));
82 
83  switch (GB(_m[t].m5, WBL_TYPE_BEGIN, WBL_TYPE_COUNT)) {
85  case WBL_TYPE_LOCK: return WATER_TILE_LOCK;
86  case WBL_TYPE_DEPOT: return WATER_TILE_DEPOT;
87  default: NOT_REACHED();
88  }
89 }
90 
97 static inline bool HasTileWaterClass(TileIndex t)
98 {
100 }
101 
108 static inline WaterClass GetWaterClass(TileIndex t)
109 {
110  assert(HasTileWaterClass(t));
111  return (WaterClass)GB(_m[t].m1, 5, 2);
112 }
113 
120 static inline void SetWaterClass(TileIndex t, WaterClass wc)
121 {
122  assert(HasTileWaterClass(t));
123  SB(_m[t].m1, 5, 2, wc);
124 }
125 
132 static inline bool IsTileOnWater(TileIndex t)
133 {
134  return (GetWaterClass(t) != WATER_CLASS_INVALID);
135 }
136 
143 static inline bool IsWater(TileIndex t)
144 {
145  return GetWaterTileType(t) == WATER_TILE_CLEAR;
146 }
147 
154 static inline bool IsSea(TileIndex t)
155 {
156  return IsWater(t) && GetWaterClass(t) == WATER_CLASS_SEA;
157 }
158 
165 static inline bool IsCanal(TileIndex t)
166 {
167  return IsWater(t) && GetWaterClass(t) == WATER_CLASS_CANAL;
168 }
169 
176 static inline bool IsRiver(TileIndex t)
177 {
178  return IsWater(t) && GetWaterClass(t) == WATER_CLASS_RIVER;
179 }
180 
186 static inline bool IsWaterTile(TileIndex t)
187 {
188  return IsTileType(t, MP_WATER) && IsWater(t);
189 }
190 
197 static inline bool IsCoast(TileIndex t)
198 {
199  return GetWaterTileType(t) == WATER_TILE_COAST;
200 }
201 
207 static inline bool IsCoastTile(TileIndex t)
208 {
209  return (IsTileType(t, MP_WATER) && IsCoast(t)) || (IsTileType(t, MP_TREES) && GetWaterClass(t) != WATER_CLASS_INVALID);
210 }
211 
218 static inline bool IsShipDepot(TileIndex t)
219 {
220  return GetWaterTileType(t) == WATER_TILE_DEPOT;
221 }
222 
228 static inline bool IsShipDepotTile(TileIndex t)
229 {
230  return IsTileType(t, MP_WATER) && IsShipDepot(t);
231 }
232 
240 {
241  assert(IsShipDepotTile(t));
242  return (Axis)GB(_m[t].m5, WBL_DEPOT_AXIS, 1);
243 }
244 
252 {
253  assert(IsShipDepotTile(t));
254  return (DepotPart)GB(_m[t].m5, WBL_DEPOT_PART, 1);
255 }
256 
264 {
266 }
267 
275 {
276  return t + (GetShipDepotPart(t) != DEPOT_PART_NORTH ? -1 : 1) * (GetShipDepotAxis(t) != AXIS_X ? TileDiffXY(0, 1) : TileDiffXY(1, 0));
277 }
278 
286 {
287  assert(IsShipDepot(t));
288  TileIndex tile2 = GetOtherShipDepotTile(t);
289 
290  return t < tile2 ? t : tile2;
291 }
292 
299 static inline bool IsLock(TileIndex t)
300 {
301  return GetWaterTileType(t) == WATER_TILE_LOCK;
302 }
303 
311 {
312  assert(IsLock(t));
314 }
315 
322 static inline byte GetLockPart(TileIndex t)
323 {
324  assert(IsLock(t));
326 }
327 
334 static inline byte GetWaterTileRandomBits(TileIndex t)
335 {
336  assert(IsTileType(t, MP_WATER));
337  return _m[t].m4;
338 }
339 
346 static inline bool HasTileWaterGround(TileIndex t)
347 {
348  return HasTileWaterClass(t) && IsTileOnWater(t) && !IsCoastTile(t);
349 }
350 
357 static inline void SetDockingTile(TileIndex t, bool b)
358 {
360  SB(_m[t].m1, 7, 1, b ? 1 : 0);
361 }
362 
367 static inline bool IsDockingTile(TileIndex t)
368 {
369  return (IsTileType(t, MP_WATER) || IsTileType(t, MP_RAILWAY) || IsTileType(t, MP_STATION) || IsTileType(t, MP_TUNNELBRIDGE)) && HasBit(_m[t].m1, 7);
370 }
371 
372 
377 static inline void MakeShore(TileIndex t)
378 {
379  SetTileType(t, MP_WATER);
382  SetDockingTile(t, false);
383  _m[t].m2 = 0;
384  _m[t].m3 = 0;
385  _m[t].m4 = 0;
387  SB(_me[t].m6, 2, 4, 0);
388  _me[t].m7 = 0;
389 }
390 
398 static inline void MakeWater(TileIndex t, Owner o, WaterClass wc, uint8 random_bits)
399 {
400  SetTileType(t, MP_WATER);
401  SetTileOwner(t, o);
402  SetWaterClass(t, wc);
403  SetDockingTile(t, false);
404  _m[t].m2 = 0;
405  _m[t].m3 = 0;
406  _m[t].m4 = random_bits;
408  SB(_me[t].m6, 2, 4, 0);
409  _me[t].m7 = 0;
410 }
411 
416 static inline void MakeSea(TileIndex t)
417 {
419 }
420 
426 static inline void MakeRiver(TileIndex t, uint8 random_bits)
427 {
428  MakeWater(t, OWNER_WATER, WATER_CLASS_RIVER, random_bits);
429 }
430 
437 static inline void MakeCanal(TileIndex t, Owner o, uint8 random_bits)
438 {
439  assert(o != OWNER_WATER);
440  MakeWater(t, o, WATER_CLASS_CANAL, random_bits);
441 }
442 
452 static inline void MakeShipDepot(TileIndex t, Owner o, DepotID did, DepotPart part, Axis a, WaterClass original_water_class)
453 {
454  SetTileType(t, MP_WATER);
455  SetTileOwner(t, o);
456  SetWaterClass(t, original_water_class);
457  SetDockingTile(t, false);
458  _m[t].m2 = did;
459  _m[t].m3 = 0;
460  _m[t].m4 = 0;
462  SB(_me[t].m6, 2, 4, 0);
463  _me[t].m7 = 0;
464 }
465 
475 static inline void MakeLockTile(TileIndex t, Owner o, LockPart part, DiagDirection dir, WaterClass original_water_class)
476 {
477  SetTileType(t, MP_WATER);
478  SetTileOwner(t, o);
479  SetWaterClass(t, original_water_class);
480  SetDockingTile(t, false);
481  _m[t].m2 = 0;
482  _m[t].m3 = 0;
483  _m[t].m4 = 0;
485  SB(_me[t].m6, 2, 4, 0);
486  _me[t].m7 = 0;
487 }
488 
498 static inline void MakeLock(TileIndex t, Owner o, DiagDirection d, WaterClass wc_lower, WaterClass wc_upper, WaterClass wc_middle)
499 {
500  TileIndexDiff delta = TileOffsByDiagDir(d);
501 
502  /* Keep the current waterclass and owner for the tiles.
503  * It allows to restore them after the lock is deleted */
504  MakeLockTile(t, o, LOCK_PART_MIDDLE, d, wc_middle);
505  MakeLockTile(t - delta, IsWaterTile(t - delta) ? GetTileOwner(t - delta) : o, LOCK_PART_LOWER, d, wc_lower);
506  MakeLockTile(t + delta, IsWaterTile(t + delta) ? GetTileOwner(t + delta) : o, LOCK_PART_UPPER, d, wc_upper);
507 }
508 
509 #endif /* WATER_MAP_H */
Helper template class that makes basic properties of given enumeration type visible from outsize...
Definition: enum_type.hpp:64
Owner
Enum for all companies/owners.
Definition: company_type.h:20
uint16 DepotID
Type for the unique identifier of depots.
Definition: depot_type.h:15
static byte GetLockPart(TileIndex t)
Get the part of a lock.
Definition: water_map.h:322
static void SetTileOwner(TileIndex tile, Owner owner)
Sets the owner of a tile.
Definition: tile_map.h:200
static Axis GetShipDepotAxis(TileIndex t)
Get the axis of the ship depot.
Definition: water_map.h:239
Part of an industry.
Definition: tile_type.h:51
LockPart
Sections of the water lock.
Definition: water_map.h:66
int32 TileIndexDiff
An offset value between to tiles.
Definition: map_func.h:156
static bool IsWater(TileIndex t)
Is it a plain water tile?
Definition: water_map.h:143
static WaterClass GetWaterClass(TileIndex t)
Get the water class at a tile.
Definition: water_map.h:108
static TileIndex GetShipDepotNorthTile(TileIndex t)
Get the most northern tile of a ship depot.
Definition: water_map.h:285
Lock (&#39;type&#39; bitfield).
Definition: water_map.h:26
byte m7
Primarily used for newgrf support.
Definition: map_type.h:37
uint16 m2
Primarily used for indices to towns, industries and stations.
Definition: map_type.h:22
static void MakeShore(TileIndex t)
Helper function to make a coast tile.
Definition: water_map.h:377
Tile * _m
Tiles of the map.
Definition: map.cpp:32
WaterTileTypeBitLayout
Bit field layout of m5 for water tiles.
Definition: water_map.h:21
static TileIndex GetOtherShipDepotTile(TileIndex t)
Get the other tile of the ship depot.
Definition: water_map.h:274
static bool HasTileWaterGround(TileIndex t)
Checks whether the tile has water at the ground.
Definition: water_map.h:346
Clear water or coast (&#39;type&#39; bitfield).
Definition: water_map.h:25
Water Depot.
Definition: water_map.h:45
A railway.
Definition: tile_type.h:44
Contains objects such as transmitters and owned land.
Definition: tile_type.h:53
Length of the &#39;type&#39; bitfield.
Definition: water_map.h:23
static void SetWaterClass(TileIndex t, WaterClass wc)
Set the water class at a tile.
Definition: water_map.h:120
Length of lock orientation bitfield.
Definition: water_map.h:32
Depot (&#39;type&#39; bitfield).
Definition: water_map.h:27
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.
static bool IsLock(TileIndex t)
Is there a lock on a given water tile?
Definition: water_map.h:299
Depot part flag.
Definition: water_map.h:36
Plain water.
Definition: water_map.h:42
Flag for coast.
Definition: water_map.h:29
Header files for depots (not hangars)
static void MakeSea(TileIndex t)
Make a sea tile.
Definition: water_map.h:416
static TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition: map_func.h:343
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:152
static bool IsCoast(TileIndex t)
Is it a coast tile?
Definition: water_map.h:197
WaterClass
classes of water (for WATER_TILE_CLEAR water tile type).
Definition: water_map.h:49
static Owner GetTileOwner(TileIndex tile)
Returns the owner of a tile.
Definition: tile_map.h:180
static byte GetWaterTileRandomBits(TileIndex t)
Get the random bits of the water tile.
Definition: water_map.h:334
Informative template class exposing basic enumeration properties used by several other templates belo...
Definition: enum_type.hpp:50
Water tile.
Definition: tile_type.h:49
TileExtended * _me
Extended Tiles of the map.
Definition: map.cpp:33
byte m5
General purpose.
Definition: map_type.h:26
static bool IsDockingTile(TileIndex t)
Checks whether the tile is marked as a dockling tile.
Definition: water_map.h:367
static WaterTileType GetWaterTileType(TileIndex t)
Get the water tile type at a tile.
Definition: water_map.h:79
static void MakeLockTile(TileIndex t, Owner o, LockPart part, DiagDirection dir, WaterClass original_water_class)
Make a lock section.
Definition: water_map.h:475
static bool IsWaterTile(TileIndex t)
Is it a water tile with plain water?
Definition: water_map.h:186
static bool IsShipDepotTile(TileIndex t)
Is it a ship depot tile?
Definition: water_map.h:228
static void MakeRiver(TileIndex t, uint8 random_bits)
Make a river tile.
Definition: water_map.h:426
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:452
Depot axis flag.
Definition: water_map.h:37
static bool IsSea(TileIndex t)
Is it a sea water tile?
Definition: water_map.h:154
Water lock.
Definition: water_map.h:44
The X axis.
Length of lock part bitfield.
Definition: water_map.h:34
Northern part of a depot.
Definition: water_map.h:60
The tile/execution is done by "water".
Definition: company_type.h:28
static DiagDirection GetShipDepotDirection(TileIndex t)
Get the direction of the ship depot.
Definition: water_map.h:263
Upper part of a lock.
Definition: water_map.h:69
Tile got trees.
Definition: tile_type.h:47
Tunnel entry/exit and bridge heads.
Definition: tile_type.h:52
static DiagDirection XYNSToDiagDir(Axis xy, uint ns)
Convert an axis and a flag for north/south into a DiagDirection.
static DiagDirection GetLockDirection(TileIndex t)
Get the direction of the water lock.
Definition: water_map.h:310
static bool IsShipDepot(TileIndex t)
Is it a water tile with a ship depot on it?
Definition: water_map.h:218
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
Middle part of a lock.
Definition: water_map.h:67
DepotPart
Sections of the water depot.
Definition: water_map.h:59
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:498
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Lower part of a lock.
Definition: water_map.h:68
static bool IsCanal(TileIndex t)
Is it a canal tile?
Definition: water_map.h:165
static void SetTileType(TileIndex tile, TileType type)
Set the type of a tile.
Definition: tile_map.h:133
Southern part of a depot.
Definition: water_map.h:61
A tile of a station.
Definition: tile_type.h:48
static bool IsRiver(TileIndex t)
Is it a river water tile?
Definition: water_map.h:176
static void MakeWater(TileIndex t, Owner o, WaterClass wc, uint8 random_bits)
Helper function for making a watery tile.
Definition: water_map.h:398
static void MakeCanal(TileIndex t, Owner o, uint8 random_bits)
Make a canal tile.
Definition: water_map.h:437
Start of lock part bitfield.
Definition: water_map.h:33
static bool IsCoastTile(TileIndex t)
Is it a coast tile.
Definition: water_map.h:207
static TileIndexDiff TileDiffXY(int x, int y)
Calculates an offset for the given coordinate(-offset).
Definition: map_func.h:181
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
static bool HasTileWaterClass(TileIndex t)
Checks whether the tile has an waterclass associated.
Definition: water_map.h:97
DiagDirection
Enumeration for diagonal directions.
Start of lock orientation bitfield.
Definition: water_map.h:31
Start of the &#39;type&#39; bitfield.
Definition: water_map.h:22
Map writing/reading functions for tiles.
WaterTileType
Available water tile types.
Definition: water_map.h:41
static bool IsTileOnWater(TileIndex t)
Tests if the tile was built on water.
Definition: water_map.h:132
static void SetDockingTile(TileIndex t, bool b)
Set the docking tile state of a tile.
Definition: water_map.h:357
byte m3
General purpose.
Definition: map_type.h:24
Axis
Allow incrementing of DiagDirDiff variables.
Used for industry tiles on land (also for oilrig if newgrf says so).
Definition: water_map.h:53
static DepotPart GetShipDepotPart(TileIndex t)
Get the part of a ship depot.
Definition: water_map.h:251
byte m4
General purpose.
Definition: map_type.h:25