bjarni@2676: /* $Id$ */ bjarni@2676: belugas@6422: /** @file train.h */ belugas@6422: bjarni@2676: #ifndef TRAIN_H bjarni@2676: #define TRAIN_H bjarni@2676: bjarni@2676: #include "stdafx.h" bjarni@2676: #include "vehicle.h" bjarni@2676: bjarni@2676: bjarni@2676: /* bjarni@2676: * enum to handle train subtypes bjarni@2676: * Do not access it directly unless you have to. Use the access functions below bjarni@2676: * This is an enum to tell what bit to access as it is a bitmask bjarni@2676: */ bjarni@2676: rubidium@6248: enum TrainSubtype { belugas@6422: Train_Front = 0, ///< Leading engine of a train belugas@6422: Train_Articulated_Part = 1, ///< Articulated part of an engine belugas@6422: Train_Wagon = 2, ///< Wagon belugas@6422: Train_Engine = 3, ///< Engine, that can be front engines, but might be placed behind another engine belugas@6422: Train_Free_Wagon = 4, ///< First in a wagon chain (in depot) belugas@6422: Train_Multiheaded = 5, ///< Engine is a multiheaded rubidium@6248: }; bjarni@2676: bjarni@2676: bjarni@2676: /** Check if a vehicle is front engine bjarni@2676: * @param v vehicle to check bjarni@2676: * @return Returns true if vehicle is a front engine bjarni@2676: */ bjarni@2676: static inline bool IsFrontEngine(const Vehicle *v) bjarni@2676: { maedhros@6771: assert(v->type == VEH_TRAIN); skidd13@7928: return HasBit(v->subtype, Train_Front); bjarni@2676: } bjarni@2676: bjarni@2676: /** Set front engine state bjarni@2676: * @param v vehicle to change bjarni@2676: */ bjarni@2676: static inline void SetFrontEngine(Vehicle *v) bjarni@2676: { maedhros@6771: assert(v->type == VEH_TRAIN); skidd13@7931: SetBit(v->subtype, Train_Front); bjarni@2676: } bjarni@2676: bjarni@2676: /** Remove the front engine state bjarni@2676: * @param v vehicle to change bjarni@2676: */ bjarni@2676: static inline void ClearFrontEngine(Vehicle *v) bjarni@2676: { maedhros@6771: assert(v->type == VEH_TRAIN); skidd13@7929: ClrBit(v->subtype, Train_Front); bjarni@2676: } bjarni@2676: bjarni@2676: /** Check if a vehicle is an articulated part of an engine bjarni@2676: * @param v vehicle to check bjarni@2676: * @return Returns true if vehicle is an articulated part bjarni@2676: */ bjarni@2676: static inline bool IsArticulatedPart(const Vehicle *v) bjarni@2676: { maedhros@6771: assert(v->type == VEH_TRAIN); skidd13@7928: return HasBit(v->subtype, Train_Articulated_Part); bjarni@2676: } bjarni@2676: bjarni@2676: /** Set a vehicle to be an articulated part bjarni@2676: * @param v vehicle to change bjarni@2676: */ bjarni@2676: static inline void SetArticulatedPart(Vehicle *v) bjarni@2676: { maedhros@6771: assert(v->type == VEH_TRAIN); skidd13@7931: SetBit(v->subtype, Train_Articulated_Part); bjarni@2676: } bjarni@2676: bjarni@2676: /** Clear a vehicle from being an articulated part bjarni@2676: * @param v vehicle to change bjarni@2676: */ bjarni@2676: static inline void ClearArticulatedPart(Vehicle *v) bjarni@2676: { maedhros@6771: assert(v->type == VEH_TRAIN); skidd13@7929: ClrBit(v->subtype, Train_Articulated_Part); bjarni@2676: } bjarni@2676: bjarni@2676: /** Check if a vehicle is a wagon bjarni@2676: * @param v vehicle to check bjarni@2676: * @return Returns true if vehicle is a wagon bjarni@2676: */ bjarni@2676: static inline bool IsTrainWagon(const Vehicle *v) bjarni@2676: { maedhros@6771: assert(v->type == VEH_TRAIN); skidd13@7928: return HasBit(v->subtype, Train_Wagon); bjarni@2676: } bjarni@2676: bjarni@2676: /** Set a vehicle to be a wagon bjarni@2676: * @param v vehicle to change bjarni@2676: */ bjarni@2676: static inline void SetTrainWagon(Vehicle *v) bjarni@2676: { maedhros@6771: assert(v->type == VEH_TRAIN); skidd13@7931: SetBit(v->subtype, Train_Wagon); bjarni@2676: } bjarni@2676: bjarni@2676: /** Clear wagon property bjarni@2676: * @param v vehicle to change bjarni@2676: */ bjarni@2676: static inline void ClearTrainWagon(Vehicle *v) bjarni@2676: { maedhros@6771: assert(v->type == VEH_TRAIN); skidd13@7929: ClrBit(v->subtype, Train_Wagon); bjarni@2676: } bjarni@2676: bjarni@2676: /** Check if a vehicle is an engine (can be first in a train) bjarni@2676: * @param v vehicle to check bjarni@2676: * @return Returns true if vehicle is an engine bjarni@2676: */ bjarni@2676: static inline bool IsTrainEngine(const Vehicle *v) bjarni@2676: { maedhros@6771: assert(v->type == VEH_TRAIN); skidd13@7928: return HasBit(v->subtype, Train_Engine); bjarni@2676: } bjarni@2676: bjarni@2676: /** Set engine status bjarni@2676: * @param v vehicle to change bjarni@2676: */ bjarni@2676: static inline void SetTrainEngine(Vehicle *v) bjarni@2676: { maedhros@6771: assert(v->type == VEH_TRAIN); skidd13@7931: SetBit(v->subtype, Train_Engine); bjarni@2676: } bjarni@2676: bjarni@2676: /** Clear engine status bjarni@2676: * @param v vehicle to change bjarni@2676: */ bjarni@2676: static inline void ClearTrainEngine(Vehicle *v) bjarni@2676: { maedhros@6771: assert(v->type == VEH_TRAIN); skidd13@7929: ClrBit(v->subtype, Train_Engine); bjarni@2676: } bjarni@2676: bjarni@2676: /** Check if a vehicle is a free wagon (got no engine in front of it) bjarni@2676: * @param v vehicle to check bjarni@2676: * @return Returns true if vehicle is a free wagon bjarni@2676: */ bjarni@2676: static inline bool IsFreeWagon(const Vehicle *v) bjarni@2676: { maedhros@6771: assert(v->type == VEH_TRAIN); skidd13@7928: return HasBit(v->subtype, Train_Free_Wagon); bjarni@2676: } bjarni@2676: bjarni@2676: /** Set if a vehicle is a free wagon bjarni@2676: * @param v vehicle to change bjarni@2676: */ bjarni@2676: static inline void SetFreeWagon(Vehicle *v) bjarni@2676: { maedhros@6771: assert(v->type == VEH_TRAIN); skidd13@7931: SetBit(v->subtype, Train_Free_Wagon); bjarni@2676: } bjarni@2676: bjarni@2676: /** Clear a vehicle from being a free wagon bjarni@2676: * @param v vehicle to change bjarni@2676: */ bjarni@2676: static inline void ClearFreeWagon(Vehicle *v) bjarni@2676: { maedhros@6771: assert(v->type == VEH_TRAIN); skidd13@7929: ClrBit(v->subtype, Train_Free_Wagon); bjarni@2676: } bjarni@2676: bjarni@2676: /** Check if a vehicle is a multiheaded engine bjarni@2676: * @param v vehicle to check bjarni@2676: * @return Returns true if vehicle is a multiheaded engine bjarni@2676: */ bjarni@2676: static inline bool IsMultiheaded(const Vehicle *v) bjarni@2676: { maedhros@6771: assert(v->type == VEH_TRAIN); skidd13@7928: return HasBit(v->subtype, Train_Multiheaded); bjarni@2676: } bjarni@2676: bjarni@2676: /** Set if a vehicle is a multiheaded engine bjarni@2676: * @param v vehicle to change bjarni@2676: */ bjarni@2676: static inline void SetMultiheaded(Vehicle *v) bjarni@2676: { maedhros@6771: assert(v->type == VEH_TRAIN); skidd13@7931: SetBit(v->subtype, Train_Multiheaded); bjarni@2676: } bjarni@2676: bjarni@2676: /** Clear multiheaded engine property bjarni@2676: * @param v vehicle to change bjarni@2676: */ bjarni@2676: static inline void ClearMultiheaded(Vehicle *v) bjarni@2676: { maedhros@6771: assert(v->type == VEH_TRAIN); skidd13@7929: ClrBit(v->subtype, Train_Multiheaded); bjarni@2676: } bjarni@2676: bjarni@2676: /** Check if an engine has an articulated part. bjarni@2676: * @param v Vehicle. bjarni@2676: * @return True if the engine has an articulated part. bjarni@2676: */ bjarni@2676: static inline bool EngineHasArticPart(const Vehicle *v) bjarni@2676: { maedhros@6771: assert(v->type == VEH_TRAIN); rubidium@7492: return (v->Next() != NULL && IsArticulatedPart(v->Next())); bjarni@2676: } bjarni@2676: truelight@4384: /** truelight@4384: * Get the next part of a multi-part engine. truelight@4384: * Will only work on a multi-part engine (EngineHasArticPart(v) == true), truelight@4384: * Result is undefined for normal engine. truelight@4384: */ truelight@4384: static inline Vehicle *GetNextArticPart(const Vehicle *v) truelight@4384: { truelight@4384: assert(EngineHasArticPart(v)); rubidium@7492: return v->Next(); truelight@4384: } truelight@4384: bjarni@2676: /** Get the last part of a multi-part engine. bjarni@2676: * @param v Vehicle. bjarni@2676: * @return Last part of the engine. bjarni@2676: */ bjarni@2676: static inline Vehicle *GetLastEnginePart(Vehicle *v) bjarni@2676: { maedhros@6771: assert(v->type == VEH_TRAIN); truelight@4384: while (EngineHasArticPart(v)) v = GetNextArticPart(v); bjarni@2676: return v; bjarni@2676: } bjarni@2676: bjarni@7526: /** Tell if we are dealing with the rear end of a multiheaded engine. bjarni@7526: * @param v Vehicle. bjarni@7526: * @return True if the engine is the rear part of a dualheaded engine. bjarni@7526: */ bjarni@7526: static inline bool IsRearDualheaded(const Vehicle *v) bjarni@7526: { bjarni@7526: assert(v->type == VEH_TRAIN); bjarni@7526: return (IsMultiheaded(v) && !IsTrainEngine(v)); bjarni@7526: } bjarni@7526: truelight@4384: /** Get the next real (non-articulated part) vehicle in the consist. truelight@4384: * @param v Vehicle. truelight@4384: * @return Next vehicle in the consist. truelight@4384: */ truelight@4384: static inline Vehicle *GetNextVehicle(const Vehicle *v) truelight@4384: { maedhros@6771: assert(v->type == VEH_TRAIN); truelight@4384: while (EngineHasArticPart(v)) v = GetNextArticPart(v); truelight@4384: truelight@4384: /* v now contains the last artic part in the engine */ rubidium@7492: return v->Next(); truelight@4384: } truelight@4384: bjarni@7527: /** Get the next real (non-articulated part and non rear part of dualheaded engine) vehicle in the consist. bjarni@7527: * @param v Vehicle. bjarni@7527: * @return Next vehicle in the consist. bjarni@7527: */ bjarni@7527: static inline Vehicle *GetNextUnit(Vehicle *v) bjarni@7527: { bjarni@7527: assert(v->type == VEH_TRAIN); bjarni@7527: v = GetNextVehicle(v); bjarni@7527: if (v != NULL && IsRearDualheaded(v)) v = v->Next(); bjarni@7527: bjarni@7527: return v; bjarni@7527: } bjarni@7527: rubidium@6247: void ConvertOldMultiheadToNew(); rubidium@6247: void ConnectMultiheadedTrains(); peter1138@4737: uint CountArticulatedParts(EngineID engine_type); bjarni@2855: bjarni@4648: int CheckTrainInDepot(const Vehicle *v, bool needs_to_be_stopped); bjarni@5779: void CcBuildLoco(bool success, TileIndex tile, uint32 p1, uint32 p2); bjarni@5779: void CcBuildWagon(bool success, TileIndex tile, uint32 p1, uint32 p2); bjarni@4640: peter1138@5316: byte FreightWagonMult(CargoID cargo); peter1138@5163: rubidium@7490: int CheckTrainInDepot(const Vehicle *v, bool needs_to_be_stopped); rubidium@7490: int CheckTrainStoppedInDepot(const Vehicle *v); rubidium@7490: rubidium@6552: /** rubidium@6552: * This class 'wraps' Vehicle; you do not actually instantiate this class. rubidium@6552: * You create a Vehicle using AllocateVehicle, so it is added to the pool rubidium@6552: * and you reinitialize that to a Train using: rubidium@6552: * v = new (v) Train(); rubidium@6552: * rubidium@6552: * As side-effect the vehicle type is set correctly. rubidium@6552: */ rubidium@6552: struct Train : public Vehicle { rubidium@6552: /** Initializes the Vehicle to a train */ rubidium@6552: Train() { this->type = VEH_TRAIN; } rubidium@6552: rubidium@6552: /** We want to 'destruct' the right class. */ rubidium@7412: virtual ~Train() { this->PreDestructor(); } rubidium@6552: rubidium@6563: const char *GetTypeString() const { return "train"; } rubidium@6553: void MarkDirty(); rubidium@6558: void UpdateDeltaXY(Direction direction); rubidium@6563: ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_TRAIN_INC : EXPENSES_TRAIN_RUN; } rubidium@6563: WindowClass GetVehicleListWindowClass() const { return WC_TRAINS_LIST; } rubidium@6593: void PlayLeaveStationSound() const; maedhros@6773: bool IsPrimaryVehicle() const { return IsFrontEngine(this); } rubidium@7134: int GetImage(Direction direction) const; rubidium@7533: int GetDisplaySpeed() const { return this->u.rail.last_speed * 10 / 16; } rubidium@7484: int GetDisplayMaxSpeed() const { return this->u.rail.cached_max_speed * 10 / 16; } rubidium@7488: Money GetRunningCost() const; rubidium@7490: bool IsInDepot() const { return CheckTrainInDepot(this, false) != -1; } rubidium@7490: bool IsStoppedInDepot() const { return CheckTrainStoppedInDepot(this) >= 0; } rubidium@7135: void Tick(); rubidium@6552: }; rubidium@6552: bjarni@2676: #endif /* TRAIN_H */