OpenTTD Source: src/engine.cpp File Reference

OpenTTD Source 12.2

Functions | Variables
engine.cpp File Reference
`#include "stdafx.h"`
`#include "company_func.h"`
`#include "command_func.h"`
`#include "news_func.h"`
`#include "aircraft.h"`
`#include "newgrf.h"`
`#include "newgrf_engine.h"`
`#include "strings_func.h"`
`#include "core/random_func.hpp"`
`#include "window_func.h"`
`#include "date_func.h"`
`#include "autoreplace_gui.h"`
`#include "string_func.h"`
`#include "ai/ai.hpp"`
`#include "core/pool_func.hpp"`
`#include "engine_gui.h"`
`#include "engine_func.h"`
`#include "engine_base.h"`
`#include "company_base.h"`
`#include "vehicle_func.h"`
`#include "articulated_vehicles.h"`
`#include "error.h"`
`#include "table/strings.h"`
`#include "table/engines.h"`
`#include "safeguards.h"`

Go to the source code of this file.

## Functions
void | SetupEngines ()
| Initialise the engine pool with the data from the original vehicles.
void | ShowEnginePreviewWindow (EngineID engine)
static bool | IsWagon (EngineID index)
| Determine whether an engine type is a wagon (and not a loco). More...
static void | CalcEngineReliability (Engine *e)
| Update Engine::reliability and (if needed) update the engine GUIs. More...
void | SetYearEngineAgingStops ()
| Compute the value for _year_engine_aging_stops.
void | StartupOneEngine (Engine *e, Date aging_date)
| Start/initialise one engine. More...
void | StartupEngines ()
| Start/initialise all our engines. More...
static void | EnableEngineForCompany (EngineID eid, CompanyID company)
| Allows engine eid to be used by a company company. More...
static void | DisableEngineForCompany (EngineID eid, CompanyID company)
| Forbids engine eid to be used by a company company. More...
static void | AcceptEnginePreview (EngineID eid, CompanyID company)
| Company company accepts engine eid for preview. More...
static CompanyID | GetPreviewCompany (Engine *e)
| Get the best company for an engine preview. More...
static bool | IsVehicleTypeDisabled (VehicleType type, bool ai)
| Checks if a vehicle type is disabled for all/ai companies. More...
void | EnginesDailyLoop ()
| Daily check to offer an exclusive engine preview to the companies. More...
void | ClearEnginesHiddenFlagOfCompany (CompanyID cid)
| Clear the 'hidden' flag for all engines of a new company. More...
CommandCost | CmdSetVehicleVisibility (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const std::string &text)
| Set the visibility of an engine. More...
CommandCost | CmdWantEnginePreview (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const std::string &text)
| Accept an engine prototype. More...
CommandCost | CmdEngineCtrl (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const std::string &text)
| Allow or forbid a specific company to use an engine. More...
static void | NewVehicleAvailable (Engine *e)
| An engine has become available for general use. More...
void | EnginesMonthlyLoop ()
| Monthly update of the availability, reliability, and preview offers of the engines.
static bool | IsUniqueEngineName (const std::string &name)
| Is name still free as name for an engine? More...
CommandCost | CmdRenameEngine (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const std::string &text)
| Rename an engine. More...
bool | IsEngineBuildable (EngineID engine, VehicleType type, CompanyID company)
| Check if an engine is buildable. More...
bool | IsEngineRefittable (EngineID engine)
| Check if an engine is refittable. More...
void | CheckEngines ()
| Check for engines that have an appropriate availability.

## Variables
EnginePool | _engine_pool ("Engine")
EngineOverrideManager | _engine_mngr
static Year | _year_engine_aging_stops
| Year that engine aging stops. More...
const uint8 | _engine_counts [4]
| Number of engines of each vehicle type in original engine data. More...
const uint8 | _engine_offsets [4]
| Offset of the first engine of each vehicle type in original engine data. More...

## Detailed Description

Base for all engine handling.

Definition in file engine.cpp.

## Function Documentation

## ◆ AcceptEnginePreview()

static void AcceptEnginePreview | ( | EngineID | eid,
|  | CompanyID | company
| ) |  |
| static

Company company accepts engine eid for preview.

Parameters

  eid | Engine being accepted (is under preview).
  company | Current company previewing the engine.

Definition at line 756 of file engine.cpp.

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

Referenced by CmdWantEnginePreview().

## ◆ CalcEngineReliability()

