vehicle.cpp File Reference

Base implementations of all vehicles. More...

#include "stdafx.h"
#include "gui.h"
#include "debug.h"
#include "roadveh.h"
#include "ship.h"
#include "spritecache.h"
#include "landscape.h"
#include "timetable.h"
#include "viewport_func.h"
#include "news_func.h"
#include "command_func.h"
#include "company_func.h"
#include "vehicle_gui.h"
#include "train.h"
#include "aircraft.h"
#include "newgrf_engine.h"
#include "newgrf_sound.h"
#include "newgrf_station.h"
#include "group.h"
#include "group_gui.h"
#include "strings_func.h"
#include "zoom_func.h"
#include "functions.h"
#include "date_func.h"
#include "window_func.h"
#include "vehicle_func.h"
#include "autoreplace_func.h"
#include "autoreplace_gui.h"
#include "station_base.h"
#include "ai/ai.hpp"
#include "core/smallmap_type.hpp"
#include "depot_func.h"
#include "network/network.h"
#include "core/pool_func.hpp"
#include "economy_base.h"
#include "articulated_vehicles.h"
#include "roadstop_base.h"
#include "core/random_func.hpp"
#include "engine_base.h"
#include "newgrf.h"
#include "table/sprites.h"
#include "table/strings.h"

Go to the source code of this file.

Defines

#define GEN_HASH(x, y)   ((GB((y), 6, 6) << 6) + GB((x), 7, 6))

Typedefs

typedef SmallMap< Vehicle
*, bool, 4 > 
AutoreplaceMap
 List of vehicles that should check for autoreplace this tick.

Functions

void VehicleServiceInDepot (Vehicle *v)
void ShowNewGrfVehicleError (EngineID engine, StringID part1, StringID part2, GRFBugs bug_type, bool critical)
 Displays a "NewGrf Bug" error message for a engine, and pauses the game if not networking.
static VehicleEnsureNoVehicleProcZ (Vehicle *v, void *data)
bool EnsureNoVehicleOnGround (TileIndex tile)
static VehicleGetVehicleTunnelBridgeProc (Vehicle *v, void *data)
 Procedure called for every vehicle found in tunnel/bridge in the hash map.
bool HasVehicleOnTunnelBridge (TileIndex tile, TileIndex endtile, const Vehicle *ignore)
 Finds vehicle in tunnel / bridge.
byte VehicleRandomBits ()
 Get a value for a vehicle's random_bits.
static VehicleVehicleFromHash (int xl, int yl, int xu, int yu, void *data, VehicleFromPosProc *proc, bool find_first)
static VehicleVehicleFromPosXY (int x, int y, void *data, VehicleFromPosProc *proc, bool find_first)
 Helper function for FindVehicleOnPos/HasVehicleOnPos.
void FindVehicleOnPosXY (int x, int y, void *data, VehicleFromPosProc *proc)
 Find a vehicle from a specific location.
bool HasVehicleOnPosXY (int x, int y, void *data, VehicleFromPosProc *proc)
 Checks whether a vehicle in on a specific location.
static VehicleVehicleFromPos (TileIndex tile, void *data, VehicleFromPosProc *proc, bool find_first)
 Helper function for FindVehicleOnPos/HasVehicleOnPos.
void FindVehicleOnPos (TileIndex tile, void *data, VehicleFromPosProc *proc)
 Find a vehicle from a specific location.
bool HasVehicleOnPos (TileIndex tile, void *data, VehicleFromPosProc *proc)
 Checks whether a vehicle in on a specific location.
static void UpdateNewVehiclePosHash (Vehicle *v, bool remove)
static void UpdateVehiclePosHash (Vehicle *v, int x, int y)
void ResetVehiclePosHash ()
void ResetVehicleColourMap ()
void InitializeVehicles ()
uint CountVehiclesInChain (const Vehicle *v)
void VehicleEnteredDepotThisTick (Vehicle *v)
 Adds a vehicle to the list of vehicles, that visited a depot this tick.
static void RunVehicleDayProc ()
 Increases the day counter for all vehicles and calls 1-day and 32-day handlers.
void CallVehicleTicks ()
static void DoDrawVehicle (const Vehicle *v)
void ViewportAddVehicles (DrawPixelInfo *dpi)
VehicleCheckClickOnVehicle (const ViewPort *vp, int x, int y)
void DecreaseVehicleValue (Vehicle *v)
void CheckVehicleBreakdown (Vehicle *v)
void AgeVehicle (Vehicle *v)
uint8 CalcPercentVehicleFilled (const Vehicle *v, StringID *colour)
 Calculates how full a vehicle is.
void VehicleEnterDepot (Vehicle *v)
void VehicleMove (Vehicle *v, bool update_viewport)
 Move a vehicle in the game state; that is moving it's position in the position hashes and marking it's location in the viewport dirty if requested.
void MarkSingleVehicleDirty (const Vehicle *v)
 Marks viewports dirty where the vehicle's image is In fact, it equals BeginVehicleMove(v); EndVehicleMove(v);.
GetNewVehiclePosResult GetNewVehiclePos (const Vehicle *v)
 Get position information of a vehicle when moving one pixel in the direction it is facing.
Direction GetDirectionTowards (const Vehicle *v, int x, int y)
VehicleEnterTileStatus VehicleEnterTile (Vehicle *v, TileIndex tile, int x, int y)
 Call the tile callback function for a vehicle entering a tile.
UnitID GetFreeUnitNumber (VehicleType type)
bool CanBuildVehicleInfrastructure (VehicleType type)
 Check whether we can build infrastructure for the given vehicle type.
const LiveryGetEngineLivery (EngineID engine_type, CompanyID company, EngineID parent_engine_type, const Vehicle *v)
 Determines the livery for a vehicle.
static PaletteID GetEngineColourMap (EngineID engine_type, CompanyID company, EngineID parent_engine_type, const Vehicle *v)
PaletteID GetEnginePalette (EngineID engine_type, CompanyID company)
 Get the colour map for an engine.
PaletteID GetVehiclePalette (const Vehicle *v)
 Get the colour map for a vehicle.
uint GetVehicleCapacity (const Vehicle *v, uint16 *mail_capacity)
 Determines capacity of a given vehicle from scratch.
void StopAllVehicles ()
void VehiclesYearlyLoop ()
bool CanVehicleUseStation (EngineID engine_type, const Station *st)
 Can this station be used by the given engine type?
bool CanVehicleUseStation (const Vehicle *v, const Station *st)
 Can this station be used by the given vehicle?

Variables

VehicleID _vehicle_id_ctr_day
const Vehicle_place_clicked_vehicle
VehicleID _new_vehicle_id
uint16 _returned_refit_capacity
byte _age_cargo_skip_counter
 Skip aging of cargo?
VehiclePool _vehicle_pool ("Vehicle")
const int HASH_BITS = 7
const int HASH_SIZE = 1 << HASH_BITS
const int HASH_MASK = HASH_SIZE - 1
const int TOTAL_HASH_SIZE = 1 << (HASH_BITS * 2)
const int TOTAL_HASH_MASK = TOTAL_HASH_SIZE - 1
const int HASH_RES = 0
static Vehicle_new_vehicle_position_hash [TOTAL_HASH_SIZE]
static Vehicle_vehicle_position_hash [0x1000]
static AutoreplaceMap _vehicles_to_autoreplace
static const byte _breakdown_chance [64]
static const Direction _new_direction_table []

Detailed Description

Base implementations of all vehicles.

Definition in file vehicle.cpp.


Typedef Documentation

typedef SmallMap<Vehicle *, bool, 4> AutoreplaceMap

List of vehicles that should check for autoreplace this tick.

Mapping of vehicle -> leave depot immediatelly after autoreplace.

Definition at line 513 of file vehicle.cpp.


Function Documentation

uint8 CalcPercentVehicleFilled ( const Vehicle v,
StringID colour 
)

Calculates how full a vehicle is.

Parameters:
v The Vehicle to check. For trains, use the first engine.
colour The string to show depending on if we are unloading or loading
Returns:
A percentage of how full the Vehicle is.

Definition at line 950 of file vehicle.cpp.

References Vehicle::cargo, Vehicle::cargo_cap, Vehicle::cargo_type, CargoList< Tinst >::Count(), Vehicle::current_order, SpecializedStation< Station, false >::Get(), Order::GetLoadType(), Station::goods, HasBit(), max(), Vehicle::Next(), OLFB_NO_LOAD, and Vehicle::vehicle_flags.

Referenced by LoadUnloadVehicle(), and ProcessConditionalOrder().

bool CanBuildVehicleInfrastructure ( VehicleType  type  ) 

Check whether we can build infrastructure for the given vehicle type.

This to disable building stations etc. when you are not allowed/able to have the vehicle type yet.

