Files
@ r28822:57cd1bc354de
Branch filter:
Location: cpp/openttd-patchpack/source/src/water_map.h
r28822:57cd1bc354de
14.1 KiB
text/x-c
Codechange: Add `GetVisibleRangeIterators()` to `Scrollbar`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 | /*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file water_map.h Map accessors for water tiles. */
#ifndef WATER_MAP_H
#define WATER_MAP_H
#include "depot_type.h"
#include "tile_map.h"
/**
* Bit field layout of m5 for water tiles.
*/
enum WaterTileTypeBitLayout {
WBL_TYPE_BEGIN = 4, ///< Start of the 'type' bitfield.
WBL_TYPE_COUNT = 4, ///< Length of the 'type' bitfield.
WBL_TYPE_NORMAL = 0x0, ///< Clear water or coast ('type' bitfield).
WBL_TYPE_LOCK = 0x1, ///< Lock ('type' bitfield).
WBL_TYPE_DEPOT = 0x8, ///< Depot ('type' bitfield).
WBL_COAST_FLAG = 0, ///< Flag for coast.
WBL_LOCK_ORIENT_BEGIN = 0, ///< Start of lock orientation bitfield.
WBL_LOCK_ORIENT_COUNT = 2, ///< Length of lock orientation bitfield.
WBL_LOCK_PART_BEGIN = 2, ///< Start of lock part bitfield.
WBL_LOCK_PART_COUNT = 2, ///< Length of lock part bitfield.
WBL_DEPOT_PART = 0, ///< Depot part flag.
WBL_DEPOT_AXIS = 1, ///< Depot axis flag.
};
/** Available water tile types. */
enum WaterTileType {
WATER_TILE_CLEAR, ///< Plain water.
WATER_TILE_COAST, ///< Coast.
WATER_TILE_LOCK, ///< Water lock.
WATER_TILE_DEPOT, ///< Water Depot.
};
/** classes of water (for #WATER_TILE_CLEAR water tile type). */
enum WaterClass : byte {
WATER_CLASS_SEA, ///< Sea.
WATER_CLASS_CANAL, ///< Canal.
WATER_CLASS_RIVER, ///< River.
WATER_CLASS_INVALID, ///< Used for industry tiles on land (also for oilrig if newgrf says so).
};
/**
* Checks if a water class is valid.
*
* @param wc The value to check
* @return true if the given value is a valid water class.
*/
inline bool IsValidWaterClass(WaterClass wc)
{
return wc < WATER_CLASS_INVALID;
}
/** Sections of the water depot. */
enum DepotPart {
DEPOT_PART_NORTH = 0, ///< Northern part of a depot.
DEPOT_PART_SOUTH = 1, ///< Southern part of a depot.
DEPOT_PART_END
};
/** Sections of the water lock. */
enum LockPart {
LOCK_PART_MIDDLE = 0, ///< Middle part of a lock.
LOCK_PART_LOWER = 1, ///< Lower part of a lock.
LOCK_PART_UPPER = 2, ///< Upper part of a lock.
};
bool IsPossibleDockingTile(Tile t);
/**
* Get the water tile type at a tile.
* @param t Water tile to query.
* @return Water tile type at the tile.
*/
inline WaterTileType GetWaterTileType(Tile t)
{
assert(IsTileType(t, MP_WATER));
switch (GB(t.m5(), WBL_TYPE_BEGIN, WBL_TYPE_COUNT)) {
case WBL_TYPE_NORMAL: return HasBit(t.m5(), WBL_COAST_FLAG) ? WATER_TILE_COAST : WATER_TILE_CLEAR;
case WBL_TYPE_LOCK: return WATER_TILE_LOCK;
case WBL_TYPE_DEPOT: return WATER_TILE_DEPOT;
default: NOT_REACHED();
}
}
/**
* Checks whether the tile has an waterclass associated.
* You can then subsequently call GetWaterClass().
* @param t Tile to query.
* @return True if the tiletype has a waterclass.
*/
inline bool HasTileWaterClass(Tile t)
{
return IsTileType(t, MP_WATER) || IsTileType(t, MP_STATION) || IsTileType(t, MP_INDUSTRY) || IsTileType(t, MP_OBJECT) || IsTileType(t, MP_TREES);
}
/**
* Get the water class at a tile.
* @param t Water tile to query.
* @pre IsTileType(t, MP_WATER) || IsTileType(t, MP_STATION) || IsTileType(t, MP_INDUSTRY) || IsTileType(t, MP_OBJECT)
* @return Water class at the tile.
*/
inline WaterClass GetWaterClass(Tile t)
{
assert(HasTileWaterClass(t));
return (WaterClass)GB(t.m1(), 5, 2);
}
/**
* Set the water class at a tile.
* @param t Water tile to change.
* @param wc New water class.
* @pre IsTileType(t, MP_WATER) || IsTileType(t, MP_STATION) || IsTileType(t, MP_INDUSTRY) || IsTileType(t, MP_OBJECT)
*/
inline void SetWaterClass(Tile t, WaterClass wc)
{
assert(HasTileWaterClass(t));
SB(t.m1(), 5, 2, wc);
}
/**
* Tests if the tile was built on water.
* @param t the tile to check
* @pre IsTileType(t, MP_WATER) || IsTileType(t, MP_STATION) || IsTileType(t, MP_INDUSTRY) || IsTileType(t, MP_OBJECT)
* @return true iff on water
*/
inline bool IsTileOnWater(Tile t)
{
return (GetWaterClass(t) != WATER_CLASS_INVALID);
}
/**
* Is it a plain water tile?
* @param t Water tile to query.
* @return \c true if any type of clear water like ocean, river, or canal.
* @pre IsTileType(t, MP_WATER)
*/
inline bool IsWater(Tile t)
{
return GetWaterTileType(t) == WATER_TILE_CLEAR;
}
/**
* Is it a sea water tile?
* @param t Water tile to query.
* @return \c true if it is a sea water tile.
* @pre IsTileType(t, MP_WATER)
*/
inline bool IsSea(Tile t)
{
return IsWater(t) && GetWaterClass(t) == WATER_CLASS_SEA;
}
/**
* Is it a canal tile?
* @param t Water tile to query.
* @return \c true if it is a canal tile.
* @pre IsTileType(t, MP_WATER)
*/
inline bool IsCanal(Tile t)
{
return IsWater(t) && GetWaterClass(t) == WATER_CLASS_CANAL;
}
/**
* Is it a river water tile?
* @param t Water tile to query.
* @return \c true if it is a river water tile.
* @pre IsTileType(t, MP_WATER)
*/
inline bool IsRiver(Tile t)
{
return IsWater(t) && GetWaterClass(t) == WATER_CLASS_RIVER;
}
/**
* Is it a water tile with plain water?
* @param t Tile to query.
* @return \c true if it is a plain water tile.
*/
inline bool IsWaterTile(Tile t)
{
return IsTileType(t, MP_WATER) && IsWater(t);
}
/**
* Is it a coast tile?
* @param t Water tile to query.
* @return \c true if it is a sea water tile.
* @pre IsTileType(t, MP_WATER)
*/
inline bool IsCoast(Tile t)
{
return GetWaterTileType(t) == WATER_TILE_COAST;
}
/**
* Is it a coast tile
* @param t Tile to query.
* @return \c true if it is a coast.
*/
inline bool IsCoastTile(Tile t)
{
return (IsTileType(t, MP_WATER) && IsCoast(t)) || (IsTileType(t, MP_TREES) && GetWaterClass(t) != WATER_CLASS_INVALID);
}
/**
* Is it a water tile with a ship depot on it?
* @param t Water tile to query.
* @return \c true if it is a ship depot tile.
* @pre IsTileType(t, MP_WATER)
*/
inline bool IsShipDepot(Tile t)
{
return GetWaterTileType(t) == WATER_TILE_DEPOT;
}
/**
* Is it a ship depot tile?
* @param t Tile to query.
* @return \c true if it is a ship depot tile.
*/
inline bool IsShipDepotTile(Tile t)
{
return IsTileType(t, MP_WATER) && IsShipDepot(t);
}
/**
* Get the axis of the ship depot.
* @param t Water tile to query.
* @return Axis of the depot.
* @pre IsShipDepotTile(t)
*/
inline Axis GetShipDepotAxis(Tile t)
{
assert(IsShipDepotTile(t));
return (Axis)GB(t.m5(), WBL_DEPOT_AXIS, 1);
}
/**
* Get the part of a ship depot.
* @param t Water tile to query.
* @return Part of the depot.
* @pre IsShipDepotTile(t)
*/
inline DepotPart GetShipDepotPart(Tile t)
{
assert(IsShipDepotTile(t));
return (DepotPart)GB(t.m5(), WBL_DEPOT_PART, 1);
}
/**
* Get the direction of the ship depot.
* @param t Water tile to query.
* @return Direction of the depot.
* @pre IsShipDepotTile(t)
*/
inline DiagDirection GetShipDepotDirection(Tile t)
{
return XYNSToDiagDir(GetShipDepotAxis(t), GetShipDepotPart(t));
}
/**
* Get the other tile of the ship depot.
* @param t Tile to query, containing one section of a ship depot.
* @return Tile containing the other section of the depot.
* @pre IsShipDepotTile(t)
*/
inline TileIndex GetOtherShipDepotTile(Tile t)
{
return TileIndex(t) + (GetShipDepotPart(t) != DEPOT_PART_NORTH ? -1 : 1) * (GetShipDepotAxis(t) != AXIS_X ? TileDiffXY(0, 1) : TileDiffXY(1, 0));
}
/**
* Get the most northern tile of a ship depot.
* @param t One of the tiles of the ship depot.
* @return The northern tile of the depot.
* @pre IsShipDepotTile(t)
*/
inline TileIndex GetShipDepotNorthTile(Tile t)
{
assert(IsShipDepot(t));
TileIndex tile2 = GetOtherShipDepotTile(t);
return t < tile2 ? TileIndex(t) : tile2;
}
/**
* Is there a lock on a given water tile?
* @param t Water tile to query.
* @return \c true if it is a water lock tile.
* @pre IsTileType(t, MP_WATER)
*/
inline bool IsLock(Tile t)
{
return GetWaterTileType(t) == WATER_TILE_LOCK;
}
/**
* Get the direction of the water lock.
* @param t Water tile to query.
* @return Direction of the lock.
* @pre IsTileType(t, MP_WATER) && IsLock(t)
*/
inline DiagDirection GetLockDirection(Tile t)
{
assert(IsLock(t));
return (DiagDirection)GB(t.m5(), WBL_LOCK_ORIENT_BEGIN, WBL_LOCK_ORIENT_COUNT);
}
/**
* Get the part of a lock.
* @param t Water tile to query.
* @return The part.
* @pre IsTileType(t, MP_WATER) && IsLock(t)
*/
inline byte GetLockPart(Tile t)
{
assert(IsLock(t));
return GB(t.m5(), WBL_LOCK_PART_BEGIN, WBL_LOCK_PART_COUNT);
}
/**
* Get the random bits of the water tile.
* @param t Water tile to query.
* @return Random bits of the tile.
* @pre IsTileType(t, MP_WATER)
*/
inline byte GetWaterTileRandomBits(Tile t)
{
assert(IsTileType(t, MP_WATER));
return t.m4();
}
/**
* Checks whether the tile has water at the ground.
* That is, it is either some plain water tile, or a object/industry/station/... with water under it.
* @return true iff the tile has water at the ground.
* @note Coast tiles are not considered waterish, even if there is water on a halftile.
*/
inline bool HasTileWaterGround(Tile t)
{
return HasTileWaterClass(t) && IsTileOnWater(t) && !IsCoastTile(t);
}
/**
* Set the docking tile state of a tile. This is used by pathfinders to reach their destination.
* As well as water tiles, half-rail tiles, buoys and aqueduct ends can also be docking tiles.
* @param t the tile
* @param b the docking tile state
*/
inline void SetDockingTile(Tile t, bool b)
{
assert(IsTileType(t, MP_WATER) || IsTileType(t, MP_RAILWAY) || IsTileType(t, MP_STATION) || IsTileType(t, MP_TUNNELBRIDGE));
SB(t.m1(), 7, 1, b ? 1 : 0);
}
/**
* Checks whether the tile is marked as a dockling tile.
* @return true iff the tile is marked as a docking tile.
*/
inline bool IsDockingTile(Tile t)
{
return (IsTileType(t, MP_WATER) || IsTileType(t, MP_RAILWAY) || IsTileType(t, MP_STATION) || IsTileType(t, MP_TUNNELBRIDGE)) && HasBit(t.m1(), 7);
}
/**
* Helper function to make a coast tile.
* @param t The tile to change into water
*/
inline void MakeShore(Tile t)
{
SetTileType(t, MP_WATER);
SetTileOwner(t, OWNER_WATER);
SetWaterClass(t, WATER_CLASS_SEA);
SetDockingTile(t, false);
t.m2() = 0;
t.m3() = 0;
t.m4() = 0;
t.m5() = WBL_TYPE_NORMAL << WBL_TYPE_BEGIN | 1 << WBL_COAST_FLAG;
SB(t.m6(), 2, 4, 0);
t.m7() = 0;
}
/**
* Helper function for making a watery tile.
* @param t The tile to change into water
* @param o The owner of the water
* @param wc The class of water the tile has to be
* @param random_bits Eventual random bits to be set for this tile
*/
inline void MakeWater(Tile t, Owner o, WaterClass wc, uint8_t random_bits)
{
SetTileType(t, MP_WATER);
SetTileOwner(t, o);
SetWaterClass(t, wc);
SetDockingTile(t, false);
t.m2() = 0;
t.m3() = 0;
t.m4() = random_bits;
t.m5() = WBL_TYPE_NORMAL << WBL_TYPE_BEGIN;
SB(t.m6(), 2, 4, 0);
t.m7() = 0;
}
/**
* Make a sea tile.
* @param t The tile to change into sea
*/
inline void MakeSea(Tile t)
{
MakeWater(t, OWNER_WATER, WATER_CLASS_SEA, 0);
}
/**
* Make a river tile
* @param t The tile to change into river
* @param random_bits Random bits to be set for this tile
*/
inline void MakeRiver(Tile t, uint8_t random_bits)
{
MakeWater(t, OWNER_WATER, WATER_CLASS_RIVER, random_bits);
}
/**
* Make a canal tile
* @param t The tile to change into canal
* @param o The owner of the canal
* @param random_bits Random bits to be set for this tile
*/
inline void MakeCanal(Tile t, Owner o, uint8_t random_bits)
{
assert(o != OWNER_WATER);
MakeWater(t, o, WATER_CLASS_CANAL, random_bits);
}
/**
* Make a ship depot section.
* @param t Tile to place the ship depot section.
* @param o Owner of the depot.
* @param did Depot ID.
* @param part Depot part (either #DEPOT_PART_NORTH or #DEPOT_PART_SOUTH).
* @param a Axis of the depot.
* @param original_water_class Original water class.
*/
inline void MakeShipDepot(Tile t, Owner o, DepotID did, DepotPart part, Axis a, WaterClass original_water_class)
{
SetTileType(t, MP_WATER);
SetTileOwner(t, o);
SetWaterClass(t, original_water_class);
SetDockingTile(t, false);
t.m2() = did;
t.m3() = 0;
t.m4() = 0;
t.m5() = WBL_TYPE_DEPOT << WBL_TYPE_BEGIN | part << WBL_DEPOT_PART | a << WBL_DEPOT_AXIS;
SB(t.m6(), 2, 4, 0);
t.m7() = 0;
}
/**
* Make a lock section.
* @param t Tile to place the water lock section.
* @param o Owner of the lock.
* @param part Part to place.
* @param dir Lock orientation
* @param original_water_class Original water class.
* @see MakeLock
*/
inline void MakeLockTile(Tile t, Owner o, LockPart part, DiagDirection dir, WaterClass original_water_class)
{
SetTileType(t, MP_WATER);
SetTileOwner(t, o);
SetWaterClass(t, original_water_class);
SetDockingTile(t, false);
t.m2() = 0;
t.m3() = 0;
t.m4() = 0;
t.m5() = WBL_TYPE_LOCK << WBL_TYPE_BEGIN | part << WBL_LOCK_PART_BEGIN | dir << WBL_LOCK_ORIENT_BEGIN;
SB(t.m6(), 2, 4, 0);
t.m7() = 0;
}
/**
* Make a water lock.
* @param t Tile to place the water lock section.
* @param o Owner of the lock.
* @param d Direction of the water lock.
* @param wc_lower Original water class of the lower part.
* @param wc_upper Original water class of the upper part.
* @param wc_middle Original water class of the middle part.
*/
inline void MakeLock(Tile t, Owner o, DiagDirection d, WaterClass wc_lower, WaterClass wc_upper, WaterClass wc_middle)
{
TileIndexDiff delta = TileOffsByDiagDir(d);
Tile lower_tile = TileIndex(t) - delta;
Tile upper_tile = TileIndex(t) + delta;
/* Keep the current waterclass and owner for the tiles.
* It allows to restore them after the lock is deleted */
MakeLockTile(t, o, LOCK_PART_MIDDLE, d, wc_middle);
MakeLockTile(lower_tile, IsWaterTile(lower_tile) ? GetTileOwner(lower_tile) : o, LOCK_PART_LOWER, d, wc_lower);
MakeLockTile(upper_tile, IsWaterTile(upper_tile) ? GetTileOwner(upper_tile) : o, LOCK_PART_UPPER, d, wc_upper);
}
#endif /* WATER_MAP_H */
|