# 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 "base_station_base.h"
    00016 #include "newgrf_airport.h"
    00017 #include "cargopacket.h"
    00018 #include "industry_type.h"
    00019 #include "newgrf_storage.h"
    00020
    00021 typedef Pool<BaseStation, StationID, 32, 64000> StationPool;
    00022 extern StationPool _station_pool;
    00023
    00024 static const byte INITIAL_STATION_RATING = 175;
    00025
    00026 struct GoodsEntry {
    00027   enum AcceptancePickup {
    00028     ACCEPTANCE,
    00029     PICKUP
    00030   };
    00031
    00032   GoodsEntry() :
    00033     acceptance_pickup(0),
    00034     days_since_pickup(255),
    00035     rating(INITIAL_STATION_RATING),
    00036     last_speed(0),
    00037     last_age(255)
    00038   {}
    00039
    00040   byte acceptance_pickup;
    00041   byte days_since_pickup;
    00042   byte rating;
    00043   byte last_speed;
    00044   byte last_age;
    00045   byte amount_fract;
    00046   StationCargoList cargo;
    00047 };
    00048
    00050 struct Airport : public TileArea {
    00051   typedef PersistentStorageArray<int32, 16> PersistentStorage;
    00052
    00053   Airport() : TileArea(INVALID_TILE, 0, 0) {}
    00054
    00055   uint64 flags;
    00056   byte type;
    00057   byte layout;
    00058   Direction rotation;
    00059   PersistentStorage psa;
    00060
    00066   const AirportSpec *GetSpec() const
    00067   {
    00068     if (this->tile == INVALID_TILE) return &AirportSpec::dummy;
    00069     return AirportSpec::Get(this->type);
    00070   }
    00071
    00078   const AirportFTAClass *GetFTA() const
    00079   {
    00080     return this->GetSpec()->fsm;
    00081   }
    00082
    00084   FORCEINLINE bool HasHangar() const
    00085   {
    00086     return this->GetSpec()->nof_depots > 0;
    00087   }
    00088
    00097   FORCEINLINE TileIndex GetRotatedTileFromOffset(TileIndexDiffC tidc) const
    00098   {
    00099     const AirportSpec *as = this->GetSpec();
    00100     switch (this->rotation) {
    00101       case DIR_N: return this->tile + ToTileIndexDiff(tidc);
    00102
    00103       case DIR_E: return this->tile + TileDiffXY(tidc.y, as->size_x - 1 - tidc.x);
    00104
    00105       case DIR_S: return this->tile + TileDiffXY(as->size_x - 1 - tidc.x, as->size_y - 1 - tidc.y);
    00106
    00107       case DIR_W: return this->tile + TileDiffXY(as->size_y - 1 - tidc.y, tidc.x);
    00108
    00109       default: NOT_REACHED();
    00110     }
    00111   }
    00112
    00119   FORCEINLINE TileIndex GetHangarTile(uint hangar_num) const
    00120   {
    00121     const AirportSpec *as = this->GetSpec();
    00122     for (uint i = 0; i < as->nof_depots; i++) {
    00123       if (as->depot_table[i].hangar_num == hangar_num) {
    00124         return this->GetRotatedTileFromOffset(as->depot_table[i].ti);
    00125       }
    00126     }
    00127     NOT_REACHED();
    00128   }
    00129
    00136   FORCEINLINE uint GetHangarNum(TileIndex tile) const
    00137   {
    00138     const AirportSpec *as = this->GetSpec();
    00139     for (uint i = 0; i < as->nof_depots; i++) {
    00140       if (this->GetRotatedTileFromOffset(as->depot_table[i].ti) == tile) {
    00141         return as->depot_table[i].hangar_num;
    00142       }
    00143     }
    00144     NOT_REACHED();
    00145   }
    00146
    00148   FORCEINLINE uint GetNumHangars() const
    00149   {
    00150     uint num = 0;
    00151     uint counted = 0;
    00152     const AirportSpec *as = this->GetSpec();
    00153     for (uint i = 0; i < as->nof_depots; i++) {
    00154       if (!HasBit(counted, as->depot_table[i].hangar_num)) {
    00155         num++;
    00156         SetBit(counted, as->depot_table[i].hangar_num);
    00157       }
    00158     }
    00159     return num;
    00160   }
    00161 };
    00162
    00163 typedef SmallVector<Industry *, 2> IndustryVector;
    00164
    00166 struct Station : SpecializedStation<Station, false> {
    00167 public:
    00168   RoadStop *GetPrimaryRoadStop(RoadStopType type) const
    00169   {
    00170     return type == ROADSTOP_BUS ? bus_stops : truck_stops;
    00171   }
    00172
    00173   RoadStop *GetPrimaryRoadStop(const struct RoadVehicle *v) const;
    00174
    00175   RoadStop *bus_stops;
    00176   TileArea bus_station;
    00177   RoadStop *truck_stops;
    00178   TileArea truck_station;
    00179
    00180   Airport airport;
    00181   TileIndex dock_tile;
    00182
    00183   IndustryType indtype;
    00184
    00185   StationHadVehicleOfTypeByte had_vehicle_of_type;
    00186
    00187   byte time_since_load;
    00188   byte time_since_unload;
    00189
    00190   byte last_vehicle_type;
    00191   std::list<Vehicle *> loading_vehicles;
    00192   GoodsEntry goods[NUM_CARGO];
    00193   uint32 always_accepted;
    00194
    00195   IndustryVector industries_near;
    00196
    00197   Station(TileIndex tile = INVALID_TILE);
    00198   ~Station();
    00199
    00200   void AddFacility(StationFacility new_facility_bit, TileIndex facil_xy);
    00201
    00202   void MarkTilesDirty(bool cargo_change) const;
    00203
    00204   void UpdateVirtCoord();
    00205
    00206   /* virtual */ uint GetPlatformLength(TileIndex tile, DiagDirection dir) const;
    00207   /* virtual */ uint GetPlatformLength(TileIndex tile) const;
    00208   void RecomputeIndustriesNear();
    00209   static void RecomputeIndustriesNearForAll();
    00210
    00211   uint GetCatchmentRadius() const;
    00212   Rect GetCatchmentRect() const;
    00213
    00214   /* virtual */ FORCEINLINE bool TileBelongsToRailStation(TileIndex tile) const
    00215   {
    00216     return IsRailStationTile(tile) && GetStationIndex(tile) == this->index;
    00217   }
    00218
    00219   FORCEINLINE bool TileBelongsToAirport(TileIndex tile) const
    00220   {
    00221     return IsAirportTile(tile) && GetStationIndex(tile) == this->index;
    00222   }
    00223
    00224   /* virtual */ uint32 GetNewGRFVariable(const ResolverObject *object, byte variable, byte parameter, bool *available) const;
    00225
    00226   /* virtual */ void GetTileArea(TileArea *ta, StationType type) const;
    00227 };
    00228
    00229 #define FOR_ALL_STATIONS(var) FOR_ALL_BASE_STATIONS_OF_TYPE(Station, var)
    00230
    00231 #endif /* STATION_BASE_H */