OpenTTD Source 20250528-master-g3aca5d62a8
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
31
32static inline bool IsCustomVehicleSpriteNum(uint8_t spritenum)
33{
34 return spritenum >= CUSTOM_VEHICLE_SPRITENUM;
35}
36
38static const Money VEHICLE_PROFIT_THRESHOLD = 10000;
39
46template <VehicleType T>
47bool IsValidImageIndex(uint8_t image_index);
48
54public:
58 class Iterator {
59 public:
60 using value_type = Vehicle *;
61 using difference_type = std::ptrdiff_t;
62 using iterator_category = std::forward_iterator_tag;
63 using pointer = void;
64 using reference = void;
65
66 explicit Iterator(TileIndex tile);
67
68 bool operator==(const Iterator &rhs) const { return this->current == rhs.current; }
69 bool operator==(const std::default_sentinel_t &) const { return this->current == nullptr; }
70
71 Vehicle *operator*() const { return this->current; }
72
73 Iterator &operator++()
74 {
75 this->Increment();
76 this->SkipFalseMatches();
77 return *this;
78 }
79
80 Iterator operator++(int)
81 {
82 Iterator result = *this;
83 ++*this;
84 return result;
85 }
86 private:
87 TileIndex tile;
88 Vehicle *current;
89
90 void Increment();
91 void SkipFalseMatches();
92 };
93
94 explicit VehiclesOnTile(TileIndex tile) : start(tile) {}
95 Iterator begin() const { return this->start; }
96 std::default_sentinel_t end() const { return std::default_sentinel_t(); }
97private:
98 Iterator start;
99};
100
105template <class UnaryPred>
106bool HasVehicleOnTile(TileIndex tile, UnaryPred &&predicate)
107{
108 for (const auto *v : VehiclesOnTile(tile)) {
109 if (predicate(v)) return true;
110 }
111 return false;
112}
113
120public:
124 class Iterator {
125 public:
126 using value_type = Vehicle *;
127 using difference_type = std::ptrdiff_t;
128 using iterator_category = std::forward_iterator_tag;
129 using pointer = void;
130 using reference = void;
131
132 explicit Iterator(int32_t x, int32_t y, uint max_dist);
133
134 bool operator==(const Iterator &rhs) const { return this->current_veh == rhs.current_veh; }
135 bool operator==(const std::default_sentinel_t &) const { return this->current_veh == nullptr; }
136
137 Vehicle *operator*() const { return this->current_veh; }
138
139 Iterator &operator++()
140 {
141 this->Increment();
142 this->SkipFalseMatches();
143 return *this;
144 }
145
146 Iterator operator++(int)
147 {
148 Iterator result = *this;
149 ++*this;
150 return result;
151 }
152 private:
153 Rect pos_rect;
154 uint hxmin, hxmax, hymin, hymax;
155 uint hx, hy;
156 Vehicle *current_veh;
157
158 void Increment();
159 void SkipEmptyBuckets();
160 void SkipFalseMatches();
161 };
162
163 explicit VehiclesNearTileXY(int32_t x, int32_t y, uint max_dist) : start(x, y, max_dist) {}
164 Iterator begin() const { return this->start; }
165 std::default_sentinel_t end() const { return std::default_sentinel_t(); }
166private:
167 Iterator start;
168};
169
175template <class UnaryPred>
176bool HasVehicleNearTileXY(int32_t x, int32_t y, uint max_dist, UnaryPred &&predicate)
177{
178 for (const auto *v : VehiclesNearTileXY(x, y, max_dist)) {
179 if (predicate(v)) return true;
180 }
181 return false;
182}
183
185uint CountVehiclesInChain(const Vehicle *v);
186void CallVehicleTicks();
187uint8_t CalcPercentVehicleFilled(const Vehicle *v, StringID *colour);
188
189void VehicleLengthChanged(const Vehicle *u);
190
191void ResetVehicleHash();
192void ResetVehicleColourMap();
193
194uint8_t GetBestFittingSubType(Vehicle *v_from, Vehicle *v_for, CargoType dest_cargo_type);
195
197
198void ShowNewGrfVehicleError(EngineID engine, StringID part1, StringID part2, GRFBug bug_type, bool critical);
199CommandCost TunnelBridgeIsFree(TileIndex tile, TileIndex endtile, const Vehicle *ignore = nullptr);
200
202void CheckVehicleBreakdown(Vehicle *v);
204void AgeVehicle(Vehicle *v);
206
208
210
211bool CanBuildVehicleInfrastructure(VehicleType type, uint8_t subtype = 0);
212
219
221Direction GetDirectionTowards(const Vehicle *v, int x, int y);
222
229{
230 return type < VEH_COMPANY_END;
231}
232
239{
241}
242
243LiveryScheme GetEngineLiveryScheme(EngineID engine_type, EngineID parent_engine_type, const Vehicle *v);
244const struct Livery *GetEngineLivery(EngineID engine_type, CompanyID company, EngineID parent_engine_type, const Vehicle *v, uint8_t livery_setting);
245
246SpriteID GetEnginePalette(EngineID engine_type, CompanyID company);
248
249extern const StringID _veh_build_msg_table[];
250extern const StringID _veh_sell_msg_table[];
251extern const StringID _veh_sell_all_msg_table[];
252extern const StringID _veh_autoreplace_msg_table[];
253extern const StringID _veh_refit_msg_table[];
254extern const StringID _send_to_depot_msg_table[];
255
256/* Functions to find the right command for certain vehicle type */
257inline StringID GetCmdBuildVehMsg(VehicleType type)
258{
259 return _veh_build_msg_table[type];
260}
261
262inline StringID GetCmdBuildVehMsg(const BaseVehicle *v)
263{
264 return GetCmdBuildVehMsg(v->type);
265}
266
267inline StringID GetCmdSellVehMsg(VehicleType type)
268{
269 return _veh_sell_msg_table[type];
270}
271
272inline StringID GetCmdSellVehMsg(const BaseVehicle *v)
273{
274 return GetCmdSellVehMsg(v->type);
275}
276
277inline StringID GetCmdSellAllVehMsg(VehicleType type)
278{
279 return _veh_sell_all_msg_table[type];
280}
281
282inline StringID GetCmdAutoreplaceVehMsg(VehicleType type)
283{
284 return _veh_autoreplace_msg_table[type];
285}
286
287inline StringID GetCmdRefitVehMsg(VehicleType type)
288{
289 return _veh_refit_msg_table[type];
290}
291
292inline StringID GetCmdRefitVehMsg(const BaseVehicle *v)
293{
294 return GetCmdRefitVehMsg(v->type);
295}
296
297inline StringID GetCmdSendToDepotMsg(VehicleType type)
298{
299 return _send_to_depot_msg_table[type];
300}
301
302inline StringID GetCmdSendToDepotMsg(const BaseVehicle *v)
303{
304 return GetCmdSendToDepotMsg(v->type);
305}
306
309
310bool CanVehicleUseStation(EngineID engine_type, const struct Station *st);
311bool CanVehicleUseStation(const Vehicle *v, const struct Station *st);
313
315
316typedef std::vector<VehicleID> VehicleSet;
317void GetVehicleSet(VehicleSet &set, Vehicle *v, uint8_t num_vehicles);
318
320
321bool VehiclesHaveSameEngineList(const Vehicle *v1, const Vehicle *v2);
322bool VehiclesHaveSameOrderList(const Vehicle *v1, const Vehicle *v2);
323
324bool IsUniqueVehicleName(const std::string &name);
325
326#endif /* VEHICLE_FUNC_H */
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:23
Common return value for all commands.
static constexpr int DAYS_IN_YEAR
days per year
void SkipEmptyBuckets()
Advance the internal state until we reach a non-empty bucket, or the end.
Definition vehicle.cpp:472
void Increment()
Advance the internal state to the next potential vehicle.
Definition vehicle.cpp:462
void SkipFalseMatches()
Advance the internal state until it reaches a vehicle within the search area.
Definition vehicle.cpp:490
Iterate over all vehicles near a given world coordinate.
Forward iterator.
void Increment()
Advance the internal state to the next potential vehicle.
Definition vehicle.cpp:509
void SkipFalseMatches()
Advance the internal state until it reaches a vehicle on the correct tile or the end.
Definition vehicle.cpp:517
Iterate over all vehicles on a tile.
Types related to commands.
Different types to 'show' directions.
Direction
Defines the 8 directions on the map.
Types related to engines.
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:17
Functions/types related to livery colours.
LiveryScheme
List of different livery schemes.
Definition livery.h:21
Functions to find and configure NewGRFs.
GRFBug
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:158
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
Specification of a rectangle with absolute coordinates of all edges.
Station data structure.
Templated helper to make a type-safe 'typedef' representing a single POD value.
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:3019
void ShowNewGrfVehicleError(EngineID engine, StringID part1, StringID part2, GRFBug bug_type, bool critical)
Displays a "NewGrf Bug" error message for a engine, and pauses the game if not networking.
Definition vehicle.cpp:328
bool HasVehicleNearTileXY(int32_t x, int32_t y, uint max_dist, UnaryPred &&predicate)
Loop over vehicles near a given world coordinate, and check whether a predicate is true for any of th...
CommandCost EnsureNoVehicleOnGround(TileIndex tile)
Ensure there is no vehicle at the ground at the given position.
Definition vehicle.cpp:527
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:3233
bool VehiclesHaveSameOrderList(const Vehicle *v1, const Vehicle *v2)
Checks if two vehicles have the same list of orders.
Definition vehicle.cpp:3250
SpriteID GetEnginePalette(EngineID engine_type, CompanyID company)
Get the colour map for an engine.
Definition vehicle.cpp:2112
void VehicleEnterDepot(Vehicle *v)
Vehicle entirely entered the depot, update its status, orders, vehicle windows, service it,...
Definition vehicle.cpp:1521
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:3179
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:1865
void RunVehicleCalendarDayProc()
Age all vehicles, spreading out the action using the current TimerGameCalendar::date_fract.
Definition vehicle.cpp:907
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:354
bool IsValidImageIndex(uint8_t image_index)
Helper to check whether an image index is valid for a particular vehicle.
void VehicleServiceInDepot(Vehicle *v)
Service a vehicle and all subsequent vehicles in the consist.
Definition vehicle.cpp:178
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:575
LiveryScheme GetEngineLiveryScheme(EngineID engine_type, EngineID parent_engine_type, const Vehicle *v)
Determines the LiveryScheme for a vehicle.
Definition vehicle.cpp:1940
uint8_t GetBestFittingSubType(Vehicle *v_from, Vehicle *v_for, CargoType dest_cargo_type)
Get the best fitting subtype when 'cloning'/'replacing' v_from with v_for.
void ViewportAddVehicles(DrawPixelInfo *dpi)
Add the vehicle sprites that should be drawn at a part of the screen.
Definition vehicle.cpp:1122
GetNewVehiclePosResult GetNewVehiclePos(const Vehicle *v)
Get position information of a vehicle when moving one pixel in the direction it is facing.
Definition vehicle.cpp:1754
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:3065
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:1271
void EconomyAgeVehicle(Vehicle *v)
Update economy age of a vehicle.
Definition vehicle.cpp:1399
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:2034
CustomVehicleSpriteNum
Special values for Vehicle::spritenum and (Aircraft|Rail|Road|Ship)VehicleInfo::image_index.
@ CUSTOM_VEHICLE_SPRITENUM
Vehicle sprite from NewGRF.
@ CUSTOM_VEHICLE_SPRITENUM_REVERSED
Vehicle sprite from NewGRF with reverse driving direction (from articulation callback)
CommandCost TunnelBridgeIsFree(TileIndex tile, TileIndex endtile, const Vehicle *ignore=nullptr)
Finds vehicle in tunnel / bridge.
Definition vehicle.cpp:551
bool CanBuildVehicleInfrastructure(VehicleType type, uint8_t subtype=0)
Check whether we can build infrastructure for the given vehicle type.
Definition vehicle.cpp:1892
bool HasVehicleOnTile(TileIndex tile, UnaryPred &&predicate)
Loop over vehicles on a tile, and check whether a predicate is true for any of them.
void AgeVehicle(Vehicle *v)
Update age of a vehicle.
Definition vehicle.cpp:1411
SpriteID GetVehiclePalette(const Vehicle *v)
Get the colour map for a vehicle.
Definition vehicle.cpp:2122
uint8_t CalcPercentVehicleFilled(const Vehicle *v, StringID *colour)
Calculates how full a vehicle is.
Definition vehicle.cpp:1462
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.