OpenTTD Source 20241224-master-gf74b0cf984
newgrf_engine.cpp File Reference

NewGRF handling of engines. More...

#include "stdafx.h"
#include "debug.h"
#include "train.h"
#include "roadveh.h"
#include "company_func.h"
#include "newgrf_cargo.h"
#include "newgrf_spritegroup.h"
#include "timer/timer_game_calendar.h"
#include "vehicle_func.h"
#include "core/random_func.hpp"
#include "core/container_func.hpp"
#include "aircraft.h"
#include "station_base.h"
#include "company_base.h"
#include "newgrf_railtype.h"
#include "newgrf_roadtype.h"
#include "ship.h"
#include "safeguards.h"

Go to the source code of this file.

Data Structures

struct  ListOrderChange
 

Enumerations

enum  TTDPAircraftMovementStates {
  AMS_TTDP_HANGAR , AMS_TTDP_TO_HANGAR , AMS_TTDP_TO_PAD1 , AMS_TTDP_TO_PAD2 ,
  AMS_TTDP_TO_PAD3 , AMS_TTDP_TO_ENTRY_2_AND_3 , AMS_TTDP_TO_ENTRY_2_AND_3_AND_H , AMS_TTDP_TO_JUNCTION ,
  AMS_TTDP_LEAVE_RUNWAY , AMS_TTDP_TO_INWAY , AMS_TTDP_TO_RUNWAY , AMS_TTDP_TO_OUTWAY ,
  AMS_TTDP_WAITING , AMS_TTDP_TAKEOFF , AMS_TTDP_TO_TAKEOFF , AMS_TTDP_CLIMBING ,
  AMS_TTDP_FLIGHT_APPROACH , AMS_TTDP_UNUSED_0x11 , AMS_TTDP_FLIGHT_TO_TOWER , AMS_TTDP_UNUSED_0x13 ,
  AMS_TTDP_FLIGHT_FINAL , AMS_TTDP_FLIGHT_DESCENT , AMS_TTDP_BRAKING , AMS_TTDP_HELI_TAKEOFF_AIRPORT ,
  AMS_TTDP_HELI_TO_TAKEOFF_AIRPORT , AMS_TTDP_HELI_LAND_AIRPORT , AMS_TTDP_HELI_TAKEOFF_HELIPORT , AMS_TTDP_HELI_TO_TAKEOFF_HELIPORT ,
  AMS_TTDP_HELI_LAND_HELIPORT
}
 
enum  TTDPAircraftMovementActions {
  AMA_TTDP_IN_HANGAR , AMA_TTDP_ON_PAD1 , AMA_TTDP_ON_PAD2 , AMA_TTDP_ON_PAD3 ,
  AMA_TTDP_HANGAR_TO_PAD1 , AMA_TTDP_HANGAR_TO_PAD2 , AMA_TTDP_HANGAR_TO_PAD3 , AMA_TTDP_LANDING_TO_PAD1 ,
  AMA_TTDP_LANDING_TO_PAD2 , AMA_TTDP_LANDING_TO_PAD3 , AMA_TTDP_PAD1_TO_HANGAR , AMA_TTDP_PAD2_TO_HANGAR ,
  AMA_TTDP_PAD3_TO_HANGAR , AMA_TTDP_PAD1_TO_TAKEOFF , AMA_TTDP_PAD2_TO_TAKEOFF , AMA_TTDP_PAD3_TO_TAKEOFF ,
  AMA_TTDP_HANGAR_TO_TAKOFF , AMA_TTDP_LANDING_TO_HANGAR , AMA_TTDP_IN_FLIGHT
}
 

Functions

void SetWagonOverrideSprites (EngineID engine, CargoID cargo, const SpriteGroup *group, std::span< EngineID > engine_ids)
 
const SpriteGroupGetWagonOverrideSpriteSet (EngineID engine, CargoID cargo, EngineID overriding_engine)
 
void SetCustomEngineSprites (EngineID engine, uint8_t cargo, const SpriteGroup *group)
 
void SetEngineGRF (EngineID engine, const GRFFile *file)
 Tie a GRFFile entry to an engine, to allow us to retrieve GRF parameters etc during a game.
 
static int MapOldSubType (const Vehicle *v)
 
static uint8_t MapAircraftMovementState (const Aircraft *v)
 Map OTTD aircraft movement states to TTDPatch style movement states (VarAction 2 Variable 0xE2)
 