Parameters:
type the vehicle type to check this for
Returns:
true if there is any reason why you may build the infrastructure for the given vehicle type

Definition at line 1250 of file vehicle.cpp.

References _settings_client, _settings_game, GUISettings::always_build_infrastructure, ClientSettings::gui, HasBit(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), max(), VehicleSettings::max_aircraft, VehicleSettings::max_roadveh, VehicleSettings::max_ships, VehicleSettings::max_trains, BaseVehicle::type, VEH_AIRCRAFT, VEH_ROAD, VEH_SHIP, VEH_TRAIN, and GameSettings::vehicle.

Referenced by MainToolbarWindow::OnKeyPress(), MainToolbarWindow::OnPaint(), and BuildDocksToolbarWindow::OnPaint().

bool CanVehicleUseStation ( const Vehicle v,
const Station st 
)

Can this station be used by the given vehicle?

Parameters:
v the vehicle to test
st the station to test for
Returns:
true if and only if the vehicle can use this station.

Definition at line 1833 of file vehicle.cpp.

References CanVehicleUseStation(), SpecializedVehicle< RoadVehicle, VEH_ROAD >::From(), BaseVehicle::type, and VEH_ROAD.

bool CanVehicleUseStation ( EngineID  engine_type,
const Station st 
)

Can this station be used by the given engine type?

Parameters:
engine_type the type of vehicles to test
st the station to test for
Returns:
true if and only if the vehicle of the type can use this station.
Note:
For road vehicles the Vehicle is needed to determine whether it can use the station. This function will return true for road vehicles when at least one of the facilities is available.

Definition at line 1800 of file vehicle.cpp.

References AIR_CTOL, FACIL_AIRPORT, FACIL_BUS_STOP, FACIL_DOCK, FACIL_TRAIN, FACIL_TRUCK_STOP, BaseStation::facilities, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_engine_pool >::GetIfValid(), Engine::type, VEH_AIRCRAFT, VEH_ROAD, VEH_SHIP, and VEH_TRAIN.

Referenced by CanVehicleUseStation(), CheckOrders(), CmdBuildAircraft(), CmdCloneOrder(), CmdInsertOrder(), and RoadVehicle::GetOrderStationLocation().

void FindVehicleOnPos ( TileIndex  tile,
void *  data,
VehicleFromPosProc *  proc 
)

Find a vehicle from a specific location.

It will call proc for ALL vehicles on the tile and YOU must make SURE that the "best one" is stored in the data value and is ALWAYS the same regardless of the order of the vehicles where proc was called on! When you fail to do this properly you create an almost untraceable DESYNC!

Note:
The return value of proc will be ignored.
Use this when you have the intention that all vehicles should be iterated over.
Parameters:
tile The location on the map
data Arbitrary data passed to proc
proc The proc that determines whether a vehicle will be "found".

Definition at line 411 of file vehicle.cpp.

References VehicleFromPos().

Referenced by CheckTrainCollision(), CmdConvertRail(), DeleteLastWagon(), FloodVehicles(), FollowTrainReservation(), GetTrainForReservation(), RoadStop::Entry::Rebuild(), and RemoveRoadStop().

void FindVehicleOnPosXY ( int  x,
int  y,
void *  data,
VehicleFromPosProc *  proc 
)

Find a vehicle from a specific location.

It will call proc for ALL vehicles on the tile and YOU must make SURE that the "best one" is stored in the data value and is ALWAYS the same regardless of the order of the vehicles where proc was called on! When you fail to do this properly you create an almost untraceable DESYNC!

Note:
The return value of proc will be ignored.
Use this when you have the intention that all vehicles should be iterated over.
Parameters:
x The X location on the map
y The Y location on the map
data Arbitrary data passed to proc
proc The proc that determines whether a vehicle will be "found".

Definition at line 351 of file vehicle.cpp.

References VehicleFromPosXY().

Referenced by CheckTrainCollision().

const Livery* GetEngineLivery ( EngineID  engine_type,
CompanyID  company,
EngineID  parent_engine_type,
const Vehicle v 
) [read]
PaletteID GetEnginePalette ( EngineID  engine_type,
CompanyID  company 
)

Get the colour map for an engine.

This used for unbuilt engines in the user interface.

Parameters:
engine_type ID of engine
company ID of company
Returns:
A ready-to-use palette modifier

Definition at line 1439 of file vehicle.cpp.

Referenced by DrawEngineList(), NewsWindow::DrawWidget(), and EnginePreviewWindow::DrawWidget().

