train.h

Go to the documentation of this file.
00001 /* $Id: train.h 23640 2011-12-20 17:57:56Z truebrain $ */
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 TRAIN_H
00013 #define TRAIN_H
00014 
00015 #include "newgrf_engine.h"
00016 #include "cargotype.h"
00017 #include "rail.h"
00018 #include "engine_base.h"
00019 #include "rail_map.h"
00020 #include "ground_vehicle.hpp"
00021 
00022 struct Train;
00023 
00025 enum VehicleRailFlags {
00026   VRF_REVERSING                     = 0,
00027   VRF_POWEREDWAGON                  = 3, 
00028   VRF_REVERSE_DIRECTION             = 4, 
00029 
00030   VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL = 6, 
00031   VRF_TOGGLE_REVERSE                = 7, 
00032   VRF_TRAIN_STUCK                   = 8, 
00033   VRF_LEAVING_STATION               = 9, 
00034 };
00035 
00037 enum TrainForceProceeding {
00038   TFP_NONE   = 0,    
00039   TFP_STUCK  = 1,    
00040   TFP_SIGNAL = 2,    
00041 };
00042 typedef SimpleTinyEnumT<TrainForceProceeding, byte> TrainForceProceedingByte;
00043 
00044 byte FreightWagonMult(CargoID cargo);
00045 
00046 void CheckTrainsLengths();
00047 
00048 void FreeTrainTrackReservation(const Train *v, TileIndex origin = INVALID_TILE, Trackdir orig_td = INVALID_TRACKDIR);
00049 bool TryPathReserve(Train *v, bool mark_as_stuck = false, bool first_tile_okay = false);
00050 
00051 int GetTrainStopLocation(StationID station_id, TileIndex tile, const Train *v, int *station_ahead, int *station_length);
00052 
00054 struct TrainCache {
00055   /* Cached wagon override spritegroup */
00056   const struct SpriteGroup *cached_override;
00057 
00058   /* cached values, recalculated on load and each time a vehicle is added to/removed from the consist. */
00059   bool cached_tilt;           
00060 
00061   byte user_def_data;         
00062 
00063   /* cached max. speed / acceleration data */
00064   int cached_max_curve_speed; 
00065 };
00066 
00070 struct Train FINAL : public GroundVehicle<Train, VEH_TRAIN> {
00071   TrainCache tcache;
00072 
00073   /* Link between the two ends of a multiheaded engine */
00074   Train *other_multiheaded_part;
00075 
00076   uint16 crash_anim_pos; 
00077 
00078   uint16 flags;
00079   TrackBitsByte track;
00080   TrainForceProceedingByte force_proceed;
00081   RailTypeByte railtype;
00082   RailTypes compatible_railtypes;
00083 
00085   uint16 wait_counter;
00086 
00088   Train() : GroundVehicleBase() {}
00090   virtual ~Train() { this->PreDestructor(); }
00091 
00092   friend struct GroundVehicle<Train, VEH_TRAIN>; // GroundVehicle needs to use the acceleration functions defined at Train.
00093 
00094   void MarkDirty();
00095   void UpdateDeltaXY(Direction direction);
00096   ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_TRAIN_INC : EXPENSES_TRAIN_RUN; }
00097   void PlayLeaveStationSound() const;
00098   bool IsPrimaryVehicle() const { return this->IsFrontEngine(); }
00099   SpriteID GetImage(Direction direction, EngineImageType image_type) const;
00100   int GetDisplaySpeed() const { return this->gcache.last_speed; }
00101   int GetDisplayMaxSpeed() const { return this->vcache.cached_max_speed; }
00102   Money GetRunningCost() const;
00103   int GetDisplayImageWidth(Point *offset = NULL) const;
00104   bool IsInDepot() const;
00105   bool IsStoppedInDepot() const;
00106   bool Tick();
00107   void OnNewDay();
00108   uint Crash(bool flooded = false);
00109   Trackdir GetVehicleTrackdir() const;
00110   TileIndex GetOrderStationLocation(StationID station);
00111   bool FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse);
00112 
00113   void ReserveTrackUnderConsist() const;
00114 
00115   int GetCurveSpeedLimit() const;
00116 
00117   void ConsistChanged(bool same_length);
00118 
00119   void RailtypeChanged();
00120 
00121   int UpdateSpeed();
00122 
00123   void UpdateAcceleration();
00124 
00125   int GetCurrentMaxSpeed() const;
00126 
00131   inline Train *GetNextUnit() const
00132   {
00133     Train *v = this->GetNextVehicle();
00134     if (v != NULL && v->IsRearDualheaded()) v = v->GetNextVehicle();
00135 
00136     return v;
00137   }
00138 
00143   inline Train *GetPrevUnit()
00144   {
00145     Train *v = this->GetPrevVehicle();
00146     if (v != NULL && v->IsRearDualheaded()) v = v->GetPrevVehicle();
00147 
00148     return v;
00149   }
00150 
00155   int CalcNextVehicleOffset() const
00156   {
00157     /* For vehicles with odd lengths the part before the center will be one unit
00158      * longer than the part after the center. This means we have to round up the
00159      * length of the next vehicle but may not round the length of the current
00160      * vehicle. */
00161     return this->gcache.cached_veh_length / 2 + (this->Next() != NULL ? this->Next()->gcache.cached_veh_length + 1 : 0) / 2;
00162   }
00163 
00164 protected: // These functions should not be called outside acceleration code.
00165 
00170   inline uint16 GetPower() const
00171   {
00172     /* Power is not added for articulated parts */
00173     if (!this->IsArticulatedPart() && HasPowerOnRail(this->railtype, GetRailType(this->tile))) {
00174       uint16 power = GetVehicleProperty(this, PROP_TRAIN_POWER, RailVehInfo(this->engine_type)->power);
00175       /* Halve power for multiheaded parts */
00176       if (this->IsMultiheaded()) power /= 2;
00177       return power;
00178     }
00179 
00180     return 0;
00181   }
00182 
00187   inline uint16 GetPoweredPartPower(const Train *head) const
00188   {
00189     /* For powered wagons the engine defines the type of engine (i.e. railtype) */
00190     if (HasBit(this->flags, VRF_POWEREDWAGON) && HasPowerOnRail(head->railtype, GetRailType(this->tile))) {
00191       return RailVehInfo(this->gcache.first_engine)->pow_wag_power;
00192     }
00193 
00194     return 0;
00195   }
00196 
00201   inline uint16 GetWeight() const
00202   {
00203     uint16 weight = (CargoSpec::Get(this->cargo_type)->weight * this->cargo.Count() * FreightWagonMult(this->cargo_type)) / 16;
00204 
00205     /* Vehicle weight is not added for articulated parts. */
00206     if (!this->IsArticulatedPart()) {
00207       weight += GetVehicleProperty(this, PROP_TRAIN_WEIGHT, RailVehInfo(this->engine_type)->weight);
00208     }
00209 
00210     /* Powered wagons have extra weight added. */
00211     if (HasBit(this->flags, VRF_POWEREDWAGON)) {
00212       weight += RailVehInfo(this->gcache.first_engine)->pow_wag_weight;
00213     }
00214 
00215     return weight;
00216   }
00217 
00222   inline byte GetTractiveEffort() const
00223   {
00224     return GetVehicleProperty(this, PROP_TRAIN_TRACTIVE_EFFORT, RailVehInfo(this->engine_type)->tractive_effort);
00225   }
00226 
00231   inline byte GetAirDragArea() const
00232   {
00233     /* Air drag is higher in tunnels due to the limited cross-section. */
00234     return (this->track == TRACK_BIT_WORMHOLE && this->vehstatus & VS_HIDDEN) ? 28 : 14;
00235   }
00236 
00241   inline byte GetAirDrag() const
00242   {
00243     return RailVehInfo(this->engine_type)->air_drag;
00244   }
00245 
00250   inline AccelStatus GetAccelerationStatus() const
00251   {
00252     return (this->vehstatus & VS_STOPPED) || HasBit(this->flags, VRF_REVERSING) || HasBit(this->flags, VRF_TRAIN_STUCK) ? AS_BRAKE : AS_ACCEL;
00253   }
00254 
00259   inline uint16 GetCurrentSpeed() const
00260   {
00261     return this->cur_speed;
00262   }
00263 
00268   inline uint32 GetRollingFriction() const
00269   {
00270     /* Rolling friction for steel on steel is between 0.1% and 0.2%.
00271      * The friction coefficient increases with speed in a way that
00272      * it doubles at 512 km/h, triples at 1024 km/h and so on. */
00273     return 15 * (512 + this->GetCurrentSpeed()) / 512;
00274   }
00275 
00280   inline int GetAccelerationType() const
00281   {
00282     return GetRailTypeInfo(this->railtype)->acceleration_type;
00283   }
00284 
00289   inline uint32 GetSlopeSteepness() const
00290   {
00291     return _settings_game.vehicle.train_slope_steepness;
00292   }
00293 
00298   inline uint16 GetMaxTrackSpeed() const
00299   {
00300     return GetRailTypeInfo(GetRailType(this->tile))->max_speed;
00301   }
00302 
00307   inline bool TileMayHaveSlopedTrack() const
00308   {
00309     /* Any track that isn't TRACK_BIT_X or TRACK_BIT_Y cannot be sloped. */
00310     return this->track == TRACK_BIT_X || this->track == TRACK_BIT_Y;
00311   }
00312 
00318   inline bool HasToUseGetSlopePixelZ()
00319   {
00320     return false;
00321   }
00322 };
00323 
00324 #define FOR_ALL_TRAINS(var) FOR_ALL_VEHICLES_OF_TYPE(Train, var)
00325 
00326 #endif /* TRAIN_H */