(svn r27724) -Cleanup: Remove pointless usage of IsOpenTTDBaseGRF. System GRFs are...
[openttd.git] / src / water_map.h
blobab249a82794c13e3237f46bf2572d736161b2a10
1 /* $Id$ */
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 */
10 /** @file water_map.h Map accessors for water tiles. */
12 #ifndef WATER_MAP_H
13 #define WATER_MAP_H
15 #include "depot_type.h"
16 #include "tile_map.h"
18 /**
19 * Bit field layout of m5 for water tiles.
21 enum WaterTileTypeBitLayout {
22 WBL_TYPE_BEGIN = 4, ///< Start of the 'type' bitfield.
23 WBL_TYPE_COUNT = 4, ///< Length of the 'type' bitfield.
25 WBL_TYPE_NORMAL = 0x0, ///< Clear water or coast ('type' bitfield).
26 WBL_TYPE_LOCK = 0x1, ///< Lock ('type' bitfield).
27 WBL_TYPE_DEPOT = 0x8, ///< Depot ('type' bitfield).
29 WBL_COAST_FLAG = 0, ///< Flag for coast.
31 WBL_LOCK_ORIENT_BEGIN = 0, ///< Start of lock orientiation bitfield.
32 WBL_LOCK_ORIENT_COUNT = 2, ///< Length of lock orientiation bitfield.
33 WBL_LOCK_PART_BEGIN = 2, ///< Start of lock part bitfield.
34 WBL_LOCK_PART_COUNT = 2, ///< Length of lock part bitfield.
36 WBL_DEPOT_PART = 0, ///< Depot part flag.
37 WBL_DEPOT_AXIS = 1, ///< Depot axis flag.
40 /** Available water tile types. */
41 enum WaterTileType {
42 WATER_TILE_CLEAR, ///< Plain water.
43 WATER_TILE_COAST, ///< Coast.
44 WATER_TILE_LOCK, ///< Water lock.
45 WATER_TILE_DEPOT, ///< Water Depot.
48 /** classes of water (for #WATER_TILE_CLEAR water tile type). */
49 enum WaterClass {
50 WATER_CLASS_SEA, ///< Sea.
51 WATER_CLASS_CANAL, ///< Canal.
52 WATER_CLASS_RIVER, ///< River.
53 WATER_CLASS_INVALID, ///< Used for industry tiles on land (also for oilrig if newgrf says so).
55 /** Helper information for extract tool. */
56 template <> struct EnumPropsT<WaterClass> : MakeEnumPropsT<WaterClass, byte, WATER_CLASS_SEA, WATER_CLASS_INVALID, WATER_CLASS_INVALID, 2> {};
58 /** Sections of the water depot. */
59 enum DepotPart {
60 DEPOT_PART_NORTH = 0, ///< Northern part of a depot.
61 DEPOT_PART_SOUTH = 1, ///< Southern part of a depot.
62 DEPOT_PART_END
65 /** Sections of the water lock. */
66 enum LockPart {
67 LOCK_PART_MIDDLE = 0, ///< Middle part of a lock.
68 LOCK_PART_LOWER = 1, ///< Lower part of a lock.
69 LOCK_PART_UPPER = 2, ///< Upper part of a lock.
72 /**
73 * Get the water tile type at a tile.
74 * @param t Water tile to query.
75 * @return Water tile type at the tile.
77 static inline WaterTileType GetWaterTileType(TileIndex t)
79 assert(IsTileType(t, MP_WATER));
81 switch (GB(_m[t].m5, WBL_TYPE_BEGIN, WBL_TYPE_COUNT)) {
82 case WBL_TYPE_NORMAL: return HasBit(_m[t].m5, WBL_COAST_FLAG) ? WATER_TILE_COAST : WATER_TILE_CLEAR;
83 case WBL_TYPE_LOCK: return WATER_TILE_LOCK;
84 case WBL_TYPE_DEPOT: return WATER_TILE_DEPOT;
85 default: NOT_REACHED();
89 /**
90 * Checks whether the tile has an waterclass associated.
91 * You can then subsequently call GetWaterClass().
92 * @param t Tile to query.
93 * @return True if the tiletype has a waterclass.
95 static inline bool HasTileWaterClass(TileIndex t)
97 return IsTileType(t, MP_WATER) || IsTileType(t, MP_STATION) || IsTileType(t, MP_INDUSTRY) || IsTileType(t, MP_OBJECT);
101 * Get the water class at a tile.
102 * @param t Water tile to query.
103 * @pre IsTileType(t, MP_WATER) || IsTileType(t, MP_STATION) || IsTileType(t, MP_INDUSTRY) || IsTileType(t, MP_OBJECT)
104 * @return Water class at the tile.
106 static inline WaterClass GetWaterClass(TileIndex t)
108 assert(HasTileWaterClass(t));
109 return (WaterClass)GB(_m[t].m1, 5, 2);
113 * Set the water class at a tile.
114 * @param t Water tile to change.
115 * @param wc New water class.
116 * @pre IsTileType(t, MP_WATER) || IsTileType(t, MP_STATION) || IsTileType(t, MP_INDUSTRY) || IsTileType(t, MP_OBJECT)
118 static inline void SetWaterClass(TileIndex t, WaterClass wc)
120 assert(HasTileWaterClass(t));
121 SB(_m[t].m1, 5, 2, wc);
125 * Tests if the tile was built on water.
126 * @param t the tile to check
127 * @pre IsTileType(t, MP_WATER) || IsTileType(t, MP_STATION) || IsTileType(t, MP_INDUSTRY) || IsTileType(t, MP_OBJECT)
128 * @return true iff on water
130 static inline bool IsTileOnWater(TileIndex t)
132 return (GetWaterClass(t) != WATER_CLASS_INVALID);
136 * Is it a plain water tile?
137 * @param t Water tile to query.
138 * @return \c true if any type of clear water like ocean, river, or canal.
139 * @pre IsTileType(t, MP_WATER)
141 static inline bool IsWater(TileIndex t)
143 return GetWaterTileType(t) == WATER_TILE_CLEAR;
147 * Is it a sea water tile?
148 * @param t Water tile to query.
149 * @return \c true if it is a sea water tile.
150 * @pre IsTileType(t, MP_WATER)
152 static inline bool IsSea(TileIndex t)
154 return IsWater(t) && GetWaterClass(t) == WATER_CLASS_SEA;
158 * Is it a canal tile?
159 * @param t Water tile to query.
160 * @return \c true if it is a canal tile.
161 * @pre IsTileType(t, MP_WATER)
163 static inline bool IsCanal(TileIndex t)
165 return IsWater(t) && GetWaterClass(t) == WATER_CLASS_CANAL;
169 * Is it a river water tile?
170 * @param t Water tile to query.
171 * @return \c true if it is a river water tile.
172 * @pre IsTileType(t, MP_WATER)
174 static inline bool IsRiver(TileIndex t)
176 return IsWater(t) && GetWaterClass(t) == WATER_CLASS_RIVER;
180 * Is it a water tile with plain water?
181 * @param t Tile to query.
182 * @return \c true if it is a plain water tile.
184 static inline bool IsWaterTile(TileIndex t)
186 return IsTileType(t, MP_WATER) && IsWater(t);
190 * Is it a coast tile?
191 * @param t Water tile to query.
192 * @return \c true if it is a sea water tile.
193 * @pre IsTileType(t, MP_WATER)
195 static inline bool IsCoast(TileIndex t)
197 return GetWaterTileType(t) == WATER_TILE_COAST;
201 * Is it a coast tile
202 * @param t Tile to query.
203 * @return \c true if it is a coast.
205 static inline bool IsCoastTile(TileIndex t)
207 return IsTileType(t, MP_WATER) && IsCoast(t);
211 * Is it a water tile with a ship depot on it?
212 * @param t Water tile to query.
213 * @return \c true if it is a ship depot tile.
214 * @pre IsTileType(t, MP_WATER)
216 static inline bool IsShipDepot(TileIndex t)
218 return GetWaterTileType(t) == WATER_TILE_DEPOT;
222 * Is it a ship depot tile?
223 * @param t Tile to query.
224 * @return \c true if it is a ship depot tile.
226 static inline bool IsShipDepotTile(TileIndex t)
228 return IsTileType(t, MP_WATER) && IsShipDepot(t);
232 * Get the axis of the ship depot.
233 * @param t Water tile to query.
234 * @return Axis of the depot.
235 * @pre IsShipDepotTile(t)
237 static inline Axis GetShipDepotAxis(TileIndex t)
239 assert(IsShipDepotTile(t));
240 return (Axis)GB(_m[t].m5, WBL_DEPOT_AXIS, 1);
244 * Get the part of a ship depot.
245 * @param t Water tile to query.
246 * @return Part of the depot.
247 * @pre IsShipDepotTile(t)
249 static inline DepotPart GetShipDepotPart(TileIndex t)
251 assert(IsShipDepotTile(t));
252 return (DepotPart)GB(_m[t].m5, WBL_DEPOT_PART, 1);
256 * Get the direction of the ship depot.
257 * @param t Water tile to query.
258 * @return Direction of the depot.
259 * @pre IsShipDepotTile(t)
261 static inline DiagDirection GetShipDepotDirection(TileIndex t)
263 return XYNSToDiagDir(GetShipDepotAxis(t), GetShipDepotPart(t));
267 * Get the other tile of the ship depot.
268 * @param t Tile to query, containing one section of a ship depot.
269 * @return Tile containing the other section of the depot.
270 * @pre IsShipDepotTile(t)
272 static inline TileIndex GetOtherShipDepotTile(TileIndex t)
274 return t + (GetShipDepotPart(t) != DEPOT_PART_NORTH ? -1 : 1) * (GetShipDepotAxis(t) != AXIS_X ? TileDiffXY(0, 1) : TileDiffXY(1, 0));
278 * Get the most northern tile of a ship depot.
279 * @param t One of the tiles of the ship depot.
280 * @return The northern tile of the depot.
281 * @pre IsShipDepotTile(t)
283 static inline TileIndex GetShipDepotNorthTile(TileIndex t)
285 assert(IsShipDepot(t));
286 TileIndex tile2 = GetOtherShipDepotTile(t);
288 return t < tile2 ? t : tile2;
292 * Is there a lock on a given water tile?
293 * @param t Water tile to query.
294 * @return \c true if it is a water lock tile.
295 * @pre IsTileType(t, MP_WATER)
297 static inline bool IsLock(TileIndex t)
299 return GetWaterTileType(t) == WATER_TILE_LOCK;
303 * Get the direction of the water lock.
304 * @param t Water tile to query.
305 * @return Direction of the lock.
306 * @pre IsTileType(t, MP_WATER) && IsLock(t)
308 static inline DiagDirection GetLockDirection(TileIndex t)
310 assert(IsLock(t));
311 return (DiagDirection)GB(_m[t].m5, WBL_LOCK_ORIENT_BEGIN, WBL_LOCK_ORIENT_COUNT);
315 * Get the part of a lock.
316 * @param t Water tile to query.
317 * @return The part.
318 * @pre IsTileType(t, MP_WATER) && IsLock(t)
320 static inline byte GetLockPart(TileIndex t)
322 assert(IsLock(t));
323 return GB(_m[t].m5, WBL_LOCK_PART_BEGIN, WBL_LOCK_PART_COUNT);
327 * Get the random bits of the water tile.
328 * @param t Water tile to query.
329 * @return Random bits of the tile.
330 * @pre IsTileType(t, MP_WATER)
332 static inline byte GetWaterTileRandomBits(TileIndex t)
334 assert(IsTileType(t, MP_WATER));
335 return _m[t].m4;
339 * Checks whether the tile has water at the ground.
340 * That is, it is either some plain water tile, or a object/industry/station/... with water under it.
341 * @return true iff the tile has water at the ground.
342 * @note Coast tiles are not considered waterish, even if there is water on a halftile.
344 static inline bool HasTileWaterGround(TileIndex t)
346 return HasTileWaterClass(t) && IsTileOnWater(t) && !IsCoastTile(t);
351 * Helper function to make a coast tile.
352 * @param t The tile to change into water
354 static inline void MakeShore(TileIndex t)
356 SetTileType(t, MP_WATER);
357 SetTileOwner(t, OWNER_WATER);
358 SetWaterClass(t, WATER_CLASS_SEA);
359 _m[t].m2 = 0;
360 _m[t].m3 = 0;
361 _m[t].m4 = 0;
362 _m[t].m5 = WBL_TYPE_NORMAL << WBL_TYPE_BEGIN | 1 << WBL_COAST_FLAG;
363 SB(_me[t].m6, 2, 4, 0);
364 _me[t].m7 = 0;
368 * Helper function for making a watery tile.
369 * @param t The tile to change into water
370 * @param o The owner of the water
371 * @param wc The class of water the tile has to be
372 * @param random_bits Eventual random bits to be set for this tile
374 static inline void MakeWater(TileIndex t, Owner o, WaterClass wc, uint8 random_bits)
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;
382 _m[t].m5 = WBL_TYPE_NORMAL << WBL_TYPE_BEGIN;
383 SB(_me[t].m6, 2, 4, 0);
384 _me[t].m7 = 0;
388 * Make a sea tile.
389 * @param t The tile to change into sea
391 static inline void MakeSea(TileIndex t)
393 MakeWater(t, OWNER_WATER, WATER_CLASS_SEA, 0);
397 * Make a river tile
398 * @param t The tile to change into river
399 * @param random_bits Random bits to be set for this tile
401 static inline void MakeRiver(TileIndex t, uint8 random_bits)
403 MakeWater(t, OWNER_WATER, WATER_CLASS_RIVER, random_bits);
407 * Make a canal tile
408 * @param t The tile to change into canal
409 * @param o The owner of the canal
410 * @param random_bits Random bits to be set for this tile
412 static inline void MakeCanal(TileIndex t, Owner o, uint8 random_bits)
414 assert(o != OWNER_WATER);
415 MakeWater(t, o, WATER_CLASS_CANAL, random_bits);
419 * Make a ship depot section.
420 * @param t Tile to place the ship depot section.
421 * @param o Owner of the depot.
422 * @param did Depot ID.
423 * @param part Depot part (either #DEPOT_PART_NORTH or #DEPOT_PART_SOUTH).
424 * @param a Axis of the depot.
425 * @param original_water_class Original water class.
427 static inline void MakeShipDepot(TileIndex t, Owner o, DepotID did, DepotPart part, Axis a, WaterClass original_water_class)
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;
435 _m[t].m5 = WBL_TYPE_DEPOT << WBL_TYPE_BEGIN | part << WBL_DEPOT_PART | a << WBL_DEPOT_AXIS;
436 SB(_me[t].m6, 2, 4, 0);
437 _me[t].m7 = 0;
441 * Make a lock section.
442 * @param t Tile to place the water lock section.
443 * @param o Owner of the lock.
444 * @param part Part to place.
445 * @param dir Lock orientation
446 * @param original_water_class Original water class.
447 * @see MakeLock
449 static inline void MakeLockTile(TileIndex t, Owner o, LockPart part, DiagDirection dir, WaterClass original_water_class)
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;
457 _m[t].m5 = WBL_TYPE_LOCK << WBL_TYPE_BEGIN | part << WBL_LOCK_PART_BEGIN | dir << WBL_LOCK_ORIENT_BEGIN;
458 SB(_me[t].m6, 2, 4, 0);
459 _me[t].m7 = 0;
463 * Make a water lock.
464 * @param t Tile to place the water lock section.
465 * @param o Owner of the lock.
466 * @param d Direction of the water lock.
467 * @param wc_lower Original water class of the lower part.
468 * @param wc_upper Original water class of the upper part.
469 * @param wc_middle Original water class of the middle part.
471 static inline void MakeLock(TileIndex t, Owner o, DiagDirection d, WaterClass wc_lower, WaterClass wc_upper, WaterClass wc_middle)
473 TileIndexDiff delta = TileOffsByDiagDir(d);
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);
482 #endif /* WATER_MAP_H */