GetNewVehiclePosResult GetNewVehiclePos ( const Vehicle v  ) 

Get position information of a vehicle when moving one pixel in the direction it is facing.

Parameters:
v Vehicle to move
Returns:
Position information after the move

Definition at line 1141 of file vehicle.cpp.

References GetNewVehiclePosResult::new_tile, GetNewVehiclePosResult::old_tile, Vehicle::tile, and GetNewVehiclePosResult::y.

Referenced by AircraftController(), DisasterTick_Aircraft(), DisasterTick_Big_Ufo(), DisasterTick_Big_Ufo_Destroyer(), DisasterTick_Submarine(), DisasterTick_Ufo(), DisasterTick_Zeppeliner(), UpdateAirplanesOnNewStation(), and UpdateOldAircraft().

uint GetVehicleCapacity ( const Vehicle v,
uint16 *  mail_capacity 
)
PaletteID GetVehiclePalette ( const Vehicle v  ) 
bool HasVehicleOnPos ( TileIndex  tile,
void *  data,
VehicleFromPosProc *  proc 
)

Checks whether a vehicle in on a specific location.

It will call proc for vehicles until it returns non-NULL.

Note:
Use FindVehicleOnPos when you have the intention that all vehicles should be iterated over.
Parameters:
tile The location on the map
data Arbitrary data passed to proc
proc The proc that determines whether a vehicle will be "found".
Returns:
True if proc returned non-NULL.

Definition at line 426 of file vehicle.cpp.

References VehicleFromPos().

Referenced by CheckRoadBlockedForOvertaking(), EnsureNoTrainOnTrack(), ExploreSegment(), HasVehicleOnTunnelBridge(), TrainApproachingCrossing(), and UpdateLevelCrossing().

bool HasVehicleOnPosXY ( int  x,
int  y,
void *  data,
VehicleFromPosProc *  proc 
)

Checks whether a vehicle in on a specific location.

It will call proc for vehicles until it returns non-NULL.

Note:
Use FindVehicleOnPosXY when you have the intention that all vehicles should be iterated over.
Parameters:
x The X location on the map
y The Y location on the map
data Arbitrary data passed to proc
proc The proc that determines whether a vehicle will be "found".
Returns:
True if proc returned non-NULL.

Definition at line 367 of file vehicle.cpp.

References VehicleFromPosXY().

bool HasVehicleOnTunnelBridge ( TileIndex  tile,
TileIndex  endtile,
const Vehicle ignore 
)

Finds vehicle in tunnel / bridge.

Parameters:
tile first end
endtile second end
ignore Ignore this vehicle when searching
Returns:
true if the bridge has a vehicle

Definition at line 255 of file vehicle.cpp.

References GetVehicleTunnelBridgeProc(), and HasVehicleOnPos().

Referenced by ClearPathReservation(), CmdBuildRoad(), CmdConvertRail(), and RemoveRoad().

void MarkSingleVehicleDirty ( const Vehicle v  ) 

Marks viewports dirty where the vehicle's image is In fact, it equals BeginVehicleMove(v); EndVehicleMove(v);.

Parameters:
v vehicle to mark dirty
See also:
BeginVehicleMove()
EndVehicleMove()

Definition at line 1132 of file vehicle.cpp.

References MarkAllViewportsDirty().

Referenced by Vehicle::Crash(), InitializeDisasterVehicle(), and Vehicle::~Vehicle().

static void RunVehicleDayProc (  )  [static]

Increases the day counter for all vehicles and calls 1-day and 32-day handlers.

Each tick, it processes vehicles with "index % DAY_TICKS == _date_fract", so each day, all vehicles are processes in DAY_TICKS steps.

Definition at line 653 of file vehicle.cpp.

