base_station_base.h

Go to the documentation of this file.
00001 /* $Id: base_station_base.h 21593 2010-12-22 10:50:32Z alberth $ */
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 
00075   TileArea train_station;         
00076   StationRect rect;               
00077 
00082   BaseStation(TileIndex tile) :
00083     xy(tile),
00084     train_station(INVALID_TILE, 0, 0)
00085   {
00086   }
00087 
00088   virtual ~BaseStation();
00089 
00095   virtual bool TileBelongsToRailStation(TileIndex tile) const = 0;
00096 
00105   virtual uint32 GetNewGRFVariable(const struct ResolverObject *object, byte variable, byte parameter, bool *available) const = 0;
00106 
00110   virtual void UpdateVirtCoord() = 0;
00111 
00117   virtual void GetTileArea(TileArea *ta, StationType type) const = 0;
00118 
00119 
00126   virtual uint GetPlatformLength(TileIndex tile) const = 0;
00127 
00135   virtual uint GetPlatformLength(TileIndex tile, DiagDirection dir) const = 0;
00136 
00142   static FORCEINLINE BaseStation *GetByTile(TileIndex tile)
00143   {
00144     return BaseStation::Get(GetStationIndex(tile));
00145   }
00146 
00153   FORCEINLINE bool IsInUse() const
00154   {
00155     return (this->facilities & ~FACIL_WAYPOINT) != 0;
00156   }
00157 
00158   static void PostDestructor(size_t index);
00159 };
00160 
00161 #define FOR_ALL_BASE_STATIONS(var) FOR_ALL_ITEMS_FROM(BaseStation, station_index, var, 0)
00162 
00167 template <class T, bool Tis_waypoint>
00168 struct SpecializedStation : public BaseStation {
00169   static const StationFacility EXPECTED_FACIL = Tis_waypoint ? FACIL_WAYPOINT : FACIL_NONE; 
00170 
00175   FORCEINLINE SpecializedStation<T, Tis_waypoint>(TileIndex tile) :
00176       BaseStation(tile)
00177   {
00178     this->facilities = EXPECTED_FACIL;
00179   }
00180 
00186   static FORCEINLINE bool IsExpected(const BaseStation *st)
00187   {
00188     return (st->facilities & FACIL_WAYPOINT) == EXPECTED_FACIL;
00189   }
00190 
00196   static FORCEINLINE bool IsValidID(size_t index)
00197   {
00198     return BaseStation::IsValidID(index) && IsExpected(BaseStation::Get(index));
00199   }
00200 
00205   static FORCEINLINE T *Get(size_t index)
00206   {
00207     return (T *)BaseStation::Get(index);
00208   }
00209 
00214   static FORCEINLINE T *GetIfValid(size_t index)
00215   {
00216     return IsValidID(index) ? Get(index) : NULL;
00217   }
00218 
00224   static FORCEINLINE T *GetByTile(TileIndex tile)
00225   {
00226     return GetIfValid(GetStationIndex(tile));
00227   }
00228 
00234   static FORCEINLINE T *From(BaseStation *st)
00235   {
00236     assert(IsExpected(st));
00237     return (T *)st;
00238   }
00239 
00245   static FORCEINLINE const T *From(const BaseStation *st)
00246   {
00247     assert(IsExpected(st));
00248     return (const T *)st;
00249   }
00250 };
00251 
00252 #define FOR_ALL_BASE_STATIONS_OF_TYPE(name, var) FOR_ALL_ITEMS_FROM(name, station_index, var, 0) if (name::IsExpected(var))
00253 
00254 #endif /* BASE_STATION_BASE_H */

Generated on Sun May 15 19:20:06 2011 for OpenTTD by  doxygen 1.6.1