Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
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
00056 const struct SpriteGroup *cached_override;
00057
00058
00059 bool cached_tilt;
00060
00061 byte user_def_data;
00062
00063
00064 int cached_max_curve_speed;
00065 };
00066
00070 struct Train FINAL : public GroundVehicle<Train, VEH_TRAIN> {
00071 TrainCache tcache;
00072
00073
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>;
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 { return this->track == TRACK_BIT_DEPOT; }
00105 bool Tick();
00106 void OnNewDay();
00107 uint Crash(bool flooded = false);
00108 Trackdir GetVehicleTrackdir() const;
00109 TileIndex GetOrderStationLocation(StationID station);
00110 bool FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse);
00111
00112 void ReserveTrackUnderConsist() const;
00113
00114 int GetCurveSpeedLimit() const;
00115
00116 void ConsistChanged(bool same_length);
00117
00118 int UpdateSpeed();
00119
00120 void UpdateAcceleration();
00121
00122 int GetCurrentMaxSpeed() const;
00123
00128 inline Train *GetNextUnit() const
00129 {
00130 Train *v = this->GetNextVehicle();
00131 if (v != NULL && v->IsRearDualheaded()) v = v->GetNextVehicle();
00132
00133 return v;
00134 }
00135
00140 inline Train *GetPrevUnit()
00141 {
00142 Train *v = this->GetPrevVehicle();
00143 if (v != NULL && v->IsRearDualheaded()) v = v->GetPrevVehicle();
00144
00145 return v;
00146 }
00147
00152 int CalcNextVehicleOffset() const
00153 {
00154
00155
00156
00157
00158 return this->gcache.cached_veh_length / 2 + (this->Next() != NULL ? this->Next()->gcache.cached_veh_length + 1 : 0) / 2;
00159 }
00160
00161 protected:
00162
00167 inline uint16 GetPower() const
00168 {
00169
00170 if (!this->IsArticulatedPart() && HasPowerOnRail(this->railtype, GetRailType(this->tile))) {
00171 uint16 power = GetVehicleProperty(this, PROP_TRAIN_POWER, RailVehInfo(this->engine_type)->power);
00172
00173 if (this->IsMultiheaded()) power /= 2;
00174 return power;
00175 }
00176
00177 return 0;
00178 }
00179
00184 inline uint16 GetPoweredPartPower(const Train *head) const
00185 {
00186
00187 if (HasBit(this->flags, VRF_POWEREDWAGON) && HasPowerOnRail(head->railtype, GetRailType(this->tile))) {
00188 return RailVehInfo(this->gcache.first_engine)->pow_wag_power;
00189 }
00190
00191 return 0;
00192 }
00193
00198 inline uint16 GetWeight() const
00199 {
00200 uint16 weight = (CargoSpec::Get(this->cargo_type)->weight * this->cargo.Count() * FreightWagonMult(this->cargo_type)) / 16;
00201
00202
00203 if (!this->IsArticulatedPart()) {
00204 weight += GetVehicleProperty(this, PROP_TRAIN_WEIGHT, RailVehInfo(this->engine_type)->weight);
00205 }
00206
00207
00208 if (HasBit(this->flags, VRF_POWEREDWAGON)) {
00209 weight += RailVehInfo(this->gcache.first_engine)->pow_wag_weight;
00210 }
00211
00212 return weight;
00213 }
00214
00219 inline byte GetTractiveEffort() const
00220 {
00221 return GetVehicleProperty(this, PROP_TRAIN_TRACTIVE_EFFORT, RailVehInfo(this->engine_type)->tractive_effort);
00222 }
00223
00228 inline byte GetAirDragArea() const
00229 {
00230
00231 return (this->track == TRACK_BIT_WORMHOLE && this->vehstatus & VS_HIDDEN) ? 28 : 14;
00232 }
00233
00238 inline byte GetAirDrag() const
00239 {
00240 return RailVehInfo(this->engine_type)->air_drag;
00241 }
00242
00247 inline AccelStatus GetAccelerationStatus() const
00248 {
00249 return (this->vehstatus & VS_STOPPED) || HasBit(this->flags, VRF_REVERSING) || HasBit(this->flags, VRF_TRAIN_STUCK) ? AS_BRAKE : AS_ACCEL;
00250 }
00251
00256 inline uint16 GetCurrentSpeed() const
00257 {
00258 return this->cur_speed;
00259 }
00260
00265 inline uint32 GetRollingFriction() const
00266 {
00267
00268
00269
00270 return 15 * (512 + this->GetCurrentSpeed()) / 512;
00271 }
00272
00277 inline int GetAccelerationType() const
00278 {
00279 return GetRailTypeInfo(this->railtype)->acceleration_type;
00280 }
00281
00286 inline uint32 GetSlopeSteepness() const
00287 {
00288 return _settings_game.vehicle.train_slope_steepness;
00289 }
00290
00295 inline uint16 GetMaxTrackSpeed() const
00296 {
00297 return GetRailTypeInfo(GetRailType(this->tile))->max_speed;
00298 }
00299
00304 inline bool TileMayHaveSlopedTrack() const
00305 {
00306
00307 return this->track == TRACK_BIT_X || this->track == TRACK_BIT_Y;
00308 }
00309
00315 inline bool HasToUseGetSlopePixelZ()
00316 {
00317 return false;
00318 }
00319 };
00320
00321 #define FOR_ALL_TRAINS(var) FOR_ALL_VEHICLES_OF_TYPE(Train, var)
00322
00323 #endif