OpenTTD

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