# roadveh_cmd.cpp File Reference

Handling of road vehicles. More...

`#include "stdafx.h"`
`#include "landscape.h"`
`#include "roadveh.h"`
`#include "station_map.h"`
`#include "command_func.h"`
`#include "news_func.h"`
`#include "pathfind.h"`
`#include "npf.h"`
`#include "company_func.h"`
`#include "vehicle_gui.h"`
`#include "articulated_vehicles.h"`
`#include "newgrf_engine.h"`
`#include "newgrf_sound.h"`
`#include "yapf/yapf.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 "ai/ai.hpp"`
`#include "depot_base.h"`
`#include "effectvehicle_func.h"`
`#include "settings_type.h"`
`#include "table/strings.h"`
`#include "table/sprites.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; }

## Functions

static SpriteID | GetRoadVehIcon (EngineID engine)
void | DrawRoadVehEngine (int x, int y, EngineID engine, SpriteID pal)
byte | GetRoadVehLength (const Vehicle *v)
void | RoadVehUpdateCache (Vehicle *v)
CommandCost | CmdBuildRoadVeh (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Build a road vehicle.
void | ClearSlot (Vehicle *v)
CommandCost | CmdSellRoadVeh (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| 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, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Send a road vehicle to the depot.
CommandCost | CmdTurnRoadVeh (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Turn a roadvehicle around.
static void | ClearCrashedStation (Vehicle *v)
static void | DeleteLastRoadVeh (Vehicle *v)
static byte | SetRoadVehPosition (Vehicle *v, int x, int y, bool turned)
static void | RoadVehSetRandomDirection (Vehicle *v)
static void | RoadVehIsCrashed (Vehicle *v)
static Vehicle * | EnumCheckRoadVehCrashTrain (Vehicle *v, void *data)
static void | RoadVehCrash (Vehicle *v)
static bool | RoadVehCheckTrainCrash (Vehicle *v)
static void | HandleBrokenRoadVeh (Vehicle *v)
static void | StartRoadVehSound (const Vehicle *v)
static Vehicle * | 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 int | RoadVehAccelerate (Vehicle *v)
static Direction | RoadVehGetNewDirection (const Vehicle *v, int x, int y)
static Direction | RoadVehGetSlidingDirection (const Vehicle *v, int x, int y)
static Vehicle * | 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 (CompanyID c, 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)
CommandCost | CmdRefitRoadVeh (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| 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

Handling of road vehicles.

Definition in file roadveh_cmd.cpp.

## Function Documentation

static bool CanBuildTramTrackOnTile | ( | CompanyID | c,
|  | TileIndex | t,
|  | RoadBits | r |
| ) |  |  | `[static]`

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

Parameters:

  | c | the company 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 1322 of file roadveh_cmd.cpp.

References CMD_BUILD_ROAD, CmdSucceeded(), DC_NONE, 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 885 of file roadveh_cmd.cpp.

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

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

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 163 of file roadveh_cmd.cpp.

References _cur_year, _date, _settings_game, AllocaM, CheckConsistencyOfArticulatedVehicle(), CMD_ERROR, DAYS_IN_LEAP_YEAR, DC_AUTOREPLACE, DC_EXEC, DC_QUERY_COST, DiagDirToDir(), EF_ROAD_TRAM, ENGINE_EXCLUSIVE_PREVIEW, HasBit(), InvalidateAutoreplaceWindow(), InvalidateWindow(), InvalidateWindowClassesData(), InvalidateWindowData(), IsEngineBuildable(), IsTileOwner(), VehicleSettings::max_roadveh, return_cmd_error, ROADTYPE_ROAD, ROADTYPE_TRAM, RoadTypeToRoadTypes(), RoadVehicle::RoadVehicle(), RVSB_IN_DEPOT, VehicleSettings::servint_roadveh, SetBit(), TILE_SIZE, TileX(), TileY(), GameSettings::vehicle, VehicleMove(), and VehicleRandomBits().

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

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 1959 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(), InvalidateWindow(), InvalidateWindowClassesData(), IsValidVehicleID(), and return_cmd_error.

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

Sell a road vehicle.

Parameters:

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

Definition at line 328 of file roadveh_cmd.cpp.

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

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

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 433 of file roadveh_cmd.cpp.

References CMD_ERROR, DEPOT_MASS_SEND, DEPOT_SERVICE, IsValidVehicleID(), and SendAllVehiclesToDepot().

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

Turn a roadvehicle around.

Parameters:

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

Definition at line 456 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 1010 of file roadveh_cmd.cpp.

References _road_enter_dir_to_reachable_trackdirs, _road_exit_dir_to_incoming_trackdirs, _settings_game, AxisToRoadBits(), CC_PASSENGERS, DiagDirToAxis(), DiagDirToDiagTrackdir(), DiagDirToRoadBits(), FindFirstBit2x64(), FOR_EACH_SET_BIT, GetAnyRoadBits(), GetRoadStopDir(), GetTileTrackStatus(), HasBit(), INVALID_RAILTYPES, INVALID_TRACKDIR, IsTileOwner(), IsTileType(), KillFirstBit(), MP_ROAD, MP_STATION, PathfinderSettings::pathfinder_for_roadvehs, GameSettings::pf, ROADSTOP_BUS, ROADSTOP_TRUCK, ROADTYPE_TRAM, PathfinderSettings::roadveh_queue, TileOffsByDiagDir(), TRACKDIR_BIT_NONE, TrackStatusToRedSignals(), TrackStatusToTrackdirBits(), TRANSPORT_ROAD, 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 60 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 73 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 359 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 67 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 1204 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 81 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 48 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 37 of file roadveh_cmd.cpp.