|
@@ -364,19 +364,19 @@ static CommandCost ReplaceChain(Vehicle
|
|
|
|
|
|
new_costs[i] = ret.GetCost();
|
|
|
if (new_vehs[i] != NULL) *nothing_to_do = false;
|
|
|
}
|
|
|
Vehicle *new_head = (new_vehs[0] != NULL ? new_vehs[0] : old_vehs[0]);
|
|
|
|
|
|
/* Note: When autoreplace has already failed here, old_vehs[] is not completely initialized. But it is also not needed. */
|
|
|
if (cost.Succeeded()) {
|
|
|
/* Separate the head, so we can start constructing the new chain */
|
|
|
if (cost.Succeeded()) {
|
|
|
Vehicle *second = GetNextUnit(old_head);
|
|
|
if (second != NULL) cost.AddCost(MoveVehicle(second, NULL, DC_EXEC | DC_AUTOREPLACE, true));
|
|
|
|
|
|
assert(GetNextUnit(new_head) == NULL);
|
|
|
}
|
|
|
|
|
|
/* Append engines to the new chain
|
|
|
* We do this from back to front, so that the head of the temporary vehicle chain does not change all the time.
|
|
|
* OTOH the vehicle attach callback is more expensive this way :s */
|
|
|
Vehicle *last_engine = NULL; ///< Shall store the last engine unit after this step
|
|
|
if (cost.Succeeded()) {
|
|
@@ -465,29 +465,32 @@ static CommandCost ReplaceChain(Vehicle
|
|
|
old_vehs[i] = NULL;
|
|
|
if (i == 0) old_head = NULL;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/* If we are not in DC_EXEC undo everything */
|
|
|
/* If we are not in DC_EXEC undo everything, i.e. rearrange old vehicles.
|
|
|
* We do this from back to front, so that the head of the temporary vehicle chain does not change all the time.
|
|
|
* Note: The vehicle attach callback is disabled here :) */
|
|
|
if ((flags & DC_EXEC) == 0) {
|
|
|
/* Separate the head, so we can reattach the old vehicles */
|
|
|
Vehicle *second = GetNextUnit(old_head);
|
|
|
if (second != NULL) MoveVehicle(second, NULL, DC_EXEC | DC_AUTOREPLACE, true);
|
|
|
|
|
|
assert(GetNextUnit(old_head) == NULL);
|
|
|
|
|
|
/* Rearrange old vehicles and sell new
|
|
|
* We do this from back to front, so that the head of the temporary vehicle chain does not change all the time.
|
|
|
* Note: The vehicle attach callback is disabled here :) */
|
|
|
|
|
|
for (int i = num_units - 1; i >= 0; i--) {
|
|
|
if (i > 0) {
|
|
|
for (int i = num_units - 1; i > 0; i--) {
|
|
|
CommandCost ret = MoveVehicle(old_vehs[i], old_head, DC_EXEC | DC_AUTOREPLACE, false);
|
|
|
assert(ret.Succeeded());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/* Finally undo buying of new vehicles */
|
|
|
if ((flags & DC_EXEC) == 0) {
|
|
|
for (int i = num_units - 1; i >= 0; i--) {
|
|
|
if (new_vehs[i] != NULL) {
|
|
|
DoCommand(0, new_vehs[i]->index, 0, DC_EXEC, GetCmdSellVeh(new_vehs[i]));
|
|
|
new_vehs[i] = NULL;
|
|
|
}
|
|
|
}
|
|
|
}
|