diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp index 32f26ef8ed..e8c7350183 100644 --- a/src/build_vehicle_gui.cpp +++ b/src/build_vehicle_gui.cpp @@ -1425,6 +1425,54 @@ struct BuildVehicleWindowBase : Window { return list; } + + void FillTestedEngineCapacity(EngineID engine, CargoID cargo, TestedEngineDetails &te) const + { + const Engine *e = Engine::Get(engine); + if (!e->CanPossiblyCarryCargo()) { + te.cost = 0; + te.cargo = INVALID_CARGO; + te.all_capacities.Clear(); + return; + } + + if (this->virtual_train_mode) { + if (cargo != INVALID_CARGO && cargo != e->GetDefaultCargoType()) { + SavedRandomSeeds saved_seeds; + SaveRandomSeeds(&saved_seeds); + StringID err; + Train *t = BuildVirtualRailVehicle(engine, err, 0, false); + if (t != nullptr) { + const CommandCost ret = CmdRefitVehicle(0, DC_QUERY_COST, t->index, cargo | (1 << 16), nullptr); + te.cost = ret.GetCost(); + te.capacity = _returned_refit_capacity; + te.mail_capacity = _returned_mail_refit_capacity; + te.cargo = (cargo == INVALID_CARGO) ? e->GetDefaultCargoType() : cargo; + te.all_capacities = _returned_vehicle_capacities; + delete t; + RestoreRandomSeeds(saved_seeds); + return; + } else { + RestoreRandomSeeds(saved_seeds); + } + } + } else if (!this->listview_mode) { + /* Query for cost and refitted capacity */ + CommandCost ret = DoCommand(this->window_number, engine | (cargo << 24), 0, DC_QUERY_COST, GetCmdBuildVeh(this->vehicle_type), nullptr); + if (ret.Succeeded()) { + te.cost = ret.GetCost() - e->GetCost(); + te.capacity = _returned_refit_capacity; + te.mail_capacity = _returned_mail_refit_capacity; + te.cargo = (cargo == INVALID_CARGO) ? e->GetDefaultCargoType() : cargo; + te.all_capacities = _returned_vehicle_capacities; + return; + } + } + + /* Purchase test was not possible or failed, fill in the defaults instead. */ + te = {}; + te.FillDefaultCapacities(e); + } }; /** GUI for building vehicles. */ @@ -1579,50 +1627,7 @@ struct BuildVehicleWindow : BuildVehicleWindowBase { if (this->sel_engine == INVALID_ENGINE) return; - const Engine *e = Engine::Get(this->sel_engine); - if (!e->CanPossiblyCarryCargo()) { - this->te.cost = 0; - this->te.cargo = INVALID_CARGO; - this->te.all_capacities.Clear(); - return; - } - - if (this->virtual_train_mode) { - if (cargo != INVALID_CARGO && cargo != e->GetDefaultCargoType()) { - SavedRandomSeeds saved_seeds; - SaveRandomSeeds(&saved_seeds); - StringID err; - Train *t = BuildVirtualRailVehicle(this->sel_engine, err, 0, false); - if (t != nullptr) { - const CommandCost ret = CmdRefitVehicle(0, DC_QUERY_COST, t->index, cargo | (1 << 16), nullptr); - this->te.cost = ret.GetCost(); - this->te.capacity = _returned_refit_capacity; - this->te.mail_capacity = _returned_mail_refit_capacity; - this->te.cargo = (cargo == INVALID_CARGO) ? e->GetDefaultCargoType() : cargo; - this->te.all_capacities = _returned_vehicle_capacities; - delete t; - RestoreRandomSeeds(saved_seeds); - return; - } else { - RestoreRandomSeeds(saved_seeds); - } - } - } else if (!this->listview_mode) { - /* Query for cost and refitted capacity */ - CommandCost ret = DoCommand(this->window_number, this->sel_engine | (cargo << 24), 0, DC_QUERY_COST, GetCmdBuildVeh(this->vehicle_type), nullptr); - if (ret.Succeeded()) { - this->te.cost = ret.GetCost() - e->GetCost(); - this->te.capacity = _returned_refit_capacity; - this->te.mail_capacity = _returned_mail_refit_capacity; - this->te.cargo = (cargo == INVALID_CARGO) ? e->GetDefaultCargoType() : cargo; - this->te.all_capacities = _returned_vehicle_capacities; - return; - } - } - - /* Purchase test was not possible or failed, fill in the defaults instead. */ - this->te = {}; - this->te.FillDefaultCapacities(e); + this->FillTestedEngineCapacity(this->sel_engine, cargo, this->te); } void OnInit() override @@ -2486,50 +2491,7 @@ struct BuildVehicleWindowTrainAdvanced final : BuildVehicleWindowBase { if (state.sel_engine == INVALID_ENGINE) return; - const Engine *e = Engine::Get(state.sel_engine); - if (!e->CanPossiblyCarryCargo()) { - state.te.cost = 0; - state.te.cargo = INVALID_CARGO; - state.te.all_capacities.Clear(); - return; - } - - if (this->virtual_train_mode) { - if (cargo != INVALID_CARGO && cargo != e->GetDefaultCargoType()) { - SavedRandomSeeds saved_seeds; - SaveRandomSeeds(&saved_seeds); - StringID err; - Train *t = BuildVirtualRailVehicle(state.sel_engine, err, 0, false); - if (t != nullptr) { - const CommandCost ret = CmdRefitVehicle(0, DC_QUERY_COST, t->index, cargo | (1 << 16), nullptr); - state.te.cost = ret.GetCost(); - state.te.capacity = _returned_refit_capacity; - state.te.mail_capacity = _returned_mail_refit_capacity; - state.te.cargo = (cargo == INVALID_CARGO) ? e->GetDefaultCargoType() : cargo; - state.te.all_capacities = _returned_vehicle_capacities; - delete t; - RestoreRandomSeeds(saved_seeds); - return; - } else { - RestoreRandomSeeds(saved_seeds); - } - } - } else if (!this->listview_mode) { - /* Query for cost and refitted capacity */ - const CommandCost ret = DoCommand(this->window_number, state.sel_engine | (cargo << 24), 0, DC_QUERY_COST, GetCmdBuildVeh(this->vehicle_type), nullptr); - if (ret.Succeeded()) { - state.te.cost = ret.GetCost() - e->GetCost(); - state.te.capacity = _returned_refit_capacity; - state.te.mail_capacity = _returned_mail_refit_capacity; - state.te.cargo = (cargo == INVALID_CARGO) ? e->GetDefaultCargoType() : cargo; - state.te.all_capacities = _returned_vehicle_capacities; - return; - } - } - - /* Purchase test was not possible or failed, fill in the defaults instead. */ - state.te = {}; - state.te.FillDefaultCapacities(e); + this->FillTestedEngineCapacity(state.sel_engine, cargo, state.te); } void SelectColumn(bool wagon)