2005-11-18 23:41:03 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file train.h Base for the train class. */
|
2007-04-04 03:21:14 +00:00
|
|
|
|
2005-11-18 23:41:03 +00:00
|
|
|
#ifndef TRAIN_H
|
|
|
|
#define TRAIN_H
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
2007-12-27 13:35:39 +00:00
|
|
|
#include "core/bitmath_func.hpp"
|
|
|
|
#include "vehicle_base.h"
|
2005-11-18 23:41:03 +00:00
|
|
|
|
|
|
|
|
2008-04-07 12:36:50 +00:00
|
|
|
/** enum to handle train subtypes
|
2005-11-18 23:41:03 +00:00
|
|
|
* Do not access it directly unless you have to. Use the access functions below
|
|
|
|
* This is an enum to tell what bit to access as it is a bitmask
|
|
|
|
*/
|
2007-03-07 12:11:48 +00:00
|
|
|
enum TrainSubtype {
|
2008-02-20 15:18:35 +00:00
|
|
|
TS_FRONT = 0, ///< Leading engine of a train
|
|
|
|
TS_ARTICULATED_PART = 1, ///< Articulated part of an engine
|
|
|
|
TS_WAGON = 2, ///< Wagon
|
|
|
|
TS_ENGINE = 3, ///< Engine, that can be front engines, but might be placed behind another engine
|
|
|
|
TS_FREE_WAGON = 4, ///< First in a wagon chain (in depot)
|
|
|
|
TS_MULTIHEADED = 5, ///< Engine is a multiheaded
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2005-11-18 23:41:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
/** Check if a vehicle is front engine
|
|
|
|
* @param v vehicle to check
|
|
|
|
* @return Returns true if vehicle is a front engine
|
|
|
|
*/
|
|
|
|
static inline bool IsFrontEngine(const Vehicle *v)
|
|
|
|
{
|
2007-06-01 11:17:30 +00:00
|
|
|
assert(v->type == VEH_TRAIN);
|
2008-02-20 15:18:35 +00:00
|
|
|
return HasBit(v->subtype, TS_FRONT);
|
2005-11-18 23:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Set front engine state
|
|
|
|
* @param v vehicle to change
|
|
|
|
*/
|
|
|
|
static inline void SetFrontEngine(Vehicle *v)
|
|
|
|
{
|
2007-06-01 11:17:30 +00:00
|
|
|
assert(v->type == VEH_TRAIN);
|
2008-02-20 15:18:35 +00:00
|
|
|
SetBit(v->subtype, TS_FRONT);
|
2005-11-18 23:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Remove the front engine state
|
|
|
|
* @param v vehicle to change
|
|
|
|
*/
|
|
|
|
static inline void ClearFrontEngine(Vehicle *v)
|
|
|
|
{
|
2007-06-01 11:17:30 +00:00
|
|
|
assert(v->type == VEH_TRAIN);
|
2008-02-20 15:18:35 +00:00
|
|
|
ClrBit(v->subtype, TS_FRONT);
|
2005-11-18 23:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Check if a vehicle is an articulated part of an engine
|
|
|
|
* @param v vehicle to check
|
|
|
|
* @return Returns true if vehicle is an articulated part
|
|
|
|
*/
|
|
|
|
static inline bool IsArticulatedPart(const Vehicle *v)
|
|
|
|
{
|
2007-06-01 11:17:30 +00:00
|
|
|
assert(v->type == VEH_TRAIN);
|
2008-02-20 15:18:35 +00:00
|
|
|
return HasBit(v->subtype, TS_ARTICULATED_PART);
|
2005-11-18 23:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Set a vehicle to be an articulated part
|
|
|
|
* @param v vehicle to change
|
|
|
|
*/
|
|
|
|
static inline void SetArticulatedPart(Vehicle *v)
|
|
|
|
{
|
2007-06-01 11:17:30 +00:00
|
|
|
assert(v->type == VEH_TRAIN);
|
2008-02-20 15:18:35 +00:00
|
|
|
SetBit(v->subtype, TS_ARTICULATED_PART);
|
2005-11-18 23:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Clear a vehicle from being an articulated part
|
|
|
|
* @param v vehicle to change
|
|
|
|
*/
|
|
|
|
static inline void ClearArticulatedPart(Vehicle *v)
|
|
|
|
{
|
2007-06-01 11:17:30 +00:00
|
|
|
assert(v->type == VEH_TRAIN);
|
2008-02-20 15:18:35 +00:00
|
|
|
ClrBit(v->subtype, TS_ARTICULATED_PART);
|
2005-11-18 23:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Check if a vehicle is a wagon
|
|
|
|
* @param v vehicle to check
|
|
|
|
* @return Returns true if vehicle is a wagon
|
|
|
|
*/
|
|
|
|
static inline bool IsTrainWagon(const Vehicle *v)
|
|
|
|
{
|
2007-06-01 11:17:30 +00:00
|
|
|
assert(v->type == VEH_TRAIN);
|
2008-02-20 15:18:35 +00:00
|
|
|
return HasBit(v->subtype, TS_WAGON);
|
2005-11-18 23:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Set a vehicle to be a wagon
|
|
|
|
* @param v vehicle to change
|
|
|
|
*/
|
|
|
|
static inline void SetTrainWagon(Vehicle *v)
|
|
|
|
{
|
2007-06-01 11:17:30 +00:00
|
|
|
assert(v->type == VEH_TRAIN);
|
2008-02-20 15:18:35 +00:00
|
|
|
SetBit(v->subtype, TS_WAGON);
|
2005-11-18 23:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Clear wagon property
|
|
|
|
* @param v vehicle to change
|
|
|
|
*/
|
|
|
|
static inline void ClearTrainWagon(Vehicle *v)
|
|
|
|
{
|
2007-06-01 11:17:30 +00:00
|
|
|
assert(v->type == VEH_TRAIN);
|
2008-02-20 15:18:35 +00:00
|
|
|
ClrBit(v->subtype, TS_WAGON);
|
2005-11-18 23:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Check if a vehicle is an engine (can be first in a train)
|
|
|
|
* @param v vehicle to check
|
|
|
|
* @return Returns true if vehicle is an engine
|
|
|
|
*/
|
|
|
|
static inline bool IsTrainEngine(const Vehicle *v)
|
|
|
|
{
|
2007-06-01 11:17:30 +00:00
|
|
|
assert(v->type == VEH_TRAIN);
|
2008-02-20 15:18:35 +00:00
|
|
|
return HasBit(v->subtype, TS_ENGINE);
|
2005-11-18 23:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Set engine status
|
|
|
|
* @param v vehicle to change
|
|
|
|
*/
|
|
|
|
static inline void SetTrainEngine(Vehicle *v)
|
|
|
|
{
|
2007-06-01 11:17:30 +00:00
|
|
|
assert(v->type == VEH_TRAIN);
|
2008-02-20 15:18:35 +00:00
|
|
|
SetBit(v->subtype, TS_ENGINE);
|
2005-11-18 23:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Clear engine status
|
|
|
|
* @param v vehicle to change
|
|
|
|
*/
|
|
|
|
static inline void ClearTrainEngine(Vehicle *v)
|
|
|
|
{
|
2007-06-01 11:17:30 +00:00
|
|
|
assert(v->type == VEH_TRAIN);
|
2008-02-20 15:18:35 +00:00
|
|
|
ClrBit(v->subtype, TS_ENGINE);
|
2005-11-18 23:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Check if a vehicle is a free wagon (got no engine in front of it)
|
|
|
|
* @param v vehicle to check
|
|
|
|
* @return Returns true if vehicle is a free wagon
|
|
|
|
*/
|
|
|
|
static inline bool IsFreeWagon(const Vehicle *v)
|
|
|
|
{
|
2007-06-01 11:17:30 +00:00
|
|
|
assert(v->type == VEH_TRAIN);
|
2008-02-20 15:18:35 +00:00
|
|
|
return HasBit(v->subtype, TS_FREE_WAGON);
|
2005-11-18 23:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Set if a vehicle is a free wagon
|
|
|
|
* @param v vehicle to change
|
|
|
|
*/
|
|
|
|
static inline void SetFreeWagon(Vehicle *v)
|
|
|
|
{
|
2007-06-01 11:17:30 +00:00
|
|
|
assert(v->type == VEH_TRAIN);
|
2008-02-20 15:18:35 +00:00
|
|
|
SetBit(v->subtype, TS_FREE_WAGON);
|
2005-11-18 23:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Clear a vehicle from being a free wagon
|
|
|
|
* @param v vehicle to change
|
|
|
|
*/
|
|
|
|
static inline void ClearFreeWagon(Vehicle *v)
|
|
|
|
{
|
2007-06-01 11:17:30 +00:00
|
|
|
assert(v->type == VEH_TRAIN);
|
2008-02-20 15:18:35 +00:00
|
|
|
ClrBit(v->subtype, TS_FREE_WAGON);
|
2005-11-18 23:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Check if a vehicle is a multiheaded engine
|
|
|
|
* @param v vehicle to check
|
|
|
|
* @return Returns true if vehicle is a multiheaded engine
|
|
|
|
*/
|
|
|
|
static inline bool IsMultiheaded(const Vehicle *v)
|
|
|
|
{
|
2007-06-01 11:17:30 +00:00
|
|
|
assert(v->type == VEH_TRAIN);
|
2008-02-20 15:18:35 +00:00
|
|
|
return HasBit(v->subtype, TS_MULTIHEADED);
|
2005-11-18 23:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Set if a vehicle is a multiheaded engine
|
|
|
|
* @param v vehicle to change
|
|
|
|
*/
|
|
|
|
static inline void SetMultiheaded(Vehicle *v)
|
|
|
|
{
|
2007-06-01 11:17:30 +00:00
|
|
|
assert(v->type == VEH_TRAIN);
|
2008-02-20 15:18:35 +00:00
|
|
|
SetBit(v->subtype, TS_MULTIHEADED);
|
2005-11-18 23:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Clear multiheaded engine property
|
|
|
|
* @param v vehicle to change
|
|
|
|
*/
|
|
|
|
static inline void ClearMultiheaded(Vehicle *v)
|
|
|
|
{
|
2007-06-01 11:17:30 +00:00
|
|
|
assert(v->type == VEH_TRAIN);
|
2008-02-20 15:18:35 +00:00
|
|
|
ClrBit(v->subtype, TS_MULTIHEADED);
|
2005-11-18 23:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Check if an engine has an articulated part.
|
|
|
|
* @param v Vehicle.
|
|
|
|
* @return True if the engine has an articulated part.
|
|
|
|
*/
|
|
|
|
static inline bool EngineHasArticPart(const Vehicle *v)
|
|
|
|
{
|
2007-06-01 11:17:30 +00:00
|
|
|
assert(v->type == VEH_TRAIN);
|
2007-08-30 13:03:56 +00:00
|
|
|
return (v->Next() != NULL && IsArticulatedPart(v->Next()));
|
2005-11-18 23:41:03 +00:00
|
|
|
}
|
|
|
|
|
2006-08-26 14:22:54 +00:00
|
|
|
/**
|
|
|
|
* Get the next part of a multi-part engine.
|
|
|
|
* Will only work on a multi-part engine (EngineHasArticPart(v) == true),
|
|
|
|
* Result is undefined for normal engine.
|
|
|
|
*/
|
|
|
|
static inline Vehicle *GetNextArticPart(const Vehicle *v)
|
|
|
|
{
|
|
|
|
assert(EngineHasArticPart(v));
|
2007-08-30 13:03:56 +00:00
|
|
|
return v->Next();
|
2006-08-26 14:22:54 +00:00
|
|
|
}
|
|
|
|
|
2005-11-18 23:41:03 +00:00
|
|
|
/** Get the last part of a multi-part engine.
|
|
|
|
* @param v Vehicle.
|
|
|
|
* @return Last part of the engine.
|
|
|
|
*/
|
|
|
|
static inline Vehicle *GetLastEnginePart(Vehicle *v)
|
|
|
|
{
|
2007-06-01 11:17:30 +00:00
|
|
|
assert(v->type == VEH_TRAIN);
|
2006-08-26 14:22:54 +00:00
|
|
|
while (EngineHasArticPart(v)) v = GetNextArticPart(v);
|
2005-11-18 23:41:03 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2007-09-05 10:15:23 +00:00
|
|
|
/** Tell if we are dealing with the rear end of a multiheaded engine.
|
|
|
|
* @param v Vehicle.
|
|
|
|
* @return True if the engine is the rear part of a dualheaded engine.
|
|
|
|
*/
|
|
|
|
static inline bool IsRearDualheaded(const Vehicle *v)
|
|
|
|
{
|
|
|
|
assert(v->type == VEH_TRAIN);
|
|
|
|
return (IsMultiheaded(v) && !IsTrainEngine(v));
|
|
|
|
}
|
|
|
|
|
2006-08-26 14:22:54 +00:00
|
|
|
/** Get the next real (non-articulated part) vehicle in the consist.
|
|
|
|
* @param v Vehicle.
|
|
|
|
* @return Next vehicle in the consist.
|
|
|
|
*/
|
|
|
|
static inline Vehicle *GetNextVehicle(const Vehicle *v)
|
|
|
|
{
|
2007-06-01 11:17:30 +00:00
|
|
|
assert(v->type == VEH_TRAIN);
|
2006-08-26 14:22:54 +00:00
|
|
|
while (EngineHasArticPart(v)) v = GetNextArticPart(v);
|
|
|
|
|
|
|
|
/* v now contains the last artic part in the engine */
|
2007-08-30 13:03:56 +00:00
|
|
|
return v->Next();
|
2006-08-26 14:22:54 +00:00
|
|
|
}
|
|
|
|
|
2008-08-16 14:02:20 +00:00
|
|
|
/** Get the previous real (non-articulated part) vehicle in the consist.
|
|
|
|
* @param w Vehicle.
|
|
|
|
* @return Previous vehicle in the consist.
|
|
|
|
*/
|
|
|
|
static inline Vehicle *GetPrevVehicle(const Vehicle *w)
|
|
|
|
{
|
|
|
|
assert(w->type == VEH_TRAIN);
|
|
|
|
|
|
|
|
Vehicle *v = w->Previous();
|
|
|
|
while (v != NULL && IsArticulatedPart(v)) v = v->Previous();
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2007-09-05 10:33:42 +00:00
|
|
|
/** Get the next real (non-articulated part and non rear part of dualheaded engine) vehicle in the consist.
|
|
|
|
* @param v Vehicle.
|
|
|
|
* @return Next vehicle in the consist.
|
|
|
|
*/
|
2008-08-23 22:31:36 +00:00
|
|
|
static inline Vehicle *GetNextUnit(const Vehicle *v)
|
2007-09-05 10:33:42 +00:00
|
|
|
{
|
|
|
|
assert(v->type == VEH_TRAIN);
|
2008-08-23 22:31:36 +00:00
|
|
|
Vehicle *w = GetNextVehicle(v);
|
|
|
|
if (w != NULL && IsRearDualheaded(w)) w = GetNextVehicle(w);
|
2007-09-05 10:33:42 +00:00
|
|
|
|
2008-08-23 22:31:36 +00:00
|
|
|
return w;
|
2007-09-05 10:33:42 +00:00
|
|
|
}
|
|
|
|
|
2008-08-16 14:02:20 +00:00
|
|
|
/** Get the previous real (non-articulated part and non rear part of dualheaded engine) vehicle in the consist.
|
|
|
|
* @param v Vehicle.
|
|
|
|
* @return Previous vehicle in the consist.
|
|
|
|
*/
|
2008-08-23 22:31:36 +00:00
|
|
|
static inline Vehicle *GetPrevUnit(const Vehicle *v)
|
2008-08-16 14:02:20 +00:00
|
|
|
{
|
|
|
|
assert(v->type == VEH_TRAIN);
|
2008-08-23 22:31:36 +00:00
|
|
|
Vehicle *w = GetPrevVehicle(v);
|
|
|
|
if (w != NULL && IsRearDualheaded(w)) w = GetPrevVehicle(w);
|
2008-08-16 14:02:20 +00:00
|
|
|
|
2008-08-23 22:31:36 +00:00
|
|
|
return w;
|
2008-08-16 14:02:20 +00:00
|
|
|
}
|
|
|
|
|
2007-01-21 22:50:43 +00:00
|
|
|
void CcBuildLoco(bool success, TileIndex tile, uint32 p1, uint32 p2);
|
|
|
|
void CcBuildWagon(bool success, TileIndex tile, uint32 p1, uint32 p2);
|
2006-09-26 19:20:35 +00:00
|
|
|
|
2006-12-10 19:00:06 +00:00
|
|
|
byte FreightWagonMult(CargoID cargo);
|
2006-11-27 23:11:56 +00:00
|
|
|
|
2007-08-29 21:49:08 +00:00
|
|
|
int CheckTrainInDepot(const Vehicle *v, bool needs_to_be_stopped);
|
|
|
|
int CheckTrainStoppedInDepot(const Vehicle *v);
|
2009-01-10 00:31:47 +00:00
|
|
|
void UpdateTrainAcceleration(Vehicle *v);
|
2008-07-24 15:19:26 +00:00
|
|
|
void CheckTrainsLengths();
|
2007-08-29 21:49:08 +00:00
|
|
|
|
2008-08-02 22:53:51 +00:00
|
|
|
void FreeTrainTrackReservation(const Vehicle *v, TileIndex origin = INVALID_TILE, Trackdir orig_td = INVALID_TRACKDIR);
|
2008-08-02 22:54:38 +00:00
|
|
|
bool TryPathReserve(Vehicle *v, bool mark_as_stuck = false, bool first_tile_okay = false);
|
2008-08-02 22:53:51 +00:00
|
|
|
|
2007-04-29 21:24:08 +00:00
|
|
|
/**
|
|
|
|
* This class 'wraps' Vehicle; you do not actually instantiate this class.
|
|
|
|
* You create a Vehicle using AllocateVehicle, so it is added to the pool
|
|
|
|
* and you reinitialize that to a Train using:
|
|
|
|
* v = new (v) Train();
|
|
|
|
*
|
|
|
|
* As side-effect the vehicle type is set correctly.
|
|
|
|
*/
|
|
|
|
struct Train : public Vehicle {
|
|
|
|
/** Initializes the Vehicle to a train */
|
|
|
|
Train() { this->type = VEH_TRAIN; }
|
|
|
|
|
|
|
|
/** We want to 'destruct' the right class. */
|
2007-08-05 17:43:04 +00:00
|
|
|
virtual ~Train() { this->PreDestructor(); }
|
2007-04-29 21:24:08 +00:00
|
|
|
|
2007-05-02 09:39:11 +00:00
|
|
|
const char *GetTypeString() const { return "train"; }
|
2007-04-29 22:33:51 +00:00
|
|
|
void MarkDirty();
|
2007-05-01 16:35:14 +00:00
|
|
|
void UpdateDeltaXY(Direction direction);
|
2007-05-02 09:39:11 +00:00
|
|
|
ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_TRAIN_INC : EXPENSES_TRAIN_RUN; }
|
2007-05-07 15:58:05 +00:00
|
|
|
void PlayLeaveStationSound() const;
|
2007-06-01 12:03:10 +00:00
|
|
|
bool IsPrimaryVehicle() const { return IsFrontEngine(this); }
|
2008-04-21 20:50:58 +00:00
|
|
|
SpriteID GetImage(Direction direction) const;
|
2007-09-07 21:09:37 +00:00
|
|
|
int GetDisplaySpeed() const { return this->u.rail.last_speed * 10 / 16; }
|
2007-08-28 06:46:33 +00:00
|
|
|
int GetDisplayMaxSpeed() const { return this->u.rail.cached_max_speed * 10 / 16; }
|
2007-08-29 21:27:16 +00:00
|
|
|
Money GetRunningCost() const;
|
2007-08-29 21:49:08 +00:00
|
|
|
bool IsInDepot() const { return CheckTrainInDepot(this, false) != -1; }
|
|
|
|
bool IsStoppedInDepot() const { return CheckTrainStoppedInDepot(this) >= 0; }
|
2007-07-01 19:24:54 +00:00
|
|
|
void Tick();
|
2008-02-01 22:02:14 +00:00
|
|
|
void OnNewDay();
|
2008-04-05 10:55:50 +00:00
|
|
|
TileIndex GetOrderStationLocation(StationID station);
|
2008-04-11 08:14:43 +00:00
|
|
|
bool FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse);
|
2007-04-29 21:24:08 +00:00
|
|
|
};
|
|
|
|
|
2005-11-18 23:41:03 +00:00
|
|
|
#endif /* TRAIN_H */
|