OpenTTD Source 20241224-master-gf74b0cf984
|
Deals with autoreplace execution but not the setup. More...
#include "stdafx.h"
#include "company_func.h"
#include "train.h"
#include "command_func.h"
#include "engine_func.h"
#include "vehicle_func.h"
#include "autoreplace_func.h"
#include "autoreplace_gui.h"
#include "articulated_vehicles.h"
#include "core/bitmath_func.hpp"
#include "core/random_func.hpp"
#include "vehiclelist.h"
#include "road.h"
#include "ai/ai.hpp"
#include "news_func.h"
#include "strings_func.h"
#include "autoreplace_cmd.h"
#include "group_cmd.h"
#include "order_cmd.h"
#include "train_cmd.h"
#include "vehicle_cmd.h"
#include "table/strings.h"
#include "safeguards.h"
Go to the source code of this file.
Data Structures | |
struct | ReplaceChainItem |
Struct for recording vehicle chain replacement information. More... | |
Functions | |
void | ChangeVehicleViewports (VehicleID from_index, VehicleID to_index) |
Switches viewports following vehicles, which get autoreplaced. | |
void | ChangeVehicleNews (VehicleID from_index, VehicleID to_index) |
Report a change in vehicle IDs (due to autoreplace) to affected vehicle news. | |
void | ChangeVehicleViewWindow (VehicleID from_index, VehicleID to_index) |
Report a change in vehicle IDs (due to autoreplace) to affected vehicle windows. | |
static bool | EnginesHaveCargoInCommon (EngineID engine_a, EngineID engine_b) |
Figure out if two engines got at least one type of cargo in common (refitting if needed) | |
bool | CheckAutoreplaceValidity (EngineID from, EngineID to, CompanyID company) |
Checks some basic properties whether autoreplace is allowed. | |
void | CheckCargoCapacity (Vehicle *v) |
Check the capacity of all vehicles in a chain and spread cargo if needed. | |
static void | TransferCargo (Vehicle *old_veh, Vehicle *new_head, bool part_of_chain) |
Transfer cargo from a single (articulated )old vehicle to the new vehicle chain. | |
static bool | VerifyAutoreplaceRefitForOrders (const Vehicle *v, EngineID engine_type) |
Tests whether refit orders that applied to v will also apply to the new vehicle type. | |
static int | GetIncompatibleRefitOrderIdForAutoreplace (const Vehicle *v, EngineID engine_type) |
Gets the index of the first refit order that is incompatible with the requested engine type. | |
static CargoID | GetNewCargoTypeForReplace (Vehicle *v, EngineID engine_type, bool part_of_chain) |
Function to find what type of cargo to refit to when autoreplacing. | |
static CommandCost | GetNewEngineType (const Vehicle *v, const Company *c, bool always_replace, EngineID &e) |
Get the EngineID of the replacement for a vehicle. | |
static CommandCost | BuildReplacementVehicle (Vehicle *old_veh, Vehicle **new_vehicle, bool part_of_chain, DoCommandFlag flags) |
Builds and refits a replacement vehicle Important: The old vehicle is still in the original vehicle chain (used for determining the cargo when the old vehicle did not carry anything, but the new one does) | |
static CommandCost | DoCmdStartStopVehicle (const Vehicle *v, bool evaluate_callback) |
Issue a start/stop command. | |
static CommandCost | CmdMoveVehicle (const Vehicle *v, const Vehicle *after, DoCommandFlag flags, bool whole_chain) |
Issue a train vehicle move command. | |
static CommandCost | CopyHeadSpecificThings (Vehicle *old_head, Vehicle *new_head, DoCommandFlag flags) |
Copy head specific things to the new vehicle chain after it was successfully constructed. | |
static CommandCost | ReplaceFreeUnit (Vehicle **single_unit, DoCommandFlag flags, bool *nothing_to_do) |
Replace a single unit in a free wagon chain. | |
static CommandCost | ReplaceChain (Vehicle **chain, DoCommandFlag flags, bool wagon_removal, bool *nothing_to_do) |
Replace a whole vehicle chain. | |
CommandCost | CmdAutoreplaceVehicle (DoCommandFlag flags, VehicleID veh_id) |
Autoreplaces a vehicle Trains are replaced as a whole chain, free wagons in depot are replaced on their own. | |
CommandCost | CmdSetAutoReplace (DoCommandFlag flags, GroupID id_g, EngineID old_engine_type, EngineID new_engine_type, bool when_old) |
Change engine renewal parameters. | |
Deals with autoreplace execution but not the setup.
Definition in file autoreplace_cmd.cpp.
|
static |
Builds and refits a replacement vehicle Important: The old vehicle is still in the original vehicle chain (used for determining the cargo when the old vehicle did not carry anything, but the new one does)
old_veh | A single (articulated/multiheaded) vehicle that shall be replaced. |
new_vehicle | Returns the newly build and refitted vehicle |
part_of_chain | The vehicle is part of a train |
flags | The calling command flags. |
Definition at line 321 of file autoreplace_cmd.cpp.
References _current_company, CommandCost::AddCost(), AddVehicleAdviceNewsItem(), CARGO_NO_REFIT, Vehicle::cargo_type, DC_AUTOREPLACE, DC_EXEC, CommandCost::Failed(), SpecializedVehicle< T, Type >::From(), CargoSpec::Get(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_vehicle_pool >::Get(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), GetBestFittingSubType(), GetIncompatibleRefitOrderIdForAutoreplace(), GetNewCargoTypeForReplace(), GetNewEngineType(), HasBit(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, INVALID_CLIENT_ID, INVALID_ENGINE, IsLocalCompany(), IsValidCargoID(), CargoSpec::name, SetDParam(), CommandCost::Succeeded(), Vehicle::tile, BaseVehicle::type, VEH_TRAIN, and VRF_REVERSE_DIRECTION.
Referenced by ReplaceChain(), and ReplaceFreeUnit().
Report a change in vehicle IDs (due to autoreplace) to affected vehicle news.
from_index | the old vehicle ID |
to_index | the new vehicle ID |
Definition at line 1052 of file news_gui.cpp.
References _news, NewsItem::flags, NF_VEHICLE_PARAM0, NR_VEHICLE, NewsItem::params, NewsItem::ref1, NewsItem::ref2, NewsItem::reftype1, and NewsItem::reftype2.
Referenced by CopyHeadSpecificThings().
Switches viewports following vehicles, which get autoreplaced.
from_index | the old vehicle ID |
to_index | the new vehicle ID |
Definition at line 3435 of file window.cpp.
References ViewportData::follow_vehicle, Window::SetDirty(), and Window::viewport.
Referenced by CopyHeadSpecificThings().
Report a change in vehicle IDs (due to autoreplace) to affected vehicle windows.
from_index | the old vehicle ID |
to_index | the new vehicle ID |
Definition at line 1608 of file vehicle_gui.cpp.
References ChangeVehicleWindow(), WC_VEHICLE_DETAILS, WC_VEHICLE_ORDERS, WC_VEHICLE_REFIT, WC_VEHICLE_TIMETABLE, and WC_VEHICLE_VIEW.
Referenced by CopyHeadSpecificThings().
Checks some basic properties whether autoreplace is allowed.
from | Origin engine |
to | Destination engine |
company | Company to check for |
Definition at line 60 of file autoreplace_cmd.cpp.
References AIR_CTOL, RailTypeInfo::compatible_railtypes, EF_ROAD_TRAM, EnginesHaveCargoInCommon(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get(), GetRailTypeInfo(), GetRoadTypeInfo(), HasBit(), IsEngineBuildable(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::IsValidID(), EngineInfo::misc_flags, RoadTypeInfo::powered_roadtypes, RailVehicleInfo::railtype, RAILVEH_WAGON, RoadVehicleInfo::roadtype, ROADTYPES_NONE, AircraftVehicleInfo::subtype, Engine::type, VEH_AIRCRAFT, VEH_ROAD, and VEH_TRAIN.
Referenced by CmdSetAutoReplace(), and ReplaceVehicleWindow::GenerateReplaceVehList().
void CheckCargoCapacity | ( | Vehicle * | v | ) |
Check the capacity of all vehicles in a chain and spread cargo if needed.
v | The vehicle to check. |
Definition at line 107 of file autoreplace_cmd.cpp.
References Vehicle::First(), CargoList< VehicleCargoList, CargoPacketList >::MTA_KEEP, and Vehicle::Next().
Referenced by CmdMoveRailVehicle(), and ReplaceChain().
CommandCost CmdAutoreplaceVehicle | ( | DoCommandFlag | flags, |
VehicleID | veh_id | ||
) |
Autoreplaces a vehicle Trains are replaced as a whole chain, free wagons in depot are replaced on their own.
flags | type of operation |
veh_id | Index of vehicle |
Definition at line 732 of file autoreplace_cmd.cpp.
References _current_company, CommandCost::AddCost(), CheckOwnership(), CMD_ERROR, DC_EXEC, DoCmdStartStopVehicle(), EXPENSES_NEW_VEHICLES, CommandCost::Failed(), SpecializedVehicle< T, Type >::First(), Group::flags, SpecializedVehicle< T, Type >::From(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), CommandCost::GetCost(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_group_pool >::GetIfValid(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_vehicle_pool >::GetIfValid(), GetNewEngineType(), GF_REPLACE_WAGON_REMOVAL, Vehicle::group_id, HasBit(), INVALID_ENGINE, Vehicle::IsArticulatedPart(), Vehicle::IsChainInDepot(), Vehicle::IsFrontEngine(), Vehicle::IsPrimaryVehicle(), GroundVehicle< T, Type >::IsRearDualheaded(), Vehicle::IsStoppedInDepot(), Vehicle::owner, CompanySettings::renew_keep_length, ReplaceChain(), ReplaceFreeUnit(), RestoreRandomSeeds(), SaveRandomSeeds(), CompanyProperties::settings, CommandCost::Succeeded(), BaseVehicle::type, VEH_TRAIN, Vehicle::vehstatus, VS_CRASHED, and VS_STOPPED.
|
inlinestatic |
Issue a train vehicle move command.
v | The vehicle to move |
after | The vehicle to insert 'v' after, or nullptr to start new chain |
flags | the command flags to use |
whole_chain | move all vehicles following 'v' (true), or only 'v' (false) |
Definition at line 398 of file autoreplace_cmd.cpp.
References DC_NO_CARGO_CAP_CHECK, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, and INVALID_VEHICLE.
Referenced by ReplaceChain(), and ReplaceFreeUnit().
CommandCost CmdSetAutoReplace | ( | DoCommandFlag | flags, |
GroupID | id_g, | ||
EngineID | old_engine_type, | ||
EngineID | new_engine_type, | ||
bool | when_old | ||
) |
Change engine renewal parameters.
flags | operation to perform |
id_g | engine group |
old_engine_type | old engine type |
new_engine_type | new engine type |
when_old | replace when engine gets old? |
Definition at line 820 of file autoreplace_cmd.cpp.
References _current_company, AddEngineReplacementForCompany(), CheckAutoreplaceValidity(), CMD_ERROR, DC_EXEC, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_group_pool >::Get(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::GetIfValid(), GetWindowClassForVehicleType(), INVALID_ENGINE, InvalidateAutoreplaceWindow(), IsAllGroupID(), IsLocalCompany(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_group_pool >::IsValidID(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::IsValidID(), RemoveEngineReplacementForCompany(), SetWindowDirty(), GroupStatistics::UpdateAutoreplace(), and WC_REPLACE_VEHICLE.
|
static |
Copy head specific things to the new vehicle chain after it was successfully constructed.
old_head | The old front vehicle (no wagons attached anymore) |
new_head | The new head of the completely replaced vehicle chain |
flags | the command flags to use |
Definition at line 409 of file autoreplace_cmd.cpp.
References CommandCost::AddCost(), GroupStatistics::AddProfitLastYear(), ChangeVehicleNews(), ChangeVehicleViewports(), ChangeVehicleViewWindow(), Vehicle::CopyVehicleConfigAndStatistics(), DC_EXEC, DoCmdStartStopVehicle(), Vehicle::group_id, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, CommandCost::Succeeded(), Vehicle::vehstatus, and VS_STOPPED.
Referenced by ReplaceChain().
|
inlinestatic |
Issue a start/stop command.
v | a vehicle |
evaluate_callback | shall the start/stop callback be evaluated? |
Definition at line 385 of file autoreplace_cmd.cpp.
References DC_AUTOREPLACE, DC_EXEC, and Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index.
Referenced by CmdAutoreplaceVehicle(), and CopyHeadSpecificThings().
Figure out if two engines got at least one type of cargo in common (refitting if needed)
engine_a | one of the EngineIDs |
engine_b | the other EngineID |
Definition at line 46 of file autoreplace_cmd.cpp.
References GetUnionOfArticulatedRefitMasks().
Referenced by CheckAutoreplaceValidity().
|
static |
Gets the index of the first refit order that is incompatible with the requested engine type.
v | The vehicle to be replaced |
engine_type | The type we want to replace with |
Definition at line 205 of file autoreplace_cmd.cpp.
References Vehicle::First(), OrderList::GetNumOrders(), OrderList::GetOrderAt(), Order::GetRefitCargo(), GetUnionOfArticulatedRefitMasks(), HasBit(), Order::IsRefit(), Vehicle::orders, BaseVehicle::type, and VEH_TRAIN.
Referenced by BuildReplacementVehicle().
|
static |
Function to find what type of cargo to refit to when autoreplacing.
*v | Original vehicle that is being replaced. |
engine_type | The EngineID of the vehicle that is being replaced to |
part_of_chain | The vehicle is part of a train |
Definition at line 232 of file autoreplace_cmd.cpp.
References Engine::CanCarryCargo(), CARGO_NO_REFIT, Vehicle::cargo_type, Vehicle::First(), GetArticulatedRefitMasks(), GetCargoTypesOfArticulatedParts(), GetCargoTypesOfArticulatedVehicle(), Vehicle::GetEngine(), HasAtMostOneBit(), HasBit(), IsValidCargoID(), Vehicle::Next(), BaseVehicle::type, VEH_TRAIN, and VerifyAutoreplaceRefitForOrders().
Referenced by BuildReplacementVehicle().
|
static |
Get the EngineID of the replacement for a vehicle.
v | The vehicle to find a replacement for | |
c | The vehicle's owner (it's faster to forward the pointer than refinding it) | |
always_replace | Always replace, even if not old. | |
[out] | e | the EngineID of the replacement. INVALID_ENGINE if no replacement is found |
Definition at line 282 of file autoreplace_cmd.cpp.
References _current_company, Vehicle::engine_type, EngineReplacementForCompany(), SpecializedVehicle< T, Type >::From(), Vehicle::group_id, INVALID_ENGINE, Vehicle::IsArticulatedPart(), IsEngineBuildable(), Vehicle::NeedsAutorenewing(), BaseVehicle::type, and VEH_TRAIN.
Referenced by BuildReplacementVehicle(), and CmdAutoreplaceVehicle().
|
static |
Replace a whole vehicle chain.
chain | vehicle chain to let autoreplace/renew operator on |
flags | command flags |
wagon_removal | remove wagons when the resulting chain occupies more tiles than the old did |
nothing_to_do | is set to 'false' when something was done (only valid when not failed) |
< Shall store the last engine unit after this step
Definition at line 518 of file autoreplace_cmd.cpp.
References _settings_game, CommandCost::AddCost(), BuildReplacementVehicle(), CeilDiv(), CheckCargoCapacity(), CmdMoveVehicle(), CopyHeadSpecificThings(), DC_AUTOREPLACE, DC_EXEC, Vehicle::engine_type, EXPENSES_NEW_VEHICLES, CommandCost::Failed(), Vehicle::First(), SpecializedVehicle< T, Type >::From(), CommandCost::GetCost(), CommandCost::GetErrorMessage(), Train::GetNextUnit(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, INVALID_CLIENT_ID, Vehicle::IsPrimaryVehicle(), VehicleSettings::max_train_length, ReplaceChainItem::new_veh, AI::NewEvent(), ReplaceChainItem::old_veh, Vehicle::owner, RAILVEH_WAGON, CommandCost::Succeeded(), TILE_SIZE, TransferCargo(), BaseVehicle::type, VEH_TRAIN, and GameSettings::vehicle.
Referenced by CmdAutoreplaceVehicle().
|
static |
Replace a single unit in a free wagon chain.
single_unit | vehicle to let autoreplace/renew operator on |
flags | command flags |
nothing_to_do | is set to 'false' when something was done (only valid when not failed) |
Definition at line 451 of file autoreplace_cmd.cpp.
References CommandCost::AddCost(), BuildReplacementVehicle(), CmdMoveVehicle(), DC_EXEC, EXPENSES_NEW_VEHICLES, SpecializedVehicle< T, Type >::From(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, INVALID_CLIENT_ID, Vehicle::IsArticulatedPart(), GroundVehicle< T, Type >::IsRearDualheaded(), AI::NewEvent(), Vehicle::owner, CommandCost::Succeeded(), and TransferCargo().
Referenced by CmdAutoreplaceVehicle().
Transfer cargo from a single (articulated )old vehicle to the new vehicle chain.
old_veh | Old vehicle that will be sold |
new_head | Head of the completely constructed new vehicle chain |
part_of_chain | The vehicle is part of a train |
Definition at line 142 of file autoreplace_cmd.cpp.
References Vehicle::cargo, CCF_LOADUNLOAD, SpecializedVehicle< T, Type >::From(), Vehicle::IsArticulatedPart(), Vehicle::IsPrimaryVehicle(), CargoList< VehicleCargoList, CargoPacketList >::MTA_KEEP, Vehicle::Next(), NUM_CARGO, VehicleCargoList::TotalCount(), BaseVehicle::type, and VEH_TRAIN.
Referenced by ReplaceChain(), and ReplaceFreeUnit().
Tests whether refit orders that applied to v will also apply to the new vehicle type.
v | The vehicle to be replaced |
engine_type | The type we want to replace with |
Definition at line 182 of file autoreplace_cmd.cpp.
References Vehicle::engine_type, Vehicle::First(), GetUnionOfArticulatedRefitMasks(), HasBit(), Vehicle::Orders(), BaseVehicle::type, and VEH_TRAIN.
Referenced by GetNewCargoTypeForReplace().