OpenTTD Source 20260421-master-gc2fbc6fdeb
roadveh_cmd.cpp File Reference

Handling of road vehicles. More...

#include "stdafx.h"
#include "roadveh.h"
#include "command_func.h"
#include "error_func.h"
#include "news_func.h"
#include "station_base.h"
#include "company_func.h"
#include "articulated_vehicles.h"
#include "newgrf_sound.h"
#include "pathfinder/yapf/yapf.h"
#include "strings_func.h"
#include "tunnelbridge_map.h"
#include "timer/timer_game_calendar.h"
#include "timer/timer_game_economy.h"
#include "vehicle_func.h"
#include "sound_func.h"
#include "ai/ai.hpp"
#include "game/game.hpp"
#include "depot_map.h"
#include "effectvehicle_func.h"
#include "roadstop_base.h"
#include "core/random_func.hpp"
#include "company_base.h"
#include "core/backup_type.hpp"
#include "newgrf.h"
#include "zoom_func.h"
#include "framerate_type.h"
#include "roadveh_cmd.h"
#include "road_cmd.h"
#include "newgrf_roadstop.h"
#include "table/strings.h"
#include "safeguards.h"
#include "table/roadveh_movement.h"

Go to the source code of this file.

Data Structures

struct  RoadVehFindData
struct  OvertakeData
struct  RoadDriveEntry

Functions

template<>
bool IsValidImageIndex< VEH_ROAD > (uint8_t image_index)
 Helper to check whether an image index is valid for a particular vehicle.
static void GetRoadVehIcon (EngineID engine, EngineImageType image_type, VehicleSpriteSeq *result)
void DrawRoadVehEngine (int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal, EngineImageType image_type)
 Draw a road vehicle engine.
void GetRoadVehSpriteSize (EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type)
 Get the size of the sprite of a road vehicle sprite heading west (used for lists).
static uint GetRoadVehLength (const RoadVehicle *v)
 Get length of a road vehicle.
void RoadVehUpdateCache (RoadVehicle *v, bool same_length)
 Update the cache of a road vehicle.
CommandCost CmdBuildRoadVehicle (DoCommandFlags flags, TileIndex tile, const Engine *e, Vehicle **ret)
 Build a road vehicle.
static FindDepotData FindClosestRoadDepot (const RoadVehicle *v, int max_distance)
CommandCost CmdTurnRoadVeh (DoCommandFlags flags, VehicleID veh_id)
 Turn a roadvehicle around.
static void DeleteLastRoadVeh (RoadVehicle *v)
 Delete last vehicle of a chain road vehicles.
static void RoadVehSetRandomDirection (RoadVehicle *v)
static bool RoadVehIsCrashed (RoadVehicle *v)
 Road vehicle chain has crashed.
static void RoadVehCrash (RoadVehicle *v)
static bool RoadVehCheckTrainCrash (RoadVehicle *v)
static void StartRoadVehSound (const RoadVehicle *v)
static void FindClosestBlockingRoadVeh (Vehicle *v, RoadVehFindData *rvf)
static RoadVehicleRoadVehFindCloseTo (RoadVehicle *v, int x, int y, Direction dir, bool update_blocked_ctr=true)
static void RoadVehArrivesAt (const RoadVehicle *v, Station *st)
 A road vehicle arrives at a station.
static Direction RoadVehGetNewDirection (const RoadVehicle *v, int x, int y)
static Direction RoadVehGetSlidingDirection (const RoadVehicle *v, int x, int y)
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, int 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.
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, RoadType rt, RoadBits r)
 Can a tram track build without destruction on the given tile?
bool IndividualRoadVehicleController (RoadVehicle *v, const RoadVehicle *prev)
static bool RoadVehController (RoadVehicle *v)
static void CheckIfRoadVehNeedsService (RoadVehicle *v)

Variables

static const uint16_t _roadveh_images []
static const uint16_t _roadveh_full_adder []
static const Trackdir _road_reverse_table [DIAGDIR_END]

Detailed Description

Handling of road vehicles.

Definition in file roadveh_cmd.cpp.

Function Documentation

◆ CanBuildTramTrackOnTile()

bool CanBuildTramTrackOnTile ( CompanyID c,
TileIndex t,
RoadType rt,
RoadBits r )
static

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

Parameters
cthe company that would be building the tram tracks
tthe tile to build on.
rtthe tram type to build.
rthe road bits needed.
Returns
true when a track track can be build on 't'

Definition at line 1143 of file roadveh_cmd.cpp.

References _current_company, NoWater, Backup< T >::Restore(), and CommandCost::Succeeded().

◆ CheckIfRoadVehNeedsService()

