# train.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 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   int UpdateSpeed();
    00120
    00121   void UpdateAcceleration();
    00122
    00123   int GetCurrentMaxSpeed() const;
    00124
    00129   inline Train *GetNextUnit() const
    00130   {
    00131     Train *v = this->GetNextVehicle();
    00132     if (v != NULL && v->IsRearDualheaded()) v = v->GetNextVehicle();
    00133
    00134     return v;
    00135   }
    00136
    00141   inline Train *GetPrevUnit()
    00142   {
    00143     Train *v = this->GetPrevVehicle();
    00144     if (v != NULL && v->IsRearDualheaded()) v = v->GetPrevVehicle();
    00145
    00146     return v;
    00147   }
    00148
    00153   int CalcNextVehicleOffset() const
    00154   {
    00155     /* For vehicles with odd lengths the part before the center will be one unit
    00156      * longer than the part after the center. This means we have to round up the
    00157      * length of the next vehicle but may not round the length of the current
    00158      * vehicle. */
    00159     return this->gcache.cached_veh_length / 2 + (this->Next() != NULL ? this->Next()->gcache.cached_veh_length + 1 : 0) / 2;
    00160   }
    00161
    00162 protected: // These functions should not be called outside acceleration code.
    00163
    00168   inline uint16 GetPower() const
    00169   {
    00170     /* Power is not added for articulated parts */
    00171     if (!this->IsArticulatedPart() && HasPowerOnRail(this->railtype, GetRailType(this->tile))) {
    00172       uint16 power = GetVehicleProperty(this, PROP_TRAIN_POWER, RailVehInfo(this->engine_type)->power);
    00173       /* Halve power for multiheaded parts */
    00174       if (this->IsMultiheaded()) power /= 2;
    00175       return power;
    00176     }
    00177
    00178     return 0;
    00179   }
    00180
    00185   inline uint16 GetPoweredPartPower(const Train *head) const
    00186   {
    00187     /* For powered wagons the engine defines the type of engine (i.e. railtype) */
    00188     if (HasBit(this->flags, VRF_POWEREDWAGON) && HasPowerOnRail(head->railtype, GetRailType(this->tile))) {
    00189       return RailVehInfo(this->gcache.first_engine)->pow_wag_power;
    00190     }
    00191
    00192     return 0;
    00193   }
    00194
    00199   inline uint16 GetWeight() const
    00200   {
    00201     uint16 weight = (CargoSpec::Get(this->cargo_type)->weight * this->cargo.Count() * FreightWagonMult(this->cargo_type)) / 16;
    00202
    00203     /* Vehicle weight is not added for articulated parts. */
    00204     if (!this->IsArticulatedPart()) {
    00205       weight += GetVehicleProperty(this, PROP_TRAIN_WEIGHT, RailVehInfo(this->engine_type)->weight);
    00206     }
    00207
    00208     /* Powered wagons have extra weight added. */
    00209     if (HasBit(this->flags, VRF_POWEREDWAGON)) {
    00210       weight += RailVehInfo(this->gcache.first_engine)->pow_wag_weight;
    00211     }
    00212
    00213     return weight;
    00214   }
    00215
    00220   inline byte GetTractiveEffort() const
    00221   {
    00222     return GetVehicleProperty(this, PROP_TRAIN_TRACTIVE_EFFORT, RailVehInfo(this->engine_type)->tractive_effort);
    00223   }
    00224
    00229   inline byte GetAirDragArea() const
    00230   {
    00231     /* Air drag is higher in tunnels due to the limited cross-section. */
    00232     return (this->track == TRACK_BIT_WORMHOLE && this->vehstatus & VS_HIDDEN) ? 28 : 14;
    00233   }
    00234
    00239   inline byte GetAirDrag() const
    00240   {
    00241     return RailVehInfo(this->engine_type)->air_drag;
    00242   }
    00243
    00248   inline AccelStatus GetAccelerationStatus() const
    00249   {
    00250     return (this->vehstatus & VS_STOPPED) || HasBit(this->flags, VRF_REVERSING) || HasBit(this->flags, VRF_TRAIN_STUCK) ? AS_BRAKE : AS_ACCEL;
    00251   }
    00252
    00257   inline uint16 GetCurrentSpeed() const
    00258   {
    00259     return this->cur_speed;
    00260   }
    00261
    00266   inline uint32 GetRollingFriction() const
    00267   {
    00268     /* Rolling friction for steel on steel is between 0.1% and 0.2%.
    00269      * The friction coefficient increases with speed in a way that
    00270      * it doubles at 512 km/h, triples at 1024 km/h and so on. */
    00271     return 15 * (512 + this->GetCurrentSpeed()) / 512;
    00272   }
    00273
    00278   inline int GetAccelerationType() const
    00279   {
    00280     return GetRailTypeInfo(this->railtype)->acceleration_type;
    00281   }
    00282
    00287   inline uint32 GetSlopeSteepness() const
    00288   {
    00289     return _settings_game.vehicle.train_slope_steepness;
    00290   }
    00291
    00296   inline uint16 GetMaxTrackSpeed() const
    00297   {
    00298     return GetRailTypeInfo(GetRailType(this->tile))->max_speed;
    00299   }
    00300
    00305   inline bool TileMayHaveSlopedTrack() const
    00306   {
    00307     /* Any track that isn't TRACK_BIT_X or TRACK_BIT_Y cannot be sloped. */
    00308     return this->track == TRACK_BIT_X || this->track == TRACK_BIT_Y;
    00309   }
    00310
    00316   inline bool HasToUseGetSlopePixelZ()
    00317   {
    00318     return false;
    00319   }
    00320 };
    00321
    00322 #define FOR_ALL_TRAINS(var) FOR_ALL_VEHICLES_OF_TYPE(Train, var)
    00323
    00324 #endif /* TRAIN_H */