33#include "script/api/script_event_types.hpp"
35#include "table/strings.h"
53 return available_cargoes_a.
None() || available_cargoes_b.
None() || available_cargoes_a.
Any(available_cargoes_b);
112 assert(v ==
nullptr || v->
First() == v);
114 for (
Vehicle *src = v; src !=
nullptr; src = src->
Next()) {
115 assert(src->cargo.TotalCount() == src->cargo.ActionCount(VehicleCargoList::MoveToAction::Keep));
118 if (src->cargo.TotalCount() <= src->cargo_cap)
continue;
121 uint to_spread = src->cargo.TotalCount() - src->cargo_cap;
122 for (
Vehicle *dest = v; dest !=
nullptr && to_spread != 0; dest = dest->
Next()) {
123 assert(dest->cargo.TotalCount() == dest->cargo.ActionCount(VehicleCargoList::MoveToAction::Keep));
124 if (dest->cargo.TotalCount() >= dest->cargo_cap || dest->cargo_type != src->cargo_type)
continue;
126 uint amount = std::min(to_spread, dest->cargo_cap - dest->cargo.TotalCount());
127 src->cargo.Shift(amount, &dest->cargo);
132 if (src->cargo_cap < src->cargo.TotalCount()) src->cargo.Truncate(src->cargo.TotalCount() - src->cargo_cap);
149 for (
Vehicle *src = old_veh; src !=
nullptr; src = src->
Next()) {
150 assert(src->cargo.TotalCount() == src->cargo.ActionCount(VehicleCargoList::MoveToAction::Keep));
151 if (!part_of_chain && src->type ==
VehicleType::Train && src != old_veh && src !=
Train::From(old_veh)->other_multiheaded_part && !src->IsArticulatedPart()) {
153 src = src->GetLastEnginePart();
156 if (src->cargo_type >=
NUM_CARGO || src->cargo.TotalCount() == 0)
continue;
160 assert(dest->cargo.TotalCount() == dest->cargo.ActionCount(VehicleCargoList::MoveToAction::Keep));
161 if (!part_of_chain && dest->type ==
VehicleType::Train && dest != new_head && dest !=
Train::From(new_head)->other_multiheaded_part && !dest->IsArticulatedPart()) {
163 dest = dest->GetLastEnginePart();
166 if (dest->cargo_type != src->cargo_type)
continue;
168 uint amount = std::min(src->cargo.TotalCount(), dest->cargo_cap - dest->cargo.TotalCount());
169 if (amount <= 0)
continue;
171 src->cargo.Shift(amount, &dest->cargo);
191 for (
const Order &o : u->Orders()) {
192 if (!o.IsRefit() || o.IsAutoRefit())
continue;
193 CargoType cargo_type = o.GetRefitCargo();
195 if (!union_refit_mask_a.
Test(cargo_type))
continue;
196 if (!union_refit_mask_b.
Test(cargo_type))
return false;
215 if (orders ==
nullptr)
return -1;
245 if ((cargo_mask & new_engine_default_cargoes) == cargo_mask) {
260 for (v = v->
First(); v !=
nullptr; v = v->
Next()) {
288 e = EngineID::Invalid();
296 e = er ==
nullptr ? EngineID::Invalid() : er->
to;
299 if (!always_replace && replace_when_old && !v->
NeedsAutorenewing(c,
false)) e = EngineID::Invalid();
327 *new_vehicle =
nullptr;
333 if (cost.
Failed())
return cost;
334 if (e == EngineID::Invalid())
return CommandCost();
345 if (order_id != -1) {
349 STR_ERROR_AUTOREPLACE_INCOMPATIBLE_REFIT,
355 STR_ERROR_AUTOREPLACE_INCOMPATIBLE_CARGO,
365 std::tie(cost, new_veh_id, std::ignore, std::ignore, std::ignore) = Command<Commands::BuildVehicle>::Do({
DoCommandFlag::Execute,
DoCommandFlag::AutoReplace}, old_veh->
tile, e,
true,
INVALID_CARGO,
ClientID::Invalid);
366 if (cost.
Failed())
return cost;
369 *new_vehicle = new_veh;
476 if (cost.
Succeeded() && new_v !=
nullptr) {
477 *nothing_to_do =
false;
491 *single_unit = new_v;
493 AI::NewEvent(old_v->
owner,
new ScriptEventVehicleAutoReplaced(old_v->index, new_v->index));
548 std::vector<ReplaceChainItem> replacements;
552 for (
Train *w =
Train::From(old_head); w !=
nullptr; w = w->GetNextUnit()) {
560 if (replacement.
new_veh !=
nullptr) *nothing_to_do =
false;
562 Vehicle *new_head = replacements.front().GetVehicle();
570 assert(
Train::From(new_head)->GetNextUnit() ==
nullptr);
576 Vehicle *last_engine =
nullptr;
578 for (
auto it = std::rbegin(replacements); it != std::rend(replacements); ++it) {
579 Vehicle *append = it->GetVehicle();
583 if (it->new_veh !=
nullptr) {
587 CmdMoveVehicle(it->old_veh,
nullptr, {DoCommandFlag::Execute, DoCommandFlag::AutoReplace},
false);
590 if (last_engine ==
nullptr) last_engine = append;
594 if (last_engine ==
nullptr) last_engine = new_head;
598 if (cost.
Succeeded() && wagon_removal &&
Train::From(new_head)->gcache.cached_total_length > old_total_length) cost =
CommandCost(STR_ERROR_TRAIN_TOO_LONG_AFTER_REPLACEMENT);
604 for (
auto it = std::rbegin(replacements); it != std::rend(replacements); ++it) {
605 assert(last_engine !=
nullptr);
606 Vehicle *append = it->GetVehicle();
615 if (wagon_removal && (res.
Failed() ? res.
GetErrorMessage() == STR_ERROR_TRAIN_TOO_LONG :
Train::From(new_head)->gcache.cached_total_length > old_total_length)) {
624 assert(append == last_engine);
633 for (
auto it = std::next(std::begin(replacements)); it != std::end(replacements); ++it) {
635 if (wagon ==
nullptr)
continue;
636 if (wagon->
First() == new_head)
break;
643 it->new_veh =
nullptr;
658 AI::NewEvent(old_head->
owner,
new ScriptEventVehicleAutoReplaced(old_head->index, new_head->index));
662 for (
auto it = std::begin(replacements); it != std::end(replacements); ++it) {
666 if (w->
First() == new_head)
continue;
675 it->old_veh =
nullptr;
676 if (it == std::begin(replacements)) old_head =
nullptr;
691 assert(
Train::From(old_head)->GetNextUnit() ==
nullptr);
693 for (
auto it = std::rbegin(replacements); it != std::rend(replacements); ++it) {
694 [[maybe_unused]]
CommandCost ret =
CmdMoveVehicle(it->old_veh, old_head, {DoCommandFlag::Execute, DoCommandFlag::AutoReplace},
false);
702 for (
auto it = std::rbegin(replacements); it != std::rend(replacements); ++it) {
703 if (it->new_veh !=
nullptr) {
705 it->new_veh =
nullptr;
715 if (cost.
Succeeded() && new_head !=
nullptr) {
716 *nothing_to_do =
false;
727 AI::NewEvent(old_head->
owner,
new ScriptEventVehicleAutoReplaced(old_head->index, new_head->index));
757 if (ret.
Failed())
return ret;
761 bool free_wagon =
false;
780 bool any_replacements =
false;
781 while (w !=
nullptr) {
784 if (cost.
Failed())
return cost;
785 any_replacements |= (e != EngineID::Invalid());
790 bool nothing_to_do =
true;
792 if (any_replacements) {
797 if (cost.
Failed())
return cost;
817 ret =
ReplaceChain(&v, flags, wagon_removal, ¬hing_to_do);
850 if (new_engine_type != EngineID::Invalid()) {
Base functions for all AIs.
CargoTypes GetUnionOfArticulatedRefitMasks(EngineID engine, bool include_initial_cargo_type)
Ors the refit_masks of all articulated parts.
CargoTypes GetCargoTypesOfArticulatedVehicle(const Vehicle *v, CargoType *cargo_type)
Get cargo mask of all cargoes carried by an articulated vehicle.
void GetArticulatedRefitMasks(EngineID engine, bool include_initial_cargo_type, CargoTypes *union_mask, CargoTypes *intersection_mask)
Merges the refit_masks of all articulated parts.
CargoTypes GetCargoTypesOfArticulatedParts(EngineID engine)
Get the cargo mask of the parts of a given engine.
Functions related to articulated vehicles.
CommandCost AddEngineReplacement(DoCommandFlags flags, CompanyID company, GroupID group, EngineID engine, EngineID new_engine, bool replace_when_old)
Add an engine replacement to the given renewlist.
const EngineRenew * GetEngineRenew(const EngineRenewList &erl, EngineID engine)
Search within a single engine renew list for an engine to renew.
CommandCost RemoveEngineReplacement(DoCommandFlags flags, CompanyID company, GroupID group, EngineID engine)
Remove an engine replacement from a given renewlist.
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 CommandCost DoCmdStartStopVehicle(const Vehicle *v, bool evaluate_callback)
Issue a start/stop command.
void CheckCargoCapacity(Vehicle *v)
Check the capacity of all vehicles in a chain and spread cargo if needed.
static CargoType GetNewCargoTypeForReplace(Vehicle *v, EngineID engine_type, bool part_of_chain)
Function to find what type of cargo to refit to when autoreplacing.
void ChangeVehicleViewports(VehicleID from_index, VehicleID to_index)
Switches viewports following vehicles, which get autoreplaced.
static CommandCost CmdMoveVehicle(const Vehicle *v, const Vehicle *after, DoCommandFlags flags, bool whole_chain)
Issue a train vehicle move command.
CommandCost CmdSetAutoReplace(DoCommandFlags flags, GroupID id_g, EngineID old_engine_type, EngineID new_engine_type, bool when_old)
Change engine renewal parameters.
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).
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.
bool CheckAutoreplaceValidity(EngineID from, EngineID to, CompanyID company)
Checks some basic properties whether autoreplace is allowed.
static CommandCost CopyHeadSpecificThings(Vehicle *old_head, Vehicle *new_head, DoCommandFlags flags)
Copy head specific things to the new vehicle chain after it was successfully constructed.
static CommandCost GetNewEngineType(const Vehicle *v, const Company *c, bool always_replace, EngineID &e)
Get the EngineID of the replacement for a vehicle.
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 CommandCost ReplaceFreeUnit(Vehicle **single_unit, DoCommandFlags flags, bool *nothing_to_do)
Replace a single unit in a free wagon chain.
static CommandCost BuildReplacementVehicle(Vehicle *old_veh, Vehicle **new_vehicle, bool part_of_chain, DoCommandFlags flags)
Builds and refits a replacement vehicle Important: The old vehicle is still in the original vehicle c...
static CommandCost ReplaceChain(Vehicle **chain, DoCommandFlags flags, bool wagon_removal, bool *nothing_to_do)
Replace a whole vehicle chain.
CommandCost CmdAutoreplaceVehicle(DoCommandFlags flags, VehicleID veh_id)
Autoreplaces a vehicle Trains are replaced as a whole chain, free wagons in depot are replaced on the...
Command definitions related to autoreplace.
Functions related to autoreplacing.
void InvalidateAutoreplaceWindow(EngineID e, GroupID id_g)
Rebuild the left autoreplace list if an engine is removed or added.
Functions related to the autoreplace GUIs.
Functions related to bit mathematics.
constexpr bool HasAtMostOneBit(T value)
Test whether value has at most 1 bit set.
static constexpr CargoType INVALID_CARGO
Invalid marker for cargoes.
bool IsValidCargoType(CargoType cargo)
Test whether cargo type is not INVALID_CARGO.
EnumBitSet< CargoType, uint64_t > CargoTypes
Bitset of CargoType elements.
static constexpr CargoType NUM_CARGO
Maximum number of cargo types in a game.
static constexpr CargoType CARGO_NO_REFIT
Do not refit cargo of a vehicle (used in vehicle orders and auto-replace/auto-renew).
CargoType
Cargo slots to indicate a cargo type within a game.
static void NewEvent(CompanyID company, ScriptEvent *event)
Queue a new event for an AI.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Tstorage base() const noexcept
Retrieve the raw value behind this bit set.
constexpr bool None() const
Test if none of the values are set.
constexpr Timpl & Set()
Set all bits.
constexpr bool Any(const Timpl &other) const
Test if any of the given values are set.
Common return value for all commands.
bool Succeeded() const
Did this command succeed?
void AddCost(const Money &cost)
Adds the given cost to the cost of the command.
Money GetCost() const
The costs as made up to this moment.
bool Failed() const
Did this command fail?
StringID GetErrorMessage() const
Returns the error message of a command.
Container for an encoded string, created by GetEncodedString.
VehicleType type
Vehicle type, ie VehicleType::Road, VehicleType::Train, etc.
bool CanCarryCargo() const
Determines whether an engine can carry something.
RoadTypes powered_roadtypes
bitmask to the OTHER roadtypes on which a vehicle of THIS roadtype generates power
uint TotalCount() const
Returns sum of cargo, including reserved cargo.
Functions related to commands.
CommandCost & ExtractCommandCost(Tret &ret)
Extract the CommandCost from a command proc result.
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
@ Execute
execute the given command
@ NoCargoCapacityCheck
when autoreplace/autorenew is in progress, this shall prevent truncating the amount of cargo in the v...
@ AutoReplace
autoreplace/autorenew is in progress, this shall disable vehicle limits when building,...
EnumBitSet< DoCommandFlag, uint16_t > DoCommandFlags
Bitset of DoCommandFlag elements.
Definition of stuff that is very close to a company, like the company struct itself.
CommandCost CheckOwnership(Owner owner, TileIndex tile)
Check whether the current owner owns something.
CompanyID _current_company
Company currently doing an action.
Functions related to companies.
bool IsLocalCompany()
Is the current company the local company?
@ NewVehicles
New vehicles.
bool IsEngineBuildable(EngineID engine, VehicleType type, CompanyID company)
Check if an engine is buildable.
Functions related to engines.
@ AIR_CTOL
Conventional Take Off and Landing, i.e. planes.
@ RoadIsTram
Road vehicle is a tram/light rail vehicle.
PoolID< uint16_t, struct EngineIDTag, 64000, 0xFFFF > EngineID
Unique identification number of an engine.
@ Wagon
simple wagon, not motorized
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
@ ReplaceWagonRemoval
If set, autoreplace will perform wagon removal on vehicles in this group.
bool IsAllGroupID(GroupID id_g)
Checks if a GroupID stands for all vehicles of a company.
Command definitions related to engine groups.
constexpr uint CeilDiv(uint a, uint b)
Computes ceil(a / b) for non-negative a and b.
@ Invalid
Client is not part of anything.
std::optional< bool > TestVehicleBuildProbability(Vehicle *v, BuildProbabilityType type)
Test for vehicle build probability type.
@ Reversed
Change the rail vehicle should be reversed when purchased.
Functions related to news.
void AddVehicleAdviceNewsItem(AdviceType advice_type, EncodedString &&headline, VehicleID vehicle)
Adds a vehicle-advice news item.
@ AutorenewFailed
Autorenew or autoreplace failed.
Command definitions related to orders.
uint8_t VehicleOrderID
The index of an order within its current vehicle (not pool related).
RailTypes GetAllCompatibleRailTypes(RailTypes railtypes)
Returns all compatible railtypes for a set of railtypes.
Pseudo random number generator.
void SaveRandomSeeds(SavedRandomSeeds *storage)
Saves the current seeds.
void RestoreRandomSeeds(const SavedRandomSeeds &storage)
Restores previously saved seeds.
const RoadTypeInfo * GetRoadTypeInfo(RoadType roadtype)
Returns a pointer to the Roadtype information for a given roadtype.
A number of safeguards to prevent using unsafe methods.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition of base types and functions in a cross-platform compatible way.
EncodedString GetEncodedString(StringID str)
Encode a string with no parameters into an encoded string.
Functions related to OTTD's strings.
Information about a aircraft vehicle.
uint8_t subtype
Type of aircraft.
VehicleType type
Type of vehicle.
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo type.
CompanySettings settings
settings specific for each company
bool renew_keep_length
sell some wagons if after autoreplace the train is longer than before
EngineMiscFlags misc_flags
Miscellaneous flags.
Information about engine autorenews and autoreplacements.
EngineID to
Engine to replace to.
bool replace_when_old
Do replacement only when vehicle is old.
bool IsRearDualheaded() const
Tell if we are dealing with the rear end of a multiheaded engine.
static void AddProfitLastYear(const Vehicle *v)
Add a vehicle's last year profit to the profit sum of its group.
static void UpdateAutoreplace(CompanyID company)
Update autoreplace_defined and autoreplace_finished of all statistics of a company.
GroupFlags flags
Group flags.
Shared order list linking together the linked list of orders and the list of vehicles sharing this or...
VehicleOrderID GetNumOrders() const
Get number of orders in the order list.
const Order * GetOrderAt(VehicleOrderID index) const
Get a certain order of the order chain.
If you change this, keep in mind that it is also saved in 2 other places:
CargoType GetRefitCargo() const
Get the cargo to to refit to.
bool IsRefit() const
Is this order a refit order.
static Engine * Get(auto index)
static bool IsValidID(auto index)
static Vehicle * GetIfValid(auto index)
Information about a rail vehicle.
RailTypes railtypes
Railtypes, mangled if elrail is disabled.
RailVehicleType railveh_type
Type of rail vehicle.
Struct for recording vehicle chain replacement information.
Vehicle * GetVehicle() const
Get vehicle to use for this position.
Vehicle * new_veh
Replacement vehicle, or nullptr if no replacement.
ReplaceChainItem(Vehicle *old_veh, Vehicle *new_veh, Money cost)
Create a new item.
Money cost
Cost of buying and refitting replacement.
Vehicle * old_veh
Old vehicle to replace.
Information about a road vehicle.
RoadType roadtype
Road type.
Stores the state of all random number generators.
static Train * From(Vehicle *v)
T * First() const
Get the first vehicle in the chain.
'Train' is either a loco or a wagon.
Train * GetNextUnit() const
Get the next real (non-articulated part and non rear part of dualheaded engine) vehicle in the consis...
Train * GetPrevUnit()
Get the previous real (non-articulated part and non rear part of dualheaded engine) vehicle in the co...
void ConsistChanged(ConsistChangeFlags allowed_changes)
Recalculates the cached stuff of a train.
The information about a vehicle list.
EngineID engine_type
The type of engine used for this vehicle.
const Engine * GetEngine() const
Retrieves the engine of the vehicle.
bool IsStoppedInDepot() const
Check whether the vehicle is in the depot and stopped.
virtual bool IsChainInDepot() const
Check whether the whole vehicle chain is in the depot.
VehicleCargoList cargo
The cargo this vehicle is carrying.
GroupID group_id
Index of group Pool array.
VehStates vehstatus
Status.
bool IsArticulatedPart() const
Check if the vehicle is an articulated part of an engine.
bool NeedsAutorenewing(const Company *c, bool use_renew_setting=true) const
Function to tell if a vehicle needs to be autorenewed.
CargoType cargo_type
type of cargo this vehicle is carrying
Vehicle * First() const
Get the first vehicle of this vehicle chain.
Vehicle * Next() const
Get the next vehicle of this vehicle.
OrderList * orders
Pointer to the order list for this vehicle.
virtual bool IsPrimaryVehicle() const
Whether this is the primary vehicle in the chain.
bool IsFrontEngine() const
Check if the vehicle is a front engine.
TileIndex tile
Current tile index.
void CopyVehicleConfigAndStatistics(Vehicle *src)
Copy certain configurations and statistics of a vehicle after successful autoreplace/renew The functi...
Owner owner
Which company owns the vehicle?
static constexpr uint TILE_SIZE
Tile size in world coordinates.
Base for the train class.
static constexpr ConsistChangeFlags CCF_LOADUNLOAD
Valid changes while vehicle is loading/unloading.
@ Flipped
Reverse the visible direction of the vehicle.
Command definitions related to trains.
@ Crashed
Vehicle is crashed.
@ Stopped
Vehicle is stopped by the player.
Command definitions for vehicles.
Functions related to vehicles.
uint8_t GetBestFittingSubType(Vehicle *v_from, Vehicle *v_for, CargoType dest_cargo_type)
Get the best fitting subtype when 'cloning'/'replacing' v_from with v_for.
bool IsCompanyBuildableVehicleType(VehicleType type)
Is the given vehicle type buildable by a company?
Functions related to the vehicle's GUIs.
WindowClass GetWindowClassForVehicleType(VehicleType vt)
Get WindowClass for vehicle list of given vehicle type.
PoolID< uint32_t, struct VehicleIDTag, 0xFF000, 0xFFFFF > VehicleID
The type all our vehicle IDs have.
VehicleType
Available vehicle types.
@ Aircraft
Aircraft vehicle type.
@ Train
Train vehicle type.
Functions and type for generating vehicle lists.
@ Group
Index is the group.
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting).