# train.h

Go to the documentation of this file.

    00001 /* $Id: train.h 13827 2008-07-25 19:54:14Z rubidium $ */
    00002
    00005 #ifndef TRAIN_H
    00006 #define TRAIN_H
    00007
    00008 #include "stdafx.h"
    00009 #include "core/bitmath_func.hpp"
    00010 #include "vehicle_base.h"
    00011
    00012
    00013 /*
    00014  * enum to handle train subtypes
    00015  * Do not access it directly unless you have to. Use the access functions below
    00016  * This is an enum to tell what bit to access as it is a bitmask
    00017  */
    00018
    00019 enum TrainSubtype {
    00020   TS_FRONT             = 0,
    00021   TS_ARTICULATED_PART  = 1,
    00022   TS_WAGON             = 2,
    00023   TS_ENGINE            = 3,
    00024   TS_FREE_WAGON        = 4,
    00025   TS_MULTIHEADED       = 5,
    00026 };
    00027
    00028
    00033 static inline bool IsFrontEngine(const Vehicle *v)
    00034 {
    00035   assert(v->type == VEH_TRAIN);
    00036   return HasBit(v->subtype, TS_FRONT);
    00037 }
    00038
    00042 static inline void SetFrontEngine(Vehicle *v)
    00043 {
    00044   assert(v->type == VEH_TRAIN);
    00045   SetBit(v->subtype, TS_FRONT);
    00046 }
    00047
    00051 static inline void ClearFrontEngine(Vehicle *v)
    00052 {
    00053   assert(v->type == VEH_TRAIN);
    00054   ClrBit(v->subtype, TS_FRONT);
    00055 }
    00056
    00061 static inline bool IsArticulatedPart(const Vehicle *v)
    00062 {
    00063   assert(v->type == VEH_TRAIN);
    00064   return HasBit(v->subtype, TS_ARTICULATED_PART);
    00065 }
    00066
    00070 static inline void SetArticulatedPart(Vehicle *v)
    00071 {
    00072   assert(v->type == VEH_TRAIN);
    00073   SetBit(v->subtype, TS_ARTICULATED_PART);
    00074 }
    00075
    00079 static inline void ClearArticulatedPart(Vehicle *v)
    00080 {
    00081   assert(v->type == VEH_TRAIN);
    00082   ClrBit(v->subtype, TS_ARTICULATED_PART);
    00083 }
    00084
    00089 static inline bool IsTrainWagon(const Vehicle *v)
    00090 {
    00091   assert(v->type == VEH_TRAIN);
    00092   return HasBit(v->subtype, TS_WAGON);
    00093 }
    00094
    00098 static inline void SetTrainWagon(Vehicle *v)
    00099 {
    00100   assert(v->type == VEH_TRAIN);
    00101   SetBit(v->subtype, TS_WAGON);
    00102 }
    00103
    00107 static inline void ClearTrainWagon(Vehicle *v)
    00108 {
    00109   assert(v->type == VEH_TRAIN);
    00110   ClrBit(v->subtype, TS_WAGON);
    00111 }
    00112
    00117 static inline bool IsTrainEngine(const Vehicle *v)
    00118 {
    00119   assert(v->type == VEH_TRAIN);
    00120   return HasBit(v->subtype, TS_ENGINE);
    00121 }
    00122
    00126 static inline void SetTrainEngine(Vehicle *v)
    00127 {
    00128   assert(v->type == VEH_TRAIN);
    00129   SetBit(v->subtype, TS_ENGINE);
    00130 }
    00131
    00135 static inline void ClearTrainEngine(Vehicle *v)
    00136 {
    00137   assert(v->type == VEH_TRAIN);
    00138   ClrBit(v->subtype, TS_ENGINE);
    00139 }
    00140
    00145 static inline bool IsFreeWagon(const Vehicle *v)
    00146 {
    00147   assert(v->type == VEH_TRAIN);
    00148   return HasBit(v->subtype, TS_FREE_WAGON);
    00149 }
    00150
    00154 static inline void SetFreeWagon(Vehicle *v)
    00155 {
    00156   assert(v->type == VEH_TRAIN);
    00157   SetBit(v->subtype, TS_FREE_WAGON);
    00158 }
    00159
    00163 static inline void ClearFreeWagon(Vehicle *v)
    00164 {
    00165   assert(v->type == VEH_TRAIN);
    00166   ClrBit(v->subtype, TS_FREE_WAGON);
    00167 }
    00168
    00173 static inline bool IsMultiheaded(const Vehicle *v)
    00174 {
    00175   assert(v->type == VEH_TRAIN);
    00176   return HasBit(v->subtype, TS_MULTIHEADED);
    00177 }
    00178
    00182 static inline void SetMultiheaded(Vehicle *v)
    00183 {
    00184   assert(v->type == VEH_TRAIN);
    00185   SetBit(v->subtype, TS_MULTIHEADED);
    00186 }
    00187
    00191 static inline void ClearMultiheaded(Vehicle *v)
    00192 {
    00193   assert(v->type == VEH_TRAIN);
    00194   ClrBit(v->subtype, TS_MULTIHEADED);
    00195 }
    00196
    00201 static inline bool EngineHasArticPart(const Vehicle *v)
    00202 {
    00203   assert(v->type == VEH_TRAIN);
    00204   return (v->Next() != NULL && IsArticulatedPart(v->Next()));
    00205 }
    00206
    00212 static inline Vehicle *GetNextArticPart(const Vehicle *v)
    00213 {
    00214   assert(EngineHasArticPart(v));
    00215   return v->Next();
    00216 }
    00217
    00222 static inline Vehicle *GetLastEnginePart(Vehicle *v)
    00223 {
    00224   assert(v->type == VEH_TRAIN);
    00225   while (EngineHasArticPart(v)) v = GetNextArticPart(v);
    00226   return v;
    00227 }
    00228
    00233 static inline bool IsRearDualheaded(const Vehicle *v)
    00234 {
    00235   assert(v->type == VEH_TRAIN);
    00236   return (IsMultiheaded(v) && !IsTrainEngine(v));
    00237 }
    00238
    00243 static inline Vehicle *GetNextVehicle(const Vehicle *v)
    00244 {
    00245   assert(v->type == VEH_TRAIN);
    00246   while (EngineHasArticPart(v)) v = GetNextArticPart(v);
    00247
    00248   /* v now contains the last artic part in the engine */
    00249   return v->Next();
    00250 }
    00251
    00256 static inline Vehicle *GetNextUnit(Vehicle *v)
    00257 {
    00258   assert(v->type == VEH_TRAIN);
    00259   v = GetNextVehicle(v);
    00260   if (v != NULL && IsRearDualheaded(v)) v = v->Next();
    00261
    00262   return v;
    00263 }
    00264
    00265 void ConvertOldMultiheadToNew();
    00266 void ConnectMultiheadedTrains();
    00267 uint CountArticulatedParts(EngineID engine_type);
    00268
    00269 int CheckTrainInDepot(const Vehicle *v, bool needs_to_be_stopped);
    00270 void CcBuildLoco(bool success, TileIndex tile, uint32 p1, uint32 p2);
    00271 void CcBuildWagon(bool success, TileIndex tile, uint32 p1, uint32 p2);
    00272
    00273 byte FreightWagonMult(CargoID cargo);
    00274
    00275 int CheckTrainInDepot(const Vehicle *v, bool needs_to_be_stopped);
    00276 int CheckTrainStoppedInDepot(const Vehicle *v);
    00277 void UpdateTrainAcceleration(Vehicle* v);
    00278 void CheckTrainsLengths();
    00279
    00288 struct Train : public Vehicle {
    00290   Train() { this->type = VEH_TRAIN; }
    00291
    00293   virtual ~Train() { this->PreDestructor(); }
    00294
    00295   const char *GetTypeString() const { return "train"; }
    00296   void MarkDirty();
    00297   void UpdateDeltaXY(Direction direction);
    00298   ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_TRAIN_INC : EXPENSES_TRAIN_RUN; }
    00299   WindowClass GetVehicleListWindowClass() const { return WC_TRAINS_LIST; }
    00300   void PlayLeaveStationSound() const;
    00301   bool IsPrimaryVehicle() const { return IsFrontEngine(this); }
    00302   int GetImage(Direction direction) const;
    00303   int GetDisplaySpeed() const { return this->u.rail.last_speed * 10 / 16; }
    00304   int GetDisplayMaxSpeed() const { return this->u.rail.cached_max_speed * 10 / 16; }
    00305   Money GetRunningCost() const;
    00306   bool IsInDepot() const { return CheckTrainInDepot(this, false) != -1; }
    00307   bool IsStoppedInDepot() const { return CheckTrainStoppedInDepot(this) >= 0; }
    00308   void Tick();
    00309   void OnNewDay();
    00310 };
    00311
    00312 #endif /* TRAIN_H */