2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file autoreplace_cmd.cpp Deals with autoreplace execution but not the setup */
11 #include "company_func.h"
13 #include "command_func.h"
14 #include "engine_func.h"
15 #include "vehicle_func.h"
16 #include "autoreplace_func.h"
17 #include "autoreplace_gui.h"
18 #include "articulated_vehicles.h"
19 #include "core/bitmath_func.hpp"
20 #include "core/random_func.hpp"
21 #include "vehiclelist.h"
24 #include "news_func.h"
25 #include "strings_func.h"
26 #include "autoreplace_cmd.h"
27 #include "group_cmd.h"
28 #include "order_cmd.h"
29 #include "train_cmd.h"
30 #include "vehicle_cmd.h"
32 #include "table/strings.h"
34 #include "safeguards.h"
36 extern void ChangeVehicleViewports(VehicleID from_index
, VehicleID to_index
);
37 extern void ChangeVehicleNews(VehicleID from_index
, VehicleID to_index
);
38 extern void ChangeVehicleViewWindow(VehicleID from_index
, VehicleID to_index
);
41 * Figure out if two engines got at least one type of cargo in common (refitting if needed)
42 * @param engine_a one of the EngineIDs
43 * @param engine_b the other EngineID
44 * @return true if they can both carry the same type of cargo (or at least one of them got no capacity at all)
46 static bool EnginesHaveCargoInCommon(EngineID engine_a
, EngineID engine_b
)
48 CargoTypes available_cargoes_a
= GetUnionOfArticulatedRefitMasks(engine_a
, true);
49 CargoTypes available_cargoes_b
= GetUnionOfArticulatedRefitMasks(engine_b
, true);
50 return (available_cargoes_a
== 0 || available_cargoes_b
== 0 || (available_cargoes_a
& available_cargoes_b
) != 0);
54 * Checks some basic properties whether autoreplace is allowed
55 * @param from Origin engine
56 * @param to Destination engine
57 * @param company Company to check for
58 * @return true if autoreplace is allowed
60 bool CheckAutoreplaceValidity(EngineID from
, EngineID to
, CompanyID company
)
62 assert(Engine::IsValidID(from
) && Engine::IsValidID(to
));
64 const Engine
*e_from
= Engine::Get(from
);
65 const Engine
*e_to
= Engine::Get(to
);
66 VehicleType type
= e_from
->type
;
68 /* check that the new vehicle type is available to the company and its type is the same as the original one */
69 if (!IsEngineBuildable(to
, type
, company
)) return false;
73 /* make sure the railtypes are compatible */
74 if ((GetRailTypeInfo(e_from
->u
.rail
.railtype
)->compatible_railtypes
& GetRailTypeInfo(e_to
->u
.rail
.railtype
)->compatible_railtypes
) == 0) return false;
76 /* make sure we do not replace wagons with engines or vice versa */
77 if ((e_from
->u
.rail
.railveh_type
== RAILVEH_WAGON
) != (e_to
->u
.rail
.railveh_type
== RAILVEH_WAGON
)) return false;
82 /* make sure the roadtypes are compatible */
83 if ((GetRoadTypeInfo(e_from
->u
.road
.roadtype
)->powered_roadtypes
& GetRoadTypeInfo(e_to
->u
.road
.roadtype
)->powered_roadtypes
) == ROADTYPES_NONE
) return false;
85 /* make sure that we do not replace a tram with a normal road vehicles or vice versa */
86 if (HasBit(e_from
->info
.misc_flags
, EF_ROAD_TRAM
) != HasBit(e_to
->info
.misc_flags
, EF_ROAD_TRAM
)) return false;
90 /* make sure that we do not replace a plane with a helicopter or vice versa */
91 if ((e_from
->u
.air
.subtype
& AIR_CTOL
) != (e_to
->u
.air
.subtype
& AIR_CTOL
)) return false;
97 /* the engines needs to be able to carry the same cargo */
98 return EnginesHaveCargoInCommon(from
, to
);
102 * Check the capacity of all vehicles in a chain and spread cargo if needed.
103 * @param v The vehicle to check.
104 * @pre You can only do this if the consist is not loading or unloading. It
105 * must not carry reserved cargo, nor cargo to be unloaded or transferred.
107 void CheckCargoCapacity(Vehicle
*v
)
109 assert(v
== nullptr || v
->First() == v
);
111 for (Vehicle
*src
= v
; src
!= nullptr; src
= src
->Next()) {
112 assert(src
->cargo
.TotalCount() == src
->cargo
.ActionCount(VehicleCargoList::MTA_KEEP
));
114 /* Do we need to more cargo away? */
115 if (src
->cargo
.TotalCount() <= src
->cargo_cap
) continue;
117 /* We need to move a particular amount. Try that on the other vehicles. */
118 uint to_spread
= src
->cargo
.TotalCount() - src
->cargo_cap
;
119 for (Vehicle
*dest
= v
; dest
!= nullptr && to_spread
!= 0; dest
= dest
->Next()) {
120 assert(dest
->cargo
.TotalCount() == dest
->cargo
.ActionCount(VehicleCargoList::MTA_KEEP
));
121 if (dest
->cargo
.TotalCount() >= dest
->cargo_cap
|| dest
->cargo_type
!= src
->cargo_type
) continue;
123 uint amount
= std::min(to_spread
, dest
->cargo_cap
- dest
->cargo
.TotalCount());
124 src
->cargo
.Shift(amount
, &dest
->cargo
);
128 /* Any left-overs will be thrown away, but not their feeder share. */
129 if (src
->cargo_cap
< src
->cargo
.TotalCount()) src
->cargo
.Truncate(src
->cargo
.TotalCount() - src
->cargo_cap
);
134 * Transfer cargo from a single (articulated )old vehicle to the new vehicle chain
135 * @param old_veh Old vehicle that will be sold
136 * @param new_head Head of the completely constructed new vehicle chain
137 * @param part_of_chain The vehicle is part of a train
138 * @pre You can only do this if both consists are not loading or unloading.
139 * They must not carry reserved cargo, nor cargo to be unloaded or
142 static void TransferCargo(Vehicle
*old_veh
, Vehicle
*new_head
, bool part_of_chain
)
144 assert(!part_of_chain
|| new_head
->IsPrimaryVehicle());
145 /* Loop through source parts */
146 for (Vehicle
*src
= old_veh
; src
!= nullptr; src
= src
->Next()) {
147 assert(src
->cargo
.TotalCount() == src
->cargo
.ActionCount(VehicleCargoList::MTA_KEEP
));
148 if (!part_of_chain
&& src
->type
== VEH_TRAIN
&& src
!= old_veh
&& src
!= Train::From(old_veh
)->other_multiheaded_part
&& !src
->IsArticulatedPart()) {
149 /* Skip vehicles, which do not belong to old_veh */
150 src
= src
->GetLastEnginePart();
153 if (src
->cargo_type
>= NUM_CARGO
|| src
->cargo
.TotalCount() == 0) continue;
155 /* Find free space in the new chain */
156 for (Vehicle
*dest
= new_head
; dest
!= nullptr && src
->cargo
.TotalCount() > 0; dest
= dest
->Next()) {
157 assert(dest
->cargo
.TotalCount() == dest
->cargo
.ActionCount(VehicleCargoList::MTA_KEEP
));
158 if (!part_of_chain
&& dest
->type
== VEH_TRAIN
&& dest
!= new_head
&& dest
!= Train::From(new_head
)->other_multiheaded_part
&& !dest
->IsArticulatedPart()) {
159 /* Skip vehicles, which do not belong to new_head */
160 dest
= dest
->GetLastEnginePart();
163 if (dest
->cargo_type
!= src
->cargo_type
) continue;
165 uint amount
= std::min(src
->cargo
.TotalCount(), dest
->cargo_cap
- dest
->cargo
.TotalCount());
166 if (amount
<= 0) continue;
168 src
->cargo
.Shift(amount
, &dest
->cargo
);
172 /* Update train weight etc., the old vehicle will be sold anyway */
173 if (part_of_chain
&& new_head
->type
== VEH_TRAIN
) Train::From(new_head
)->ConsistChanged(CCF_LOADUNLOAD
);
177 * Tests whether refit orders that applied to v will also apply to the new vehicle type
178 * @param v The vehicle to be replaced
179 * @param engine_type The type we want to replace with
180 * @return true iff all refit orders stay valid
182 static bool VerifyAutoreplaceRefitForOrders(const Vehicle
*v
, EngineID engine_type
)
184 CargoTypes union_refit_mask_a
= GetUnionOfArticulatedRefitMasks(v
->engine_type
, false);
185 CargoTypes union_refit_mask_b
= GetUnionOfArticulatedRefitMasks(engine_type
, false);
187 const Vehicle
*u
= (v
->type
== VEH_TRAIN
) ? v
->First() : v
;
188 for (const Order
*o
: u
->Orders()) {
189 if (!o
->IsRefit() || o
->IsAutoRefit()) continue;
190 CargoID cargo_type
= o
->GetRefitCargo();
192 if (!HasBit(union_refit_mask_a
, cargo_type
)) continue;
193 if (!HasBit(union_refit_mask_b
, cargo_type
)) return false;
200 * Gets the index of the first refit order that is incompatible with the requested engine type
201 * @param v The vehicle to be replaced
202 * @param engine_type The type we want to replace with
203 * @return index of the incompatible order or -1 if none were found
205 static int GetIncompatibleRefitOrderIdForAutoreplace(const Vehicle
*v
, EngineID engine_type
)
207 CargoTypes union_refit_mask
= GetUnionOfArticulatedRefitMasks(engine_type
, false);
210 const Vehicle
*u
= (v
->type
== VEH_TRAIN
) ? v
->First() : v
;
212 const OrderList
*orders
= u
->orders
;
213 if (orders
== nullptr) return -1;
214 for (VehicleOrderID i
= 0; i
< orders
->GetNumOrders(); i
++) {
215 o
= orders
->GetOrderAt(i
);
216 if (!o
->IsRefit()) continue;
217 if (!HasBit(union_refit_mask
, o
->GetRefitCargo())) return i
;
224 * Function to find what type of cargo to refit to when autoreplacing
225 * @param *v Original vehicle that is being replaced.
226 * @param engine_type The EngineID of the vehicle that is being replaced to
227 * @param part_of_chain The vehicle is part of a train
228 * @return The cargo type to replace to
229 * CARGO_NO_REFIT is returned if no refit is needed
230 * INVALID_CARGO is returned when both old and new vehicle got cargo capacity and refitting the new one to the old one's cargo type isn't possible
232 static CargoID
GetNewCargoTypeForReplace(Vehicle
*v
, EngineID engine_type
, bool part_of_chain
)
234 CargoTypes available_cargo_types
, union_mask
;
235 GetArticulatedRefitMasks(engine_type
, true, &union_mask
, &available_cargo_types
);
237 if (union_mask
== 0) return CARGO_NO_REFIT
; // Don't try to refit an engine with no cargo capacity
240 CargoTypes cargo_mask
= GetCargoTypesOfArticulatedVehicle(v
, &cargo_type
);
241 if (!HasAtMostOneBit(cargo_mask
)) {
242 CargoTypes new_engine_default_cargoes
= GetCargoTypesOfArticulatedParts(engine_type
);
243 if ((cargo_mask
& new_engine_default_cargoes
) == cargo_mask
) {
244 return CARGO_NO_REFIT
; // engine_type is already a mixed cargo type which matches the incoming vehicle by default, no refit required
247 return INVALID_CARGO
; // We cannot refit to mixed cargoes in an automated way
250 if (!IsValidCargoID(cargo_type
)) {
251 if (v
->type
!= VEH_TRAIN
) return CARGO_NO_REFIT
; // If the vehicle does not carry anything at all, every replacement is fine.
253 if (!part_of_chain
) return CARGO_NO_REFIT
;
255 /* the old engine didn't have cargo capacity, but the new one does
256 * now we will figure out what cargo the train is carrying and refit to fit this */
258 for (v
= v
->First(); v
!= nullptr; v
= v
->Next()) {
259 if (!v
->GetEngine()->CanCarryCargo()) continue;
260 /* Now we found a cargo type being carried on the train and we will see if it is possible to carry to this one */
261 if (HasBit(available_cargo_types
, v
->cargo_type
)) return v
->cargo_type
;
264 return CARGO_NO_REFIT
; // We failed to find a cargo type on the old vehicle and we will not refit the new one
266 if (!HasBit(available_cargo_types
, cargo_type
)) return INVALID_CARGO
; // We can't refit the vehicle to carry the cargo we want
268 if (part_of_chain
&& !VerifyAutoreplaceRefitForOrders(v
, engine_type
)) return INVALID_CARGO
; // Some refit orders lose their effect
275 * Get the EngineID of the replacement for a vehicle
276 * @param v The vehicle to find a replacement for
277 * @param c The vehicle's owner (it's faster to forward the pointer than refinding it)
278 * @param always_replace Always replace, even if not old.
279 * @param[out] e the EngineID of the replacement. INVALID_ENGINE if no replacement is found
280 * @return Error if the engine to build is not available
282 static CommandCost
GetNewEngineType(const Vehicle
*v
, const Company
*c
, bool always_replace
, EngineID
&e
)
284 assert(v
->type
!= VEH_TRAIN
|| !v
->IsArticulatedPart());
288 if (v
->type
== VEH_TRAIN
&& Train::From(v
)->IsRearDualheaded()) {
289 /* we build the rear ends of multiheaded trains with the front ones */
290 return CommandCost();
293 bool replace_when_old
;
294 e
= EngineReplacementForCompany(c
, v
->engine_type
, v
->group_id
, &replace_when_old
);
295 if (!always_replace
&& replace_when_old
&& !v
->NeedsAutorenewing(c
, false)) e
= INVALID_ENGINE
;
297 /* Autoreplace, if engine is available */
298 if (e
!= INVALID_ENGINE
&& IsEngineBuildable(e
, v
->type
, _current_company
)) {
299 return CommandCost();
302 /* Autorenew if needed */
303 if (v
->NeedsAutorenewing(c
)) e
= v
->engine_type
;
305 /* Nothing to do or all is fine? */
306 if (e
== INVALID_ENGINE
|| IsEngineBuildable(e
, v
->type
, _current_company
)) return CommandCost();
308 /* The engine we need is not available. Report error to user */
309 return CommandCost(STR_ERROR_RAIL_VEHICLE_NOT_AVAILABLE
+ v
->type
);
313 * Builds and refits a replacement vehicle
314 * 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)
315 * @param old_veh A single (articulated/multiheaded) vehicle that shall be replaced.
316 * @param new_vehicle Returns the newly build and refitted vehicle
317 * @param part_of_chain The vehicle is part of a train
318 * @param flags The calling command flags.
319 * @return cost or error
321 static CommandCost
BuildReplacementVehicle(Vehicle
*old_veh
, Vehicle
**new_vehicle
, bool part_of_chain
, DoCommandFlag flags
)
323 *new_vehicle
= nullptr;
325 /* Shall the vehicle be replaced? */
326 const Company
*c
= Company::Get(_current_company
);
328 CommandCost cost
= GetNewEngineType(old_veh
, c
, true, e
);
329 if (cost
.Failed()) return cost
;
330 if (e
== INVALID_ENGINE
) return CommandCost(); // neither autoreplace is set, nor autorenew is triggered
332 /* Does it need to be refitted */
333 CargoID refit_cargo
= GetNewCargoTypeForReplace(old_veh
, e
, part_of_chain
);
334 if (!IsValidCargoID(refit_cargo
)) {
335 if (!IsLocalCompany() || (flags
& DC_EXEC
) == 0) return CommandCost();
337 VehicleID old_veh_id
= (old_veh
->type
== VEH_TRAIN
) ? Train::From(old_veh
)->First()->index
: old_veh
->index
;
338 SetDParam(0, old_veh_id
);
340 int order_id
= GetIncompatibleRefitOrderIdForAutoreplace(old_veh
, e
);
341 if (order_id
!= -1) {
342 /* Orders contained a refit order that is incompatible with the new vehicle. */
343 SetDParam(1, STR_ERROR_AUTOREPLACE_INCOMPATIBLE_REFIT
);
344 SetDParam(2, order_id
+ 1); // 1-based indexing for display
346 /* Current cargo is incompatible with the new vehicle. */
347 SetDParam(1, STR_ERROR_AUTOREPLACE_INCOMPATIBLE_CARGO
);
348 SetDParam(2, CargoSpec::Get(old_veh
->cargo_type
)->name
);
351 AddVehicleAdviceNewsItem(STR_NEWS_VEHICLE_AUTORENEW_FAILED
, old_veh_id
);
352 return CommandCost();
355 /* Build the new vehicle */
356 VehicleID new_veh_id
;
357 std::tie(cost
, new_veh_id
, std::ignore
, std::ignore
, std::ignore
) = Command
<CMD_BUILD_VEHICLE
>::Do(DC_EXEC
| DC_AUTOREPLACE
, old_veh
->tile
, e
, true, INVALID_CARGO
, INVALID_CLIENT_ID
);
358 if (cost
.Failed()) return cost
;
360 Vehicle
*new_veh
= Vehicle::Get(new_veh_id
);
361 *new_vehicle
= new_veh
;
363 /* Refit the vehicle if needed */
364 if (refit_cargo
!= CARGO_NO_REFIT
) {
365 uint8_t subtype
= GetBestFittingSubType(old_veh
, new_veh
, refit_cargo
);
367 cost
.AddCost(std::get
<0>(Command
<CMD_REFIT_VEHICLE
>::Do(DC_EXEC
, new_veh
->index
, refit_cargo
, subtype
, false, false, 0)));
368 assert(cost
.Succeeded()); // This should be ensured by GetNewCargoTypeForReplace()
371 /* Try to reverse the vehicle, but do not care if it fails as the new type might not be reversible */
372 if (new_veh
->type
== VEH_TRAIN
&& HasBit(Train::From(old_veh
)->flags
, VRF_REVERSE_DIRECTION
)) {
373 Command
<CMD_REVERSE_TRAIN_DIRECTION
>::Do(DC_EXEC
, new_veh
->index
, true);
380 * Issue a start/stop command
382 * @param evaluate_callback shall the start/stop callback be evaluated?
383 * @return success or error
385 static inline CommandCost
DoCmdStartStopVehicle(const Vehicle
*v
, bool evaluate_callback
)
387 return Command
<CMD_START_STOP_VEHICLE
>::Do(DC_EXEC
| DC_AUTOREPLACE
, v
->index
, evaluate_callback
);
391 * Issue a train vehicle move command
392 * @param v The vehicle to move
393 * @param after The vehicle to insert 'v' after, or nullptr to start new chain
394 * @param flags the command flags to use
395 * @param whole_chain move all vehicles following 'v' (true), or only 'v' (false)
396 * @return success or error
398 static inline CommandCost
CmdMoveVehicle(const Vehicle
*v
, const Vehicle
*after
, DoCommandFlag flags
, bool whole_chain
)
400 return Command
<CMD_MOVE_RAIL_VEHICLE
>::Do(flags
| DC_NO_CARGO_CAP_CHECK
, v
->index
, after
!= nullptr ? after
->index
: INVALID_VEHICLE
, whole_chain
);
404 * Copy head specific things to the new vehicle chain after it was successfully constructed
405 * @param old_head The old front vehicle (no wagons attached anymore)
406 * @param new_head The new head of the completely replaced vehicle chain
407 * @param flags the command flags to use
409 static CommandCost
CopyHeadSpecificThings(Vehicle
*old_head
, Vehicle
*new_head
, DoCommandFlag flags
)
411 CommandCost cost
= CommandCost();
414 if (cost
.Succeeded() && old_head
!= new_head
) cost
.AddCost(Command
<CMD_CLONE_ORDER
>::Do(DC_EXEC
, CO_SHARE
, new_head
->index
, old_head
->index
));
416 /* Copy group membership */
417 if (cost
.Succeeded() && old_head
!= new_head
) cost
.AddCost(std::get
<0>(Command
<CMD_ADD_VEHICLE_GROUP
>::Do(DC_EXEC
, old_head
->group_id
, new_head
->index
, false, VehicleListIdentifier
{})));
419 /* Perform start/stop check whether the new vehicle suits newgrf restrictions etc. */
420 if (cost
.Succeeded()) {
421 /* Start the vehicle, might be denied by certain things */
422 assert((new_head
->vehstatus
& VS_STOPPED
) != 0);
423 cost
.AddCost(DoCmdStartStopVehicle(new_head
, true));
425 /* Stop the vehicle again, but do not care about evil newgrfs allowing starting but not stopping :p */
426 if (cost
.Succeeded()) cost
.AddCost(DoCmdStartStopVehicle(new_head
, false));
429 /* Last do those things which do never fail (resp. we do not care about), but which are not undo-able */
430 if (cost
.Succeeded() && old_head
!= new_head
&& (flags
& DC_EXEC
) != 0) {
431 /* Copy other things which cannot be copied by a command and which shall not stay resetted from the build vehicle command */
432 new_head
->CopyVehicleConfigAndStatistics(old_head
);
433 GroupStatistics::AddProfitLastYear(new_head
);
435 /* Switch vehicle windows/news to the new vehicle, so they are not closed/deleted when the old vehicle is sold */
436 ChangeVehicleViewports(old_head
->index
, new_head
->index
);
437 ChangeVehicleViewWindow(old_head
->index
, new_head
->index
);
438 ChangeVehicleNews(old_head
->index
, new_head
->index
);
445 * Replace a single unit in a free wagon chain
446 * @param single_unit vehicle to let autoreplace/renew operator on
447 * @param flags command flags
448 * @param nothing_to_do is set to 'false' when something was done (only valid when not failed)
449 * @return cost or error
451 static CommandCost
ReplaceFreeUnit(Vehicle
**single_unit
, DoCommandFlag flags
, bool *nothing_to_do
)
453 Train
*old_v
= Train::From(*single_unit
);
454 assert(!old_v
->IsArticulatedPart() && !old_v
->IsRearDualheaded());
456 CommandCost cost
= CommandCost(EXPENSES_NEW_VEHICLES
, (Money
)0);
458 /* Build and refit replacement vehicle */
459 Vehicle
*new_v
= nullptr;
460 cost
.AddCost(BuildReplacementVehicle(old_v
, &new_v
, false, flags
));
462 /* Was a new vehicle constructed? */
463 if (cost
.Succeeded() && new_v
!= nullptr) {
464 *nothing_to_do
= false;
466 if ((flags
& DC_EXEC
) != 0) {
467 /* Move the new vehicle behind the old */
468 CmdMoveVehicle(new_v
, old_v
, DC_EXEC
, false);
471 * Note: We do only transfer cargo from the old to the new vehicle.
472 * I.e. we do not transfer remaining cargo to other vehicles.
473 * Else you would also need to consider moving cargo to other free chains,
474 * or doing the same in ReplaceChain(), which would be quite troublesome.
476 TransferCargo(old_v
, new_v
, false);
478 *single_unit
= new_v
;
480 AI::NewEvent(old_v
->owner
, new ScriptEventVehicleAutoReplaced(old_v
->index
, new_v
->index
));
483 /* Sell the old vehicle */
484 cost
.AddCost(Command
<CMD_SELL_VEHICLE
>::Do(flags
, old_v
->index
, false, false, INVALID_CLIENT_ID
));
486 /* If we are not in DC_EXEC undo everything */
487 if ((flags
& DC_EXEC
) == 0) {
488 Command
<CMD_SELL_VEHICLE
>::Do(DC_EXEC
, new_v
->index
, false, false, INVALID_CLIENT_ID
);
495 /** Struct for recording vehicle chain replacement information. */
496 struct ReplaceChainItem
{
497 Vehicle
*old_veh
; ///< Old vehicle to replace.
498 Vehicle
*new_veh
; ///< Replacement vehicle, or nullptr if no replacement.
499 Money cost
; /// Cost of buying and refitting replacement.
501 ReplaceChainItem(Vehicle
*old_veh
, Vehicle
*new_veh
, Money cost
) : old_veh(old_veh
), new_veh(new_veh
), cost(cost
) { }
504 * Get vehicle to use for this position.
505 * @return Either the new vehicle, or the old vehicle if there is no replacement.
507 Vehicle
*GetVehicle() const { return new_veh
== nullptr ? old_veh
: new_veh
; }
511 * Replace a whole vehicle chain
512 * @param chain vehicle chain to let autoreplace/renew operator on
513 * @param flags command flags
514 * @param wagon_removal remove wagons when the resulting chain occupies more tiles than the old did
515 * @param nothing_to_do is set to 'false' when something was done (only valid when not failed)
516 * @return cost or error
518 static CommandCost
ReplaceChain(Vehicle
**chain
, DoCommandFlag flags
, bool wagon_removal
, bool *nothing_to_do
)
520 Vehicle
*old_head
= *chain
;
521 assert(old_head
->IsPrimaryVehicle());
523 CommandCost cost
= CommandCost(EXPENSES_NEW_VEHICLES
, (Money
)0);
525 if (old_head
->type
== VEH_TRAIN
) {
526 /* Store the length of the old vehicle chain, rounded up to whole tiles */
527 uint16_t old_total_length
= CeilDiv(Train::From(old_head
)->gcache
.cached_total_length
, TILE_SIZE
) * TILE_SIZE
;
529 std::vector
<ReplaceChainItem
> replacements
;
531 /* Collect vehicles and build replacements
532 * Note: The replacement vehicles can only successfully build as long as the old vehicles are still in their chain */
533 for (Train
*w
= Train::From(old_head
); w
!= nullptr; w
= w
->GetNextUnit()) {
534 ReplaceChainItem
&replacement
= replacements
.emplace_back(w
, nullptr, 0);
536 CommandCost ret
= BuildReplacementVehicle(replacement
.old_veh
, &replacement
.new_veh
, true, flags
);
538 if (cost
.Failed()) break;
540 replacement
.cost
= ret
.GetCost();
541 if (replacement
.new_veh
!= nullptr) *nothing_to_do
= false;
543 Vehicle
*new_head
= replacements
.front().GetVehicle();
545 /* Note: When autoreplace has already failed here, old_vehs[] is not completely initialized. But it is also not needed. */
546 if (cost
.Succeeded()) {
547 /* Separate the head, so we can start constructing the new chain */
548 Train
*second
= Train::From(old_head
)->GetNextUnit();
549 if (second
!= nullptr) cost
.AddCost(CmdMoveVehicle(second
, nullptr, DC_EXEC
| DC_AUTOREPLACE
, true));
551 assert(Train::From(new_head
)->GetNextUnit() == nullptr);
553 /* Append engines to the new chain
554 * We do this from back to front, so that the head of the temporary vehicle chain does not change all the time.
555 * That way we also have less trouble when exceeding the unitnumber limit.
556 * OTOH the vehicle attach callback is more expensive this way :s */
557 Vehicle
*last_engine
= nullptr; ///< Shall store the last engine unit after this step
558 if (cost
.Succeeded()) {
559 for (auto it
= std::rbegin(replacements
); it
!= std::rend(replacements
); ++it
) {
560 Vehicle
*append
= it
->GetVehicle();
562 if (RailVehInfo(append
->engine_type
)->railveh_type
== RAILVEH_WAGON
) continue;
564 if (it
->new_veh
!= nullptr) {
565 /* Move the old engine to a separate row with DC_AUTOREPLACE. Else
566 * moving the wagon in front may fail later due to unitnumber limit.
567 * (We have to attach wagons without DC_AUTOREPLACE.) */
568 CmdMoveVehicle(it
->old_veh
, nullptr, DC_EXEC
| DC_AUTOREPLACE
, false);
571 if (last_engine
== nullptr) last_engine
= append
;
572 cost
.AddCost(CmdMoveVehicle(append
, new_head
, DC_EXEC
, false));
573 if (cost
.Failed()) break;
575 if (last_engine
== nullptr) last_engine
= new_head
;
578 /* When wagon removal is enabled and the new engines without any wagons are already longer than the old, we have to fail */
579 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
);
581 /* Append/insert wagons into the new vehicle chain
582 * We do this from back to front, so we can stop when wagon removal or maximum train length (i.e. from mammoth-train setting) is triggered.
584 if (cost
.Succeeded()) {
585 for (auto it
= std::rbegin(replacements
); it
!= std::rend(replacements
); ++it
) {
586 assert(last_engine
!= nullptr);
587 Vehicle
*append
= it
->GetVehicle();
589 if (RailVehInfo(append
->engine_type
)->railveh_type
== RAILVEH_WAGON
) {
590 /* Insert wagon after 'last_engine' */
591 CommandCost res
= CmdMoveVehicle(append
, last_engine
, DC_EXEC
, false);
593 /* When we allow removal of wagons, either the move failing due
594 * to the train becoming too long, or the train becoming longer
595 * would move the vehicle to the empty vehicle chain. */
596 if (wagon_removal
&& (res
.Failed() ? res
.GetErrorMessage() == STR_ERROR_TRAIN_TOO_LONG
: Train::From(new_head
)->gcache
.cached_total_length
> old_total_length
)) {
597 CmdMoveVehicle(append
, nullptr, DC_EXEC
| DC_AUTOREPLACE
, false);
602 if (cost
.Failed()) break;
604 /* We have reached 'last_engine', continue with the next engine towards the front */
605 assert(append
== last_engine
);
606 last_engine
= Train::From(last_engine
)->GetPrevUnit();
611 /* Sell superfluous new vehicles that could not be inserted. */
612 if (cost
.Succeeded() && wagon_removal
) {
613 assert(Train::From(new_head
)->gcache
.cached_total_length
<= _settings_game
.vehicle
.max_train_length
* TILE_SIZE
);
614 for (auto it
= std::next(std::begin(replacements
)); it
!= std::end(replacements
); ++it
) {
615 Vehicle
*wagon
= it
->new_veh
;
616 if (wagon
== nullptr) continue;
617 if (wagon
->First() == new_head
) break;
619 assert(RailVehInfo(wagon
->engine_type
)->railveh_type
== RAILVEH_WAGON
);
622 [[maybe_unused
]] CommandCost ret
= Command
<CMD_SELL_VEHICLE
>::Do(DC_EXEC
, wagon
->index
, false, false, INVALID_CLIENT_ID
);
623 assert(ret
.Succeeded());
624 it
->new_veh
= nullptr;
626 /* Revert the money subtraction when the vehicle was built.
627 * This value is different from the sell value, esp. because of refitting */
628 cost
.AddCost(-it
->cost
);
632 /* The new vehicle chain is constructed, now take over orders and everything... */
633 if (cost
.Succeeded()) cost
.AddCost(CopyHeadSpecificThings(old_head
, new_head
, flags
));
635 if (cost
.Succeeded()) {
637 if ((flags
& DC_EXEC
) != 0 && new_head
!= old_head
) {
639 AI::NewEvent(old_head
->owner
, new ScriptEventVehicleAutoReplaced(old_head
->index
, new_head
->index
));
642 /* Transfer cargo of old vehicles and sell them */
643 for (auto it
= std::begin(replacements
); it
!= std::end(replacements
); ++it
) {
644 Vehicle
*w
= it
->old_veh
;
645 /* Is the vehicle again part of the new chain?
646 * Note: We cannot test 'new_vehs[i] != nullptr' as wagon removal might cause to remove both */
647 if (w
->First() == new_head
) continue;
649 if ((flags
& DC_EXEC
) != 0) TransferCargo(w
, new_head
, true);
652 * Note: This might temporarily construct new trains, so use DC_AUTOREPLACE to prevent
653 * it from failing due to engine limits. */
654 cost
.AddCost(Command
<CMD_SELL_VEHICLE
>::Do(flags
| DC_AUTOREPLACE
, w
->index
, false, false, INVALID_CLIENT_ID
));
655 if ((flags
& DC_EXEC
) != 0) {
656 it
->old_veh
= nullptr;
657 if (it
== std::begin(replacements
)) old_head
= nullptr;
661 if ((flags
& DC_EXEC
) != 0) CheckCargoCapacity(new_head
);
664 /* If we are not in DC_EXEC undo everything, i.e. rearrange old vehicles.
665 * We do this from back to front, so that the head of the temporary vehicle chain does not change all the time.
666 * Note: The vehicle attach callback is disabled here :) */
667 if ((flags
& DC_EXEC
) == 0) {
668 /* Separate the head, so we can reattach the old vehicles */
669 Train
*second
= Train::From(old_head
)->GetNextUnit();
670 if (second
!= nullptr) CmdMoveVehicle(second
, nullptr, DC_EXEC
| DC_AUTOREPLACE
, true);
672 assert(Train::From(old_head
)->GetNextUnit() == nullptr);
674 for (auto it
= std::rbegin(replacements
); it
!= std::rend(replacements
); ++it
) {
675 [[maybe_unused
]] CommandCost ret
= CmdMoveVehicle(it
->old_veh
, old_head
, DC_EXEC
| DC_AUTOREPLACE
, false);
676 assert(ret
.Succeeded());
681 /* Finally undo buying of new vehicles */
682 if ((flags
& DC_EXEC
) == 0) {
683 for (auto it
= std::rbegin(replacements
); it
!= std::rend(replacements
); ++it
) {
684 if (it
->new_veh
!= nullptr) {
685 Command
<CMD_SELL_VEHICLE
>::Do(DC_EXEC
, it
->new_veh
->index
, false, false, INVALID_CLIENT_ID
);
686 it
->new_veh
= nullptr;
691 /* Build and refit replacement vehicle */
692 Vehicle
*new_head
= nullptr;
693 cost
.AddCost(BuildReplacementVehicle(old_head
, &new_head
, true, flags
));
695 /* Was a new vehicle constructed? */
696 if (cost
.Succeeded() && new_head
!= nullptr) {
697 *nothing_to_do
= false;
699 /* The new vehicle is constructed, now take over orders and everything... */
700 cost
.AddCost(CopyHeadSpecificThings(old_head
, new_head
, flags
));
702 if (cost
.Succeeded()) {
703 /* The new vehicle is constructed, now take over cargo */
704 if ((flags
& DC_EXEC
) != 0) {
705 TransferCargo(old_head
, new_head
, true);
708 AI::NewEvent(old_head
->owner
, new ScriptEventVehicleAutoReplaced(old_head
->index
, new_head
->index
));
711 /* Sell the old vehicle */
712 cost
.AddCost(Command
<CMD_SELL_VEHICLE
>::Do(flags
, old_head
->index
, false, false, INVALID_CLIENT_ID
));
715 /* If we are not in DC_EXEC undo everything */
716 if ((flags
& DC_EXEC
) == 0) {
717 Command
<CMD_SELL_VEHICLE
>::Do(DC_EXEC
, new_head
->index
, false, false, INVALID_CLIENT_ID
);
726 * Autoreplaces a vehicle
727 * Trains are replaced as a whole chain, free wagons in depot are replaced on their own
728 * @param flags type of operation
729 * @param veh_id Index of vehicle
730 * @return the cost of this operation or an error
732 CommandCost
CmdAutoreplaceVehicle(DoCommandFlag flags
, VehicleID veh_id
)
734 Vehicle
*v
= Vehicle::GetIfValid(veh_id
);
735 if (v
== nullptr) return CMD_ERROR
;
737 CommandCost ret
= CheckOwnership(v
->owner
);
738 if (ret
.Failed()) return ret
;
740 if (v
->vehstatus
& VS_CRASHED
) return CMD_ERROR
;
742 bool free_wagon
= false;
743 if (v
->type
== VEH_TRAIN
) {
744 Train
*t
= Train::From(v
);
745 if (t
->IsArticulatedPart() || t
->IsRearDualheaded()) return CMD_ERROR
;
746 free_wagon
= !t
->IsFrontEngine();
747 if (free_wagon
&& t
->First()->IsFrontEngine()) return CMD_ERROR
;
749 if (!v
->IsPrimaryVehicle()) return CMD_ERROR
;
751 if (!v
->IsChainInDepot()) return CMD_ERROR
;
753 const Company
*c
= Company::Get(_current_company
);
754 bool wagon_removal
= c
->settings
.renew_keep_length
;
756 const Group
*g
= Group::GetIfValid(v
->group_id
);
757 if (g
!= nullptr) wagon_removal
= HasBit(g
->flags
, GroupFlags::GF_REPLACE_WAGON_REMOVAL
);
759 /* Test whether any replacement is set, before issuing a whole lot of commands that would end in nothing changed */
761 bool any_replacements
= false;
762 while (w
!= nullptr) {
764 CommandCost cost
= GetNewEngineType(w
, c
, false, e
);
765 if (cost
.Failed()) return cost
;
766 any_replacements
|= (e
!= INVALID_ENGINE
);
767 w
= (!free_wagon
&& w
->type
== VEH_TRAIN
? Train::From(w
)->GetNextUnit() : nullptr);
770 CommandCost cost
= CommandCost(EXPENSES_NEW_VEHICLES
, (Money
)0);
771 bool nothing_to_do
= true;
773 if (any_replacements
) {
774 bool was_stopped
= free_wagon
|| ((v
->vehstatus
& VS_STOPPED
) != 0);
776 /* Stop the vehicle */
777 if (!was_stopped
) cost
.AddCost(DoCmdStartStopVehicle(v
, true));
778 if (cost
.Failed()) return cost
;
780 assert(free_wagon
|| v
->IsStoppedInDepot());
782 /* We have to construct the new vehicle chain to test whether it is valid.
783 * Vehicle construction needs random bits, so we have to save the random seeds
784 * to prevent desyncs and to replay newgrf callbacks during DC_EXEC */
785 SavedRandomSeeds saved_seeds
;
786 SaveRandomSeeds(&saved_seeds
);
788 cost
.AddCost(ReplaceFreeUnit(&v
, flags
& ~DC_EXEC
, ¬hing_to_do
));
790 cost
.AddCost(ReplaceChain(&v
, flags
& ~DC_EXEC
, wagon_removal
, ¬hing_to_do
));
792 RestoreRandomSeeds(saved_seeds
);
794 if (cost
.Succeeded() && (flags
& DC_EXEC
) != 0) {
796 ret
= ReplaceFreeUnit(&v
, flags
, ¬hing_to_do
);
798 ret
= ReplaceChain(&v
, flags
, wagon_removal
, ¬hing_to_do
);
800 assert(ret
.Succeeded() && ret
.GetCost() == cost
.GetCost());
803 /* Restart the vehicle */
804 if (!was_stopped
) cost
.AddCost(DoCmdStartStopVehicle(v
, false));
807 if (cost
.Succeeded() && nothing_to_do
) cost
= CommandCost(STR_ERROR_AUTOREPLACE_NOTHING_TO_DO
);
812 * Change engine renewal parameters
813 * @param flags operation to perform
814 * @param id_g engine group
815 * @param old_engine_type old engine type
816 * @param new_engine_type new engine type
817 * @param when_old replace when engine gets old?
818 * @return the cost of this operation or an error
820 CommandCost
CmdSetAutoReplace(DoCommandFlag flags
, GroupID id_g
, EngineID old_engine_type
, EngineID new_engine_type
, bool when_old
)
822 Company
*c
= Company::GetIfValid(_current_company
);
823 if (c
== nullptr) return CMD_ERROR
;
827 if (Group::IsValidID(id_g
) ? Group::Get(id_g
)->owner
!= _current_company
: !IsAllGroupID(id_g
) && !IsDefaultGroupID(id_g
)) return CMD_ERROR
;
828 if (!Engine::IsValidID(old_engine_type
)) return CMD_ERROR
;
829 if (Group::IsValidID(id_g
) && Group::Get(id_g
)->vehicle_type
!= Engine::Get(old_engine_type
)->type
) return CMD_ERROR
;
831 if (new_engine_type
!= INVALID_ENGINE
) {
832 if (!Engine::IsValidID(new_engine_type
)) return CMD_ERROR
;
833 if (!CheckAutoreplaceValidity(old_engine_type
, new_engine_type
, _current_company
)) return CMD_ERROR
;
835 cost
= AddEngineReplacementForCompany(c
, old_engine_type
, new_engine_type
, id_g
, when_old
, flags
);
837 cost
= RemoveEngineReplacementForCompany(c
, old_engine_type
, id_g
, flags
);
840 if (flags
& DC_EXEC
) {
841 GroupStatistics::UpdateAutoreplace(_current_company
);
842 if (IsLocalCompany()) SetWindowDirty(WC_REPLACE_VEHICLE
, Engine::Get(old_engine_type
)->type
);
844 const VehicleType vt
= Engine::Get(old_engine_type
)->type
;
845 SetWindowDirty(GetWindowClassForVehicleType(vt
), VehicleListIdentifier(VL_GROUP_LIST
, vt
, _current_company
).Pack());
847 if ((flags
& DC_EXEC
) && IsLocalCompany()) InvalidateAutoreplaceWindow(old_engine_type
, id_g
);