OpenTTD Source: src/vehicle_func.h Source File

OpenTTD Source 1.11.0-RC1

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"
15 #include "command_type.h"
16 #include "vehicle_type.h"
17 #include "engine_type.h"
18 #include "transport_type.h"
19 #include "newgrf_config.h"
20 #include "track_type.h"
21 #include "livery.h"
22
23 #define is_custom_sprite(x) (x >= 0xFD)
24 #define IS_CUSTOM_FIRSTHEAD_SPRITE(x) (x == 0xFD)
25 #define IS_CUSTOM_SECONDHEAD_SPRITE(x) (x == 0xFE)
26
27 static const int VEHICLE_PROFIT_MIN_AGE = DAYS_IN_YEAR * 2;
28 static const Money VEHICLE_PROFIT_THRESHOLD = 10000;
29
36 template <VehicleType T>
37 bool IsValidImageIndex(uint8 image_index);
38
39 typedef Vehicle *VehicleFromPosProc(Vehicle *v, void *data);
40
41 void VehicleServiceInDepot(Vehicle *v);
42 uint CountVehiclesInChain(const Vehicle *v);
43 void FindVehicleOnPos(TileIndex tile, void *data, VehicleFromPosProc *proc);
44 void FindVehicleOnPosXY(int x, int y, void *data, VehicleFromPosProc *proc);
45 bool HasVehicleOnPos(TileIndex tile, void *data, VehicleFromPosProc *proc);
46 bool HasVehicleOnPosXY(int x, int y, void *data, VehicleFromPosProc *proc);
47 void CallVehicleTicks();
48 uint8 CalcPercentVehicleFilled(const Vehicle *v, StringID *colour);
49
50 void VehicleLengthChanged(const Vehicle *u);
51
52 byte VehicleRandomBits();
53 void ResetVehicleHash();
54 void ResetVehicleColourMap();
55
56 byte GetBestFittingSubType(Vehicle *v_from, Vehicle *v_for, CargoID dest_cargo_type);
57
58 void ViewportAddVehicles(DrawPixelInfo *dpi);
59
60 void ShowNewGrfVehicleError(EngineID engine, StringID part1, StringID part2, GRFBugs bug_type, bool critical);
61 CommandCost TunnelBridgeIsFree(TileIndex tile, TileIndex endtile, const Vehicle *ignore = nullptr);
62
63 void DecreaseVehicleValue(Vehicle *v);
64 void CheckVehicleBreakdown(Vehicle *v);
65 void AgeVehicle(Vehicle *v);
66 void VehicleEnteredDepotThisTick(Vehicle *v);
67
68 UnitID GetFreeUnitNumber(VehicleType type);
69
70 void VehicleEnterDepot(Vehicle *v);
71
72 bool CanBuildVehicleInfrastructure(VehicleType type, byte subtype = 0);
73
75 struct GetNewVehiclePosResult {
76 int x, y;
77 TileIndex old_tile;
78 TileIndex new_tile;
79 };
80
81 GetNewVehiclePosResult GetNewVehiclePos(const Vehicle *v);
82 Direction GetDirectionTowards(const Vehicle *v, int x, int y);
83
89 static inline bool IsCompanyBuildableVehicleType(VehicleType type)
90 {
91 switch (type) {
92 case VEH_TRAIN:
93 case VEH_ROAD:
94 case VEH_SHIP:
95 case VEH_AIRCRAFT:
96 return true;
97
98 default: return false;
99 }
100 }
101
107 static inline bool IsCompanyBuildableVehicleType(const BaseVehicle *v)
108 {
109 return IsCompanyBuildableVehicleType(v->type);
110 }
111
112 LiveryScheme GetEngineLiveryScheme(EngineID engine_type, EngineID parent_engine_type, const Vehicle *v);
113 const struct Livery *GetEngineLivery(EngineID engine_type, CompanyID company, EngineID parent_engine_type, const Vehicle *v, byte livery_setting);
114
115 SpriteID GetEnginePalette(EngineID engine_type, CompanyID company);
116 SpriteID GetVehiclePalette(const Vehicle *v);
117
118 extern const uint32 _veh_build_proc_table[];
119 extern const uint32 _veh_sell_proc_table[];
120 extern const uint32 _veh_refit_proc_table[];
121 extern const uint32 _send_to_depot_proc_table[];
122
123 /* Functions to find the right command for certain vehicle type */
124 static inline uint32 GetCmdBuildVeh(VehicleType type)
125 {
126 return _veh_build_proc_table[type];
127 }
128
129 static inline uint32 GetCmdBuildVeh(const BaseVehicle *v)
130 {
131 return GetCmdBuildVeh(v->type);
132 }
133
134 static inline uint32 GetCmdSellVeh(VehicleType type)
135 {
136 return _veh_sell_proc_table[type];
137 }
138
139 static inline uint32 GetCmdSellVeh(const BaseVehicle *v)
140 {
141 return GetCmdSellVeh(v->type);
142 }
143
144 static inline uint32 GetCmdRefitVeh(VehicleType type)
145 {
146 return _veh_refit_proc_table[type];
147 }
148
149 static inline uint32 GetCmdRefitVeh(const BaseVehicle *v)
150 {
151 return GetCmdRefitVeh(v->type);
152 }
153
154 static inline uint32 GetCmdSendToDepot(VehicleType type)
155 {
156 return _send_to_depot_proc_table[type];
157 }
158
159 static inline uint32 GetCmdSendToDepot(const BaseVehicle *v)
160 {
161 return GetCmdSendToDepot(v->type);
162 }
163
164 CommandCost EnsureNoVehicleOnGround(TileIndex tile);
165 CommandCost EnsureNoTrainOnTrackBits(TileIndex tile, TrackBits track_bits);
166
167 extern VehicleID _new_vehicle_id;
168 extern uint _returned_refit_capacity;
169 extern uint16 _returned_mail_refit_capacity;
170
171 bool CanVehicleUseStation(EngineID engine_type, const struct Station *st);
172 bool CanVehicleUseStation(const Vehicle *v, const struct Station *st);
173
174 void ReleaseDisastersTargetingVehicle(VehicleID vehicle);
175
176 typedef std::vector<VehicleID> VehicleSet;
177 void GetVehicleSet(VehicleSet &set, Vehicle *v, uint8 num_vehicles);
178
179 void CheckCargoCapacity(Vehicle *v);
180
181 #endif /* VEHICLE_FUNC_H */
VEH_AIRCRAFT
@ VEH_AIRCRAFT
Aircraft vehicle type.
Definition: vehicle_type.h:27
IsCompanyBuildableVehicleType
static bool IsCompanyBuildableVehicleType(VehicleType type)
Is the given vehicle type buildable by a company?
Definition: vehicle_func.h:89
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
transport_type.h
direction_type.h
ViewportAddVehicles
void ViewportAddVehicles(DrawPixelInfo *dpi)
Add the vehicle sprites that should be drawn at a part of the screen.
Definition: vehicle.cpp:1107
Direction
Direction
Defines the 8 directions on the map.
Definition: direction_type.h:24
GetVehiclePalette
SpriteID GetVehiclePalette(const Vehicle *v)
Get the colour map for a vehicle.
Definition: vehicle.cpp:2041
UnitID
uint16 UnitID
Type for the company global vehicle unit number.
Definition: transport_type.h:16
Station
Station data structure.
Definition: station_base.h:450
IsValidImageIndex
bool IsValidImageIndex(uint8 image_index)
Helper to check whether an image index is valid for a particular vehicle.
EnsureNoVehicleOnGround
CommandCost EnsureNoVehicleOnGround(TileIndex tile)
Ensure there is no vehicle at the ground at the given position.
Definition: vehicle.cpp:539
GetEnginePalette
SpriteID GetEnginePalette(EngineID engine_type, CompanyID company)
Get the colour map for an engine.
Definition: vehicle.cpp:2031
HasVehicleOnPos
bool HasVehicleOnPos(TileIndex tile, void *data, VehicleFromPosProc *proc)
Checks whether a vehicle is on a specific location.
Definition: vehicle.cpp:513
VEH_ROAD
@ VEH_ROAD
Road vehicle type.
Definition: vehicle_type.h:25
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:222
newgrf_config.h
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
GRFBugs
GRFBugs
Encountered GRF bugs.
Definition: newgrf_config.h:43
_returned_mail_refit_capacity
uint16 _returned_mail_refit_capacity
Stores the mail capacity after a refit operation (Aircraft only).
Definition: vehicle.cpp:86
SpriteID
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
TunnelBridgeIsFree
CommandCost TunnelBridgeIsFree(TileIndex tile, TileIndex endtile, const Vehicle *ignore=nullptr)
Finds vehicle in tunnel / bridge.
Definition: vehicle.cpp:568
EngineID
uint16 EngineID
Unique identification number of an engine.
Definition: engine_type.h:21
CheckCargoCapacity
void CheckCargoCapacity(Vehicle *v)
Check the capacity of all vehicles in a chain and spread cargo if needed.
Definition: autoreplace_cmd.cpp:104
CommandCost
Common return value for all commands.
Definition: command_type.h:23
BaseVehicle
Base vehicle class.
Definition: vehicle_type.h:50
GetEngineLivery
const struct Livery * GetEngineLivery(EngineID engine_type, CompanyID company, EngineID parent_engine_type, const Vehicle *v, byte livery_setting)
Determines the livery for a vehicle.
Definition: vehicle.cpp:1953
GetFreeUnitNumber
UnitID GetFreeUnitNumber(VehicleType type)
Get an unused unit number for a vehicle (if allowed).
Definition: vehicle.cpp:1785
GetNewVehiclePosResult::new_tile
TileIndex new_tile
Tile of the vehicle after moving.
Definition: vehicle_func.h:78
VehicleRandomBits
byte VehicleRandomBits()
Get a value for a vehicle's random_bits.
Definition: vehicle.cpp:363
FindVehicleOnPosXY
void FindVehicleOnPosXY(int x, int y, void *data, VehicleFromPosProc *proc)
Find a vehicle from a specific location.
Definition: vehicle.cpp:438
livery.h
VehicleType
VehicleType
Available vehicle types.
Definition: vehicle_type.h:21
HasVehicleOnPosXY
bool HasVehicleOnPosXY(int x, int y, void *data, VehicleFromPosProc *proc)
Checks whether a vehicle in on a specific location.
Definition: vehicle.cpp:454
command_type.h
ShowNewGrfVehicleError
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:297
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
vehicle_type.h
VehicleEnteredDepotThisTick
void VehicleEnteredDepotThisTick(Vehicle *v)
Adds a vehicle to the list of vehicles that visited a depot this tick.
Definition: vehicle.cpp:892
engine_type.h
VehicleServiceInDepot
void VehicleServiceInDepot(Vehicle *v)
Service a vehicle and all subsequent vehicles in the consist.
Definition: vehicle.cpp:162
GetNewVehiclePosResult
Position information of a vehicle after it moved.
Definition: vehicle_func.h:75
VehicleEnterDepot
void VehicleEnterDepot(Vehicle *v)
Vehicle entirely entered the depot, update its status, orders, vehicle windows, service it,...
Definition: vehicle.cpp:1472
DAYS_IN_YEAR
static const int DAYS_IN_YEAR
days per year
Definition: date_type.h:29
GetNewVehiclePosResult::old_tile
TileIndex old_tile
Current tile of the vehicle.
Definition: vehicle_func.h:77
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:330
VehicleID
uint32 VehicleID
The type all our vehicle IDs have.
Definition: vehicle_type.h:16
TrackBits
TrackBits
Bitfield corresponding to Track.
Definition: track_type.h:38
OverflowSafeInt< int64, INT64_MAX, INT64_MIN >
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
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:1680
_returned_refit_capacity
uint _returned_refit_capacity
Stores the capacity after a refit operation.
Definition: vehicle.cpp:85
EnsureNoTrainOnTrackBits
CommandCost EnsureNoTrainOnTrackBits(TileIndex tile, TrackBits track_bits)
Tests if a vehicle interacts with the specified track bits.
Definition: vehicle.cpp:601
LiveryScheme
LiveryScheme
List of different livery schemes.
Definition: livery.h:20
CanBuildVehicleInfrastructure
bool CanBuildVehicleInfrastructure(VehicleType type, byte subtype=0)
Check whether we can build infrastructure for the given vehicle type.
Definition: vehicle.cpp:1814
AgeVehicle
void AgeVehicle(Vehicle *v)
Update age of a vehicle.
Definition: vehicle.cpp:1363
VEH_TRAIN
@ VEH_TRAIN
Train vehicle type.
Definition: vehicle_type.h:24
BaseVehicle::type
VehicleType type
Type of vehicle.
Definition: vehicle_type.h:52
ReleaseDisastersTargetingVehicle
void ReleaseDisastersTargetingVehicle(VehicleID vehicle)
Notify disasters that we are about to delete a vehicle.
Definition: disaster_vehicle.cpp:953
DecreaseVehicleValue
void DecreaseVehicleValue(Vehicle *v)
Decrease the value of a vehicle.
Definition: vehicle.cpp:1235
CalcPercentVehicleFilled
uint8 CalcPercentVehicleFilled(const Vehicle *v, StringID *colour)
Calculates how full a vehicle is.
Definition: vehicle.cpp:1413
CanVehicleUseStation
bool CanVehicleUseStation(EngineID engine_type, const Station *st)
Can this station be used by the given engine type?
Definition: vehicle.cpp:2838
GetEngineLiveryScheme
LiveryScheme GetEngineLiveryScheme(EngineID engine_type, EngineID parent_engine_type, const Vehicle *v)
Determines the LiveryScheme for a vehicle.
Definition: vehicle.cpp:1862
GetNewVehiclePosResult::y
int y
x and y position of the vehicle after moving
Definition: vehicle_func.h:76
gfx_type.h
VEH_SHIP
@ VEH_SHIP
Ship vehicle type.
Definition: vehicle_type.h:26
FindVehicleOnPos
void FindVehicleOnPos(TileIndex tile, void *data, VehicleFromPosProc *proc)
Find a vehicle from a specific location.
Definition: vehicle.cpp:498
Livery
Information about a particular livery.
Definition: livery.h:78
GetBestFittingSubType
byte GetBestFittingSubType(Vehicle *v_from, Vehicle *v_for, CargoID dest_cargo_type)
Get the best fitting subtype when 'cloning'/'replacing' v_from with v_for.
Definition: vehicle_gui.cpp:312
track_type.h
GetVehicleSet
void GetVehicleSet(VehicleSet &set, Vehicle *v, uint8 num_vehicles)
Calculates the set of vehicles that will be affected by a given selection.
Definition: vehicle.cpp:2946
DrawPixelInfo
Data about how and where to blit pixels.
Definition: gfx_type.h:155
VEHICLE_PROFIT_MIN_AGE
static const int VEHICLE_PROFIT_MIN_AGE
Only vehicles older than this have a meaningful profit.
Definition: vehicle_func.h:27
VEHICLE_PROFIT_THRESHOLD
static const Money VEHICLE_PROFIT_THRESHOLD
Threshold for a vehicle to be considered making good profit.
Definition: vehicle_func.h:28