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 STATION_BASE_H
00013 #define STATION_BASE_H
00014 
00015 #include "core/random_func.hpp"
00016 #include "base_station_base.h"
00017 #include "newgrf_airport.h"
00018 #include "cargopacket.h"
00019 #include "industry_type.h"
00020 #include "linkgraph/linkgraph_type.h"
00021 #include "newgrf_storage.h"
00022 #include <map>
00023 
00024 typedef Pool<BaseStation, StationID, 32, 64000> StationPool;
00025 extern StationPool _station_pool;
00026 
00027 static const byte INITIAL_STATION_RATING = 175;
00028 
00036 class FlowStat {
00037 public:
00038   typedef std::map<uint32, StationID> SharesMap;
00039 
00040   inline FlowStat() {NOT_REACHED();}
00041 
00042   inline FlowStat(StationID st, uint flow)
00043   {
00044     assert(flow > 0);
00045     this->shares[flow] = st;
00046   }
00047 
00055   inline void AppendShare(StationID st, uint flow)
00056   {
00057     assert(flow > 0);
00058     this->shares[(--this->shares.end())->first + flow] = st;
00059   }
00060 
00061   uint GetShare(StationID st) const;
00062 
00063   void ChangeShare(StationID st, int flow);
00064 
00065   inline const SharesMap *GetShares() const { return &this->shares; }
00066 
00074   inline StationID GetVia() const
00075   {
00076     assert(!this->shares.empty());
00077     return this->shares.upper_bound(RandomRange((--this->shares.end())->first - 1))->second;
00078   }
00079 
00080   StationID GetVia(StationID excluded, StationID excluded2 = INVALID_STATION) const;
00081 
00082 private:
00083   SharesMap shares;  
00084 };
00085 
00087 class FlowStatMap : public std::map<StationID, FlowStat> {
00088 public:
00089   void AddFlow(StationID origin, StationID via, uint amount);
00090   void PassOnFlow(StationID origin, StationID via, uint amount);
00091   void DeleteFlows(StationID via);
00092   void FinalizeLocalConsumption(StationID self);
00093 };
00094 
00098 struct GoodsEntry {
00100   enum GoodsEntryStatus {
00105     GES_ACCEPTANCE,
00106 
00114     GES_PICKUP,
00115 
00120     GES_EVER_ACCEPTED,
00121 
00126     GES_LAST_MONTH,
00127 
00132     GES_CURRENT_MONTH,
00133 
00138     GES_ACCEPTED_BIGTICK,
00139   };
00140 
00141   GoodsEntry() :
00142     acceptance_pickup(0),
00143     time_since_pickup(255),
00144     rating(INITIAL_STATION_RATING),
00145     last_speed(0),
00146     last_age(255),
00147     link_graph(INVALID_LINK_GRAPH),
00148     node(INVALID_NODE),
00149     max_waiting_cargo(0)
00150   {}
00151 
00152   byte acceptance_pickup; 
00153 
00159   byte time_since_pickup;
00160 
00161   byte rating;            
00162 
00172   byte last_speed;
00173 
00178   byte last_age;
00179 
00180   byte amount_fract;      
00181   StationCargoList cargo; 
00182 
00183   LinkGraphID link_graph; 
00184   NodeID node;            
00185   FlowStatMap flows;      
00186   uint max_waiting_cargo; 
00187 
00193   bool HasVehicleEverTriedLoading() const { return this->last_speed != 0; }
00194 
00199   inline bool HasRating() const
00200   {
00201     return HasBit(this->acceptance_pickup, GES_PICKUP);
00202   }
00203 
00204   uint GetSumFlowVia(StationID via) const;
00205 
00212   inline StationID GetVia(StationID source) const
00213   {
00214     FlowStatMap::const_iterator flow_it(this->flows.find(source));
00215     return flow_it != this->flows.end() ? flow_it->second.GetVia() : INVALID_STATION;
00216   }
00217 
00226   inline StationID GetVia(StationID source, StationID excluded, StationID excluded2 = INVALID_STATION) const
00227   {
00228     FlowStatMap::const_iterator flow_it(this->flows.find(source));
00229     return flow_it != this->flows.end() ? flow_it->second.GetVia(excluded, excluded2) : INVALID_STATION;
00230   }
00231 };
00232 
00234 struct Airport : public TileArea {
00235   Airport() : TileArea(INVALID_TILE, 0, 0) {}
00236 
00237   uint64 flags;       
00238   byte type;          
00239   byte layout;        
00240   Direction rotation; 
00241 
00242   PersistentStorage *psa; 
00243 
00249   const AirportSpec *GetSpec() const
00250   {
00251     if (this->tile == INVALID_TILE) return &AirportSpec::dummy;
00252     return AirportSpec::Get(this->type);
00253   }
00254 
00261   const AirportFTAClass *GetFTA() const
00262   {
00263     return this->GetSpec()->fsm;
00264   }
00265 
00267   inline bool HasHangar() const
00268   {
00269     return this->GetSpec()->nof_depots > 0;
00270   }
00271 
00280   inline TileIndex GetRotatedTileFromOffset(TileIndexDiffC tidc) const
00281   {
00282     const AirportSpec *as = this->GetSpec();
00283     switch (this->rotation) {
00284       case DIR_N: return this->tile + ToTileIndexDiff(tidc);
00285 
00286       case DIR_E: return this->tile + TileDiffXY(tidc.y, as->size_x - 1 - tidc.x);
00287 
00288       case DIR_S: return this->tile + TileDiffXY(as->size_x - 1 - tidc.x, as->size_y - 1 - tidc.y);
00289 
00290       case DIR_W: return this->tile + TileDiffXY(as->size_y - 1 - tidc.y, tidc.x);
00291 
00292       default: NOT_REACHED();
00293     }
00294   }
00295 
00302   inline TileIndex GetHangarTile(uint hangar_num) const
00303   {
00304     const AirportSpec *as = this->GetSpec();
00305     for (uint i = 0; i < as->nof_depots; i++) {
00306       if (as->depot_table[i].hangar_num == hangar_num) {
00307         return this->GetRotatedTileFromOffset(as->depot_table[i].ti);
00308       }
00309     }
00310     NOT_REACHED();
00311   }
00312 
00319   inline Direction GetHangarExitDirection(TileIndex tile) const
00320   {
00321     const AirportSpec *as = this->GetSpec();
00322     const HangarTileTable *htt = GetHangarDataByTile(tile);
00323     return ChangeDir(htt->dir, DirDifference(this->rotation, as->rotation[0]));
00324   }
00325 
00332   inline uint GetHangarNum(TileIndex tile) const
00333   {
00334     const HangarTileTable *htt = GetHangarDataByTile(tile);
00335     return htt->hangar_num;
00336   }
00337 
00339   inline uint GetNumHangars() const
00340   {
00341     uint num = 0;
00342     uint counted = 0;
00343     const AirportSpec *as = this->GetSpec();
00344     for (uint i = 0; i < as->nof_depots; i++) {
00345       if (!HasBit(counted, as->depot_table[i].hangar_num)) {
00346         num++;
00347         SetBit(counted, as->depot_table[i].hangar_num);
00348       }
00349     }
00350     return num;
00351   }
00352 
00353 private:
00360   inline const HangarTileTable *GetHangarDataByTile(TileIndex tile) const
00361   {
00362     const AirportSpec *as = this->GetSpec();
00363     for (uint i = 0; i < as->nof_depots; i++) {
00364       if (this->GetRotatedTileFromOffset(as->depot_table[i].ti) == tile) {
00365         return as->depot_table + i;
00366       }
00367     }
00368     NOT_REACHED();
00369   }
00370 };
00371 
00372 typedef SmallVector<Industry *, 2> IndustryVector;
00373 
00375 struct Station FINAL : SpecializedStation<Station, false> {
00376 public:
00377   RoadStop *GetPrimaryRoadStop(RoadStopType type) const
00378   {
00379     return type == ROADSTOP_BUS ? bus_stops : truck_stops;
00380   }
00381 
00382   RoadStop *GetPrimaryRoadStop(const struct RoadVehicle *v) const;
00383 
00384   RoadStop *bus_stops;    
00385   TileArea bus_station;   
00386   RoadStop *truck_stops;  
00387   TileArea truck_station; 
00388 
00389   Airport airport;        
00390   TileIndex dock_tile;    
00391 
00392   IndustryType indtype;   
00393 
00394   StationHadVehicleOfTypeByte had_vehicle_of_type;
00395 
00396   byte time_since_load;
00397   byte time_since_unload;
00398 
00399   byte last_vehicle_type;
00400   std::list<Vehicle *> loading_vehicles;
00401   GoodsEntry goods[NUM_CARGO];  
00402   uint32 always_accepted;       
00403 
00404   IndustryVector industries_near; 
00405 
00406   Station(TileIndex tile = INVALID_TILE);
00407   ~Station();
00408 
00409   void AddFacility(StationFacility new_facility_bit, TileIndex facil_xy);
00410 
00411   void MarkTilesDirty(bool cargo_change) const;
00412 
00413   void UpdateVirtCoord();
00414 
00415   /* virtual */ uint GetPlatformLength(TileIndex tile, DiagDirection dir) const;
00416   /* virtual */ uint GetPlatformLength(TileIndex tile) const;
00417   void RecomputeIndustriesNear();
00418   static void RecomputeIndustriesNearForAll();
00419 
00420   uint GetCatchmentRadius() const;
00421   Rect GetCatchmentRect() const;
00422 
00423   /* virtual */ inline bool TileBelongsToRailStation(TileIndex tile) const
00424   {
00425     return IsRailStationTile(tile) && GetStationIndex(tile) == this->index;
00426   }
00427 
00428   inline bool TileBelongsToAirport(TileIndex tile) const
00429   {
00430     return IsAirportTile(tile) && GetStationIndex(tile) == this->index;
00431   }
00432 
00433   /* virtual */ uint32 GetNewGRFVariable(const ResolverObject *object, byte variable, byte parameter, bool *available) const;
00434 
00435   /* virtual */ void GetTileArea(TileArea *ta, StationType type) const;
00436 
00437   void RunAverages();
00438 };
00439 
00440 #define FOR_ALL_STATIONS(var) FOR_ALL_BASE_STATIONS_OF_TYPE(Station, var)
00441 
00443 class AirportTileIterator : public OrthogonalTileIterator {
00444 private:
00445   const Station *st; 
00446 
00447 public:
00452   AirportTileIterator(const Station *st) : OrthogonalTileIterator(st->airport), st(st)
00453   {
00454     if (!st->TileBelongsToAirport(this->tile)) ++(*this);
00455   }
00456 
00457   inline TileIterator& operator ++()
00458   {
00459     (*this).OrthogonalTileIterator::operator++();
00460     while (this->tile != INVALID_TILE && !st->TileBelongsToAirport(this->tile)) {
00461       (*this).OrthogonalTileIterator::operator++();
00462     }
00463     return *this;
00464   }
00465 
00466   virtual TileIterator *Clone() const
00467   {
00468     return new AirportTileIterator(*this);
00469   }
00470 };
00471 
00472 #endif /* STATION_BASE_H */