# roadveh_cmd.cpp File Reference

`#include "stdafx.h"`
`#include "openttd.h"`
`#include "debug.h"`
`#include "tile_cmd.h"`
`#include "landscape.h"`
`#include "road_map.h"`
`#include "roadveh.h"`
`#include "station_map.h"`
`#include "timetable.h"`
`#include "engine.h"`
`#include "command_func.h"`
`#include "station.h"`
`#include "news.h"`
`#include "pathfind.h"`
`#include "npf.h"`
`#include "player_func.h"`
`#include "player_base.h"`
`#include "depot.h"`
`#include "bridge.h"`
`#include "tunnel_map.h"`
`#include "bridge_map.h"`
`#include "vehicle_gui.h"`
`#include "articulated_vehicles.h"`
`#include "newgrf_callbacks.h"`
`#include "newgrf_engine.h"`
`#include "newgrf_text.h"`
`#include "newgrf_sound.h"`
`#include "yapf/yapf.h"`
`#include "cargotype.h"`
`#include "strings_func.h"`
`#include "tunnelbridge_map.h"`
`#include "functions.h"`
`#include "window_func.h"`
`#include "date_func.h"`
`#include "vehicle_func.h"`
`#include "sound_func.h"`
`#include "variables.h"`
`#include "autoreplace_gui.h"`
`#include "gfx_func.h"`
`#include "settings_type.h"`
`#include "table/strings.h"`
`#include "table/roadveh_movement.h"`

Go to the source code of this file.

