src/train.h
branchNewGRF_ports
changeset 6719 4cc327ad39d5
parent 6574 e1d1a12faaf7
child 6720 35756db7e577
equal deleted inserted replaced
6718:5a8b295aa345 6719:4cc327ad39d5
     1 /* $Id$ */
     1 /* $Id$ */
       
     2 
       
     3 /** @file train.h */
     2 
     4 
     3 #ifndef TRAIN_H
     5 #ifndef TRAIN_H
     4 #define TRAIN_H
     6 #define TRAIN_H
     5 
     7 
     6 #include "stdafx.h"
     8 #include "stdafx.h"
    12  * Do not access it directly unless you have to. Use the access functions below
    14  * Do not access it directly unless you have to. Use the access functions below
    13  * This is an enum to tell what bit to access as it is a bitmask
    15  * This is an enum to tell what bit to access as it is a bitmask
    14  */
    16  */
    15 
    17 
    16 enum TrainSubtype {
    18 enum TrainSubtype {
    17 	Train_Front             = 0, // Leading engine of a train
    19 	Train_Front             = 0, ///< Leading engine of a train
    18 	Train_Articulated_Part  = 1, // Articulated part of an engine
    20 	Train_Articulated_Part  = 1, ///< Articulated part of an engine
    19 	Train_Wagon             = 2, // Wagon
    21 	Train_Wagon             = 2, ///< Wagon
    20 	Train_Engine            = 3, // Engine, that can be front engines, but might be placed behind another engine
    22 	Train_Engine            = 3, ///< Engine, that can be front engines, but might be placed behind another engine
    21 	Train_Free_Wagon        = 4, // First in a wagon chain (in depot)
    23 	Train_Free_Wagon        = 4, ///< First in a wagon chain (in depot)
    22 	Train_Multiheaded       = 5, // Engine is a multiheaded
    24 	Train_Multiheaded       = 5, ///< Engine is a multiheaded
    23 };
    25 };
    24 
    26 
    25 
    27 
    26 /** Check if a vehicle is front engine
    28 /** Check if a vehicle is front engine
    27  * @param v vehicle to check
    29  * @param v vehicle to check
    28  * @return Returns true if vehicle is a front engine
    30  * @return Returns true if vehicle is a front engine
    29  */
    31  */
    30 static inline bool IsFrontEngine(const Vehicle *v)
    32 static inline bool IsFrontEngine(const Vehicle *v)
    31 {
    33 {
       
    34 	assert(v->type == VEH_TRAIN);
    32 	return HASBIT(v->subtype, Train_Front);
    35 	return HASBIT(v->subtype, Train_Front);
    33 }
    36 }
    34 
    37 
    35 /** Set front engine state
    38 /** Set front engine state
    36  * @param v vehicle to change
    39  * @param v vehicle to change
    37  */
    40  */
    38 static inline void SetFrontEngine(Vehicle *v)
    41 static inline void SetFrontEngine(Vehicle *v)
    39 {
    42 {
       
    43 	assert(v->type == VEH_TRAIN);
    40 	SETBIT(v->subtype, Train_Front);
    44 	SETBIT(v->subtype, Train_Front);
    41 }
    45 }
    42 
    46 
    43 /** Remove the front engine state
    47 /** Remove the front engine state
    44  * @param v vehicle to change
    48  * @param v vehicle to change
    45  */
    49  */
    46 static inline void ClearFrontEngine(Vehicle *v)
    50 static inline void ClearFrontEngine(Vehicle *v)
    47 {
    51 {
       
    52 	assert(v->type == VEH_TRAIN);
    48 	CLRBIT(v->subtype, Train_Front);
    53 	CLRBIT(v->subtype, Train_Front);
    49 }
    54 }
    50 
    55 
    51 /** Check if a vehicle is an articulated part of an engine
    56 /** Check if a vehicle is an articulated part of an engine
    52  * @param v vehicle to check
    57  * @param v vehicle to check
    53  * @return Returns true if vehicle is an articulated part
    58  * @return Returns true if vehicle is an articulated part
    54  */
    59  */
    55 static inline bool IsArticulatedPart(const Vehicle *v)
    60 static inline bool IsArticulatedPart(const Vehicle *v)
    56 {
    61 {
       
    62 	assert(v->type == VEH_TRAIN);
    57 	return HASBIT(v->subtype, Train_Articulated_Part);
    63 	return HASBIT(v->subtype, Train_Articulated_Part);
    58 }
    64 }
    59 
    65 
    60 /** Set a vehicle to be an articulated part
    66 /** Set a vehicle to be an articulated part
    61  * @param v vehicle to change
    67  * @param v vehicle to change
    62  */
    68  */
    63 static inline void SetArticulatedPart(Vehicle *v)
    69 static inline void SetArticulatedPart(Vehicle *v)
    64 {
    70 {
       
    71 	assert(v->type == VEH_TRAIN);
    65 	SETBIT(v->subtype, Train_Articulated_Part);
    72 	SETBIT(v->subtype, Train_Articulated_Part);
    66 }
    73 }
    67 
    74 
    68 /** Clear a vehicle from being an articulated part
    75 /** Clear a vehicle from being an articulated part
    69  * @param v vehicle to change
    76  * @param v vehicle to change
    70  */
    77  */
    71 static inline void ClearArticulatedPart(Vehicle *v)
    78 static inline void ClearArticulatedPart(Vehicle *v)
    72 {
    79 {
       
    80 	assert(v->type == VEH_TRAIN);
    73 	CLRBIT(v->subtype, Train_Articulated_Part);
    81 	CLRBIT(v->subtype, Train_Articulated_Part);
    74 }
    82 }
    75 
    83 
    76 /** Check if a vehicle is a wagon
    84 /** Check if a vehicle is a wagon
    77  * @param v vehicle to check
    85  * @param v vehicle to check
    78  * @return Returns true if vehicle is a wagon
    86  * @return Returns true if vehicle is a wagon
    79  */
    87  */
    80 static inline bool IsTrainWagon(const Vehicle *v)
    88 static inline bool IsTrainWagon(const Vehicle *v)
    81 {
    89 {
       
    90 	assert(v->type == VEH_TRAIN);
    82 	return HASBIT(v->subtype, Train_Wagon);
    91 	return HASBIT(v->subtype, Train_Wagon);
    83 }
    92 }
    84 
    93 
    85 /** Set a vehicle to be a wagon
    94 /** Set a vehicle to be a wagon
    86  * @param v vehicle to change
    95  * @param v vehicle to change
    87  */
    96  */
    88 static inline void SetTrainWagon(Vehicle *v)
    97 static inline void SetTrainWagon(Vehicle *v)
    89 {
    98 {
       
    99 	assert(v->type == VEH_TRAIN);
    90 	SETBIT(v->subtype, Train_Wagon);
   100 	SETBIT(v->subtype, Train_Wagon);
    91 }
   101 }
    92 
   102 
    93 /** Clear wagon property
   103 /** Clear wagon property
    94  * @param v vehicle to change
   104  * @param v vehicle to change
    95  */
   105  */
    96 static inline void ClearTrainWagon(Vehicle *v)
   106 static inline void ClearTrainWagon(Vehicle *v)
    97 {
   107 {
       
   108 	assert(v->type == VEH_TRAIN);
    98 	CLRBIT(v->subtype, Train_Wagon);
   109 	CLRBIT(v->subtype, Train_Wagon);
    99 }
   110 }
   100 
   111 
   101 /** Check if a vehicle is an engine (can be first in a train)
   112 /** Check if a vehicle is an engine (can be first in a train)
   102  * @param v vehicle to check
   113  * @param v vehicle to check
   103  * @return Returns true if vehicle is an engine
   114  * @return Returns true if vehicle is an engine
   104  */
   115  */
   105 static inline bool IsTrainEngine(const Vehicle *v)
   116 static inline bool IsTrainEngine(const Vehicle *v)
   106 {
   117 {
       
   118 	assert(v->type == VEH_TRAIN);
   107 	return HASBIT(v->subtype, Train_Engine);
   119 	return HASBIT(v->subtype, Train_Engine);
   108 }
   120 }
   109 
   121 
   110 /** Set engine status
   122 /** Set engine status
   111  * @param v vehicle to change
   123  * @param v vehicle to change
   112  */
   124  */
   113 static inline void SetTrainEngine(Vehicle *v)
   125 static inline void SetTrainEngine(Vehicle *v)
   114 {
   126 {
       
   127 	assert(v->type == VEH_TRAIN);
   115 	SETBIT(v->subtype, Train_Engine);
   128 	SETBIT(v->subtype, Train_Engine);
   116 }
   129 }
   117 
   130 
   118 /** Clear engine status
   131 /** Clear engine status
   119  * @param v vehicle to change
   132  * @param v vehicle to change
   120  */
   133  */
   121 static inline void ClearTrainEngine(Vehicle *v)
   134 static inline void ClearTrainEngine(Vehicle *v)
   122 {
   135 {
       
   136 	assert(v->type == VEH_TRAIN);
   123 	CLRBIT(v->subtype, Train_Engine);
   137 	CLRBIT(v->subtype, Train_Engine);
   124 }
   138 }
   125 
   139 
   126 /** Check if a vehicle is a free wagon (got no engine in front of it)
   140 /** Check if a vehicle is a free wagon (got no engine in front of it)
   127  * @param v vehicle to check
   141  * @param v vehicle to check
   128  * @return Returns true if vehicle is a free wagon
   142  * @return Returns true if vehicle is a free wagon
   129  */
   143  */
   130 static inline bool IsFreeWagon(const Vehicle *v)
   144 static inline bool IsFreeWagon(const Vehicle *v)
   131 {
   145 {
       
   146 	assert(v->type == VEH_TRAIN);
   132 	return HASBIT(v->subtype, Train_Free_Wagon);
   147 	return HASBIT(v->subtype, Train_Free_Wagon);
   133 }
   148 }
   134 
   149 
   135 /** Set if a vehicle is a free wagon
   150 /** Set if a vehicle is a free wagon
   136  * @param v vehicle to change
   151  * @param v vehicle to change
   137  */
   152  */
   138 static inline void SetFreeWagon(Vehicle *v)
   153 static inline void SetFreeWagon(Vehicle *v)
   139 {
   154 {
       
   155 	assert(v->type == VEH_TRAIN);
   140 	SETBIT(v->subtype, Train_Free_Wagon);
   156 	SETBIT(v->subtype, Train_Free_Wagon);
   141 }
   157 }
   142 
   158 
   143 /** Clear a vehicle from being a free wagon
   159 /** Clear a vehicle from being a free wagon
   144  * @param v vehicle to change
   160  * @param v vehicle to change
   145  */
   161  */
   146 static inline void ClearFreeWagon(Vehicle *v)
   162 static inline void ClearFreeWagon(Vehicle *v)
   147 {
   163 {
       
   164 	assert(v->type == VEH_TRAIN);
   148 	CLRBIT(v->subtype, Train_Free_Wagon);
   165 	CLRBIT(v->subtype, Train_Free_Wagon);
   149 }
   166 }
   150 
   167 
   151 /** Check if a vehicle is a multiheaded engine
   168 /** Check if a vehicle is a multiheaded engine
   152  * @param v vehicle to check
   169  * @param v vehicle to check
   153  * @return Returns true if vehicle is a multiheaded engine
   170  * @return Returns true if vehicle is a multiheaded engine
   154  */
   171  */
   155 static inline bool IsMultiheaded(const Vehicle *v)
   172 static inline bool IsMultiheaded(const Vehicle *v)
   156 {
   173 {
       
   174 	assert(v->type == VEH_TRAIN);
   157 	return HASBIT(v->subtype, Train_Multiheaded);
   175 	return HASBIT(v->subtype, Train_Multiheaded);
   158 }
   176 }
   159 
   177 
   160 /** Set if a vehicle is a multiheaded engine
   178 /** Set if a vehicle is a multiheaded engine
   161  * @param v vehicle to change
   179  * @param v vehicle to change
   162  */
   180  */
   163 static inline void SetMultiheaded(Vehicle *v)
   181 static inline void SetMultiheaded(Vehicle *v)
   164 {
   182 {
       
   183 	assert(v->type == VEH_TRAIN);
   165 	SETBIT(v->subtype, Train_Multiheaded);
   184 	SETBIT(v->subtype, Train_Multiheaded);
   166 }
   185 }
   167 
   186 
   168 /** Clear multiheaded engine property
   187 /** Clear multiheaded engine property
   169  * @param v vehicle to change
   188  * @param v vehicle to change
   170  */
   189  */
   171 static inline void ClearMultiheaded(Vehicle *v)
   190 static inline void ClearMultiheaded(Vehicle *v)
   172 {
   191 {
       
   192 	assert(v->type == VEH_TRAIN);
   173 	CLRBIT(v->subtype, Train_Multiheaded);
   193 	CLRBIT(v->subtype, Train_Multiheaded);
   174 }
   194 }
   175 
   195 
   176 /** Check if an engine has an articulated part.
   196 /** Check if an engine has an articulated part.
   177  * @param v Vehicle.
   197  * @param v Vehicle.
   178  * @return True if the engine has an articulated part.
   198  * @return True if the engine has an articulated part.
   179  */
   199  */
   180 static inline bool EngineHasArticPart(const Vehicle *v)
   200 static inline bool EngineHasArticPart(const Vehicle *v)
   181 {
   201 {
       
   202 	assert(v->type == VEH_TRAIN);
   182 	return (v->next != NULL && IsArticulatedPart(v->next));
   203 	return (v->next != NULL && IsArticulatedPart(v->next));
   183 }
   204 }
   184 
   205 
   185 /**
   206 /**
   186  * Get the next part of a multi-part engine.
   207  * Get the next part of a multi-part engine.
   197  * @param v Vehicle.
   218  * @param v Vehicle.
   198  * @return Last part of the engine.
   219  * @return Last part of the engine.
   199  */
   220  */
   200 static inline Vehicle *GetLastEnginePart(Vehicle *v)
   221 static inline Vehicle *GetLastEnginePart(Vehicle *v)
   201 {
   222 {
       
   223 	assert(v->type == VEH_TRAIN);
   202 	while (EngineHasArticPart(v)) v = GetNextArticPart(v);
   224 	while (EngineHasArticPart(v)) v = GetNextArticPart(v);
   203 	return v;
   225 	return v;
   204 }
   226 }
   205 
   227 
   206 /** Get the next real (non-articulated part) vehicle in the consist.
   228 /** Get the next real (non-articulated part) vehicle in the consist.
   207  * @param v Vehicle.
   229  * @param v Vehicle.
   208  * @return Next vehicle in the consist.
   230  * @return Next vehicle in the consist.
   209  */
   231  */
   210 static inline Vehicle *GetNextVehicle(const Vehicle *v)
   232 static inline Vehicle *GetNextVehicle(const Vehicle *v)
   211 {
   233 {
       
   234 	assert(v->type == VEH_TRAIN);
   212 	while (EngineHasArticPart(v)) v = GetNextArticPart(v);
   235 	while (EngineHasArticPart(v)) v = GetNextArticPart(v);
   213 
   236 
   214 	/* v now contains the last artic part in the engine */
   237 	/* v now contains the last artic part in the engine */
   215 	return v->next;
   238 	return v->next;
   216 }
   239 }
   224 void CcBuildWagon(bool success, TileIndex tile, uint32 p1, uint32 p2);
   247 void CcBuildWagon(bool success, TileIndex tile, uint32 p1, uint32 p2);
   225 void CcCloneTrain(bool success, TileIndex tile, uint32 p1, uint32 p2);
   248 void CcCloneTrain(bool success, TileIndex tile, uint32 p1, uint32 p2);
   226 
   249 
   227 byte FreightWagonMult(CargoID cargo);
   250 byte FreightWagonMult(CargoID cargo);
   228 
   251 
       
   252 /**
       
   253  * This class 'wraps' Vehicle; you do not actually instantiate this class.
       
   254  * You create a Vehicle using AllocateVehicle, so it is added to the pool
       
   255  * and you reinitialize that to a Train using:
       
   256  *   v = new (v) Train();
       
   257  *
       
   258  * As side-effect the vehicle type is set correctly.
       
   259  */
       
   260 struct Train : public Vehicle {
       
   261 	/** Initializes the Vehicle to a train */
       
   262 	Train() { this->type = VEH_TRAIN; }
       
   263 
       
   264 	/** We want to 'destruct' the right class. */
       
   265 	virtual ~Train() {}
       
   266 
       
   267 	const char *GetTypeString() const { return "train"; }
       
   268 	void MarkDirty();
       
   269 	void UpdateDeltaXY(Direction direction);
       
   270 	ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_TRAIN_INC : EXPENSES_TRAIN_RUN; }
       
   271 	WindowClass GetVehicleListWindowClass() const { return WC_TRAINS_LIST; }
       
   272 	void PlayLeaveStationSound() const;
       
   273 	bool IsPrimaryVehicle() const { return IsFrontEngine(this); }
       
   274 	bool HasFront() const { return true; }
       
   275 };
       
   276 
   229 #endif /* TRAIN_H */
   277 #endif /* TRAIN_H */