# base_station_base.h

Go to the documentation of this file.

    00001 /* $Id: base_station_base.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 BASE_STATION_BASE_H
    00013 #define BASE_STATION_BASE_H
    00014
    00015 #include "core/pool_type.hpp"
    00016 #include "viewport_type.h"
    00017 #include "station_map.h"
    00018
    00019 typedef Pool<BaseStation, StationID, 32, 64000> StationPool;
    00020 extern StationPool _station_pool;
    00021
    00022 struct StationSpecList {
    00023   const StationSpec *spec;
    00024   uint32 grfid;
    00025   uint8  localidx;
    00026 };
    00027
    00028
    00030 struct StationRect : public Rect {
    00031   enum StationRectMode
    00032   {
    00033     ADD_TEST = 0,
    00034     ADD_TRY,
    00035     ADD_FORCE
    00036   };
    00037
    00038   StationRect();
    00039   void MakeEmpty();
    00040   bool PtInExtendedRect(int x, int y, int distance = 0) const;
    00041   bool IsEmpty() const;
    00042   bool BeforeAddTile(TileIndex tile, StationRectMode mode);
    00043   bool BeforeAddRect(TileIndex tile, int w, int h, StationRectMode mode);
    00044   bool AfterRemoveTile(BaseStation *st, TileIndex tile);
    00045   bool AfterRemoveRect(BaseStation *st, TileIndex tile, int w, int h);
    00046
    00047   static bool ScanForStationTiles(StationID st_id, int left_a, int top_a, int right_a, int bottom_a);
    00048
    00049   StationRect& operator = (Rect src);
    00050 };
    00051
    00053 struct BaseStation : StationPool::PoolItem<&_station_pool> {
    00054   TileIndex xy;
    00055   ViewportSign sign;
    00056   byte delete_ctr;
    00057
    00058   char *name;
    00059   StringID string_id;
    00060
    00061   Town *town;
    00062   OwnerByte owner;
    00063   StationFacilityByte facilities;
    00064
    00065   uint8 num_specs;
    00066   StationSpecList *speclist;
    00067
    00068   Date build_date;
    00069
    00070   uint16 random_bits;
    00071   byte waiting_triggers;
    00072   uint8 cached_anim_triggers;
    00073
    00074   TileArea train_station;
    00075   StationRect rect;
    00076
    00081   BaseStation(TileIndex tile) :
    00082     xy(tile),
    00083     train_station(INVALID_TILE, 0, 0)
    00084   {
    00085   }
    00086
    00087   virtual ~BaseStation();
    00088
    00094   virtual bool TileBelongsToRailStation(TileIndex tile) const = 0;
    00095
    00104   virtual uint32 GetNewGRFVariable(const struct ResolverObject *object, byte variable, byte parameter, bool *available) const = 0;
    00105
    00109   virtual void UpdateVirtCoord() = 0;
    00110
    00116   virtual void GetTileArea(TileArea *ta, StationType type) const = 0;
    00117
    00118
    00125   virtual uint GetPlatformLength(TileIndex tile) const = 0;
    00126
    00134   virtual uint GetPlatformLength(TileIndex tile, DiagDirection dir) const = 0;
    00135
    00141   static FORCEINLINE BaseStation *GetByTile(TileIndex tile)
    00142   {
    00143     return BaseStation::Get(GetStationIndex(tile));
    00144   }
    00145
    00152   FORCEINLINE bool IsInUse() const
    00153   {
    00154     return (this->facilities & ~FACIL_WAYPOINT) != 0;
    00155   }
    00156
    00157   static void PostDestructor(size_t index);
    00158 };
    00159
    00160 #define FOR_ALL_BASE_STATIONS(var) FOR_ALL_ITEMS_FROM(BaseStation, station_index, var, 0)
    00161
    00166 template <class T, bool Tis_waypoint>
    00167 struct SpecializedStation : public BaseStation {
    00168   static const StationFacility EXPECTED_FACIL = Tis_waypoint ? FACIL_WAYPOINT : FACIL_NONE;
    00169
    00174   FORCEINLINE SpecializedStation<T, Tis_waypoint>(TileIndex tile) :
    00175       BaseStation(tile)
    00176   {
    00177     this->facilities = EXPECTED_FACIL;
    00178   }
    00179
    00185   static FORCEINLINE bool IsExpected(const BaseStation *st)
    00186   {
    00187     return (st->facilities & FACIL_WAYPOINT) == EXPECTED_FACIL;
    00188   }
    00189
    00195   static FORCEINLINE bool IsValidID(size_t index)
    00196   {
    00197     return BaseStation::IsValidID(index) && IsExpected(BaseStation::Get(index));
    00198   }
    00199
    00204   static FORCEINLINE T *Get(size_t index)
    00205   {
    00206     return (T *)BaseStation::Get(index);
    00207   }
    00208
    00213   static FORCEINLINE T *GetIfValid(size_t index)
    00214   {
    00215     return IsValidID(index) ? Get(index) : NULL;
    00216   }
    00217
    00223   static FORCEINLINE T *GetByTile(TileIndex tile)
    00224   {
    00225     return GetIfValid(GetStationIndex(tile));
    00226   }
    00227
    00233   static FORCEINLINE T *From(BaseStation *st)
    00234   {
    00235     assert(IsExpected(st));
    00236     return (T *)st;
    00237   }
    00238
    00244   static FORCEINLINE const T *From(const BaseStation *st)
    00245   {
    00246     assert(IsExpected(st));
    00247     return (const T *)st;
    00248   }
    00249 };
    00250
    00251 #define FOR_ALL_BASE_STATIONS_OF_TYPE(name, var) FOR_ALL_ITEMS_FROM(name, station_index, var, 0) if (name::IsExpected(var))
    00252
    00253 #endif /* STATION_BASE_H */