## Data Structures
struct | RoadFindDepotData
struct | RoadVehFindData
struct | OvertakeData
struct | FindRoadToChooseData
struct | RoadDriveEntry
## Defines
#define | MKIT(a, b, c, d) ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | ((d & 0xFF) << 0)
#define | return_track(x) { best_track = (Trackdir)x; goto found_best_track; }
## Enumerations
enum | { RDE_NEXT_TILE = 0x80, RDE_TURNED = 0x40, RVC_DEFAULT_START_FRAME = 0, RVC_TURN_AROUND_START_FRAME = 1, RVC_DEPOT_START_FRAME = 6, RVC_START_FRAME_AFTER_LONG_TRAM = 22, RVC_TURN_AROUND_START_FRAME_SHORT_TRAM = 16, RVC_DRIVE_THROUGH_STOP_FRAME = 7 }
## Functions
void | DrawRoadVehEngine (int x, int y, EngineID engine, SpriteID pal)
static CommandCost | EstimateRoadVehCost (EngineID engine_type)
byte | GetRoadVehLength (const Vehicle *v)
void | RoadVehUpdateCache (Vehicle *v)
CommandCost | CmdBuildRoadVeh (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Build a road vehicle.
CommandCost | CmdStartStopRoadVeh (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Start/Stop a road vehicle.
void | ClearSlot (Vehicle *v)
CommandCost | CmdSellRoadVeh (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Sell a road vehicle.
static bool | EnumRoadSignalFindDepot (TileIndex tile, void *data, Trackdir trackdir, uint length)
static const Depot * | FindClosestRoadDepot (const Vehicle *v)
CommandCost | CmdSendRoadVehToDepot (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Send a road vehicle to the depot.
CommandCost | CmdTurnRoadVeh (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Turn a roadvehicle around.
static void | ClearCrashedStation (Vehicle *v)
static void | DeleteLastRoadVeh (Vehicle *v)
static byte | SetRoadVehPosition (Vehicle *v, int x, int y)
static void | RoadVehSetRandomDirection (Vehicle *v)
static void | RoadVehIsCrashed (Vehicle *v)
static void * | EnumCheckRoadVehCrashTrain (Vehicle *v, void *data)
static void | RoadVehCrash (Vehicle *v)
static void | RoadVehCheckTrainCrash (Vehicle *v)
static void | HandleBrokenRoadVeh (Vehicle *v)
static void | ProcessRoadVehOrder (Vehicle *v)
static void | StartRoadVehSound (const Vehicle *v)
static void * | EnumCheckRoadVehClose (Vehicle *v, void *data)
static Vehicle * | RoadVehFindCloseTo (Vehicle *v, int x, int y, Direction dir)
static void | RoadVehArrivesAt (const Vehicle *v, Station *st)
static bool | RoadVehAccelerate (Vehicle *v)
static Direction | RoadVehGetNewDirection (const Vehicle *v, int x, int y)
static Direction | RoadVehGetSlidingDirection (const Vehicle *v, int x, int y)
static void * | EnumFindVehBlockingOvertake (Vehicle *v, void *data)
static bool | CheckRoadBlockedForOvertaking (OvertakeData *od)
| Check if overtaking is possible on a piece of track.
static void | RoadVehCheckOvertake (Vehicle *v, Vehicle *u)
static void | RoadZPosAffectSpeed (Vehicle *v, byte old_z)
static int | PickRandomBit (uint bits)
static bool | EnumRoadTrackFindDist (TileIndex tile, void *data, Trackdir trackdir, uint length)
static NPFFoundTargetData | PerfNPFRouteToStationOrTile (TileIndex tile, Trackdir trackdir, bool ignore_start_tile, NPFFindStationOrTileData *target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes)
static Trackdir | RoadFindPathToDest (Vehicle *v, TileIndex tile, DiagDirection enterdir)
| Returns direction to for a road vehicle to take or INVALID_TRACKDIR if the direction is currently blocked.
static uint | RoadFindPathToStop (const Vehicle *v, TileIndex tile)
static bool | RoadVehLeaveDepot (Vehicle *v, bool first)
static Trackdir | FollowPreviousRoadVehicle (const Vehicle *v, const Vehicle *prev, TileIndex tile, DiagDirection entry_dir, bool already_reversed)
static bool | CanBuildTramTrackOnTile (PlayerID p, TileIndex t, RoadBits r)
| Can a tram track build without destruction on the given tile?
static bool | IndividualRoadVehicleController (Vehicle *v, const Vehicle *prev)
static void | RoadVehController (Vehicle *v)
static void | AgeRoadVehCargo (Vehicle *v)
static void | CheckIfRoadVehNeedsService (Vehicle *v)
void | RoadVehiclesYearlyLoop ()
CommandCost | CmdRefitRoadVeh (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Refit a road vehicle to the specified cargo type.
## Variables
static const uint16 | _roadveh_images [63]
static const uint16 | _roadveh_full_adder [63]
static const TrackdirBits | _road_enter_dir_to_reachable_trackdirs [DIAGDIR_END]
| 'Convert' the DiagDirection where a road vehicle enters to the trackdirs it can drive onto
static const Trackdir | _road_reverse_table [DIAGDIR_END]
static const TrackdirBits | _road_exit_dir_to_incoming_trackdirs [DIAGDIR_END]
| 'Convert' the DiagDirection where a road vehicle should exit to the trackdirs it can use to drive to the exit direction
static const Trackdir | _roadveh_depot_exit_trackdir [DIAGDIR_END]
| Converts the exit direction of a depot to trackdir the vehicle is going to drive to.
static const DiagDirection | _road_pf_directions []
static const byte | _road_veh_data_1 []

## Detailed Description

Definition in file roadveh_cmd.cpp.

## Function Documentation

static bool CanBuildTramTrackOnTile | ( | PlayerID | p,
|  | TileIndex | t,
|  | RoadBits | r |
| ) |  |  | `[static]`

Can a tram track build without destruction on the given tile?

Parameters:

  | p | the player that would be building the tram tracks
  | t | the tile to build on.
  | r | the road bits needed.

Returns:
  true when a track track can be build on 't'

Definition at line 1486 of file roadveh_cmd.cpp.

References CMD_BUILD_ROAD, CmdSucceeded(), DoCommand(), and ROADTYPE_TRAM.

static bool CheckRoadBlockedForOvertaking | ( | OvertakeData * | od | ) | `[static]`

Check if overtaking is possible on a piece of track.

Parameters:

  | od | Information about the tile and the involved vehicles

Returns:
  true if we have to abort overtaking

Definition at line 1037 of file roadveh_cmd.cpp.

References HasBit(), HasVehicleOnPos(), TRACK_BIT_CROSS, TRACKDIR_BIT_NONE, TrackdirBitsToTrackBits(), TrackStatusToRedSignals(), and TrackStatusToTrackdirBits().

CommandCost CmdBuildRoadVeh | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Build a road vehicle.

Parameters:

  | tile | tile of depot where road vehicle is built
  | flags | operation to perform
  | p1 | bus/truck type being built (engine)
  | p2 | unused

Definition at line 168 of file roadveh_cmd.cpp.

References CMD_ERROR, DC_AUTOREPLACE, DC_EXEC, DC_QUERY_COST, DiagDirToDir(), EF_ROAD_TRAM, ENGINE_EXCLUSIVE_PREVIEW, CommandCost::GetCost(), HasBit(), InvalidateAutoreplaceWindow(), IsEngineBuildable(), IsTileDepotType(), IsTileOwner(), return_cmd_error, ROADTYPE_ROAD, ROADTYPE_TRAM, RoadTypeToRoadTypes(), RoadVehicle::RoadVehicle(), RVSB_IN_DEPOT, SetBit(), TILE_SIZE, TileX(), TileY(), and VehicleRandomBits().

CommandCost CmdRefitRoadVeh | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Refit a road vehicle to the specified cargo type.

Parameters:

  | tile | unused
  | flags | operation to perform
  | p1 | Vehicle ID of the vehicle to refit
  | p2 | Bitstuffed elements p2 = (bit 0-7) - the new cargo type to refit to p2 = (bit 8-15) - the new cargo subtype to refit to p2 = (bit 16) - refit only this vehicle

Returns:
  cost of refit or error

Definition at line 2138 of file roadveh_cmd.cpp.

References CommandCost::AddCost(), CALLBACK_FAILED, CanRefitTo(), CBID_VEHICLE_REFIT_CAPACITY, CBM_VEHICLE_REFIT_CAPACITY, CMD_ERROR, DC_EXEC, GB(), GetRefitCost(), GetVehicleCallback(), HasBit(), IsValidVehicleID(), and return_cmd_error.

CommandCost CmdSellRoadVeh | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Sell a road vehicle.

Parameters:

  | tile | unused
  | flags | operation to perform
  | p1 | vehicle ID to be sold
  | p2 | unused

Definition at line 356 of file roadveh_cmd.cpp.

References CMD_ERROR, DC_EXEC, DeleteDepotHighlightOfVehicle(), HASBITS, IsValidVehicleID(), and return_cmd_error.

CommandCost CmdSendRoadVehToDepot | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Send a road vehicle to the depot.

Parameters:

  | tile | unused
  | flags | operation to perform
  | p1 | vehicle ID to send to the depot
  | p2 | various bitmasked elements p2 bit 0-3 - DEPOT_ flags (see vehicle.h) p2 bit 8-10 - VLW flag (for mass goto depot)

Definition at line 455 of file roadveh_cmd.cpp.

References ClrBit(), CMD_ERROR, DC_EXEC, HasBit(), IsValidVehicleID(), OFB_NON_STOP, return_cmd_error, SendAllVehiclesToDepot(), SetBit(), and ToggleBit().

CommandCost CmdStartStopRoadVeh | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Start/Stop a road vehicle.

Parameters:

  | tile | unused
  | flags | operation to perform
  | p1 | road vehicle ID to start/stop
  | p2 | unused

Definition at line 293 of file roadveh_cmd.cpp.

References CALLBACK_FAILED, CBID_VEHICLE_START_STOP_CHECK, CMD_ERROR, DC_EXEC, DeleteVehicleNews(), error, GB(), GetEngineGRFID(), GetGRFStringID(), GetVehicleCallback(), IsValidVehicleID(), and return_cmd_error.

CommandCost CmdTurnRoadVeh | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Turn a roadvehicle around.

Parameters:

  | tile | unused
  | flags | operation to perform
  | p1 | vehicle ID to turn
  | p2 | unused

Definition at line 529 of file roadveh_cmd.cpp.

References CMD_ERROR, DC_EXEC, DirToDiagDir(), DRD_NONE, GetDisallowedRoadDirections(), GetTunnelBridgeDirection(), IsTileType(), IsValidVehicleID(), MP_TUNNELBRIDGE, and RVSB_WORMHOLE.

static Trackdir RoadFindPathToDest | ( | Vehicle * | v,
|  | TileIndex | tile,
|  | DiagDirection | enterdir |
| ) |  |  | `[static]`

Returns direction to for a road vehicle to take or INVALID_TRACKDIR if the direction is currently blocked.

Parameters:

  | v | the Vehicle to do the pathfinding for
  | tile | the where to start the pathfinding
  | enterdir | the direction the vehicle enters the tile from

Returns:
  the Trackdir to take

Definition at line 1162 of file roadveh_cmd.cpp.

References _road_enter_dir_to_reachable_trackdirs, _road_exit_dir_to_incoming_trackdirs, RoadStop::BUS, CC_PASSENGERS, DiagdirToDiagTrackdir(), FindFirstBit2x64(), FOR_EACH_SET_BIT, GetRoadStopDir(), HasBit(), INVALID_RAILTYPES, INVALID_TRACKDIR, IsTileOwner(), IsTileType(), KillFirstBit(), MP_ROAD, MP_STATION, TileOffsByDiagDir(), TRACKDIR_BIT_NONE, TrackStatusToRedSignals(), TrackStatusToTrackdirBits(), RoadStop::TRUCK, VPF_NPF, VPF_OPF, VPF_YAPF, and YapfChooseRoadTrack().

## Variable Documentation

const TrackdirBits _road_enter_dir_to_reachable_trackdirs[DIAGDIR_END]`[static]`

Initial value:

     {
      TRACKDIR_BIT_LEFT_N  | TRACKDIR_BIT_LOWER_E | TRACKDIR_BIT_X_NE,
      TRACKDIR_BIT_LEFT_S  | TRACKDIR_BIT_UPPER_E | TRACKDIR_BIT_Y_SE,
      TRACKDIR_BIT_UPPER_W | TRACKDIR_BIT_X_SW    | TRACKDIR_BIT_RIGHT_S,
      TRACKDIR_BIT_RIGHT_N | TRACKDIR_BIT_LOWER_W | TRACKDIR_BIT_Y_NW
    }

'Convert' the DiagDirection where a road vehicle enters to the trackdirs it can drive onto

Definition at line 71 of file roadveh_cmd.cpp.

Referenced by RoadFindPathToDest().

const TrackdirBits _road_exit_dir_to_incoming_trackdirs[DIAGDIR_END]`[static]`

Initial value:

     {
      TRACKDIR_BIT_LOWER_W | TRACKDIR_BIT_X_SW    | TRACKDIR_BIT_LEFT_S,
      TRACKDIR_BIT_LEFT_N  | TRACKDIR_BIT_UPPER_W | TRACKDIR_BIT_Y_NW,
      TRACKDIR_BIT_RIGHT_N | TRACKDIR_BIT_UPPER_E | TRACKDIR_BIT_X_NE,
      TRACKDIR_BIT_RIGHT_S | TRACKDIR_BIT_LOWER_E | TRACKDIR_BIT_Y_SE
    }

'Convert' the DiagDirection where a road vehicle should exit to the trackdirs it can use to drive to the exit direction

Definition at line 84 of file roadveh_cmd.cpp.

Referenced by RoadFindPathToDest().

const DiagDirection _road_pf_directions[]`[static]`

Initial value:

     {
      DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_SW, DIAGDIR_SE, INVALID_DIAGDIR, INVALID_DIAGDIR,
      DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NE, INVALID_DIAGDIR, INVALID_DIAGDIR
    }

Definition at line 393 of file roadveh_cmd.cpp.

const Trackdir _road_reverse_table[DIAGDIR_END]`[static]`

Initial value:

     {
      TRACKDIR_RVREV_NE, TRACKDIR_RVREV_SE, TRACKDIR_RVREV_SW, TRACKDIR_RVREV_NW
    }

Definition at line 78 of file roadveh_cmd.cpp.

const byte _road_veh_data_1[]`[static]`

Initial value:

     {
      20, 20, 16, 16, 0, 0, 0, 0,
      19, 19, 15, 15, 0, 0, 0, 0,
      16, 16, 12, 12, 0, 0, 0, 0,
      15, 15, 11, 11
    }

Definition at line 1364 of file roadveh_cmd.cpp.

const Trackdir _roadveh_depot_exit_trackdir[DIAGDIR_END]`[static]`

Initial value:

     {
      TRACKDIR_X_NE, TRACKDIR_Y_SE, TRACKDIR_X_SW, TRACKDIR_Y_NW
    }

Converts the exit direction of a depot to trackdir the vehicle is going to drive to.

Definition at line 92 of file roadveh_cmd.cpp.

const uint16 _roadveh_full_adder[63]`[static]`

Initial value:

     {
       0,  88,   0,   0,   0,   0,  48,  48,
      48,  48,   0,   0,  64,  64,   0,  16,
      16,   0,  88,   0,   0,   0,   0,  48,
      48,  48,  48,   0,   0,  64,  64,   0,
      16,  16,   0,  88,   0,   0,   0,   0,
      48,  48,  48,  48,   0,   0,  64,  64,
       0,  16,  16,   0,   8,   8,   8,   8,
       0,   0,   0,   8,   8,   8,   8
    }

Definition at line 59 of file roadveh_cmd.cpp.

const uint16 _roadveh_images[63]`[static]`

Initial value:

     {
      0xCD4, 0xCDC, 0xCE4, 0xCEC, 0xCF4, 0xCFC, 0xD0C, 0xD14,
      0xD24, 0xD1C, 0xD2C, 0xD04, 0xD1C, 0xD24, 0xD6C, 0xD74,
      0xD7C, 0xC14, 0xC1C, 0xC24, 0xC2C, 0xC34, 0xC3C, 0xC4C,
      0xC54, 0xC64, 0xC5C, 0xC6C, 0xC44, 0xC5C, 0xC64, 0xCAC,
      0xCB4, 0xCBC, 0xD94, 0xD9C, 0xDA4, 0xDAC, 0xDB4, 0xDBC,
      0xDCC, 0xDD4, 0xDE4, 0xDDC, 0xDEC, 0xDC4, 0xDDC, 0xDE4,
      0xE2C, 0xE34, 0xE3C, 0xC14, 0xC1C, 0xC2C, 0xC3C, 0xC4C,
      0xC5C, 0xC64, 0xC6C, 0xC74, 0xC84, 0xC94, 0xCA4
    }

Definition at line 48 of file roadveh_cmd.cpp.