static uint8_t MapAircraftMovementAction (const Aircraft *v)
 Map OTTD aircraft movement states to TTDPatch style movement actions (VarAction 2 Variable 0xE6) This is not fully supported yet but it's enough for Planeset.
 
static const LiveryLiveryHelper (EngineID engine, const Vehicle *v)
 Determines the livery of an engine.
 
static uint32_t PositionHelper (const Vehicle *v, bool consecutive)
 Helper to get the position of a vehicle within a chain of vehicles.
 
static uint32_t VehicleGetVariable (Vehicle *v, const VehicleScopeResolver *object, uint8_t variable, uint32_t parameter, bool &available)
 
static const GRFFileGetEngineGrfFile (EngineID engine_type)
 Get the grf file associated with an engine type.
 
void GetCustomEngineSprite (EngineID engine, const Vehicle *v, Direction direction, EngineImageType image_type, VehicleSpriteSeq *result)
 
void GetRotorOverrideSprite (EngineID engine, const struct Aircraft *v, EngineImageType image_type, VehicleSpriteSeq *result)
 
bool UsesWagonOverride (const Vehicle *v)
 Check if a wagon is currently using a wagon override.
 
uint16_t GetVehicleCallback (CallbackID callback, uint32_t param1, uint32_t param2, EngineID engine, const Vehicle *v)
 Evaluate a newgrf callback for vehicles.
 
uint16_t GetVehicleCallbackParent (CallbackID callback, uint32_t param1, uint32_t param2, EngineID engine, const Vehicle *v, const Vehicle *parent)
 Evaluate a newgrf callback for vehicles with a different vehicle for parent scope.
 
int GetVehicleProperty (const Vehicle *v, PropertyID property, int orig_value, bool is_signed)
 
int GetEngineProperty (EngineID engine, PropertyID property, int orig_value, const Vehicle *v, bool is_signed)
 
bool TestVehicleBuildProbability (Vehicle *v, EngineID engine, BuildProbabilityType type)
 Test for vehicle build probablity type.
 
static void DoTriggerVehicle (Vehicle *v, VehicleTrigger trigger, uint16_t base_random_bits, bool first)
 
void TriggerVehicle (Vehicle *v, VehicleTrigger trigger)
 
void AlterVehicleListOrder (EngineID engine, uint16_t target)
 Record a vehicle ListOrderChange.
 
static bool EnginePreSort (const EngineID &a, const EngineID &b)
 Comparator function to sort engines via scope-GRFID and local ID.
 
void CommitVehicleListOrderChanges ()
 Deternine default engine sorting and execute recorded ListOrderChanges from AlterVehicleListOrder.
 
void FillNewGRFVehicleCache (const Vehicle *v)
 Fill the grf_cache of the given vehicle.
 

Variables

static std::vector< ListOrderChange_list_order_changes
 

Detailed Description

NewGRF handling of engines.

Definition in file newgrf_engine.cpp.

Enumeration Type Documentation

◆ TTDPAircraftMovementActions

enum TTDPAircraftMovementActions

Definition at line 233 of file newgrf_engine.cpp.

◆ TTDPAircraftMovementStates

enum TTDPAircraftMovementStates

Definition at line 97 of file newgrf_engine.cpp.

Function Documentation

◆ AlterVehicleListOrder()

void AlterVehicleListOrder ( EngineID  engine,
uint16_t  target 
)

Record a vehicle ListOrderChange.

Parameters
engineEngine to move
targetLocal engine ID to move engine in front of
Note
All sorting is done later in CommitVehicleListOrderChanges

Definition at line 1302 of file newgrf_engine.cpp.

Referenced by AircraftVehicleChangeInfo(), RailVehicleChangeInfo(), RoadVehicleChangeInfo(), and ShipVehicleChangeInfo().

◆ CommitVehicleListOrderChanges()

◆ DoTriggerVehicle()

static void DoTriggerVehicle ( Vehicle v,
VehicleTrigger  trigger,
uint16_t  base_random_bits,
bool  first 
)
static

Definition at line 1211 of file newgrf_engine.cpp.

◆ EnginePreSort()

static bool EnginePreSort ( const EngineID a,
const EngineID b 
)
static

Comparator function to sort engines via scope-GRFID and local ID.

Parameters
aleft side
bright side
Returns
comparison result