static void CalcEngineReliability | ( | Engine * | e | ) |
| static

Update Engine::reliability and (if needed) update the engine GUIs.

Parameters

  e | Engine to update.

Definition at line 563 of file engine.cpp.

References _settings_game, AddRemoveEngineFromAutoreplaceAndBuildWindows(), EngineInfo::base_life, Engine::company_avail, Engine::duration_phase_1, Engine::duration_phase_2, Engine::duration_phase_3, VehicleSettings::never_expire_vehicles, Engine::reliability, Engine::reliability_final, Engine::reliability_max, Engine::reliability_start, EngineInfo::retire_early, SetWindowClassesDirty(), Engine::type, GameSettings::vehicle, WC_BUILD_VEHICLE, and WC_REPLACE_VEHICLE.

Referenced by EnginesMonthlyLoop().

## ◆ ClearEnginesHiddenFlagOfCompany()

void ClearEnginesHiddenFlagOfCompany | ( | CompanyID | cid | ) |

Clear the 'hidden' flag for all engines of a new company.

Parameters

  cid | Company being created.

Definition at line 868 of file engine.cpp.

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

## ◆ CmdEngineCtrl()

CommandCost CmdEngineCtrl | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const std::string & | text
| ) |  |

Allow or forbid a specific company to use an engine.

Parameters

  tile | unused
  flags | operation to perform
  p1 | engine id
  p2 | various bitstuffed elements p2 = (bit 0 - 7) - Company to allow/forbid the use of an engine. p2 = (bit 31) - 0 to forbid, 1 to allow.
  text | unused

Returns
  the cost of this operation or an error

Definition at line 929 of file engine.cpp.

References _current_company, CMD_ERROR, and OWNER_DEITY.

## ◆ CmdRenameEngine()

CommandCost CmdRenameEngine | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const std::string & | text
| ) |  |

Rename an engine.

Parameters

  tile | unused
  flags | operation to perform
  p1 | engine ID to rename
  p2 | unused
  text | the new name or an empty string when resetting to the default

Returns
  the cost of this operation or an error

Definition at line 1081 of file engine.cpp.

References CMD_ERROR, DC_EXEC, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::GetIfValid(), IsUniqueEngineName(), MarkWholeScreenDirty(), MAX_LENGTH_ENGINE_NAME_CHARS, Engine::name, return_cmd_error, and Utf8StringLength().

## ◆ CmdSetVehicleVisibility()

CommandCost CmdSetVehicleVisibility | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const std::string & | text
| ) |  |

Set the visibility of an engine.

Parameters

  tile | Unused.
  flags | Operation to perform.
  p1 | Unused.
  p2 | Bit 31: 0=visible, 1=hidden, other bits for the EngineID.
  text | Unused.

Returns
  The cost of this operation or an error.

Definition at line 884 of file engine.cpp.

