OpenTTD Source 20260218-master-g2123fca5ea
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
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
37static const TimerGameEconomy::Date VEHICLE_PROFIT_MIN_AGE{CalendarTime::DAYS_IN_YEAR * 2};
38static const Money VEHICLE_PROFIT_THRESHOLD = 10000;
39
46template <VehicleType T>
47bool IsValidImageIndex(uint8_t image_index);
48
53class VehiclesOnTile {
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
108template <class UnaryPred>
109bool HasVehicleOnTile(TileIndex tile, UnaryPred &&predicate)
110{
111 for (const auto *v : VehiclesOnTile(tile)) {
112 if (predicate(v)) return true;
113 }
114 return false;
115}
116
122class VehiclesNearTileXY {
123public:
127 class Iterator {
128 public:
129 using value_type = Vehicle *;
130 using difference_type = std::ptrdiff_t;
131 using iterator_category = std::forward_iterator_tag;
132 using pointer = void;
133 using reference = void;
134
135 explicit Iterator(int32_t x, int32_t y, uint max_dist);
136
137 bool operator==(const Iterator &rhs) const { return this->current_veh == rhs.current_veh; }
138 bool operator==(const std::default_sentinel_t &) const { return this->current_veh == nullptr; }
139
140 Vehicle *operator*() const { return this->current_veh; }
141
142 Iterator &operator++()
143 {
144 this->Increment();
145 this->SkipFalseMatches();
146 return *this;
147 }
148
149 Iterator operator++(int)
150 {
151 Iterator result = *this;
152 ++*this;
153 return result;
154 }
155 private:
156 Rect pos_rect;
157 uint hxmin, hxmax, hymin, hymax;
158 uint hx, hy;
159 Vehicle *current_veh;
160
161 void Increment();
162 void SkipEmptyBuckets();
163 void SkipFalseMatches();
164 };
165
166 explicit VehiclesNearTileXY(int32_t x, int32_t y, uint max_dist) : start(x, y, max_dist) {}
167 Iterator begin() const { return this->start; }
168 std::default_sentinel_t end() const { return std::default_sentinel_t(); }
169private:
170 Iterator start;
171};
172
183template <class UnaryPred>
184bool HasVehicleNearTileXY(int32_t x, int32_t y, uint max_dist, UnaryPred &&predicate)
185{
186 for (const auto *v : VehiclesNearTileXY(x, y, max_dist)) {
187 if (predicate(v)) return true;
188 }
189 return false;
190}
191
193uint CountVehiclesInChain(const Vehicle *v);
194void CallVehicleTicks();
195uint8_t CalcPercentVehicleFilled(const Vehicle *v, StringID *colour);
196
197void VehicleLengthChanged(const Vehicle *u);
198
199void ResetVehicleHash();
200void ResetVehicleColourMap();
201
202uint8_t GetBestFittingSubType(Vehicle *v_from, Vehicle *v_for, CargoType dest_cargo_type);
203
205
206void ShowNewGrfVehicleError(EngineID engine, StringID part1, StringID part2, GRFBug bug_type, bool critical);
207CommandCost TunnelBridgeIsFree(TileIndex tile, TileIndex endtile, const Vehicle *ignore = nullptr);
208
212void AgeVehicle(Vehicle *v);
214
216
218
219bool CanBuildVehicleInfrastructure(VehicleType type, uint8_t subtype = 0);
220
227
229Direction GetDirectionTowards(const Vehicle *v, int x, int y);
230
237{
238 return type < VEH_COMPANY_END;
239}
240
247{
249}
250
251LiveryScheme GetEngineLiveryScheme(EngineID engine_type, EngineID parent_engine_type, const Vehicle *v);
252const struct Livery *GetEngineLivery(EngineID engine_type, CompanyID company, EngineID parent_engine_type, const Vehicle *v, uint8_t livery_setting);
253
254SpriteID GetEnginePalette(EngineID engine_type, CompanyID company);
256
257extern const StringID _veh_build_msg_table[];
258extern const StringID _veh_sell_msg_table[];
259extern const StringID _veh_sell_all_msg_table[];
260extern const StringID _veh_autoreplace_msg_table[];
261extern const StringID _veh_refit_msg_table[];
262extern const StringID _send_to_depot_msg_table[];
263
264/* Functions to find the right command for certain vehicle type */
265inline StringID GetCmdBuildVehMsg(VehicleType type)
266{
267 return _veh_build_msg_table[type];
268}
269
270inline StringID GetCmdBuildVehMsg(const BaseVehicle *v)
271{
272 return GetCmdBuildVehMsg(v->type);
273}
274
275inline StringID GetCmdSellVehMsg(VehicleType type)
276{
277 return _veh_sell_msg_table[type];
278}
279
280inline StringID GetCmdSellVehMsg(const BaseVehicle *v)
281{
282 return GetCmdSellVehMsg(v->type);
283}
284
285inline StringID GetCmdSellAllVehMsg(VehicleType type)
286{
287 return _veh_sell_all_msg_table[type];
288}
289
290inline StringID GetCmdAutoreplaceVehMsg(VehicleType type)
291{
292 return _veh_autoreplace_msg_table[type];
293}
294
295inline StringID GetCmdRefitVehMsg(VehicleType type)
296{
297 return _veh_refit_msg_table[type];
298}
299
300inline StringID GetCmdRefitVehMsg(const BaseVehicle *v)
301{
302 return GetCmdRefitVehMsg(v->type);
303}
304
305inline StringID GetCmdSendToDepotMsg(VehicleType type)
306{
307 return _send_to_depot_msg_table[type];
308}
309
310inline StringID GetCmdSendToDepotMsg(const BaseVehicle *v)
311{
312 return GetCmdSendToDepotMsg(v->type);
313}
314
317
318bool CanVehicleUseStation(EngineID engine_type, const struct Station *st);
319bool CanVehicleUseStation(const Vehicle *v, const struct Station *st);
321
323
324typedef std::vector<VehicleID> VehicleSet;
325void GetVehicleSet(VehicleSet &set, Vehicle *v, uint8_t num_vehicles);
326
328
329bool VehiclesHaveSameEngineList(const Vehicle *v1, const Vehicle *v2);
330bool VehiclesHaveSameOrderList(const Vehicle *v1, const Vehicle *v2);
331
332bool IsUniqueVehicleName(const std::string &name);
333
334#endif /* VEHICLE_FUNC_H */
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:21
Common return value for all commands.
void SkipEmptyBuckets()
Advance the internal state until we reach a non-empty bucket, or the end.
Definition vehicle.cpp:473
Iterator(int32_t x, int32_t y, uint max_dist)
Iterator constructor.
Definition vehicle.cpp:433
void Increment()
Advance the internal state to the next potential vehicle.
Definition vehicle.cpp:463
void SkipFalseMatches()
Advance the internal state until it reaches a vehicle within the search area.
Definition vehicle.cpp:491
Iterate over all vehicles near a given world coordinate.
Forward iterator.
Iterator(TileIndex tile)
Iterator constructor.
Definition vehicle.cpp:500
void Increment()
Advance the internal state to the next potential vehicle.
Definition vehicle.cpp:510
void SkipFalseMatches()
Advance the internal state until it reaches a vehicle on the correct tile or the end.
Definition vehicle.cpp:518
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.
PoolID< uint16_t, struct EngineIDTag, 64000, 0xFFFF > EngineID
Unique identification number of an engine.
Definition engine_type.h:26
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:22
#define Rect
Macro that prevents name conflicts between included headers.
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: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:79
Station data structure.
Vehicle data structure.
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
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:3060
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:528
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:3274
bool VehiclesHaveSameOrderList(const Vehicle *v1, const Vehicle *v2)
Checks if two vehicles have the same list of orders.
Definition vehicle.cpp:3291
SpriteID GetEnginePalette(EngineID engine_type, CompanyID company)
Get the colour map for an engine.
Definition vehicle.cpp:2136
void VehicleEnterDepot(Vehicle *v)
Vehicle entirely entered the depot, update its status, orders, vehicle windows, service it,...
Definition vehicle.cpp:1536
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:3220
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:1888
void RunVehicleCalendarDayProc()
Age all vehicles, spreading out the action using the current TimerGameCalendar::date_fract.
Definition vehicle.cpp:908
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:576
LiveryScheme GetEngineLiveryScheme(EngineID engine_type, EngineID parent_engine_type, const Vehicle *v)
Determines the LiveryScheme for a vehicle.
Definition vehicle.cpp:1964
void CheckVehicleBreakdown(Vehicle *v)
Periodic check for a vehicle to maybe break down.
Definition vehicle.cpp:1292
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:1777
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:3106
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:1414
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:2058
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:552
bool CanBuildVehicleInfrastructure(VehicleType type, uint8_t subtype=0)
Check whether we can build infrastructure for the given vehicle type.
Definition vehicle.cpp:1916
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:1426
SpriteID GetVehiclePalette(const Vehicle *v)
Get the colour map for a vehicle.
Definition vehicle.cpp:2146
uint8_t CalcPercentVehicleFilled(const Vehicle *v, StringID *colour)
Calculates how full a vehicle is.
Definition vehicle.cpp:1477
bool IsCompanyBuildableVehicleType(VehicleType type)
Is the given vehicle type buildable by a company?
Types related to vehicles.
PoolID< uint32_t, struct VehicleIDTag, 0xFF000, 0xFFFFF > VehicleID
The type all our vehicle IDs have.
VehicleType
Available vehicle types.
@ VEH_COMPANY_END
Last company-ownable type.