OpenTTD
water_map.h
Go to the documentation of this file.
1 /* $Id: water_map.h 26878 2014-09-21 11:23:33Z rubidium $ */
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 
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 
106 static inline WaterClass GetWaterClass(TileIndex t)
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);
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 
349 
354 static inline void MakeShore(TileIndex t)
355 {
356  SetTileType(t, MP_WATER);
359  _m[t].m2 = 0;
360  _m[t].m3 = 0;
361  _m[t].m4 = 0;
363  SB(_me[t].m6, 2, 4, 0);
364  _me[t].m7 = 0;
365 }
366 
374 static inline void MakeWater(TileIndex t, Owner o, WaterClass wc, uint8 random_bits)
375 {
376  SetTileType(t, MP_WATER);
377  SetTileOwner(t, o);
378  SetWaterClass(t, wc);
379  _m[t].m2 = 0;
380  _m[t].m3 = 0;
381  _m[t].m4 = random_bits;
383  SB(_me[t].m6, 2, 4, 0);
384  _me[t].m7 = 0;
385 }
386 
391 static inline void MakeSea(TileIndex t)
392 {
394 }
395 
401 static inline void MakeRiver(TileIndex t, uint8 random_bits)
402 {
403  MakeWater(t, OWNER_WATER, WATER_CLASS_RIVER, random_bits);
404 }
405 
412 static inline void MakeCanal(TileIndex t, Owner o, uint8 random_bits)
413 {
414  assert(o != OWNER_WATER);
415  MakeWater(t, o, WATER_CLASS_CANAL, random_bits);
416 }
417 
427 static inline void MakeShipDepot(TileIndex t, Owner o, DepotID did, DepotPart part, Axis a, WaterClass original_water_class)
428 {
429  SetTileType(t, MP_WATER);
430  SetTileOwner(t, o);
431  SetWaterClass(t, original_water_class);
432  _m[t].m2 = did;
433  _m[t].m3 = 0;
434  _m[t].m4 = 0;
436  SB(_me[t].m6, 2, 4, 0);
437  _me[t].m7 = 0;
438 }
439 
449 static inline void MakeLockTile(TileIndex t, Owner o, LockPart part, DiagDirection dir, WaterClass original_water_class)
450 {
451  SetTileType(t, MP_WATER);
452  SetTileOwner(t, o);
453  SetWaterClass(t, original_water_class);
454  _m[t].m2 = 0;
455  _m[t].m3 = 0;
456  _m[t].m4 = 0;
458  SB(_me[t].m6, 2, 4, 0);
459  _me[t].m7 = 0;
460 }
461 
471 static inline void MakeLock(TileIndex t, Owner o, DiagDirection d, WaterClass wc_lower, WaterClass wc_upper, WaterClass wc_middle)
472 {
473  TileIndexDiff delta = TileOffsByDiagDir(d);
474 
475  /* Keep the current waterclass and owner for the tiles.
476  * It allows to restore them after the lock is deleted */
477  MakeLockTile(t, o, LOCK_PART_MIDDLE, d, wc_middle);
478  MakeLockTile(t - delta, IsWaterTile(t - delta) ? GetTileOwner(t - delta) : o, LOCK_PART_LOWER, d, wc_lower);
479  MakeLockTile(t + delta, IsWaterTile(t + delta) ? GetTileOwner(t + delta) : o, LOCK_PART_UPPER, d, wc_upper);
480 }
481 
482 #endif /* WATER_MAP_H */
Helper template class that makes basic properties of given enumeration type visible from outsize...
Definition: enum_type.hpp:66
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:320
static void SetTileOwner(TileIndex tile, Owner owner)
Sets the owner of a tile.
Definition: tile_map.h:191
static Axis GetShipDepotAxis(TileIndex t)
Get the axis of the ship depot.
Definition: water_map.h:237
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:141
static WaterClass GetWaterClass(TileIndex t)
Get the water class at a tile.
Definition: water_map.h:106
static TileIndex GetShipDepotNorthTile(TileIndex t)
Get the most northern tile of a ship depot.
Definition: water_map.h:283
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:354
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:272
static bool HasTileWaterGround(TileIndex t)
Checks whether the tile has water at the ground.
Definition: water_map.h:344
Clear water or coast (&#39;type&#39; bitfield).
Definition: water_map.h:25
Water Depot.
Definition: water_map.h:45
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:118
Length of lock orientiation 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:297
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:391
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 give tiletype.
Definition: tile_map.h:143
static bool IsCoast(TileIndex t)
Is it a coast tile?
Definition: water_map.h:195
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:171
static byte GetWaterTileRandomBits(TileIndex t)
Get the random bits of the water tile.
Definition: water_map.h:332
Informative template class exposing basic enumeration properties used by several other templates belo...
Definition: enum_type.hpp:52
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 WaterTileType GetWaterTileType(TileIndex t)
Get the water tile type at a tile.
Definition: water_map.h:77
DiagDirection
Enumeration for diagonal directions.
static void MakeLockTile(TileIndex t, Owner o, LockPart part, DiagDirection dir, WaterClass original_water_class)
Make a lock section.
Definition: water_map.h:449
static bool IsWaterTile(TileIndex t)
Is it a water tile with plain water?
Definition: water_map.h:184
static bool IsShipDepotTile(TileIndex t)
Is it a ship depot tile?
Definition: water_map.h:226
static void MakeRiver(TileIndex t, uint8 random_bits)
Make a river tile.
Definition: water_map.h:401
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:427
Depot axis flag.
Definition: water_map.h:37
static bool IsSea(TileIndex t)
Is it a sea water tile?
Definition: water_map.h:152
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
static DiagDirection GetShipDepotDirection(TileIndex t)
Get the direction of the ship depot.
Definition: water_map.h:261
Upper part of a lock.
Definition: water_map.h:69
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:308
static bool IsShipDepot(TileIndex t)
Is it a water tile with a ship depot on it?
Definition: water_map.h:216
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:471
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:163
static void SetTileType(TileIndex tile, TileType type)
Set the type of a tile.
Definition: tile_map.h:124
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:174
static void MakeWater(TileIndex t, Owner o, WaterClass wc, uint8 random_bits)
Helper function for making a watery tile.
Definition: water_map.h:374
static void MakeCanal(TileIndex t, Owner o, uint8 random_bits)
Make a canal tile.
Definition: water_map.h:412
Start of lock part bitfield.
Definition: water_map.h:33
static bool IsCoastTile(TileIndex t)
Is it a coast tile.
Definition: water_map.h:205
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:95
Start of lock orientiation bitfield.
Definition: water_map.h:31
Start of the &#39;type&#39; bitfield.
Definition: water_map.h:22
Map writing/reading functions for tiles.
Owner
Enum for all companies/owners.
Definition: company_type.h:20
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:130
byte m3
General purpose.
Definition: map_type.h:24
The tile/execution is done by "water".
Definition: company_type.h:28
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:249
byte m4
General purpose.
Definition: map_type.h:25