Fix no error message when attaching new template/virtual vehicle fails

See: #187
This commit is contained in:
Jonathan G Rennison
2020-09-08 23:56:38 +01:00
parent ff12051ee1
commit 11a9a4dc0c
5 changed files with 42 additions and 2 deletions

View File

@@ -31,6 +31,7 @@
#include "core/geometry_func.hpp" #include "core/geometry_func.hpp"
#include "autoreplace_func.h" #include "autoreplace_func.h"
#include "train.h" #include "train.h"
#include "error.h"
#include "widgets/build_vehicle_widget.h" #include "widgets/build_vehicle_widget.h"
@@ -1676,7 +1677,7 @@ struct BuildVehicleWindow : Window {
} else { } else {
VehicleID target = (*(this->virtual_train_out))->GetLastUnit()->index; VehicleID target = (*(this->virtual_train_out))->GetLastUnit()->index;
DoCommandP(0, (1 << 23) | (1 << 21) | toadd->index, target, CMD_MOVE_RAIL_VEHICLE); DoCommandP(0, (1 << 23) | (1 << 21) | toadd->index, target, CMD_MOVE_RAIL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_MOVE_VEHICLE), CcMoveNewVirtualEngine);
} }
InvalidateWindowClassesData(WC_CREATE_TEMPLATE); InvalidateWindowClassesData(WC_CREATE_TEMPLATE);
InvalidateWindowClassesData(WC_TEMPLATEGUI_MAIN); InvalidateWindowClassesData(WC_TEMPLATEGUI_MAIN);
@@ -1691,6 +1692,21 @@ void CcAddVirtualEngine(const CommandCost &result, TileIndex tile, uint32 p1, ui
if (window) { if (window) {
Train* train = Train::From(Vehicle::Get(_new_vehicle_id)); Train* train = Train::From(Vehicle::Get(_new_vehicle_id));
((BuildVehicleWindow*) window)->AddVirtualEngine(train); ((BuildVehicleWindow*) window)->AddVirtualEngine(train);
} else {
DoCommandP(0, _new_vehicle_id | (1 << 21), 0, CMD_SELL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_SELL_TRAIN));
}
}
void CcMoveNewVirtualEngine(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
{
if (result.Failed()) return;
Window* window = FindWindowById(WC_BUILD_VIRTUAL_TRAIN, 0);
if (window) {
if (result.IsSuccessWithMessage()) {
CommandCost res = result.UnwrapSuccessWithMessage();
ShowErrorMessage(STR_ERROR_CAN_T_MOVE_VEHICLE, res.GetErrorMessage(), WL_INFO, 0, 0, res.GetTextRefStackGRF(), res.GetTextRefStackSize(), res.GetTextRefStack(), res.GetExtraErrorMessage());
}
} }
} }

View File

@@ -135,5 +135,6 @@ CommandCallback CcDeleteVirtualTrain;
/* build_vehicle_gui.cpp */ /* build_vehicle_gui.cpp */
CommandCallback CcAddVirtualEngine; CommandCallback CcAddVirtualEngine;
CommandCallback CcMoveNewVirtualEngine;
#endif /* COMMAND_FUNC_H */ #endif /* COMMAND_FUNC_H */

View File

@@ -198,6 +198,25 @@ public:
* @return the number of bytes written * @return the number of bytes written
*/ */
int WriteSummaryMessage(char *buf, char *last, StringID cmd_msg = 0) const; int WriteSummaryMessage(char *buf, char *last, StringID cmd_msg = 0) const;
bool IsSuccessWithMessage() const
{
return this->Succeeded() && this->message != INVALID_STRING_ID;
}
void MakeSuccessWithMessage()
{
assert(this->message != INVALID_STRING_ID);
this->success = true;
}
CommandCost UnwrapSuccessWithMessage() const
{
assert(this->IsSuccessWithMessage());
CommandCost res = *this;
res.success = false;
return res;
}
}; };
/** /**

View File

@@ -52,6 +52,7 @@ static CommandCallback * const _callback_table[] = {
/* 0x1E */ CcVirtualTrainWagonsMoved, /* 0x1E */ CcVirtualTrainWagonsMoved,
/* 0x1F */ CcDeleteVirtualTrain, /* 0x1F */ CcDeleteVirtualTrain,
/* 0x20 */ CcAddVirtualEngine, /* 0x20 */ CcAddVirtualEngine,
/* 0x21 */ CcMoveNewVirtualEngine,
}; };
/** /**

View File

@@ -1396,7 +1396,10 @@ CommandCost CmdMoveRailVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, u
auto check_on_failure = [&](CommandCost cost) -> CommandCost { auto check_on_failure = [&](CommandCost cost) -> CommandCost {
if (delete_failed_virtual && src->IsVirtual()) { if (delete_failed_virtual && src->IsVirtual()) {
return DoCommand(src->tile, src->index | (1 << 21), 0, flags, CMD_SELL_VEHICLE); CommandCost res = DoCommand(src->tile, src->index | (1 << 21), 0, flags, CMD_SELL_VEHICLE);
if (res.Failed() || cost.GetErrorMessage() == INVALID_STRING_ID) return res;
cost.MakeSuccessWithMessage();
return cost;
} else { } else {
return cost; return cost;
} }