# roadveh_cmd.cpp File Reference

Handling of road vehicles. More...

`#include "stdafx.h"`
`#include "landscape.h"`
`#include "roadveh.h"`
`#include "command_func.h"`
`#include "news_func.h"`
`#include "pathfinder/npf/npf_func.h"`
`#include "station_base.h"`
`#include "company_func.h"`
`#include "vehicle_gui.h"`
`#include "articulated_vehicles.h"`
`#include "newgrf_engine.h"`
`#include "newgrf_sound.h"`
`#include "pathfinder/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 "autoreplace_gui.h"`
`#include "ai/ai.hpp"`
`#include "depot_map.h"`
`#include "effectvehicle_func.h"`
`#include "effectvehicle_base.h"`
`#include "roadstop_base.h"`
`#include "cargotype.h"`
`#include "spritecache.h"`
`#include "core/random_func.hpp"`
`#include "engine_base.h"`
`#include "company_base.h"`
`#include "engine_func.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 | RoadVehFindData
struct | OvertakeData
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 left, int right, int preferred_x, int y, EngineID engine, PaletteID pal)
static uint | GetRoadVehLength (const RoadVehicle *v)
void | RoadVehUpdateCache (RoadVehicle *v)
CommandCost | CmdBuildRoadVeh (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Build a road vehicle.
CommandCost | CmdSellRoadVeh (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Sell a road vehicle.
static FindDepotData | FindClosestRoadDepot (const RoadVehicle *v, int max_distance)
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 | DeleteLastRoadVeh (RoadVehicle *v)
static byte | SetRoadVehPosition (RoadVehicle *v, int x, int y, bool turned)
static void | RoadVehSetRandomDirection (RoadVehicle *v)
static bool | RoadVehIsCrashed (RoadVehicle *v)
static Vehicle * | EnumCheckRoadVehCrashTrain (Vehicle *v, void *data)
static void | RoadVehCrash (RoadVehicle *v)
static bool | RoadVehCheckTrainCrash (RoadVehicle *v)
static void | HandleBrokenRoadVeh (RoadVehicle *v)
static void | StartRoadVehSound (const RoadVehicle *v)
static Vehicle * | EnumCheckRoadVehClose (Vehicle *v, void *data)
static RoadVehicle * | RoadVehFindCloseTo (RoadVehicle *v, int x, int y, Direction dir, bool update_blocked_ctr=true)
static void | RoadVehArrivesAt (const RoadVehicle *v, Station *st)
static int | RoadVehAccelerate (RoadVehicle *v)
static Direction | RoadVehGetNewDirection (const RoadVehicle *v, int x, int y)
static Direction | RoadVehGetSlidingDirection (const RoadVehicle *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 (RoadVehicle *v, RoadVehicle *u)
static void | RoadZPosAffectSpeed (RoadVehicle *v, byte old_z)
static int | PickRandomBit (uint bits)
static Trackdir | RoadFindPathToDest (RoadVehicle *v, TileIndex tile, DiagDirection enterdir)
| Returns direction to for a road vehicle to take or INVALID_TRACKDIR if the direction is currently blocked.
static bool | RoadVehLeaveDepot (RoadVehicle *v, bool first)
static Trackdir | FollowPreviousRoadVehicle (const RoadVehicle *v, const RoadVehicle *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 (RoadVehicle *v, const RoadVehicle *prev)
static bool | RoadVehController (RoadVehicle *v)
static void | CheckIfRoadVehNeedsService (RoadVehicle *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 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 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 1138 of file roadveh_cmd.cpp.

References CMD_BUILD_ROAD, DC_NONE, DoCommand(), ROADTYPE_TRAM, and CommandCost::Succeeded().

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 825 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
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 201 of file roadveh_cmd.cpp.

References _cur_year, _date, _settings_game, RoadVehicleCache::cached_veh_length, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_vehicle_pool >::CanAllocateItem(), Vehicle::cargo_cap, Vehicle::cargo_type, CheckConsistencyOfArticulatedVehicle(), CMD_ERROR, CT_INVALID, DC_AUTOREPLACE, DC_EXEC, DC_QUERY_COST, DiagDirToDir(), EF_ROAD_TRAM, ENGINE_EXCLUSIVE_PREVIEW, EXPENSES_NEW_VEHICLES, RoadVehicleCache::first_engine, GB(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_engine_pool >::Get(), Engine::GetDefaultCargoType(), GetFreeUnitNumber(), Engine::GetLifeLengthInDays(), GetVehicleCapacity(), Vehicle::group_id, HasBit(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, InvalidateAutoreplaceWindow(), Vehicle::InvalidateNewGRFCache(), Vehicle::InvalidateNewGRFCacheOfChain(), InvalidateWindowClassesData(), InvalidateWindowData(), IsEngineBuildable(), IsTileOwner(), VehicleSettings::max_roadveh, RoadVehicleInfo::max_speed, Vehicle::max_speed, SpecializedVehicle< T, Type >::Next(), RoadVehicle::rcache, return_cmd_error, ROADTYPE_ROAD, ROADTYPE_TRAM, RoadTypeToRoadTypes(), RVSB_IN_DEPOT, SetBit(), RoadVehicle::SetRoadVehFront(), SetWindowDirty(), RoadVehicle::state, Vehicle::tile, TILE_SIZE, TileX(), TileY(), Vehicle::value, VEH_ROAD, GameSettings::vehicle, Vehicle::vehicle_flags, VehicleMove(), VehicleRandomBits(), and Vehicle::vehstatus.

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
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 1728 of file roadveh_cmd.cpp.

References CheckOwnership(), CMD_ERROR, DC_EXEC, SpecializedVehicle< T, Type >::First(), GB(), SpecializedVehicle< RoadVehicle, VEH_ROAD >::GetIfValid(), HasBit(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, Vehicle::InvalidateNewGRFCacheOfChain(), InvalidateWindowClassesData(), InvalidateWindowData(), RoadVehicle::IsStoppedInDepot(), NUM_CARGO, RefitVehicle(), return_cmd_error, SetWindowDirty(), Vehicle::tile, and Vehicle::vehstatus.

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
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 330 of file roadveh_cmd.cpp.

References CheckOwnership(), CMD_ERROR, DC_EXEC, EXPENSES_NEW_VEHICLES, SpecializedVehicle< RoadVehicle, VEH_ROAD >::GetIfValid(), RoadVehicle::IsStoppedInDepot(), return_cmd_error, Vehicle::value, and Vehicle::vehstatus.

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)
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 383 of file roadveh_cmd.cpp.

References CMD_ERROR, DEPOT_MASS_SEND, DEPOT_SERVICE, SpecializedVehicle< RoadVehicle, VEH_ROAD >::GetIfValid(), SendAllVehiclesToDepot(), Vehicle::SendToDepot(), and VEH_ROAD.

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
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 405 of file roadveh_cmd.cpp.

References CheckOwnership(), CMD_ERROR, Vehicle::current_order, DC_EXEC, DirToDiagDir(), DRD_NONE, GetDisallowedRoadDirections(), SpecializedVehicle< RoadVehicle, VEH_ROAD >::GetIfValid(), GetTunnelBridgeDirection(), RoadVehicle::IsInDepot(), IsTileType(), Order::IsType(), MP_TUNNELBRIDGE, RVSB_WORMHOLE, RoadVehicle::state, Vehicle::tile, and Vehicle::vehstatus.

static Trackdir RoadFindPathToDest | ( | RoadVehicle * | 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 920 of file roadveh_cmd.cpp.

References _road_enter_dir_to_reachable_trackdirs, _settings_game, AxisToRoadBits(), Vehicle::dest_tile, DiagDirToAxis(), DiagDirToRoadBits(), FindFirstBit2x64(), GetAnyRoadBits(), RoadStop::GetByTile(), GetRoadStopDir(), GetRoadStopType(), GetTileTrackStatus(), RoadVehicle::HasArticulatedPart(), HasBit(), INVALID_TRACKDIR, RoadVehicle::IsBus(), IsStandardRoadStopTile(), IsTileOwner(), IsTileType(), KillFirstBit(), MP_ROAD, MP_STATION, NPFRoadVehicleChooseTrack(), PathfinderSettings::pathfinder_for_roadvehs, GameSettings::pf, ROADSTOP_BUS, ROADSTOP_TRUCK, ROADTYPE_TRAM, PathfinderSettings::roadveh_queue, Vehicle::tile, TRACKDIR_BIT_NONE, TrackStatusToRedSignals(), TrackStatusToTrackdirBits(), TRANSPORT_ROAD, VPF_NPF, VPF_YAPF, and YapfRoadVehicleChooseTrack().

## 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 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 1020 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 83 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.