24 #include "table/strings.h" 42 return (available_cargoes_a == 0 || available_cargoes_b == 0 || (available_cargoes_a & available_cargoes_b) != 0);
57 if (from == to)
return false;
104 assert(v ==
nullptr || v->
First() == v);
106 for (
Vehicle *src = v; src !=
nullptr; src = src->
Next()) {
110 if (src->cargo.TotalCount() <= src->cargo_cap)
continue;
113 uint to_spread = src->cargo.TotalCount() - src->cargo_cap;
114 for (
Vehicle *dest = v; dest !=
nullptr && to_spread != 0; dest = dest->
Next()) {
116 if (dest->cargo.TotalCount() >= dest->cargo_cap || dest->cargo_type != src->cargo_type)
continue;
118 uint amount =
min(to_spread, dest->cargo_cap - dest->cargo.TotalCount());
119 src->cargo.Shift(amount, &dest->cargo);
124 if (src->cargo_cap < src->cargo.TotalCount()) src->cargo.Truncate(src->cargo.TotalCount() - src->cargo_cap);
141 for (
Vehicle *src = old_veh; src !=
nullptr; src = src->
Next()) {
145 src = src->GetLastEnginePart();
148 if (src->cargo_type >=
NUM_CARGO || src->cargo.TotalCount() == 0)
continue;
155 dest = dest->GetLastEnginePart();
158 if (dest->cargo_type != src->cargo_type)
continue;
160 uint amount =
min(src->cargo.TotalCount(), dest->cargo_cap - dest->cargo.TotalCount());
161 if (amount <= 0)
continue;
163 src->cargo.Shift(amount, &dest->cargo);
184 FOR_VEHICLE_ORDERS(u, o) {
188 if (!
HasBit(union_refit_mask_a, cargo_type))
continue;
189 if (!
HasBit(union_refit_mask_b, cargo_type))
return false;
206 CargoTypes available_cargo_types, union_mask;
222 for (v = v->
First(); v !=
nullptr; v = v->
Next()) {
257 bool replace_when_old;
286 *new_vehicle =
nullptr;
292 if (cost.
Failed())
return cost;
301 if (cost.
Failed())
return cost;
304 *new_vehicle = new_veh;
405 if (cost.Succeeded() && new_v !=
nullptr) {
406 *nothing_to_do =
false;
420 *single_unit = new_v;
426 cost.AddCost(
DoCommand(0, old_v->
index, 0, flags, GetCmdSellVeh(old_v)));
429 if ((flags & DC_EXEC) == 0) {
459 Train **old_vehs = CallocT<Train *>(num_units);
460 Train **new_vehs = CallocT<Train *>(num_units);
461 Money *new_costs = MallocT<Money>(num_units);
468 assert(i < num_units);
473 if (cost.Failed())
break;
476 if (new_vehs[i] !=
nullptr) *nothing_to_do =
false;
478 Train *new_head = (new_vehs[0] !=
nullptr ? new_vehs[0] : old_vehs[0]);
481 if (cost.Succeeded()) {
486 assert(
Train::From(new_head)->GetNextUnit() ==
nullptr);
492 Train *last_engine =
nullptr;
493 if (cost.Succeeded()) {
494 for (
int i = num_units - 1; i > 0; i--) {
495 Train *append = (new_vehs[i] !=
nullptr ? new_vehs[i] : old_vehs[i]);
499 if (new_vehs[i] !=
nullptr) {
506 if (last_engine ==
nullptr) last_engine = append;
508 if (cost.Failed())
break;
510 if (last_engine ==
nullptr) last_engine = new_head;
519 if (cost.Succeeded()) {
520 for (
int i = num_units - 1; i > 0; i--) {
521 assert(last_engine !=
nullptr);
522 Vehicle *append = (new_vehs[i] !=
nullptr ? new_vehs[i] : old_vehs[i]);
537 if (cost.Failed())
break;
540 assert(append == last_engine);
547 if (cost.Succeeded() && wagon_removal) {
549 for (
int i = 1; i < num_units; i++) {
551 if (wagon ==
nullptr)
continue;
552 if (wagon->
First() == new_head)
break;
559 new_vehs[i] =
nullptr;
563 cost.AddCost(-new_costs[i]);
570 if (cost.Succeeded()) {
572 if ((flags &
DC_EXEC) != 0 && new_head != old_head) {
577 for (
int i = 0; i < num_units; i++) {
581 if (w->
First() == new_head)
continue;
583 if ((flags & DC_EXEC) != 0)
TransferCargo(w, new_head,
true);
589 if ((flags & DC_EXEC) != 0) {
590 old_vehs[i] =
nullptr;
609 assert(
Train::From(old_head)->GetNextUnit() ==
nullptr);
611 for (
int i = num_units - 1; i > 0; i--) {
620 for (
int i = num_units - 1; i >= 0; i--) {
621 if (new_vehs[i] !=
nullptr) {
622 DoCommand(0, new_vehs[i]->index, 0, DC_EXEC, GetCmdSellVeh(new_vehs[i]));
623 new_vehs[i] =
nullptr;
637 if (cost.Succeeded() && new_head !=
nullptr) {
638 *nothing_to_do =
false;
643 if (cost.Succeeded()) {
653 cost.AddCost(
DoCommand(0, old_head->
index, 0, flags, GetCmdSellVeh(old_head)));
658 DoCommand(0, new_head->
index, 0, DC_EXEC, GetCmdSellVeh(new_head));
682 if (ret.
Failed())
return ret;
687 bool free_wagon =
false;
702 bool any_replacements =
false;
703 while (w !=
nullptr) {
706 if (cost.
Failed())
return cost;
712 bool nothing_to_do =
true;
714 if (any_replacements) {
719 if (cost.
Failed())
return cost;
740 ret =
ReplaceChain(&v, flags, wagon_removal, ¬hing_to_do);
bool IsEngineBuildable(EngineID engine, VehicleType type, CompanyID company)
Check if an engine is buildable.
bool CheckAutoreplaceValidity(EngineID from, EngineID to, CompanyID company)
Checks some basic properties whether autoreplace is allowed.
Owner
Enum for all companies/owners.
VehicleSettings vehicle
options for vehicles
static CommandCost GetNewEngineType(const Vehicle *v, const Company *c, bool always_replace, EngineID &e)
Get the EngineID of the replacement for a vehicle.
static bool IsLocalCompany()
Is the current company the local company?
Vehicle is stopped by the player.
VehicleCargoList cargo
The cargo this vehicle is carrying.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
static const RailtypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
The information about a vehicle list.
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Functions related to the autoreplace GUIs.
Functions and type for generating vehicle lists.
static Titem * Get(size_t index)
Returns Titem with given index.
bool CanCarryCargo() const
Determines whether an engine can carry something.
Conventional Take Off and Landing, i.e. planes.
Base for the train class.
Stores the state of all random number generators.
Train * GetPrevUnit()
Get the previous real (non-articulated part and non rear part of dualheaded engine) vehicle in the co...
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Replace vehicle window; Window numbers:
Maximal number of cargo types in a game.
bool IsStoppedInDepot() const
Check whether the vehicle is in the depot and stopped.
VehicleType
Available vehicle types.
Train * GetNextUnit() const
Get the next real (non-articulated part and non rear part of dualheaded engine) vehicle in the consis...
static void RestoreRandomSeeds(const SavedRandomSeeds &storage)
Restores previously saved seeds.
byte GetBestFittingSubType(Vehicle *v_from, Vehicle *v_for, CargoID dest_cargo_type)
Get the best fitting subtype when 'cloning'/'replacing' v_from with v_for.
Functions related to vehicles.
CargoTypes GetUnionOfArticulatedRefitMasks(EngineID engine, bool include_initial_cargo_type)
Ors the refit_masks of all articulated parts.
static CommandCost AddEngineReplacementForCompany(Company *c, EngineID old_engine, EngineID new_engine, GroupID group, bool replace_when_old, DoCommandFlag flags)
Add an engine replacement for the company.
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) ...
Tindex index
Index of this pool item.
T * First() const
Get the first vehicle in the chain.
uint TotalCount() const
Returns sum of cargo, including reserved cargo.
clone (and share) an order
Money GetCost() const
The costs as made up to this moment.
RailTypes compatible_railtypes
bitmask to the OTHER railtypes on which an engine of THIS railtype can physically travel ...
bool IsArticulatedVehicleCarryingDifferentCargoes(const Vehicle *v, CargoID *cargo_type)
Tests if all parts of an articulated vehicle are refitted to the same cargo.
Common return value for all commands.
static const VehicleID INVALID_VEHICLE
Constant representing a non-existing vehicle.
static Train * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
CompanySettings settings
settings specific for each company
const Engine * GetEngine() const
Retrieves the engine of the vehicle.
static const uint TILE_SIZE
Tile size in world coordinates.
void AddCost(const Money &cost)
Adds the given cost to the cost of the command.
Do not refit cargo of a vehicle (used in vehicle orders and auto-replace/auto-new).
when autoreplace/autorenew is in progress, this shall prevent truncating the amount of cargo in the v...
RoadType roadtype
Road type.
bool IsAutoRefit() const
Is this order a auto-refit order.
Pseudo random number generator.
static bool IsAllGroupID(GroupID id_g)
Checks if a GroupID stands for all vehicles of a company.
static const RoadTypeInfo * GetRoadTypeInfo(RoadType roadtype)
Returns a pointer to the Roadtype information for a given roadtype.
static CommandCost BuildReplacementVehicle(Vehicle *old_veh, Vehicle **new_vehicle, bool part_of_chain)
Builds and refits a replacement vehicle Important: The old vehicle is still in the original vehicle c...
virtual bool IsPrimaryVehicle() const
Whether this is the primary vehicle in the chain.
RoadTypes powered_roadtypes
bitmask to the OTHER roadtypes on which a vehicle of THIS roadtype generates power ...
CommandCost DoCommand(const CommandContainer *container, DoCommandFlag flags)
Shorthand for calling the long DoCommand with a container.
byte subtype
Type of aircraft.
void ChangeVehicleViewports(VehicleID from_index, VehicleID to_index)
Switches viewports following vehicles, which get autoreplaced.
void ConsistChanged(ConsistChangeFlags allowed_changes)
Recalculates the cached stuff of a train.
bool IsRefit() const
Is this order a refit order.
Functions related to engines.
uint32 VehicleID
The type all our vehicle IDs have.
StringID GetErrorMessage() const
Returns the error message of a command.
DoCommandFlag
List of flags for a command.
simple wagon, not motorized
bool Succeeded() const
Did this command succeed?
Definition of base types and functions in a cross-platform compatible way.
bool IsArticulatedPart() const
Check if the vehicle is an articulated part of an engine.
A number of safeguards to prevent using unsafe methods.
static uint CeilDiv(uint a, uint b)
Computes ceil(a / b) for non-negative a and b.
uint16 GroupID
Type for all group identifiers.
VehicleType type
Vehicle type, ie VEH_ROAD, VEH_TRAIN, etc.
static CargoID GetNewCargoTypeForReplace(Vehicle *v, EngineID engine_type, bool part_of_chain)
Function to find what type of cargo to refit to when autoreplacing.
CargoID cargo_type
type of cargo this vehicle is carrying
bool IsFrontEngine() const
Check if the vehicle is a front engine.
byte misc_flags
Miscellaneous flags.
TileIndex tile
Current tile index.
CommandCost CheckOwnership(Owner owner, TileIndex tile)
Check whether the current owner owns something.
static EngineID EngineReplacementForCompany(const Company *c, EngineID engine, GroupID group, bool *replace_when_old=nullptr)
Retrieve the engine replacement for the given company and original engine type.
bool IsRearDualheaded() const
Tell if we are dealing with the rear end of a multiheaded engine.
Owner owner
Which company owns the vehicle?
bool renew_keep_length
sell some wagons if after autoreplace the train is longer than before
static T min(const T a, const T b)
Returns the minimum of two values.
CommandCost CmdSetAutoReplace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Change engine renewal parameters.
Vehicle * First() const
Get the first vehicle of this vehicle chain.
bool Failed() const
Did this command fail?
void ChangeVehicleNews(VehicleID from_index, VehicleID to_index)
Report a change in vehicle IDs (due to autoreplace) to affected vehicle news.
void CheckCargoCapacity(Vehicle *v)
Check the capacity of all vehicles in a chain and spread cargo if needed.
void InvalidateAutoreplaceWindow(EngineID e, GroupID id_g)
Rebuild the left autoreplace list if an engine is removed or added.
static void NewEvent(CompanyID company, ScriptEvent *event)
Queue a new event for an AI.
autoreplace/autorenew is in progress, this shall disable vehicle limits when building, and ignore certain restrictions when undoing things (like vehicle attach callback)
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.
'Train' is either a loco or a wagon.
execute the given command
Keep the cargo in the vehicle.
static const EngineID INVALID_ENGINE
Constant denoting an invalid engine.
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 CmdStartStopVehicle(const Vehicle *v, bool evaluate_callback)
Issue a start/stop command.
Functions related to companies.
Functions related to articulated vehicles.
bool NeedsAutorenewing(const Company *c, bool use_renew_setting=true) const
Function to tell if a vehicle needs to be autorenewed.
uint16 EngineID
Unique identification number of an engine.
uint32 TileIndex
The index/ID of a Tile.
Vehicle * Next() const
Get the next vehicle of this vehicle.
void GetArticulatedRefitMasks(EngineID engine, bool include_initial_cargo_type, CargoTypes *union_mask, CargoTypes *intersection_mask)
Merges the refit_masks of all articulated parts.
static void UpdateAutoreplace(CompanyID company)
Update autoreplace_defined and autoreplace_finished of all statistics of a company.
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
uint16 cached_total_length
Length of the whole vehicle (valid only for the first engine).
VehicleType type
Type of vehicle.
Valid changes while vehicle is loading/unloading.
Reverse the visible direction of the vehicle.
void CopyVehicleConfigAndStatistics(const Vehicle *src)
Copy certain configurations and statistics of a vehicle after successful autoreplace/renew The functi...
Functions related to commands.
CompanyID _current_company
Company currently doing an action.
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
static WindowClass GetWindowClassForVehicleType(VehicleType vt)
Get WindowClass for vehicle list of given vehicle type.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
CommandCost CmdAutoreplaceVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Autoreplaces a vehicle Trains are replaced as a whole chain, free wagons in depot are replaced on the...
EngineID engine_type
The type of engine used for this vehicle.
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Base functions for all AIs.
static void SaveRandomSeeds(SavedRandomSeeds *storage)
Saves the current seeds.
static CommandCost CmdMoveVehicle(const Vehicle *v, const Vehicle *after, DoCommandFlag flags, bool whole_chain)
Issue a train vehicle move command.
static CommandCost ReplaceFreeUnit(Vehicle **single_unit, DoCommandFlag flags, bool *nothing_to_do)
Replace a single unit in a free wagon chain.
byte CargoID
Cargo slots to indicate a cargo type within a game.
Road vehicle is a tram/light rail vehicle.
virtual bool IsChainInDepot() const
Check whether the whole vehicle chain is in the depot.
static CommandCost ReplaceChain(Vehicle **chain, DoCommandFlag flags, bool wagon_removal, bool *nothing_to_do)
Replace a whole vehicle chain.
move a rail vehicle (in the depot)
static CommandCost RemoveEngineReplacementForCompany(Company *c, EngineID engine, GroupID group, DoCommandFlag flags)
Remove an engine replacement for the company.
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.
Functions related to autoreplacing.
GroupID group_id
Index of group Pool array.
GroundVehicleCache gcache
Cache of often calculated values.
CargoID GetRefitCargo() const
Get the cargo to to refit to.
uint8 max_train_length
maximum length for trains