Fix #9521: Don't load at just removed docks that were part of a multi-dock station...
[openttd-github.git] / src / vehicle_func.h
blobec14e526afff594e464b1b27d9403b2fff819971
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 */
8 /** @file vehicle_func.h Functions related to vehicles. */
10 #ifndef VEHICLE_FUNC_H
11 #define VEHICLE_FUNC_H
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"
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)
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.
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.
36 template <VehicleType T>
37 bool IsValidImageIndex(uint8 image_index);
39 typedef Vehicle *VehicleFromPosProc(Vehicle *v, void *data);
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);
50 void VehicleLengthChanged(const Vehicle *u);
52 byte VehicleRandomBits();
53 void ResetVehicleHash();
54 void ResetVehicleColourMap();
56 byte GetBestFittingSubType(Vehicle *v_from, Vehicle *v_for, CargoID dest_cargo_type);
58 void ViewportAddVehicles(DrawPixelInfo *dpi);
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);
63 void DecreaseVehicleValue(Vehicle *v);
64 void CheckVehicleBreakdown(Vehicle *v);
65 void AgeVehicle(Vehicle *v);
66 void VehicleEnteredDepotThisTick(Vehicle *v);
68 UnitID GetFreeUnitNumber(VehicleType type);
70 void VehicleEnterDepot(Vehicle *v);
72 bool CanBuildVehicleInfrastructure(VehicleType type, byte subtype = 0);
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
81 GetNewVehiclePosResult GetNewVehiclePos(const Vehicle *v);
82 Direction GetDirectionTowards(const Vehicle *v, int x, int y);
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.
89 static inline bool IsCompanyBuildableVehicleType(VehicleType type)
91 switch (type) {
92 case VEH_TRAIN:
93 case VEH_ROAD:
94 case VEH_SHIP:
95 case VEH_AIRCRAFT:
96 return true;
98 default: return false;
103 * Is the given vehicle buildable by a company?
104 * @param v Vehicle being queried.
105 * @return Vehicle is buildable by a company.
107 static inline bool IsCompanyBuildableVehicleType(const BaseVehicle *v)
109 return IsCompanyBuildableVehicleType(v->type);
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);
115 SpriteID GetEnginePalette(EngineID engine_type, CompanyID company);
116 SpriteID GetVehiclePalette(const Vehicle *v);
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[];
123 /* Functions to find the right command for certain vehicle type */
124 static inline uint32 GetCmdBuildVeh(VehicleType type)
126 return _veh_build_proc_table[type];
129 static inline uint32 GetCmdBuildVeh(const BaseVehicle *v)
131 return GetCmdBuildVeh(v->type);
134 static inline uint32 GetCmdSellVeh(VehicleType type)
136 return _veh_sell_proc_table[type];
139 static inline uint32 GetCmdSellVeh(const BaseVehicle *v)
141 return GetCmdSellVeh(v->type);
144 static inline uint32 GetCmdRefitVeh(VehicleType type)
146 return _veh_refit_proc_table[type];
149 static inline uint32 GetCmdRefitVeh(const BaseVehicle *v)
151 return GetCmdRefitVeh(v->type);
154 static inline uint32 GetCmdSendToDepot(VehicleType type)
156 return _send_to_depot_proc_table[type];
159 static inline uint32 GetCmdSendToDepot(const BaseVehicle *v)
161 return GetCmdSendToDepot(v->type);
164 CommandCost EnsureNoVehicleOnGround(TileIndex tile);
165 CommandCost EnsureNoTrainOnTrackBits(TileIndex tile, TrackBits track_bits);
167 extern VehicleID _new_vehicle_id;
168 extern uint _returned_refit_capacity;
169 extern uint16 _returned_mail_refit_capacity;
171 bool CanVehicleUseStation(EngineID engine_type, const struct Station *st);
172 bool CanVehicleUseStation(const Vehicle *v, const struct Station *st);
174 void ReleaseDisastersTargetingVehicle(VehicleID vehicle);
176 typedef std::vector<VehicleID> VehicleSet;
177 void GetVehicleSet(VehicleSet &set, Vehicle *v, uint8 num_vehicles);
179 void CheckCargoCapacity(Vehicle *v);
181 #endif /* VEHICLE_FUNC_H */