|
@@ -49,22 +49,22 @@ static void MoveVehicleCargo(Vehicle *de
|
|
|
/*
|
|
|
* The of the train will be incorrect at this moment. This is due
|
|
|
* to the fact that removing the old wagon updates the weight of
|
|
|
* the complete train, which is without the weight of cargo we just
|
|
|
* moved back into some (of the) new wagon(s).
|
|
|
*/
|
|
|
if (dest->type == VEH_TRAIN) TrainConsistChanged(dest->first);
|
|
|
if (dest->type == VEH_TRAIN) TrainConsistChanged(dest->First());
|
|
|
}
|
|
|
|
|
|
static bool VerifyAutoreplaceRefitForOrders(const Vehicle *v, const EngineID engine_type)
|
|
|
{
|
|
|
const Order *o;
|
|
|
const Vehicle *u;
|
|
|
|
|
|
if (v->type == VEH_TRAIN) {
|
|
|
u = GetFirstVehicleInChain(v);
|
|
|
u = v->First();
|
|
|
} else {
|
|
|
u = v;
|
|
|
}
|
|
|
|
|
|
FOR_VEHICLE_ORDERS(u, o) {
|
|
|
if (!(o->refit_cargo < NUM_CARGO)) continue;
|
|
@@ -101,13 +101,13 @@ static CargoID GetNewCargoTypeForReplace
|
|
|
/* Below this line it's safe to assume that the vehicle in question is a train */
|
|
|
|
|
|
if (v->cargo_cap != 0) return CT_INVALID; // trying to replace a vehicle with cargo capacity into another one with incompatible cargo type
|
|
|
|
|
|
/* the old engine didn't have cargo capacity, but the new one does
|
|
|
* now we will figure out what cargo the train is carrying and refit to fit this */
|
|
|
v = GetFirstVehicleInChain(v);
|
|
|
v = v->First();
|
|
|
do {
|
|
|
if (v->cargo_cap == 0) continue;
|
|
|
/* Now we found a cargo type being carried on the train and we will see if it is possible to carry to this one */
|
|
|
if (v->cargo_type == new_cargo_type) return CT_NO_REFIT;
|
|
|
if (CanRefitTo(engine_type, v->cargo_type)) return v->cargo_type;
|
|
|
} while ((v = v->Next()) != NULL);
|
|
@@ -197,15 +197,15 @@ static CommandCost ReplaceVehicle(Vehicl
|
|
|
if (old_v->type == VEH_TRAIN && !IsFrontEngine(old_v)) {
|
|
|
/* this is a railcar. We need to move the car into the train
|
|
|
* We add the new engine after the old one instead of replacing it. It will give the same result anyway when we
|
|
|
* sell the old engine in a moment
|
|
|
*/
|
|
|
/* Get the vehicle in front of the one we move out */
|
|
|
Vehicle *front = GetPrevVehicleInChain(old_v);
|
|
|
Vehicle *front = old_v->Previous();
|
|
|
/* If the vehicle in front is the rear end of a dualheaded engine, then we need to use the one in front of that one */
|
|
|
if (IsMultiheaded(front) && !IsTrainEngine(front)) front = GetPrevVehicleInChain(front);
|
|
|
if (IsMultiheaded(front) && !IsTrainEngine(front)) front = front->Previous();
|
|
|
/* Now we move the old one out of the train */
|
|
|
DoCommand(0, (INVALID_VEHICLE << 16) | old_v->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
|
|
|
/* Add the new vehicle */
|
|
|
DoCommand(0, (front->index << 16) | new_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
|
|
|
} else {
|
|
|
// copy/clone the orders
|
|
@@ -233,13 +233,13 @@ static CommandCost ReplaceVehicle(Vehicl
|
|
|
if (temp_v != NULL) {
|
|
|
DoCommand(0, (new_v->index << 16) | temp_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
/* We are done setting up the new vehicle. Now we move the cargo from the old one to the new one */
|
|
|
MoveVehicleCargo(new_v->type == VEH_TRAIN ? GetFirstVehicleInChain(new_v) : new_v, old_v);
|
|
|
MoveVehicleCargo(new_v->type == VEH_TRAIN ? new_v->First() : new_v, old_v);
|
|
|
|
|
|
// Get the name of the old vehicle if it has a custom name.
|
|
|
if (!IsCustomName(old_v->string_id)) {
|
|
|
vehicle_name[0] = '\0';
|
|
|
} else {
|
|
|
SetDParam(0, old_v->index);
|