Fix some daylength issues, possible division by zero in main menu.
[openttd-joker.git] / src / vehicle_func.h
bloba06449d62b70b9b8f3d1f073a65ba7eefdca2110
1 /* $Id$ */
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 */
10 /** @file vehicle_func.h Functions related to vehicles. */
12 #ifndef VEHICLE_FUNC_H
13 #define VEHICLE_FUNC_H
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"
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)
29 static const int VEHICLE_PROFIT_MIN_AGE = DAYS_IN_YEAR * 2; ///< Only vehicles older than this have a meaningful profit.
30 static const Money VEHICLE_PROFIT_THRESHOLD = 10000; ///< Threshold for a vehicle to be considered making good profit.
32 /**
33 * Helper to check whether an image index is valid for a particular vehicle.
34 * @param <T> The type of vehicle.
35 * @param image_index The image index to check.
36 * @return True iff the image index is valid.
38 template <VehicleType T>
39 bool IsValidImageIndex(uint8 image_index);
41 typedef Vehicle *VehicleFromPosProc(Vehicle *v, void *data);
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);
52 void VehicleLengthChanged(const Vehicle *u);
54 byte VehicleRandomBits();
55 void ResetVehicleHash();
56 void ResetVehicleColourMap();
58 byte GetBestFittingSubType(Vehicle *v_from, Vehicle *v_for, CargoID dest_cargo_type);
60 void ViewportAddVehicles(DrawPixelInfo *dpi);
61 void ViewportMapDrawVehicles(DrawPixelInfo *dpi);
63 void ShowNewGrfVehicleError(EngineID engine, StringID part1, StringID part2, GRFBugs bug_type, bool critical);
64 CommandCost TunnelBridgeIsFree(TileIndex tile, TileIndex endtile, const Vehicle *ignore = NULL);
66 void DecreaseVehicleValue(Vehicle *v);
67 void CheckVehicleBreakdown(Vehicle *v);
68 void AgeVehicle(Vehicle *v);
69 void VehicleEnteredDepotThisTick(Vehicle *v);
71 UnitID GetFreeUnitNumber(VehicleType type);
73 void VehicleEnterDepot(Vehicle *v);
75 bool CanBuildVehicleInfrastructure(VehicleType type);
77 /** Position information of a vehicle after it moved */
78 struct GetNewVehiclePosResult {
79 int x, y; ///< x and y position of the vehicle after moving
80 TileIndex old_tile; ///< Current tile of the vehicle
81 TileIndex new_tile; ///< Tile of the vehicle after moving
84 GetNewVehiclePosResult GetNewVehiclePos(const Vehicle *v);
85 Direction GetDirectionTowards(const Vehicle *v, int x, int y);
87 /**
88 * Is the given vehicle type buildable by a company?
89 * @param type Vehicle type being queried.
90 * @return Vehicle type is buildable by a company.
92 static inline bool IsCompanyBuildableVehicleType(VehicleType type)
94 switch (type) {
95 case VEH_TRAIN:
96 case VEH_ROAD:
97 case VEH_SHIP:
98 case VEH_AIRCRAFT:
99 return true;
101 default: return false;
106 * Is the given vehicle buildable by a company?
107 * @param v Vehicle being queried.
108 * @return Vehicle is buildable by a company.
110 static inline bool IsCompanyBuildableVehicleType(const BaseVehicle *v)
112 return IsCompanyBuildableVehicleType(v->type);
115 LiveryScheme GetEngineLiveryScheme(EngineID engine_type, EngineID parent_engine_type, const Vehicle *v);
116 const struct Livery *GetEngineLivery(EngineID engine_type, CompanyID company, EngineID parent_engine_type, const Vehicle *v, byte livery_setting);
118 SpriteID GetEnginePalette(EngineID engine_type, CompanyID company);
119 SpriteID GetVehiclePalette(const Vehicle *v);
121 extern const uint32 _veh_build_proc_table[];
122 extern const uint32 _veh_sell_proc_table[];
123 extern const uint32 _veh_refit_proc_table[];
124 extern const uint32 _send_to_depot_proc_table[];
126 /* Functions to find the right command for certain vehicle type */
127 static inline uint32 GetCmdBuildVeh(VehicleType type)
129 return _veh_build_proc_table[type];
132 static inline uint32 GetCmdBuildVeh(const BaseVehicle *v)
134 return GetCmdBuildVeh(v->type);
137 static inline uint32 GetCmdSellVeh(VehicleType type)
139 return _veh_sell_proc_table[type];
142 static inline uint32 GetCmdSellVeh(const BaseVehicle *v)
144 return GetCmdSellVeh(v->type);
147 static inline uint32 GetCmdRefitVeh(VehicleType type)
149 return _veh_refit_proc_table[type];
152 static inline uint32 GetCmdRefitVeh(const BaseVehicle *v)
154 return GetCmdRefitVeh(v->type);
157 static inline uint32 GetCmdSendToDepot(VehicleType type)
159 return _send_to_depot_proc_table[type];
162 static inline uint32 GetCmdSendToDepot(const BaseVehicle *v)
164 return GetCmdSendToDepot(v->type);
167 CommandCost EnsureNoVehicleOnGround(TileIndex tile);
168 CommandCost EnsureNoTrainOnTrackBits(TileIndex tile, TrackBits track_bits);
170 extern VehicleID _new_vehicle_id;
171 extern uint16 _returned_refit_capacity;
172 extern uint16 _returned_mail_refit_capacity;
174 bool CanVehicleUseStation(EngineID engine_type, const struct Station *st);
175 bool CanVehicleUseStation(const Vehicle *v, const struct Station *st);
177 void ReleaseDisastersTargetingVehicle(VehicleID vehicle);
179 typedef SmallVector<VehicleID, 2> VehicleSet;
180 void GetVehicleSet(VehicleSet &set, Vehicle *v, uint8 num_vehicles);
182 void CheckCargoCapacity(Vehicle *v);
184 #endif /* VEHICLE_FUNC_H */