From de7f1c11685d491c972079a6d0164d9a6ed007b6 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sun, 14 Feb 2016 16:54:37 +0000 Subject: [PATCH] Show cost text effect and failure advice messages for TBTR replacement. Minor whitespace/formatting changes. --- src/train_cmd.cpp | 11 +++++----- src/vehicle.cpp | 53 +++++++++++++++++++++++++++++++++-------------- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 091b638c24..5f8d9ae1f0 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -4290,11 +4290,12 @@ CommandCost CmdTemplateReplaceVehicle(TileIndex tile, DoCommandFlag flags, uint3 short store_refit_csubt = 0; // if a train shall keep its old refit, store the refit setting of its first vehicle if (!use_refit) { - for (Train *getc = incoming; getc != NULL; getc = getc->GetNextUnit()) + for (Train *getc = incoming; getc != NULL; getc = getc->GetNextUnit()) { if (getc->cargo_type != CT_INVALID) { store_refit_ct = getc->cargo_type; break; } + } } // TODO: set result status to success/no success before returning @@ -4308,7 +4309,7 @@ CommandCost CmdTemplateReplaceVehicle(TileIndex tile, DoCommandFlag flags, uint3 CommandCost buyCost = TestBuyAllTemplateVehiclesInChain(tv, tile); if (!buyCost.Succeeded() || !CheckCompanyHasMoney(buyCost)) { if (!stayInDepot) incoming->vehstatus &= ~VS_STOPPED; - return buy; + return_cmd_error(STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY); } } @@ -4388,8 +4389,9 @@ CommandCost CmdTemplateReplaceVehicle(TileIndex tile, DoCommandFlag flags, uint3 // 3. must buy new engine else { tmp_result = DoCommand(tile, cur_tmpl->engine_type, 0, flags, CMD_BUILD_VEHICLE); - if (!tmp_result.Succeeded()) + if (!tmp_result.Succeeded()) { return tmp_result; + } buy.AddCost(tmp_result); tmp_chain = Train::Get(_new_vehicle_id); move_cost.AddCost(CmdMoveRailVehicle(tile, flags, tmp_chain->index, last_veh->index, 0)); @@ -4399,8 +4401,7 @@ CommandCost CmdTemplateReplaceVehicle(TileIndex tile, DoCommandFlag flags, uint3 if (use_refit) { uint32 cb = GetCmdRefitVeh(tmp_chain); DoCommand(tmp_chain->tile, tmp_chain->index, cur_tmpl->cargo_type | (cur_tmpl->cargo_subtype << 8) | (1 << 16) | (1 << 5), flags, cb); - } - else { + } else { uint32 cb = GetCmdRefitVeh(tmp_chain); DoCommand(tmp_chain->tile, tmp_chain->index, store_refit_ct | (store_refit_csubt << 8) | (1 << 16) | (1 << 5), flags, cb); } diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 2f49bc6155..9f17876dfe 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -892,6 +892,25 @@ static void RunVehicleDayProc() } } +static void ShowAutoReplaceAdviceMessage(const CommandCost &res, const Vehicle *v) +{ + StringID error_message = res.GetErrorMessage(); + if (error_message == STR_ERROR_AUTOREPLACE_NOTHING_TO_DO || error_message == INVALID_STRING_ID) return; + + if (error_message == STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY) error_message = STR_ERROR_AUTOREPLACE_MONEY_LIMIT; + + StringID message; + if (error_message == STR_ERROR_TRAIN_TOO_LONG_AFTER_REPLACEMENT) { + message = error_message; + } else { + message = STR_NEWS_VEHICLE_AUTORENEW_FAILED; + } + + SetDParam(0, v->index); + SetDParam(1, error_message); + AddVehicleAdviceNewsItem(message, v->index); +} + void CallVehicleTicks() { _vehicles_to_autoreplace.Clear(); @@ -1001,21 +1020,7 @@ void CallVehicleTicks() continue; } - StringID error_message = res.GetErrorMessage(); - if (error_message == STR_ERROR_AUTOREPLACE_NOTHING_TO_DO || error_message == INVALID_STRING_ID) continue; - - if (error_message == STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY) error_message = STR_ERROR_AUTOREPLACE_MONEY_LIMIT; - - StringID message; - if (error_message == STR_ERROR_TRAIN_TOO_LONG_AFTER_REPLACEMENT) { - message = error_message; - } else { - message = STR_NEWS_VEHICLE_AUTORENEW_FAILED; - } - - SetDParam(0, v->index); - SetDParam(1, error_message); - AddVehicleAdviceNewsItem(message, v->index); + ShowAutoReplaceAdviceMessage(res, v); } cur_company.Restore(); @@ -1024,12 +1029,28 @@ void CallVehicleTicks() for (TemplateReplacementMap::iterator it = _vehicles_to_templatereplace.Begin(); it != _vehicles_to_templatereplace.End(); it++) { Train *t = it->first; + /* Store the position of the effect as the vehicle pointer will become invalid later */ + int x = t->x_pos; + int y = t->y_pos; + int z = t->z_pos; + tmpl_cur_company.Change(t->owner); bool stayInDepot = it->second; it->first->vehstatus |= VS_STOPPED; - DoCommand(t->tile, t->index, stayInDepot ? 1 : 0, DC_EXEC, CMD_TEMPLATE_REPLACE_VEHICLE); + CommandCost res = DoCommand(t->tile, t->index, stayInDepot ? 1 : 0, DC_EXEC, CMD_TEMPLATE_REPLACE_VEHICLE); + + if (!IsLocalCompany()) continue; + + if (res.Succeeded()) { + if (res.GetCost() != 0) { + ShowCostOrIncomeAnimation(x, y, z, res.GetCost()); + } + continue; + } + + ShowAutoReplaceAdviceMessage(res, t); } tmpl_cur_company.Restore(); }