# water_map.h

Go to the documentation of this file.

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