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;
101 assert(v == NULL || v->
First() == v);
103 for (
Vehicle *src = v; src != NULL; src = src->
Next()) {
107 if (src->cargo.TotalCount() <= src->cargo_cap)
continue;
110 uint to_spread = src->cargo.TotalCount() - src->cargo_cap;
111 for (
Vehicle *dest = v; dest != NULL && to_spread != 0; dest = dest->
Next()) {
113 if (dest->cargo.TotalCount() >= dest->cargo_cap || dest->cargo_type != src->cargo_type)
continue;
115 uint amount =
min(to_spread, dest->cargo_cap - dest->cargo.TotalCount());
116 src->cargo.Shift(amount, &dest->cargo);
121 if (src->cargo_cap < src->cargo.TotalCount()) src->cargo.Truncate(src->cargo.TotalCount() - src->cargo_cap);
138 for (
Vehicle *src = old_veh; src != NULL; src = src->
Next()) {
142 src = src->GetLastEnginePart();
145 if (src->cargo_type >=
NUM_CARGO || src->cargo.TotalCount() == 0)
continue;
152 dest = dest->GetLastEnginePart();
155 if (dest->cargo_type != src->cargo_type)
continue;
157 uint amount =
min(src->cargo.TotalCount(), dest->cargo_cap - dest->cargo.TotalCount());
158 if (amount <= 0)
continue;
160 src->cargo.Shift(amount, &dest->cargo);
181 FOR_VEHICLE_ORDERS(u, o) {
185 if (!
HasBit(union_refit_mask_a, cargo_type))
continue;
186 if (!
HasBit(union_refit_mask_b, cargo_type))
return false;
203 CargoTypes available_cargo_types, union_mask;
219 for (v = v->
First(); v != NULL; v = v->
Next()) {
254 bool replace_when_old;
289 if (cost.
Failed())
return cost;
298 if (cost.
Failed())
return cost;
301 *new_vehicle = new_veh;
402 if (cost.Succeeded() && new_v != NULL) {
403 *nothing_to_do =
false;
417 *single_unit = new_v;
421 cost.AddCost(
DoCommand(0, old_v->
index, 0, flags, GetCmdSellVeh(old_v)));
424 if ((flags & DC_EXEC) == 0) {
454 Train **old_vehs = CallocT<Train *>(num_units);
455 Train **new_vehs = CallocT<Train *>(num_units);
456 Money *new_costs = MallocT<Money>(num_units);
463 assert(i < num_units);
468 if (cost.Failed())
break;
471 if (new_vehs[i] != NULL) *nothing_to_do =
false;
473 Train *new_head = (new_vehs[0] != NULL ? new_vehs[0] : old_vehs[0]);
476 if (cost.Succeeded()) {
481 assert(
Train::From(new_head)->GetNextUnit() == NULL);
487 Train *last_engine = NULL;
488 if (cost.Succeeded()) {
489 for (
int i = num_units - 1; i > 0; i--) {
490 Train *append = (new_vehs[i] != NULL ? new_vehs[i] : old_vehs[i]);
494 if (new_vehs[i] != NULL) {
501 if (last_engine == NULL) last_engine = append;
503 if (cost.Failed())
break;
505 if (last_engine == NULL) last_engine = new_head;
514 if (cost.Succeeded()) {
515 for (
int i = num_units - 1; i > 0; i--) {
516 assert(last_engine != NULL);
517 Vehicle *append = (new_vehs[i] != NULL ? new_vehs[i] : old_vehs[i]);
532 if (cost.Failed())
break;
535 assert(append == last_engine);
542 if (cost.Succeeded() && wagon_removal) {
544 for (
int i = 1; i < num_units; i++) {
546 if (wagon == NULL)
continue;
547 if (wagon->
First() == new_head)
break;
558 cost.AddCost(-new_costs[i]);
565 if (cost.Succeeded()) {
567 if ((flags &
DC_EXEC) != 0 && new_head != old_head) {
572 for (
int i = 0; i < num_units; i++) {
576 if (w->
First() == new_head)
continue;
578 if ((flags & DC_EXEC) != 0)
TransferCargo(w, new_head,
true);
584 if ((flags & DC_EXEC) != 0) {
586 if (i == 0) old_head = NULL;
601 assert(
Train::From(old_head)->GetNextUnit() == NULL);
603 for (
int i = num_units - 1; i > 0; i--) {
612 for (
int i = num_units - 1; i >= 0; i--) {
613 if (new_vehs[i] != NULL) {
614 DoCommand(0, new_vehs[i]->index, 0, DC_EXEC, GetCmdSellVeh(new_vehs[i]));
629 if (cost.Succeeded() && new_head != NULL) {
630 *nothing_to_do =
false;
635 if (cost.Succeeded()) {
643 cost.AddCost(
DoCommand(0, old_head->
index, 0, flags, GetCmdSellVeh(old_head)));
648 DoCommand(0, new_head->
index, 0, DC_EXEC, GetCmdSellVeh(new_head));
672 if (ret.
Failed())
return ret;
677 bool free_wagon =
false;
692 bool any_replacements =
false;
696 if (cost.
Failed())
return cost;
702 bool nothing_to_do =
true;
704 if (any_replacements) {
709 if (cost.
Failed())
return cost;
730 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.
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 EngineID EngineReplacementForCompany(const Company *c, EngineID engine, GroupID group, bool *replace_when_old=NULL)
Retrieve the engine replacement for the given company and original engine type.
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.
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...
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 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.
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.
VehicleType
Available vehicle types.
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.
bool IsRearDualheaded() const
Tell if we are dealing with the rear end of a multiheaded engine.
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.
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.
CompanyByte _current_company
Company currently doing an action.
uint32 TileIndex
The index/ID of a Tile.
Vehicle * Next() const
Get the next vehicle of this vehicle.
OwnerByte owner
Which company owns the 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).
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.
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-NULL) 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.
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.
Owner
Enum for all companies/owners.
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.
VehicleTypeByte type
Type of vehicle.
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