OpenTTD Source: src/vehicle_func.h Source File

OpenTTD Source 15.3

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
10#ifndef VEHICLE_FUNC_H
11#define VEHICLE_FUNC_H
12
13#include "gfx_type.h"
14#include "direction_type.h"
15#include "timer/timer_game_economy.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
27 enum CustomVehicleSpriteNum {
28 CUSTOM_VEHICLE_SPRITENUM = 0xFD,
29 CUSTOM_VEHICLE_SPRITENUM_REVERSED = 0xFE,
30};
31
32static inline bool IsCustomVehicleSpriteNum(uint8_t spritenum)
33{
34 return spritenum >= CUSTOM_VEHICLE_SPRITENUM;
35}
36
37 static const TimerGameEconomy::Date VEHICLE_PROFIT_MIN_AGE{CalendarTime::DAYS_IN_YEAR * 2};
38 static const Money VEHICLE_PROFIT_THRESHOLD = 10000;
39
46template <VehicleType T>
47 bool IsValidImageIndex(uint8_t image_index);
48
53 class 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
105template <class UnaryPred>
106 bool 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
119 class VehiclesNearTileXY {
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>
176 bool 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
184void VehicleServiceInDepot(Vehicle *v);
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
196void ViewportAddVehicles(DrawPixelInfo *dpi);
197
198void ShowNewGrfVehicleError(EngineID engine, StringID part1, StringID part2, GRFBug bug_type, bool critical);
199 CommandCost TunnelBridgeIsFree(TileIndex tile, TileIndex endtile, const Vehicle *ignore = nullptr);
200
201void DecreaseVehicleValue(Vehicle *v);
202void CheckVehicleBreakdown(Vehicle *v);
203void EconomyAgeVehicle(Vehicle *v);
204void AgeVehicle(Vehicle *v);
205void RunVehicleCalendarDayProc();
206
207 UnitID GetFreeUnitNumber(VehicleType type);
208
209void VehicleEnterDepot(Vehicle *v);
210
211bool CanBuildVehicleInfrastructure(VehicleType type, uint8_t subtype = 0);
212
214 struct GetNewVehiclePosResult {
215 int x, y;
216 TileIndex old_tile;
217 TileIndex new_tile;
218};
219
220 GetNewVehiclePosResult GetNewVehiclePos(const Vehicle *v);
221 Direction GetDirectionTowards(const Vehicle *v, int x, int y);
222
228 inline bool IsCompanyBuildableVehicleType(VehicleType type)
229{
230 return type < VEH_COMPANY_END;
231}
232
238 inline bool IsCompanyBuildableVehicleType(const BaseVehicle *v)
239{
240 return IsCompanyBuildableVehicleType(v->type);
241}
242
243 LiveryScheme 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
246 SpriteID GetEnginePalette(EngineID engine_type, CompanyID company);
247 SpriteID GetVehiclePalette(const Vehicle *v);
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
307 CommandCost EnsureNoVehicleOnGround(TileIndex tile);
308 CommandCost EnsureNoTrainOnTrackBits(TileIndex tile, TrackBits track_bits);
309
310bool CanVehicleUseStation(EngineID engine_type, const struct Station *st);
311bool CanVehicleUseStation(const Vehicle *v, const struct Station *st);
312 StringID GetVehicleCannotUseStationReason(const Vehicle *v, const Station *st);
313
314void ReleaseDisasterVehicle(VehicleID vehicle);
315
316typedef std::vector<VehicleID> VehicleSet;
317void GetVehicleSet(VehicleSet &set, Vehicle *v, uint8_t num_vehicles);
318
319void CheckCargoCapacity(Vehicle *v);
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 */
CargoType
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:21
CommandCost
Common return value for all commands.
Definition command_type.h:24
OverflowSafeInt< int64_t >
TimerGameConst< struct Calendar >::DAYS_IN_YEAR
static constexpr int DAYS_IN_YEAR
days per year
Definition timer_game_common.h:149
VehiclesNearTileXY::Iterator
Forward iterator.
Definition vehicle_func.h:124
VehiclesNearTileXY::Iterator::SkipEmptyBuckets
void SkipEmptyBuckets()
Advance the internal state until we reach a non-empty bucket, or the end.
Definition vehicle.cpp:472
VehiclesNearTileXY::Iterator::Increment
void Increment()
Advance the internal state to the next potential vehicle.
Definition vehicle.cpp:462
VehiclesNearTileXY::Iterator::SkipFalseMatches
void SkipFalseMatches()
Advance the internal state until it reaches a vehicle within the search area.
Definition vehicle.cpp:490
VehiclesNearTileXY
Iterate over all vehicles near a given world coordinate.
Definition vehicle_func.h:119
VehiclesOnTile::Iterator
Forward iterator.
Definition vehicle_func.h:58
VehiclesOnTile::Iterator::Increment
void Increment()
Advance the internal state to the next potential vehicle.
Definition vehicle.cpp:509
VehiclesOnTile::Iterator::SkipFalseMatches
void SkipFalseMatches()
Advance the internal state until it reaches a vehicle on the correct tile or the end.
Definition vehicle.cpp:517
VehiclesOnTile
Iterate over all vehicles on a tile.
Definition vehicle_func.h:53
command_type.h
Types related to commands.
direction_type.h
Different types to 'show' directions.
Direction
Direction
Defines the 8 directions on the map.
Definition direction_type.h:24
engine_type.h
Types related to engines.
gfx_type.h
Types related to the graphics and/or input devices.
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
livery.h
Functions/types related to livery colours.
LiveryScheme
LiveryScheme
List of different livery schemes.
Definition livery.h:22
newgrf_config.h
Functions to find and configure NewGRFs.
GRFBug
GRFBug
Encountered GRF bugs.
Definition newgrf_config.h:42
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:19
BaseVehicle
Base vehicle class.
Definition vehicle_type.h:50
BaseVehicle::type
VehicleType type
Type of vehicle.
Definition vehicle_type.h:51
DrawPixelInfo
Data about how and where to blit pixels.
Definition gfx_type.h:157
GetNewVehiclePosResult
Position information of a vehicle after it moved.
Definition vehicle_func.h:214
GetNewVehiclePosResult::new_tile
TileIndex new_tile
Tile of the vehicle after moving.
Definition vehicle_func.h:217
GetNewVehiclePosResult::y
int y
x and y position of the vehicle after moving
Definition vehicle_func.h:215
GetNewVehiclePosResult::old_tile
TileIndex old_tile
Current tile of the vehicle.
Definition vehicle_func.h:216
Livery
Information about a particular livery.
Definition livery.h:79
PoolID< uint16_t, struct EngineIDTag, 64000, 0xFFFF >
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition geometry_type.hpp:101
Station
Station data structure.
Definition station_base.h:520
StrongType::Typedef
Templated helper to make a type-safe 'typedef' representing a single POD value.
Definition strong_typedef_type.hpp:130
Vehicle
Vehicle data structure.
Definition vehicle_base.h:205
timer_game_economy.h
Definition of the game-economy-timer.
track_type.h
All types related to tracks.
TrackBits
TrackBits
Allow incrementing of Track variables.
Definition track_type.h:35
transport_type.h
Base types related to transport.
UnitID
uint16_t UnitID
Type for the company global vehicle unit number.
Definition transport_type.h:14
CanVehicleUseStation
bool CanVehicleUseStation(EngineID engine_type, const Station *st)
Can this station be used by the given engine type?
Definition vehicle.cpp:3046
ShowNewGrfVehicleError
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
HasVehicleNearTileXY
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...
Definition vehicle_func.h:176
EnsureNoVehicleOnGround
CommandCost EnsureNoVehicleOnGround(TileIndex tile)
Ensure there is no vehicle at the ground at the given position.
Definition vehicle.cpp:527
IsUniqueVehicleName
bool IsUniqueVehicleName(const std::string &name)
Test if a name is unique among vehicle names.
Definition vehicle_cmd.cpp:761
VehiclesHaveSameEngineList
bool VehiclesHaveSameEngineList(const Vehicle *v1, const Vehicle *v2)
Checks if two vehicle chains have the same list of engines.
Definition vehicle.cpp:3260
VehiclesHaveSameOrderList
bool VehiclesHaveSameOrderList(const Vehicle *v1, const Vehicle *v2)
Checks if two vehicles have the same list of orders.
Definition vehicle.cpp:3277
GetEnginePalette
SpriteID GetEnginePalette(EngineID engine_type, CompanyID company)
Get the colour map for an engine.
Definition vehicle.cpp:2123
VehicleEnterDepot
void VehicleEnterDepot(Vehicle *v)
Vehicle entirely entered the depot, update its status, orders, vehicle windows, service it,...
Definition vehicle.cpp:1520
VEHICLE_PROFIT_MIN_AGE
static const TimerGameEconomy::Date VEHICLE_PROFIT_MIN_AGE
Only vehicles older than this have a meaningful profit.
Definition vehicle_func.h:37
GetVehicleSet
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:3206
CheckCargoCapacity
void CheckCargoCapacity(Vehicle *v)
Check the capacity of all vehicles in a chain and spread cargo if needed.
Definition autoreplace_cmd.cpp:107
GetFreeUnitNumber
UnitID GetFreeUnitNumber(VehicleType type)
Get an unused unit number for a vehicle (if allowed).
Definition vehicle.cpp:1876
RunVehicleCalendarDayProc
void RunVehicleCalendarDayProc()
Age all vehicles, spreading out the action using the current TimerGameCalendar::date_fract.
Definition vehicle.cpp:907
VehicleLengthChanged
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
IsValidImageIndex
bool IsValidImageIndex(uint8_t image_index)
Helper to check whether an image index is valid for a particular vehicle.
VehicleServiceInDepot
void VehicleServiceInDepot(Vehicle *v)
Service a vehicle and all subsequent vehicles in the consist.
Definition vehicle.cpp:178
VEHICLE_PROFIT_THRESHOLD
static const Money VEHICLE_PROFIT_THRESHOLD
Threshold for a vehicle to be considered making good profit.
Definition vehicle_func.h:38
EnsureNoTrainOnTrackBits
CommandCost EnsureNoTrainOnTrackBits(TileIndex tile, TrackBits track_bits)
Tests if a vehicle interacts with the specified track bits.
Definition vehicle.cpp:575
GetEngineLiveryScheme
LiveryScheme GetEngineLiveryScheme(EngineID engine_type, EngineID parent_engine_type, const Vehicle *v)
Determines the LiveryScheme for a vehicle.
Definition vehicle.cpp:1951
GetBestFittingSubType
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.
Definition vehicle_gui.cpp:600
ViewportAddVehicles
void ViewportAddVehicles(DrawPixelInfo *dpi)
Add the vehicle sprites that should be drawn at a part of the screen.
Definition vehicle.cpp:1121
GetNewVehiclePos
GetNewVehiclePosResult GetNewVehiclePos(const Vehicle *v)
Get position information of a vehicle when moving one pixel in the direction it is facing.
Definition vehicle.cpp:1765
GetVehicleCannotUseStationReason
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:3092
ReleaseDisasterVehicle
void ReleaseDisasterVehicle(VehicleID vehicle)
Notify disasters that we are about to delete a vehicle.
Definition disaster_vehicle.cpp:981
DecreaseVehicleValue
void DecreaseVehicleValue(Vehicle *v)
Decrease the value of a vehicle.
Definition vehicle.cpp:1270
EconomyAgeVehicle
void EconomyAgeVehicle(Vehicle *v)
Update economy age of a vehicle.
Definition vehicle.cpp:1398
GetEngineLivery
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:2045
CustomVehicleSpriteNum
CustomVehicleSpriteNum
Special values for Vehicle::spritenum and (Aircraft|Rail|Road|Ship)VehicleInfo::image_index.
Definition vehicle_func.h:27
CUSTOM_VEHICLE_SPRITENUM
@ CUSTOM_VEHICLE_SPRITENUM
Vehicle sprite from NewGRF.
Definition vehicle_func.h:28
CUSTOM_VEHICLE_SPRITENUM_REVERSED
@ CUSTOM_VEHICLE_SPRITENUM_REVERSED
Vehicle sprite from NewGRF with reverse driving direction (from articulation callback)
Definition vehicle_func.h:29
TunnelBridgeIsFree
CommandCost TunnelBridgeIsFree(TileIndex tile, TileIndex endtile, const Vehicle *ignore=nullptr)
Finds vehicle in tunnel / bridge.
Definition vehicle.cpp:551
CanBuildVehicleInfrastructure
bool CanBuildVehicleInfrastructure(VehicleType type, uint8_t subtype=0)
Check whether we can build infrastructure for the given vehicle type.
Definition vehicle.cpp:1903
HasVehicleOnTile
bool HasVehicleOnTile(TileIndex tile, UnaryPred &&predicate)
Loop over vehicles on a tile, and check whether a predicate is true for any of them.
Definition vehicle_func.h:106
AgeVehicle
void AgeVehicle(Vehicle *v)
Update age of a vehicle.
Definition vehicle.cpp:1410
GetVehiclePalette
SpriteID GetVehiclePalette(const Vehicle *v)
Get the colour map for a vehicle.
Definition vehicle.cpp:2133
CalcPercentVehicleFilled
uint8_t CalcPercentVehicleFilled(const Vehicle *v, StringID *colour)
Calculates how full a vehicle is.
Definition vehicle.cpp:1461
IsCompanyBuildableVehicleType
bool IsCompanyBuildableVehicleType(VehicleType type)
Is the given vehicle type buildable by a company?
Definition vehicle_func.h:228
vehicle_type.h
Types related to vehicles.
VehicleType
VehicleType
Available vehicle types.
Definition vehicle_type.h:22
VEH_COMPANY_END
@ VEH_COMPANY_END
Last company-ownable type.
Definition vehicle_type.h:30