References CALLBACK_FAILED, CBID_VEHICLE_32DAY_CALLBACK, Vehicle::day_counter, DAY_TICKS, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_vehicle_pool >::Get(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_vehicle_pool >::GetPoolSize(), GetVehicleCallback(), HasBit(), and Vehicle::OnNewDay().

void ShowNewGrfVehicleError ( EngineID  engine,
StringID  part1,
StringID  part2,
GRFBugs  bug_type,
bool  critical 
)

Displays a "NewGrf Bug" error message for a engine, and pauses the game if not networking.

Parameters:
engine The engine that caused the problem
part1 Part 1 of the error message, taking the grfname as parameter 1
part2 Part 2 of the error message, taking the engine as parameter 2
bug_type Flag to check and set in grfconfig
critical Shall the "OpenTTD might crash"-message be shown when the player tries to unpause?

Definition at line 195 of file vehicle.cpp.

References _networking, CMD_PAUSE, DC_EXEC, DoCommand(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_engine_pool >::Get(), GRFConfig::grf_bugs, HasBit(), lastof, GRFConfig::name, PM_PAUSED_ERROR, PM_PAUSED_NORMAL, SetBit(), SetDParam(), SetDParamStr(), and ShowErrorMessage().

Referenced by CheckConsistencyOfArticulatedVehicle(), and RailVehicleLengthChanged().

void VehicleEnteredDepotThisTick ( Vehicle v  ) 

Adds a vehicle to the list of vehicles, that visited a depot this tick.

Parameters:
*v vehicle to add

Definition at line 635 of file vehicle.cpp.

References Vehicle::vehstatus.

VehicleEnterTileStatus VehicleEnterTile ( Vehicle v,
TileIndex  tile,
int  x,
int  y 
)

Call the tile callback function for a vehicle entering a tile.

Parameters:
v Vehicle entering the tile
tile Tile entered
x X position
y Y position
Returns:
Some meta-data over the to be entered tile.
See also:
VehicleEnterTileStatus to see what the bits in the return value mean.

Definition at line 1195 of file vehicle.cpp.

References _tile_type_procs, GetTileType(), and TileTypeProcs::vehicle_enter_tile_proc.

static Vehicle* VehicleFromPos ( TileIndex  tile,
void *  data,
VehicleFromPosProc *  proc,
bool  find_first 
) [static]

Helper function for FindVehicleOnPos/HasVehicleOnPos.

Note:
Do not call this function directly!
Parameters:
tile The location on the map
data Arbitrary data passed to proc
proc The proc that determines whether a vehicle will be "found".
find_first Whether to return on the first found or iterate over all vehicles
Returns:
the best matching or first vehicle (depending on find_first).

Definition at line 382 of file vehicle.cpp.

References GB(), Vehicle::tile, TileX(), and TileY().

Referenced by FindVehicleOnPos(), and HasVehicleOnPos().

static Vehicle* VehicleFromPosXY ( int  x,
int  y,
void *  data,
VehicleFromPosProc *  proc,
bool  find_first 
) [static]

Helper function for FindVehicleOnPos/HasVehicleOnPos.

Note:
Do not call this function directly!
Parameters:
x The X location on the map
y The Y location on the map
data Arbitrary data passed to proc
proc The proc that determines whether a vehicle will be "found".
find_first Whether to return on the first found or iterate over all vehicles
Returns:
the best matching or first vehicle (depending on find_first).

Definition at line 324 of file vehicle.cpp.

References GB(), and TILE_SIZE.

Referenced by FindVehicleOnPosXY(), and HasVehicleOnPosXY().

void VehicleMove ( Vehicle v,
bool  update_viewport 
)

Move a vehicle in the game state; that is moving it's position in the position hashes and marking it's location in the viewport dirty if requested.

Parameters:
v vehicle to move
update_viewport whether to dirty the viewport

Definition at line 1097 of file vehicle.cpp.

References Sprite::height, MarkAllViewportsDirty(), max(), min(), RemapCoords(), ST_NORMAL, Sprite::width, Sprite::x_offs, and Sprite::y_offs.

Referenced by AfterLoadVehicles(), CmdBuildAircraft(), CmdBuildRailVehicle(), CmdBuildRailWagon(), CmdBuildRoadVeh(), CmdBuildShip(), DisasterTick_Helicopter_Rotors(), InitializeDisasterVehicle(), and Vehicle::UpdateViewport().

byte VehicleRandomBits (  ) 

Get a value for a vehicle's random_bits.

Returns:
A random value from 0 to 255.

Definition at line 276 of file vehicle.cpp.

References GB().

Referenced by CmdBuildAircraft(), CmdBuildRailVehicle(), CmdBuildRailWagon(), CmdBuildRoadVeh(), and CmdBuildShip().


Variable Documentation

const byte _breakdown_chance[64] [static]
Initial value:
 {
    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,
  150, 170, 190, 210, 230, 250, 250, 250,
}

Definition at line 864 of file vehicle.cpp.

const Direction _new_direction_table[] [static]
Initial value:

Definition at line 1159 of file vehicle.cpp.


Generated on Wed Mar 3 23:32:53 2010 for OpenTTD by  doxygen 1.6.1