train.h

Go to the documentation of this file.
00001 /* $Id: train.h 15308 2009-02-01 17:14:39Z frosch $ */
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 
00017 enum TrainSubtype {
00018   TS_FRONT             = 0, 
00019   TS_ARTICULATED_PART  = 1, 
00020   TS_WAGON             = 2, 
00021   TS_ENGINE            = 3, 
00022   TS_FREE_WAGON        = 4, 
00023   TS_MULTIHEADED       = 5, 
00024 };
00025 
00026 
00031 static inline bool IsFrontEngine(const Vehicle *v)
00032 {
00033   assert(v->type == VEH_TRAIN);
00034   return HasBit(v->subtype, TS_FRONT);
00035 }
00036 
00040 static inline void SetFrontEngine(Vehicle *v)
00041 {
00042   assert(v->type == VEH_TRAIN);
00043   SetBit(v->subtype, TS_FRONT);
00044 }
00045 
00049 static inline void ClearFrontEngine(Vehicle *v)
00050 {
00051   assert(v->type == VEH_TRAIN);
00052   ClrBit(v->subtype, TS_FRONT);
00053 }
00054 
00059 static inline bool IsArticulatedPart(const Vehicle *v)
00060 {
00061   assert(v->type == VEH_TRAIN);
00062   return HasBit(v->subtype, TS_ARTICULATED_PART);
00063 }
00064 
00068 static inline void SetArticulatedPart(Vehicle *v)
00069 {
00070   assert(v->type == VEH_TRAIN);
00071   SetBit(v->subtype, TS_ARTICULATED_PART);
00072 }
00073 
00077 static inline void ClearArticulatedPart(Vehicle *v)
00078 {
00079   assert(v->type == VEH_TRAIN);
00080   ClrBit(v->subtype, TS_ARTICULATED_PART);
00081 }
00082 
00087 static inline bool IsTrainWagon(const Vehicle *v)
00088 {
00089   assert(v->type == VEH_TRAIN);
00090   return HasBit(v->subtype, TS_WAGON);
00091 }
00092 
00096 static inline void SetTrainWagon(Vehicle *v)
00097 {
00098   assert(v->type == VEH_TRAIN);
00099   SetBit(v->subtype, TS_WAGON);
00100 }
00101 
00105 static inline void ClearTrainWagon(Vehicle *v)
00106 {
00107   assert(v->type == VEH_TRAIN);
00108   ClrBit(v->subtype, TS_WAGON);
00109 }
00110 
00115 static inline bool IsTrainEngine(const Vehicle *v)
00116 {
00117   assert(v->type == VEH_TRAIN);
00118   return HasBit(v->subtype, TS_ENGINE);
00119 }
00120 
00124 static inline void SetTrainEngine(Vehicle *v)
00125 {
00126   assert(v->type == VEH_TRAIN);
00127   SetBit(v->subtype, TS_ENGINE);
00128 }
00129 
00133 static inline void ClearTrainEngine(Vehicle *v)
00134 {
00135   assert(v->type == VEH_TRAIN);
00136   ClrBit(v->subtype, TS_ENGINE);
00137 }
00138 
00143 static inline bool IsFreeWagon(const Vehicle *v)
00144 {
00145   assert(v->type == VEH_TRAIN);
00146   return HasBit(v->subtype, TS_FREE_WAGON);
00147 }
00148 
00152 static inline void SetFreeWagon(Vehicle *v)
00153 {
00154   assert(v->type == VEH_TRAIN);
00155   SetBit(v->subtype, TS_FREE_WAGON);
00156 }
00157 
00161 static inline void ClearFreeWagon(Vehicle *v)
00162 {
00163   assert(v->type == VEH_TRAIN);
00164   ClrBit(v->subtype, TS_FREE_WAGON);
00165 }
00166 
00171 static inline bool IsMultiheaded(const Vehicle *v)
00172 {
00173   assert(v->type == VEH_TRAIN);
00174   return HasBit(v->subtype, TS_MULTIHEADED);
00175 }
00176 
00180 static inline void SetMultiheaded(Vehicle *v)
00181 {
00182   assert(v->type == VEH_TRAIN);
00183   SetBit(v->subtype, TS_MULTIHEADED);
00184 }
00185 
00189 static inline void ClearMultiheaded(Vehicle *v)
00190 {
00191   assert(v->type == VEH_TRAIN);
00192   ClrBit(v->subtype, TS_MULTIHEADED);
00193 }
00194 
00199 static inline bool EngineHasArticPart(const Vehicle *v)
00200 {
00201   assert(v->type == VEH_TRAIN);
00202   return (v->Next() != NULL && IsArticulatedPart(v->Next()));
00203 }
00204 
00210 static inline Vehicle *GetNextArticPart(const Vehicle *v)
00211 {
00212   assert(EngineHasArticPart(v));
00213   return v->Next();
00214 }
00215 
00220 static inline Vehicle *GetLastEnginePart(Vehicle *v)
00221 {
00222   assert(v->type == VEH_TRAIN);
00223   while (EngineHasArticPart(v)) v = GetNextArticPart(v);
00224   return v;
00225 }
00226 
00231 static inline bool IsRearDualheaded(const Vehicle *v)
00232 {
00233   assert(v->type == VEH_TRAIN);
00234   return (IsMultiheaded(v) && !IsTrainEngine(v));
00235 }
00236 
00241 static inline Vehicle *GetNextVehicle(const Vehicle *v)
00242 {
00243   assert(v->type == VEH_TRAIN);
00244   while (EngineHasArticPart(v)) v = GetNextArticPart(v);
00245 
00246   /* v now contains the last artic part in the engine */
00247   return v->Next();
00248 }
00249 
00254 static inline Vehicle *GetPrevVehicle(const Vehicle *w)
00255 {
00256   assert(w->type == VEH_TRAIN);
00257 
00258   Vehicle *v = w->Previous();
00259   while (v != NULL && IsArticulatedPart(v)) v = v->Previous();
00260 
00261   return v;
00262 }
00263 
00268 static inline Vehicle *GetNextUnit(const Vehicle *v)
00269 {
00270   assert(v->type == VEH_TRAIN);
00271   Vehicle *w = GetNextVehicle(v);
00272   if (w != NULL && IsRearDualheaded(w)) w = GetNextVehicle(w);
00273 
00274   return w;
00275 }
00276 
00281 static inline Vehicle *GetPrevUnit(const Vehicle *v)
00282 {
00283   assert(v->type == VEH_TRAIN);
00284   Vehicle *w = GetPrevVehicle(v);
00285   if (w != NULL && IsRearDualheaded(w)) w = GetPrevVehicle(w);
00286 
00287   return w;
00288 }
00289 
00290 void CcBuildLoco(bool success, TileIndex tile, uint32 p1, uint32 p2);
00291 void CcBuildWagon(bool success, TileIndex tile, uint32 p1, uint32 p2);
00292 
00293 byte FreightWagonMult(CargoID cargo);
00294 
00295 int CheckTrainInDepot(const Vehicle *v, bool needs_to_be_stopped);
00296 int CheckTrainStoppedInDepot(const Vehicle *v);
00297 void UpdateTrainAcceleration(Vehicle *v);
00298 void CheckTrainsLengths();
00299 
00300 void FreeTrainTrackReservation(const Vehicle *v, TileIndex origin = INVALID_TILE, Trackdir orig_td = INVALID_TRACKDIR);
00301 bool TryPathReserve(Vehicle *v, bool mark_as_stuck = false, bool first_tile_okay = false);
00302 
00311 struct Train : public Vehicle {
00313   Train() { this->type = VEH_TRAIN; }
00314 
00316   virtual ~Train() { this->PreDestructor(); }
00317 
00318   const char *GetTypeString() const { return "train"; }
00319   void MarkDirty();
00320   void UpdateDeltaXY(Direction direction);
00321   ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_TRAIN_INC : EXPENSES_TRAIN_RUN; }
00322   void PlayLeaveStationSound() const;
00323   bool IsPrimaryVehicle() const { return IsFrontEngine(this); }
00324   SpriteID GetImage(Direction direction) const;
00325   int GetDisplaySpeed() const { return this->u.rail.last_speed; }
00326   int GetDisplayMaxSpeed() const { return this->u.rail.cached_max_speed; }
00327   Money GetRunningCost() const;
00328   bool IsInDepot() const { return CheckTrainInDepot(this, false) != -1; }
00329   bool IsStoppedInDepot() const { return CheckTrainStoppedInDepot(this) >= 0; }
00330   void Tick();
00331   void OnNewDay();
00332   TileIndex GetOrderStationLocation(StationID station);
00333   bool FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse);
00334 };
00335 
00336 #endif /* TRAIN_H */

Generated on Sun Nov 15 15:40:16 2009 for OpenTTD by  doxygen 1.5.6