# base_station_base.h

Go to the documentation of this file.

    00001 /* $Id: base_station_base.h 26085 2013-11-24 14:41:19Z frosch $ */
    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 BASE_STATION_BASE_H
    00013 #define BASE_STATION_BASE_H
    00014
    00015 #include "core/pool_type.hpp"
    00016 #include "command_type.h"
    00017 #include "viewport_type.h"
    00018 #include "station_map.h"
    00019
    00020 typedef Pool<BaseStation, StationID, 32, 64000> StationPool;
    00021 extern StationPool _station_pool;
    00022
    00023 struct StationSpecList {
    00024   const StationSpec *spec;
    00025   uint32 grfid;
    00026   uint8  localidx;
    00027 };
    00028
    00029
    00031 struct StationRect : public Rect {
    00032   enum StationRectMode
    00033   {
    00034     ADD_TEST = 0,
    00035     ADD_TRY,
    00036     ADD_FORCE
    00037   };
    00038
    00039   StationRect();
    00040   void MakeEmpty();
    00041   bool PtInExtendedRect(int x, int y, int distance = 0) const;
    00042   bool IsEmpty() const;
    00043   CommandCost BeforeAddTile(TileIndex tile, StationRectMode mode);
    00044   CommandCost BeforeAddRect(TileIndex tile, int w, int h, StationRectMode mode);
    00045   bool AfterRemoveTile(BaseStation *st, TileIndex tile);
    00046   bool AfterRemoveRect(BaseStation *st, TileArea ta);
    00047
    00048   static bool ScanForStationTiles(StationID st_id, int left_a, int top_a, int right_a, int bottom_a);
    00049
    00050   StationRect& operator = (const Rect &src);
    00051 };
    00052
    00054 struct BaseStation : StationPool::PoolItem<&_station_pool> {
    00055   TileIndex xy;
    00056   ViewportSign sign;
    00057   byte delete_ctr;
    00058
    00059   char *name;
    00060   StringID string_id;
    00061
    00062   Town *town;
    00063   OwnerByte owner;
    00064   StationFacilityByte facilities;
    00065
    00066   uint8 num_specs;
    00067   StationSpecList *speclist;
    00068
    00069   Date build_date;
    00070
    00071   uint16 random_bits;
    00072   byte waiting_triggers;
    00073   uint8 cached_anim_triggers;
    00074   uint32 cached_cargo_triggers;
    00075
    00076   TileArea train_station;
    00077   StationRect rect;
    00078
    00083   BaseStation(TileIndex tile) :
    00084     xy(tile),
    00085     train_station(INVALID_TILE, 0, 0)
    00086   {
    00087   }
    00088
    00089   virtual ~BaseStation();
    00090
    00096   virtual bool TileBelongsToRailStation(TileIndex tile) const = 0;
    00097
    00106   virtual uint32 GetNewGRFVariable(const struct ResolverObject &object, byte variable, byte parameter, bool *available) const = 0;
    00107
    00111   virtual void UpdateVirtCoord() = 0;
    00112
    00118   virtual void GetTileArea(TileArea *ta, StationType type) const = 0;
    00119
    00120
    00127   virtual uint GetPlatformLength(TileIndex tile) const = 0;
    00128
    00136   virtual uint GetPlatformLength(TileIndex tile, DiagDirection dir) const = 0;
    00137
    00143   static inline BaseStation *GetByTile(TileIndex tile)
    00144   {
    00145     return BaseStation::Get(GetStationIndex(tile));
    00146   }
    00147
    00154   inline bool IsInUse() const
    00155   {
    00156     return (this->facilities & ~FACIL_WAYPOINT) != 0;
    00157   }
    00158
    00159   static void PostDestructor(size_t index);
    00160 };
    00161
    00162 #define FOR_ALL_BASE_STATIONS(var) FOR_ALL_ITEMS_FROM(BaseStation, station_index, var, 0)
    00163
    00168 template <class T, bool Tis_waypoint>
    00169 struct SpecializedStation : public BaseStation {
    00170   static const StationFacility EXPECTED_FACIL = Tis_waypoint ? FACIL_WAYPOINT : FACIL_NONE;
    00171
    00176   inline SpecializedStation<T, Tis_waypoint>(TileIndex tile) :
    00177       BaseStation(tile)
    00178   {
    00179     this->facilities = EXPECTED_FACIL;
    00180   }
    00181
    00187   static inline bool IsExpected(const BaseStation *st)
    00188   {
    00189     return (st->facilities & FACIL_WAYPOINT) == EXPECTED_FACIL;
    00190   }
    00191
    00197   static inline bool IsValidID(size_t index)
    00198   {
    00199     return BaseStation::IsValidID(index) && IsExpected(BaseStation::Get(index));
    00200   }
    00201
    00206   static inline T *Get(size_t index)
    00207   {
    00208     return (T *)BaseStation::Get(index);
    00209   }
    00210
    00215   static inline T *GetIfValid(size_t index)
    00216   {
    00217     return IsValidID(index) ? Get(index) : NULL;
    00218   }
    00219
    00225   static inline T *GetByTile(TileIndex tile)
    00226   {
    00227     return GetIfValid(GetStationIndex(tile));
    00228   }
    00229
    00235   static inline T *From(BaseStation *st)
    00236   {
    00237     assert(IsExpected(st));
    00238     return (T *)st;
    00239   }
    00240
    00246   static inline const T *From(const BaseStation *st)
    00247   {
    00248     assert(IsExpected(st));
    00249     return (const T *)st;
    00250   }
    00251 };
    00252
    00253 #define FOR_ALL_BASE_STATIONS_OF_TYPE(name, var) FOR_ALL_ITEMS_FROM(name, station_index, var, 0) if (name::IsExpected(var))
    00254
    00255 #endif /* BASE_STATION_BASE_H */