base_station_base.h

Go to the documentation of this file.
00001 /* $Id$ */
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 AreaInExtendedRect(const TileArea& area, int distance = 0) const;
00043   bool IsEmpty() const;
00044   CommandCost BeforeAddTile(TileIndex tile, StationRectMode mode);
00045   CommandCost BeforeAddRect(TileIndex tile, int w, int h, StationRectMode mode);
00046   bool AfterRemoveTile(BaseStation *st, TileIndex tile);
00047   bool AfterRemoveRect(BaseStation *st, TileArea ta);
00048 
00049   static bool ScanForStationTiles(StationID st_id, int left_a, int top_a, int right_a, int bottom_a);
00050 
00051   StationRect& operator = (const Rect &src);
00052 };
00053 
00055 struct BaseStation : StationPool::PoolItem<&_station_pool> {
00056   TileIndex xy;                   
00057   ViewportSign sign;              
00058   byte delete_ctr;                
00059 
00060   char *name;                     
00061   StringID string_id;             
00062 
00063   Town *town;                     
00064   OwnerByte owner;                
00065   StationFacilityByte facilities; 
00066 
00067   uint8 num_specs;                
00068   StationSpecList *speclist;      
00069 
00070   Date build_date;                
00071 
00072   uint16 random_bits;             
00073   byte waiting_triggers;          
00074   uint8 cached_anim_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 FORCEINLINE BaseStation *GetByTile(TileIndex tile)
00144   {
00145     return BaseStation::Get(GetStationIndex(tile));
00146   }
00147 
00154   FORCEINLINE 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   FORCEINLINE SpecializedStation<T, Tis_waypoint>(TileIndex tile) :
00177       BaseStation(tile)
00178   {
00179     this->facilities = EXPECTED_FACIL;
00180   }
00181 
00187   static FORCEINLINE bool IsExpected(const BaseStation *st)
00188   {
00189     return (st->facilities & FACIL_WAYPOINT) == EXPECTED_FACIL;
00190   }
00191 
00197   static FORCEINLINE bool IsValidID(size_t index)
00198   {
00199     return BaseStation::IsValidID(index) && IsExpected(BaseStation::Get(index));
00200   }
00201 
00206   static FORCEINLINE T *Get(size_t index)
00207   {
00208     return (T *)BaseStation::Get(index);
00209   }
00210 
00215   static FORCEINLINE T *GetIfValid(size_t index)
00216   {
00217     return IsValidID(index) ? Get(index) : NULL;
00218   }
00219 
00225   static FORCEINLINE T *GetByTile(TileIndex tile)
00226   {
00227     return GetIfValid(GetStationIndex(tile));
00228   }
00229 
00235   static FORCEINLINE T *From(BaseStation *st)
00236   {
00237     assert(IsExpected(st));
00238     return (T *)st;
00239   }
00240 
00246   static FORCEINLINE 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 */

Generated on Sun May 8 07:30:10 2011 for OpenTTD by  doxygen 1.6.1