OpenTTD Source 20241224-master-gf74b0cf984
vehicle_func.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10#ifndef VEHICLE_FUNC_H
11#define VEHICLE_FUNC_H
12
13#include "gfx_type.h"
14#include "direction_type.h"
16#include "command_type.h"
17#include "vehicle_type.h"
18#include "engine_type.h"
19#include "transport_type.h"
20#include "newgrf_config.h"
21#include "track_type.h"
22#include "livery.h"
23
24#define is_custom_sprite(x) (x >= 0xFD)
25#define IS_CUSTOM_FIRSTHEAD_SPRITE(x) (x == 0xFD)
26#define IS_CUSTOM_SECONDHEAD_SPRITE(x) (x == 0xFE)
27
28static const TimerGameEconomy::Date VEHICLE_PROFIT_MIN_AGE = CalendarTime::DAYS_IN_YEAR * 2;
29static const Money VEHICLE_PROFIT_THRESHOLD = 10000;
30
37template <VehicleType T>
38bool IsValidImageIndex(uint8_t image_index);
39
40typedef Vehicle *VehicleFromPosProc(Vehicle *v, void *data);
41
43uint CountVehiclesInChain(const Vehicle *v);
44void FindVehicleOnPos(TileIndex tile, void *data, VehicleFromPosProc *proc);
45void FindVehicleOnPosXY(int x, int y, void *data, VehicleFromPosProc *proc);
46bool HasVehicleOnPos(TileIndex tile, void *data, VehicleFromPosProc *proc);
47bool HasVehicleOnPosXY(int x, int y, void *data, VehicleFromPosProc *proc);
48void CallVehicleTicks();
49uint8_t CalcPercentVehicleFilled(const Vehicle *v, StringID *colour);
50
51void VehicleLengthChanged(const Vehicle *u);
52
53void ResetVehicleHash();
54void ResetVehicleColourMap();
55
56uint8_t GetBestFittingSubType(Vehicle *v_from, Vehicle *v_for, CargoID dest_cargo_type);
57
59
60void ShowNewGrfVehicleError(EngineID engine, StringID part1, StringID part2, GRFBugs bug_type, bool critical);
61CommandCost TunnelBridgeIsFree(TileIndex tile, TileIndex endtile, const Vehicle *ignore = nullptr);
62
64void CheckVehicleBreakdown(Vehicle *v);
66void AgeVehicle(Vehicle *v);
69
71
73
74bool CanBuildVehicleInfrastructure(VehicleType type, uint8_t subtype = 0);
75
82
84Direction GetDirectionTowards(const Vehicle *v, int x, int y);
85
92{
93 return type < VEH_COMPANY_END;
94}
95
102{
104}
105
106LiveryScheme GetEngineLiveryScheme(EngineID engine_type, EngineID parent_engine_type, const Vehicle *v);
107const struct Livery *GetEngineLivery(EngineID engine_type, CompanyID company, EngineID parent_engine_type, const Vehicle *v, uint8_t livery_setting);
108
109SpriteID GetEnginePalette(EngineID engine_type, CompanyID company);
111
112extern const StringID _veh_build_msg_table[];
113extern const StringID _veh_sell_msg_table[];
114extern const StringID _veh_refit_msg_table[];
115extern const StringID _send_to_depot_msg_table[];
116
117/* Functions to find the right command for certain vehicle type */
118inline StringID GetCmdBuildVehMsg(VehicleType type)
119{
120 return _veh_build_msg_table[type];
121}
122
123inline StringID GetCmdBuildVehMsg(const BaseVehicle *v)
124{
125 return GetCmdBuildVehMsg(v->type);
126}
127
128inline StringID GetCmdSellVehMsg(VehicleType type)
129{
130 return _veh_sell_msg_table[type];
131}
132
133inline StringID GetCmdSellVehMsg(const BaseVehicle *v)
134{
135 return GetCmdSellVehMsg(v->type);
136}
137
138inline StringID GetCmdRefitVehMsg(VehicleType type)
139{
140 return _veh_refit_msg_table[type];
141}
142
143inline StringID GetCmdRefitVehMsg(const BaseVehicle *v)
144{
145 return GetCmdRefitVehMsg(v->type);
146}
147
148inline StringID GetCmdSendToDepotMsg(VehicleType type)
149{
150 return _send_to_depot_msg_table[type];
151}
152
153inline StringID GetCmdSendToDepotMsg(const BaseVehicle *v)
154{
155 return GetCmdSendToDepotMsg(v->type);
156}
157
160
161bool CanVehicleUseStation(EngineID engine_type, const struct Station *st);
162bool CanVehicleUseStation(const Vehicle *v, const struct Station *st);
164
166
167typedef std::vector<VehicleID> VehicleSet;
168void GetVehicleSet(VehicleSet &set, Vehicle *v, uint8_t num_vehicles);
169
171
172bool VehiclesHaveSameEngineList(const Vehicle *v1, const Vehicle *v2);
173bool VehiclesHaveSameOrderList(const Vehicle *v1, const Vehicle *v2);
174
175bool IsUniqueVehicleName(const std::string &name);
176
177#endif /* VEHICLE_FUNC_H */
uint8_t CargoID
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:22
Common return value for all commands.
static constexpr int DAYS_IN_YEAR
days per year
Types related to commands.
Owner
Enum for all companies/owners.
Different types to 'show' directions.
Direction
Defines the 8 directions on the map.
Types related to engines.
uint16_t EngineID
Unique identification number of an engine.
Definition engine_type.h:21
Types related to the graphics and/or input devices.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:18
Functions/types related to livery colours.
LiveryScheme
List of different livery schemes.
Definition livery.h:21
Functions to find and configure NewGRFs.
GRFBugs
Encountered GRF bugs.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Base vehicle class.
VehicleType type
Type of vehicle.
Data about how and where to blit pixels.
Definition gfx_type.h:157
Position information of a vehicle after it moved.
TileIndex new_tile
Tile of the vehicle after moving.
int y
x and y position of the vehicle after moving
TileIndex old_tile
Current tile of the vehicle.
Information about a particular livery.
Definition livery.h:78
Station data structure.
Vehicle data structure.
Definition of the game-economy-timer.
All types related to tracks.
TrackBits
Allow incrementing of Track variables.
Definition track_type.h:35
Base types related to transport.
uint16_t UnitID
Type for the company global vehicle unit number.
bool CanVehicleUseStation(EngineID engine_type, const Station *st)
Can this station be used by the given engine type?
Definition vehicle.cpp:3058
CommandCost EnsureNoVehicleOnGround(TileIndex tile)
Ensure there is no vehicle at the ground at the given position.
Definition vehicle.cpp:546
bool IsUniqueVehicleName(const std::string &name)
Test if a name is unique among vehicle names.
bool VehiclesHaveSameEngineList(const Vehicle *v1, const Vehicle *v2)
Checks if two vehicle chains have the same list of engines.
Definition vehicle.cpp:3272
bool VehiclesHaveSameOrderList(const Vehicle *v1, const Vehicle *v2)
Checks if two vehicles have the same list of orders.
Definition vehicle.cpp:3289
SpriteID GetEnginePalette(EngineID engine_type, CompanyID company)
Get the colour map for an engine.
Definition vehicle.cpp:2142
void VehicleEnterDepot(Vehicle *v)
Vehicle entirely entered the depot, update its status, orders, vehicle windows, service it,...
Definition vehicle.cpp:1552
static const TimerGameEconomy::Date VEHICLE_PROFIT_MIN_AGE
Only vehicles older than this have a meaningful profit.
void GetVehicleSet(VehicleSet &set, Vehicle *v, uint8_t num_vehicles)
Calculates the set of vehicles that will be affected by a given selection.
Definition vehicle.cpp:3218
bool HasVehicleOnPosXY(int x, int y, void *data, VehicleFromPosProc *proc)
Checks whether a vehicle in on a specific location.
Definition vehicle.cpp:461
void CheckCargoCapacity(Vehicle *v)
Check the capacity of all vehicles in a chain and spread cargo if needed.
UnitID GetFreeUnitNumber(VehicleType type)
Get an unused unit number for a vehicle (if allowed).
Definition vehicle.cpp:1895
void RunVehicleCalendarDayProc()
Age all vehicles, spreading out the action using the current TimerGameCalendar::date_fract.
Definition vehicle.cpp:937
void VehicleLengthChanged(const Vehicle *u)
Logs a bug in GRF and shows a warning message if this is for the first time this happened.
Definition vehicle.cpp:346
bool IsValidImageIndex(uint8_t image_index)
Helper to check whether an image index is valid for a particular vehicle.
uint8_t GetBestFittingSubType(Vehicle *v_from, Vehicle *v_for, CargoID dest_cargo_type)
Get the best fitting subtype when 'cloning'/'replacing' v_from with v_for.
void VehicleServiceInDepot(Vehicle *v)
Service a vehicle and all subsequent vehicles in the consist.
Definition vehicle.cpp:167
static const Money VEHICLE_PROFIT_THRESHOLD
Threshold for a vehicle to be considered making good profit.
CommandCost EnsureNoTrainOnTrackBits(TileIndex tile, TrackBits track_bits)
Tests if a vehicle interacts with the specified track bits.
Definition vehicle.cpp:608
void FindVehicleOnPosXY(int x, int y, void *data, VehicleFromPosProc *proc)
Find a vehicle from a specific location.
Definition vehicle.cpp:445
LiveryScheme GetEngineLiveryScheme(EngineID engine_type, EngineID parent_engine_type, const Vehicle *v)
Determines the LiveryScheme for a vehicle.
Definition vehicle.cpp:1970
void ViewportAddVehicles(DrawPixelInfo *dpi)
Add the vehicle sprites that should be drawn at a part of the screen.
Definition vehicle.cpp:1154
GetNewVehiclePosResult GetNewVehiclePos(const Vehicle *v)
Get position information of a vehicle when moving one pixel in the direction it is facing.
Definition vehicle.cpp:1784
bool HasVehicleOnPos(TileIndex tile, void *data, VehicleFromPosProc *proc)
Checks whether a vehicle is on a specific location.
Definition vehicle.cpp:520
void FindVehicleOnPos(TileIndex tile, void *data, VehicleFromPosProc *proc)
Find a vehicle from a specific location.
Definition vehicle.cpp:505
StringID GetVehicleCannotUseStationReason(const Vehicle *v, const Station *st)
Get reason string why this station can't be used by the given vehicle.
Definition vehicle.cpp:3104
void ReleaseDisasterVehicle(VehicleID vehicle)
Notify disasters that we are about to delete a vehicle.
void DecreaseVehicleValue(Vehicle *v)
Decrease the value of a vehicle.
Definition vehicle.cpp:1301
void EconomyAgeVehicle(Vehicle *v)
Update economy age of a vehicle.
Definition vehicle.cpp:1429
const struct Livery * GetEngineLivery(EngineID engine_type, CompanyID company, EngineID parent_engine_type, const Vehicle *v, uint8_t livery_setting)
Determines the livery for a vehicle.
Definition vehicle.cpp:2064
CommandCost TunnelBridgeIsFree(TileIndex tile, TileIndex endtile, const Vehicle *ignore=nullptr)
Finds vehicle in tunnel / bridge.
Definition vehicle.cpp:575
bool CanBuildVehicleInfrastructure(VehicleType type, uint8_t subtype=0)
Check whether we can build infrastructure for the given vehicle type.
Definition vehicle.cpp:1922
void ShowNewGrfVehicleError(EngineID engine, StringID part1, StringID part2, GRFBugs bug_type, bool critical)
Displays a "NewGrf Bug" error message for a engine, and pauses the game if not networking.
Definition vehicle.cpp:317
void AgeVehicle(Vehicle *v)
Update age of a vehicle.
Definition vehicle.cpp:1441
void VehicleEnteredDepotThisTick(Vehicle *v)
Adds a vehicle to the list of vehicles that visited a depot this tick.
Definition vehicle.cpp:921
SpriteID GetVehiclePalette(const Vehicle *v)
Get the colour map for a vehicle.
Definition vehicle.cpp:2152
uint8_t CalcPercentVehicleFilled(const Vehicle *v, StringID *colour)
Calculates how full a vehicle is.
Definition vehicle.cpp:1493
bool IsCompanyBuildableVehicleType(VehicleType type)
Is the given vehicle type buildable by a company?
Types related to vehicles.
VehicleType
Available vehicle types.
@ VEH_COMPANY_END
Last company-ownable type.
uint32_t VehicleID
The type all our vehicle IDs have.