void CheckIfRoadVehNeedsService ( RoadVehicle * v)
static

Definition at line 1687 of file roadveh_cmd.cpp.

◆ CheckRoadBlockedForOvertaking()

bool CheckRoadBlockedForOvertaking ( OvertakeData * od)
static

Check if overtaking is possible on a piece of track.

Parameters
odInformation about the tile and the involved vehicles
Returns
true if we have to abort overtaking

Definition at line 792 of file roadveh_cmd.cpp.

References RoadVehicle::compatible_roadtypes, GetTileTrackStatus(), HasBit(), HasTileAnyRoadType(), HasVehicleOnTile(), RoadVehicle::roadtype, TRACK_BIT_CROSS, TRACKDIR_BIT_NONE, TrackdirBitsToTrackBits(), TrackStatusToRedSignals(), TrackStatusToTrackdirBits(), and TRANSPORT_ROAD.

◆ CmdBuildRoadVehicle()

◆ CmdTurnRoadVeh()

◆ DeleteLastRoadVeh()

void DeleteLastRoadVeh ( RoadVehicle * v)
static

◆ DrawRoadVehEngine()

void DrawRoadVehEngine ( int left,
int right,
int preferred_x,
int y,
EngineID engine,
PaletteID pal,
EngineImageType image_type )

Draw a road vehicle engine.

Parameters
leftLeft edge to draw within.
rightRight edge to draw within.
preferred_xPreferred position of the engine.
yVertical position of the engine.
engineEngine to draw
palPalette to use.
image_typeContext where the image is being drawn.

Definition at line 151 of file roadveh_cmd.cpp.

References Clamp(), VehicleSpriteSeq::Draw(), VehicleSpriteSeq::GetBounds(), PALETTE_CRASH, Rect, and UnScaleGUI().

Referenced by DrawVehicleEngine().

◆ FindClosestBlockingRoadVeh()

void FindClosestBlockingRoadVeh ( Vehicle * v,
RoadVehFindData * rvf )
static

Definition at line 629 of file roadveh_cmd.cpp.

◆ FindClosestRoadDepot()

FindDepotData FindClosestRoadDepot ( const RoadVehicle * v,
int max_distance )
static

Definition at line 343 of file roadveh_cmd.cpp.

◆ FollowPreviousRoadVehicle()

Trackdir FollowPreviousRoadVehicle ( const RoadVehicle * v,
const RoadVehicle * prev,
TileIndex tile,
DiagDirection entry_dir,
bool already_reversed )
static

Definition at line 1057 of file roadveh_cmd.cpp.

◆ GetRoadVehIcon()

void GetRoadVehIcon ( EngineID engine,
EngineImageType image_type,
VehicleSpriteSeq * result )
static

Definition at line 105 of file roadveh_cmd.cpp.

◆ GetRoadVehLength()

uint GetRoadVehLength ( const RoadVehicle * v)
static

Get length of a road vehicle.

Parameters
vRoad vehicle to query length.
Returns
Length of the given road vehicle.

Definition at line 193 of file roadveh_cmd.cpp.

References CALLBACK_FAILED, CBID_VEHICLE_LENGTH, Clamp(), Vehicle::engine_type, ErrorUnknownCallbackResult(), Vehicle::GetEngine(), Engine::GetGRF(), Engine::GetGRFID(), GetVehicleCallback(), PROP_ROADVEH_SHORTEN_FACTOR, and VEHICLE_LENGTH.

Referenced by RoadVehUpdateCache().

◆ GetRoadVehSpriteSize()

void GetRoadVehSpriteSize ( EngineID engine,
uint & width,
uint & height,
int & xoffs,
int & yoffs,
EngineImageType image_type )

Get the size of the sprite of a road vehicle sprite heading west (used for lists).

Parameters
engineThe engine to get the sprite from.
[out]widthThe width of the sprite.
[out]heightThe height of the sprite.
[out]xoffsNumber of pixels to shift the sprite to the right.
[out]yoffsNumber of pixels to shift the sprite downwards.
image_typeContext the sprite is used in.

Definition at line 174 of file roadveh_cmd.cpp.

References VehicleSpriteSeq::GetBounds(), Rect::Height(), Rect, UnScaleGUI(), and Rect::Width().

Referenced by EnginePreviewWindow::UpdateWidgetSize().

◆ IndividualRoadVehicleController()

bool IndividualRoadVehicleController ( RoadVehicle * v,
const RoadVehicle * prev )

Definition at line 1154 of file roadveh_cmd.cpp.

◆ IsValidImageIndex< VEH_ROAD >()

template<>
bool IsValidImageIndex< VEH_ROAD > ( uint8_t image_index)

Helper to check whether an image index is valid for a particular vehicle.

