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 
8 /** @file vehicle_func.h Functions related to vehicles. */
9 
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; ///< Only vehicles older than this have a meaningful profit.
28 static const Money VEHICLE_PROFIT_THRESHOLD = 10000;        ///< Threshold for a vehicle to be considered making good profit.
29 
30 /**
31  * Helper to check whether an image index is valid for a particular vehicle.
32  * @tparam T The type of vehicle.
33  * @param image_index The image index to check.
34  * @return True iff the image index is valid.
35  */
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 
74 /** Position information of a vehicle after it moved */
75 struct GetNewVehiclePosResult {
76 	int x, y;  ///< x and y position of the vehicle after moving
77 	TileIndex old_tile; ///< Current tile of the vehicle
78 	TileIndex new_tile; ///< Tile of the vehicle after moving
79 };
80 
81 GetNewVehiclePosResult GetNewVehiclePos(const Vehicle *v);
82 Direction GetDirectionTowards(const Vehicle *v, int x, int y);
83 
84 /**
85  * Is the given vehicle type buildable by a company?
86  * @param type Vehicle type being queried.
87  * @return Vehicle type is buildable by a company.
88  */
IsCompanyBuildableVehicleType(VehicleType type)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 
102 /**
103  * Is the given vehicle buildable by a company?
104  * @param v Vehicle being queried.
105  * @return Vehicle is buildable by a company.
106  */
IsCompanyBuildableVehicleType(const BaseVehicle * v)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 */
GetCmdBuildVeh(VehicleType type)124 static inline uint32 GetCmdBuildVeh(VehicleType type)
125 {
126 	return _veh_build_proc_table[type];
127 }
128 
GetCmdBuildVeh(const BaseVehicle * v)129 static inline uint32 GetCmdBuildVeh(const BaseVehicle *v)
130 {
131 	return GetCmdBuildVeh(v->type);
132 }
133 
GetCmdSellVeh(VehicleType type)134 static inline uint32 GetCmdSellVeh(VehicleType type)
135 {
136 	return _veh_sell_proc_table[type];
137 }
138 
GetCmdSellVeh(const BaseVehicle * v)139 static inline uint32 GetCmdSellVeh(const BaseVehicle *v)
140 {
141 	return GetCmdSellVeh(v->type);
142 }
143 
GetCmdRefitVeh(VehicleType type)144 static inline uint32 GetCmdRefitVeh(VehicleType type)
145 {
146 	return _veh_refit_proc_table[type];
147 }
148 
GetCmdRefitVeh(const BaseVehicle * v)149 static inline uint32 GetCmdRefitVeh(const BaseVehicle *v)
150 {
151 	return GetCmdRefitVeh(v->type);
152 }
153 
GetCmdSendToDepot(VehicleType type)154 static inline uint32 GetCmdSendToDepot(VehicleType type)
155 {
156 	return _send_to_depot_proc_table[type];
157 }
158 
GetCmdSendToDepot(const BaseVehicle * v)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 */
182