References _current_company, AddRemoveEngineFromAutoreplaceAndBuildWindows(), CMD_ERROR, Engine::company_hidden, DC_EXEC, GB(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::GetIfValid(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, IsEngineBuildable(), MAX_COMPANIES, SB(), and Engine::type.

## ◆ CmdWantEnginePreview()

CommandCost CmdWantEnginePreview | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const std::string & | text
| ) |  |

Accept an engine prototype.

XXX - it is possible that the top-company changes while you are waiting to accept the offer? Then it becomes invalid

Parameters

  tile | unused
  flags | operation to perform
  p1 | engine-prototype offered
  p2 | unused
  text | unused

Returns
  the cost of this operation or an error

Definition at line 908 of file engine.cpp.

References _current_company, AcceptEnginePreview(), CMD_ERROR, DC_EXEC, ENGINE_EXCLUSIVE_PREVIEW, Engine::flags, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::GetIfValid(), and Engine::preview_company.

## ◆ DisableEngineForCompany()

static void DisableEngineForCompany | ( | EngineID | eid,
|  | CompanyID | company
| ) |  |
| static

Forbids engine eid to be used by a company company.

Parameters

  eid | The engine to disable.
  company | The company to forbid using the engine.

Definition at line 734 of file engine.cpp.

References _local_company, AddRemoveEngineFromAutoreplaceAndBuildWindows(), Company::avail_railtypes, Company::avail_roadtypes, ClrBit(), Engine::company_avail, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get(), GetCompanyRailtypes(), GetCompanyRoadTypes(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, Engine::type, VEH_ROAD, and VEH_TRAIN.

## ◆ EnableEngineForCompany()

static void EnableEngineForCompany | ( | EngineID | eid,
|  | CompanyID | company
| ) |  |
| static

Allows engine eid to be used by a company company.

Parameters

  eid | The engine to enable.
  company | The company to allow using the engine.

Definition at line 706 of file engine.cpp.

References _local_company, AddRemoveEngineFromAutoreplaceAndBuildWindows(), Company::avail_railtypes, Company::avail_roadtypes, Engine::company_avail, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get(), GetCompanyRailtypes(), GetCompanyRoadTypes(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, InvalidateWindowData(), SetBit(), TRANSPORT_AIR, TRANSPORT_ROAD, TRANSPORT_WATER, Engine::type, VEH_AIRCRAFT, VEH_ROAD, VEH_SHIP, VEH_TRAIN, WC_BUILD_TOOLBAR, and WC_MAIN_TOOLBAR.

## ◆ EnginesDailyLoop()

void EnginesDailyLoop | ( |  | ) |

Daily check to offer an exclusive engine preview to the companies.

Functions used by the IncreaseDate function.

Definition at line 825 of file engine.cpp.

References _cur_year, _date, _year_engine_aging_stops, AddDateIntroducedRailTypes(), AddDateIntroducedRoadTypes(), CloseWindowById(), CountBits(), ENGINE_EXCLUSIVE_PREVIEW, GetPreviewCompany(), INVALID_COMPANY, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Iterate(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Iterate(), MAX_COMPANIES, and WC_ENGINE_PREVIEW.

## ◆ GetPreviewCompany()

static CompanyID GetPreviewCompany | ( | Engine * | e | ) |
| static

Get the best company for an engine preview.

Parameters

  e | Engine to preview.

Returns
  Best company if it exists, INVALID_COMPANY otherwise.

Definition at line 778 of file engine.cpp.

References GetUnionOfArticulatedRefitMasks(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, INVALID_COMPANY, Engine::type, and VEH_TRAIN.

Referenced by EnginesDailyLoop().

## ◆ IsEngineBuildable()

bool IsEngineBuildable | ( | EngineID | engine,
|  | VehicleType | type,
|  | CompanyID | company
| ) |  |

Check if an engine is buildable.

Parameters

  engine | index of the engine to check.
  type | the type the engine should be.
  company | index of the company.

Returns
  True if an engine is valid, of the specified type, and buildable by the given company.

Definition at line 1115 of file engine.cpp.

References Company::avail_railtypes, Company::avail_roadtypes, Engine::company_avail, ENGINE_AVAILABLE, Engine::flags, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::GetIfValid(), GetRailTypeInfo(), GetRoadTypeInfo(), HasBit(), Engine::IsEnabled(), OWNER_DEITY, RoadTypeInfo::powered_roadtypes, RoadVehicleInfo::roadtype, ROADTYPES_NONE, Engine::type, VEH_ROAD, and VEH_TRAIN.

Referenced by CheckAutoreplaceValidity(), CmdBuildVehicle(), CmdSetVehicleVisibility(), and GetNewEngineType().

## ◆ IsEngineRefittable()

bool IsEngineRefittable | ( | EngineID | engine | ) |

Check if an engine is refittable.

Note: Likely you want to use IsArticulatedVehicleRefittable().

Parameters

  engine | index of the engine to check.

Returns
  true if the engine is refittable.

Definition at line 1156 of file engine.cpp.

References EngineInfo::callback_mask, Engine::CanCarryCargo(), CBM_VEHICLE_CARGO_SUFFIX, CT_INVALID, Engine::GetDefaultCargoType(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::GetIfValid(), HasBit(), and SetBit().

Referenced by GetArticulatedVehicleCargoesAndRefits(), IsArticulatedVehicleRefittable(), IsVehicleRefitable(), and OrdersWindow::UpdateAutoRefitState().

## ◆ IsUniqueEngineName()

static bool IsUniqueEngineName | ( | const std::string & | name | ) |
| static

Is name still free as name for an engine?

Parameters

  name | New name of an engine.

Returns
  `false` if the name is being used already, else `true`.

Definition at line 1063 of file engine.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Iterate().

Referenced by CmdRenameEngine().

## ◆ IsVehicleTypeDisabled()

static bool IsVehicleTypeDisabled | ( | VehicleType | type,
|  | bool | ai
| ) |  |
| static

Checks if a vehicle type is disabled for all/ai companies.

Parameters

  type | The vehicle type which shall be checked.
  ai | If true, check if the type is disabled for AI companies, otherwise check if the vehicle type is disabled for human companies.

Returns
  Whether or not a vehicle type is disabled.

Definition at line 812 of file engine.cpp.

References _settings_game, GameSettings::ai, AISettings::ai_disable_veh_aircraft, AISettings::ai_disable_veh_roadveh, AISettings::ai_disable_veh_ship, AISettings::ai_disable_veh_train, VehicleSettings::max_aircraft, VehicleSettings::max_roadveh, VehicleSettings::max_ships, VehicleSettings::max_trains, VEH_AIRCRAFT, VEH_ROAD, VEH_SHIP, VEH_TRAIN, and GameSettings::vehicle.

Referenced by EnginesMonthlyLoop().

## ◆ IsWagon()

static bool IsWagon | ( | EngineID | index | ) |
| static

Determine whether an engine type is a wagon (and not a loco).

Parameters

  index | Engine getting queried.

Returns
  Whether the queried engine is a wagon.

Definition at line 553 of file engine.cpp.

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

Referenced by EnginesMonthlyLoop().

## ◆ NewVehicleAvailable()

static void NewVehicleAvailable | ( | Engine * | e | ) |
| static

An engine has become available for general use.

Also handle the exclusive engine preview contract.

Parameters

  e | Engine generally available as of now.

Definition at line 954 of file engine.cpp.

References AddRemoveEngineFromAutoreplaceAndBuildWindows(), Engine::company_avail, ENGINE_AVAILABLE, ENGINE_EXCLUSIVE_PREVIEW, Engine::flags, SpecializedVehicle< Aircraft, VEH_AIRCRAFT >::From(), HasBit(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, Aircraft::IsNormalAircraft(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Iterate(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_vehicle_pool >::Iterate(), Engine::type, VEH_AIRCRAFT, VEH_ROAD, VEH_SHIP, and VEH_TRAIN.

Referenced by EnginesMonthlyLoop().

## ◆ StartupEngines()

void StartupEngines | ( |  | ) |

Start/initialise all our engines.

Must be called whenever there are changes to the NewGRF config.

Definition at line 682 of file engine.cpp.

References _date, _year_engine_aging_stops, ConvertYMDToDate(), GetCompanyRailtypes(), GetCompanyRoadTypes(), InvalidateWindowClassesData(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Iterate(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Iterate(), StartupOneEngine(), and WC_BUILD_VEHICLE.

Referenced by DEF_CONSOLE_CMD().

## ◆ StartupOneEngine()

void StartupOneEngine | ( | Engine * | e,
|  | Date | aging_date
| ) |  |

Start/initialise one engine.

Parameters

  e | The engine to initialise.
  aging_date | The date used for age calculations.

Definition at line 626 of file engine.cpp.

References _settings_game, EngineInfo::base_intro, Engine::company_avail, Engine::company_hidden, Engine::flags, GameSettings::game_creation, GameCreationSettings::generation_seed, Engine::GetGRFID(), SaveRandomSeeds(), SetRandomSeed(), and Engine::type.

Referenced by StartupEngines().

## Variable Documentation

## ◆ _engine_counts

const uint8 _engine_counts[4]

Initial value:
= {
lengthof(_orig_rail_vehicle_info),
lengthof(_orig_road_vehicle_info),
lengthof(_orig_ship_vehicle_info),
lengthof(_orig_aircraft_vehicle_info),
}

Number of engines of each vehicle type in original engine data.

Definition at line 50 of file engine.cpp.

Referenced by EngineOverrideManager::ResetToDefaultMapping().

## ◆ _engine_offsets

const uint8 _engine_offsets[4]

Initial value:
= {
0,
lengthof(_orig_rail_vehicle_info),
lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_info),
lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_info) + lengthof(_orig_ship_vehicle_info),
}

Offset of the first engine of each vehicle type in original engine data.

Definition at line 58 of file engine.cpp.

## ◆ _year_engine_aging_stops

Year _year_engine_aging_stops
| static

Year that engine aging stops.

Engines will not reduce in reliability and no more engines will be introduced

Definition at line 47 of file engine.cpp.

Referenced by EnginesDailyLoop(), EnginesMonthlyLoop(), SetYearEngineAgingStops(), and StartupEngines().

lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:378