Template Parameters
TThe type of vehicle.
Parameters
image_indexThe image index to check.
Returns
True iff the image index is valid.

Definition at line 70 of file roadveh_cmd.cpp.

References lengthof.

◆ PickRandomBit()

int PickRandomBit ( uint bits)
static

Definition at line 873 of file roadveh_cmd.cpp.

◆ RoadFindPathToDest()

◆ RoadVehArrivesAt()

void RoadVehArrivesAt ( const RoadVehicle * v,
Station * st )
static

A road vehicle arrives at a station.

If it is the first time, create a news item.

Parameters
vRoad vehicle that arrived.
stStation where the road vehicle arrived.

Definition at line 703 of file roadveh_cmd.cpp.

References _local_company, AddVehicleNewsItem(), ArrivalCompany, ArrivalOther, GetEncodedString(), HVOT_BUS, HVOT_TRUCK, RoadVehicle::IsBus(), AI::NewEvent(), Game::NewEvent(), Vehicle::owner, and RoadVehicle::roadtype.

◆ RoadVehCheckOvertake()

void RoadVehCheckOvertake ( RoadVehicle * v,
RoadVehicle * u )
static

Definition at line 809 of file roadveh_cmd.cpp.

◆ RoadVehCheckTrainCrash()

bool RoadVehCheckTrainCrash ( RoadVehicle * v)
static

Definition at line 575 of file roadveh_cmd.cpp.

◆ RoadVehController()

bool RoadVehController ( RoadVehicle * v)
static

Definition at line 1584 of file roadveh_cmd.cpp.

◆ RoadVehCrash()

void RoadVehCrash ( RoadVehicle * v)
static

Definition at line 557 of file roadveh_cmd.cpp.

◆ RoadVehFindCloseTo()

RoadVehicle * RoadVehFindCloseTo ( RoadVehicle * v,
int x,
int y,
Direction dir,
bool update_blocked_ctr = true )
static

Definition at line 658 of file roadveh_cmd.cpp.

◆ RoadVehGetNewDirection()

Direction RoadVehGetNewDirection ( const RoadVehicle * v,
int x,
int y )
static

Definition at line 753 of file roadveh_cmd.cpp.

◆ RoadVehGetSlidingDirection()

Direction RoadVehGetSlidingDirection ( const RoadVehicle * v,
int x,
int y )
static

Definition at line 768 of file roadveh_cmd.cpp.

◆ RoadVehIsCrashed()

bool RoadVehIsCrashed ( RoadVehicle * v)
static

Road vehicle chain has crashed.

Parameters
vFirst roadvehicle.
Returns
whether the chain still exists.

Definition at line 526 of file roadveh_cmd.cpp.

References RoadVehicle::crashed_ctr, CreateEffectVehicleRel(), DeleteLastRoadVeh(), EV_EXPLOSION_LARGE, SpecializedVehicle< T, Type >::Next(), and Vehicle::tick_counter.

◆ RoadVehLeaveDepot()

bool RoadVehLeaveDepot ( RoadVehicle * v,
bool first )

Definition at line 1009 of file roadveh_cmd.cpp.

◆ RoadVehSetRandomDirection()

void RoadVehSetRandomDirection ( RoadVehicle * v)
static

Definition at line 507 of file roadveh_cmd.cpp.

◆ RoadVehUpdateCache()

void RoadVehUpdateCache ( RoadVehicle * v,
bool same_length )

◆ RoadZPosAffectSpeed()

void RoadZPosAffectSpeed ( RoadVehicle * v,
int old_z )
static

Definition at line 861 of file roadveh_cmd.cpp.

◆ StartRoadVehSound()

void StartRoadVehSound ( const RoadVehicle * v)
static

Definition at line 609 of file roadveh_cmd.cpp.

Variable Documentation

◆ _road_reverse_table

const Trackdir _road_reverse_table[DIAGDIR_END]
static
Initial value:
= {
}
@ TRACKDIR_RVREV_NE
(Road vehicle) reverse direction north-east
Definition track_type.h:74
@ TRACKDIR_RVREV_SE
(Road vehicle) reverse direction south-east
Definition track_type.h:75
@ TRACKDIR_RVREV_NW
(Road vehicle) reverse direction north-west
Definition track_type.h:83
@ TRACKDIR_RVREV_SW
(Road vehicle) reverse direction south-west
Definition track_type.h:82

Definition at line 75 of file roadveh_cmd.cpp.

◆ _roadveh_full_adder

const uint16_t _roadveh_full_adder[]
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 56 of file roadveh_cmd.cpp.

◆ _roadveh_images

const uint16_t _roadveh_images[]
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 45 of file roadveh_cmd.cpp.