From a6aec252b1987d11e09f2778e481ae3d6fb916fa Mon Sep 17 00:00:00 2001 From: Chris Stevens Date: Fri, 8 Jan 2021 15:05:49 +0000 Subject: [PATCH] Fix #8153: Report incompatible cargo/order when autoreplace fails (#8169) --- src/autoreplace_cmd.cpp | 43 ++++++++++++++++++++++++++++++++++++++++- src/lang/english.txt | 2 ++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/autoreplace_cmd.cpp b/src/autoreplace_cmd.cpp index 313de99cf0..5785968cf0 100644 --- a/src/autoreplace_cmd.cpp +++ b/src/autoreplace_cmd.cpp @@ -20,6 +20,8 @@ #include "vehiclelist.h" #include "road.h" #include "ai/ai.hpp" +#include "news_func.h" +#include "strings_func.h" #include "table/strings.h" @@ -191,6 +193,29 @@ static bool VerifyAutoreplaceRefitForOrders(const Vehicle *v, EngineID engine_ty return true; } +/** + * Gets the index of the first refit order that is incompatible with the requested engine type + * @param v The vehicle to be replaced + * @param engine_type The type we want to replace with + * @return index of the incompatible order or -1 if none were found + */ +static int GetIncompatibleRefitOrderIdForAutoreplace(const Vehicle *v, EngineID engine_type) +{ + CargoTypes union_refit_mask = GetUnionOfArticulatedRefitMasks(engine_type, false); + + const Order *o; + const Vehicle *u = (v->type == VEH_TRAIN) ? v->First() : v; + + const OrderList *orders = u->orders.list; + for (VehicleOrderID i = 0; i < orders->GetNumOrders(); i++) { + o = orders->GetOrderAt(i); + if (!o->IsRefit()) continue; + if (!HasBit(union_refit_mask, o->GetRefitCargo())) return i; + } + + return -1; +} + /** * Function to find what type of cargo to refit to when autoreplacing * @param *v Original vehicle that is being replaced. @@ -293,7 +318,23 @@ static CommandCost BuildReplacementVehicle(Vehicle *old_veh, Vehicle **new_vehic /* Does it need to be refitted */ CargoID refit_cargo = GetNewCargoTypeForReplace(old_veh, e, part_of_chain); - if (refit_cargo == CT_INVALID) return CommandCost(); // incompatible cargoes + if (refit_cargo == CT_INVALID) { + SetDParam(0, old_veh->index); + + int order_id = GetIncompatibleRefitOrderIdForAutoreplace(old_veh, e); + if (order_id != -1) { + /* Orders contained a refit order that is incompatible with the new vehicle. */ + SetDParam(1, STR_ERROR_AUTOREPLACE_INCOMPATIBLE_REFIT); + SetDParam(2, order_id + 1); // 1-based indexing for display + } else { + /* Current cargo is incompatible with the new vehicle. */ + SetDParam(1, STR_ERROR_AUTOREPLACE_INCOMPATIBLE_CARGO); + SetDParam(2, CargoSpec::Get(old_veh->cargo_type)->name); + } + + AddVehicleAdviceNewsItem(STR_NEWS_VEHICLE_AUTORENEW_FAILED, old_veh->index); + return CommandCost(); + } /* Build the new vehicle */ cost = DoCommand(old_veh->tile, e | (CT_INVALID << 24), 0, DC_EXEC | DC_AUTOREPLACE, GetCmdBuildVeh(old_veh)); diff --git a/src/lang/english.txt b/src/lang/english.txt index 52b10d8d11..52b75f413f 100644 --- a/src/lang/english.txt +++ b/src/lang/english.txt @@ -4474,6 +4474,8 @@ STR_ERROR_DEPOT_WRONG_DEPOT_TYPE :Wrong depot typ STR_ERROR_TRAIN_TOO_LONG_AFTER_REPLACEMENT :{WHITE}{VEHICLE} is too long after replacement STR_ERROR_AUTOREPLACE_NOTHING_TO_DO :{WHITE}No autoreplace/renew rules applied STR_ERROR_AUTOREPLACE_MONEY_LIMIT :(money limit) +STR_ERROR_AUTOREPLACE_INCOMPATIBLE_CARGO :{WHITE}New vehicle can't carry {STRING} +STR_ERROR_AUTOREPLACE_INCOMPATIBLE_REFIT :{WHITE}New vehicle can't do refit in order {NUM} # Rail construction errors STR_ERROR_IMPOSSIBLE_TRACK_COMBINATION :{WHITE}Impossible track combination