Skip to content

Commit

Permalink
Fix #8153: Report incompatible cargo/order when autoreplace fails (#8169
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Chr12t0pher committed Jan 8, 2021
1 parent 9aa39d0 commit a6aec25
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/autoreplace_cmd.cpp
Expand Up @@ -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"

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 2 additions & 0 deletions src/lang/english.txt
Expand Up @@ -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
Expand Down

0 comments on commit a6aec25

Please sign in to comment.