2005-11-18 23:41:03 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-05-06 16:11:33 +01:00
|
|
|
/** @file train.h Base for the train class. */
|
2007-04-04 04:21:14 +01: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
|
|
|
|
2009-05-22 23:22:46 +01:00
|
|
|
struct Train;
|
2005-11-18 23:41:03 +00:00
|
|
|
|
2009-05-22 23:33:05 +01:00
|
|
|
enum VehicleRailFlags {
|
|
|
|
VRF_REVERSING = 0,
|
|
|
|
|
|
|
|
/* used to calculate if train is going up or down */
|
|
|
|
VRF_GOINGUP = 1,
|
|
|
|
VRF_GOINGDOWN = 2,
|
|
|
|
|
|
|
|
/* used to store if a wagon is powered or not */
|
|
|
|
VRF_POWEREDWAGON = 3,
|
|
|
|
|
|
|
|
/* used to reverse the visible direction of the vehicle */
|
|
|
|
VRF_REVERSE_DIRECTION = 4,
|
|
|
|
|
|
|
|
/* used to mark train as lost because PF can't find the route */
|
|
|
|
VRF_NO_PATH_TO_DESTINATION = 5,
|
|
|
|
|
|
|
|
/* used to mark that electric train engine is allowed to run on normal rail */
|
|
|
|
VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL = 6,
|
|
|
|
|
|
|
|
/* used for vehicle var 0xFE bit 8 (toggled each time the train is reversed, accurate for first vehicle only) */
|
|
|
|
VRF_TOGGLE_REVERSE = 7,
|
|
|
|
|
|
|
|
/* used to mark a train that can't get a path reservation */
|
|
|
|
VRF_TRAIN_STUCK = 8,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-04-07 13:36:50 +01: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 12:17:30 +01: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 12:17:30 +01: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 12:17:30 +01: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 12:17:30 +01: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 12:17:30 +01: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 12:17:30 +01: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 12:17:30 +01: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 12:17:30 +01: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 12:17:30 +01: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 12:17:30 +01: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 12:17:30 +01: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 12:17:30 +01: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 12:17:30 +01: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 12:17:30 +01: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 12:17:30 +01: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 12:17:30 +01: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 12:17:30 +01: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 12:17:30 +01: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 12:17:30 +01:00
|
|
|
assert(v->type == VEH_TRAIN);
|
2007-08-30 14:03:56 +01:00
|
|
|
return (v->Next() != NULL && IsArticulatedPart(v->Next()));
|
2005-11-18 23:41:03 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 23:22:46 +01: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));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CcBuildLoco(bool success, TileIndex tile, uint32 p1, uint32 p2);
|
|
|
|
void CcBuildWagon(bool success, TileIndex tile, uint32 p1, uint32 p2);
|
|
|
|
|
|
|
|
byte FreightWagonMult(CargoID cargo);
|
|
|
|
|
|
|
|
int CheckTrainInDepot(const Train *v, bool needs_to_be_stopped);
|
|
|
|
int CheckTrainStoppedInDepot(const Train *v);
|
|
|
|
void UpdateTrainAcceleration(Train *v);
|
|
|
|
void CheckTrainsLengths();
|
|
|
|
|
|
|
|
void FreeTrainTrackReservation(const Train *v, TileIndex origin = INVALID_TILE, Trackdir orig_td = INVALID_TRACKDIR);
|
|
|
|
bool TryPathReserve(Train *v, bool mark_as_stuck = false, bool first_tile_okay = false);
|
|
|
|
|
|
|
|
int GetTrainStopLocation(StationID station_id, TileIndex tile, const Train *v, int *station_ahead, int *station_length);
|
|
|
|
|
|
|
|
void TrainConsistChanged(Train *v, bool same_length);
|
|
|
|
void TrainPowerChanged(Train *v);
|
|
|
|
Money GetTrainRunningCost(const Train *v);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
2009-05-22 23:33:05 +01:00
|
|
|
/* Link between the two ends of a multiheaded engine */
|
|
|
|
Train *other_multiheaded_part;
|
|
|
|
|
|
|
|
uint16 crash_anim_pos;
|
|
|
|
|
|
|
|
uint16 flags;
|
|
|
|
TrackBitsByte track;
|
|
|
|
byte force_proceed;
|
|
|
|
RailTypeByte railtype;
|
|
|
|
RailTypes compatible_railtypes;
|
|
|
|
|
2009-05-22 23:22:46 +01:00
|
|
|
/** Initializes the Vehicle to a train */
|
|
|
|
Train() { this->type = VEH_TRAIN; }
|
|
|
|
|
|
|
|
/** We want to 'destruct' the right class. */
|
|
|
|
virtual ~Train() { this->PreDestructor(); }
|
|
|
|
|
|
|
|
const char *GetTypeString() const { return "train"; }
|
|
|
|
void MarkDirty();
|
|
|
|
void UpdateDeltaXY(Direction direction);
|
|
|
|
ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_TRAIN_INC : EXPENSES_TRAIN_RUN; }
|
|
|
|
void PlayLeaveStationSound() const;
|
|
|
|
bool IsPrimaryVehicle() const { return IsFrontEngine(this); }
|
|
|
|
SpriteID GetImage(Direction direction) const;
|
|
|
|
int GetDisplaySpeed() const { return this->u.rail.last_speed; }
|
|
|
|
int GetDisplayMaxSpeed() const { return this->u.rail.cached_max_speed; }
|
|
|
|
Money GetRunningCost() const;
|
|
|
|
bool IsInDepot() const { return CheckTrainInDepot(this, false) != -1; }
|
|
|
|
bool IsStoppedInDepot() const { return CheckTrainStoppedInDepot(this) >= 0; }
|
|
|
|
bool Tick();
|
|
|
|
void OnNewDay();
|
|
|
|
Trackdir GetVehicleTrackdir() const;
|
|
|
|
TileIndex GetOrderStationLocation(StationID station);
|
|
|
|
bool FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse);
|
|
|
|
Train *First() { return (Train *)this->Vehicle::First(); }
|
|
|
|
Train *First() const { return (Train *)this->Vehicle::First(); }
|
|
|
|
Train *Next() { return (Train *)this->Vehicle::Next(); }
|
|
|
|
Train *Next() const { return (Train *)this->Vehicle::Next(); }
|
|
|
|
Train *Previous() { return (Train *)this->Vehicle::Previous(); }
|
|
|
|
Train *Previous() const { return (Train *)this->Vehicle::Previous(); }
|
|
|
|
};
|
|
|
|
|
2006-08-26 15:22:54 +01: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.
|
|
|
|
*/
|
2009-05-22 23:22:46 +01:00
|
|
|
static inline Train *GetNextArticPart(const Train *v)
|
2006-08-26 15:22:54 +01:00
|
|
|
{
|
|
|
|
assert(EngineHasArticPart(v));
|
2007-08-30 14:03:56 +01:00
|
|
|
return v->Next();
|
2006-08-26 15:22:54 +01: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.
|
|
|
|
*/
|
2009-05-22 23:22:46 +01:00
|
|
|
static inline Train *GetLastEnginePart(Train *v)
|
2005-11-18 23:41:03 +00:00
|
|
|
{
|
2007-06-01 12:17:30 +01:00
|
|
|
assert(v->type == VEH_TRAIN);
|
2006-08-26 15:22:54 +01:00
|
|
|
while (EngineHasArticPart(v)) v = GetNextArticPart(v);
|
2005-11-18 23:41:03 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2006-08-26 15:22:54 +01:00
|
|
|
/** Get the next real (non-articulated part) vehicle in the consist.
|
|
|
|
* @param v Vehicle.
|
|
|
|
* @return Next vehicle in the consist.
|
|
|
|
*/
|
2009-05-22 23:22:46 +01:00
|
|
|
static inline Train *GetNextVehicle(const Train *v)
|
2006-08-26 15:22:54 +01:00
|
|
|
{
|
2007-06-01 12:17:30 +01:00
|
|
|
assert(v->type == VEH_TRAIN);
|
2006-08-26 15:22:54 +01:00
|
|
|
while (EngineHasArticPart(v)) v = GetNextArticPart(v);
|
|
|
|
|
|
|
|
/* v now contains the last artic part in the engine */
|
2007-08-30 14:03:56 +01:00
|
|
|
return v->Next();
|
2006-08-26 15:22:54 +01:00
|
|
|
}
|
|
|
|
|
2008-08-16 15:02:20 +01:00
|
|
|
/** Get the previous real (non-articulated part) vehicle in the consist.
|
|
|
|
* @param w Vehicle.
|
|
|
|
* @return Previous vehicle in the consist.
|
|
|
|
*/
|
2009-05-22 23:22:46 +01:00
|
|
|
static inline Train *GetPrevVehicle(const Train *w)
|
2008-08-16 15:02:20 +01:00
|
|
|
{
|
|
|
|
assert(w->type == VEH_TRAIN);
|
|
|
|
|
2009-05-22 23:22:46 +01:00
|
|
|
Train *v = w->Previous();
|
2008-08-16 15:02:20 +01:00
|
|
|
while (v != NULL && IsArticulatedPart(v)) v = v->Previous();
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2007-09-05 11:33:42 +01: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.
|
|
|
|
*/
|
2009-05-22 23:22:46 +01:00
|
|
|
static inline Train *GetNextUnit(const Train *v)
|
2007-09-05 11:33:42 +01:00
|
|
|
{
|
|
|
|
assert(v->type == VEH_TRAIN);
|
2009-05-22 23:22:46 +01:00
|
|
|
Train *w = GetNextVehicle(v);
|
2008-08-23 23:31:36 +01:00
|
|
|
if (w != NULL && IsRearDualheaded(w)) w = GetNextVehicle(w);
|
2007-09-05 11:33:42 +01:00
|
|
|
|
2008-08-23 23:31:36 +01:00
|
|
|
return w;
|
2007-09-05 11:33:42 +01:00
|
|
|
}
|
|
|
|
|
2008-08-16 15:02:20 +01: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.
|
|
|
|
*/
|
2009-05-22 23:22:46 +01:00
|
|
|
static inline Train *GetPrevUnit(const Train *v)
|
2008-08-16 15:02:20 +01:00
|
|
|
{
|
|
|
|
assert(v->type == VEH_TRAIN);
|
2009-05-22 23:22:46 +01:00
|
|
|
Train *w = GetPrevVehicle(v);
|
2008-08-23 23:31:36 +01:00
|
|
|
if (w != NULL && IsRearDualheaded(w)) w = GetPrevVehicle(w);
|
2008-08-16 15:02:20 +01:00
|
|
|
|
2008-08-23 23:31:36 +01:00
|
|
|
return w;
|
2008-08-16 15:02:20 +01:00
|
|
|
}
|
|
|
|
|
2005-11-18 23:41:03 +00:00
|
|
|
#endif /* TRAIN_H */
|