2005-07-24 14:12:37 +00:00
/* $Id$ */
2007-04-04 04:08:47 +00:00
/** @file vehicle.cpp */
2004-08-09 17:04:08 +00:00
# include "stdafx.h"
2005-06-02 19:30:21 +00:00
# include "openttd.h"
2006-06-05 08:34:39 +00:00
# include "road_map.h"
2006-06-05 10:23:18 +00:00
# include "roadveh.h"
2006-06-05 11:28:00 +00:00
# include "ship.h"
2005-02-10 05:43:30 +00:00
# include "spritecache.h"
2005-02-13 11:27:41 +00:00
# include "table/sprites.h"
2004-11-25 10:47:30 +00:00
# include "table/strings.h"
2005-07-22 07:02:20 +00:00
# include "functions.h"
2007-04-12 13:07:15 +00:00
# include "landscape.h"
2004-12-15 22:18:54 +00:00
# include "map.h"
2005-01-29 12:19:05 +00:00
# include "tile.h"
2004-08-09 17:04:08 +00:00
# include "vehicle.h"
# include "gfx.h"
# include "viewport.h"
# include "news.h"
# include "command.h"
# include "saveload.h"
# include "player.h"
# include "engine.h"
2004-11-05 23:12:33 +00:00
# include "sound.h"
2005-03-29 11:19:10 +00:00
# include "debug.h"
2005-05-02 23:59:11 +00:00
# include "vehicle_gui.h"
2005-05-03 20:45:23 +00:00
# include "depot.h"
# include "station.h"
(svn r2448) General cleanup of rail related code, more to follow.
* Add: rail.[ch] for rail-related enums and wrapper functions.
* Codechange: Removed dozens of magic numbers with below enums.
* Codechange: Rewrote CheckTrackCombination().
* Add: TILE_SIZE, TILE_PIXELS and TILE_HEIGHT constants.
* Add: enums RailTileType, RailTileSubtype, SignalType to mask against the map arrays.
* Add: enums Track, TrackBits, Trackdir, TrackdirBits for railway track data. (Note that the old RAIL_BIT constants are replaced by TRACK_BIT ones).
* Add: enums Direction and DiagDirection
* Codechange: Moved a bunch of track(dir) related lookup arrays from npf.[ch] to rail.[ch].
* Codechange: move RailType enum from tile.h to rail.h.
* Add: Wrapper functions for masking signal status in the map arrays: SignalAlongTrackdir, SignalAgainstTrackdir and SignalOnTrack.
* Add: Wrapper functions to access rail tiles, using above enums
* Add: Wrapper functions to modify tracks, trackdirs, directions, etc.
* Add: Wrapper functions for all lookup arrays in rail.[ch] (Arrays are still used in parts of the code)
* Codechange: Renamed some variables and arguments to better represent what they contain (railbit -> track, bits -> trackdirbits, etc.).
* Codechange: Don't use FindLandscapeHeight() in CmdRemoveSingleRail(), since it returns way too much info. Use GetTileSlope() instead.
* Codechange: [NPF] Removed some unused globals and code from npf.c.
2005-06-16 18:04:02 +00:00
# include "rail.h"
2005-11-18 23:41:03 +00:00
# include "train.h"
2006-09-28 14:17:08 +00:00
# include "aircraft.h"
2006-04-03 12:41:31 +00:00
# include "industry_map.h"
2006-03-31 19:10:54 +00:00
# include "station_map.h"
2006-06-05 08:34:39 +00:00
# include "water_map.h"
2007-01-02 17:34:03 +00:00
# include "network/network.h"
2006-07-11 19:04:50 +00:00
# include "yapf/yapf.h"
2006-08-14 14:21:15 +00:00
# include "date.h"
2007-01-17 22:19:12 +00:00
# include "newgrf_callbacks.h"
2006-09-15 12:27:00 +00:00
# include "newgrf_engine.h"
2006-09-27 18:17:01 +00:00
# include "newgrf_sound.h"
2007-01-10 18:56:51 +00:00
# include "helpers.hpp"
2004-08-09 17:04:08 +00:00
# define INVALID_COORD (-0x8000)
2006-07-26 08:27:05 +00:00
# define GEN_HASH(x, y) ((GB((y), 6, 6) << 6) + GB((x), 7, 6))
2004-08-09 17:04:08 +00:00
2005-10-24 19:40:48 +00:00
2007-01-22 16:16:52 +00:00
/* Tables used in vehicle.h to find the right command for a certain vehicle type */
const uint32 _veh_build_proc_table [ ] = {
2005-10-24 19:40:48 +00:00
CMD_BUILD_RAIL_VEHICLE ,
CMD_BUILD_ROAD_VEH ,
CMD_BUILD_SHIP ,
CMD_BUILD_AIRCRAFT ,
} ;
2007-01-22 16:16:52 +00:00
const uint32 _veh_sell_proc_table [ ] = {
2005-10-24 19:40:48 +00:00
CMD_SELL_RAIL_WAGON ,
CMD_SELL_ROAD_VEH ,
CMD_SELL_SHIP ,
CMD_SELL_AIRCRAFT ,
} ;
2005-12-14 06:28:48 +00:00
2007-01-22 16:16:52 +00:00
const uint32 _veh_refit_proc_table [ ] = {
2005-10-24 19:40:48 +00:00
CMD_REFIT_RAIL_VEHICLE ,
2006-06-09 07:45:26 +00:00
CMD_REFIT_ROAD_VEH ,
2005-10-24 19:40:48 +00:00
CMD_REFIT_SHIP ,
CMD_REFIT_AIRCRAFT ,
} ;
2006-08-29 23:39:57 +00:00
const uint32 _send_to_depot_proc_table [ ] = {
2006-08-31 15:57:38 +00:00
CMD_SEND_TRAIN_TO_DEPOT ,
2006-08-29 23:39:57 +00:00
CMD_SEND_ROADVEH_TO_DEPOT ,
CMD_SEND_SHIP_TO_DEPOT ,
CMD_SEND_AIRCRAFT_TO_HANGAR ,
} ;
2005-10-24 19:40:48 +00:00
2005-01-06 22:31:58 +00:00
enum {
2006-03-09 20:37:51 +00:00
BLOCKS_FOR_SPECIAL_VEHICLES = 2 , ///< Blocks needed for special vehicles
2005-01-06 22:31:58 +00:00
} ;
2005-02-04 13:23:29 +00:00
/**
* Called if a new block is added to the vehicle - pool
*/
static void VehiclePoolNewBlock ( uint start_item )
{
Vehicle * v ;
2006-08-22 15:33:35 +00:00
/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
* TODO - This is just a temporary stage , this will be removed . */
2007-02-07 19:10:19 +00:00
for ( v = GetVehicle ( start_item ) ; v ! = NULL ; v = ( v - > index + 1U < GetVehiclePoolSize ( ) ) ? GetVehicle ( v - > index + 1 ) : NULL ) {
v - > index = start_item + + ;
2007-03-08 16:27:54 +00:00
v - > type = VEH_INVALID ;
2007-02-07 19:10:19 +00:00
}
2005-02-04 13:23:29 +00:00
}
/* Initialize the vehicle-pool */
2006-12-03 17:27:43 +00:00
DEFINE_OLD_POOL ( Vehicle , Vehicle , VehiclePoolNewBlock , NULL )
2005-02-04 13:23:29 +00:00
2004-12-09 21:46:56 +00:00
void VehicleServiceInDepot ( Vehicle * v )
{
v - > date_of_last_service = _date ;
v - > breakdowns_since_last_service = 0 ;
2005-06-07 18:13:49 +00:00
v - > reliability = GetEngine ( v - > engine_type ) - > reliability ;
2006-10-04 12:01:59 +00:00
InvalidateWindow ( WC_VEHICLE_DETAILS , v - > index ) ; // ensure that last service date and reliability are updated
2004-12-09 21:46:56 +00:00
}
2004-08-09 17:04:08 +00:00
2004-12-11 10:17:10 +00:00
bool VehicleNeedsService ( const Vehicle * v )
{
2005-05-03 19:31:33 +00:00
if ( v - > vehstatus & VS_CRASHED )
2007-04-04 04:08:47 +00:00
return false ; // Crashed vehicles don't need service anymore
2005-05-03 19:31:33 +00:00
2006-08-14 15:03:01 +00:00
if ( _patches . no_servicing_if_no_breakdowns & & _opt . diff . vehicle_breakdowns = = 0 ) {
2006-08-14 16:18:41 +00:00
return EngineHasReplacementForPlayer ( GetPlayer ( v - > owner ) , v - > engine_type ) ; /* Vehicles set for autoreplacing needs to go to a depot even if breakdowns are turned off */
2006-08-14 15:03:01 +00:00
}
2004-12-27 18:18:44 +00:00
return _patches . servint_ispercent ?
2005-06-07 18:13:49 +00:00
( v - > reliability < GetEngine ( v - > engine_type ) - > reliability * ( 100 - v - > service_interval ) / 100 ) :
2004-12-11 10:17:10 +00:00
( v - > date_of_last_service + v - > service_interval < _date ) ;
}
2006-05-21 11:34:08 +00:00
StringID VehicleInTheWayErrMsg ( const Vehicle * v )
2004-08-09 17:04:08 +00:00
{
2005-11-13 14:54:09 +00:00
switch ( v - > type ) {
2007-03-08 16:27:54 +00:00
case VEH_TRAIN : return STR_8803_TRAIN_IN_THE_WAY ;
case VEH_ROAD : return STR_9000_ROAD_VEHICLE_IN_THE_WAY ;
case VEH_AIRCRAFT : return STR_A015_AIRCRAFT_IN_THE_WAY ;
2006-05-21 11:34:08 +00:00
default : return STR_980E_SHIP_IN_THE_WAY ;
2005-11-13 14:54:09 +00:00
}
2004-08-09 17:04:08 +00:00
}
static void * EnsureNoVehicleProc ( Vehicle * v , void * data )
{
2007-03-08 16:27:54 +00:00
if ( v - > tile ! = * ( const TileIndex * ) data | | v - > type = = VEH_DISASTER )
2004-08-09 17:04:08 +00:00
return NULL ;
2004-09-10 19:02:27 +00:00
2006-05-21 11:34:08 +00:00
_error_message = VehicleInTheWayErrMsg ( v ) ;
2004-12-03 14:38:02 +00:00
return v ;
2004-09-10 19:02:27 +00:00
}
2004-08-09 17:04:08 +00:00
bool EnsureNoVehicle ( TileIndex tile )
{
2004-12-03 14:38:02 +00:00
return VehicleFromPos ( tile , & tile , EnsureNoVehicleProc ) = = NULL ;
2004-08-09 17:04:08 +00:00
}
static void * EnsureNoVehicleProcZ ( Vehicle * v , void * data )
{
2007-01-10 18:56:51 +00:00
const TileInfo * ti = ( const TileInfo * ) data ;
2004-12-03 14:38:02 +00:00
2007-03-08 16:27:54 +00:00
if ( v - > tile ! = ti - > tile | | v - > type = = VEH_DISASTER ) return NULL ;
2006-05-09 09:56:09 +00:00
if ( v - > z_pos > ti - > z ) return NULL ;
2004-08-09 17:04:08 +00:00
2006-05-21 11:34:08 +00:00
_error_message = VehicleInTheWayErrMsg ( v ) ;
2004-12-03 14:38:02 +00:00
return v ;
2004-08-09 17:04:08 +00:00
}
2006-05-09 09:56:09 +00:00
bool EnsureNoVehicleOnGround ( TileIndex tile )
2005-01-21 19:52:32 +00:00
{
TileInfo ti ;
2006-01-22 17:17:11 +00:00
ti . tile = tile ;
2006-05-09 09:56:09 +00:00
ti . z = GetTileMaxZ ( tile ) ;
2004-12-03 14:38:02 +00:00
return VehicleFromPos ( tile , & ti , EnsureNoVehicleProcZ ) = = NULL ;
2004-08-09 17:04:08 +00:00
}
2005-03-30 09:25:20 +00:00
Vehicle * FindVehicleOnTileZ ( TileIndex tile , byte z )
{
TileInfo ti ;
ti . tile = tile ;
ti . z = z ;
2007-01-10 18:56:51 +00:00
return ( Vehicle * ) VehicleFromPos ( tile , & ti , EnsureNoVehicleProcZ ) ;
2005-03-30 09:25:20 +00:00
}
2007-02-05 14:00:32 +00:00
Vehicle * FindVehicleBetween ( TileIndex from , TileIndex to , byte z , bool without_crashed )
2004-08-09 17:04:08 +00:00
{
2005-01-07 17:02:43 +00:00
int x1 = TileX ( from ) ;
int y1 = TileY ( from ) ;
int x2 = TileX ( to ) ;
int y2 = TileY ( to ) ;
2004-08-09 17:04:08 +00:00
Vehicle * veh ;
/* Make sure x1 < x2 or y1 < y2 */
if ( x1 > x2 | | y1 > y2 ) {
2007-02-22 08:43:02 +00:00
Swap ( x1 , x2 ) ;
Swap ( y1 , y2 ) ;
2004-08-09 17:04:08 +00:00
}
2005-01-06 22:31:58 +00:00
FOR_ALL_VEHICLES ( veh ) {
2007-02-05 14:00:32 +00:00
if ( without_crashed & & ( veh - > vehstatus & VS_CRASHED ) ! = 0 ) continue ;
2007-04-18 22:10:36 +00:00
if ( ( veh - > type = = VEH_TRAIN | | veh - > type = = VEH_ROAD ) & & ( z = = 0xFF | | veh - > z_pos = = z ) ) {
if ( ( veh - > x_pos > > 4 ) > = x1 & & ( veh - > x_pos > > 4 ) < = x2 & &
( veh - > y_pos > > 4 ) > = y1 & & ( veh - > y_pos > > 4 ) < = y2 ) {
2004-08-09 17:04:08 +00:00
return veh ;
}
}
}
return NULL ;
}
2006-01-05 12:40:50 +00:00
static void UpdateVehiclePosHash ( Vehicle * v , int x , int y ) ;
2004-08-09 17:04:08 +00:00
void VehiclePositionChanged ( Vehicle * v )
{
int img = v - > cur_image ;
Point pt = RemapCoords ( v - > x_pos + v - > x_offs , v - > y_pos + v - > y_offs , v - > z_pos ) ;
2005-08-08 21:35:27 +00:00
const Sprite * spr = GetSprite ( img ) ;
2004-08-09 17:04:08 +00:00
2005-08-08 21:35:27 +00:00
pt . x + = spr - > x_offs ;
pt . y + = spr - > y_offs ;
2004-08-09 17:04:08 +00:00
UpdateVehiclePosHash ( v , pt . x , pt . y ) ;
v - > left_coord = pt . x ;
v - > top_coord = pt . y ;
2005-08-08 21:35:27 +00:00
v - > right_coord = pt . x + spr - > width + 2 ;
v - > bottom_coord = pt . y + spr - > height + 2 ;
2004-08-09 17:04:08 +00:00
}
2007-04-04 04:08:47 +00:00
/** Called after load to update coordinates */
2007-03-07 11:47:46 +00:00
void AfterLoadVehicles ( )
2004-08-09 17:04:08 +00:00
{
Vehicle * v ;
FOR_ALL_VEHICLES ( v ) {
2005-03-29 11:19:10 +00:00
v - > first = NULL ;
2007-03-08 16:27:54 +00:00
if ( v - > type = = VEH_TRAIN ) v - > u . rail . first_engine = INVALID_ENGINE ;
2006-03-29 16:30:26 +00:00
}
FOR_ALL_VEHICLES ( v ) {
2007-03-08 16:27:54 +00:00
if ( v - > type = = VEH_TRAIN & & ( IsFrontEngine ( v ) | | IsFreeWagon ( v ) ) )
2006-02-07 18:57:16 +00:00
TrainConsistChanged ( v ) ;
}
FOR_ALL_VEHICLES ( v ) {
2006-08-22 15:33:35 +00:00
switch ( v - > type ) {
2007-03-08 16:27:54 +00:00
case VEH_TRAIN : v - > cur_image = GetTrainImage ( v , v - > direction ) ; break ;
case VEH_ROAD : v - > cur_image = GetRoadVehImage ( v , v - > direction ) ; break ;
case VEH_SHIP : v - > cur_image = GetShipImage ( v , v - > direction ) ; break ;
case VEH_AIRCRAFT :
2007-01-27 12:29:55 +00:00
if ( IsNormalAircraft ( v ) ) {
2006-08-22 15:33:35 +00:00
v - > cur_image = GetAircraftImage ( v , v - > direction ) ;
2007-01-28 21:53:13 +00:00
/* The plane's shadow will have the same image as the plane */
Vehicle * shadow = v - > next ;
shadow - > cur_image = v - > cur_image ;
/* In the case of a helicopter we will update the rotor sprites */
if ( v - > subtype = = AIR_HELICOPTER ) {
Vehicle * rotor = shadow - > next ;
rotor - > cur_image = GetRotorImage ( v ) ;
}
2007-04-18 18:37:40 +00:00
UpdateAircraftCache ( v ) ;
2006-08-22 15:33:35 +00:00
}
break ;
default : break ;
2004-08-09 17:04:08 +00:00
}
2006-08-22 15:33:35 +00:00
v - > left_coord = INVALID_COORD ;
VehiclePositionChanged ( v ) ;
2004-08-09 17:04:08 +00:00
}
}
static Vehicle * InitializeVehicle ( Vehicle * v )
{
VehicleID index = v - > index ;
memset ( v , 0 , sizeof ( Vehicle ) ) ;
v - > index = index ;
2005-01-15 19:06:22 +00:00
assert ( v - > orders = = NULL ) ;
2004-08-09 17:04:08 +00:00
2007-03-08 16:27:54 +00:00
v - > type = VEH_INVALID ;
2004-08-09 17:04:08 +00:00
v - > left_coord = INVALID_COORD ;
2005-03-29 11:19:10 +00:00
v - > first = NULL ;
2004-08-09 17:04:08 +00:00
v - > next = NULL ;
2007-01-09 16:27:25 +00:00
v - > next_hash = NULL ;
2004-08-09 17:04:08 +00:00
v - > string_id = 0 ;
2005-01-23 13:30:02 +00:00
v - > next_shared = NULL ;
v - > prev_shared = NULL ;
2005-10-31 12:59:47 +00:00
v - > depot_list = NULL ;
2005-12-28 22:29:59 +00:00
v - > random_bits = 0 ;
2004-08-09 17:04:08 +00:00
return v ;
}
2005-12-28 22:29:59 +00:00
/**
* Get a value for a vehicle ' s random_bits .
* @ return A random value from 0 to 255.
*/
2007-03-07 11:47:46 +00:00
byte VehicleRandomBits ( )
2005-12-28 22:29:59 +00:00
{
return GB ( Random ( ) , 0 , 8 ) ;
}
2007-03-07 11:47:46 +00:00
Vehicle * ForceAllocateSpecialVehicle ( )
2004-08-09 17:04:08 +00:00
{
2005-02-04 13:23:29 +00:00
/* This stays a strange story.. there should always be room for special
* vehicles ( special effects all over the map ) , but with 65 k of vehicles
* is this realistic to double - check for that ? For now we just reserve
* BLOCKS_FOR_SPECIAL_VEHICLES times block_size vehicles that may only
* be used for special vehicles . . should work nicely : ) */
2004-08-09 17:04:08 +00:00
Vehicle * v ;
2005-02-04 13:23:29 +00:00
2006-08-22 15:33:35 +00:00
/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
* TODO - This is just a temporary stage , this will be removed . */
2006-10-28 10:55:59 +00:00
for ( v = GetVehicle ( 0 ) ; v ! = NULL ; v = ( v - > index + 1U < GetVehiclePoolSize ( ) ) ? GetVehicle ( v - > index + 1 ) : NULL ) {
2005-02-04 13:23:29 +00:00
/* No more room for the special vehicles, return NULL */
2006-10-28 10:55:59 +00:00
if ( v - > index > = ( 1 < < Vehicle_POOL_BLOCK_SIZE_BITS ) * BLOCKS_FOR_SPECIAL_VEHICLES )
2005-01-06 22:31:58 +00:00
return NULL ;
2006-08-22 15:33:35 +00:00
if ( ! IsValidVehicle ( v ) ) return InitializeVehicle ( v ) ;
2004-08-09 17:04:08 +00:00
}
2005-02-04 13:23:29 +00:00
2004-08-09 17:04:08 +00:00
return NULL ;
}
2007-04-04 04:08:47 +00:00
/**
2005-11-05 19:58:16 +00:00
* finds a free vehicle in the memory or allocates a new one
* returns a pointer to the first free vehicle or NULL if all vehicles are in use
* * skip_vehicles is an offset to where in the array we should begin looking
* this is to avoid looping though the same vehicles more than once after we learned that they are not free
* this feature is used by AllocateVehicles ( ) since it need to allocate more than one and when
2006-10-28 10:55:59 +00:00
* another block is added to _Vehicle_pool , since we only do that when we know it ' s already full
2005-11-05 19:58:16 +00:00
*/
static Vehicle * AllocateSingleVehicle ( VehicleID * skip_vehicles )
2004-08-09 17:04:08 +00:00
{
2005-02-04 13:23:29 +00:00
/* See note by ForceAllocateSpecialVehicle() why we skip the
* first blocks */
2004-08-09 17:04:08 +00:00
Vehicle * v ;
2006-10-28 10:55:59 +00:00
const int offset = ( 1 < < Vehicle_POOL_BLOCK_SIZE_BITS ) * BLOCKS_FOR_SPECIAL_VEHICLES ;
2004-08-09 17:04:08 +00:00
2006-08-22 15:33:35 +00:00
/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
2007-04-04 04:08:47 +00:00
* @ todo - This is just a temporary stage , this will be removed . */
2006-10-28 10:55:59 +00:00
if ( * skip_vehicles < ( _Vehicle_pool . total_items - offset ) ) { // make sure the offset in the array is not larger than the array itself
for ( v = GetVehicle ( offset + * skip_vehicles ) ; v ! = NULL ; v = ( v - > index + 1U < GetVehiclePoolSize ( ) ) ? GetVehicle ( v - > index + 1 ) : NULL ) {
2005-11-05 19:58:16 +00:00
( * skip_vehicles ) + + ;
2006-08-22 15:33:35 +00:00
if ( ! IsValidVehicle ( v ) ) return InitializeVehicle ( v ) ;
2005-11-05 19:58:16 +00:00
}
2004-08-09 17:04:08 +00:00
}
2005-02-04 13:23:29 +00:00
/* Check if we can add a block to the pool */
2006-10-28 10:55:59 +00:00
if ( AddBlockToPool ( & _Vehicle_pool ) )
2005-11-05 19:58:16 +00:00
return AllocateSingleVehicle ( skip_vehicles ) ;
2005-02-04 13:23:29 +00:00
return NULL ;
2004-08-09 17:04:08 +00:00
}
2005-11-14 19:48:04 +00:00
2007-03-07 11:47:46 +00:00
Vehicle * AllocateVehicle ( )
2005-11-05 19:58:16 +00:00
{
VehicleID counter = 0 ;
return AllocateSingleVehicle ( & counter ) ;
}
2005-11-14 19:48:04 +00:00
2005-11-05 14:01:00 +00:00
/** Allocates a lot of vehicles and frees them again
2006-09-04 20:40:33 +00:00
* @ 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
*/
2005-11-05 14:01:00 +00:00
bool AllocateVehicles ( Vehicle * * vl , int num )
{
int i ;
Vehicle * v ;
2005-11-05 19:58:16 +00:00
VehicleID counter = 0 ;
2005-11-05 14:01:00 +00:00
2006-02-01 07:36:15 +00:00
for ( i = 0 ; i ! = num ; i + + ) {
2005-11-05 19:58:16 +00:00
v = AllocateSingleVehicle ( & counter ) ;
2005-11-05 14:01:00 +00:00
if ( v = = NULL ) {
2005-11-05 19:58:16 +00:00
return false ;
}
if ( vl ! = NULL ) {
vl [ i ] = v ;
2005-11-05 14:01:00 +00:00
}
}
2005-11-05 19:58:16 +00:00
return true ;
2005-11-05 14:01:00 +00:00
}
2007-01-09 16:27:25 +00:00
static Vehicle * _vehicle_position_hash [ 0x1000 ] ;
2005-11-16 11:50:40 +00:00
2004-08-09 17:04:08 +00:00
void * VehicleFromPos ( TileIndex tile , void * data , VehicleFromPosProc * proc )
{
2006-04-03 05:32:11 +00:00
Point pt = RemapCoords ( TileX ( tile ) * TILE_SIZE , TileY ( tile ) * TILE_SIZE , 0 ) ;
2004-08-09 17:04:08 +00:00
2007-04-04 04:08:47 +00:00
/* The hash area to scan */
2006-07-26 08:27:05 +00:00
const int xl = GB ( pt . x - 174 , 7 , 6 ) ;
const int xu = GB ( pt . x + 104 , 7 , 6 ) ;
const int yl = GB ( pt . y - 294 , 6 , 6 ) < < 6 ;
const int yu = GB ( pt . y + 56 , 6 , 6 ) < < 6 ;
2004-08-09 17:04:08 +00:00
2006-07-26 08:27:05 +00:00
int x ;
int y ;
2004-08-09 17:04:08 +00:00
2006-07-26 08:27:05 +00:00
for ( y = yl ; ; y = ( y + ( 1 < < 6 ) ) & ( 0x3F < < 6 ) ) {
for ( x = xl ; ; x = ( x + 1 ) & 0x3F ) {
2007-01-09 16:27:25 +00:00
Vehicle * v = _vehicle_position_hash [ ( x + y ) & 0xFFFF ] ;
2006-07-26 08:27:05 +00:00
2007-01-09 16:27:25 +00:00
while ( v ! = NULL ) {
2006-07-26 08:27:05 +00:00
void * a = proc ( v , data ) ;
2004-09-10 19:02:27 +00:00
2005-11-14 19:48:04 +00:00
if ( a ! = NULL ) return a ;
2007-01-09 16:27:25 +00:00
v = v - > next_hash ;
2004-08-09 17:04:08 +00:00
}
2006-07-26 08:27:05 +00:00
if ( x = = xu ) break ;
2004-08-09 17:04:08 +00:00
}
2006-07-26 08:27:05 +00:00
if ( y = = yu ) break ;
2004-08-09 17:04:08 +00:00
}
return NULL ;
}
2006-01-05 12:40:50 +00:00
static void UpdateVehiclePosHash ( Vehicle * v , int x , int y )
2004-08-09 17:04:08 +00:00
{
2007-01-09 16:27:25 +00:00
Vehicle * * old_hash , * * new_hash ;
2004-08-09 17:04:08 +00:00
int old_x = v - > left_coord ;
int old_y = v - > top_coord ;
2007-04-18 22:10:36 +00:00
new_hash = ( x = = INVALID_COORD ) ? NULL : & _vehicle_position_hash [ GEN_HASH ( x , y ) ] ;
2004-08-09 17:04:08 +00:00
old_hash = ( old_x = = INVALID_COORD ) ? NULL : & _vehicle_position_hash [ GEN_HASH ( old_x , old_y ) ] ;
2004-09-10 19:02:27 +00:00
2005-11-14 19:48:04 +00:00
if ( old_hash = = new_hash ) return ;
2004-08-09 17:04:08 +00:00
/* remove from hash table? */
if ( old_hash ! = NULL ) {
Vehicle * last = NULL ;
2007-01-09 16:27:25 +00:00
Vehicle * u = * old_hash ;
while ( u ! = v ) {
2004-08-09 17:04:08 +00:00
last = u ;
2007-01-09 16:27:25 +00:00
u = u - > next_hash ;
assert ( u ! = NULL ) ;
2004-08-09 17:04:08 +00:00
}
2005-11-14 19:48:04 +00:00
if ( last = = NULL ) {
2004-08-09 17:04:08 +00:00
* old_hash = v - > next_hash ;
2005-11-14 19:48:04 +00:00
} else {
2004-08-09 17:04:08 +00:00
last - > next_hash = v - > next_hash ;
2005-11-14 19:48:04 +00:00
}
2004-08-09 17:04:08 +00:00
}
/* insert into hash table? */
if ( new_hash ! = NULL ) {
v - > next_hash = * new_hash ;
2007-01-09 16:27:25 +00:00
* new_hash = v ;
2004-08-09 17:04:08 +00:00
}
}
2007-03-07 11:47:46 +00:00
void ResetVehiclePosHash ( )
2006-12-21 10:29:16 +00:00
{
2007-01-09 16:27:25 +00:00
memset ( _vehicle_position_hash , 0 , sizeof ( _vehicle_position_hash ) ) ;
2006-12-21 10:29:16 +00:00
}
2007-03-07 11:47:46 +00:00
void InitializeVehicles ( )
2004-08-09 17:04:08 +00:00
{
2006-07-26 08:27:05 +00:00
uint i ;
2004-08-09 17:04:08 +00:00
2005-02-04 13:23:29 +00:00
/* Clean the vehicle pool, and reserve enough blocks
* for the special vehicles , plus one for all the other
* vehicles ( which is increased on - the - fly ) */
2006-10-28 10:55:59 +00:00
CleanPool ( & _Vehicle_pool ) ;
AddBlockToPool ( & _Vehicle_pool ) ;
2006-12-21 10:29:16 +00:00
for ( i = 0 ; i < BLOCKS_FOR_SPECIAL_VEHICLES ; i + + ) {
2006-10-28 10:55:59 +00:00
AddBlockToPool ( & _Vehicle_pool ) ;
2006-07-26 08:27:05 +00:00
}
2006-12-21 10:29:16 +00:00
ResetVehiclePosHash ( ) ;
2004-08-09 17:04:08 +00:00
}
Vehicle * GetLastVehicleInChain ( Vehicle * v )
{
while ( v - > next ! = NULL ) v = v - > next ;
return v ;
}
2005-06-26 21:59:21 +00:00
/** Finds the previous vehicle in a chain, by a brute force search.
* This old function is REALLY slow because it searches through all vehicles to
* find the previous vehicle , but if v - > first has not been set , then this function
* will need to be used to find the previous one . This function should never be
* called by anything but GetFirstVehicleInChain
*/
static Vehicle * GetPrevVehicleInChain_bruteforce ( const Vehicle * v )
{
Vehicle * u ;
2005-07-03 13:02:54 +00:00
2007-03-08 16:27:54 +00:00
FOR_ALL_VEHICLES ( u ) if ( u - > type = = VEH_TRAIN & & u - > next = = v ) return u ;
2005-07-03 13:02:54 +00:00
2005-06-26 21:59:21 +00:00
return NULL ;
}
/** Find the previous vehicle in a chain, by using the v->first cache.
* While this function is fast , it cannot be used in the GetFirstVehicleInChain
* function , otherwise you ' ll end up in an infinite loop call
*/
2005-03-09 21:54:52 +00:00
Vehicle * GetPrevVehicleInChain ( const Vehicle * v )
2004-08-09 17:04:08 +00:00
{
2005-03-29 08:37:44 +00:00
Vehicle * u ;
2005-06-26 21:59:21 +00:00
assert ( v ! = NULL ) ;
2004-08-09 17:04:08 +00:00
2005-06-26 21:59:21 +00:00
u = GetFirstVehicleInChain ( v ) ;
2007-04-04 04:08:47 +00:00
/* Check to see if this is the first */
2005-06-26 21:59:21 +00:00
if ( v = = u ) return NULL ;
2006-07-23 17:17:43 +00:00
for ( ; u - > next ! = v ; u = u - > next ) assert ( u - > next ! = NULL ) ;
2004-08-09 17:04:08 +00:00
2006-07-23 17:17:43 +00:00
return u ;
2004-08-09 17:04:08 +00:00
}
2005-06-26 21:59:21 +00:00
/** Finds the first vehicle in a chain.
* This function reads out the v - > first cache . Should the cache be dirty ,
* it determines the first vehicle in a chain , and updates the cache .
*/
2005-03-09 21:54:52 +00:00
Vehicle * GetFirstVehicleInChain ( const Vehicle * v )
2004-08-09 17:04:08 +00:00
{
2005-03-29 11:19:10 +00:00
Vehicle * u ;
2005-06-26 21:59:21 +00:00
assert ( v ! = NULL ) ;
2007-03-08 16:27:54 +00:00
assert ( v - > type = = VEH_TRAIN ) ;
2005-06-26 21:59:21 +00:00
2005-03-29 11:19:10 +00:00
if ( v - > first ! = NULL ) {
2006-02-08 08:18:29 +00:00
if ( IsFrontEngine ( v - > first ) | | IsFreeWagon ( v - > first ) ) return v - > first ;
2005-05-05 20:44:52 +00:00
2006-12-26 17:36:18 +00:00
DEBUG ( misc , 0 , " v->first cache faulty. We shouldn't be here, rebuilding cache! " ) ;
2005-03-29 11:19:10 +00:00
}
2005-03-29 08:37:44 +00:00
2005-05-05 20:44:52 +00:00
/* It is the fact (currently) that newly built vehicles do not have
2006-09-04 20:40:33 +00:00
* their - > first pointer set . When this is the case , go up to the
* first engine and set the pointers correctly . Also the first pointer
* is not saved in a savegame , so this has to be fixed up after loading */
2005-05-05 20:44:52 +00:00
/* Find the 'locomotive' or the first wagon in a chain */
2005-06-26 21:59:21 +00:00
while ( ( u = GetPrevVehicleInChain_bruteforce ( v ) ) ! = NULL ) v = u ;
2005-03-29 08:37:44 +00:00
2005-05-05 20:44:52 +00:00
/* Set the first pointer of all vehicles in that chain to the first wagon */
2006-02-08 08:18:29 +00:00
if ( IsFrontEngine ( v ) | | IsFreeWagon ( v ) )
2005-03-29 11:19:10 +00:00
for ( u = ( Vehicle * ) v ; u ! = NULL ; u = u - > next ) u - > first = ( Vehicle * ) v ;
2005-03-29 08:37:44 +00:00
return ( Vehicle * ) v ;
2004-08-09 17:04:08 +00:00
}
2005-11-13 13:43:55 +00:00
uint CountVehiclesInChain ( const Vehicle * v )
2004-08-09 17:04:08 +00:00
{
2005-11-14 19:48:04 +00:00
uint count = 0 ;
do count + + ; while ( ( v = v - > next ) ! = NULL ) ;
2004-08-09 17:04:08 +00:00
return count ;
}
2006-09-08 10:47:39 +00:00
/** Check if a vehicle is counted in num_engines in each player struct
* @ param * v Vehicle to test
* @ return true if the vehicle is counted in num_engines
*/
bool IsEngineCountable ( const Vehicle * v )
{
switch ( v - > type ) {
2007-03-08 16:27:54 +00:00
case VEH_AIRCRAFT : return IsNormalAircraft ( v ) ; // don't count plane shadows and helicopter rotors
case VEH_TRAIN :
2006-09-08 10:47:39 +00:00
return ! IsArticulatedPart ( v ) & & // tenders and other articulated parts
( ! IsMultiheaded ( v ) | | IsTrainEngine ( v ) ) ; // rear parts of multiheaded engines
2007-03-08 16:27:54 +00:00
case VEH_ROAD :
case VEH_SHIP :
2006-09-08 10:47:39 +00:00
return true ;
default : return false ; // Only count player buildable vehicles
}
}
2006-08-26 20:09:25 +00:00
void DestroyVehicle ( Vehicle * v )
2004-08-09 17:04:08 +00:00
{
2007-04-20 17:52:28 +00:00
if ( IsValidStationID ( v - > last_station_visited ) ) {
2007-04-20 08:00:30 +00:00
GetStation ( v - > last_station_visited ) - > loading_vehicles . remove ( v ) ;
}
2007-02-06 11:11:12 +00:00
if ( IsEngineCountable ( v ) ) {
GetPlayer ( v - > owner ) - > num_engines [ v - > engine_type ] - - ;
if ( v - > owner = = _local_player ) InvalidateAutoreplaceWindow ( v - > engine_type ) ;
}
2006-09-08 10:47:39 +00:00
2006-03-04 11:01:35 +00:00
DeleteVehicleNews ( v - > index , INVALID_STRING_ID ) ;
2006-08-26 20:09:25 +00:00
DeleteName ( v - > string_id ) ;
2007-03-08 16:27:54 +00:00
if ( v - > type = = VEH_ROAD ) ClearSlot ( v ) ;
2006-08-26 20:09:25 +00:00
2007-03-08 16:27:54 +00:00
if ( v - > type ! = VEH_TRAIN | | ( v - > type = = VEH_TRAIN & & ( IsFrontEngine ( v ) | | IsFreeWagon ( v ) ) ) ) {
2006-10-05 12:59:28 +00:00
InvalidateWindowData ( WC_VEHICLE_DEPOT , v - > tile ) ;
}
2006-08-26 20:09:25 +00:00
UpdateVehiclePosHash ( v , INVALID_COORD , 0 ) ;
2007-01-09 16:27:25 +00:00
v - > next_hash = NULL ;
2007-01-21 00:13:39 +00:00
if ( IsPlayerBuildableVehicleType ( v ) ) DeleteVehicleOrders ( v ) ;
2006-08-26 20:09:25 +00:00
/* Now remove any artic part. This will trigger an other
* destroy vehicle , which on his turn can remove any
* other artic parts . */
if ( EngineHasArticPart ( v ) ) DeleteVehicle ( v - > next ) ;
2004-08-09 17:04:08 +00:00
}
void DeleteVehicleChain ( Vehicle * v )
{
do {
Vehicle * u = v ;
2005-11-05 16:07:26 +00:00
v = GetNextVehicle ( v ) ;
2004-08-09 17:04:08 +00:00
DeleteVehicle ( u ) ;
2005-05-05 20:44:52 +00:00
} while ( v ! = NULL ) ;
2004-08-09 17:04:08 +00:00
}
void Aircraft_Tick ( Vehicle * v ) ;
void RoadVeh_Tick ( Vehicle * v ) ;
void Ship_Tick ( Vehicle * v ) ;
void Train_Tick ( Vehicle * v ) ;
2004-11-14 19:44:06 +00:00
static void EffectVehicle_Tick ( Vehicle * v ) ;
2004-08-09 17:04:08 +00:00
void DisasterVehicle_Tick ( Vehicle * v ) ;
2005-10-31 12:59:47 +00:00
2007-04-04 04:08:47 +00:00
/** head of the linked list to tell what vehicles that visited a depot in a tick */
2005-11-13 13:43:55 +00:00
static Vehicle * _first_veh_in_depot_list ;
2005-10-31 12:59:47 +00:00
/** Adds a vehicle to the list of vehicles, that visited a depot this tick
2006-09-04 20:40:33 +00:00
* @ param * v vehicle to add
*/
2005-10-31 12:59:47 +00:00
void VehicleEnteredDepotThisTick ( Vehicle * v )
{
2007-04-04 04:08:47 +00:00
/* we need to set v->leave_depot_instantly as we have no control of it's contents at this time */
2005-11-04 22:10:49 +00:00
if ( HASBIT ( v - > current_order . flags , OFB_HALT_IN_DEPOT ) & & ! HASBIT ( v - > current_order . flags , OFB_PART_OF_ORDERS ) & & v - > current_order . type = = OT_GOTO_DEPOT ) {
2007-04-04 04:08:47 +00:00
/* we keep the vehicle in the depot since the user ordered it to stay */
2005-11-03 19:51:28 +00:00
v - > leave_depot_instantly = false ;
} else {
2007-04-04 04:08:47 +00:00
/* the vehicle do not plan on stopping in the depot, so we stop it to ensure that it will not reserve the path
* out of the depot before we might autoreplace it to a different engine . The new engine would not own the reserved path
* we store that we stopped the vehicle , so autoreplace can start it again */
2005-11-01 17:20:06 +00:00
v - > vehstatus | = VS_STOPPED ;
v - > leave_depot_instantly = true ;
}
2005-10-31 12:59:47 +00:00
if ( _first_veh_in_depot_list = = NULL ) {
_first_veh_in_depot_list = v ;
} else {
Vehicle * w = _first_veh_in_depot_list ;
while ( w - > depot_list ! = NULL ) w = w - > depot_list ;
w - > depot_list = v ;
}
}
2004-08-09 17:04:08 +00:00
2006-07-26 08:41:14 +00:00
typedef void VehicleTickProc ( Vehicle * ) ;
2005-12-14 06:20:23 +00:00
static VehicleTickProc * _vehicle_tick_procs [ ] = {
2004-08-09 17:04:08 +00:00
Train_Tick ,
RoadVeh_Tick ,
Ship_Tick ,
Aircraft_Tick ,
EffectVehicle_Tick ,
DisasterVehicle_Tick ,
} ;
2007-03-07 11:47:46 +00:00
void CallVehicleTicks ( )
2004-08-09 17:04:08 +00:00
{
Vehicle * v ;
2006-07-11 19:04:50 +00:00
# ifdef ENABLE_NETWORK
2007-04-04 04:08:47 +00:00
/* hotfix for desync problem:
* for MP games invalidate the YAPF cache every tick to keep it exactly the same on the server and all clients */
2006-07-11 19:04:50 +00:00
if ( _networking ) {
2007-01-10 18:56:51 +00:00
YapfNotifyTrackLayoutChange ( INVALID_TILE , INVALID_TRACK ) ;
2006-07-11 19:04:50 +00:00
}
# endif //ENABLE_NETWORK
2006-08-28 18:53:03 +00:00
_first_veh_in_depot_list = NULL ; // now we are sure it's initialized at the start of each tick
2005-10-31 12:59:47 +00:00
2004-08-09 17:04:08 +00:00
FOR_ALL_VEHICLES ( v ) {
2007-02-07 19:10:19 +00:00
_vehicle_tick_procs [ v - > type ] ( v ) ;
2006-09-27 18:17:01 +00:00
switch ( v - > type ) {
2007-03-08 16:27:54 +00:00
case VEH_TRAIN :
case VEH_ROAD :
case VEH_AIRCRAFT :
case VEH_SHIP :
if ( v - > type = = VEH_TRAIN & & IsTrainWagon ( v ) ) continue ;
if ( v - > type = = VEH_AIRCRAFT & & v - > subtype ! = AIR_HELICOPTER ) continue ;
2006-09-27 18:17:01 +00:00
v - > motion_counter + = ( v - > direction & 1 ) ? ( v - > cur_speed * 3 ) / 4 : v - > cur_speed ;
/* Play a running sound if the motion counter passes 256 (Do we not skip sounds?) */
if ( GB ( v - > motion_counter , 0 , 8 ) < v - > cur_speed ) PlayVehicleSound ( v , VSE_RUNNING ) ;
/* Play an alterate running sound every 16 ticks */
if ( GB ( v - > tick_counter , 0 , 4 ) = = 0 ) PlayVehicleSound ( v , v - > cur_speed > 0 ? VSE_RUNNING_16 : VSE_STOPPED_16 ) ;
}
2005-10-31 12:59:47 +00:00
}
2007-04-04 04:08:47 +00:00
/* now we handle all the vehicles that entered a depot this tick */
2005-10-31 12:59:47 +00:00
v = _first_veh_in_depot_list ;
while ( v ! = NULL ) {
Vehicle * w = v - > depot_list ;
2006-08-28 18:53:03 +00:00
v - > depot_list = NULL ; // it should always be NULL at the end of each tick
2006-09-29 18:39:20 +00:00
MaybeReplaceVehicle ( v , false , true ) ;
2005-10-31 12:59:47 +00:00
v = w ;
2004-08-09 17:04:08 +00:00
}
}
static bool CanFillVehicle_FullLoadAny ( Vehicle * v )
{
uint32 full = 0 , not_full = 0 ;
2006-12-21 20:56:27 +00:00
bool keep_loading = false ;
const GoodsEntry * ge = GetStation ( v - > last_station_visited ) - > goods ;
2004-09-10 19:02:27 +00:00
2007-04-04 04:08:47 +00:00
/* special handling of aircraft */
2005-01-08 12:47:26 +00:00
2007-04-04 04:08:47 +00:00
/* if the aircraft carries passengers and is NOT full, then
* continue loading , no matter how much mail is in */
2007-03-08 16:27:54 +00:00
if ( v - > type = = VEH_AIRCRAFT & &
2007-03-18 22:32:05 +00:00
IsCargoInClass ( v - > cargo_type , CC_PASSENGERS ) & &
2006-06-27 21:25:53 +00:00
v - > cargo_cap ! = v - > cargo_count ) {
2005-01-07 09:51:16 +00:00
return true ;
}
2007-04-04 04:08:47 +00:00
/* patch should return "true" to continue loading, i.e. when there is no cargo type that is fully loaded. */
2004-08-09 17:04:08 +00:00
do {
2007-04-04 04:08:47 +00:00
/* Should never happen, but just in case future additions change this */
2004-08-09 17:04:08 +00:00
assert ( v - > cargo_type < 32 ) ;
if ( v - > cargo_cap ! = 0 ) {
uint32 mask = 1 < < v - > cargo_type ;
2006-06-27 21:25:53 +00:00
if ( v - > cargo_cap = = v - > cargo_count ) {
full | = mask ;
2006-12-21 20:56:27 +00:00
} else if ( GB ( ge [ v - > cargo_type ] . waiting_acceptance , 0 , 12 ) > 0 | |
2007-02-28 17:18:36 +00:00
( HASBIT ( v - > vehicle_flags , VF_CARGO_UNLOADING ) & & ( ge [ v - > cargo_type ] . waiting_acceptance & 0x8000 ) ) ) {
2006-12-21 20:56:27 +00:00
/* If there is any cargo waiting, or this vehicle is still unloading
* and the station accepts the cargo , don ' t leave the station . */
keep_loading = true ;
2006-06-27 21:25:53 +00:00
} else {
not_full | = mask ;
}
2004-08-09 17:04:08 +00:00
}
2006-06-27 21:25:53 +00:00
} while ( ( v = v - > next ) ! = NULL ) ;
2004-08-09 17:04:08 +00:00
2007-04-04 04:08:47 +00:00
/* continue loading if there is a non full cargo type and no cargo type that is full */
2006-12-21 20:56:27 +00:00
return keep_loading | | ( not_full & & ( full & ~ not_full ) = = 0 ) ;
2004-08-09 17:04:08 +00:00
}
bool CanFillVehicle ( Vehicle * v )
{
2005-01-18 17:19:34 +00:00
TileIndex tile = v - > tile ;
if ( IsTileType ( tile , MP_STATION ) | |
2007-03-08 16:27:54 +00:00
( v - > type = = VEH_SHIP & & (
2005-01-18 17:19:34 +00:00
IsTileType ( TILE_ADDXY ( tile , 1 , 0 ) , MP_STATION ) | |
IsTileType ( TILE_ADDXY ( tile , - 1 , 0 ) , MP_STATION ) | |
IsTileType ( TILE_ADDXY ( tile , 0 , 1 ) , MP_STATION ) | |
IsTileType ( TILE_ADDXY ( tile , 0 , - 1 ) , MP_STATION ) | |
IsTileType ( TILE_ADDXY ( tile , - 2 , 0 ) , MP_STATION )
) ) ) {
2004-08-09 17:04:08 +00:00
2007-04-04 04:08:47 +00:00
/* If patch is active, use alternative CanFillVehicle-function */
2006-12-02 16:56:32 +00:00
if ( _patches . full_load_any & & v - > current_order . flags & OF_FULL_LOAD ) return CanFillVehicle_FullLoadAny ( v ) ;
2004-08-09 17:04:08 +00:00
do {
2006-06-27 21:25:53 +00:00
if ( v - > cargo_count ! = v - > cargo_cap ) return true ;
} while ( ( v = v - > next ) ! = NULL ) ;
2004-08-09 17:04:08 +00:00
}
return false ;
}
2005-11-29 22:29:59 +00:00
/** Check if a given engine type can be refitted to a given cargo
* @ param engine_type Engine type to check
2005-05-14 12:36:16 +00:00
* @ param cid_to check refit to this cargo - type
* @ return true if it is possible , false otherwise
*/
2005-11-29 22:29:59 +00:00
bool CanRefitTo ( EngineID engine_type , CargoID cid_to )
2005-05-14 12:36:16 +00:00
{
2007-02-24 23:36:40 +00:00
return HASBIT ( EngInfo ( engine_type ) - > refit_mask , cid_to ) ;
2005-05-14 12:36:16 +00:00
}
2006-06-07 07:20:28 +00:00
/** Find the first cargo type that an engine can be refitted to.
2007-04-18 00:41:09 +00:00
* @ param engine_type Which engine to find cargo for .
2006-06-07 07:20:28 +00:00
* @ return A climate dependent cargo type . CT_INVALID is returned if not refittable .
*/
CargoID FindFirstRefittableCargo ( EngineID engine_type )
{
uint32 refit_mask = EngInfo ( engine_type ) - > refit_mask ;
if ( refit_mask ! = 0 ) {
2007-03-21 13:19:01 +00:00
for ( CargoID cid = 0 ; cid < NUM_CARGO ; cid + + ) {
2007-02-24 23:36:40 +00:00
if ( HASBIT ( refit_mask , cid ) ) return cid ;
2006-06-07 07:20:28 +00:00
}
}
return CT_INVALID ;
}
2006-09-04 09:07:52 +00:00
/** Learn the price of refitting a certain engine
2007-04-18 00:41:09 +00:00
* @ param engine_type Which engine to refit
2006-09-04 09:07:52 +00:00
* @ return Price for refitting
*/
int32 GetRefitCost ( EngineID engine_type )
{
2006-09-05 13:39:38 +00:00
int32 base_cost = 0 ;
2006-09-04 09:07:52 +00:00
switch ( GetEngine ( engine_type ) - > type ) {
2007-03-08 16:27:54 +00:00
case VEH_SHIP : base_cost = _price . ship_base ; break ;
case VEH_ROAD : base_cost = _price . roadveh_base ; break ;
case VEH_AIRCRAFT : base_cost = _price . aircraft_base ; break ;
case VEH_TRAIN :
2007-01-30 11:53:35 +00:00
base_cost = 2 * ( ( RailVehInfo ( engine_type ) - > railveh_type = = RAILVEH_WAGON ) ?
2006-09-04 09:07:52 +00:00
_price . build_railwagon : _price . build_railvehicle ) ;
break ;
default : NOT_REACHED ( ) ; break ;
}
return ( EngInfo ( engine_type ) - > refit_cost * base_cost ) > > 10 ;
}
2006-06-07 07:20:28 +00:00
2005-09-18 20:56:44 +00:00
static void DoDrawVehicle ( const Vehicle * v )
2004-08-09 17:04:08 +00:00
{
2007-01-14 19:57:49 +00:00
SpriteID image = v - > cur_image ;
SpriteID pal ;
2004-09-10 19:02:27 +00:00
2006-07-26 08:32:20 +00:00
if ( v - > vehstatus & VS_SHADOW ) {
2007-01-14 19:57:49 +00:00
SETBIT ( image , PALETTE_MODIFIER_TRANSPARENT ) ;
pal = PALETTE_TO_TRANSPARENT ;
2004-08-09 17:04:08 +00:00
} else if ( v - > vehstatus & VS_DEFPAL ) {
2007-01-14 19:57:49 +00:00
pal = ( v - > vehstatus & VS_CRASHED ) ? PALETTE_CRASH : GetVehiclePalette ( v ) ;
} else {
pal = PAL_NONE ;
2004-09-10 19:02:27 +00:00
}
2004-08-09 17:04:08 +00:00
2007-01-14 19:57:49 +00:00
AddSortableSpriteToDraw ( image , pal , v - > x_pos + v - > x_offs , v - > y_pos + v - > y_offs ,
2004-08-09 17:04:08 +00:00
v - > sprite_width , v - > sprite_height , v - > z_height , v - > z_pos ) ;
}
void ViewportAddVehicles ( DrawPixelInfo * dpi )
{
2007-04-04 04:08:47 +00:00
/* The bounding rectangle */
2006-07-26 08:27:05 +00:00
const int l = dpi - > left ;
const int r = dpi - > left + dpi - > width ;
const int t = dpi - > top ;
const int b = dpi - > top + dpi - > height ;
2004-08-09 17:04:08 +00:00
2007-04-04 04:08:47 +00:00
/* The hash area to scan */
2006-07-26 08:27:05 +00:00
const int xl = GB ( l - 70 , 7 , 6 ) ;
const int xu = GB ( r , 7 , 6 ) ;
const int yl = GB ( t - 70 , 6 , 6 ) < < 6 ;
const int yu = GB ( b , 6 , 6 ) < < 6 ;
2004-08-09 17:04:08 +00:00
2006-07-26 08:27:05 +00:00
int x ;
int y ;
for ( y = yl ; ; y = ( y + ( 1 < < 6 ) ) & ( 0x3F < < 6 ) ) {
for ( x = xl ; ; x = ( x + 1 ) & 0x3F ) {
2007-01-09 16:27:25 +00:00
const Vehicle * v = _vehicle_position_hash [ ( x + y ) & 0xFFFF ] ;
2004-09-10 19:02:27 +00:00
2007-01-09 16:27:25 +00:00
while ( v ! = NULL ) {
2004-09-10 19:02:27 +00:00
if ( ! ( v - > vehstatus & VS_HIDDEN ) & &
2006-07-26 08:27:05 +00:00
l < = v - > right_coord & &
t < = v - > bottom_coord & &
r > = v - > left_coord & &
b > = v - > top_coord ) {
2004-08-09 17:04:08 +00:00
DoDrawVehicle ( v ) ;
}
2007-01-09 16:27:25 +00:00
v = v - > next_hash ;
2004-08-09 17:04:08 +00:00
}
2006-07-26 08:27:05 +00:00
if ( x = = xu ) break ;
2004-08-09 17:04:08 +00:00
}
2006-07-26 08:27:05 +00:00
if ( y = = yu ) break ;
2004-08-09 17:04:08 +00:00
}
}
2005-02-14 20:34:31 +00:00
static void ChimneySmokeInit ( Vehicle * v )
2004-08-09 17:04:08 +00:00
{
uint32 r = Random ( ) ;
2005-07-20 15:29:28 +00:00
v - > cur_image = SPR_CHIMNEY_SMOKE_0 + GB ( r , 0 , 3 ) ;
v - > progress = GB ( r , 16 , 3 ) ;
2004-08-09 17:04:08 +00:00
}
2005-02-14 20:34:31 +00:00
static void ChimneySmokeTick ( Vehicle * v )
2004-08-09 17:04:08 +00:00
{
2005-02-14 20:34:31 +00:00
if ( v - > progress > 0 ) {
v - > progress - - ;
} else {
TileIndex tile ;
2004-08-09 17:04:08 +00:00
BeginVehicleMove ( v ) ;
2004-09-10 19:02:27 +00:00
2005-06-25 06:15:43 +00:00
tile = TileVirtXY ( v - > x_pos , v - > y_pos ) ;
2005-01-16 11:24:58 +00:00
if ( ! IsTileType ( tile , MP_INDUSTRY ) ) {
2004-08-09 17:04:08 +00:00
EndVehicleMove ( v ) ;
DeleteVehicle ( v ) ;
return ;
}
2005-02-14 20:34:31 +00:00
if ( v - > cur_image ! = SPR_CHIMNEY_SMOKE_7 ) {
v - > cur_image + + ;
} else {
v - > cur_image = SPR_CHIMNEY_SMOKE_0 ;
}
2004-08-09 17:04:08 +00:00
v - > progress = 7 ;
VehiclePositionChanged ( v ) ;
EndVehicleMove ( v ) ;
}
}
2005-02-14 20:34:31 +00:00
static void SteamSmokeInit ( Vehicle * v )
2004-08-09 17:04:08 +00:00
{
2005-02-14 20:34:31 +00:00
v - > cur_image = SPR_STEAM_SMOKE_0 ;
2004-08-09 17:04:08 +00:00
v - > progress = 12 ;
}
2005-02-14 20:34:31 +00:00
static void SteamSmokeTick ( Vehicle * v )
2004-08-09 17:04:08 +00:00
{
2005-02-14 20:34:31 +00:00
bool moved = false ;
2004-09-10 19:02:27 +00:00
2004-08-09 17:04:08 +00:00
BeginVehicleMove ( v ) ;
2004-09-10 19:02:27 +00:00
2005-02-14 20:34:31 +00:00
v - > progress + + ;
2004-09-10 19:02:27 +00:00
2005-02-14 20:34:31 +00:00
if ( ( v - > progress & 7 ) = = 0 ) {
2004-08-09 17:04:08 +00:00
v - > z_pos + + ;
moved = true ;
}
2005-02-14 20:34:31 +00:00
if ( ( v - > progress & 0xF ) = = 4 ) {
if ( v - > cur_image ! = SPR_STEAM_SMOKE_4 ) {
v - > cur_image + + ;
} else {
2004-08-09 17:04:08 +00:00
EndVehicleMove ( v ) ;
DeleteVehicle ( v ) ;
return ;
}
moved = true ;
}
if ( moved ) {
VehiclePositionChanged ( v ) ;
EndVehicleMove ( v ) ;
}
}
2005-02-14 20:34:31 +00:00
static void DieselSmokeInit ( Vehicle * v )
2004-08-09 17:04:08 +00:00
{
2005-02-14 20:34:31 +00:00
v - > cur_image = SPR_DIESEL_SMOKE_0 ;
2004-08-09 17:04:08 +00:00
v - > progress = 0 ;
}
2005-02-14 20:34:31 +00:00
static void DieselSmokeTick ( Vehicle * v )
2004-08-09 17:04:08 +00:00
{
2005-02-14 20:34:31 +00:00
v - > progress + + ;
if ( ( v - > progress & 3 ) = = 0 ) {
2004-08-09 17:04:08 +00:00
BeginVehicleMove ( v ) ;
v - > z_pos + + ;
VehiclePositionChanged ( v ) ;
EndVehicleMove ( v ) ;
} else if ( ( v - > progress & 7 ) = = 1 ) {
BeginVehicleMove ( v ) ;
2005-02-14 20:34:31 +00:00
if ( v - > cur_image ! = SPR_DIESEL_SMOKE_5 ) {
v - > cur_image + + ;
VehiclePositionChanged ( v ) ;
2004-08-09 17:04:08 +00:00
EndVehicleMove ( v ) ;
} else {
EndVehicleMove ( v ) ;
2005-02-14 20:34:31 +00:00
DeleteVehicle ( v ) ;
2004-08-09 17:04:08 +00:00
}
}
}
2005-02-14 20:34:31 +00:00
static void ElectricSparkInit ( Vehicle * v )
2004-08-09 17:04:08 +00:00
{
2005-02-14 20:34:31 +00:00
v - > cur_image = SPR_ELECTRIC_SPARK_0 ;
2004-08-09 17:04:08 +00:00
v - > progress = 1 ;
}
2005-02-14 20:34:31 +00:00
static void ElectricSparkTick ( Vehicle * v )
2004-08-09 17:04:08 +00:00
{
2005-02-14 20:34:31 +00:00
if ( v - > progress < 2 ) {
v - > progress + + ;
} else {
2004-08-09 17:04:08 +00:00
v - > progress = 0 ;
BeginVehicleMove ( v ) ;
2005-02-14 20:34:31 +00:00
if ( v - > cur_image ! = SPR_ELECTRIC_SPARK_5 ) {
v - > cur_image + + ;
VehiclePositionChanged ( v ) ;
2004-08-09 17:04:08 +00:00
EndVehicleMove ( v ) ;
} else {
EndVehicleMove ( v ) ;
2005-02-14 20:34:31 +00:00
DeleteVehicle ( v ) ;
2004-08-09 17:04:08 +00:00
}
}
}
2005-02-14 20:34:31 +00:00
static void SmokeInit ( Vehicle * v )
2004-08-09 17:04:08 +00:00
{
2005-02-14 20:34:31 +00:00
v - > cur_image = SPR_SMOKE_0 ;
2004-08-09 17:04:08 +00:00
v - > progress = 12 ;
}
2005-02-14 20:34:31 +00:00
static void SmokeTick ( Vehicle * v )
2004-08-09 17:04:08 +00:00
{
2005-02-14 20:34:31 +00:00
bool moved = false ;
2004-09-10 19:02:27 +00:00
2004-08-09 17:04:08 +00:00
BeginVehicleMove ( v ) ;
2004-09-10 19:02:27 +00:00
2005-02-14 20:34:31 +00:00
v - > progress + + ;
2004-09-10 19:02:27 +00:00
2005-02-14 20:34:31 +00:00
if ( ( v - > progress & 3 ) = = 0 ) {
2004-08-09 17:04:08 +00:00
v - > z_pos + + ;
moved = true ;
}
2005-02-14 20:34:31 +00:00
if ( ( v - > progress & 0xF ) = = 4 ) {
if ( v - > cur_image ! = SPR_SMOKE_4 ) {
v - > cur_image + + ;
} else {
2004-08-09 17:04:08 +00:00
EndVehicleMove ( v ) ;
DeleteVehicle ( v ) ;
return ;
}
moved = true ;
}
if ( moved ) {
VehiclePositionChanged ( v ) ;
EndVehicleMove ( v ) ;
}
}
2005-02-14 20:34:31 +00:00
static void ExplosionLargeInit ( Vehicle * v )
2004-08-09 17:04:08 +00:00
{
2005-02-14 20:34:31 +00:00
v - > cur_image = SPR_EXPLOSION_LARGE_0 ;
2004-08-09 17:04:08 +00:00
v - > progress = 0 ;
}
2005-02-14 20:34:31 +00:00
static void ExplosionLargeTick ( Vehicle * v )
2004-08-09 17:04:08 +00:00
{
2005-02-14 20:34:31 +00:00
v - > progress + + ;
if ( ( v - > progress & 3 ) = = 0 ) {
2004-08-09 17:04:08 +00:00
BeginVehicleMove ( v ) ;
2005-02-14 20:34:31 +00:00
if ( v - > cur_image ! = SPR_EXPLOSION_LARGE_F ) {
v - > cur_image + + ;
VehiclePositionChanged ( v ) ;
2004-08-09 17:04:08 +00:00
EndVehicleMove ( v ) ;
} else {
EndVehicleMove ( v ) ;
2005-02-14 20:34:31 +00:00
DeleteVehicle ( v ) ;
2004-08-09 17:04:08 +00:00
}
}
}
2005-02-14 20:34:31 +00:00
static void BreakdownSmokeInit ( Vehicle * v )
2004-08-09 17:04:08 +00:00
{
2005-02-14 20:34:31 +00:00
v - > cur_image = SPR_BREAKDOWN_SMOKE_0 ;
2004-08-09 17:04:08 +00:00
v - > progress = 0 ;
}
2005-02-14 20:34:31 +00:00
static void BreakdownSmokeTick ( Vehicle * v )
2004-08-09 17:04:08 +00:00
{
2005-02-14 20:34:31 +00:00
v - > progress + + ;
if ( ( v - > progress & 7 ) = = 0 ) {
2004-08-09 17:04:08 +00:00
BeginVehicleMove ( v ) ;
2005-02-14 20:34:31 +00:00
if ( v - > cur_image ! = SPR_BREAKDOWN_SMOKE_3 ) {
v - > cur_image + + ;
} else {
v - > cur_image = SPR_BREAKDOWN_SMOKE_0 ;
}
2004-08-09 17:04:08 +00:00
VehiclePositionChanged ( v ) ;
EndVehicleMove ( v ) ;
}
2005-02-14 20:34:31 +00:00
v - > u . special . unk0 - - ;
if ( v - > u . special . unk0 = = 0 ) {
2004-08-09 17:04:08 +00:00
BeginVehicleMove ( v ) ;
EndVehicleMove ( v ) ;
DeleteVehicle ( v ) ;
}
}
2005-02-14 20:34:31 +00:00
static void ExplosionSmallInit ( Vehicle * v )
2004-08-09 17:04:08 +00:00
{
2005-02-14 20:34:31 +00:00
v - > cur_image = SPR_EXPLOSION_SMALL_0 ;
2004-08-09 17:04:08 +00:00
v - > progress = 0 ;
}
2005-02-14 20:34:31 +00:00
static void ExplosionSmallTick ( Vehicle * v )
2004-08-09 17:04:08 +00:00
{
2005-02-14 20:34:31 +00:00
v - > progress + + ;
if ( ( v - > progress & 3 ) = = 0 ) {
2004-08-09 17:04:08 +00:00
BeginVehicleMove ( v ) ;
2005-02-14 20:34:31 +00:00
if ( v - > cur_image ! = SPR_EXPLOSION_SMALL_B ) {
v - > cur_image + + ;
VehiclePositionChanged ( v ) ;
2004-08-09 17:04:08 +00:00
EndVehicleMove ( v ) ;
} else {
EndVehicleMove ( v ) ;
2005-02-14 20:34:31 +00:00
DeleteVehicle ( v ) ;
2004-08-09 17:04:08 +00:00
}
}
}
2005-02-13 11:27:41 +00:00
static void BulldozerInit ( Vehicle * v )
2004-08-09 17:04:08 +00:00
{
2005-02-13 11:27:41 +00:00
v - > cur_image = SPR_BULLDOZER_NE ;
2004-08-09 17:04:08 +00:00
v - > progress = 0 ;
v - > u . special . unk0 = 0 ;
v - > u . special . unk2 = 0 ;
}
2007-03-07 12:11:48 +00:00
struct BulldozerMovement {
2005-02-13 11:27:41 +00:00
byte direction : 2 ;
2005-02-13 11:31:35 +00:00
byte image : 2 ;
2005-02-13 11:27:41 +00:00
byte duration : 3 ;
2007-03-07 12:11:48 +00:00
} ;
2005-02-13 11:27:41 +00:00
static const BulldozerMovement _bulldozer_movement [ ] = {
{ 0 , 0 , 4 } ,
{ 3 , 3 , 4 } ,
{ 2 , 2 , 7 } ,
{ 0 , 2 , 7 } ,
{ 1 , 1 , 3 } ,
{ 2 , 2 , 7 } ,
{ 0 , 2 , 7 } ,
{ 1 , 1 , 3 } ,
{ 2 , 2 , 7 } ,
{ 0 , 2 , 7 } ,
{ 3 , 3 , 6 } ,
{ 2 , 2 , 6 } ,
{ 1 , 1 , 7 } ,
{ 3 , 1 , 7 } ,
{ 0 , 0 , 3 } ,
{ 1 , 1 , 7 } ,
{ 3 , 1 , 7 } ,
{ 0 , 0 , 3 } ,
{ 1 , 1 , 7 } ,
{ 3 , 1 , 7 }
2004-08-09 17:04:08 +00:00
} ;
2005-02-13 11:27:41 +00:00
static const struct {
int8 x ;
int8 y ;
} _inc_by_dir [ ] = {
{ - 1 , 0 } ,
{ 0 , 1 } ,
{ 1 , 0 } ,
{ 0 , - 1 }
2004-08-09 17:04:08 +00:00
} ;
2005-02-13 11:27:41 +00:00
static void BulldozerTick ( Vehicle * v )
2004-08-09 17:04:08 +00:00
{
2005-02-14 20:34:31 +00:00
v - > progress + + ;
if ( ( v - > progress & 7 ) = = 0 ) {
2005-02-13 11:27:41 +00:00
const BulldozerMovement * b = & _bulldozer_movement [ v - > u . special . unk0 ] ;
2004-08-09 17:04:08 +00:00
BeginVehicleMove ( v ) ;
2005-02-13 11:27:41 +00:00
v - > cur_image = SPR_BULLDOZER_NE + b - > image ;
2004-08-09 17:04:08 +00:00
2005-02-13 11:27:41 +00:00
v - > x_pos + = _inc_by_dir [ b - > direction ] . x ;
v - > y_pos + = _inc_by_dir [ b - > direction ] . y ;
2004-08-09 17:04:08 +00:00
2005-02-13 11:27:41 +00:00
v - > u . special . unk2 + + ;
2005-02-13 11:31:35 +00:00
if ( v - > u . special . unk2 > = b - > duration ) {
2004-08-09 17:04:08 +00:00
v - > u . special . unk2 = 0 ;
v - > u . special . unk0 + + ;
2005-02-13 11:27:41 +00:00
if ( v - > u . special . unk0 = = lengthof ( _bulldozer_movement ) ) {
2004-08-09 17:04:08 +00:00
EndVehicleMove ( v ) ;
DeleteVehicle ( v ) ;
return ;
}
}
VehiclePositionChanged ( v ) ;
EndVehicleMove ( v ) ;
}
}
2005-02-14 20:34:31 +00:00
static void BubbleInit ( Vehicle * v )
2004-08-09 17:04:08 +00:00
{
2005-02-14 20:34:31 +00:00
v - > cur_image = SPR_BUBBLE_GENERATE_0 ;
2004-08-09 17:04:08 +00:00
v - > spritenum = 0 ;
v - > progress = 0 ;
}
2007-03-07 12:11:48 +00:00
struct BubbleMovement {
2005-02-14 20:34:31 +00:00
int8 x : 4 ;
int8 y : 4 ;
int8 z : 4 ;
byte image : 4 ;
2007-03-07 12:11:48 +00:00
} ;
2004-08-09 17:04:08 +00:00
2005-02-14 20:34:31 +00:00
# define MK(x, y, z, i) { x, y, z, i }
# define ME(i) { i, 4, 0, 0 }
static const BubbleMovement _bubble_float_sw [ ] = {
2006-08-22 14:38:37 +00:00
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 1 , 0 , 1 , 1 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 1 , 0 , 1 , 2 ) ,
2005-02-14 20:34:31 +00:00
ME ( 1 )
2004-08-09 17:04:08 +00:00
} ;
2005-02-14 20:34:31 +00:00
static const BubbleMovement _bubble_float_ne [ ] = {
2006-08-22 14:38:37 +00:00
MK ( 0 , 0 , 1 , 0 ) ,
MK ( - 1 , 0 , 1 , 1 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( - 1 , 0 , 1 , 2 ) ,
2005-02-14 20:34:31 +00:00
ME ( 1 )
2004-08-09 17:04:08 +00:00
} ;
2005-02-14 20:34:31 +00:00
static const BubbleMovement _bubble_float_se [ ] = {
2006-08-22 14:38:37 +00:00
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 1 , 1 , 1 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 1 , 1 , 2 ) ,
2005-02-14 20:34:31 +00:00
ME ( 1 )
2004-08-09 17:04:08 +00:00
} ;
2005-02-14 20:34:31 +00:00
static const BubbleMovement _bubble_float_nw [ ] = {
2006-08-22 14:38:37 +00:00
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , - 1 , 1 , 1 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , - 1 , 1 , 2 ) ,
2005-02-14 20:34:31 +00:00
ME ( 1 )
2004-08-09 17:04:08 +00:00
} ;
2005-02-14 20:34:31 +00:00
static const BubbleMovement _bubble_burst [ ] = {
2006-08-22 14:38:37 +00:00
MK ( 0 , 0 , 1 , 2 ) ,
MK ( 0 , 0 , 1 , 7 ) ,
MK ( 0 , 0 , 1 , 8 ) ,
MK ( 0 , 0 , 1 , 9 ) ,
2005-02-14 20:34:31 +00:00
ME ( 0 )
2004-08-09 17:04:08 +00:00
} ;
2005-02-14 20:34:31 +00:00
static const BubbleMovement _bubble_absorb [ ] = {
2006-08-22 14:38:37 +00:00
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 1 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 2 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 1 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 2 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 1 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 2 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 1 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 2 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 1 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 2 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 1 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 2 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 1 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 2 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 1 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 2 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 1 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 2 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 1 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 2 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 1 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 2 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 1 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 2 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 1 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 2 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 1 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 2 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 1 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 2 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 0 , 0 , 1 , 1 ) ,
MK ( 2 , 1 , 3 , 0 ) ,
MK ( 1 , 1 , 3 , 1 ) ,
MK ( 2 , 1 , 3 , 0 ) ,
MK ( 1 , 1 , 3 , 2 ) ,
MK ( 2 , 1 , 3 , 0 ) ,
MK ( 1 , 1 , 3 , 1 ) ,
MK ( 2 , 1 , 3 , 0 ) ,
MK ( 1 , 0 , 1 , 2 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 1 , 0 , 1 , 1 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 1 , 0 , 1 , 2 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 1 , 0 , 1 , 1 ) ,
MK ( 0 , 0 , 1 , 0 ) ,
MK ( 1 , 0 , 1 , 2 ) ,
2005-02-14 20:34:31 +00:00
ME ( 2 ) ,
2006-08-22 14:38:37 +00:00
MK ( 0 , 0 , 0 , 0xA ) ,
MK ( 0 , 0 , 0 , 0xB ) ,
MK ( 0 , 0 , 0 , 0xC ) ,
MK ( 0 , 0 , 0 , 0xD ) ,
MK ( 0 , 0 , 0 , 0xE ) ,
2005-02-14 20:34:31 +00:00
ME ( 0 )
2004-08-09 17:04:08 +00:00
} ;
2005-02-14 20:34:31 +00:00
# undef ME
2004-08-09 17:04:08 +00:00
# undef MK
2005-02-14 20:34:31 +00:00
static const BubbleMovement * const _bubble_movement [ ] = {
_bubble_float_sw ,
_bubble_float_ne ,
_bubble_float_se ,
_bubble_float_nw ,
_bubble_burst ,
_bubble_absorb ,
2004-08-09 17:04:08 +00:00
} ;
2005-02-14 20:34:31 +00:00
static void BubbleTick ( Vehicle * v )
2004-08-09 17:04:08 +00:00
{
2004-12-04 17:54:56 +00:00
/*
* Warning : those effects can NOT use Random ( ) , and have to use
* InteractiveRandom ( ) , because somehow someone forgot to save
* spritenum to the savegame , and so it will cause desyncs in
* multiplayer ! ! ( that is : in ToyLand )
*/
2005-02-14 20:34:31 +00:00
uint et ;
const BubbleMovement * b ;
2004-08-09 17:04:08 +00:00
2005-02-14 20:34:31 +00:00
v - > progress + + ;
if ( ( v - > progress & 3 ) ! = 0 )
2004-08-09 17:04:08 +00:00
return ;
BeginVehicleMove ( v ) ;
if ( v - > spritenum = = 0 ) {
2005-02-14 20:34:31 +00:00
v - > cur_image + + ;
if ( v - > cur_image < SPR_BUBBLE_GENERATE_3 ) {
2004-08-09 17:04:08 +00:00
VehiclePositionChanged ( v ) ;
EndVehicleMove ( v ) ;
return ;
}
if ( v - > u . special . unk2 ! = 0 ) {
2005-11-14 08:09:57 +00:00
v - > spritenum = GB ( InteractiveRandom ( ) , 0 , 2 ) + 1 ;
2004-08-09 17:04:08 +00:00
} else {
v - > spritenum = 6 ;
}
et = 0 ;
2005-02-14 20:34:31 +00:00
} else {
et = v - > engine_type + 1 ;
2004-08-09 17:04:08 +00:00
}
2005-02-14 20:34:31 +00:00
b = & _bubble_movement [ v - > spritenum - 1 ] [ et ] ;
2004-08-09 17:04:08 +00:00
2005-02-14 20:34:31 +00:00
if ( b - > y = = 4 & & b - > x = = 0 ) {
2004-08-09 17:04:08 +00:00
EndVehicleMove ( v ) ;
DeleteVehicle ( v ) ;
return ;
2004-09-10 19:02:27 +00:00
}
2005-02-14 20:34:31 +00:00
if ( b - > y = = 4 & & b - > x = = 1 ) {
if ( v - > z_pos > 180 | | CHANCE16I ( 1 , 96 , InteractiveRandom ( ) ) ) {
2004-08-09 17:04:08 +00:00
v - > spritenum = 5 ;
2004-12-04 09:26:39 +00:00
SndPlayVehicleFx ( SND_2F_POP , v ) ;
2004-08-09 17:04:08 +00:00
}
et = 0 ;
2004-09-10 19:02:27 +00:00
}
2005-02-14 20:34:31 +00:00
if ( b - > y = = 4 & & b - > x = = 2 ) {
TileIndex tile ;
2004-08-09 17:04:08 +00:00
et + + ;
2004-12-04 09:26:39 +00:00
SndPlayVehicleFx ( SND_31_EXTRACT , v ) ;
2004-08-09 17:04:08 +00:00
2005-06-25 06:15:43 +00:00
tile = TileVirtXY ( v - > x_pos , v - > y_pos ) ;
2006-04-03 12:41:31 +00:00
if ( IsTileType ( tile , MP_INDUSTRY ) & & GetIndustryGfx ( tile ) = = 0xA2 ) AddAnimatedTile ( tile ) ;
2004-08-09 17:04:08 +00:00
}
2005-02-14 20:34:31 +00:00
v - > engine_type = et ;
b = & _bubble_movement [ v - > spritenum - 1 ] [ et ] ;
v - > x_pos + = b - > x ;
v - > y_pos + = b - > y ;
v - > z_pos + = b - > z ;
v - > cur_image = SPR_BUBBLE_0 + b - > image ;
2004-08-09 17:04:08 +00:00
VehiclePositionChanged ( v ) ;
EndVehicleMove ( v ) ;
}
typedef void EffectInitProc ( Vehicle * v ) ;
typedef void EffectTickProc ( Vehicle * v ) ;
static EffectInitProc * const _effect_init_procs [ ] = {
2005-02-14 20:34:31 +00:00
ChimneySmokeInit ,
SteamSmokeInit ,
DieselSmokeInit ,
ElectricSparkInit ,
SmokeInit ,
ExplosionLargeInit ,
BreakdownSmokeInit ,
ExplosionSmallInit ,
2005-02-13 11:27:41 +00:00
BulldozerInit ,
2005-02-14 20:34:31 +00:00
BubbleInit ,
2004-08-09 17:04:08 +00:00
} ;
static EffectTickProc * const _effect_tick_procs [ ] = {
2005-02-14 20:34:31 +00:00
ChimneySmokeTick ,
SteamSmokeTick ,
DieselSmokeTick ,
ElectricSparkTick ,
SmokeTick ,
ExplosionLargeTick ,
BreakdownSmokeTick ,
ExplosionSmallTick ,
2005-02-13 11:27:41 +00:00
BulldozerTick ,
2005-02-14 20:34:31 +00:00
BubbleTick ,
2004-08-09 17:04:08 +00:00
} ;
2005-02-12 15:53:32 +00:00
Vehicle * CreateEffectVehicle ( int x , int y , int z , EffectVehicle type )
2004-08-09 17:04:08 +00:00
{
Vehicle * v ;
2004-09-10 19:02:27 +00:00
2004-08-09 17:04:08 +00:00
v = ForceAllocateSpecialVehicle ( ) ;
if ( v ! = NULL ) {
2007-03-08 16:27:54 +00:00
v - > type = VEH_SPECIAL ;
2004-08-09 17:04:08 +00:00
v - > subtype = type ;
v - > x_pos = x ;
v - > y_pos = y ;
v - > z_pos = z ;
v - > z_height = v - > sprite_width = v - > sprite_height = 1 ;
v - > x_offs = v - > y_offs = 0 ;
v - > tile = 0 ;
v - > vehstatus = VS_UNCLICKABLE ;
_effect_init_procs [ type ] ( v ) ;
VehiclePositionChanged ( v ) ;
BeginVehicleMove ( v ) ;
EndVehicleMove ( v ) ;
}
return v ;
}
2005-02-12 15:53:32 +00:00
Vehicle * CreateEffectVehicleAbove ( int x , int y , int z , EffectVehicle type )
2004-08-09 17:04:08 +00:00
{
2006-12-09 08:37:15 +00:00
int safe_x = clamp ( x , 0 , MapMaxX ( ) * TILE_SIZE ) ;
int safe_y = clamp ( y , 0 , MapMaxY ( ) * TILE_SIZE ) ;
return CreateEffectVehicle ( x , y , GetSlopeZ ( safe_x , safe_y ) + z , type ) ;
2004-08-09 17:04:08 +00:00
}
2005-02-12 15:53:32 +00:00
Vehicle * CreateEffectVehicleRel ( const Vehicle * v , int x , int y , int z , EffectVehicle type )
2004-08-09 17:04:08 +00:00
{
return CreateEffectVehicle ( v - > x_pos + x , v - > y_pos + y , v - > z_pos + z , type ) ;
}
2004-11-14 19:44:06 +00:00
static void EffectVehicle_Tick ( Vehicle * v )
2004-08-09 17:04:08 +00:00
{
_effect_tick_procs [ v - > subtype ] ( v ) ;
}
2005-07-17 20:14:58 +00:00
Vehicle * CheckClickOnVehicle ( const ViewPort * vp , int x , int y )
2004-08-09 17:04:08 +00:00
{
Vehicle * found = NULL , * v ;
uint dist , best_dist = ( uint ) - 1 ;
if ( ( uint ) ( x - = vp - > left ) > = ( uint ) vp - > width | |
( uint ) ( y - = vp - > top ) > = ( uint ) vp - > height )
return NULL ;
x = ( x < < vp - > zoom ) + vp - > virtual_left ;
y = ( y < < vp - > zoom ) + vp - > virtual_top ;
FOR_ALL_VEHICLES ( v ) {
2006-08-22 15:33:35 +00:00
if ( ( v - > vehstatus & ( VS_HIDDEN | VS_UNCLICKABLE ) ) = = 0 & &
2004-08-09 17:04:08 +00:00
x > = v - > left_coord & & x < = v - > right_coord & &
y > = v - > top_coord & & y < = v - > bottom_coord ) {
2004-09-10 19:02:27 +00:00
2004-08-09 17:04:08 +00:00
dist = max (
myabs ( ( ( v - > left_coord + v - > right_coord ) > > 1 ) - x ) ,
myabs ( ( ( v - > top_coord + v - > bottom_coord ) > > 1 ) - y )
) ;
if ( dist < best_dist ) {
found = v ;
best_dist = dist ;
}
}
}
return found ;
}
void DecreaseVehicleValue ( Vehicle * v )
{
v - > value - = v - > value > > 8 ;
InvalidateWindow ( WC_VEHICLE_DETAILS , v - > index ) ;
}
static const byte _breakdown_chance [ 64 ] = {
2006-08-22 14:38:37 +00:00
3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 ,
4 , 4 , 5 , 5 , 6 , 6 , 7 , 7 ,
8 , 8 , 9 , 9 , 10 , 10 , 11 , 11 ,
12 , 13 , 13 , 13 , 13 , 14 , 15 , 16 ,
17 , 19 , 21 , 25 , 28 , 31 , 34 , 37 ,
40 , 44 , 48 , 52 , 56 , 60 , 64 , 68 ,
72 , 80 , 90 , 100 , 110 , 120 , 130 , 140 ,
2004-08-09 17:04:08 +00:00
150 , 170 , 190 , 210 , 230 , 250 , 250 , 250 ,
} ;
void CheckVehicleBreakdown ( Vehicle * v )
{
int rel , rel_old ;
uint32 r ;
int chance ;
/* decrease reliability */
v - > reliability = rel = max ( ( rel_old = v - > reliability ) - v - > reliability_spd_dec , 0 ) ;
if ( ( rel_old > > 8 ) ! = ( rel > > 8 ) )
InvalidateWindow ( WC_VEHICLE_DETAILS , v - > index ) ;
2005-11-14 19:48:04 +00:00
if ( v - > breakdown_ctr ! = 0 | | v - > vehstatus & VS_STOPPED | |
v - > cur_speed < 5 | | _game_mode = = GM_MENU ) {
return ;
}
2004-08-09 17:04:08 +00:00
r = Random ( ) ;
/* increase chance of failure */
chance = v - > breakdown_chance + 1 ;
if ( CHANCE16I ( 1 , 25 , r ) ) chance + = 25 ;
v - > breakdown_chance = min ( 255 , chance ) ;
/* calculate reliability value to use in comparison */
rel = v - > reliability ;
2007-03-08 16:27:54 +00:00
if ( v - > type = = VEH_SHIP ) rel + = 0x6666 ;
2004-09-10 19:02:27 +00:00
2004-08-09 17:04:08 +00:00
/* disabled breakdowns? */
2005-11-14 19:48:04 +00:00
if ( _opt . diff . vehicle_breakdowns < 1 ) return ;
2004-08-09 17:04:08 +00:00
/* reduced breakdowns? */
if ( _opt . diff . vehicle_breakdowns = = 1 ) rel + = 0x6666 ;
/* check if to break down */
if ( _breakdown_chance [ ( uint ) min ( rel , 0xffff ) > > 10 ] < = v - > breakdown_chance ) {
2005-07-20 15:29:28 +00:00
v - > breakdown_ctr = GB ( r , 16 , 6 ) + 0x3F ;
v - > breakdown_delay = GB ( r , 24 , 7 ) + 0x80 ;
2004-08-09 17:04:08 +00:00
v - > breakdown_chance = 0 ;
}
}
static const StringID _vehicle_type_names [ 4 ] = {
STR_019F_TRAIN ,
STR_019C_ROAD_VEHICLE ,
STR_019E_SHIP ,
STR_019D_AIRCRAFT ,
} ;
static void ShowVehicleGettingOld ( Vehicle * v , StringID msg )
{
2005-11-14 19:48:04 +00:00
if ( v - > owner ! = _local_player ) return ;
2004-12-27 18:18:44 +00:00
2007-04-04 04:08:47 +00:00
/* Do not show getting-old message if autorenew is active */
2005-11-14 19:48:04 +00:00
if ( GetPlayer ( v - > owner ) - > engine_renew ) return ;
2004-08-09 17:04:08 +00:00
2007-02-07 19:10:19 +00:00
SetDParam ( 0 , _vehicle_type_names [ v - > type ] ) ;
2004-12-02 22:53:07 +00:00
SetDParam ( 1 , v - > unitnumber ) ;
2004-08-09 17:04:08 +00:00
AddNewsItem ( msg , NEWS_FLAGS ( NM_SMALL , NF_VIEWPORT | NF_VEHICLE , NT_ADVICE , 0 ) , v - > index , 0 ) ;
}
void AgeVehicle ( Vehicle * v )
{
int age ;
if ( v - > age < 65535 )
v - > age + + ;
age = v - > age - v - > max_age ;
if ( age = = 366 * 0 | | age = = 366 * 1 | | age = = 366 * 2 | | age = = 366 * 3 | | age = = 366 * 4 )
2004-09-10 19:02:27 +00:00
v - > reliability_spd_dec < < = 1 ;
2004-08-09 17:04:08 +00:00
InvalidateWindow ( WC_VEHICLE_DETAILS , v - > index ) ;
if ( age = = - 366 ) {
ShowVehicleGettingOld ( v , STR_01A0_IS_GETTING_OLD ) ;
} else if ( age = = 0 ) {
ShowVehicleGettingOld ( v , STR_01A1_IS_GETTING_VERY_OLD ) ;
} else if ( age = = 366 * 1 | | age = = 366 * 2 | | age = = 366 * 3 | | age = = 366 * 4 | | age = = 366 * 5 ) {
ShowVehicleGettingOld ( v , STR_01A2_IS_GETTING_VERY_OLD_AND ) ;
}
}
2006-09-26 19:20:35 +00:00
/** Starts or stops a lot of vehicles
2006-09-29 11:30:48 +00:00
* @ param tile Tile of the depot where the vehicles are started / stopped ( only used for depots )
2007-04-04 04:08:47 +00:00
* @ param flags type of operation
2006-10-07 08:24:11 +00:00
* @ param p1 Station / Order / Depot ID ( only used for vehicle list windows )
2006-09-29 11:30:48 +00:00
* @ param p2 bitmask
2006-10-07 08:24:11 +00:00
* - bit 0 - 4 Vehicle type
* - bit 5 false = start vehicles , true = stop vehicles
* - bit 6 if set , then it ' s a vehicle list window , not a depot and Tile is ignored in this case
2006-09-29 11:30:48 +00:00
* - bit 8 - 11 Vehicle List Window type ( ignored unless bit 1 is set )
2006-09-26 19:20:35 +00:00
*/
int32 CmdMassStartStopVehicle ( TileIndex tile , uint32 flags , uint32 p1 , uint32 p2 )
{
Vehicle * * vl = NULL ;
uint16 engine_list_length = 0 ;
uint16 engine_count = 0 ;
int32 return_value = CMD_ERROR ;
uint i ;
uint stop_command ;
2006-10-07 08:24:11 +00:00
byte vehicle_type = GB ( p2 , 0 , 5 ) ;
bool start_stop = HASBIT ( p2 , 5 ) ;
bool vehicle_list_window = HASBIT ( p2 , 6 ) ;
2006-09-26 19:20:35 +00:00
switch ( vehicle_type ) {
2007-03-08 16:27:54 +00:00
case VEH_TRAIN : stop_command = CMD_START_STOP_TRAIN ; break ;
case VEH_ROAD : stop_command = CMD_START_STOP_ROADVEH ; break ;
case VEH_SHIP : stop_command = CMD_START_STOP_SHIP ; break ;
case VEH_AIRCRAFT : stop_command = CMD_START_STOP_AIRCRAFT ; break ;
2006-09-26 19:20:35 +00:00
default : return CMD_ERROR ;
}
2006-09-29 11:30:48 +00:00
if ( vehicle_list_window ) {
2007-01-21 01:25:59 +00:00
uint32 id = p1 ;
2006-09-29 11:30:48 +00:00
uint16 window_type = p2 & VLW_MASK ;
2007-01-21 01:07:15 +00:00
engine_count = GenerateVehicleSortList ( ( const Vehicle * * * ) & vl , & engine_list_length , vehicle_type , _current_player , id , window_type ) ;
2006-09-29 11:30:48 +00:00
} else {
/* Get the list of vehicles in the depot */
BuildDepotVehicleList ( vehicle_type , tile , & vl , & engine_list_length , & engine_count , NULL , NULL , NULL ) ;
}
2006-09-26 19:20:35 +00:00
for ( i = 0 ; i < engine_count ; i + + ) {
const Vehicle * v = vl [ i ] ;
int32 ret ;
if ( ! ! ( v - > vehstatus & VS_STOPPED ) ! = start_stop ) continue ;
2006-09-29 11:30:48 +00:00
if ( ! vehicle_list_window ) {
2007-03-08 16:27:54 +00:00
if ( vehicle_type = = VEH_TRAIN ) {
2006-09-29 11:30:48 +00:00
if ( CheckTrainInDepot ( v , false ) = = - 1 ) continue ;
} else {
if ( ! ( v - > vehstatus & VS_HIDDEN ) ) continue ;
}
2006-09-27 12:17:33 +00:00
}
2006-09-26 19:20:35 +00:00
ret = DoCommand ( tile , v - > index , 0 , flags , stop_command ) ;
if ( ! CmdFailed ( ret ) ) {
return_value = 0 ;
/* We know that the command is valid for at least one vehicle.
* If we haven ' t set DC_EXEC , then there is no point in continueing because it will be valid */
if ( ! ( flags & DC_EXEC ) ) break ;
}
}
2006-09-27 12:17:33 +00:00
free ( vl ) ;
2006-09-26 19:20:35 +00:00
return return_value ;
}
2006-09-27 22:44:39 +00:00
/** Sells all vehicles in a depot
2007-04-04 04:08:47 +00:00
* @ param tile Tile of the depot where the depot is
* @ param flags type of operation
* @ param p1 Vehicle type
* @ param p2 unused
*/
2006-09-27 22:44:39 +00:00
int32 CmdDepotSellAllVehicles ( TileIndex tile , uint32 flags , uint32 p1 , uint32 p2 )
{
Vehicle * * engines = NULL ;
Vehicle * * wagons = NULL ;
uint16 engine_list_length = 0 ;
uint16 engine_count = 0 ;
uint16 wagon_list_length = 0 ;
uint16 wagon_count = 0 ;
int32 cost = 0 ;
uint i , sell_command , total_number_vehicles ;
byte vehicle_type = GB ( p1 , 0 , 8 ) ;
switch ( vehicle_type ) {
2007-03-08 16:27:54 +00:00
case VEH_TRAIN : sell_command = CMD_SELL_RAIL_WAGON ; break ;
case VEH_ROAD : sell_command = CMD_SELL_ROAD_VEH ; break ;
case VEH_SHIP : sell_command = CMD_SELL_SHIP ; break ;
case VEH_AIRCRAFT : sell_command = CMD_SELL_AIRCRAFT ; break ;
2006-09-27 22:44:39 +00:00
default : return CMD_ERROR ;
}
/* Get the list of vehicles in the depot */
BuildDepotVehicleList ( vehicle_type , tile , & engines , & engine_list_length , & engine_count ,
& wagons , & wagon_list_length , & wagon_count ) ;
total_number_vehicles = engine_count + wagon_count ;
for ( i = 0 ; i < total_number_vehicles ; i + + ) {
const Vehicle * v ;
int32 ret ;
if ( i < engine_count ) {
v = engines [ i ] ;
} else {
v = wagons [ i - engine_count ] ;
}
ret = DoCommand ( tile , v - > index , 1 , flags , sell_command ) ;
if ( ! CmdFailed ( ret ) ) cost + = ret ;
}
free ( engines ) ;
free ( wagons ) ;
if ( cost = = 0 ) return CMD_ERROR ; // no vehicles to sell
return cost ;
}
2006-09-28 14:17:08 +00:00
/** Autoreplace all vehicles in the depot
2007-04-04 04:08:47 +00:00
* @ param tile Tile of the depot where the vehicles are
* @ param flags type of operation
* @ param p1 Type of vehicle
* @ param p2 Unused
*/
2006-09-28 14:17:08 +00:00
int32 CmdDepotMassAutoReplace ( TileIndex tile , uint32 flags , uint32 p1 , uint32 p2 )
{
Vehicle * * vl = NULL ;
uint16 engine_list_length = 0 ;
uint16 engine_count = 0 ;
uint i , x = 0 , y = 0 , z = 0 ;
int32 cost = 0 ;
byte vehicle_type = GB ( p1 , 0 , 8 ) ;
if ( ! IsTileOwner ( tile , _current_player ) ) return CMD_ERROR ;
/* Get the list of vehicles in the depot */
BuildDepotVehicleList ( vehicle_type , tile , & vl , & engine_list_length , & engine_count , NULL , NULL , NULL ) ;
for ( i = 0 ; i < engine_count ; i + + ) {
Vehicle * v = vl [ i ] ;
bool stopped = ! ( v - > vehstatus & VS_STOPPED ) ;
int32 ret ;
/* Ensure that the vehicle completely in the depot */
2006-10-05 08:39:16 +00:00
if ( ! IsVehicleInDepot ( v ) ) continue ;
2006-09-28 14:17:08 +00:00
2006-09-29 18:39:20 +00:00
x = v - > x_pos ;
y = v - > y_pos ;
z = v - > z_pos ;
if ( stopped ) {
v - > vehstatus | = VS_STOPPED ; // Stop the vehicle
v - > leave_depot_instantly = true ;
}
ret = MaybeReplaceVehicle ( v , ! ( flags & DC_EXEC ) , false ) ;
2006-09-28 14:17:08 +00:00
if ( ! CmdFailed ( ret ) ) {
cost + = ret ;
if ( ! ( flags & DC_EXEC ) ) break ;
/* There is a problem with autoreplace and newgrf
* It ' s impossible to tell the length of a train after it ' s being replaced before it ' s actually done
* Because of this , we can ' t estimate costs due to wagon removal and we will have to always return 0 and pay manually
* Since we pay after each vehicle is replaced and MaybeReplaceVehicle ( ) check if the player got enough money
* we should never reach a condition where the player will end up with negative money from doing this */
SET_EXPENSES_TYPE ( EXPENSES_NEW_VEHICLES ) ;
SubtractMoneyFromPlayer ( ret ) ;
}
}
if ( cost = = 0 ) {
cost = CMD_ERROR ;
} else {
if ( flags & DC_EXEC ) {
/* Display the cost animation now that DoCommandP() can't do it for us (see previous comments) */
if ( IsLocalPlayer ( ) ) ShowCostOrIncomeAnimation ( x , y , z , cost ) ;
}
cost = 0 ;
}
free ( vl ) ;
return cost ;
}
2005-07-31 13:08:08 +00:00
/** Clone a vehicle. If it is a train, it will clone all the cars too
2006-09-04 20:40:33 +00:00
* @ param tile tile of the depot where the cloned vehicle is build
2007-04-04 04:08:47 +00:00
* @ param flags type of operation
2006-09-04 20:40:33 +00:00
* @ param p1 the original vehicle ' s index
* @ param p2 1 = shared orders , else copied orders
*/
2006-04-10 07:15:58 +00:00
int32 CmdCloneVehicle ( TileIndex tile , uint32 flags , uint32 p1 , uint32 p2 )
2005-07-31 13:08:08 +00:00
{
2005-10-29 20:58:26 +00:00
Vehicle * v_front , * v ;
Vehicle * w_front , * w , * w_rear ;
int cost , total_cost = 0 ;
2006-05-11 13:31:14 +00:00
uint32 build_argument = 2 ;
2005-07-31 13:08:08 +00:00
2006-08-22 18:15:17 +00:00
if ( ! IsValidVehicleID ( p1 ) ) return CMD_ERROR ;
2005-07-31 13:08:08 +00:00
v = GetVehicle ( p1 ) ;
2005-10-29 20:58:26 +00:00
v_front = v ;
w = NULL ;
w_front = NULL ;
w_rear = NULL ;
2007-04-10 09:01:56 +00:00
2005-10-29 20:58:26 +00:00
/*
* v_front is the front engine in the original vehicle
2007-04-10 09:01:56 +00:00
* v is the car / vehicle of the original vehicle , that is currently being copied
2005-10-29 20:58:26 +00:00
* w_front is the front engine of the cloned vehicle
* w is the car / vehicle currently being cloned
* w_rear is the rear end of the cloned train . It ' s used to add more cars and is only used by trains
*/
2005-07-31 13:08:08 +00:00
2005-11-14 19:48:04 +00:00
if ( ! CheckOwnership ( v - > owner ) ) return CMD_ERROR ;
2005-07-31 13:08:08 +00:00
2007-03-08 16:27:54 +00:00
if ( v - > type = = VEH_TRAIN & & ( ! IsFrontEngine ( v ) | | v - > u . rail . crash_anim_pos > = 4400 ) ) return CMD_ERROR ;
2005-07-31 13:08:08 +00:00
2007-04-04 04:08:47 +00:00
/* check that we can allocate enough vehicles */
2005-11-05 14:01:00 +00:00
if ( ! ( flags & DC_EXEC ) ) {
int veh_counter = 0 ;
do {
veh_counter + + ;
} while ( ( v = v - > next ) ! = NULL ) ;
2005-11-05 19:58:16 +00:00
if ( ! AllocateVehicles ( NULL , veh_counter ) ) {
return_cmd_error ( STR_00E1_TOO_MANY_VEHICLES_IN_GAME ) ;
2005-11-05 14:01:00 +00:00
}
}
v = v_front ;
2005-10-29 20:58:26 +00:00
do {
2005-11-18 23:41:03 +00:00
2007-04-20 17:08:55 +00:00
if ( ! ( flags & DC_EXEC ) ) {
/* Get the refit cost.
* This is only needed when estimating as when the command is executed , the cost from the refit command is used .
* This needs to be done for every single unit , so it should be done before checking if it ' s a multiheaded engine . */
CargoID new_cargo_type = GetEngineCargoType ( v - > engine_type ) ;
if ( new_cargo_type ! = v - > cargo_type & & new_cargo_type ! = CT_INVALID ) {
total_cost + = GetRefitCost ( v - > engine_type ) ;
}
}
2005-11-18 23:41:03 +00:00
if ( IsMultiheaded ( v ) & & ! IsTrainEngine ( v ) ) {
/* we build the rear ends of multiheaded trains with the front ones */
continue ;
}
2007-01-22 16:16:52 +00:00
cost = DoCommand ( tile , v - > engine_type , build_argument , flags , GetCmdBuildVeh ( v ) ) ;
2006-05-11 14:24:33 +00:00
build_argument = 3 ; // ensure that we only assign a number to the first engine
2005-07-31 13:08:08 +00:00
2005-10-29 20:58:26 +00:00
if ( CmdFailed ( cost ) ) return cost ;
total_cost + = cost ;
if ( flags & DC_EXEC ) {
2005-11-14 19:48:04 +00:00
w = GetVehicle ( _new_vehicle_id ) ;
2005-10-29 20:58:26 +00:00
2007-02-17 13:50:22 +00:00
Vehicle * w2 = w ;
Vehicle * v2 = v ;
do {
if ( v2 - > cargo_type ! = w2 - > cargo_type | | v2 - > cargo_subtype ! = w2 - > cargo_subtype ) {
2007-04-10 09:01:56 +00:00
/* We can't pay for refitting because we can't estimate refitting costs for a vehicle before it's build.
* If we pay for it anyway , the cost and the estimated cost will not be the same and we will have an assert .
* We need to check the whole chain if it is a train because some newgrf articulated engines can refit some units only ( and not the front ) */
2007-04-20 17:08:55 +00:00
total_cost + = DoCommand ( 0 , w - > index , v2 - > cargo_type | ( v2 - > cargo_subtype < < 8 ) , flags , GetCmdRefitVeh ( v ) ) ;
2007-04-10 09:01:56 +00:00
break ; // We learned that the engine in question needed a refit. No need to check anymore
2007-02-17 13:50:22 +00:00
}
2007-03-08 16:27:54 +00:00
} while ( v - > type = = VEH_TRAIN & & ( w2 = w2 - > next ) ! = NULL & & ( v2 = v2 - > next ) ! = NULL ) ;
2007-02-17 13:50:22 +00:00
2007-03-08 16:27:54 +00:00
if ( v - > type = = VEH_TRAIN & & HASBIT ( v - > u . rail . flags , VRF_REVERSE_DIRECTION ) ) {
2006-05-23 21:49:18 +00:00
SETBIT ( w - > u . rail . flags , VRF_REVERSE_DIRECTION ) ;
}
2005-10-29 20:58:26 +00:00
2007-03-08 16:27:54 +00:00
if ( v - > type = = VEH_TRAIN & & ! IsFrontEngine ( v ) ) {
2007-04-04 04:08:47 +00:00
/* this s a train car
* add this unit to the end of the train */
2006-04-10 07:15:58 +00:00
DoCommand ( 0 , ( w_rear - > index < < 16 ) | w - > index , 1 , flags , CMD_MOVE_RAIL_VEHICLE ) ;
2005-10-29 20:58:26 +00:00
} else {
2007-04-04 04:08:47 +00:00
/* this is a front engine or not a train. It need orders */
2005-10-29 20:58:26 +00:00
w_front = w ;
2006-04-27 23:11:43 +00:00
w - > service_interval = v - > service_interval ;
2006-04-10 07:15:58 +00:00
DoCommand ( 0 , ( v - > index < < 16 ) | w - > index , p2 & 1 ? CO_SHARE : CO_COPY , flags , CMD_CLONE_ORDER ) ;
2005-07-31 13:08:08 +00:00
}
2006-08-28 18:53:03 +00:00
w_rear = w ; // trains needs to know the last car in the train, so they can add more in next loop
2005-07-31 13:08:08 +00:00
}
2007-03-08 16:27:54 +00:00
} while ( v - > type = = VEH_TRAIN & & ( v = GetNextVehicle ( v ) ) ! = NULL ) ;
2005-07-31 13:08:08 +00:00
2007-03-08 16:27:54 +00:00
if ( flags & DC_EXEC & & v_front - > type = = VEH_TRAIN ) {
2007-04-04 04:08:47 +00:00
/* for trains this needs to be the front engine due to the callback function */
2006-06-04 09:28:33 +00:00
_new_vehicle_id = w_front - > index ;
2005-07-31 13:08:08 +00:00
}
2006-11-08 17:44:17 +00:00
/* Set the expense type last as refitting will make the cost go towards
* running costs . . . */
SET_EXPENSES_TYPE ( EXPENSES_NEW_VEHICLES ) ;
2005-07-31 13:08:08 +00:00
return total_cost ;
}
2005-10-24 19:40:48 +00:00
2006-09-24 15:01:02 +00:00
/* Extend the list size for BuildDepotVehicleList() */
2006-09-29 20:41:28 +00:00
static inline void ExtendVehicleListSize ( const Vehicle * * * engine_list , uint16 * engine_list_length , uint16 step_size )
2006-09-24 15:01:02 +00:00
{
2006-12-05 13:58:20 +00:00
* engine_list_length = min ( * engine_list_length + step_size , GetMaxVehicleIndex ( ) + 1 ) ;
2007-01-11 17:29:39 +00:00
* engine_list = ReallocT ( * engine_list , * engine_list_length ) ;
2006-09-24 15:01:02 +00:00
}
/** Generates a list of vehicles inside a depot
* Will enlarge allocated space for the list if they are too small , so it ' s ok to call with ( pointer to NULL array , pointer to uninitised uint16 , pointer to 0 )
* If one of the lists is not needed ( say wagons when finding ships ) , all the pointers regarding that list should be set to NULL
2007-04-18 00:41:09 +00:00
* @ param type Type of vehicle
2006-09-24 15:01:02 +00:00
* @ param tile The tile the depot is located in
* @ param * * * engine_list Pointer to a pointer to an array of vehicles in the depot ( old list is freed and a new one is malloced )
* @ param * engine_list_length Allocated size of engine_list . Needs to be set to 0 when engine_list points to a NULL array
* @ param * engine_count The number of engines stored in the list
* @ param * * * wagon_list Pointer to a pointer to an array of free wagons in the depot ( old list is freed and a new one is malloced )
* @ param * wagon_list_length Allocated size of wagon_list . Needs to be set to 0 when wagon_list points to a NULL array
* @ param * wagon_count The number of engines stored in the list
*/
void BuildDepotVehicleList ( byte type , TileIndex tile , Vehicle * * * engine_list , uint16 * engine_list_length , uint16 * engine_count , Vehicle * * * wagon_list , uint16 * wagon_list_length , uint16 * wagon_count )
{
Vehicle * v ;
/* This function should never be called without an array to store results */
2007-03-08 16:27:54 +00:00
assert ( ! ( engine_list = = NULL & & type ! = VEH_TRAIN ) ) ;
assert ( ! ( type = = VEH_TRAIN & & engine_list = = NULL & & wagon_list = = NULL ) ) ;
2006-09-24 15:01:02 +00:00
/* Both array and the length should either be NULL to disable the list or both should not be NULL */
assert ( ( engine_list = = NULL & & engine_list_length = = NULL ) | | ( engine_list ! = NULL & & engine_list_length ! = NULL ) ) ;
assert ( ( wagon_list = = NULL & & wagon_list_length = = NULL ) | | ( wagon_list ! = NULL & & wagon_list_length ! = NULL ) ) ;
assert ( ! ( engine_list ! = NULL & & engine_count = = NULL ) ) ;
assert ( ! ( wagon_list ! = NULL & & wagon_count = = NULL ) ) ;
if ( engine_count ! = NULL ) * engine_count = 0 ;
if ( wagon_count ! = NULL ) * wagon_count = 0 ;
switch ( type ) {
2007-03-08 16:27:54 +00:00
case VEH_TRAIN :
2006-09-24 15:01:02 +00:00
FOR_ALL_VEHICLES ( v ) {
2007-03-08 16:27:54 +00:00
if ( v - > tile = = tile & & v - > type = = VEH_TRAIN & & v - > u . rail . track = = TRACK_BIT_DEPOT ) {
2006-09-24 15:01:02 +00:00
if ( IsFrontEngine ( v ) ) {
if ( engine_list = = NULL ) continue ;
2006-09-29 20:41:28 +00:00
if ( * engine_count = = * engine_list_length ) ExtendVehicleListSize ( ( const Vehicle * * * ) engine_list , engine_list_length , 25 ) ;
2006-09-24 15:01:02 +00:00
( * engine_list ) [ ( * engine_count ) + + ] = v ;
} else if ( IsFreeWagon ( v ) ) {
if ( wagon_list = = NULL ) continue ;
2006-09-29 20:41:28 +00:00
if ( * wagon_count = = * wagon_list_length ) ExtendVehicleListSize ( ( const Vehicle * * * ) wagon_list , wagon_list_length , 25 ) ;
2006-09-24 15:01:02 +00:00
( * wagon_list ) [ ( * wagon_count ) + + ] = v ;
}
}
}
break ;
2007-03-08 16:27:54 +00:00
case VEH_ROAD :
2006-09-24 15:01:02 +00:00
FOR_ALL_VEHICLES ( v ) {
2007-03-08 16:27:54 +00:00
if ( v - > tile = = tile & & v - > type = = VEH_ROAD & & IsRoadVehInDepot ( v ) ) {
2006-09-29 20:41:28 +00:00
if ( * engine_count = = * engine_list_length ) ExtendVehicleListSize ( ( const Vehicle * * * ) engine_list , engine_list_length , 25 ) ;
2006-09-24 15:01:02 +00:00
( * engine_list ) [ ( * engine_count ) + + ] = v ;
}
}
break ;
2007-03-08 16:27:54 +00:00
case VEH_SHIP :
2006-09-24 15:01:02 +00:00
FOR_ALL_VEHICLES ( v ) {
2007-03-08 16:27:54 +00:00
if ( v - > tile = = tile & & v - > type = = VEH_SHIP & & IsShipInDepot ( v ) ) {
2006-09-29 20:41:28 +00:00
if ( * engine_count = = * engine_list_length ) ExtendVehicleListSize ( ( const Vehicle * * * ) engine_list , engine_list_length , 25 ) ;
2006-09-24 15:01:02 +00:00
( * engine_list ) [ ( * engine_count ) + + ] = v ;
}
}
break ;
2007-03-08 16:27:54 +00:00
case VEH_AIRCRAFT :
2006-09-24 15:01:02 +00:00
FOR_ALL_VEHICLES ( v ) {
if ( v - > tile = = tile & &
2007-03-08 16:27:54 +00:00
v - > type = = VEH_AIRCRAFT & & IsNormalAircraft ( v ) & &
2006-09-24 15:01:02 +00:00
v - > vehstatus & VS_HIDDEN ) {
2006-09-29 20:41:28 +00:00
if ( * engine_count = = * engine_list_length ) ExtendVehicleListSize ( ( const Vehicle * * * ) engine_list , engine_list_length , 25 ) ;
2006-09-24 15:01:02 +00:00
( * engine_list ) [ ( * engine_count ) + + ] = v ;
}
}
break ;
default : NOT_REACHED ( ) ;
}
}
2006-08-31 17:42:27 +00:00
/**
2006-09-29 20:41:28 +00:00
* @ param sort_list list to store the list in . Either NULL or the length length_of_array tells
* @ param length_of_array informs the length allocated for sort_list . This is not the same as the number of vehicles in the list . Needs to be 0 when sort_list is NULL
2006-09-05 12:46:14 +00:00
* @ param type type of vehicle
* @ param owner PlayerID of owner to generate a list for
2007-04-18 00:41:09 +00:00
* @ param index This parameter has different meanings depending on window_type
< ul >
< li > VLW_STATION_LIST : index of station to generate a list for < / li >
< li > VLW_SHARED_ORDERS : index of order to generate a list for < li >
< li > VLW_STANDARD : not used < li >
< li > VLW_DEPOT_LIST : TileIndex of the depot / hangar to make the list for < / li >
< / ul >
2006-09-05 12:46:14 +00:00
* @ param window_type tells what kind of window the list is for . Use the VLW flags in vehicle_gui . h
* @ return the number of vehicles added to the list
*/
2007-01-21 01:25:59 +00:00
uint GenerateVehicleSortList ( const Vehicle * * * sort_list , uint16 * length_of_array , byte type , PlayerID owner , uint32 index , uint16 window_type )
2006-08-31 17:42:27 +00:00
{
2007-03-08 16:27:54 +00:00
const byte subtype = ( type ! = VEH_AIRCRAFT ) ? ( byte ) Train_Front : ( byte ) AIR_AIRCRAFT ;
2006-08-31 19:15:01 +00:00
uint n = 0 ;
2006-08-31 17:42:27 +00:00
const Vehicle * v ;
switch ( window_type ) {
case VLW_STATION_LIST : {
FOR_ALL_VEHICLES ( v ) {
if ( v - > type = = type & & (
2007-03-08 16:27:54 +00:00
( type = = VEH_TRAIN & & IsFrontEngine ( v ) ) | |
( type ! = VEH_TRAIN & & v - > subtype < = subtype ) ) ) {
2006-08-31 17:42:27 +00:00
const Order * order ;
FOR_VEHICLE_ORDERS ( v , order ) {
2007-01-21 01:07:15 +00:00
if ( order - > type = = OT_GOTO_STATION & & order - > dest = = index ) {
2006-09-29 20:41:28 +00:00
if ( n = = * length_of_array ) ExtendVehicleListSize ( sort_list , length_of_array , 50 ) ;
( * sort_list ) [ n + + ] = v ;
2006-08-31 17:42:27 +00:00
break ;
}
}
}
}
break ;
}
case VLW_SHARED_ORDERS : {
FOR_ALL_VEHICLES ( v ) {
/* Find a vehicle with the order in question */
2007-01-21 01:07:15 +00:00
if ( v - > orders ! = NULL & & v - > orders - > index = = index ) break ;
2006-08-31 17:42:27 +00:00
}
2007-01-21 01:07:15 +00:00
if ( v ! = NULL & & v - > orders ! = NULL & & v - > orders - > index = = index ) {
2006-08-31 17:42:27 +00:00
/* Only try to make the list if we found a vehicle using the order in question */
for ( v = GetFirstVehicleFromSharedList ( v ) ; v ! = NULL ; v = v - > next_shared ) {
2006-09-29 20:41:28 +00:00
if ( n = = * length_of_array ) ExtendVehicleListSize ( sort_list , length_of_array , 25 ) ;
( * sort_list ) [ n + + ] = v ;
2006-08-31 17:42:27 +00:00
}
}
break ;
}
case VLW_STANDARD : {
FOR_ALL_VEHICLES ( v ) {
if ( v - > type = = type & & v - > owner = = owner & & (
2007-03-08 16:27:54 +00:00
( type = = VEH_TRAIN & & IsFrontEngine ( v ) ) | |
( type ! = VEH_TRAIN & & v - > subtype < = subtype ) ) ) {
2006-09-29 20:41:28 +00:00
/* TODO find a better estimate on the total number of vehicles for current player */
2006-12-05 13:58:20 +00:00
if ( n = = * length_of_array ) ExtendVehicleListSize ( sort_list , length_of_array , GetNumVehicles ( ) / 4 ) ;
2006-09-29 20:41:28 +00:00
( * sort_list ) [ n + + ] = v ;
2006-08-31 17:42:27 +00:00
}
}
break ;
}
2006-09-30 13:39:34 +00:00
case VLW_DEPOT_LIST : {
FOR_ALL_VEHICLES ( v ) {
if ( v - > type = = type & & (
2007-03-08 16:27:54 +00:00
( type = = VEH_TRAIN & & IsFrontEngine ( v ) ) | |
( type ! = VEH_TRAIN & & v - > subtype < = subtype ) ) ) {
2006-09-30 13:39:34 +00:00
const Order * order ;
FOR_VEHICLE_ORDERS ( v , order ) {
2007-01-21 01:07:15 +00:00
if ( order - > type = = OT_GOTO_DEPOT & & order - > dest = = index ) {
2006-09-30 13:39:34 +00:00
if ( n = = * length_of_array ) ExtendVehicleListSize ( sort_list , length_of_array , 25 ) ;
( * sort_list ) [ n + + ] = v ;
break ;
}
}
}
}
break ;
}
2006-08-31 17:42:27 +00:00
default : NOT_REACHED ( ) ; break ;
}
2006-09-29 20:41:28 +00:00
if ( ( n + 100 ) < * length_of_array ) {
/* We allocated way too much for sort_list.
* Now we will reduce how much we allocated .
* We will still make it have room for 50 extra vehicles to prevent having
* to move the whole array if just one vehicle is added later */
* length_of_array = n + 50 ;
2007-01-11 17:29:39 +00:00
* sort_list = ReallocT ( * sort_list , ( * length_of_array ) * sizeof ( ( * sort_list ) [ 0 ] ) ) ;
2006-09-29 20:41:28 +00:00
}
2006-08-31 17:42:27 +00:00
return n ;
}
2006-08-30 21:39:01 +00:00
/** send all vehicles of type to depots
2006-09-04 20:40:33 +00:00
* @ param type type of vehicle
* @ param flags the flags used for DoCommand ( )
* @ param service should the vehicles only get service in the depots
* @ param owner PlayerID of owner of the vehicles to send
2007-04-18 00:41:09 +00:00
* @ param vlw_flag tells what kind of list requested the goto depot
2006-09-04 20:40:33 +00:00
* @ return 0 for success and CMD_ERROR if no vehicle is able to go to depot
*/
2006-09-01 10:24:15 +00:00
int32 SendAllVehiclesToDepot ( byte type , uint32 flags , bool service , PlayerID owner , uint16 vlw_flag , uint32 id )
2006-08-30 21:39:01 +00:00
{
2006-09-29 20:41:28 +00:00
const Vehicle * * sort_list = NULL ;
2006-08-31 17:42:27 +00:00
uint n , i ;
2006-09-29 20:41:28 +00:00
uint16 array_length = 0 ;
2006-08-31 17:42:27 +00:00
2007-01-21 01:07:15 +00:00
n = GenerateVehicleSortList ( & sort_list , & array_length , type , owner , id , vlw_flag ) ;
2006-08-30 23:01:45 +00:00
2006-08-30 21:39:01 +00:00
/* Send all the vehicles to a depot */
2006-08-31 19:15:01 +00:00
for ( i = 0 ; i < n ; i + + ) {
2006-08-31 17:42:27 +00:00
const Vehicle * v = sort_list [ i ] ;
2007-01-22 16:16:52 +00:00
int32 ret = DoCommand ( v - > tile , v - > index , ( service ? 1 : 0 ) | DEPOT_DONT_CANCEL , flags , GetCmdSendToDepot ( type ) ) ;
2006-09-05 23:43:42 +00:00
/* Return 0 if DC_EXEC is not set this is a valid goto depot command)
* In this case we know that at least one vehicle can be sent to a depot
* and we will issue the command . We can now safely quit the loop , knowing
* it will succeed at least once . With DC_EXEC we really need to send them to the depot */
if ( ! CmdFailed ( ret ) & & ! ( flags & DC_EXEC ) ) {
2006-09-30 17:13:39 +00:00
free ( ( void * ) sort_list ) ;
2006-08-31 17:42:27 +00:00
return 0 ;
2006-08-30 21:39:01 +00:00
}
}
2006-08-30 23:01:45 +00:00
2006-09-30 17:13:39 +00:00
free ( ( void * ) sort_list ) ;
2006-08-30 23:01:45 +00:00
return ( flags & DC_EXEC ) ? 0 : CMD_ERROR ;
2006-08-30 21:39:01 +00:00
}
2006-10-05 08:15:51 +00:00
bool IsVehicleInDepot ( const Vehicle * v )
{
switch ( v - > type ) {
2007-03-08 16:27:54 +00:00
case VEH_TRAIN : return CheckTrainInDepot ( v , false ) ! = - 1 ;
case VEH_ROAD : return IsRoadVehInDepot ( v ) ;
case VEH_SHIP : return IsShipInDepot ( v ) ;
case VEH_AIRCRAFT : return IsAircraftInHangar ( v ) ;
2006-10-05 08:15:51 +00:00
default : NOT_REACHED ( ) ;
}
return false ;
}
2006-10-04 12:01:59 +00:00
void VehicleEnterDepot ( Vehicle * v )
{
switch ( v - > type ) {
2007-03-08 16:27:54 +00:00
case VEH_TRAIN :
2006-10-04 12:01:59 +00:00
InvalidateWindowClasses ( WC_TRAINS_LIST ) ;
if ( ! IsFrontEngine ( v ) ) v = GetFirstVehicleInChain ( v ) ;
UpdateSignalsOnSegment ( v - > tile , GetRailDepotDirection ( v - > tile ) ) ;
v - > load_unload_time_rem = 0 ;
break ;
2007-03-08 16:27:54 +00:00
case VEH_ROAD :
2006-10-04 12:01:59 +00:00
InvalidateWindowClasses ( WC_ROADVEH_LIST ) ;
2007-02-13 22:27:27 +00:00
v - > u . road . state = RVSB_IN_DEPOT ;
2006-10-04 12:01:59 +00:00
break ;
2007-03-08 16:27:54 +00:00
case VEH_SHIP :
2006-10-04 12:01:59 +00:00
InvalidateWindowClasses ( WC_SHIPS_LIST ) ;
2007-02-13 10:46:45 +00:00
v - > u . ship . state = TRACK_BIT_DEPOT ;
2006-10-04 12:01:59 +00:00
RecalcShipStuff ( v ) ;
break ;
2007-03-08 16:27:54 +00:00
case VEH_AIRCRAFT :
2006-10-04 12:01:59 +00:00
InvalidateWindowClasses ( WC_AIRCRAFT_LIST ) ;
HandleAircraftEnterHangar ( v ) ;
break ;
default : NOT_REACHED ( ) ;
}
2007-03-08 16:27:54 +00:00
if ( v - > type ! = VEH_TRAIN ) {
2006-10-05 12:59:28 +00:00
/* Trains update the vehicle list when the first unit enters the depot and calls VehicleEnterDepot() when the last unit enters.
* We only increase the number of vehicles when the first one enters , so we will not need to search for more vehicles in the depot */
InvalidateWindowData ( WC_VEHICLE_DEPOT , v - > tile ) ;
}
2006-10-04 12:01:59 +00:00
InvalidateWindow ( WC_VEHICLE_DEPOT , v - > tile ) ;
v - > vehstatus | = VS_HIDDEN ;
v - > cur_speed = 0 ;
VehicleServiceInDepot ( v ) ;
TriggerVehicle ( v , VEHICLE_TRIGGER_DEPOT ) ;
if ( v - > current_order . type = = OT_GOTO_DEPOT ) {
Order t ;
InvalidateWindow ( WC_VEHICLE_VIEW , v - > index ) ;
t = v - > current_order ;
v - > current_order . type = OT_DUMMY ;
v - > current_order . flags = 0 ;
2006-10-08 21:10:00 +00:00
if ( t . refit_cargo < NUM_CARGO ) {
2006-10-04 12:01:59 +00:00
int32 cost ;
_current_player = v - > owner ;
2007-01-22 16:16:52 +00:00
cost = DoCommand ( v - > tile , v - > index , t . refit_cargo | t . refit_subtype < < 8 , DC_EXEC , GetCmdRefitVeh ( v ) ) ;
2006-10-08 21:46:05 +00:00
if ( CmdFailed ( cost ) ) {
v - > leave_depot_instantly = false ; // We ensure that the vehicle stays in the depot
if ( v - > owner = = _local_player ) {
/* Notify the user that we stopped the vehicle */
2007-02-07 19:10:19 +00:00
SetDParam ( 0 , _vehicle_type_names [ v - > type ] ) ;
2006-10-08 21:46:05 +00:00
SetDParam ( 1 , v - > unitnumber ) ;
AddNewsItem ( STR_ORDER_REFIT_FAILED , NEWS_FLAGS ( NM_SMALL , NF_VIEWPORT | NF_VEHICLE , NT_ADVICE , 0 ) , v - > index , 0 ) ;
}
} else if ( v - > owner = = _local_player & & cost ! = 0 ) {
ShowCostOrIncomeAnimation ( v - > x_pos , v - > y_pos , v - > z_pos , cost ) ;
}
2006-10-04 12:01:59 +00:00
}
if ( HASBIT ( t . flags , OFB_PART_OF_ORDERS ) ) {
/* Part of orders */
2007-03-08 16:27:54 +00:00
if ( v - > type = = VEH_TRAIN ) v - > u . rail . days_since_order_progr = 0 ;
2006-10-04 12:01:59 +00:00
v - > cur_order_index + + ;
} else if ( HASBIT ( t . flags , OFB_HALT_IN_DEPOT ) ) {
/* Force depot visit */
v - > vehstatus | = VS_STOPPED ;
if ( v - > owner = = _local_player ) {
StringID string ;
switch ( v - > type ) {
2007-03-08 16:27:54 +00:00
case VEH_TRAIN : string = STR_8814_TRAIN_IS_WAITING_IN_DEPOT ; break ;
case VEH_ROAD : string = STR_9016_ROAD_VEHICLE_IS_WAITING ; break ;
case VEH_SHIP : string = STR_981C_SHIP_IS_WAITING_IN_DEPOT ; break ;
case VEH_AIRCRAFT : string = STR_A014_AIRCRAFT_IS_WAITING_IN ; break ;
2006-10-04 12:01:59 +00:00
default : NOT_REACHED ( ) ; string = STR_EMPTY ; // Set the string to something to avoid a compiler warning
}
SetDParam ( 0 , v - > unitnumber ) ;
2007-04-18 22:41:53 +00:00
AddNewsItem ( string , NEWS_FLAGS ( NM_SMALL , NF_VIEWPORT | NF_VEHICLE , NT_ADVICE , 0 ) , v - > index , 0 ) ;
2006-10-04 12:01:59 +00:00
}
}
}
}
2005-07-31 13:08:08 +00:00
2005-05-11 00:00:27 +00:00
/** Give a custom name to your vehicle
2006-04-10 07:15:58 +00:00
* @ param tile unused
2007-04-04 04:08:47 +00:00
* @ param flags type of operation
2005-05-11 00:00:27 +00:00
* @ param p1 vehicle ID to name
* @ param p2 unused
*/
2006-04-10 07:15:58 +00:00
int32 CmdNameVehicle ( TileIndex tile , uint32 flags , uint32 p1 , uint32 p2 )
2004-08-09 17:04:08 +00:00
{
Vehicle * v ;
StringID str ;
2006-08-22 18:15:17 +00:00
if ( ! IsValidVehicleID ( p1 ) | | _cmd_text [ 0 ] = = ' \0 ' ) return CMD_ERROR ;
2005-01-30 20:50:06 +00:00
2005-01-06 22:31:58 +00:00
v = GetVehicle ( p1 ) ;
2004-08-09 17:04:08 +00:00
2005-05-11 00:00:27 +00:00
if ( ! CheckOwnership ( v - > owner ) ) return CMD_ERROR ;
2004-08-09 17:04:08 +00:00
2005-05-15 18:50:55 +00:00
str = AllocateNameUnique ( _cmd_text , 2 ) ;
2005-05-11 00:00:27 +00:00
if ( str = = 0 ) return CMD_ERROR ;
2004-09-10 19:02:27 +00:00
2004-08-09 17:04:08 +00:00
if ( flags & DC_EXEC ) {
StringID old_str = v - > string_id ;
v - > string_id = str ;
DeleteName ( old_str ) ;
2004-12-10 18:16:08 +00:00
ResortVehicleLists ( ) ;
2004-08-09 17:04:08 +00:00
MarkWholeScreenDirty ( ) ;
} else {
DeleteName ( str ) ;
}
return 0 ;
}
2006-01-05 21:35:54 +00:00
/** Change the service interval of a vehicle
2006-04-10 07:15:58 +00:00
* @ param tile unused
2007-04-04 04:08:47 +00:00
* @ param flags type of operation
2006-01-05 21:35:54 +00:00
* @ param p1 vehicle ID that is being service - interval - changed
* @ param p2 new service interval
*/
2006-04-10 07:15:58 +00:00
int32 CmdChangeServiceInt ( TileIndex tile , uint32 flags , uint32 p1 , uint32 p2 )
2006-01-05 21:35:54 +00:00
{
Vehicle * v ;
uint16 serv_int = GetServiceIntervalClamped ( p2 ) ; /* Double check the service interval from the user-input */
2006-08-22 18:15:17 +00:00
if ( serv_int ! = p2 | | ! IsValidVehicleID ( p1 ) ) return CMD_ERROR ;
2006-01-05 21:35:54 +00:00
v = GetVehicle ( p1 ) ;
2006-08-22 18:15:17 +00:00
if ( ! CheckOwnership ( v - > owner ) ) return CMD_ERROR ;
2006-01-05 21:35:54 +00:00
if ( flags & DC_EXEC ) {
v - > service_interval = serv_int ;
InvalidateWindow ( WC_VEHICLE_DETAILS , v - > index ) ;
}
return 0 ;
}
2004-08-09 17:04:08 +00:00
static Rect _old_vehicle_coords ;
void BeginVehicleMove ( Vehicle * v ) {
_old_vehicle_coords . left = v - > left_coord ;
_old_vehicle_coords . top = v - > top_coord ;
_old_vehicle_coords . right = v - > right_coord ;
_old_vehicle_coords . bottom = v - > bottom_coord ;
}
void EndVehicleMove ( Vehicle * v )
{
MarkAllViewportsDirty (
min ( _old_vehicle_coords . left , v - > left_coord ) ,
min ( _old_vehicle_coords . top , v - > top_coord ) ,
max ( _old_vehicle_coords . right , v - > right_coord ) + 1 ,
max ( _old_vehicle_coords . bottom , v - > bottom_coord ) + 1
) ;
}
/* returns true if staying in the same tile */
2007-02-25 10:49:13 +00:00
GetNewVehiclePosResult GetNewVehiclePos ( const Vehicle * v )
2004-08-09 17:04:08 +00:00
{
static const int8 _delta_coord [ 16 ] = {
- 1 , - 1 , - 1 , 0 , 1 , 1 , 1 , 0 , /* x */
- 1 , 0 , 1 , 1 , 1 , 0 , - 1 , - 1 , /* y */
} ;
int x = v - > x_pos + _delta_coord [ v - > direction ] ;
int y = v - > y_pos + _delta_coord [ v - > direction + 8 ] ;
2007-02-25 10:49:13 +00:00
GetNewVehiclePosResult gp ;
gp . x = x ;
gp . y = y ;
gp . old_tile = v - > tile ;
gp . new_tile = TileVirtXY ( x , y ) ;
return gp ;
2004-08-09 17:04:08 +00:00
}
2006-03-08 06:55:33 +00:00
static const Direction _new_direction_table [ ] = {
DIR_N , DIR_NW , DIR_W ,
DIR_NE , DIR_SE , DIR_SW ,
DIR_E , DIR_SE , DIR_S
2004-08-09 17:04:08 +00:00
} ;
2006-03-08 06:55:33 +00:00
Direction GetDirectionTowards ( const Vehicle * v , int x , int y )
2004-08-09 17:04:08 +00:00
{
2006-03-08 06:55:33 +00:00
Direction dir ;
2006-03-08 07:48:56 +00:00
DirDiff dirdiff ;
2004-08-09 17:04:08 +00:00
int i = 0 ;
if ( y > = v - > y_pos ) {
if ( y ! = v - > y_pos ) i + = 3 ;
i + = 3 ;
}
if ( x > = v - > x_pos ) {
if ( x ! = v - > x_pos ) i + + ;
i + + ;
}
dir = v - > direction ;
2006-03-08 07:48:56 +00:00
dirdiff = DirDifference ( _new_direction_table [ i ] , dir ) ;
if ( dirdiff = = DIRDIFF_SAME ) return dir ;
return ChangeDir ( dir , dirdiff > DIRDIFF_REVERSE ? DIRDIFF_45LEFT : DIRDIFF_45RIGHT ) ;
2004-08-09 17:04:08 +00:00
}
2005-06-17 00:22:46 +00:00
Trackdir GetVehicleTrackdir ( const Vehicle * v )
2005-05-02 23:59:11 +00:00
{
2007-01-10 18:56:51 +00:00
if ( v - > vehstatus & VS_CRASHED ) return INVALID_TRACKDIR ;
2005-05-03 20:45:23 +00:00
2006-02-01 07:36:15 +00:00
switch ( v - > type ) {
2007-03-08 16:27:54 +00:00
case VEH_TRAIN :
2007-04-04 04:08:47 +00:00
if ( v - > u . rail . track = = TRACK_BIT_DEPOT ) // We'll assume the train is facing outwards
return DiagdirToDiagTrackdir ( GetRailDepotDirection ( v - > tile ) ) ; // Train in depot
2005-05-05 20:44:52 +00:00
2007-04-04 04:08:47 +00:00
if ( v - > u . rail . track = = TRACK_BIT_WORMHOLE ) // train in tunnel, so just use his direction and assume a diagonal track
2006-03-08 08:28:48 +00:00
return DiagdirToDiagTrackdir ( DirToDiagDir ( v - > direction ) ) ;
2005-05-05 20:44:52 +00:00
2007-01-10 18:56:51 +00:00
return TrackDirectionToTrackdir ( FindFirstTrack ( v - > u . rail . track ) , v - > direction ) ;
2005-06-20 20:09:46 +00:00
2007-03-08 16:27:54 +00:00
case VEH_SHIP :
2006-06-05 11:28:00 +00:00
if ( IsShipInDepot ( v ) )
2007-04-04 04:08:47 +00:00
// We'll assume the ship is facing outwards
2006-06-04 16:04:15 +00:00
return DiagdirToDiagTrackdir ( GetShipDepotDirection ( v - > tile ) ) ;
2005-05-05 20:44:52 +00:00
2007-01-10 18:56:51 +00:00
return TrackDirectionToTrackdir ( FindFirstTrack ( v - > u . ship . state ) , v - > direction ) ;
2005-06-20 20:09:46 +00:00
2007-03-08 16:27:54 +00:00
case VEH_ROAD :
2007-04-04 04:08:47 +00:00
if ( IsRoadVehInDepot ( v ) ) // We'll assume the road vehicle is facing outwards
2006-03-11 09:10:46 +00:00
return DiagdirToDiagTrackdir ( GetRoadDepotDirection ( v - > tile ) ) ;
2005-05-05 20:44:52 +00:00
2007-04-04 04:08:47 +00:00
if ( IsStandardRoadStopTile ( v - > tile ) ) // We'll assume the road vehicle is facing outwards
return DiagdirToDiagTrackdir ( GetRoadStopDir ( v - > tile ) ) ; // Road vehicle in a station
2005-05-05 20:44:52 +00:00
2007-02-14 16:37:16 +00:00
if ( IsDriveThroughStopTile ( v - > tile ) ) return DiagdirToDiagTrackdir ( DirToDiagDir ( v - > direction ) ) ;
2006-08-14 22:34:43 +00:00
/* If vehicle's state is a valid track direction (vehicle is not turning around) return it */
2007-02-14 10:33:36 +00:00
if ( ! IsReversingRoadTrackdir ( ( Trackdir ) v - > u . road . state ) ) return ( Trackdir ) v - > u . road . state ;
2006-08-14 22:34:43 +00:00
/* Vehicle is turning around, get the direction from vehicle's direction */
2006-03-08 08:28:48 +00:00
return DiagdirToDiagTrackdir ( DirToDiagDir ( v - > direction ) ) ;
2005-06-20 20:09:46 +00:00
2007-03-08 16:27:54 +00:00
/* case VEH_AIRCRAFT: case VEH_SPECIAL: case VEH_DISASTER: */
2007-01-10 18:56:51 +00:00
default : return INVALID_TRACKDIR ;
2005-05-02 23:59:11 +00:00
}
}
2007-02-13 10:26:53 +00:00
/**
* Returns some meta - data over the to be entered tile .
* @ see VehicleEnterTileStatus to see what the bits in the return value mean .
*/
2005-06-24 12:38:35 +00:00
uint32 VehicleEnterTile ( Vehicle * v , TileIndex tile , int x , int y )
2004-09-10 19:02:27 +00:00
{
2006-04-25 07:32:33 +00:00
return _tile_type_procs [ GetTileType ( tile ) ] - > vehicle_enter_tile_proc ( v , tile , x , y ) ;
2004-08-09 17:04:08 +00:00
}
2005-02-04 14:24:23 +00:00
UnitID GetFreeUnitNumber ( byte type )
2004-08-09 17:04:08 +00:00
{
2006-02-13 21:47:02 +00:00
UnitID unit , max = 0 ;
2006-02-07 19:01:01 +00:00
const Vehicle * u ;
static bool * cache = NULL ;
static UnitID gmax = 0 ;
switch ( type ) {
2007-03-08 16:27:54 +00:00
case VEH_TRAIN : max = _patches . max_trains ; break ;
case VEH_ROAD : max = _patches . max_roadveh ; break ;
case VEH_SHIP : max = _patches . max_ships ; break ;
case VEH_AIRCRAFT : max = _patches . max_aircraft ; break ;
2006-02-07 19:11:51 +00:00
default : NOT_REACHED ( ) ;
2006-02-07 19:01:01 +00:00
}
2006-06-29 09:05:30 +00:00
if ( max = = 0 ) {
/* we can't build any of this kind of vehicle, so we just return 1 instead of looking for a free number
* a max of 0 will cause the following code to write to a NULL pointer
* We know that 1 is bigger than the max allowed vehicle number , so it ' s the same as returning something , that is too big
*/
return 1 ;
}
2006-02-07 19:01:01 +00:00
if ( max > gmax ) {
gmax = max ;
free ( cache ) ;
2007-01-11 17:29:39 +00:00
cache = MallocT < bool > ( max + 1 ) ;
2006-02-07 19:01:01 +00:00
}
2007-04-04 04:08:47 +00:00
/* Clear the cache */
2006-02-07 19:01:01 +00:00
memset ( cache , 0 , ( max + 1 ) * sizeof ( * cache ) ) ;
2004-08-09 17:04:08 +00:00
2007-04-04 04:08:47 +00:00
/* Fill the cache */
2004-08-09 17:04:08 +00:00
FOR_ALL_VEHICLES ( u ) {
2006-02-07 19:01:01 +00:00
if ( u - > type = = type & & u - > owner = = _current_player & & u - > unitnumber ! = 0 & & u - > unitnumber < = max )
cache [ u - > unitnumber ] = true ;
}
2007-04-04 04:08:47 +00:00
/* Find the first unused unit number */
2006-02-07 19:01:01 +00:00
for ( unit = 1 ; unit < = max ; unit + + ) {
if ( ! cache [ unit ] ) break ;
2004-08-09 17:04:08 +00:00
}
2006-02-07 19:01:01 +00:00
return unit ;
}
2004-08-09 17:04:08 +00:00
2007-01-17 22:19:12 +00:00
static SpriteID GetEngineColourMap ( EngineID engine_type , PlayerID player , EngineID parent_engine_type , const Vehicle * v )
2006-02-20 09:26:07 +00:00
{
2007-01-17 22:19:12 +00:00
SpriteID map = PAL_NONE ;
2006-09-15 12:27:00 +00:00
const Player * p = GetPlayer ( player ) ;
LiveryScheme scheme = LS_DEFAULT ;
2007-01-17 22:19:12 +00:00
CargoID cargo_type = v = = NULL ? ( CargoID ) CT_INVALID : v - > cargo_type ;
/* Check if we should use the colour map callback */
if ( HASBIT ( EngInfo ( engine_type ) - > callbackmask , CBM_COLOUR_REMAP ) ) {
uint16 callback = GetVehicleCallback ( CBID_VEHICLE_COLOUR_MAPPING , 0 , 0 , engine_type , v ) ;
/* A return value of 0xC000 is stated to "use the default two-color
* maps " which happens to be the failure action too... */
if ( callback ! = CALLBACK_FAILED & & callback ! = 0xC000 ) {
map = GB ( callback , 0 , 14 ) ;
/* If bit 14 is set, then the company colours are applied to the
* map else it ' s returned as - is . */
if ( ! HASBIT ( callback , 14 ) ) return map ;
}
}
2006-09-15 12:27:00 +00:00
/* The default livery is always available for use, but its in_use flag determines
* whether any _other_ liveries are in use . */
2006-09-17 20:52:54 +00:00
if ( p - > livery [ LS_DEFAULT ] . in_use & & ( _patches . liveries = = 2 | | ( _patches . liveries = = 1 & & player = = _local_player ) ) ) {
2006-09-15 12:27:00 +00:00
/* Determine the livery scheme to use */
switch ( GetEngine ( engine_type ) - > type ) {
2007-03-08 16:27:54 +00:00
case VEH_TRAIN : {
2007-01-21 16:08:18 +00:00
const RailVehicleInfo * rvi = RailVehInfo ( engine_type ) ;
switch ( rvi - > railtype ) {
2007-01-24 07:14:09 +00:00
default : NOT_REACHED ( ) ;
2006-09-15 12:27:00 +00:00
case RAILTYPE_RAIL :
case RAILTYPE_ELECTRIC :
{
if ( cargo_type = = CT_INVALID ) cargo_type = rvi - > cargo_type ;
2007-01-30 11:53:35 +00:00
if ( rvi - > railveh_type = = RAILVEH_WAGON ) {
2006-09-15 17:36:54 +00:00
if ( cargo_type = = CT_PASSENGERS | | cargo_type = = CT_MAIL | | cargo_type = = CT_VALUABLES ) {
if ( parent_engine_type = = INVALID_ENGINE ) {
scheme = LS_PASSENGER_WAGON_STEAM ;
} else {
switch ( RailVehInfo ( parent_engine_type ) - > engclass ) {
case 0 : scheme = LS_PASSENGER_WAGON_STEAM ; break ;
case 1 : scheme = LS_PASSENGER_WAGON_DIESEL ; break ;
case 2 : scheme = LS_PASSENGER_WAGON_ELECTRIC ; break ;
}
}
} else {
scheme = LS_FREIGHT_WAGON ;
}
2006-09-15 12:27:00 +00:00
} else {
bool is_mu = HASBIT ( _engine_info [ engine_type ] . misc_flags , EF_RAIL_IS_MU ) ;
switch ( rvi - > engclass ) {
case 0 : scheme = LS_STEAM ; break ;
case 1 : scheme = is_mu ? LS_DMU : LS_DIESEL ; break ;
case 2 : scheme = is_mu ? LS_EMU : LS_ELECTRIC ; break ;
}
}
break ;
}
case RAILTYPE_MONO : scheme = LS_MONORAIL ; break ;
case RAILTYPE_MAGLEV : scheme = LS_MAGLEV ; break ;
}
break ;
}
2007-03-08 16:27:54 +00:00
case VEH_ROAD : {
2006-09-15 12:27:00 +00:00
const RoadVehicleInfo * rvi = RoadVehInfo ( engine_type ) ;
if ( cargo_type = = CT_INVALID ) cargo_type = rvi - > cargo_type ;
2007-03-18 22:11:24 +00:00
scheme = IsCargoInClass ( cargo_type , CC_PASSENGERS ) ? LS_BUS : LS_TRUCK ;
2006-09-15 12:27:00 +00:00
break ;
}
2007-03-08 16:27:54 +00:00
case VEH_SHIP : {
2006-09-15 12:27:00 +00:00
const ShipVehicleInfo * svi = ShipVehInfo ( engine_type ) ;
if ( cargo_type = = CT_INVALID ) cargo_type = svi - > cargo_type ;
2007-03-18 22:11:24 +00:00
scheme = IsCargoInClass ( cargo_type , CC_PASSENGERS ) ? LS_PASSENGER_SHIP : LS_FREIGHT_SHIP ;
2006-09-15 12:27:00 +00:00
break ;
}
2007-03-08 16:27:54 +00:00
case VEH_AIRCRAFT : {
2006-09-15 12:27:00 +00:00
const AircraftVehicleInfo * avi = AircraftVehInfo ( engine_type ) ;
if ( cargo_type = = CT_INVALID ) cargo_type = CT_PASSENGERS ;
switch ( avi - > subtype ) {
2007-01-27 12:45:55 +00:00
case AIR_HELI : scheme = LS_HELICOPTER ; break ;
case AIR_CTOL : scheme = LS_SMALL_PLANE ; break ;
case AIR_CTOL | AIR_FAST : scheme = LS_LARGE_PLANE ; break ;
2006-09-15 12:27:00 +00:00
}
break ;
}
}
/* Switch back to the default scheme if the resolved scheme is not in use */
if ( ! p - > livery [ scheme ] . in_use ) scheme = LS_DEFAULT ;
}
2006-03-01 23:14:03 +00:00
2007-01-17 22:19:12 +00:00
bool twocc = HASBIT ( EngInfo ( engine_type ) - > misc_flags , EF_USES_2CC ) ;
if ( map = = PAL_NONE ) map = twocc ? ( SpriteID ) SPR_2CCMAP_BASE : ( SpriteID ) PALETTE_RECOLOR_START ;
map + = p - > livery [ scheme ] . colour1 ;
if ( twocc ) map + = p - > livery [ scheme ] . colour2 * 16 ;
2006-03-01 23:14:03 +00:00
2007-01-14 19:57:49 +00:00
return map ;
2006-02-20 09:26:07 +00:00
}
2007-01-14 19:57:49 +00:00
SpriteID GetEnginePalette ( EngineID engine_type , PlayerID player )
2006-03-01 17:35:01 +00:00
{
2007-01-17 22:19:12 +00:00
return GetEngineColourMap ( engine_type , player , INVALID_ENGINE , NULL ) ;
2006-03-01 17:35:01 +00:00
}
2007-01-14 19:57:49 +00:00
SpriteID GetVehiclePalette ( const Vehicle * v )
2006-03-01 17:35:01 +00:00
{
2007-03-08 16:27:54 +00:00
if ( v - > type = = VEH_TRAIN ) {
2006-09-15 12:27:00 +00:00
return GetEngineColourMap (
( v - > u . rail . first_engine ! = INVALID_ENGINE & & ( IsArticulatedPart ( v ) | | UsesWagonOverride ( v ) ) ) ?
v - > u . rail . first_engine : v - > engine_type ,
2007-01-17 22:19:12 +00:00
v - > owner , v - > u . rail . first_engine , v ) ;
2006-09-15 12:27:00 +00:00
}
2007-01-17 22:19:12 +00:00
return GetEngineColourMap ( v - > engine_type , v - > owner , INVALID_ENGINE , v ) ;
2006-03-01 17:35:01 +00:00
}
2007-04-04 04:08:47 +00:00
/** Save and load of vehicles */
2007-01-10 18:56:51 +00:00
extern const SaveLoad _common_veh_desc [ ] = {
2006-08-22 14:38:37 +00:00
SLE_VAR ( Vehicle , subtype , SLE_UINT8 ) ,
SLE_REF ( Vehicle , next , REF_VEHICLE_OLD ) ,
SLE_VAR ( Vehicle , string_id , SLE_STRINGID ) ,
SLE_CONDVAR ( Vehicle , unitnumber , SLE_FILE_U8 | SLE_VAR_U16 , 0 , 7 ) ,
SLE_CONDVAR ( Vehicle , unitnumber , SLE_UINT16 , 8 , SL_MAX_VERSION ) ,
SLE_VAR ( Vehicle , owner , SLE_UINT8 ) ,
SLE_CONDVAR ( Vehicle , tile , SLE_FILE_U16 | SLE_VAR_U32 , 0 , 5 ) ,
SLE_CONDVAR ( Vehicle , tile , SLE_UINT32 , 6 , SL_MAX_VERSION ) ,
SLE_CONDVAR ( Vehicle , dest_tile , SLE_FILE_U16 | SLE_VAR_U32 , 0 , 5 ) ,
SLE_CONDVAR ( Vehicle , dest_tile , SLE_UINT32 , 6 , SL_MAX_VERSION ) ,
SLE_CONDVAR ( Vehicle , x_pos , SLE_FILE_U16 | SLE_VAR_U32 , 0 , 5 ) ,
SLE_CONDVAR ( Vehicle , x_pos , SLE_UINT32 , 6 , SL_MAX_VERSION ) ,
SLE_CONDVAR ( Vehicle , y_pos , SLE_FILE_U16 | SLE_VAR_U32 , 0 , 5 ) ,
SLE_CONDVAR ( Vehicle , y_pos , SLE_UINT32 , 6 , SL_MAX_VERSION ) ,
SLE_VAR ( Vehicle , z_pos , SLE_UINT8 ) ,
SLE_VAR ( Vehicle , direction , SLE_UINT8 ) ,
SLE_VAR ( Vehicle , cur_image , SLE_UINT16 ) ,
SLE_VAR ( Vehicle , spritenum , SLE_UINT8 ) ,
SLE_VAR ( Vehicle , sprite_width , SLE_UINT8 ) ,
SLE_VAR ( Vehicle , sprite_height , SLE_UINT8 ) ,
SLE_VAR ( Vehicle , z_height , SLE_UINT8 ) ,
SLE_VAR ( Vehicle , x_offs , SLE_INT8 ) ,
SLE_VAR ( Vehicle , y_offs , SLE_INT8 ) ,
SLE_VAR ( Vehicle , engine_type , SLE_UINT16 ) ,
SLE_VAR ( Vehicle , max_speed , SLE_UINT16 ) ,
SLE_VAR ( Vehicle , cur_speed , SLE_UINT16 ) ,
SLE_VAR ( Vehicle , subspeed , SLE_UINT8 ) ,
SLE_VAR ( Vehicle , acceleration , SLE_UINT8 ) ,
SLE_VAR ( Vehicle , progress , SLE_UINT8 ) ,
SLE_VAR ( Vehicle , vehstatus , SLE_UINT8 ) ,
SLE_CONDVAR ( Vehicle , last_station_visited , SLE_FILE_U8 | SLE_VAR_U16 , 0 , 4 ) ,
SLE_CONDVAR ( Vehicle , last_station_visited , SLE_UINT16 , 5 , SL_MAX_VERSION ) ,
SLE_VAR ( Vehicle , cargo_type , SLE_UINT8 ) ,
2006-10-01 12:25:31 +00:00
SLE_CONDVAR ( Vehicle , cargo_subtype , SLE_UINT8 , 35 , SL_MAX_VERSION ) ,
2006-08-22 14:38:37 +00:00
SLE_VAR ( Vehicle , cargo_days , SLE_UINT8 ) ,
SLE_CONDVAR ( Vehicle , cargo_source , SLE_FILE_U8 | SLE_VAR_U16 , 0 , 6 ) ,
SLE_CONDVAR ( Vehicle , cargo_source , SLE_UINT16 , 7 , SL_MAX_VERSION ) ,
2007-01-15 14:42:24 +00:00
SLE_CONDVAR ( Vehicle , cargo_source_xy , SLE_UINT32 , 44 , SL_MAX_VERSION ) ,
2006-08-22 14:38:37 +00:00
SLE_VAR ( Vehicle , cargo_cap , SLE_UINT16 ) ,
SLE_VAR ( Vehicle , cargo_count , SLE_UINT16 ) ,
SLE_VAR ( Vehicle , day_counter , SLE_UINT8 ) ,
SLE_VAR ( Vehicle , tick_counter , SLE_UINT8 ) ,
SLE_VAR ( Vehicle , cur_order_index , SLE_UINT8 ) ,
SLE_VAR ( Vehicle , num_orders , SLE_UINT8 ) ,
2005-01-09 18:49:18 +00:00
/* This next line is for version 4 and prior compatibility.. it temporarily reads
type and flags ( which were both 4 bits ) into type . Later on this is
converted correctly */
2006-09-03 08:25:27 +00:00
SLE_CONDVARX ( offsetof ( Vehicle , current_order ) + offsetof ( Order , type ) , SLE_UINT8 , 0 , 4 ) ,
SLE_CONDVARX ( offsetof ( Vehicle , current_order ) + offsetof ( Order , dest ) , SLE_FILE_U8 | SLE_VAR_U16 , 0 , 4 ) ,
2005-01-09 18:49:18 +00:00
/* Orders for version 5 and on */
2006-09-03 08:25:27 +00:00
SLE_CONDVARX ( offsetof ( Vehicle , current_order ) + offsetof ( Order , type ) , SLE_UINT8 , 5 , SL_MAX_VERSION ) ,
SLE_CONDVARX ( offsetof ( Vehicle , current_order ) + offsetof ( Order , flags ) , SLE_UINT8 , 5 , SL_MAX_VERSION ) ,
SLE_CONDVARX ( offsetof ( Vehicle , current_order ) + offsetof ( Order , dest ) , SLE_UINT16 , 5 , SL_MAX_VERSION ) ,
2006-08-22 14:38:37 +00:00
2006-10-03 14:52:39 +00:00
/* Refit in current order */
SLE_CONDVARX ( offsetof ( Vehicle , current_order ) + offsetof ( Order , refit_cargo ) , SLE_UINT8 , 36 , SL_MAX_VERSION ) ,
SLE_CONDVARX ( offsetof ( Vehicle , current_order ) + offsetof ( Order , refit_subtype ) , SLE_UINT8 , 36 , SL_MAX_VERSION ) ,
2006-08-22 14:38:37 +00:00
SLE_REF ( Vehicle , orders , REF_ORDER ) ,
SLE_CONDVAR ( Vehicle , age , SLE_FILE_U16 | SLE_VAR_I32 , 0 , 30 ) ,
SLE_CONDVAR ( Vehicle , age , SLE_INT32 , 31 , SL_MAX_VERSION ) ,
SLE_CONDVAR ( Vehicle , max_age , SLE_FILE_U16 | SLE_VAR_I32 , 0 , 30 ) ,
SLE_CONDVAR ( Vehicle , max_age , SLE_INT32 , 31 , SL_MAX_VERSION ) ,
SLE_CONDVAR ( Vehicle , date_of_last_service , SLE_FILE_U16 | SLE_VAR_I32 , 0 , 30 ) ,
SLE_CONDVAR ( Vehicle , date_of_last_service , SLE_INT32 , 31 , SL_MAX_VERSION ) ,
SLE_CONDVAR ( Vehicle , service_interval , SLE_FILE_U16 | SLE_VAR_I32 , 0 , 30 ) ,
SLE_CONDVAR ( Vehicle , service_interval , SLE_INT32 , 31 , SL_MAX_VERSION ) ,
SLE_VAR ( Vehicle , reliability , SLE_UINT16 ) ,
SLE_VAR ( Vehicle , reliability_spd_dec , SLE_UINT16 ) ,
SLE_VAR ( Vehicle , breakdown_ctr , SLE_UINT8 ) ,
SLE_VAR ( Vehicle , breakdown_delay , SLE_UINT8 ) ,
SLE_VAR ( Vehicle , breakdowns_since_last_service , SLE_UINT8 ) ,
SLE_VAR ( Vehicle , breakdown_chance , SLE_UINT8 ) ,
SLE_CONDVAR ( Vehicle , build_year , SLE_FILE_U8 | SLE_VAR_I32 , 0 , 30 ) ,
SLE_CONDVAR ( Vehicle , build_year , SLE_INT32 , 31 , SL_MAX_VERSION ) ,
SLE_VAR ( Vehicle , load_unload_time_rem , SLE_UINT16 ) ,
2007-01-31 22:33:24 +00:00
SLE_CONDVAR ( Vehicle , cargo_paid_for , SLE_UINT16 , 45 , SL_MAX_VERSION ) ,
2007-02-28 17:18:36 +00:00
SLE_CONDVAR ( Vehicle , vehicle_flags , SLE_UINT8 , 40 , SL_MAX_VERSION ) ,
2006-08-22 14:38:37 +00:00
SLE_VAR ( Vehicle , profit_this_year , SLE_INT32 ) ,
SLE_VAR ( Vehicle , profit_last_year , SLE_INT32 ) ,
2007-03-02 18:49:11 +00:00
SLE_CONDVAR ( Vehicle , cargo_feeder_share , SLE_INT32 , 51 , SL_MAX_VERSION ) ,
SLE_CONDVAR ( Vehicle , cargo_loaded_at_xy , SLE_UINT32 , 51 , SL_MAX_VERSION ) ,
2006-08-22 14:38:37 +00:00
SLE_VAR ( Vehicle , value , SLE_UINT32 ) ,
SLE_VAR ( Vehicle , random_bits , SLE_UINT8 ) ,
SLE_VAR ( Vehicle , waiting_triggers , SLE_UINT8 ) ,
SLE_REF ( Vehicle , next_shared , REF_VEHICLE ) ,
SLE_REF ( Vehicle , prev_shared , REF_VEHICLE ) ,
2005-01-15 19:06:22 +00:00
2007-04-04 04:08:47 +00:00
/* reserve extra space in savegame here. (currently 10 bytes) */
2006-08-22 14:38:37 +00:00
SLE_CONDNULL ( 10 , 2 , SL_MAX_VERSION ) ,
2004-09-10 19:02:27 +00:00
2004-08-09 17:04:08 +00:00
SLE_END ( )
} ;
2005-05-30 22:16:05 +00:00
static const SaveLoad _train_desc [ ] = {
2007-03-08 16:27:54 +00:00
SLE_WRITEBYTE ( Vehicle , type , VEH_TRAIN , 0 ) , // Train type. VEH_TRAIN in mem, 0 in file.
2004-08-09 17:04:08 +00:00
SLE_INCLUDEX ( 0 , INC_VEHICLE_COMMON ) ,
2006-08-22 14:38:37 +00:00
SLE_VARX ( offsetof ( Vehicle , u ) + offsetof ( VehicleRail , crash_anim_pos ) , SLE_UINT16 ) ,
SLE_VARX ( offsetof ( Vehicle , u ) + offsetof ( VehicleRail , force_proceed ) , SLE_UINT8 ) ,
SLE_VARX ( offsetof ( Vehicle , u ) + offsetof ( VehicleRail , railtype ) , SLE_UINT8 ) ,
SLE_VARX ( offsetof ( Vehicle , u ) + offsetof ( VehicleRail , track ) , SLE_UINT8 ) ,
2004-08-09 17:04:08 +00:00
2006-08-22 14:38:37 +00:00
SLE_CONDVARX ( offsetof ( Vehicle , u ) + offsetof ( VehicleRail , flags ) , SLE_UINT8 , 2 , SL_MAX_VERSION ) ,
SLE_CONDVARX ( offsetof ( Vehicle , u ) + offsetof ( VehicleRail , days_since_order_progr ) , SLE_UINT16 , 2 , SL_MAX_VERSION ) ,
2004-08-09 17:04:08 +00:00
2006-03-16 00:20:33 +00:00
SLE_CONDNULL ( 2 , 2 , 19 ) ,
2007-04-04 04:08:47 +00:00
/* reserve extra space in savegame here. (currently 11 bytes) */
2006-03-16 00:20:33 +00:00
SLE_CONDNULL ( 11 , 2 , SL_MAX_VERSION ) ,
2004-08-09 17:04:08 +00:00
SLE_END ( )
} ;
2005-05-30 22:16:05 +00:00
static const SaveLoad _roadveh_desc [ ] = {
2007-03-08 16:27:54 +00:00
SLE_WRITEBYTE ( Vehicle , type , VEH_ROAD , 1 ) , // Road type. VEH_ROAD in mem, 1 in file.
2004-08-09 17:04:08 +00:00
SLE_INCLUDEX ( 0 , INC_VEHICLE_COMMON ) ,
2006-08-22 14:38:37 +00:00
SLE_VARX ( offsetof ( Vehicle , u ) + offsetof ( VehicleRoad , state ) , SLE_UINT8 ) ,
SLE_VARX ( offsetof ( Vehicle , u ) + offsetof ( VehicleRoad , frame ) , SLE_UINT8 ) ,
SLE_VARX ( offsetof ( Vehicle , u ) + offsetof ( VehicleRoad , blocked_ctr ) , SLE_UINT16 ) ,
SLE_VARX ( offsetof ( Vehicle , u ) + offsetof ( VehicleRoad , overtaking ) , SLE_UINT8 ) ,
SLE_VARX ( offsetof ( Vehicle , u ) + offsetof ( VehicleRoad , overtaking_ctr ) , SLE_UINT8 ) ,
SLE_VARX ( offsetof ( Vehicle , u ) + offsetof ( VehicleRoad , crashed_ctr ) , SLE_UINT16 ) ,
SLE_VARX ( offsetof ( Vehicle , u ) + offsetof ( VehicleRoad , reverse_ctr ) , SLE_UINT8 ) ,
SLE_CONDREFX ( offsetof ( Vehicle , u ) + offsetof ( VehicleRoad , slot ) , REF_ROADSTOPS , 6 , SL_MAX_VERSION ) ,
SLE_CONDNULL ( 1 , 6 , SL_MAX_VERSION ) ,
SLE_CONDVARX ( offsetof ( Vehicle , u ) + offsetof ( VehicleRoad , slot_age ) , SLE_UINT8 , 6 , SL_MAX_VERSION ) ,
2007-04-04 04:08:47 +00:00
/* reserve extra space in savegame here. (currently 16 bytes) */
2006-08-22 14:38:37 +00:00
SLE_CONDNULL ( 16 , 2 , SL_MAX_VERSION ) ,
2004-08-09 17:04:08 +00:00
SLE_END ( )
} ;
2005-05-30 22:16:05 +00:00
static const SaveLoad _ship_desc [ ] = {
2007-03-08 16:27:54 +00:00
SLE_WRITEBYTE ( Vehicle , type , VEH_SHIP , 2 ) , // Ship type. VEH_SHIP in mem, 2 in file.
2004-08-09 17:04:08 +00:00
SLE_INCLUDEX ( 0 , INC_VEHICLE_COMMON ) ,
2006-08-22 14:38:37 +00:00
SLE_VARX ( offsetof ( Vehicle , u ) + offsetof ( VehicleShip , state ) , SLE_UINT8 ) ,
2004-08-09 17:04:08 +00:00
2007-04-04 04:08:47 +00:00
/* reserve extra space in savegame here. (currently 16 bytes) */
2006-03-16 00:20:33 +00:00
SLE_CONDNULL ( 16 , 2 , SL_MAX_VERSION ) ,
2004-08-09 17:04:08 +00:00
SLE_END ( )
} ;
2005-05-30 22:16:05 +00:00
static const SaveLoad _aircraft_desc [ ] = {
2007-03-08 16:27:54 +00:00
SLE_WRITEBYTE ( Vehicle , type , VEH_AIRCRAFT , 3 ) , // Aircraft type. VEH_AIRCRAFT in mem, 3 in file.
2004-08-09 17:04:08 +00:00
SLE_INCLUDEX ( 0 , INC_VEHICLE_COMMON ) ,
2006-08-22 14:38:37 +00:00
SLE_VARX ( offsetof ( Vehicle , u ) + offsetof ( VehicleAir , crashed_counter ) , SLE_UINT16 ) ,
SLE_VARX ( offsetof ( Vehicle , u ) + offsetof ( VehicleAir , pos ) , SLE_UINT8 ) ,
2004-12-28 11:51:31 +00:00
2006-08-22 14:38:37 +00:00
SLE_CONDVARX ( offsetof ( Vehicle , u ) + offsetof ( VehicleAir , targetairport ) , SLE_FILE_U8 | SLE_VAR_U16 , 0 , 4 ) ,
SLE_CONDVARX ( offsetof ( Vehicle , u ) + offsetof ( VehicleAir , targetairport ) , SLE_UINT16 , 5 , SL_MAX_VERSION ) ,
2004-12-28 11:51:31 +00:00
2006-08-22 14:38:37 +00:00
SLE_VARX ( offsetof ( Vehicle , u ) + offsetof ( VehicleAir , state ) , SLE_UINT8 ) ,
2004-09-10 19:02:27 +00:00
2006-08-22 14:38:37 +00:00
SLE_CONDVARX ( offsetof ( Vehicle , u ) + offsetof ( VehicleAir , previous_pos ) , SLE_UINT8 , 2 , SL_MAX_VERSION ) ,
2004-08-09 17:04:08 +00:00
2007-04-04 04:08:47 +00:00
/* reserve extra space in savegame here. (currently 15 bytes) */
2006-08-22 14:38:37 +00:00
SLE_CONDNULL ( 15 , 2 , SL_MAX_VERSION ) ,
2004-08-09 17:04:08 +00:00
SLE_END ( )
} ;
2005-05-30 22:16:05 +00:00
static const SaveLoad _special_desc [ ] = {
2007-03-08 16:27:54 +00:00
SLE_WRITEBYTE ( Vehicle , type , VEH_SPECIAL , 4 ) ,
2004-08-09 17:04:08 +00:00
2006-08-22 14:38:37 +00:00
SLE_VAR ( Vehicle , subtype , SLE_UINT8 ) ,
2004-09-10 19:02:27 +00:00
2006-08-22 14:38:37 +00:00
SLE_CONDVAR ( Vehicle , tile , SLE_FILE_U16 | SLE_VAR_U32 , 0 , 5 ) ,
SLE_CONDVAR ( Vehicle , tile , SLE_UINT32 , 6 , SL_MAX_VERSION ) ,
2004-08-09 17:04:08 +00:00
2006-08-22 14:38:37 +00:00
SLE_CONDVAR ( Vehicle , x_pos , SLE_FILE_I16 | SLE_VAR_I32 , 0 , 5 ) ,
SLE_CONDVAR ( Vehicle , x_pos , SLE_INT32 , 6 , SL_MAX_VERSION ) ,
SLE_CONDVAR ( Vehicle , y_pos , SLE_FILE_I16 | SLE_VAR_I32 , 0 , 5 ) ,
SLE_CONDVAR ( Vehicle , y_pos , SLE_INT32 , 6 , SL_MAX_VERSION ) ,
SLE_VAR ( Vehicle , z_pos , SLE_UINT8 ) ,
2004-08-09 17:04:08 +00:00
2006-08-22 14:38:37 +00:00
SLE_VAR ( Vehicle , cur_image , SLE_UINT16 ) ,
SLE_VAR ( Vehicle , sprite_width , SLE_UINT8 ) ,
SLE_VAR ( Vehicle , sprite_height , SLE_UINT8 ) ,
SLE_VAR ( Vehicle , z_height , SLE_UINT8 ) ,
SLE_VAR ( Vehicle , x_offs , SLE_INT8 ) ,
SLE_VAR ( Vehicle , y_offs , SLE_INT8 ) ,
SLE_VAR ( Vehicle , progress , SLE_UINT8 ) ,
SLE_VAR ( Vehicle , vehstatus , SLE_UINT8 ) ,
2004-08-09 17:04:08 +00:00
2006-08-22 14:38:37 +00:00
SLE_VARX ( offsetof ( Vehicle , u ) + offsetof ( VehicleSpecial , unk0 ) , SLE_UINT16 ) ,
SLE_VARX ( offsetof ( Vehicle , u ) + offsetof ( VehicleSpecial , unk2 ) , SLE_UINT8 ) ,
2004-08-09 17:04:08 +00:00
2007-04-04 04:08:47 +00:00
/* reserve extra space in savegame here. (currently 16 bytes) */
2006-03-16 00:20:33 +00:00
SLE_CONDNULL ( 16 , 2 , SL_MAX_VERSION ) ,
2004-08-09 17:04:08 +00:00
SLE_END ( )
} ;
2005-05-30 22:16:05 +00:00
static const SaveLoad _disaster_desc [ ] = {
2007-03-08 16:27:54 +00:00
SLE_WRITEBYTE ( Vehicle , type , VEH_DISASTER , 5 ) ,
2006-08-22 14:38:37 +00:00
SLE_REF ( Vehicle , next , REF_VEHICLE_OLD ) ,
SLE_VAR ( Vehicle , subtype , SLE_UINT8 ) ,
SLE_CONDVAR ( Vehicle , tile , SLE_FILE_U16 | SLE_VAR_U32 , 0 , 5 ) ,
SLE_CONDVAR ( Vehicle , tile , SLE_UINT32 , 6 , SL_MAX_VERSION ) ,
SLE_CONDVAR ( Vehicle , dest_tile , SLE_FILE_U16 | SLE_VAR_U32 , 0 , 5 ) ,
SLE_CONDVAR ( Vehicle , dest_tile , SLE_UINT32 , 6 , SL_MAX_VERSION ) ,
SLE_CONDVAR ( Vehicle , x_pos , SLE_FILE_I16 | SLE_VAR_I32 , 0 , 5 ) ,
SLE_CONDVAR ( Vehicle , x_pos , SLE_INT32 , 6 , SL_MAX_VERSION ) ,
SLE_CONDVAR ( Vehicle , y_pos , SLE_FILE_I16 | SLE_VAR_I32 , 0 , 5 ) ,
SLE_CONDVAR ( Vehicle , y_pos , SLE_INT32 , 6 , SL_MAX_VERSION ) ,
SLE_VAR ( Vehicle , z_pos , SLE_UINT8 ) ,
SLE_VAR ( Vehicle , direction , SLE_UINT8 ) ,
SLE_VAR ( Vehicle , x_offs , SLE_INT8 ) ,
SLE_VAR ( Vehicle , y_offs , SLE_INT8 ) ,
SLE_VAR ( Vehicle , sprite_width , SLE_UINT8 ) ,
SLE_VAR ( Vehicle , sprite_height , SLE_UINT8 ) ,
SLE_VAR ( Vehicle , z_height , SLE_UINT8 ) ,
SLE_VAR ( Vehicle , owner , SLE_UINT8 ) ,
SLE_VAR ( Vehicle , vehstatus , SLE_UINT8 ) ,
2006-09-03 08:25:27 +00:00
SLE_CONDVARX ( offsetof ( Vehicle , current_order ) + offsetof ( Order , dest ) , SLE_FILE_U8 | SLE_VAR_U16 , 0 , 4 ) ,
SLE_CONDVARX ( offsetof ( Vehicle , current_order ) + offsetof ( Order , dest ) , SLE_UINT16 , 5 , SL_MAX_VERSION ) ,
2006-08-22 14:38:37 +00:00
SLE_VAR ( Vehicle , cur_image , SLE_UINT16 ) ,
SLE_CONDVAR ( Vehicle , age , SLE_FILE_U16 | SLE_VAR_I32 , 0 , 30 ) ,
SLE_CONDVAR ( Vehicle , age , SLE_INT32 , 31 , SL_MAX_VERSION ) ,
SLE_VAR ( Vehicle , tick_counter , SLE_UINT8 ) ,
SLE_VARX ( offsetof ( Vehicle , u ) + offsetof ( VehicleDisaster , image_override ) , SLE_UINT16 ) ,
SLE_VARX ( offsetof ( Vehicle , u ) + offsetof ( VehicleDisaster , unk2 ) , SLE_UINT16 ) ,
2004-08-09 17:04:08 +00:00
2007-04-04 04:08:47 +00:00
/* reserve extra space in savegame here. (currently 16 bytes) */
2006-08-22 14:38:37 +00:00
SLE_CONDNULL ( 16 , 2 , SL_MAX_VERSION ) ,
2004-08-09 17:04:08 +00:00
SLE_END ( )
} ;
static const void * _veh_descs [ ] = {
_train_desc ,
_roadveh_desc ,
_ship_desc ,
_aircraft_desc ,
_special_desc ,
_disaster_desc ,
} ;
2007-04-04 04:08:47 +00:00
/** Will be called when the vehicles need to be saved. */
2007-03-07 11:47:46 +00:00
static void Save_VEHS ( )
2004-08-09 17:04:08 +00:00
{
Vehicle * v ;
2007-04-04 04:08:47 +00:00
/* Write the vehicles */
2004-08-09 17:04:08 +00:00
FOR_ALL_VEHICLES ( v ) {
2006-08-22 15:33:35 +00:00
SlSetArrayIndex ( v - > index ) ;
2007-02-07 19:10:19 +00:00
SlObject ( v , ( SaveLoad * ) _veh_descs [ v - > type ] ) ;
2004-08-09 17:04:08 +00:00
}
}
2007-04-04 04:08:47 +00:00
/** Will be called when vehicles need to be loaded. */
2007-03-07 11:47:46 +00:00
static void Load_VEHS ( )
2004-08-09 17:04:08 +00:00
{
int index ;
Vehicle * v ;
while ( ( index = SlIterateArray ( ) ) ! = - 1 ) {
2005-02-04 13:23:29 +00:00
Vehicle * v ;
2005-01-06 22:31:58 +00:00
2006-10-28 10:55:59 +00:00
if ( ! AddBlockIfNeeded ( & _Vehicle_pool , index ) )
2005-02-04 13:23:29 +00:00
error ( " Vehicles: failed loading savegame: too many vehicles " ) ;
v = GetVehicle ( index ) ;
2007-01-10 18:56:51 +00:00
SlObject ( v , ( SaveLoad * ) _veh_descs [ SlReadByte ( ) ] ) ;
2005-02-04 13:23:29 +00:00
2005-10-23 13:04:44 +00:00
/* Old savegames used 'last_station_visited = 0xFF' */
2005-11-22 19:33:29 +00:00
if ( CheckSavegameVersion ( 5 ) & & v - > last_station_visited = = 0xFF )
2005-09-28 19:35:36 +00:00
v - > last_station_visited = INVALID_STATION ;
2005-01-09 18:49:18 +00:00
2005-11-22 19:33:29 +00:00
if ( CheckSavegameVersion ( 5 ) ) {
2005-01-09 18:49:18 +00:00
/* Convert the current_order.type (which is a mix of type and flags, because
2006-09-04 20:40:33 +00:00
* in those versions , they both were 4 bits big ) to type and flags */
2005-01-09 18:49:18 +00:00
v - > current_order . flags = ( v - > current_order . type & 0xF0 ) > > 4 ;
2007-01-10 18:56:51 +00:00
v - > current_order . type . m_val & = 0x0F ;
2005-01-09 18:49:18 +00:00
}
2004-08-09 17:04:08 +00:00
}
2005-01-15 19:06:22 +00:00
/* Check for shared order-lists (we now use pointers for that) */
2005-11-22 19:33:29 +00:00
if ( CheckSavegameVersionOldStyle ( 5 , 2 ) ) {
2005-01-15 19:06:22 +00:00
FOR_ALL_VEHICLES ( v ) {
Vehicle * u ;
FOR_ALL_VEHICLES_FROM ( u , v - > index + 1 ) {
/* If a vehicle has the same orders, add the link to eachother
2006-09-04 20:40:33 +00:00
* in both vehicles */
2005-01-15 19:06:22 +00:00
if ( v - > orders = = u - > orders ) {
v - > next_shared = u ;
u - > prev_shared = v ;
break ;
}
}
}
}
2004-08-09 17:04:08 +00:00
}
2007-01-10 18:56:51 +00:00
extern const ChunkHandler _veh_chunk_handlers [ ] = {
2005-03-24 17:03:37 +00:00
{ ' VEHS ' , Save_VEHS , Load_VEHS , CH_SPARSE_ARRAY | CH_LAST } ,
2004-08-09 17:04:08 +00:00
} ;
2007-01-13 18:55:54 +00:00
void Vehicle : : BeginLoading ( )
{
2007-03-08 16:27:54 +00:00
assert ( IsTileType ( tile , MP_STATION ) | | type = = VEH_SHIP ) ;
2007-01-13 18:55:54 +00:00
current_order . type = OT_LOADING ;
2007-04-20 08:00:30 +00:00
GetStation ( this - > last_station_visited ) - > loading_vehicles . push_back ( this ) ;
2007-01-13 18:55:54 +00:00
}
void Vehicle : : LeaveStation ( )
{
2007-03-08 16:27:54 +00:00
assert ( IsTileType ( tile , MP_STATION ) | | type = = VEH_SHIP ) ;
2007-01-13 18:55:54 +00:00
assert ( current_order . type = = OT_LOADING ) ;
current_order . type = OT_LEAVESTATION ;
current_order . flags = 0 ;
2007-04-20 11:05:56 +00:00
GetStation ( this - > last_station_visited ) - > loading_vehicles . remove ( this ) ;
2007-01-13 18:55:54 +00:00
}