2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file vehicle_func.h Functions related to vehicles. */
|
2007-12-27 13:35:39 +00:00
|
|
|
|
|
|
|
#ifndef VEHICLE_FUNC_H
|
|
|
|
#define VEHICLE_FUNC_H
|
|
|
|
|
|
|
|
#include "gfx_type.h"
|
|
|
|
#include "direction_type.h"
|
|
|
|
#include "command_type.h"
|
|
|
|
#include "vehicle_type.h"
|
2008-03-28 18:00:38 +00:00
|
|
|
#include "engine_type.h"
|
2008-05-07 09:07:19 +00:00
|
|
|
#include "transport_type.h"
|
2009-03-13 21:28:40 +00:00
|
|
|
#include "newgrf_config.h"
|
2010-03-07 15:54:58 +00:00
|
|
|
#include "track_type.h"
|
2010-09-25 21:57:52 +00:00
|
|
|
#include "livery.h"
|
2022-09-16 19:10:53 +00:00
|
|
|
#include "cargo_type.h"
|
2007-12-27 13:35:39 +00:00
|
|
|
|
|
|
|
#define is_custom_sprite(x) (x >= 0xFD)
|
|
|
|
#define IS_CUSTOM_FIRSTHEAD_SPRITE(x) (x == 0xFD)
|
|
|
|
#define IS_CUSTOM_SECONDHEAD_SPRITE(x) (x == 0xFE)
|
|
|
|
|
2011-10-03 17:25:44 +00:00
|
|
|
static const int VEHICLE_PROFIT_MIN_AGE = DAYS_IN_YEAR * 2; ///< Only vehicles older than this have a meaningful profit.
|
|
|
|
static const Money VEHICLE_PROFIT_THRESHOLD = 10000; ///< Threshold for a vehicle to be considered making good profit.
|
|
|
|
|
2020-08-27 17:31:09 +00:00
|
|
|
struct Viewport;
|
2020-03-04 22:57:47 +00:00
|
|
|
|
2013-11-26 16:08:58 +00:00
|
|
|
/**
|
|
|
|
* Helper to check whether an image index is valid for a particular vehicle.
|
2018-10-28 02:17:36 +00:00
|
|
|
* @tparam T The type of vehicle.
|
2013-11-26 16:08:58 +00:00
|
|
|
* @param image_index The image index to check.
|
|
|
|
* @return True iff the image index is valid.
|
|
|
|
*/
|
|
|
|
template <VehicleType T>
|
|
|
|
bool IsValidImageIndex(uint8 image_index);
|
|
|
|
|
2008-08-01 15:07:31 +00:00
|
|
|
typedef Vehicle *VehicleFromPosProc(Vehicle *v, void *data);
|
2007-12-27 13:35:39 +00:00
|
|
|
|
|
|
|
void VehicleServiceInDepot(Vehicle *v);
|
|
|
|
uint CountVehiclesInChain(const Vehicle *v);
|
2020-02-28 22:21:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a vehicle from a specific location. It will call \a proc for ALL vehicles
|
|
|
|
* on the tile and YOU must make SURE that the "best one" is stored in the
|
|
|
|
* data value and is ALWAYS the same regardless of the order of the vehicles
|
|
|
|
* where proc was called on!
|
|
|
|
* When you fail to do this properly you create an almost untraceable DESYNC!
|
|
|
|
* @note The return value of \a proc will be ignored.
|
|
|
|
* @note Use this function when you have the intention that all vehicles
|
|
|
|
* should be iterated over.
|
|
|
|
* @param tile The location on the map
|
|
|
|
* @param data Arbitrary data passed to \a proc.
|
|
|
|
* @param proc The proc that determines whether a vehicle will be "found".
|
|
|
|
*/
|
|
|
|
inline void FindVehicleOnPos(TileIndex tile, VehicleType type, void *data, VehicleFromPosProc *proc)
|
|
|
|
{
|
|
|
|
extern Vehicle *VehicleFromPos(TileIndex tile, VehicleType type, void *data, VehicleFromPosProc *proc, bool find_first);
|
|
|
|
VehicleFromPos(tile, type, data, proc, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether a vehicle is on a specific location. It will call \a proc for
|
|
|
|
* vehicles until it returns non-nullptr.
|
|
|
|
* @note Use #FindVehicleOnPos when you have the intention that all vehicles
|
|
|
|
* should be iterated over.
|
|
|
|
* @param tile The location on the map
|
|
|
|
* @param data Arbitrary data passed to \a proc.
|
|
|
|
* @param proc The \a proc that determines whether a vehicle will be "found".
|
|
|
|
* @return True if proc returned non-nullptr.
|
|
|
|
*/
|
|
|
|
inline bool HasVehicleOnPos(TileIndex tile, VehicleType type, void *data, VehicleFromPosProc *proc)
|
|
|
|
{
|
|
|
|
extern Vehicle *VehicleFromPos(TileIndex tile, VehicleType type, void *data, VehicleFromPosProc *proc, bool find_first);
|
|
|
|
return VehicleFromPos(tile, type, data, proc, true) != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a vehicle from a specific location. It will call proc for ALL vehicles
|
|
|
|
* on the tile and YOU must make SURE that the "best one" is stored in the
|
|
|
|
* data value and is ALWAYS the same regardless of the order of the vehicles
|
|
|
|
* where proc was called on!
|
|
|
|
* When you fail to do this properly you create an almost untraceable DESYNC!
|
|
|
|
* @note The return value of proc will be ignored.
|
|
|
|
* @note Use this when you have the intention that all vehicles
|
|
|
|
* should be iterated over.
|
|
|
|
* @param x The X location on the map
|
|
|
|
* @param y The Y location on the map
|
|
|
|
* @param data Arbitrary data passed to proc
|
|
|
|
* @param proc The proc that determines whether a vehicle will be "found".
|
|
|
|
*/
|
|
|
|
inline void FindVehicleOnPosXY(int x, int y, VehicleType type, void *data, VehicleFromPosProc *proc)
|
|
|
|
{
|
|
|
|
extern Vehicle *VehicleFromPosXY(int x, int y, VehicleType type, void *data, VehicleFromPosProc *proc, bool find_first);
|
|
|
|
VehicleFromPosXY(x, y, type, data, proc, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether a vehicle in on a specific location. It will call proc for
|
|
|
|
* vehicles until it returns non-nullptr.
|
|
|
|
* @note Use FindVehicleOnPosXY when you have the intention that all vehicles
|
|
|
|
* should be iterated over.
|
|
|
|
* @param x The X location on the map
|
|
|
|
* @param y The Y location on the map
|
|
|
|
* @param data Arbitrary data passed to proc
|
|
|
|
* @param proc The proc that determines whether a vehicle will be "found".
|
|
|
|
* @return True if proc returned non-nullptr.
|
|
|
|
*/
|
|
|
|
inline bool HasVehicleOnPosXY(int x, int y, VehicleType type, void *data, VehicleFromPosProc *proc)
|
|
|
|
{
|
|
|
|
extern Vehicle *VehicleFromPosXY(int x, int y, VehicleType type, void *data, VehicleFromPosProc *proc, bool find_first);
|
|
|
|
return VehicleFromPosXY(x, y, type, data, proc, true) != nullptr;
|
|
|
|
}
|
|
|
|
|
2007-12-27 13:35:39 +00:00
|
|
|
void CallVehicleTicks();
|
2009-02-09 02:57:15 +00:00
|
|
|
uint8 CalcPercentVehicleFilled(const Vehicle *v, StringID *colour);
|
2020-01-18 20:32:42 +00:00
|
|
|
uint8 CalcPercentVehicleFilledOfCargo(const Vehicle *v, CargoID cargo);
|
2007-12-27 13:35:39 +00:00
|
|
|
|
2011-11-04 15:04:29 +00:00
|
|
|
void VehicleLengthChanged(const Vehicle *u);
|
|
|
|
|
2012-01-02 12:07:50 +00:00
|
|
|
void ResetVehicleHash();
|
2009-02-09 02:57:15 +00:00
|
|
|
void ResetVehicleColourMap();
|
2007-12-27 13:35:39 +00:00
|
|
|
|
2011-12-10 21:09:21 +00:00
|
|
|
byte GetBestFittingSubType(Vehicle *v_from, Vehicle *v_for, CargoID dest_cargo_type);
|
2007-12-27 13:35:39 +00:00
|
|
|
|
2021-05-20 17:03:11 +00:00
|
|
|
void ViewportAddVehicles(DrawPixelInfo *dpi, bool update_vehicles);
|
2020-08-27 17:31:09 +00:00
|
|
|
void ViewportMapDrawVehicles(DrawPixelInfo *dpi, Viewport *vp);
|
2007-12-27 13:35:39 +00:00
|
|
|
|
2009-03-13 21:28:40 +00:00
|
|
|
void ShowNewGrfVehicleError(EngineID engine, StringID part1, StringID part2, GRFBugs bug_type, bool critical);
|
2023-05-06 14:48:27 +00:00
|
|
|
|
|
|
|
enum TunnelBridgeIsFreeMode {
|
|
|
|
TBIFM_ALL,
|
|
|
|
TBIFM_ACROSS_ONLY,
|
2023-05-06 15:20:23 +00:00
|
|
|
TBIFM_PRIMARY_ONLY,
|
2023-05-06 14:48:27 +00:00
|
|
|
};
|
|
|
|
CommandCost TunnelBridgeIsFree(TileIndex tile, TileIndex endtile, const Vehicle *ignore = nullptr, TunnelBridgeIsFreeMode mode = TBIFM_ALL);
|
2021-01-25 02:33:14 +00:00
|
|
|
Train *GetTrainClosestToTunnelBridgeEnd(TileIndex tile, TileIndex other_tile);
|
|
|
|
int GetAvailableFreeTilesInSignalledTunnelBridge(TileIndex entrance, TileIndex exit, TileIndex tile);
|
|
|
|
int GetAvailableFreeTilesInSignalledTunnelBridgeWithStartOffset(TileIndex entrance, TileIndex exit, int offset);
|
2007-12-27 13:35:39 +00:00
|
|
|
|
|
|
|
void DecreaseVehicleValue(Vehicle *v);
|
|
|
|
void CheckVehicleBreakdown(Vehicle *v);
|
|
|
|
void AgeVehicle(Vehicle *v);
|
|
|
|
void VehicleEnteredDepotThisTick(Vehicle *v);
|
|
|
|
|
|
|
|
UnitID GetFreeUnitNumber(VehicleType type);
|
|
|
|
|
|
|
|
void VehicleEnterDepot(Vehicle *v);
|
|
|
|
|
2019-04-06 06:46:15 +00:00
|
|
|
bool CanBuildVehicleInfrastructure(VehicleType type, byte subtype = 0);
|
2007-12-27 13:35:39 +00:00
|
|
|
|
2008-10-19 15:39:12 +00:00
|
|
|
/** Position information of a vehicle after it moved */
|
2007-12-27 13:35:39 +00:00
|
|
|
struct GetNewVehiclePosResult {
|
2008-10-19 15:39:12 +00:00
|
|
|
int x, y; ///< x and y position of the vehicle after moving
|
|
|
|
TileIndex old_tile; ///< Current tile of the vehicle
|
|
|
|
TileIndex new_tile; ///< Tile of the vehicle after moving
|
2007-12-27 13:35:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
GetNewVehiclePosResult GetNewVehiclePos(const Vehicle *v);
|
|
|
|
Direction GetDirectionTowards(const Vehicle *v, int x, int y);
|
|
|
|
|
2010-11-05 16:34:22 +00:00
|
|
|
/**
|
|
|
|
* Is the given vehicle type buildable by a company?
|
|
|
|
* @param type Vehicle type being queried.
|
|
|
|
* @return Vehicle type is buildable by a company.
|
|
|
|
*/
|
2008-09-30 20:39:50 +00:00
|
|
|
static inline bool IsCompanyBuildableVehicleType(VehicleType type)
|
2007-12-27 13:35:39 +00:00
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case VEH_TRAIN:
|
|
|
|
case VEH_ROAD:
|
|
|
|
case VEH_SHIP:
|
|
|
|
case VEH_AIRCRAFT:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-05 16:34:22 +00:00
|
|
|
/**
|
|
|
|
* Is the given vehicle buildable by a company?
|
|
|
|
* @param v Vehicle being queried.
|
|
|
|
* @return Vehicle is buildable by a company.
|
|
|
|
*/
|
2008-09-30 20:39:50 +00:00
|
|
|
static inline bool IsCompanyBuildableVehicleType(const BaseVehicle *v)
|
2007-12-27 13:35:39 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
return IsCompanyBuildableVehicleType(v->type);
|
2007-12-27 13:35:39 +00:00
|
|
|
}
|
|
|
|
|
2010-09-25 21:57:52 +00:00
|
|
|
LiveryScheme GetEngineLiveryScheme(EngineID engine_type, EngineID parent_engine_type, const Vehicle *v);
|
2020-12-26 09:37:26 +00:00
|
|
|
const struct Livery *GetEngineLivery(EngineID engine_type, CompanyID company, EngineID parent_engine_type, const Vehicle *v, byte livery_setting, bool ignore_group = false);
|
2007-12-27 13:35:39 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
SpriteID GetEnginePalette(EngineID engine_type, CompanyID company);
|
2007-12-27 13:35:39 +00:00
|
|
|
SpriteID GetVehiclePalette(const Vehicle *v);
|
2020-12-26 09:37:26 +00:00
|
|
|
SpriteID GetUncachedTrainPaletteIgnoringGroup(const Train *v);
|
2007-12-27 13:35:39 +00:00
|
|
|
|
|
|
|
extern const uint32 _veh_build_proc_table[];
|
|
|
|
extern const uint32 _veh_sell_proc_table[];
|
|
|
|
extern const uint32 _veh_refit_proc_table[];
|
|
|
|
extern const uint32 _send_to_depot_proc_table[];
|
|
|
|
|
|
|
|
/* Functions to find the right command for certain vehicle type */
|
|
|
|
static inline uint32 GetCmdBuildVeh(VehicleType type)
|
|
|
|
{
|
|
|
|
return _veh_build_proc_table[type];
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint32 GetCmdBuildVeh(const BaseVehicle *v)
|
|
|
|
{
|
|
|
|
return GetCmdBuildVeh(v->type);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint32 GetCmdSellVeh(VehicleType type)
|
|
|
|
{
|
|
|
|
return _veh_sell_proc_table[type];
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint32 GetCmdSellVeh(const BaseVehicle *v)
|
|
|
|
{
|
|
|
|
return GetCmdSellVeh(v->type);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint32 GetCmdRefitVeh(VehicleType type)
|
|
|
|
{
|
|
|
|
return _veh_refit_proc_table[type];
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint32 GetCmdRefitVeh(const BaseVehicle *v)
|
|
|
|
{
|
|
|
|
return GetCmdRefitVeh(v->type);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint32 GetCmdSendToDepot(VehicleType type)
|
|
|
|
{
|
|
|
|
return _send_to_depot_proc_table[type];
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint32 GetCmdSendToDepot(const BaseVehicle *v)
|
|
|
|
{
|
|
|
|
return GetCmdSendToDepot(v->type);
|
|
|
|
}
|
|
|
|
|
2010-03-05 21:20:22 +00:00
|
|
|
CommandCost EnsureNoVehicleOnGround(TileIndex tile);
|
2023-02-19 15:57:21 +00:00
|
|
|
bool IsTrainCollidableRoadVehicleOnGround(TileIndex tile);
|
2010-03-07 18:24:41 +00:00
|
|
|
CommandCost EnsureNoTrainOnTrackBits(TileIndex tile, TrackBits track_bits);
|
2007-12-27 13:35:39 +00:00
|
|
|
|
|
|
|
extern VehicleID _new_vehicle_id;
|
2020-04-06 14:39:58 +00:00
|
|
|
extern uint _returned_refit_capacity;
|
2010-11-16 20:56:04 +00:00
|
|
|
extern uint16 _returned_mail_refit_capacity;
|
2022-09-16 19:10:53 +00:00
|
|
|
extern CargoArray _returned_vehicle_capacities;
|
2007-12-27 13:35:39 +00:00
|
|
|
|
2009-01-10 09:51:14 +00:00
|
|
|
bool CanVehicleUseStation(EngineID engine_type, const struct Station *st);
|
|
|
|
bool CanVehicleUseStation(const Vehicle *v, const struct Station *st);
|
2022-03-29 21:14:12 +00:00
|
|
|
StringID GetVehicleCannotUseStationReason(const Vehicle *v, const Station *st);
|
2009-01-10 09:51:14 +00:00
|
|
|
|
2009-06-06 14:46:50 +00:00
|
|
|
void ReleaseDisastersTargetingVehicle(VehicleID vehicle);
|
|
|
|
|
2019-03-03 17:30:09 +00:00
|
|
|
typedef std::vector<VehicleID> VehicleSet;
|
2010-12-21 13:52:10 +00:00
|
|
|
void GetVehicleSet(VehicleSet &set, Vehicle *v, uint8 num_vehicles);
|
|
|
|
|
2011-12-28 19:48:04 +00:00
|
|
|
void CheckCargoCapacity(Vehicle *v);
|
|
|
|
|
2022-11-26 17:03:21 +00:00
|
|
|
bool VehiclesHaveSameEngineList(const Vehicle *v1, const Vehicle *v2);
|
|
|
|
bool VehiclesHaveSameOrderList(const Vehicle *v1, const Vehicle *v2);
|
|
|
|
|
2023-06-09 21:18:55 +00:00
|
|
|
bool IsUniqueVehicleName(const char *name);
|
|
|
|
|
2008-04-20 11:12:07 +00:00
|
|
|
#endif /* VEHICLE_FUNC_H */
|