diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index de3a7fb223..4fb138a65c 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -7221,6 +7221,11 @@ CommandCost CmdTemplateReplaceVehicle(TileIndex tile, DoCommandFlag flags, uint3 CommandCost buy(EXPENSES_NEW_VEHICLES); + auto refit_unit = [&](const Train *unit, CargoID cid, uint16 csubt) { + CommandCost refit_cost = DoCommand(unit->tile, unit->index, cid | csubt << 8 | (1 << 16), flags, GetCmdRefitVeh(unit)); + if (refit_cost.Succeeded()) buy.AddCost(refit_cost); + }; + if (need_replacement) { // step 1: generate primary for newchain and generate remainder_chain // 1. primary of incoming might already fit the template @@ -7292,7 +7297,7 @@ CommandCost CmdTemplateReplaceVehicle(TileIndex tile, DoCommandFlag flags, uint3 // additionally, if we don't want to use the template refit, refit as incoming // the template refit will be set further down, if we use it at all if (!refit_to_template) { - buy.AddCost(DoCommand(new_chain->tile, new_chain->index, store_refit_ct | store_refit_csubt << 8 | (1 << 16) | (1 << 31), flags, GetCmdRefitVeh(new_chain))); + refit_unit(new_chain, store_refit_ct, store_refit_csubt); } } @@ -7350,15 +7355,10 @@ CommandCost CmdTemplateReplaceVehicle(TileIndex tile, DoCommandFlag flags, uint3 if (new_part != nullptr) { last_veh = new_part; } - // TODO: is this enough ? might it be that we bought a new wagon here and it now has std refit ? - if (need_refit && new_part != nullptr) { - if (refit_to_template) { - DoCommand(tile, new_part->index, cur_tmpl->cargo_type | (cur_tmpl->cargo_subtype << 8) | (1 << 16) | (1 << 31), flags, GetCmdRefitVeh(new_part)); - } else { - DoCommand(tile, new_part->index, store_refit_ct | (store_refit_csubt << 8) | (1 << 16) | (1 << 31), flags, GetCmdRefitVeh(new_part)); - } - } + if (!refit_to_template && new_part != nullptr) { + refit_unit(new_part, store_refit_ct, store_refit_csubt); + } } } else { /* no replacement done */ diff --git a/src/vehicle_cmd.cpp b/src/vehicle_cmd.cpp index c8469d286f..e2ee028de9 100644 --- a/src/vehicle_cmd.cpp +++ b/src/vehicle_cmd.cpp @@ -512,7 +512,6 @@ static CommandCost RefitVehicle(Vehicle *v, bool only_this, uint8 num_vehicles, * Only used if "refit only this vehicle" is false. * - p2 = (bit 24) - Automatic refitting. * - p2 = (bit 25) - Refit only this vehicle. Used only for cloning vehicles. - * - p2 = (bit 31) - Is a virtual train (used by template replacement to allow refitting without stopped-in-depot checks) * @param text unused * @return the cost of this operation or an error */ @@ -529,10 +528,9 @@ CommandCost CmdRefitVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint bool auto_refit = HasBit(p2, 24); bool is_virtual_train = v->type == VEH_TRAIN && Train::From(front)->IsVirtual(); - bool virtual_train_mode = HasBit(p2, 31) || is_virtual_train; bool free_wagon = v->type == VEH_TRAIN && Train::From(front)->IsFreeWagon(); // used by autoreplace/renew - if (virtual_train_mode) { + if (is_virtual_train) { CommandCost ret = CheckOwnership(front->owner); if (ret.Failed()) return ret; } else { @@ -544,7 +542,7 @@ CommandCost CmdRefitVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint if (v != front && (v->type == VEH_AIRCRAFT)) return CMD_ERROR; /* Allow auto-refitting only during loading and normal refitting only in a depot. */ - if (!virtual_train_mode) { + if (!is_virtual_train) { if ((flags & DC_QUERY_COST) == 0 && // used by the refit GUI, including the order refit GUI. !free_wagon && // used by autoreplace/renew (!auto_refit || !front->current_order.IsType(OT_LOADING)) && // refit inside stations