2005-07-24 14:12:37 +00:00
/* $Id$ */
2007-12-27 13:35:39 +00:00
/** @file vehicle_base.h Base class for all vehicles. */
2007-04-04 04:08:47 +00:00
2007-12-27 13:35:39 +00:00
# ifndef VEHICLE_BASE_H
# define VEHICLE_BASE_H
2004-08-09 17:04:08 +00:00
2007-12-19 23:26:02 +00:00
# include "vehicle_type.h"
2007-12-18 20:38:16 +00:00
# include "track_type.h"
# include "rail_type.h"
# include "road_type.h"
2007-12-21 22:50:51 +00:00
# include "cargo_type.h"
2007-12-25 23:42:52 +00:00
# include "direction_type.h"
2007-12-23 10:56:02 +00:00
# include "gfx_type.h"
# include "command_type.h"
2007-12-26 13:50:40 +00:00
# include "date_type.h"
2008-04-27 20:09:29 +00:00
# include "player_base.h"
2008-01-12 14:10:35 +00:00
# include "player_type.h"
2007-12-23 10:56:02 +00:00
# include "oldpool.h"
2008-03-30 23:24:18 +00:00
# include "order_base.h"
2007-06-22 11:58:59 +00:00
# include "cargopacket.h"
2007-06-21 16:17:47 +00:00
# include "texteff.hpp"
2008-03-28 16:34:50 +00:00
# include "group_type.h"
2008-03-28 18:00:38 +00:00
# include "engine_type.h"
2008-04-27 20:09:29 +00:00
# include "order_func.h"
2008-05-07 09:07:19 +00:00
# include "transport_type.h"
2007-02-13 10:26:53 +00:00
2007-02-13 22:27:27 +00:00
/** Road vehicle states */
enum RoadVehicleStates {
/*
* Lower 4 bits are used for vehicle track direction . ( Trackdirs )
2007-02-26 22:25:18 +00:00
* When in a road stop ( bit 5 or bit 6 set ) these bits give the
2007-02-13 22:27:27 +00:00
* track direction of the entry to the road stop .
* As the entry direction will always be a diagonal
* direction ( X_NE , Y_SE , X_SW or Y_NW ) only bits 0 and 3
* are needed to hold this direction . Bit 1 is then used to show
* that the vehicle is using the second road stop bay .
2007-02-26 22:25:18 +00:00
* Bit 2 is then used for drive - through stops to show the vehicle
* is stopping at this road stop .
2007-02-13 22:27:27 +00:00
*/
/* Numeric values */
RVSB_IN_DEPOT = 0xFE , ///< The vehicle is in a depot
RVSB_WORMHOLE = 0xFF , ///< The vehicle is in a tunnel and/or bridge
/* Bit numbers */
RVS_USING_SECOND_BAY = 1 , ///< Only used while in a road stop
2007-02-14 16:37:16 +00:00
RVS_IS_STOPPING = 2 , ///< Only used for drive-through stops. Vehicle will stop here
2007-02-26 22:25:18 +00:00
RVS_DRIVE_SIDE = 4 , ///< Only used when retrieving move data
2007-02-13 22:27:27 +00:00
RVS_IN_ROAD_STOP = 5 , ///< The vehicle is in a road stop
2007-02-14 16:37:16 +00:00
RVS_IN_DT_ROAD_STOP = 6 , ///< The vehicle is in a drive-through road stop
2007-02-13 22:27:27 +00:00
/* Bit sets of the above specified bits */
RVSB_IN_ROAD_STOP = 1 < < RVS_IN_ROAD_STOP , ///< The vehicle is in a road stop
RVSB_IN_ROAD_STOP_END = RVSB_IN_ROAD_STOP + TRACKDIR_END ,
2007-02-14 16:37:16 +00:00
RVSB_IN_DT_ROAD_STOP = 1 < < RVS_IN_DT_ROAD_STOP , ///< The vehicle is in a drive-through road stop
RVSB_IN_DT_ROAD_STOP_END = RVSB_IN_DT_ROAD_STOP + TRACKDIR_END ,
2007-02-13 22:27:27 +00:00
RVSB_TRACKDIR_MASK = 0x0F , ///< The mask used to extract track dirs
RVSB_ROAD_STOP_TRACKDIR_MASK = 0x09 ///< Only bits 0 and 3 are used to encode the trackdir for road stops
} ;
2005-05-02 23:59:11 +00:00
enum VehStatus {
2006-08-22 14:38:37 +00:00
VS_HIDDEN = 0x01 ,
VS_STOPPED = 0x02 ,
VS_UNCLICKABLE = 0x04 ,
VS_DEFPAL = 0x08 ,
VS_TRAIN_SLOWING = 0x10 ,
VS_SHADOW = 0x20 ,
2005-05-02 23:59:11 +00:00
VS_AIRCRAFT_BROKEN = 0x40 ,
2006-08-22 14:38:37 +00:00
VS_CRASHED = 0x80 ,
2005-05-02 23:59:11 +00:00
} ;
2007-02-28 17:18:36 +00:00
enum VehicleFlags {
VF_LOADING_FINISHED ,
VF_CARGO_UNLOADING ,
2007-02-28 17:59:05 +00:00
VF_BUILT_AS_PROTOTYPE ,
2007-06-25 20:55:43 +00:00
VF_TIMETABLE_STARTED , ///< Whether the vehicle has started running on the timetable yet.
VF_AUTOFILL_TIMETABLE , ///< Whether the vehicle should fill in the timetable automatically.
2006-12-02 16:56:32 +00:00
} ;
2007-03-07 12:11:48 +00:00
struct VehicleRail {
2008-04-21 08:35:27 +00:00
/* Link between the two ends of a multiheaded engine */
Vehicle * other_multiheaded_part ;
/* Cached wagon override spritegroup */
const struct SpriteGroup * cached_override ;
2006-08-28 18:53:03 +00:00
uint16 last_speed ; // NOSAVE: only used in UI
2004-08-09 17:04:08 +00:00
uint16 crash_anim_pos ;
2004-09-10 19:02:27 +00:00
2007-04-04 04:08:47 +00:00
/* cached values, recalculated on load and each time a vehicle is added to/removed from the consist. */
2008-04-21 08:35:27 +00:00
uint32 cached_power ; ///< total power of the consist.
uint16 cached_max_speed ; ///< max speed of the consist. (minimum of the max speed of all vehicles in the consist)
2005-11-03 09:22:24 +00:00
uint16 cached_total_length ; ///< Length of the whole train, valid only for first engine.
2008-04-21 08:35:27 +00:00
uint8 cached_veh_length ; ///< length of this vehicle in units of 1/8 of normal length, cached because this can be set by a callback
bool cached_tilt ; ///< train can tilt; feature provides a bonus in curves
2005-11-03 09:22:24 +00:00
2007-04-04 04:08:47 +00:00
/* cached values, recalculated when the cargo on a train changes (in addition to the conditions above) */
2008-04-21 08:35:27 +00:00
uint32 cached_weight ; ///< total weight of the consist.
uint32 cached_veh_weight ; ///< weight of the vehicle.
uint32 cached_max_te ; ///< max tractive effort of consist
2005-11-04 12:58:18 +00:00
/**
* Position / type of visual effect .
* bit 0 - 3 = position of effect relative to vehicle . ( 0 = front , 8 = centre , 15 = rear )
* bit 4 - 5 = type of effect . ( 0 = default for engine class , 1 = steam , 2 = diesel , 3 = electric )
* bit 6 = disable visual effect .
* bit 7 = disable powered wagons .
*/
byte cached_vis_effect ;
2007-11-15 00:13:12 +00:00
byte user_def_data ;
2004-08-09 17:04:08 +00:00
2007-04-04 04:08:47 +00:00
/* NOSAVE: for wagon override - id of the first engine in train
* 0xffff = = not in train */
2005-05-12 23:46:01 +00:00
EngineID first_engine ;
2004-08-09 17:04:08 +00:00
2008-04-21 08:35:27 +00:00
byte flags ;
2007-01-10 18:56:51 +00:00
TrackBitsByte track ;
2004-08-09 17:04:08 +00:00
byte force_proceed ;
2007-01-10 18:56:51 +00:00
RailTypeByte railtype ;
2008-01-09 21:05:03 +00:00
RailTypes compatible_railtypes ;
2007-03-07 12:11:48 +00:00
} ;
2004-08-09 17:04:08 +00:00
2008-03-03 20:56:30 +00:00
enum VehicleRailFlags {
2006-08-22 14:38:37 +00:00
VRF_REVERSING = 0 ,
2004-08-09 17:04:08 +00:00
2007-04-04 04:08:47 +00:00
/* used to calculate if train is going up or down */
2006-08-22 14:38:37 +00:00
VRF_GOINGUP = 1 ,
VRF_GOINGDOWN = 2 ,
2005-06-06 00:19:24 +00:00
2007-04-04 04:08:47 +00:00
/* used to store if a wagon is powered or not */
2006-08-22 14:38:37 +00:00
VRF_POWEREDWAGON = 3 ,
2006-03-18 13:00:32 +00:00
2007-04-04 04:08:47 +00:00
/* used to reverse the visible direction of the vehicle */
2006-03-18 13:00:32 +00:00
VRF_REVERSE_DIRECTION = 4 ,
2006-10-17 16:16:19 +00:00
2007-04-04 04:08:47 +00:00
/* used to mark train as lost because PF can't find the route */
2006-10-17 16:16:19 +00:00
VRF_NO_PATH_TO_DESTINATION = 5 ,
2006-11-17 19:31:44 +00:00
2007-04-04 04:08:47 +00:00
/* used to mark that electric train engine is allowed to run on normal rail */
2006-11-17 19:31:44 +00:00
VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL = 6 ,
2008-03-03 20:56:30 +00:00
2008-04-14 20:48:17 +00:00
/* used for vehicle var 0xFE bit 8 (toggled each time the train is reversed, accurate for first vehicle only) */
2008-03-03 20:56:30 +00:00
VRF_TOGGLE_REVERSE = 7 ,
2004-08-09 17:04:08 +00:00
} ;
2007-03-07 12:11:48 +00:00
struct VehicleAir {
2004-08-09 17:04:08 +00:00
uint16 crashed_counter ;
2007-04-18 18:37:40 +00:00
uint16 cached_max_speed ;
2004-08-09 17:04:08 +00:00
byte pos ;
2006-06-27 21:25:53 +00:00
byte previous_pos ;
2006-03-26 22:55:27 +00:00
StationID targetairport ;
2004-08-09 17:04:08 +00:00
byte state ;
2007-03-07 12:11:48 +00:00
} ;
2004-08-09 17:04:08 +00:00
2007-03-07 12:11:48 +00:00
struct VehicleRoad {
2007-04-04 04:08:47 +00:00
byte state ; ///< @see RoadVehicleStates
2004-08-09 17:04:08 +00:00
byte frame ;
2006-02-11 10:45:20 +00:00
uint16 blocked_ctr ;
2004-08-09 17:04:08 +00:00
byte overtaking ;
byte overtaking_ctr ;
uint16 crashed_ctr ;
byte reverse_ctr ;
2005-01-29 19:41:44 +00:00
struct RoadStop * slot ;
byte slot_age ;
2007-06-11 14:00:16 +00:00
EngineID first_engine ;
byte cached_veh_length ;
2007-05-24 22:41:50 +00:00
RoadType roadtype ;
RoadTypes compatible_roadtypes ;
2007-03-07 12:11:48 +00:00
} ;
2004-08-09 17:04:08 +00:00
2008-04-20 10:13:54 +00:00
struct VehicleEffect {
2007-07-26 15:37:19 +00:00
uint16 animation_state ;
byte animation_substate ;
2007-03-07 12:11:48 +00:00
} ;
2004-08-09 17:04:08 +00:00
2007-03-07 12:11:48 +00:00
struct VehicleDisaster {
2004-08-09 17:04:08 +00:00
uint16 image_override ;
2007-07-26 15:37:19 +00:00
VehicleID big_ufo_destroyer_target ;
2007-03-07 12:11:48 +00:00
} ;
2004-08-09 17:04:08 +00:00
2007-03-07 12:11:48 +00:00
struct VehicleShip {
2007-01-10 18:56:51 +00:00
TrackBitsByte state ;
2007-03-07 12:11:48 +00:00
} ;
2004-08-09 17:04:08 +00:00
2007-08-03 19:36:00 +00:00
DECLARE_OLD_POOL ( Vehicle , Vehicle , 9 , 125 )
2004-08-09 17:04:08 +00:00
2007-09-07 22:06:52 +00:00
/* Some declarations of functions, so we can make them friendly */
2007-08-30 13:09:44 +00:00
struct SaveLoad ;
2007-09-07 22:06:52 +00:00
extern const SaveLoad * GetVehicleDescription ( VehicleType vt ) ;
2008-01-01 15:06:37 +00:00
extern void AfterLoadVehicles ( bool clear_te_id ) ;
2007-09-07 22:06:52 +00:00
struct LoadgameState ;
extern bool LoadOldVehicle ( LoadgameState * ls , int num ) ;
2007-08-30 13:09:44 +00:00
2007-12-27 13:35:39 +00:00
struct Vehicle : PoolItem < Vehicle , VehicleID , & _Vehicle_pool > , BaseVehicle {
2007-08-30 13:09:44 +00:00
private :
2008-04-21 08:35:27 +00:00
Vehicle * next ; ///< pointer to the next vehicle in the chain
Vehicle * previous ; ///< NOSAVE: pointer to the previous vehicle in the chain
Vehicle * first ; ///< NOSAVE: pointer to the first vehicle in the chain
2007-08-30 13:09:44 +00:00
public :
2008-04-21 08:35:27 +00:00
friend const SaveLoad * GetVehicleDescription ( VehicleType vt ) ; ///< So we can use private/protected variables in the saveload code
friend void AfterLoadVehicles ( bool clear_te_id ) ; ///< So we can set the previous and first pointers while loading
friend bool LoadOldVehicle ( LoadgameState * ls , int num ) ; ///< So we can set the proper next pointer while loading
2007-08-30 13:09:44 +00:00
2008-04-21 08:35:27 +00:00
Vehicle * depot_list ; ///< NOSAVE: linked list to tell what vehicles entered a depot during the last tick. Used by autoreplace
2004-08-09 17:04:08 +00:00
2008-01-12 19:58:06 +00:00
char * name ; ///< Name of vehicle
2004-08-09 17:04:08 +00:00
2008-04-21 08:35:27 +00:00
TileIndex tile ; ///< Current tile index
TileIndex dest_tile ; ///< Heading for this tile
Vehicle * next_shared ; ///< If not NULL, this points to the next vehicle that shared the order
Vehicle * prev_shared ; ///< If not NULL, this points to the prev vehicle that shared the order
Money profit_this_year ; ///< Profit this year << 8, low 8 bits are fract
Money profit_last_year ; ///< Profit last year << 8, low 8 bits are fract
Money value ;
2004-08-09 17:04:08 +00:00
2008-04-21 08:35:27 +00:00
/* Used for timetabling. */
uint32 current_order_time ; ///< How many ticks have passed since this order started.
int32 lateness_counter ; ///< How many ticks late (or early if negative) this vehicle is.
/* Boundaries for the current position in the world and a next hash link.
* NOSAVE : All of those can be updated with VehiclePositionChanged ( ) */
int32 left_coord ;
int32 top_coord ;
int32 right_coord ;
int32 bottom_coord ;
Vehicle * next_hash ;
Vehicle * next_new_hash ;
Vehicle * * old_new_hash ;
SpriteID colormap ; // NOSAVE: cached color mapping
/* Related to age and service time */
Year build_year ;
Date age ; // Age in days
Date max_age ; // Maximum age
Date date_of_last_service ;
Date service_interval ;
uint16 reliability ;
uint16 reliability_spd_dec ;
byte breakdown_ctr ;
byte breakdown_delay ;
byte breakdowns_since_last_service ;
byte breakdown_chance ;
2004-08-09 17:04:08 +00:00
2006-08-22 14:38:37 +00:00
int32 x_pos ; // coordinates
2005-01-25 21:43:57 +00:00
int32 y_pos ;
2004-08-09 17:04:08 +00:00
byte z_pos ;
2007-01-10 18:56:51 +00:00
DirectionByte direction ; // facing
2006-08-22 14:38:37 +00:00
2008-04-21 08:35:27 +00:00
PlayerByte owner ; // which player owns the vehicle?
2006-08-22 14:38:37 +00:00
byte spritenum ; // currently displayed sprite index
// 0xfd == custom sprite, 0xfe == custom second head sprite
// 0xff == reserved for another custom sprite
uint16 cur_image ; // sprite number for this vehicle
2008-04-01 14:03:20 +00:00
byte x_extent ; // x-extent of vehicle bounding box
byte y_extent ; // y-extent of vehicle bounding box
byte z_extent ; // z-extent of vehicle bounding box
2006-08-22 14:38:37 +00:00
int8 x_offs ; // x offset for vehicle sprite
int8 y_offs ; // y offset for vehicle sprite
2005-05-06 16:13:44 +00:00
EngineID engine_type ;
2004-08-09 17:04:08 +00:00
2007-06-21 16:17:47 +00:00
TextEffectID fill_percent_te_id ; // a text-effect id to a loading indicator object
2008-04-21 08:35:27 +00:00
UnitID unitnumber ; // unit number, for display purposes only
uint16 max_speed ; ///< maximum speed
uint16 cur_speed ; ///< current speed
byte subspeed ; ///< fractional speed
byte acceleration ; ///< used by train & aircraft
uint32 motion_counter ;
byte progress ;
2007-06-21 16:17:47 +00:00
2007-04-04 04:08:47 +00:00
/* for randomized variational spritegroups
* bitmask used to resolve them ; parts of it get reseeded when triggers
* of corresponding spritegroups get matched */
2004-11-17 08:52:47 +00:00
byte random_bits ;
2008-04-21 08:35:27 +00:00
byte waiting_triggers ; ///< triggers to be yet matched
2004-08-09 17:04:08 +00:00
2006-03-26 22:55:27 +00:00
StationID last_station_visited ;
2004-09-10 19:02:27 +00:00
2008-04-21 08:35:27 +00:00
CargoID cargo_type ; ///< type of cargo this vehicle is carrying
2006-05-19 10:04:03 +00:00
byte cargo_subtype ; ///< Used for livery refits (NewGRF variations)
2008-04-21 08:35:27 +00:00
uint16 cargo_cap ; ///< total capacity
2007-06-22 11:58:59 +00:00
CargoList cargo ; ///< The cargo this vehicle is carrying
2008-02-13 19:24:40 +00:00
byte day_counter ; ///< Increased by one for each day
byte tick_counter ; ///< Increased by one for each tick
byte running_ticks ; ///< Number of ticks this vehicle was not stopped this day
2004-08-09 17:04:08 +00:00
2008-04-21 08:35:27 +00:00
byte vehstatus ; ///< Status
Order current_order ; ///< The current order (+ status, like: loading)
2006-08-26 17:12:24 +00:00
VehicleOrderID num_orders ; ///< How many orders there are in the list
2008-04-21 08:35:27 +00:00
VehicleOrderID cur_order_index ; ///< The index to the current order
2005-01-15 19:06:22 +00:00
2008-04-21 08:35:27 +00:00
Order * orders ; ///< Pointer to the first order for this vehicle
2004-08-09 17:04:08 +00:00
2008-04-21 08:35:27 +00:00
bool leave_depot_instantly ; ///< NOSAVE: stores if the vehicle needs to leave the depot it just entered. Used by autoreplace
2005-11-07 23:20:47 +00:00
2008-04-21 08:35:27 +00:00
byte vehicle_flags ; ///< Used for gradual loading and other miscellaneous things (@see VehicleFlags enum)
2004-08-09 17:04:08 +00:00
uint16 load_unload_time_rem ;
2007-05-19 09:40:18 +00:00
2008-04-21 08:35:27 +00:00
GroupID group_id ; ///< Index of group Pool array
2007-06-20 19:17:22 +00:00
2008-04-21 08:35:27 +00:00
byte subtype ; ///< subtype (Filled with values from EffectVehicles/TrainSubTypes/AircraftSubTypes)
2007-10-28 15:40:18 +00:00
2004-08-09 17:04:08 +00:00
union {
VehicleRail rail ;
VehicleAir air ;
VehicleRoad road ;
2008-04-20 10:13:54 +00:00
VehicleEffect effect ;
2004-08-09 17:04:08 +00:00
VehicleDisaster disaster ;
VehicleShip ship ;
} u ;
2007-01-13 18:55:54 +00:00
2007-08-03 19:36:00 +00:00
/**
* Allocates a lot of vehicles .
* @ param vl pointer to an array of vehicles to get allocated . Can be NULL if the vehicles aren ' t needed ( makes it test only )
* @ param num number of vehicles to allocate room for
* @ return true if there is room to allocate all the vehicles
*/
static bool AllocateList ( Vehicle * * vl , int num ) ;
/** Create a new vehicle */
Vehicle ( ) ;
2007-08-05 17:43:04 +00:00
/** Destroy all stuff that (still) needs the virtual functions to work properly */
void PreDestructor ( ) ;
2007-08-03 19:36:00 +00:00
/** We want to 'destruct' the right class. */
virtual ~ Vehicle ( ) ;
2007-01-13 18:55:54 +00:00
void BeginLoading ( ) ;
void LeaveStation ( ) ;
2007-04-29 21:24:08 +00:00
2007-05-07 16:21:34 +00:00
/**
* Handle the loading of the vehicle ; when not it skips through dummy
* orders and does nothing in all other cases .
* @ param mode is the non - first call for this vehicle in this tick ?
*/
void HandleLoading ( bool mode = false ) ;
2007-04-29 21:24:08 +00:00
/**
* Get a string ' representation ' of the vehicle type .
* @ return the string representation .
*/
2007-08-03 20:17:26 +00:00
virtual const char * GetTypeString ( ) const { return " base vehicle " ; }
2007-04-29 22:33:51 +00:00
/**
* Marks the vehicles to be redrawn and updates cached variables
2007-09-09 10:13:17 +00:00
*
* This method marks the area of the vehicle on the screen as dirty .
* It can be use to repaint the vehicle .
*
* @ ingroup dirty
2007-04-29 22:33:51 +00:00
*/
virtual void MarkDirty ( ) { }
2007-05-01 16:35:14 +00:00
/**
* Updates the x and y offsets and the size of the sprite used
* for this vehicle .
* @ param direction the direction the vehicle is facing
*/
virtual void UpdateDeltaXY ( Direction direction ) { }
2007-05-02 09:29:41 +00:00
/**
* Sets the expense type associated to this vehicle type
* @ param income whether this is income or ( running ) expenses of the vehicle
*/
2007-05-02 09:39:11 +00:00
virtual ExpensesType GetExpenseType ( bool income ) const { return EXPENSES_OTHER ; }
2007-05-02 09:29:41 +00:00
2007-05-07 15:58:05 +00:00
/**
* Play the sound associated with leaving the station
*/
virtual void PlayLeaveStationSound ( ) const { }
2007-06-01 12:03:10 +00:00
/**
* Whether this is the primary vehicle in the chain .
*/
virtual bool IsPrimaryVehicle ( ) const { return false ; }
2007-07-01 19:11:47 +00:00
/**
* Gets the sprite to show for the given direction
* @ param direction the direction the vehicle is facing
* @ return the sprite for the given vehicle in the given direction
*/
2008-04-21 20:50:58 +00:00
virtual SpriteID GetImage ( Direction direction ) const { return 0 ; }
2007-07-01 19:24:54 +00:00
2007-08-26 20:43:22 +00:00
/**
* Gets the speed in mph that can be sent into SetDParam for string processing .
* @ return the vehicle ' s speed
*/
virtual int GetDisplaySpeed ( ) const { return 0 ; }
2007-08-28 06:46:33 +00:00
/**
* Gets the maximum speed in mph that can be sent into SetDParam for string processing .
* @ return the vehicle ' s maximum speed
*/
virtual int GetDisplayMaxSpeed ( ) const { return 0 ; }
2007-08-29 21:27:16 +00:00
/**
* Gets the running cost of a vehicle
* @ return the vehicle ' s running cost
*/
virtual Money GetRunningCost ( ) const { return 0 ; }
2007-08-29 21:49:08 +00:00
/**
* Check whether the vehicle is in the depot .
* @ return true if and only if the vehicle is in the depot .
*/
virtual bool IsInDepot ( ) const { return false ; }
/**
* Check whether the vehicle is in the depot * and * stopped .
* @ return true if and only if the vehicle is in the depot and stopped .
*/
virtual bool IsStoppedInDepot ( ) const { return this - > IsInDepot ( ) & & ( this - > vehstatus & VS_STOPPED ) ! = 0 ; }
2007-07-01 19:24:54 +00:00
/**
* Calls the tick handler of the vehicle
*/
2007-08-03 20:17:26 +00:00
virtual void Tick ( ) { } ;
2007-08-02 21:19:07 +00:00
2008-02-01 22:02:14 +00:00
/**
* Calls the new day handler of the vehicle
*/
virtual void OnNewDay ( ) { } ;
2007-08-29 21:27:16 +00:00
/**
* Gets the running cost of a vehicle that can be sent into SetDParam for string processing .
* @ return the vehicle ' s running cost
*/
Money GetDisplayRunningCost ( ) const { return ( this - > GetRunningCost ( ) > > 8 ) ; }
2008-02-20 17:06:58 +00:00
/**
* Gets the profit vehicle had this year . It can be sent into SetDParam for string processing .
* @ return the vehicle ' s profit this year
*/
Money GetDisplayProfitThisYear ( ) const { return ( this - > profit_this_year > > 8 ) ; }
/**
* Gets the profit vehicle had last year . It can be sent into SetDParam for string processing .
* @ return the vehicle ' s profit last year
*/
Money GetDisplayProfitLastYear ( ) const { return ( this - > profit_last_year > > 8 ) ; }
2007-08-30 13:03:56 +00:00
/**
* Set the next vehicle of this vehicle .
* @ param next the next vehicle . NULL removes the next vehicle .
*/
2007-08-30 21:11:12 +00:00
void SetNext ( Vehicle * next ) ;
2007-08-30 13:03:56 +00:00
/**
* Get the next vehicle of this vehicle .
* @ note articulated parts are also counted as vehicles .
* @ return the next vehicle or NULL when there isn ' t a next vehicle .
*/
inline Vehicle * Next ( ) const { return this - > next ; }
2007-08-30 21:11:12 +00:00
/**
* Get the previous vehicle of this vehicle .
* @ note articulated parts are also counted as vehicles .
* @ return the previous vehicle or NULL when there isn ' t a previous vehicle .
*/
inline Vehicle * Previous ( ) const { return this - > previous ; }
/**
* Get the first vehicle of this vehicle chain .
* @ return the first vehicle of the chain .
*/
inline Vehicle * First ( ) const { return this - > first ; }
2008-02-02 02:45:09 +00:00
/**
* Check if we share our orders with another vehicle .
* This is done by checking the previous and next pointers in the shared chain .
* @ return true if there are other vehicles sharing the same order
*/
inline bool IsOrderListShared ( ) const { return this - > next_shared ! = NULL | | this - > prev_shared ! = NULL ; } ;
2008-02-23 22:01:55 +00:00
bool NeedsAutorenewing ( const Player * p ) const ;
2008-04-05 10:55:50 +00:00
2008-04-08 15:48:32 +00:00
/**
* Check if the vehicle needs to go to a depot in near future ( if a opportunity presents itself ) for service or replacement .
*
* @ see NeedsAutomaticServicing ( )
* @ return true if the vehicle should go to a depot if a opportunity presents itself .
*/
bool NeedsServicing ( ) const ;
/**
* Checks if the current order should be interupted for a service - in - depot - order .
* @ see NeedsServicing ( )
* @ return true if the current order should be interupted .
*/
bool NeedsAutomaticServicing ( ) const ;
2008-04-05 10:55:50 +00:00
/**
* Determine the location for the station where the vehicle goes to next .
* Things done for example are allocating slots in a road stop or exact
* location of the platform is determined for ships .
* @ param station the station to make the next location of the vehicle .
* @ return the location ( tile ) to aim for .
*/
virtual TileIndex GetOrderStationLocation ( StationID station ) { return INVALID_TILE ; }
2008-04-11 08:14:43 +00:00
/**
* Find the closest depot for this vehicle and tell us the location ,
* DestinationID and whether we should reverse .
* @ param location where do we go to ?
* @ param destination what hangar do we go to ?
* @ param reverse should the vehicle be reversed ?
* @ return true if a depot could be found .
*/
virtual bool FindClosestDepot ( TileIndex * location , DestinationID * destination , bool * reverse ) { return false ; }
2008-04-11 08:40:10 +00:00
/**
* Send this vehicle to the depot using the given command ( s ) .
* @ param flags the command flags ( like execute and such ) .
* @ param command the command to execute .
* @ return the cost of the depot action .
*/
CommandCost SendToDepot ( uint32 flags , DepotCommand command ) ;
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 DisasterVehicle : public Vehicle {
/** Initializes the Vehicle to a disaster vehicle */
DisasterVehicle ( ) { this - > type = VEH_DISASTER ; }
/** We want to 'destruct' the right class. */
virtual ~ DisasterVehicle ( ) { }
2007-05-02 09:39:11 +00:00
const char * GetTypeString ( ) const { return " disaster vehicle " ; }
2007-05-01 16:35:14 +00:00
void UpdateDeltaXY ( Direction direction ) ;
2007-07-01 19:24:54 +00:00
void Tick ( ) ;
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 InvalidVehicle : public Vehicle {
/** Initializes the Vehicle to a invalid vehicle */
InvalidVehicle ( ) { this - > type = VEH_INVALID ; }
/** We want to 'destruct' the right class. */
virtual ~ InvalidVehicle ( ) { }
2007-05-02 09:39:11 +00:00
const char * GetTypeString ( ) const { return " invalid vehicle " ; }
2007-07-01 19:24:54 +00:00
void Tick ( ) { }
2004-08-09 17:04:08 +00:00
} ;
2007-03-07 11:47:46 +00:00
static inline VehicleID GetMaxVehicleIndex ( )
2006-08-22 20:41:26 +00:00
{
/* TODO - This isn't the real content of the function, but
* with the new pool - system this will be replaced with one that
2006-12-05 13:58:20 +00:00
* _really_ returns the highest index . Now it just returns
2006-08-22 20:41:26 +00:00
* the next safe value we are sure about everything is below .
*/
2006-12-05 13:58:20 +00:00
return GetVehiclePoolSize ( ) - 1 ;
}
2007-03-07 11:47:46 +00:00
static inline uint GetNumVehicles ( )
2006-12-05 13:58:20 +00:00
{
2006-08-22 20:41:26 +00:00
return GetVehiclePoolSize ( ) ;
}
2007-08-02 21:19:07 +00:00
# define FOR_ALL_VEHICLES_FROM(v, start) for (v = GetVehicle(start); v != NULL; v = (v->index + 1U < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) if (v->IsValid())
2006-08-22 15:33:35 +00:00
# define FOR_ALL_VEHICLES(v) FOR_ALL_VEHICLES_FROM(v, 0)
2005-02-04 13:23:29 +00:00
/**
* Check if an index is a vehicle - index ( so between 0 and max - vehicles )
2007-04-04 04:08:47 +00:00
* @ param index of the vehicle to query
2005-02-04 13:23:29 +00:00
* @ return Returns true if the vehicle - id is in range
*/
2006-08-22 18:15:17 +00:00
static inline bool IsValidVehicleID ( uint index )
2005-01-30 20:50:06 +00:00
{
2007-08-02 21:19:07 +00:00
return index < GetVehiclePoolSize ( ) & & GetVehicle ( index ) - > IsValid ( ) ;
2005-02-04 13:23:29 +00:00
}
2004-08-09 17:04:08 +00:00
2005-01-15 19:06:22 +00:00
/* Returns order 'index' of a vehicle or NULL when it doesn't exists */
static inline Order * GetVehicleOrder ( const Vehicle * v , int index )
{
Order * order = v - > orders ;
2005-05-05 20:44:52 +00:00
if ( index < 0 ) return NULL ;
2005-01-15 19:06:22 +00:00
while ( order ! = NULL & & index - - > 0 )
order = order - > next ;
return order ;
}
2007-04-04 04:08:47 +00:00
/**
* Returns the last order of a vehicle , or NULL if it doesn ' t exists
* @ param v Vehicle to query
* @ return last order of a vehicle , if available
*/
2005-01-15 19:06:22 +00:00
static inline Order * GetLastVehicleOrder ( const Vehicle * v )
{
Order * order = v - > orders ;
2005-05-05 20:44:52 +00:00
if ( order = = NULL ) return NULL ;
2005-01-15 19:06:22 +00:00
while ( order - > next ! = NULL )
order = order - > next ;
return order ;
}
2007-04-04 04:08:47 +00:00
/** Get the first vehicle of a shared-list, so we only have to walk forwards
* @ param v Vehicle to query
* @ return first vehicle of a shared - list
*/
2006-08-31 15:28:11 +00:00
static inline Vehicle * GetFirstVehicleFromSharedList ( const Vehicle * v )
2005-01-15 19:06:22 +00:00
{
2006-08-31 15:28:11 +00:00
Vehicle * u = ( Vehicle * ) v ;
while ( u - > prev_shared ! = NULL ) u = u - > prev_shared ;
2005-01-15 19:06:22 +00:00
return u ;
}
2004-08-09 17:04:08 +00:00
2006-03-01 17:35:01 +00:00
/**
2007-12-27 13:35:39 +00:00
* Returns the Trackdir on which the vehicle is currently located .
* Works for trains and ships .
* Currently works only sortof for road vehicles , since they have a fuzzy
* concept of being " on " a trackdir . Dunno really what it returns for a road
* vehicle that is halfway a tile , never really understood that part . For road
* vehicles that are at the beginning or end of the tile , should just return
* the diagonal trackdir on which they are driving . I _think_ .
* For other vehicles types , or vehicles with no clear trackdir ( such as those
* in depots ) , returns 0xFF .
2006-03-01 17:35:01 +00:00
*/
2007-12-27 13:35:39 +00:00
Trackdir GetVehicleTrackdir ( const Vehicle * v ) ;
2007-12-21 22:50:51 +00:00
2007-12-27 13:35:39 +00:00
void CheckVehicle32Day ( Vehicle * v ) ;
2007-12-21 22:50:51 +00:00
2008-04-27 20:09:29 +00:00
struct BackuppedVehicle {
private :
Vehicle * vehicles ;
BackuppedOrders * orders ;
PlayerMoneyBackup * economy ;
2008-05-04 10:05:35 +00:00
CargoPacket * cargo_packets ;
void BackupVehicle ( Vehicle * v ) ;
Vehicle * RestoreBackupVehicle ( Vehicle * v , Player * p ) ;
2008-04-27 20:09:29 +00:00
public :
2008-05-04 10:05:35 +00:00
BackuppedVehicle ( bool include_orders ) : vehicles ( NULL ) , economy ( NULL ) , cargo_packets ( NULL ) {
2008-04-27 20:09:29 +00:00
orders = include_orders ? new BackuppedOrders ( ) : NULL ;
}
2008-05-04 10:05:35 +00:00
~ BackuppedVehicle ( ) { free ( vehicles ) ; delete orders ; delete economy ; free ( cargo_packets ) ; }
2008-04-27 20:09:29 +00:00
2008-05-04 10:05:35 +00:00
void Backup ( Vehicle * v , Player * p = NULL ) ;
Vehicle * Restore ( Vehicle * v , Player * p ) ;
2008-04-27 20:09:29 +00:00
bool ContainsBackup ( ) { return vehicles ! = NULL ; }
} ;
2007-12-27 13:35:39 +00:00
# endif /* VEHICLE_BASE_H */