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 
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 */