Definition at line 1314 of file newgrf_engine.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get(), Engine::grf_prop, GRFFilePropsBase< Tcnt >::grfid, GRFFilePropsBase< Tcnt >::local_id, and Engine::type.

Referenced by CommitVehicleListOrderChanges().

◆ FillNewGRFVehicleCache()

◆ GetCustomEngineSprite()

void GetCustomEngineSprite ( EngineID  engine,
const Vehicle v,
Direction  direction,
EngineImageType  image_type,
VehicleSpriteSeq result 
)

Definition at line 1074 of file newgrf_engine.cpp.

◆ GetEngineGrfFile()

static const GRFFile * GetEngineGrfFile ( EngineID  engine_type)
static

Get the grf file associated with an engine type.

Parameters
engine_typeEngine to query.
Returns
grf file associated with the engine.

Definition at line 1023 of file newgrf_engine.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get(), and Engine::GetGRF().

◆ GetEngineProperty()

int GetEngineProperty ( EngineID  engine,
PropertyID  property,
int  orig_value,
const Vehicle v,
bool  is_signed 
)

Definition at line 1181 of file newgrf_engine.cpp.

◆ GetRotorOverrideSprite()

void GetRotorOverrideSprite ( EngineID  engine,
const struct Aircraft v,
EngineImageType  image_type,
VehicleSpriteSeq result 
)

Definition at line 1096 of file newgrf_engine.cpp.

◆ GetVehicleCallback()

uint16_t GetVehicleCallback ( CallbackID  callback,
uint32_t  param1,
uint32_t  param2,
EngineID  engine,
const Vehicle v 
)

Evaluate a newgrf callback for vehicles.

Parameters
callbackThe callback to evaluate
param1First parameter of the callback
param2Second parameter of the callback
engineEngine type of the vehicle to evaluate the callback for
vThe vehicle to evaluate the callback for, or nullptr if it doesn't exist yet
Returns
The value the callback returned, or CALLBACK_FAILED if it failed

Definition at line 1150 of file newgrf_engine.cpp.

References VehicleResolverObject::WO_UNCACHED.

Referenced by CalculateRefitMasks(), CmdStartStopVehicle(), Train::ConsistChanged(), Engine::DetermineCapacity(), FormatString(), GetCargoSubtypeText(), GetLoadAmount(), GetNewGRFAdditionalText(), GetNextArticulatedPart(), GetRefitCostFactor(), GetRoadVehLength(), PlayVehicleSound(), RunEconomyVehicleDayProc(), SpawnAdvancedVisualEffect(), TestVehicleBuildProbability(), and Vehicle::UpdateVisualEffect().

◆ GetVehicleCallbackParent()

uint16_t GetVehicleCallbackParent ( CallbackID  callback,
uint32_t  param1,
uint32_t  param2,
EngineID  engine,
const Vehicle v,
const Vehicle parent 
)

Evaluate a newgrf callback for vehicles with a different vehicle for parent scope.

Parameters
callbackThe callback to evaluate
param1First parameter of the callback
param2Second parameter of the callback
engineEngine type of the vehicle to evaluate the callback for
vThe vehicle to evaluate the callback for, or nullptr if it doesn't exist yet
parentThe vehicle to use for parent scope
Returns
The value the callback returned, or CALLBACK_FAILED if it failed

Definition at line 1166 of file newgrf_engine.cpp.

References VehicleResolverObject::WO_NONE.

Referenced by CheckTrainAttachment().

◆ GetVehicleProperty()

int GetVehicleProperty ( const Vehicle v,
PropertyID  property,
int  orig_value,
bool  is_signed 
)

Definition at line 1175 of file newgrf_engine.cpp.

◆ GetWagonOverrideSpriteSet()

const SpriteGroup * GetWagonOverrideSpriteSet ( EngineID  engine,
CargoID  cargo,
EngineID  overriding_engine 
)

Definition at line 42 of file newgrf_engine.cpp.

◆ LiveryHelper()

static const Livery * LiveryHelper ( EngineID  engine,
const Vehicle v 
)
static

Determines the livery of an engine.

This always uses dual company colours independent of GUI settings. So it is desync-safe.

Parameters
engineEngine type
vVehicle, nullptr in purchase list.
Returns
Livery to use

Definition at line 374 of file newgrf_engine.cpp.

