Data Structures | Enumerations | Functions | Variables

# 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 "date_func.h"`
`#include "vehicle_func.h"`
`#include "core/random_func.hpp"`
`#include "aircraft.h"`
`#include "station_base.h"`
`#include "company_base.h"`
`#include "newgrf_railtype.h"`
`#include "ship.h"`

Go to the source code of this file.

## Data Structures
struct | WagonOverride
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, EngineID *train_id, uint trains)
const SpriteGroup * | GetWagonOverrideSpriteSet (EngineID engine, CargoID cargo, EngineID overriding_engine)
void | UnloadWagonOverrides (Engine *e)
| Unload all wagon override sprite groups.
void | SetCustomEngineSprites (EngineID engine, byte 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 byte | MapAircraftMovementState (const Aircraft *v)
| Map OTTD aircraft movement states to TTDPatch style movement states (VarAction 2 Variable 0xE2).
static byte | 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 Livery * | LiveryHelper (EngineID engine, const Vehicle *v)
| Determines the livery of an engine.
static uint32 | PositionHelper (const Vehicle *v, bool consecutive)
| Helper to get the position of a vehicle within a chain of vehicles.
static uint32 | VehicleGetVariable (Vehicle *v, const VehicleScopeResolver *object, byte variable, uint32 parameter, bool *available)
static const GRFFile * | GetEngineGrfFile (EngineID engine_type)
| Get the grf file associated with an engine type.
static const SpriteGroup * | GetVehicleSpriteGroup (EngineID engine, const Vehicle *v, bool use_cache=true)
| Retrieve the SpriteGroup for the specified vehicle.
SpriteID | GetCustomEngineSprite (EngineID engine, const Vehicle *v, Direction direction, EngineImageType image_type)
SpriteID | GetRotorOverrideSprite (EngineID engine, const Aircraft *v, bool info_view, EngineImageType image_type)
bool | UsesWagonOverride (const Vehicle *v)
| Check if a wagon is currently using a wagon override.
uint16 | GetVehicleCallback (CallbackID callback, uint32 param1, uint32 param2, EngineID engine, const Vehicle *v)
| Evaluate a newgrf callback for vehicles.
uint16 | GetVehicleCallbackParent (CallbackID callback, uint32 param1, uint32 param2, EngineID engine, const Vehicle *v, const Vehicle *parent)
| Evaluate a newgrf callback for vehicles with a different vehicle for parent scope.
uint | GetVehicleProperty (const Vehicle *v, PropertyID property, uint orig_value)
uint | GetEngineProperty (EngineID engine, PropertyID property, uint orig_value, const Vehicle *v)
static void | DoTriggerVehicle (Vehicle *v, VehicleTrigger trigger, byte base_random_bits, bool first)
void | TriggerVehicle (Vehicle *v, VehicleTrigger trigger)
void | AlterVehicleListOrder (EngineID engine, uint target)
| Record a vehicle ListOrderChange.
static int CDECL | 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 SmallVector < ListOrderChange, 16 > | _list_order_changes

## Detailed Description

NewGRF handling of engines.

Definition in file newgrf_engine.cpp.

## Function Documentation

void AlterVehicleListOrder | ( | EngineID | engine,
|  | uint | target |
| ) |  |  |

Record a vehicle ListOrderChange.

Parameters:

  | engine | Engine to move
  | target | Local engine ID to move engine in front of

Note:
  All sorting is done later in CommitVehicleListOrderChanges

Definition at line 1200 of file newgrf_engine.cpp.

References SmallVector< T, S >::Append(), and ListOrderChange::target.

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

static int CDECL EnginePreSort | ( | const EngineID * | a,
|  | const EngineID * | b |
| ) |  |  | `[static]`

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

Parameters:

  | a | left side
  | b | right side

Returns:
  comparison result

Definition at line 1214 of file newgrf_engine.cpp.

References SmallVector< T, S >::Get(), EngineIDMapping::grfid, EngineIDMapping::internal_id, and EngineIDMapping::type.

void FillNewGRFVehicleCache | ( | const Vehicle * | v | ) |

Fill the grf_cache of the given vehicle.

Parameters:

  | v | The vehicle to fill the cache for.

Definition at line 1286 of file newgrf_engine.cpp.

References NewGRFCache::cache_valid, Vehicle::engine_type, VehicleResolverObject::GetScope(), ScopeResolver::GetVariable(), Vehicle::grf_cache, HasBit(), lengthof, NCVV_COMPANY_INFORMATION, NCVV_CONSIST_CARGO_INFORMATION, NCVV_END, NCVV_POSITION_CONSIST_LENGTH, NCVV_POSITION_SAME_ID_LENGTH, and VSG_SCOPE_SELF.

Referenced by CheckCaches().

static const GRFFile* GetEngineGrfFile | ( | EngineID | engine_type | ) | `[static]`

Get the grf file associated with an engine type.

Parameters:

  | engine_type | Engine to query.

Returns:
  grf file associated with the engine.

Definition at line 951 of file newgrf_engine.cpp.

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

uint16 GetVehicleCallback | ( | CallbackID | callback,
|  | uint32 | param1,
|  | uint32 | param2,
|  | EngineID | engine,
|  | const Vehicle * | v |
| ) |  |  |

Evaluate a newgrf callback for vehicles.

Parameters:

  | callback | The callback to evaluate
  | param1 | First parameter of the callback
  | param2 | Second parameter of the callback
  | engine | Engine type of the vehicle to evaluate the callback for
  | v | The vehicle to evaluate the callback for, or NULL if it doesnt exist yet

Returns:
  The value the callback returned, or CALLBACK_FAILED if it failed

Definition at line 1069 of file newgrf_engine.cpp.

References CALLBACK_FAILED, GetVehicleSpriteGroup(), and SpriteGroup::Resolve().

Referenced by RefitWindow::BuildRefitList(), CmdStartStopVehicle(), Train::ConsistChanged(), Engine::DetermineCapacity(), GetBestFittingSubType(), GetCargoSubtypeText(), GetNextArticulatedPart(), GetRefitCostFactor(), GetRoadVehLength(), LoadUnloadVehicle(), PlayVehicleSound(), RunVehicleDayProc(), ShowAdditionalText(), and Vehicle::UpdateVisualEffect().

uint16 GetVehicleCallbackParent | ( | CallbackID | callback,
|  | uint32 | param1,
|  | uint32 | param2,
|  | EngineID | engine,
|  | const Vehicle * | v,
|  | const Vehicle * | parent |
| ) |  |  |

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

Parameters:

  | callback | The callback to evaluate
  | param1 | First parameter of the callback
  | param2 | Second parameter of the callback
  | engine | Engine type of the vehicle to evaluate the callback for
  | v | The vehicle to evaluate the callback for, or NULL if it doesn't exist yet
  | parent | The vehicle to use for parent scope

Returns:
  The value the callback returned, or CALLBACK_FAILED if it failed

Definition at line 1088 of file newgrf_engine.cpp.

References CALLBACK_FAILED, GetVehicleSpriteGroup(), and SpriteGroup::Resolve().

Referenced by CheckTrainAttachment().

static const SpriteGroup* GetVehicleSpriteGroup | ( | EngineID | engine,
|  | const Vehicle * | v,
|  | bool | use_cache = `true` |
| ) |  |  | `[static]`

Retrieve the SpriteGroup for the specified vehicle.

If the vehicle is not specified, the purchase list group for the engine is chosen. For trains, an additional engine override lookup is performed.

Parameters:

  | engine | Engine type of the vehicle.
  | v | The vehicle itself.
  | use_cache | Use cached override

Returns:
  The selected SpriteGroup for the vehicle.

Definition at line 985 of file newgrf_engine.cpp.

References Vehicle::cargo_type, Vehicle::engine_type, GroundVehicleCache::first_engine, SpecializedVehicle< Train, Type >::From(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get(), Vehicle::GetGroundVehicleCache(), Engine::grf_prop, Vehicle::IsGroundVehicle(), lengthof, GRFFilePropsBase< Tcnt >::spritegroup, BaseVehicle::type, and VEH_TRAIN.

Referenced by GetVehicleCallback(), and GetVehicleCallbackParent().

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:

  | engine | Engine type
  | v | Vehicle, NULL in purchase list.

Returns:
  Livery to use

Definition at line 424 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().

static uint32 PositionHelper | ( | const Vehicle * | v,
|  | bool | consecutive |
| ) |  |  | `[static]`

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

Parameters:

  | v | the vehicle to get the position of.
  | consecutive | whether 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 447 of file newgrf_engine.cpp.

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

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:

  | engine | Engine ID to tie the GRFFile to.
  | file | Pointer of GRFFile to tie.

Definition at line 107 of file newgrf_engine.cpp.

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

bool UsesWagonOverride | ( | const Vehicle * | v | ) |

Check if a wagon is currently using a wagon override.

Parameters:

  | v | The wagon to check

Returns:
  true if it is using an override, false otherwise

Definition at line 1054 of file newgrf_engine.cpp.

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

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