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 
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 
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 
66 void CheckVehicleBreakdown(Vehicle *v);
67 void AgeVehicle(Vehicle *v);
69 
71 
72 void VehicleEnterDepot(Vehicle *v);
73 
75 
78  int x, y;
81 };
82 
84 Direction GetDirectionTowards(const Vehicle *v, int x, int y);
85 
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 {
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);
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 
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 
177 
179 void GetVehicleSet(VehicleSet &set, Vehicle *v, uint8 num_vehicles);
180 
181 void CheckCargoCapacity(Vehicle *v);
182 
183 #endif /* VEHICLE_FUNC_H */
All types related to tracks.
void CheckCargoCapacity(Vehicle *v)
Check the capacity of all vehicles in a chain and spread cargo if needed.
Different types to &#39;show&#39; directions.
static const int DAYS_IN_YEAR
days per year
Definition: date_type.h:31
Data about how and where to blit pixels.
Definition: gfx_type.h:156
static const int VEHICLE_PROFIT_MIN_AGE
Only vehicles older than this have a meaningful profit.
Definition: vehicle_func.h:29
LiveryScheme
List of different livery schemes.
Definition: livery.h:22
uint8 CalcPercentVehicleFilled(const Vehicle *v, StringID *colour)
Calculates how full a vehicle is.
Definition: vehicle.cpp:1367
bool IsValidImageIndex(uint8 image_index)
Helper to check whether an image index is valid for a particular vehicle.
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
Types related to engines.
Base types related to transport.
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.
Vehicle data structure.
Definition: vehicle_base.h:212
void FindVehicleOnPos(TileIndex tile, void *data, VehicleFromPosProc *proc)
Find a vehicle from a specific location.
Definition: vehicle.cpp:497
SpriteID GetVehiclePalette(const Vehicle *v)
Get the colour map for a vehicle.
Definition: vehicle.cpp:1954
Simple vector template class.
Common return value for all commands.
Definition: command_type.h:25
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
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
Defines the 8 directions on the map.
Types related to commands.
CommandCost TunnelBridgeIsFree(TileIndex tile, TileIndex endtile, const Vehicle *ignore=NULL)
Finds vehicle in tunnel / bridge.
Definition: vehicle.cpp:567
TrackBits
Bitfield corresponding to Track.
Definition: track_type.h:41
Aircraft vehicle type.
Definition: vehicle_type.h:27
void VehicleServiceInDepot(Vehicle *v)
Service a vehicle and all subsequent vehicles in the consist.
Definition: vehicle.cpp:163
int y
x and y position of the vehicle after moving
Definition: vehicle_func.h:78
byte VehicleRandomBits()
Get a value for a vehicle&#39;s random_bits.
Definition: vehicle.cpp:362
VehicleType
Available vehicle types.
Definition: vehicle_type.h:21
LiveryScheme GetEngineLiveryScheme(EngineID engine_type, EngineID parent_engine_type, const Vehicle *v)
Determines the LiveryScheme for a vehicle.
Definition: vehicle.cpp:1786
uint32 VehicleID
The type all our vehicle IDs have.
Definition: vehicle_type.h:18
SpriteID GetEnginePalette(EngineID engine_type, CompanyID company)
Get the colour map for an engine.
Definition: vehicle.cpp:1944
void AgeVehicle(Vehicle *v)
Update age of a vehicle.
Definition: vehicle.cpp:1320
Information about a particular livery.
Definition: livery.h:76
void FindVehicleOnPosXY(int x, int y, void *data, VehicleFromPosProc *proc)
Find a vehicle from a specific location.
Definition: vehicle.cpp:437
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
Encountered GRF bugs.
Definition: newgrf_config.h:44
bool CanVehicleUseStation(EngineID engine_type, const Station *st)
Can this station be used by the given engine type?
Definition: vehicle.cpp:2752
CommandCost EnsureNoVehicleOnGround(TileIndex tile)
Ensure there is no vehicle at the ground at the given position.
Definition: vehicle.cpp:538
UnitID GetFreeUnitNumber(VehicleType type)
Get an unused unit number for a vehicle (if allowed).
Definition: vehicle.cpp:1714
Road vehicle type.
Definition: vehicle_type.h:25
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:18
bool CanBuildVehicleInfrastructure(VehicleType type)
Check whether we can build infrastructure for the given vehicle type.
Definition: vehicle.cpp:1743
Functions to find and configure NewGRFs.
bool HasVehicleOnPos(TileIndex tile, void *data, VehicleFromPosProc *proc)
Checks whether a vehicle is on a specific location.
Definition: vehicle.cpp:512
Ship vehicle type.
Definition: vehicle_type.h:26
TileIndex old_tile
Current tile of the vehicle.
Definition: vehicle_func.h:79
void DecreaseVehicleValue(Vehicle *v)
Decrease the value of a vehicle.
Definition: vehicle.cpp:1195
Functions/types related to livery colours.
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:19
void ViewportAddVehicles(DrawPixelInfo *dpi)
Add the vehicle sprites that should be drawn at a part of the screen.
Definition: vehicle.cpp:1103
uint16 EngineID
Unique identification number of an engine.
Definition: engine_type.h:22
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
CommandCost EnsureNoTrainOnTrackBits(TileIndex tile, TrackBits track_bits)
Tests if a vehicle interacts with the specified track bits.
Definition: vehicle.cpp:600
Position information of a vehicle after it moved.
Definition: vehicle_func.h:77
Types related to vehicles.
static const Money VEHICLE_PROFIT_THRESHOLD
Threshold for a vehicle to be considered making good profit.
Definition: vehicle_func.h:30
uint16 _returned_refit_capacity
Stores the capacity after a refit operation.
Definition: vehicle.cpp:86
Base vehicle class.
Definition: vehicle_type.h:52
uint16 UnitID
Type for the company global vehicle unit number.
void ReleaseDisastersTargetingVehicle(VehicleID vehicle)
Notify disasters that we are about to delete a vehicle.
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
bool HasVehicleOnPosXY(int x, int y, void *data, VehicleFromPosProc *proc)
Checks whether a vehicle in on a specific location.
Definition: vehicle.cpp:453
void VehicleEnteredDepotThisTick(Vehicle *v)
Adds a vehicle to the list of vehicles that visited a depot this tick.
Definition: vehicle.cpp:894
Owner
Enum for all companies/owners.
Definition: company_type.h:20
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
TileIndex new_tile
Tile of the vehicle after moving.
Definition: vehicle_func.h:80
static bool IsCompanyBuildableVehicleType(VehicleType type)
Is the given vehicle type buildable by a company?
Definition: vehicle_func.h:91
VehicleTypeByte type
Type of vehicle.
Definition: vehicle_type.h:54
uint16 _returned_mail_refit_capacity
Stores the mail capacity after a refit operation (Aircraft only).
Definition: vehicle.cpp:87
Station data structure.
Definition: station_base.h:446
void VehicleEnterDepot(Vehicle *v)
Vehicle entirely entered the depot, update its status, orders, vehicle windows, service it...
Definition: vehicle.cpp:1426
Types related to the graphics and/or input devices.
Train vehicle type.
Definition: vehicle_type.h:24