OpenTTD Source  20241120-master-g6d3adc6169
engine.cpp File Reference

Base for all engine handling. More...

#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 "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 "timer/timer.h"
#include "timer/timer_game_tick.h"
#include "timer/timer_game_calendar.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 ClearLastVariant (EngineID engine_id, VehicleType type)
 Ensure engine is not set as the last used variant for any other engine. More...
 
void CalcEngineReliability (Engine *e, bool new_month)
 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, const TimerGameCalendar::YearMonthDay &aging_ymd, uint32_t seed)
 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, int recursion_depth=0)
 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 ClearEnginesHiddenFlagOfCompany (CompanyID cid)
 Clear the 'hidden' flag for all engines of a new company. More...
 
CommandCost CmdSetVehicleVisibility (DoCommandFlag flags, EngineID engine_id, bool hide)
 Set the visibility of an engine. More...
 
CommandCost CmdWantEnginePreview (DoCommandFlag flags, EngineID engine_id)
 Accept an engine prototype. More...
 
CommandCost CmdEngineCtrl (DoCommandFlag flags, EngineID engine_id, CompanyID company_id, bool allow)
 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 CalendarEnginesMonthlyLoop ()
 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 (DoCommandFlag flags, EngineID engine_id, 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 TimerGameCalendar::Year _year_engine_aging_stops
 Year that engine aging stops. More...
 
const uint8_t _engine_counts [4]
 Number of engines of each vehicle type in original engine data. More...
 
const uint8_t _engine_offsets [4]
 Offset of the first engine of each vehicle type in original engine data. More...
 
static IntervalTimer< TimerGameCalendar_calendar_engines_daily ({TimerGameCalendar::DAY, TimerGameCalendar::Priority::ENGINE}, [](auto) { for(Company *c :Company::Iterate()) { c->avail_railtypes=AddDateIntroducedRailTypes(c->avail_railtypes, TimerGameCalendar::date);c->avail_roadtypes=AddDateIntroducedRoadTypes(c->avail_roadtypes, TimerGameCalendar::date);} if(TimerGameCalendar::year >=_year_engine_aging_stops) return;for(Engine *e :Engine::Iterate()) { EngineID i=e->index;if(e->flags &ENGINE_EXCLUSIVE_PREVIEW) { if(e->preview_company !=INVALID_COMPANY) { if(!--e->preview_wait) { CloseWindowById(WC_ENGINE_PREVIEW, i);e->preview_company=INVALID_COMPANY;} } else if(CountBits(e->preview_asked)< MAX_COMPANIES) { e->preview_company=GetPreviewCompany(e);if(e->preview_company==INVALID_COMPANY) { e->preview_asked=MAX_UVALUE(CompanyMask);continue;} SetBit(e->preview_asked, e->preview_company);e->preview_wait=20;if(!IsVehicleTypeDisabled(e->type, true)) AI::NewEvent(e->preview_company, new ScriptEventEnginePreview(i));if(IsInteractiveCompany(e->preview_company)) ShowEnginePreviewWindow(i);} } } })
 Daily check to offer an exclusive engine preview to the companies.
 
static IntervalTimer< TimerGameCalendar_calendar_engines_monthly ({TimerGameCalendar::MONTH, TimerGameCalendar::Priority::ENGINE}, [](auto) { CalendarEnginesMonthlyLoop();})
 

Detailed Description

Base for all engine handling.

Definition in file engine.cpp.

Function Documentation

◆ AcceptEnginePreview()

static void AcceptEnginePreview ( EngineID  eid,
CompanyID  company,
int  recursion_depth = 0 
)
static

Company company accepts engine eid for preview.

Parameters
eidEngine being accepted (is under preview).
companyCurrent company previewing the engine.
recursion_depthRecursion depth to avoid infinite loop.

Definition at line 847 of file engine.cpp.

References EnableEngineForCompany(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get(), HasFlag(), INVALID_COMPANY, InvalidateWindowData(), Engine::IterateType(), JoinPreview, MAX_UVALUE, Engine::preview_asked, Engine::preview_company, Engine::type, and WC_ENGINE_PREVIEW.

Referenced by CmdWantEnginePreview().

◆ CalcEngineReliability()

◆ ClearEnginesHiddenFlagOfCompany()

void ClearEnginesHiddenFlagOfCompany ( CompanyID  cid)

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

Parameters
cidCompany being created.

Definition at line 969 of file engine.cpp.

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

◆ ClearLastVariant()

static void ClearLastVariant ( EngineID  engine_id,
VehicleType  type 
)
static

Ensure engine is not set as the last used variant for any other engine.

Parameters
engine_idEngine being removed.
typeType of engine.

Definition at line 600 of file engine.cpp.

References INVALID_ENGINE, and Engine::IterateType().

Referenced by CalcEngineReliability(), and DisableEngineForCompany().

◆ CmdEngineCtrl()

CommandCost CmdEngineCtrl ( DoCommandFlag  flags,
EngineID  engine_id,
CompanyID  company_id,
bool  allow 
)

Allow or forbid a specific company to use an engine.

Parameters
flagsoperation to perform
engine_idengine id
company_idCompany to allow/forbid the use of an engine.
allowfalse to forbid, true to allow.
Returns
the cost of this operation or an error

Definition at line 1022 of file engine.cpp.

References _current_company, CMD_ERROR, DC_EXEC, DisableEngineForCompany(), EnableEngineForCompany(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::IsValidID(), and OWNER_DEITY.

◆ CmdRenameEngine()

CommandCost CmdRenameEngine ( DoCommandFlag  flags,
EngineID  engine_id,
const std::string &  text 
)

Rename an engine.

Parameters
flagsoperation to perform
engine_idengine ID to rename
textthe new name or an empty string when resetting to the default
Returns
the cost of this operation or an error

Definition at line 1184 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 ( DoCommandFlag  flags,
EngineID  engine_id,
bool  hide 
)

◆ CmdWantEnginePreview()

CommandCost CmdWantEnginePreview ( DoCommandFlag  flags,
EngineID  engine_id 
)

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
flagsoperation to perform
engine_idengine-prototype offered
Returns
the cost of this operation or an error

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

◆ EnableEngineForCompany()

◆ GetPreviewCompany()

static CompanyID GetPreviewCompany ( Engine e)
static

Get the best company for an engine preview.

Parameters
eEngine to preview.
Returns
Best company if it exists, INVALID_COMPANY otherwise.

Definition at line 879 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.

◆ IsEngineBuildable()

◆ IsEngineRefittable()

bool IsEngineRefittable ( EngineID  engine)

◆ IsUniqueEngineName()

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

Is name still free as name for an engine?

Parameters
nameNew name of an engine.
Returns
false if the name is being used already, else true.

Definition at line 1168 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
typeThe vehicle type which shall be checked.
aiIf 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 913 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 CalendarEnginesMonthlyLoop(), and NewVehicleAvailable().

◆ IsWagon()

static bool IsWagon ( EngineID  index)
static

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

Parameters
indexEngine getting queried.
Returns
Whether the queried engine is a wagon.

Definition at line 589 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 CalendarEnginesMonthlyLoop(), and NewVehicleAvailable().

◆ NewVehicleAvailable()

static void NewVehicleAvailable ( Engine e)
static

◆ StartupEngines()

void StartupEngines ( )

Start/initialise all our engines.

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

Definition at line 763 of file engine.cpp.

References _year_engine_aging_stops, TimerGameCalendar::ConvertDateToYMD(), TimerGameCalendar::ConvertYMDToDate(), and TimerGameCalendar::date.

Referenced by DEF_CONSOLE_CMD().

◆ StartupOneEngine()

void StartupOneEngine ( Engine e,
const TimerGameCalendar::YearMonthDay &  aging_ymd,
uint32_t  seed 
)

Start/initialise one engine.

Parameters
eThe engine to initialise.
aging_dateThe date used for age calculations.
seedRandom seed.

Definition at line 682 of file engine.cpp.

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

Variable Documentation

◆ _engine_counts

const uint8_t _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),
}
#define lengthof(array)
Return the length of an fixed size array.
Definition: stdafx.h:280

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

Definition at line 53 of file engine.cpp.

Referenced by EngineOverrideManager::ResetToDefaultMapping().

◆ _engine_offsets

const uint8_t _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 61 of file engine.cpp.

◆ _year_engine_aging_stops

TimerGameCalendar::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 50 of file engine.cpp.

Referenced by CalendarEnginesMonthlyLoop(), SetYearEngineAgingStops(), and StartupEngines().