# vehicle.cpp File Reference

Base implementations of all vehicles. More...

`#include "stdafx.h"`
`#include "openttd.h"`
`#include "roadveh.h"`
`#include "ship.h"`
`#include "spritecache.h"`
`#include "landscape.h"`
`#include "timetable.h"`
`#include "viewport_func.h"`
`#include "gfx_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 "newgrf_text.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 "string_func.h"`
`#include "oldpool_func.h"`
`#include "depot_map.h"`
`#include "ai/ai.hpp"`
`#include "core/smallmap_type.hpp"`
`#include "vehiclelist.h"`
`#include "depot_func.h"`
`#include "settings_type.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)
StringID | VehicleInTheWayErrMsg (const Vehicle *v)
static Vehicle * | EnsureNoVehicleProcZ (Vehicle *v, void *data)
bool | EnsureNoVehicleOnGround (TileIndex tile)
static Vehicle * | GetVehicleTunnelBridgeProc (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.
static void | UpdateVehiclePosHash (Vehicle *v, int x, int y)
void | VehiclePositionChanged (Vehicle *v)
byte | VehicleRandomBits ()
| Get a value for a vehicle's random_bits.
static Vehicle * | VehicleFromHash (int xl, int yl, int xu, int yu, void *data, VehicleFromPosProc *proc, bool find_first)
static Vehicle * | VehicleFromPosXY (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 Vehicle * | VehicleFromPos (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)
void | ResetVehiclePosHash ()
void | ResetVehicleColourMap ()
void | InitializeVehicles ()
Vehicle * | GetLastVehicleInChain (Vehicle *v)
const Vehicle * | GetLastVehicleInChain (const Vehicle *v)
uint | CountVehiclesInChain (const Vehicle *v)
bool | IsEngineCountable (const Vehicle *v)
| Check if a vehicle is counted in num_engines in each company struct.
void | VehicleEnteredDepotThisTick (Vehicle *v)
| Adds a vehicle to the list of vehicles, that visited a depot this tick.
void | CallVehicleTicks ()
bool | CanRefitTo (EngineID engine_type, CargoID cid_to)
| Check if a given engine type can be refitted to a given cargo.
CargoID | FindFirstRefittableCargo (EngineID engine_type)
| Find the first cargo type that an engine can be refitted to.
CommandCost | GetRefitCost (EngineID engine_type)
| Learn the price of refitting a certain engine.
static void | DoDrawVehicle (const Vehicle *v)
void | ViewportAddVehicles (DrawPixelInfo *dpi)
Vehicle * | CheckClickOnVehicle (const ViewPort *vp, int x, int y)
void | CheckVehicle32Day (Vehicle *v)
void | DecreaseVehicleValue (Vehicle *v)
void | CheckVehicleBreakdown (Vehicle *v)
void | AgeVehicle (Vehicle *v)
CommandCost | CmdStartStopVehicle (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Start/Stop a vehicle.
CommandCost | CmdMassStartStopVehicle (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Starts or stops a lot of vehicles.
CommandCost | CmdDepotSellAllVehicles (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Sells all vehicles in a depot.
CommandCost | CmdDepotMassAutoReplace (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Autoreplace all vehicles in the depot Note: this command can make incorrect cost estimations Luckily the final price can only drop, not increase.
static bool | IsUniqueVehicleName (const char *name)
| Test if a name is unique among vehicle names.
static void | CloneVehicleName (const Vehicle *src, Vehicle *dst)
| Clone the custom name of a vehicle, adding or incrementing a number.
CommandCost | CmdCloneVehicle (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Clone a vehicle.
CommandCost | SendAllVehiclesToDepot (VehicleType type, DoCommandFlag flags, bool service, Owner owner, uint16 vlw_flag, uint32 id)
| Send all vehicles of type to depots.
uint8 | CalcPercentVehicleFilled (const Vehicle *v, StringID *colour)
| Calculates how full a vehicle is.
void | VehicleEnterDepot (Vehicle *v)
CommandCost | CmdRenameVehicle (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Give a custom name to your vehicle.
CommandCost | CmdChangeServiceInt (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Change the service interval of a vehicle.
void | BeginVehicleMove (const Vehicle *v)
| Stores the vehicle image coords for later call to EndVehicleMove().
void | EndVehicleMove (const Vehicle *v)
| Marks screen dirty after a vehicle has moved.
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)
Trackdir | GetVehicleTrackdir (const Vehicle *v)
| Returns the Trackdir on which the vehicle is currently located.
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 Livery * | GetEngineLivery (EngineID engine_type, CompanyID company, EngineID parent_engine_type, const Vehicle *v)
static SpriteID | GetEngineColourMap (EngineID engine_type, CompanyID company, EngineID parent_engine_type, const Vehicle *v)
SpriteID | GetEnginePalette (EngineID engine_type, CompanyID company)
| Get the colour map for an engine.
SpriteID | GetVehiclePalette (const Vehicle *v)
| Get the colour map for a vehicle.
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
const uint32 | _veh_build_proc_table []
const uint32 | _veh_sell_proc_table []
const uint32 | _veh_refit_proc_table []
const uint32 | _send_to_depot_proc_table []
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 Rect | _old_vehicle_coords
| coords of vehicle before it has moved
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 492 of file vehicle.cpp.

## Function Documentation

void BeginVehicleMove | ( | const Vehicle * | v | ) |

Stores the vehicle image coords for later call to EndVehicleMove().

Parameters:

  | v | vehicle which image's coords to store

See also:
  _old_vehicle_coords

  EndVehicleMove()

Definition at line 1642 of file vehicle.cpp.

References Rect::bottom, Rect::left, Rect::right, and Rect::top.

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 1442 of file vehicle.cpp.

References Station::goods, HasBit(), max(), and OUFB_UNLOAD.

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 1836 of file vehicle.cpp.

References _settings_client, _settings_game, GUISettings::always_build_infrastructure, ClientSettings::gui, HasBit(), max(), VehicleSettings::max_aircraft, VehicleSettings::max_roadveh, VehicleSettings::max_ships, VehicleSettings::max_trains, and GameSettings::vehicle.

bool CanRefitTo | ( | EngineID | engine_type,
|  | CargoID | cid_to |
| ) |  |  |

Check if a given engine type can be refitted to a given cargo.

Parameters:

  | engine_type | Engine type to check
  | cid_to | check refit to this cargo-type

Returns:
  true if it is possible, false otherwise

Definition at line 716 of file vehicle.cpp.

References HasBit().

Referenced by AIEngine::CanRefitCargo(), CmdRefitAircraft(), CmdRefitRailVehicle(), CmdRefitRoadVeh(), and CmdRefitShip().

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 2335 of file vehicle.cpp.

References CanVehicleUseStation(), and Station::GetPrimaryRoadStop().

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 2302 of file vehicle.cpp.

References AIR_CTOL, Station::Airport(), Station::facilities, and AirportFTAClass::flags.

Referenced by CanVehicleUseStation(), CmdBuildAircraft(), CmdCloneOrder(), and CmdInsertOrder().

static void CloneVehicleName | ( | const Vehicle * | src,
|  | Vehicle * | dst |
| ) |  |  | `[static]`

Clone the custom name of a vehicle, adding or incrementing a number.

Parameters:

  | src | Source vehicle, with a custom name.
  | dst | Destination vehicle.

Definition at line 1193 of file vehicle.cpp.

References buf, IsUniqueVehicleName(), lastof, seprintf(), strecat(), and strecpy().

Referenced by CmdCloneVehicle().

CommandCost CmdChangeServiceInt | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Change the service interval of a vehicle.

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | vehicle ID that is being service-interval-changed
  | p2 | new service interval

Definition at line 1615 of file vehicle.cpp.

References CMD_ERROR, DC_EXEC, GetServiceIntervalClamped(), InvalidateWindow(), and IsValidVehicleID().

CommandCost CmdCloneVehicle | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Clone a vehicle.

If it is a train, it will clone all the cars too

Parameters:

  | tile | tile of the depot where the cloned vehicle is build
  | flags | type of operation
  | p1 | the original vehicle's index
  | p2 | 1 = shared orders, else copied orders

Definition at line 1241 of file vehicle.cpp.

References CommandCost::AddCost(), CloneVehicleName(), CMD_ADD_VEHICLE_GROUP, CMD_CLONE_ORDER, CMD_ERROR, CMD_MOVE_RAIL_VEHICLE, CmdFailed(), CmdSucceeded(), DC_EXEC, DoCommand(), EngineHasArticPart(), GetNextArticPart(), GetNextVehicle(), GetRefitCost(), HasBit(), IsFrontEngine(), IsRearDualheaded(), IsValidVehicleID(), return_cmd_error, and SetBit().

CommandCost CmdDepotMassAutoReplace | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Autoreplace all vehicles in the depot Note: this command can make incorrect cost estimations Luckily the final price can only drop, not increase.

This is due to the fact that estimation can't predict wagon removal so it presumes worst case which is no income from selling wagons.

Parameters:

  | tile | Tile of the depot where the vehicles are
  | flags | type of operation
  | p1 | Type of vehicle
  | p2 | If bit 0 is set, then either replace all or nothing (instead of replacing until money runs out)

Definition at line 1128 of file vehicle.cpp.

References CommandCost::AddCost(), BuildDepotVehicleList(), CMD_AUTOREPLACE_VEHICLE, CMD_ERROR, CmdSucceeded(), DC_EXEC, DoCommand(), GB(), CommandCost::GetErrorMessage(), HasBit(), IsDepotTile(), IsTileOwner(), and SmallVector< T, S >::Length().

CommandCost CmdDepotSellAllVehicles | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Sells all vehicles in a depot.

Parameters:

  | tile | Tile of the depot where the depot is
  | flags | type of operation
  | p1 | Vehicle type
  | p2 | unused

Definition at line 1091 of file vehicle.cpp.

References CommandCost::AddCost(), BuildDepotVehicleList(), CMD_ERROR, CMD_SELL_AIRCRAFT, CMD_SELL_RAIL_WAGON, CMD_SELL_ROAD_VEH, CMD_SELL_SHIP, CmdSucceeded(), DoCommand(), GB(), CommandCost::GetCost(), and SmallVector< T, S >::Length().

CommandCost CmdMassStartStopVehicle | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Starts or stops a lot of vehicles.

Parameters:

  | tile | Tile of the depot where the vehicles are started/stopped (only used for depots)
  | flags | type of operation
  | p1 | Station/Order/Depot ID (only used for vehicle list windows)

  p2

  bitmask
  - 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
  - bit 8-11 Vehicle List Window type (ignored unless bit 1 is set)

Definition at line 1041 of file vehicle.cpp.

References BuildDepotVehicleList(), CMD_ERROR, CMD_START_STOP_VEHICLE, CmdSucceeded(), DC_EXEC, DoCommand(), GB(), GenerateVehicleSortList(), HasBit(), and SmallVector< T, S >::Length().

CommandCost CmdRenameVehicle | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Give a custom name to your vehicle.

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | vehicle ID to name
  | p2 | unused

Definition at line 1584 of file vehicle.cpp.

References CMD_ERROR, DC_AUTOREPLACE, DC_EXEC, InvalidateWindowClassesData(), IsUniqueVehicleName(), IsValidVehicleID(), MarkWholeScreenDirty(), MAX_LENGTH_VEHICLE_NAME_BYTES, return_cmd_error, and StrEmpty().

CommandCost CmdStartStopVehicle | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Start/Stop a vehicle.

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | vehicle to start/stop
  | p2 | bit 0: Shall the start/stop newgrf callback be evaluated (only valid with DC_AUTOREPLACE for network safety)

Returns:
  result of operation. Nothing if everything went well

Definition at line 968 of file vehicle.cpp.

References CALLBACK_FAILED, CBID_VEHICLE_START_STOP_CHECK, CMD_ERROR, DC_AUTOREPLACE, DC_EXEC, DeleteVehicleNews(), error, GB(), GetEngineGRFID(), GetGRFStringID(), GetVehicleCallback(), HasBit(), InvalidateWindow(), InvalidateWindowClasses(), InvalidateWindowWidget(), IsValidVehicleID(), return_cmd_error, and SetBit().

void EndVehicleMove | ( | const Vehicle * | v | ) |

Marks screen dirty after a vehicle has moved.

Parameters:

  | v | vehicle which is marked dirty

See also:
  _old_vehicle_coords

  BeginVehicleMove()

Definition at line 1656 of file vehicle.cpp.

References Rect::bottom, Rect::left, MarkAllViewportsDirty(), max(), min(), Rect::right, and Rect::top.

CargoID FindFirstRefittableCargo | ( | EngineID | engine_type | ) |

Find the first cargo type that an engine can be refitted to.

Parameters:

  | engine_type | Which engine to find cargo for.

Returns:
  A climate dependent cargo type. CT_INVALID is returned if not refittable.

Definition at line 725 of file vehicle.cpp.

References HasBit().

Referenced by CalculateRefitMasks().

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 371 of file vehicle.cpp.

References VehicleFromPos().

Referenced by CheckTrainCollision(), CmdConvertRail(), DeleteLastWagon(), FloodVehicles(), GetTrainForReservation(), 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 311 of file vehicle.cpp.

References VehicleFromPosXY().

Referenced by CheckTrainCollision().

SpriteID 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 2014 of file vehicle.cpp.

Referenced by DrawEngineList().

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 1683 of file vehicle.cpp.

References GetNewVehiclePosResult::new_tile, GetNewVehiclePosResult::old_tile, GetNewVehiclePosResult::x, and GetNewVehiclePosResult::y.

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

CommandCost GetRefitCost | ( | EngineID | engine_type | ) |

Learn the price of refitting a certain engine.

Parameters:

  | engine_type | Which engine to refit

Returns:
  Price for refitting

Definition at line 742 of file vehicle.cpp.

References RAILVEH_WAGON.

Referenced by CmdCloneVehicle(), CmdRefitAircraft(), CmdRefitRailVehicle(), CmdRefitRoadVeh(), and CmdRefitShip().

SpriteID GetVehiclePalette | ( | const Vehicle * | v | ) |

Get the colour map for a vehicle.

Parameters:

  | v | Vehicle to get colour map for

Returns:
  A ready-to-use palette modifier

Definition at line 2019 of file vehicle.cpp.

Trackdir GetVehicleTrackdir | ( | const Vehicle * | v | ) |

Returns the Trackdir on which the vehicle is currently located.

Works for trains and ships. Currently works only sortof for road vehicles, since they have a fuzzy concept of being "on" a trackdir. Dunno really what it returns for a road vehicle that is halfway a tile, never really understood that part. For road vehicles that are at the beginning or end of the tile, should just return the diagonal trackdir on which they are driving. I _think_. For other vehicles types, or vehicles with no clear trackdir (such as those in depots), returns 0xFF.

Definition at line 1728 of file vehicle.cpp.

References DiagDirToDiagTrackdir(), DirToDiagDir(), FindFirstTrack(), GetRailDepotDirection(), GetRoadStopDir(), INVALID_TRACKDIR, IsReversingRoadTrackdir(), TRACK_BIT_DEPOT, TRACK_BIT_WORMHOLE, and TrackDirectionToTrackdir().

Referenced by CheckNextTrainTile(), ChooseShipTrack(), CmdBuildRailroadStation(), CmdRemoveFromRailroadStation(), FindClosestTrainDepot(), FollowTrainReservation(), FreeTrainTrackReservation(), YapfCheckReverseTrain(), YapfFindNearestRailDepotTwoWay(), and YapfFindNearestRoadDepot().

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 386 of file vehicle.cpp.

References VehicleFromPos().

Referenced by CheckRoadBlockedForOvertaking(), EnsureNoTrainOnTrack(), ExploreSegment(), FollowTrainReservation(), 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 327 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 180 of file vehicle.cpp.

References GetVehicleTunnelBridgeProc(), and HasVehicleOnPos().

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

bool IsEngineCountable | ( | const Vehicle * | v | ) |

Check if a vehicle is counted in num_engines in each company struct.

Parameters:

  | *v | Vehicle to test

Returns:
  true if the vehicle is counted in num_engines

Definition at line 527 of file vehicle.cpp.

References IsArticulatedPart(), IsNormalAircraft(), and IsRearDualheaded().

Referenced by CmdAddVehicleGroup(), SetCachedEngineCounts(), SetTrainGroupID(), and UpdateTrainGroupID().

static bool IsUniqueVehicleName | ( | const char * | name | ) | `[static]`

Test if a name is unique among vehicle names.

Parameters:

  | name | Name to test.

Returns:
  True ifffffff the name is unique.

Definition at line 1178 of file vehicle.cpp.

Referenced by CloneVehicleName(), and CmdRenameVehicle().

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 1674 of file vehicle.cpp.

References MarkAllViewportsDirty().

Referenced by DisasterTick_Helicopter_Rotors(), DisasterTick_Submarine(), DisasterTick_Ufo(), and InitializeDisasterVehicle().

CommandCost SendAllVehiclesToDepot | ( | VehicleType | type,
|  | DoCommandFlag | flags,
|  | bool | service,
|  | Owner | owner,
|  | uint16 | vlw_flag,
|  | uint32 | id |
| ) |  |  |

Send all vehicles of type to depots.

Parameters:

  | type | type of vehicle
  | flags | the flags used for DoCommand()
  | service | should the vehicles only get service in the depots
  | owner | owner of the vehicles to send
  | vlw_flag | tells what kind of list requested the goto depot

Returns:
  0 for success and CMD_ERROR if no vehicle is able to go to depot

Definition at line 1413 of file vehicle.cpp.

References CMD_ERROR, CmdSucceeded(), DC_EXEC, DEPOT_DONT_CANCEL, DoCommand(), GenerateVehicleSortList(), and SmallVector< T, S >::Length().

Referenced by CmdSendAircraftToHangar(), CmdSendRoadVehToDepot(), CmdSendShipToDepot(), and CmdSendTrainToDepot().

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 617 of file vehicle.cpp.

References ODATFB_HALT.

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 1781 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 342 of file vehicle.cpp.

References GB(), 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 284 of file vehicle.cpp.

References GB(), and TILE_SIZE.

Referenced by FindVehicleOnPosXY(), and HasVehicleOnPosXY().

byte VehicleRandomBits | ( |  | ) |

Get a value for a vehicle's random_bits.

Returns:
  A random value from 0 to 255.

Definition at line 220 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 881 of file vehicle.cpp.

const Direction _new_direction_table[]`[static]`

Initial value:

     {
      DIR_N , DIR_NW, DIR_W ,
      DIR_NE, DIR_SE, DIR_SW,
      DIR_E , DIR_SE, DIR_S
    }

Definition at line 1701 of file vehicle.cpp.

const uint32 _send_to_depot_proc_table[]

Initial value:

     {
      CMD_SEND_TRAIN_TO_DEPOT,
      CMD_SEND_ROADVEH_TO_DEPOT,
      CMD_SEND_SHIP_TO_DEPOT,
      CMD_SEND_AIRCRAFT_TO_HANGAR,
    }

Definition at line 75 of file vehicle.cpp.

const uint32 _veh_build_proc_table[]

Initial value:

     {
      CMD_BUILD_RAIL_VEHICLE,
      CMD_BUILD_ROAD_VEH,
      CMD_BUILD_SHIP,
      CMD_BUILD_AIRCRAFT,
    }

Definition at line 55 of file vehicle.cpp.

const uint32 _veh_refit_proc_table[]

Initial value:

     {
      CMD_REFIT_RAIL_VEHICLE,
      CMD_REFIT_ROAD_VEH,
      CMD_REFIT_SHIP,
      CMD_REFIT_AIRCRAFT,
    }

Definition at line 68 of file vehicle.cpp.

const uint32 _veh_sell_proc_table[]

Initial value:

     {
      CMD_SELL_RAIL_WAGON,
      CMD_SELL_ROAD_VEH,
      CMD_SELL_SHIP,
      CMD_SELL_AIRCRAFT,
    }

Definition at line 61 of file vehicle.cpp.