# water_map.h

Go to the documentation of this file.

    00001 /* $Id: water_map.h 17817 2009-10-20 12:31:11Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #ifndef WATER_MAP_H
    00013 #define WATER_MAP_H
    00014
    00015 #include "core/math_func.hpp"
    00016 #include "depot_type.h"
    00017
    00018 enum WaterTileType {
    00019   WATER_TILE_CLEAR,
    00020   WATER_TILE_COAST,
    00021   WATER_TILE_LOCK,
    00022   WATER_TILE_DEPOT,
    00023 };
    00024
    00025 enum WaterClass {
    00026   WATER_CLASS_SEA,
    00027   WATER_CLASS_CANAL,
    00028   WATER_CLASS_RIVER,
    00029   WATER_CLASS_INVALID,
    00030 };
    00031
    00032 enum DepotPart {
    00033   DEPOT_NORTH = 0x80,
    00034   DEPOT_SOUTH = 0x81,
    00035   DEPOT_END   = 0x84,
    00036 };
    00037
    00038 enum LockPart {
    00039   LOCK_MIDDLE = 0x10,
    00040   LOCK_LOWER  = 0x14,
    00041   LOCK_UPPER  = 0x18,
    00042   LOCK_END    = 0x1C
    00043 };
    00044
    00045 static inline WaterTileType GetWaterTileType(TileIndex t)
    00046 {
    00047   assert(IsTileType(t, MP_WATER));
    00048
    00049   if (_m[t].m5 == 0) return WATER_TILE_CLEAR;
    00050   if (_m[t].m5 == 1) return WATER_TILE_COAST;
    00051   if (IsInsideMM(_m[t].m5, LOCK_MIDDLE, LOCK_END)) return WATER_TILE_LOCK;
    00052
    00053   assert(IsInsideMM(_m[t].m5, DEPOT_NORTH, DEPOT_END));
    00054   return WATER_TILE_DEPOT;
    00055 }
    00056
    00057 static inline WaterClass GetWaterClass(TileIndex t)
    00058 {
    00059   assert(IsTileType(t, MP_WATER) || IsTileType(t, MP_STATION) || IsTileType(t, MP_INDUSTRY));
    00060   return (WaterClass)(IsTileType(t, MP_INDUSTRY) ? GB(_m[t].m1, 5, 2) : GB(_m[t].m3, 0, 2));
    00061 }
    00062
    00063 static inline void SetWaterClass(TileIndex t, WaterClass wc)
    00064 {
    00065   assert(IsTileType(t, MP_WATER) || IsTileType(t, MP_STATION) || IsTileType(t, MP_INDUSTRY));
    00066   if (IsTileType(t, MP_INDUSTRY)) {
    00067     SB(_m[t].m1, 5, 2, wc);
    00068   } else {
    00069     SB(_m[t].m3, 0, 2, wc);
    00070   }
    00071 }
    00072
    00074 static inline bool IsWater(TileIndex t)
    00075 {
    00076   return GetWaterTileType(t) == WATER_TILE_CLEAR;
    00077 }
    00078
    00079 static inline bool IsSea(TileIndex t)
    00080 {
    00081   return IsWater(t) && GetWaterClass(t) == WATER_CLASS_SEA;
    00082 }
    00083
    00084 static inline bool IsCanal(TileIndex t)
    00085 {
    00086   return IsWater(t) && GetWaterClass(t) == WATER_CLASS_CANAL;
    00087 }
    00088
    00089 static inline bool IsRiver(TileIndex t)
    00090 {
    00091   return IsWater(t) && GetWaterClass(t) == WATER_CLASS_RIVER;
    00092 }
    00093
    00094 static inline bool IsWaterTile(TileIndex t)
    00095 {
    00096   return IsTileType(t, MP_WATER) && IsWater(t);
    00097 }
    00098
    00099 static inline bool IsCoast(TileIndex t)
    00100 {
    00101   return GetWaterTileType(t) == WATER_TILE_COAST;
    00102 }
    00103
    00104 static inline TileIndex GetOtherShipDepotTile(TileIndex t)
    00105 {
    00106   return t + (HasBit(_m[t].m5, 0) ? -1 : 1) * (HasBit(_m[t].m5, 1) ? TileDiffXY(0, 1) : TileDiffXY(1, 0));
    00107 }
    00108
    00109 static inline bool IsShipDepot(TileIndex t)
    00110 {
    00111   return IsInsideMM(_m[t].m5, DEPOT_NORTH, DEPOT_END);
    00112 }
    00113
    00114 static inline bool IsShipDepotTile(TileIndex t)
    00115 {
    00116   return IsTileType(t, MP_WATER) && IsShipDepot(t);
    00117 }
    00118
    00119 static inline Axis GetShipDepotAxis(TileIndex t)
    00120 {
    00121   return (Axis)GB(_m[t].m5, 1, 1);
    00122 }
    00123
    00124 static inline DiagDirection GetShipDepotDirection(TileIndex t)
    00125 {
    00126   return XYNSToDiagDir(GetShipDepotAxis(t), GB(_m[t].m5, 0, 1));
    00127 }
    00128
    00129 static inline bool IsLock(TileIndex t)
    00130 {
    00131   return IsInsideMM(_m[t].m5, LOCK_MIDDLE, LOCK_END);
    00132 }
    00133
    00134 static inline DiagDirection GetLockDirection(TileIndex t)
    00135 {
    00136   return (DiagDirection)GB(_m[t].m5, 0, 2);
    00137 }
    00138
    00139 static inline byte GetSection(TileIndex t)
    00140 {
    00141   assert(GetWaterTileType(t) == WATER_TILE_LOCK || GetWaterTileType(t) == WATER_TILE_DEPOT);
    00142   return GB(_m[t].m5, 0, 4);
    00143 }
    00144
    00145 static inline byte GetWaterTileRandomBits(TileIndex t)
    00146 {
    00147   return _m[t].m4;
    00148 }
    00149
    00150
    00151 static inline void MakeShore(TileIndex t)
    00152 {
    00153   SetTileType(t, MP_WATER);
    00154   SetTileOwner(t, OWNER_WATER);
    00155   _m[t].m2 = 0;
    00156   _m[t].m3 = 0;
    00157   _m[t].m4 = 0;
    00158   _m[t].m5 = 1;
    00159   SB(_m[t].m6, 2, 4, 0);
    00160   _me[t].m7 = 0;
    00161 }
    00162
    00170 static inline void MakeWater(TileIndex t, Owner o, WaterClass wc, uint8 random_bits)
    00171 {
    00172   SetTileType(t, MP_WATER);
    00173   SetTileOwner(t, o);
    00174   _m[t].m2 = 0;
    00175   _m[t].m3 = wc;
    00176   _m[t].m4 = random_bits;
    00177   _m[t].m5 = 0;
    00178   SB(_m[t].m6, 2, 4, 0);
    00179   _me[t].m7 = 0;
    00180 }
    00181
    00186 static inline void MakeSea(TileIndex t)
    00187 {
    00188   MakeWater(t, OWNER_WATER, WATER_CLASS_SEA, 0);
    00189 }
    00190
    00196 static inline void MakeRiver(TileIndex t, uint8 random_bits)
    00197 {
    00198   MakeWater(t, OWNER_WATER, WATER_CLASS_RIVER, random_bits);
    00199 }
    00200
    00207 static inline void MakeCanal(TileIndex t, Owner o, uint8 random_bits)
    00208 {
    00209   assert(o != OWNER_WATER);
    00210   MakeWater(t, o, WATER_CLASS_CANAL, random_bits);
    00211 }
    00212
    00213 static inline void MakeShipDepot(TileIndex t, Owner o, DepotID did, DepotPart base, Axis a, WaterClass original_water_class)
    00214 {
    00215   SetTileType(t, MP_WATER);
    00216   SetTileOwner(t, o);
    00217   _m[t].m2 = did;
    00218   _m[t].m3 = original_water_class;
    00219   _m[t].m4 = 0;
    00220   _m[t].m5 = base + a * 2;
    00221   SB(_m[t].m6, 2, 4, 0);
    00222   _me[t].m7 = 0;
    00223 }
    00224
    00225 static inline void MakeLockTile(TileIndex t, Owner o, byte section, WaterClass original_water_class)
    00226 {
    00227   SetTileType(t, MP_WATER);
    00228   SetTileOwner(t, o);
    00229   _m[t].m2 = 0;
    00230   _m[t].m3 = original_water_class;
    00231   _m[t].m4 = 0;
    00232   _m[t].m5 = section;
    00233   SB(_m[t].m6, 2, 4, 0);
    00234   _me[t].m7 = 0;
    00235 }
    00236
    00237 static inline void MakeLock(TileIndex t, Owner o, DiagDirection d, WaterClass wc_lower, WaterClass wc_upper)
    00238 {
    00239   TileIndexDiff delta = TileOffsByDiagDir(d);
    00240
    00241   MakeLockTile(t, o, LOCK_MIDDLE + d, WATER_CLASS_CANAL);
    00242   MakeLockTile(t - delta, o, LOCK_LOWER + d, wc_lower);
    00243   MakeLockTile(t + delta, o, LOCK_UPPER + d, wc_upper);
    00244 }
    00245
    00246 #endif /* WATER_MAP_H */