References _current_company, Vehicle::engine_type, GroundVehicleCache::first_engine, GetEngineLivery(), Vehicle::GetGroundVehicleCache(), INVALID_ENGINE, Vehicle::IsGroundVehicle(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), LIT_ALL, and Vehicle::owner.

Referenced by VehicleScopeResolver::GetVariable().

◆ MapAircraftMovementAction()

static uint8_t MapAircraftMovementAction ( const Aircraft v)
static

Map OTTD aircraft movement states to TTDPatch style movement actions (VarAction 2 Variable 0xE6) This is not fully supported yet but it's enough for Planeset.

Definition at line 261 of file newgrf_engine.cpp.

References Vehicle::cur_speed, Vehicle::current_order, ENDLANDING, ENDTAKEOFF, FLYING, HANGAR, HELIENDLANDING, HELILANDING, HELIPAD1, HELIPAD2, HELIPAD3, HELITAKEOFF, Order::IsType(), LANDING, STARTTAKEOFF, Aircraft::state, TAKEOFF, TERM1, TERM2, TERM3, TERM4, TERM5, TERM6, TERM7, and TERM8.

◆ MapAircraftMovementState()

◆ MapOldSubType()

static int MapOldSubType ( const Vehicle v)
static

Definition at line 79 of file newgrf_engine.cpp.

◆ PositionHelper()

static uint32_t PositionHelper ( const Vehicle v,
bool  consecutive 
)
static

Helper to get the position of a vehicle within a chain of vehicles.

Parameters
vthe vehicle to get the position of.
consecutivewhether to look at the whole chain or the vehicles with the same 'engine type'.
Returns
the position in the chain from front and tail and chain length.

Definition at line 397 of file newgrf_engine.cpp.

References Vehicle::engine_type, Vehicle::First(), and Vehicle::Next().

◆ SetCustomEngineSprites()

void SetCustomEngineSprites ( EngineID  engine,
uint8_t  cargo,
const SpriteGroup group 
)

Definition at line 53 of file newgrf_engine.cpp.

◆ SetEngineGRF()

void SetEngineGRF ( EngineID  engine,
const GRFFile file 
)

Tie a GRFFile entry to an engine, to allow us to retrieve GRF parameters etc during a game.

Parameters
engineEngine ID to tie the GRFFile to.
filePointer of GRFFile to tie.

Definition at line 71 of file newgrf_engine.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get(), Engine::grf_prop, GRFFilePropsBase< Tcnt >::grffile, and GRFFilePropsBase< Tcnt >::grfid.

◆ SetWagonOverrideSprites()

void SetWagonOverrideSprites ( EngineID  engine,
CargoID  cargo,
const SpriteGroup group,
std::span< EngineID engine_ids 
)

Definition at line 30 of file newgrf_engine.cpp.

◆ TestVehicleBuildProbability()

bool TestVehicleBuildProbability ( Vehicle v,
EngineID  engine,
BuildProbabilityType  type 
)

Test for vehicle build probablity type.

Parameters
vVehicle whose build probability to test.
typeBuild probability type to test for.
Returns
True iff the probability result says so.

Definition at line 1202 of file newgrf_engine.cpp.

References CALLBACK_FAILED, CBID_VEHICLE_BUILD_PROBABILITY, GetVehicleCallback(), RandomRange(), and to_underlying().

Referenced by AddArticulatedParts(), CmdBuildRailVehicle(), and CmdBuildRailWagon().

◆ TriggerVehicle()

void TriggerVehicle ( Vehicle v,
VehicleTrigger  trigger 
)

Definition at line 1273 of file newgrf_engine.cpp.

◆ UsesWagonOverride()

bool UsesWagonOverride ( const Vehicle v)

Check if a wagon is currently using a wagon override.

Parameters
vThe wagon to check
Returns
true if it is using an override, false otherwise

Definition at line 1135 of file newgrf_engine.cpp.

References SpecializedVehicle< T, Type >::From(), BaseVehicle::type, and VEH_TRAIN.

Referenced by Train::ConsistChanged(), and GetEngineLiveryScheme().

◆ VehicleGetVariable()

static uint32_t VehicleGetVariable ( Vehicle v,
const VehicleScopeResolver object,
uint8_t  variable,
uint32_t  parameter,
bool &  available 
)
static

Definition at line 416 of file newgrf_engine.cpp.

Variable Documentation

◆ _list_order_changes

std::vector<ListOrderChange> _list_order_changes
static

Definition at line 1294 of file newgrf_engine.cpp.