roadveh.h

Go to the documentation of this file.
00001 /* $Id: roadveh.h 21862 2011-01-19 20:04:09Z terkhen $ */
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 ROADVEH_H
00013 #define ROADVEH_H
00014 
00015 #include "ground_vehicle.hpp"
00016 #include "engine_base.h"
00017 #include "cargotype.h"
00018 #include "track_func.h"
00019 #include "road_type.h"
00020 #include "newgrf_properties.h"
00021 #include "newgrf_engine.h"
00022 
00023 struct RoadVehicle;
00024 
00026 enum RoadVehicleStates {
00027   /*
00028    * Lower 4 bits are used for vehicle track direction. (Trackdirs)
00029    * When in a road stop (bit 5 or bit 6 set) these bits give the
00030    * track direction of the entry to the road stop.
00031    * As the entry direction will always be a diagonal
00032    * direction (X_NE, Y_SE, X_SW or Y_NW) only bits 0 and 3
00033    * are needed to hold this direction. Bit 1 is then used to show
00034    * that the vehicle is using the second road stop bay.
00035    * Bit 2 is then used for drive-through stops to show the vehicle
00036    * is stopping at this road stop.
00037    */
00038 
00039   /* Numeric values */
00040   RVSB_IN_DEPOT                = 0xFE,                      
00041   RVSB_WORMHOLE                = 0xFF,                      
00042 
00043   /* Bit numbers */
00044   RVS_USING_SECOND_BAY         =    1,                      
00045   RVS_ENTERED_STOP             =    2,                      
00046   RVS_DRIVE_SIDE               =    4,                      
00047   RVS_IN_ROAD_STOP             =    5,                      
00048   RVS_IN_DT_ROAD_STOP          =    6,                      
00049 
00050   /* Bit sets of the above specified bits */
00051   RVSB_IN_ROAD_STOP            = 1 << RVS_IN_ROAD_STOP,     
00052   RVSB_IN_ROAD_STOP_END        = RVSB_IN_ROAD_STOP + TRACKDIR_END,
00053   RVSB_IN_DT_ROAD_STOP         = 1 << RVS_IN_DT_ROAD_STOP,  
00054   RVSB_IN_DT_ROAD_STOP_END     = RVSB_IN_DT_ROAD_STOP + TRACKDIR_END,
00055 
00056   RVSB_TRACKDIR_MASK           = 0x0F,                      
00057   RVSB_ROAD_STOP_TRACKDIR_MASK = 0x09                       
00058 };
00059 
00061 static const uint RDE_NEXT_TILE = 0x80; 
00062 static const uint RDE_TURNED    = 0x40; 
00063 
00064 /* Start frames for when a vehicle enters a tile/changes its state.
00065  * The start frame is different for vehicles that turned around or
00066  * are leaving the depot as the do not start at the edge of the tile.
00067  * For trams there are a few different start frames as there are two
00068  * places where trams can turn. */
00069 static const uint RVC_DEFAULT_START_FRAME                =  0;
00070 static const uint RVC_TURN_AROUND_START_FRAME            =  1;
00071 static const uint RVC_DEPOT_START_FRAME                  =  6;
00072 static const uint RVC_START_FRAME_AFTER_LONG_TRAM        = 21;
00073 static const uint RVC_TURN_AROUND_START_FRAME_SHORT_TRAM = 16;
00074 /* Stop frame for a vehicle in a drive-through stop */
00075 static const uint RVC_DRIVE_THROUGH_STOP_FRAME           = 11;
00076 static const uint RVC_DEPOT_STOP_FRAME                   = 11;
00077 
00078 void RoadVehUpdateCache(RoadVehicle *v);
00079 
00083 struct RoadVehicle : public GroundVehicle<RoadVehicle, VEH_ROAD> {
00084   byte state;             
00085   byte frame;
00086   uint16 blocked_ctr;
00087   byte overtaking;
00088   byte overtaking_ctr;
00089   uint16 crashed_ctr;
00090   byte reverse_ctr;
00091 
00092   RoadType roadtype;
00093   RoadTypes compatible_roadtypes;
00094 
00096   RoadVehicle() : GroundVehicle<RoadVehicle, VEH_ROAD>() {}
00098   virtual ~RoadVehicle() { this->PreDestructor(); }
00099 
00100   friend struct GroundVehicle<RoadVehicle, VEH_ROAD>; // GroundVehicle needs to use the acceleration functions defined at RoadVehicle.
00101 
00102   const char *GetTypeString() const { return "road vehicle"; }
00103   void MarkDirty();
00104   void UpdateDeltaXY(Direction direction);
00105   ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_ROADVEH_INC : EXPENSES_ROADVEH_RUN; }
00106   bool IsPrimaryVehicle() const { return this->IsFrontEngine(); }
00107   SpriteID GetImage(Direction direction) const;
00108   int GetDisplaySpeed() const { return this->cur_speed / 2; }
00109   int GetDisplayMaxSpeed() const { return this->vcache.cached_max_speed / 2; }
00110   Money GetRunningCost() const;
00111   int GetDisplayImageWidth(Point *offset = NULL) const;
00112   bool IsInDepot() const { return this->state == RVSB_IN_DEPOT; }
00113   bool IsStoppedInDepot() const;
00114   bool Tick();
00115   void OnNewDay();
00116   uint Crash(bool flooded = false);
00117   Trackdir GetVehicleTrackdir() const;
00118   TileIndex GetOrderStationLocation(StationID station);
00119   bool FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse);
00120 
00121   bool IsBus() const;
00122 
00123   int GetCurrentMaxSpeed() const;
00124 
00125 protected: // These functions should not be called outside acceleration code.
00126 
00131   FORCEINLINE uint16 GetPower() const
00132   {
00133     /* Power is not added for articulated parts */
00134     if (!this->IsArticulatedPart()) {
00135       /* Road vehicle power is in units of 10 HP. */
00136       return 10 * GetVehicleProperty(this, PROP_ROADVEH_POWER, RoadVehInfo(this->engine_type)->power);
00137     }
00138     return 0;
00139   }
00140 
00145   FORCEINLINE uint16 GetPoweredPartPower(const RoadVehicle *head) const
00146   {
00147     return 0;
00148   }
00149 
00154   FORCEINLINE uint16 GetWeight() const
00155   {
00156     uint16 weight = (CargoSpec::Get(this->cargo_type)->weight * this->cargo.Count()) / 16;
00157 
00158     /* Vehicle weight is not added for articulated parts. */
00159     if (!this->IsArticulatedPart()) {
00160       /* Road vehicle weight is in units of 1/4 t. */
00161       weight += GetVehicleProperty(this, PROP_ROADVEH_WEIGHT, RoadVehInfo(this->engine_type)->weight) / 4;
00162     }
00163 
00164     return weight;
00165   }
00166 
00171   FORCEINLINE byte GetTractiveEffort() const
00172   {
00173     /* The tractive effort coefficient is in units of 1/256.  */
00174     return GetVehicleProperty(this, PROP_ROADVEH_TRACTIVE_EFFORT, RoadVehInfo(this->engine_type)->tractive_effort);
00175   }
00176 
00181   FORCEINLINE byte GetAirDragArea() const
00182   {
00183     return 6;
00184   }
00185 
00190   FORCEINLINE byte GetAirDrag() const
00191   {
00192     return RoadVehInfo(this->engine_type)->air_drag;
00193   }
00194 
00199   FORCEINLINE AccelStatus GetAccelerationStatus() const
00200   {
00201     return (this->vehstatus & VS_STOPPED) ? AS_BRAKE : AS_ACCEL;
00202   }
00203 
00208   FORCEINLINE uint16 GetCurrentSpeed() const
00209   {
00210     return this->cur_speed / 2;
00211   }
00212 
00217   FORCEINLINE uint32 GetRollingFriction() const
00218   {
00219     /* Trams have a slightly greater friction coefficient than trains.
00220      * The rest of road vehicles have bigger values. */
00221     uint32 coeff = (this->roadtype == ROADTYPE_TRAM) ? 40 : 75;
00222     /* The friction coefficient increases with speed in a way that
00223      * it doubles at 128 km/h, triples at 256 km/h and so on. */
00224     return coeff * (128 + this->GetCurrentSpeed()) / 128;
00225   }
00226 
00231   FORCEINLINE int GetAccelerationType() const
00232   {
00233     return 0;
00234   }
00235 
00240   FORCEINLINE uint32 GetSlopeSteepness() const
00241   {
00242     return _settings_game.vehicle.roadveh_slope_steepness;
00243   }
00244 
00249   FORCEINLINE uint16 GetMaxTrackSpeed() const
00250   {
00251     return 0;
00252   }
00253 
00258   FORCEINLINE bool TileMayHaveSlopedTrack() const
00259   {
00260     TrackStatus ts = GetTileTrackStatus(this->tile, TRANSPORT_ROAD, this->compatible_roadtypes);
00261     TrackBits trackbits = TrackStatusToTrackBits(ts);
00262 
00263     return trackbits == TRACK_BIT_X || trackbits == TRACK_BIT_Y;
00264   }
00265 };
00266 
00267 #define FOR_ALL_ROADVEHICLES(var) FOR_ALL_VEHICLES_OF_TYPE(RoadVehicle, var)
00268 
00269 #endif /* ROADVEH_H */

Generated on Thu Jan 20 22:57:39 2011 for OpenTTD by  doxygen 1.6.1