TBTR: Fix crash and issues when using a no longer available engine type.

Show error messages when cloning a vehicle to a template
or virtual vehicle fails.
Handle failed clones gracefully.
Bypass engine availability check when doing a template edit action.
Show correct error message if template replacement fails for
a non-money reason.
This commit is contained in:
Jonathan G Rennison
2016-02-15 02:31:26 +00:00
parent d9a48a2b5e
commit 5850bcf699
8 changed files with 71 additions and 40 deletions

View File

@@ -360,8 +360,8 @@ static const Command _command_proc_table[] = {
DEF_CMD(CmdToggleKeepRemainingVehicles, CMD_ALL_TILES, CMDT_VEHICLE_MANAGEMENT ), // CMD_TOGGLE_KEEP_REMAINING_VEHICLES DEF_CMD(CmdToggleKeepRemainingVehicles, CMD_ALL_TILES, CMDT_VEHICLE_MANAGEMENT ), // CMD_TOGGLE_KEEP_REMAINING_VEHICLES
DEF_CMD(CmdToggleRefitAsTemplate, CMD_ALL_TILES, CMDT_VEHICLE_MANAGEMENT ), // CMD_TOGGLE_REFIT_AS_TEMPLATE DEF_CMD(CmdToggleRefitAsTemplate, CMD_ALL_TILES, CMDT_VEHICLE_MANAGEMENT ), // CMD_TOGGLE_REFIT_AS_TEMPLATE
DEF_CMD(CmdVirtualTrainFromTemplateVehicle, CMD_ALL_TILES, CMDT_VEHICLE_MANAGEMENT ), // CMD_VIRTUAL_TRAIN_FROM_TEMPLATE_VEHICLE DEF_CMD(CmdVirtualTrainFromTemplateVehicle, CMD_NO_TEST | CMD_ALL_TILES, CMDT_VEHICLE_MANAGEMENT), // CMD_VIRTUAL_TRAIN_FROM_TEMPLATE_VEHICLE
DEF_CMD(CmdVirtualTrainFromTrain, CMD_ALL_TILES, CMDT_VEHICLE_MANAGEMENT ), // CMD_VIRTUAL_TRAIN_FROM_TRAIN DEF_CMD(CmdVirtualTrainFromTrain, CMD_NO_TEST | CMD_ALL_TILES, CMDT_VEHICLE_MANAGEMENT), // CMD_VIRTUAL_TRAIN_FROM_TRAIN
DEF_CMD(CmdDeleteVirtualTrain, CMD_ALL_TILES, CMDT_VEHICLE_MANAGEMENT ), // CMD_DELETE_VIRTUAL_TRAIN DEF_CMD(CmdDeleteVirtualTrain, CMD_ALL_TILES, CMDT_VEHICLE_MANAGEMENT ), // CMD_DELETE_VIRTUAL_TRAIN
DEF_CMD(CmdBuildVirtualRailVehicle, CMD_ALL_TILES, CMDT_VEHICLE_MANAGEMENT ), // CMD_BUILD_VIRTUAL_RAIL_VEHICLE DEF_CMD(CmdBuildVirtualRailVehicle, CMD_ALL_TILES, CMDT_VEHICLE_MANAGEMENT ), // CMD_BUILD_VIRTUAL_RAIL_VEHICLE
DEF_CMD(CmdReplaceTemplateVehicle, CMD_ALL_TILES, CMDT_VEHICLE_MANAGEMENT ), // CMD_REPLACE_TEMPLATE_VEHICLE DEF_CMD(CmdReplaceTemplateVehicle, CMD_ALL_TILES, CMDT_VEHICLE_MANAGEMENT ), // CMD_REPLACE_TEMPLATE_VEHICLE

View File

@@ -5027,3 +5027,5 @@ STR_TMPL_RPLALLGUI_BUTTON_RPLALL :{BLACK}Replace
STR_TMPL_RPLALLGUI_BUTTON_APPLY :{BLACK}Apply STR_TMPL_RPLALLGUI_BUTTON_APPLY :{BLACK}Apply
STR_TMPL_RPLALLGUI_BUTTON_CANCEL :{BLACK}Cancel STR_TMPL_RPLALLGUI_BUTTON_CANCEL :{BLACK}Cancel
STR_TMPL_RPLALLGUI_USE_TIP :{BLACK}Select a vehicle type from each list and press 'Replace All'. If you are happy with the result displayed in the template list, press 'Apply' to actually apply these changes. STR_TMPL_RPLALLGUI_USE_TIP :{BLACK}Select a vehicle type from each list and press 'Replace All'. If you are happy with the result displayed in the template list, press 'Apply' to actually apply these changes.
STR_TMPL_CANT_CREATE :{WHITE}Can't create template or virtual vehicle...

View File

@@ -161,7 +161,7 @@ public:
this->vehicle_over = INVALID_VEHICLE; this->vehicle_over = INVALID_VEHICLE;
if (to_edit) { if (to_edit) {
DoCommandP(0, to_edit->index, 0, CMD_VIRTUAL_TRAIN_FROM_TEMPLATE_VEHICLE, CcSetVirtualTrain); DoCommandP(0, to_edit->index, 0, CMD_VIRTUAL_TRAIN_FROM_TEMPLATE_VEHICLE | CMD_MSG(STR_TMPL_CANT_CREATE), CcSetVirtualTrain);
} }
this->resize.step_height = 1; this->resize.step_height = 1;
@@ -265,7 +265,7 @@ public:
} }
// create a new one // create a new one
DoCommandP(0, v->index, 0, CMD_VIRTUAL_TRAIN_FROM_TRAIN, CcSetVirtualTrain); DoCommandP(0, v->index, 0, CMD_VIRTUAL_TRAIN_FROM_TRAIN | CMD_MSG(STR_TMPL_CANT_CREATE), CcSetVirtualTrain);
this->ToggleWidgetLoweredState(TCW_CLONE); this->ToggleWidgetLoweredState(TCW_CLONE);
ResetObjectToPlace(); ResetObjectToPlace();
this->SetDirty(); this->SetDirty();

View File

@@ -478,7 +478,7 @@ public:
virtual bool OnVehicleSelect(const Vehicle *v) virtual bool OnVehicleSelect(const Vehicle *v)
{ {
bool succeeded = DoCommandP(0, v->index, 0, CMD_CLONE_TEMPLATE_VEHICLE_FROM_TRAIN, NULL); bool succeeded = DoCommandP(0, v->index, 0, CMD_CLONE_TEMPLATE_VEHICLE_FROM_TRAIN | CMD_MSG(STR_TMPL_CANT_CREATE), NULL);
if (!succeeded) return false; if (!succeeded) return false;

View File

@@ -17,7 +17,7 @@
#include "tbtr_template_vehicle.h" #include "tbtr_template_vehicle.h"
Train* VirtualTrainFromTemplateVehicle(TemplateVehicle* tv); Train* VirtualTrainFromTemplateVehicle(TemplateVehicle* tv, StringID &err);
void DrawTemplateVehicle(const TemplateVehicle*, int, int, int, VehicleID, int, VehicleID); void DrawTemplateVehicle(const TemplateVehicle*, int, int, int, VehicleID, int, VehicleID);

View File

@@ -353,7 +353,7 @@ CommandCost CmdMoveRailVehicle(TileIndex, DoCommandFlag , uint32, uint32, const
CommandCost CmdMoveVirtualRailVehicle(TileIndex, DoCommandFlag, uint32, uint32, const char*); CommandCost CmdMoveVirtualRailVehicle(TileIndex, DoCommandFlag, uint32, uint32, const char*);
Train* CmdBuildVirtualRailWagon(const Engine*); Train* CmdBuildVirtualRailWagon(const Engine*);
Train* CmdBuildVirtualRailVehicle(EngineID); Train* CmdBuildVirtualRailVehicle(EngineID, bool lax_engine_check, StringID &error);
#define FOR_ALL_TRAINS(var) FOR_ALL_VEHICLES_OF_TYPE(Train, var) #define FOR_ALL_TRAINS(var) FOR_ALL_VEHICLES_OF_TYPE(Train, var)

View File

@@ -4136,26 +4136,27 @@ Train* CmdBuildVirtualRailWagon(const Engine *e)
return v; return v;
} }
/** Train* CmdBuildVirtualRailVehicle(EngineID eid, bool lax_engine_check, StringID &error)
* Build a railroad vehicle.
* @param tile tile of the depot where rail-vehicle is built.
* @param flags type of operation.
* @param e the engine to build.
* @param data bit 0 prevents any free cars from being added to the train.
* @param ret[out] the vehicle that has been built.
* @return the cost of this operation or an error.
*/
Train* CmdBuildVirtualRailVehicle(EngineID eid)
{ {
if (!IsEngineBuildable(eid, VEH_TRAIN, _current_company)) { if (lax_engine_check) {
const Engine *e = Engine::GetIfValid(eid);
if (e == NULL || e->type != VEH_TRAIN) {
error = STR_ERROR_RAIL_VEHICLE_NOT_AVAILABLE + VEH_TRAIN;
return NULL; return NULL;
} }
} else {
if (!IsEngineBuildable(eid, VEH_TRAIN, _current_company)) {
error = STR_ERROR_RAIL_VEHICLE_NOT_AVAILABLE + VEH_TRAIN;
return NULL;
}
}
const Engine* e = Engine::Get(eid); const Engine* e = Engine::Get(eid);
const RailVehicleInfo *rvi = &e->u.rail; const RailVehicleInfo *rvi = &e->u.rail;
int num_vehicles = (e->u.rail.railveh_type == RAILVEH_MULTIHEAD ? 2 : 1) + CountArticulatedParts(eid, false); int num_vehicles = (e->u.rail.railveh_type == RAILVEH_MULTIHEAD ? 2 : 1) + CountArticulatedParts(eid, false);
if (!Train::CanAllocateItem(num_vehicles)) { if (!Train::CanAllocateItem(num_vehicles)) {
error = STR_ERROR_TOO_MANY_VEHICLES_IN_GAME;
return NULL; return NULL;
} }
@@ -4229,13 +4230,18 @@ CommandCost CmdBuildVirtualRailVehicle(TileIndex tile, DoCommandFlag flags, uint
{ {
EngineID eid = p1; EngineID eid = p1;
if (!IsEngineBuildable(eid, VEH_TRAIN, _current_company)) {
return_cmd_error(STR_ERROR_RAIL_VEHICLE_NOT_AVAILABLE + VEH_TRAIN);
}
bool should_execute = (flags & DC_EXEC) != 0; bool should_execute = (flags & DC_EXEC) != 0;
if (should_execute) { if (should_execute) {
Train* train = CmdBuildVirtualRailVehicle(eid); StringID err = INVALID_STRING_ID;
Train* train = CmdBuildVirtualRailVehicle(eid, false, err);
if (train == NULL) { if (train == NULL) {
return CMD_ERROR; return_cmd_error(err);
} }
} }
@@ -4309,9 +4315,14 @@ CommandCost CmdTemplateReplaceVehicle(TileIndex tile, DoCommandFlag flags, uint3
CommandCost buyCost = TestBuyAllTemplateVehiclesInChain(tv, tile); CommandCost buyCost = TestBuyAllTemplateVehiclesInChain(tv, tile);
if (!buyCost.Succeeded() || !CheckCompanyHasMoney(buyCost)) { if (!buyCost.Succeeded() || !CheckCompanyHasMoney(buyCost)) {
if (!stayInDepot) incoming->vehstatus &= ~VS_STOPPED; if (!stayInDepot) incoming->vehstatus &= ~VS_STOPPED;
if (!buyCost.Succeeded() && buyCost.GetErrorMessage() != INVALID_STRING_ID) {
return buyCost;
} else {
return_cmd_error(STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY); return_cmd_error(STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY);
} }
} }
}
/* define replacement behavior */ /* define replacement behavior */
bool reuseDepot = tv->IsSetReuseDepotVehicles(); bool reuseDepot = tv->IsSetReuseDepotVehicles();

View File

@@ -925,34 +925,41 @@ CommandCost CmdVirtualTrainFromTemplateVehicle(TileIndex tile, DoCommandFlag fla
bool should_execute = (flags & DC_EXEC) != 0; bool should_execute = (flags & DC_EXEC) != 0;
if (should_execute) { if (should_execute) {
Train* train = VirtualTrainFromTemplateVehicle(tv); StringID err = INVALID_STRING_ID;
Train* train = VirtualTrainFromTemplateVehicle(tv, err);
if (train == NULL) { if (train == NULL) {
return CMD_ERROR; return_cmd_error(err);
} }
} }
return CommandCost(); return CommandCost();
} }
Train* VirtualTrainFromTemplateVehicle(TemplateVehicle* tv) CommandCost CmdDeleteVirtualTrain(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text);
Train* VirtualTrainFromTemplateVehicle(TemplateVehicle* tv, StringID &err)
{ {
CommandCost c; CommandCost c;
Train *tmp, *head, *tail; Train *tmp, *head, *tail;
head = CmdBuildVirtualRailVehicle(tv->engine_type); head = CmdBuildVirtualRailVehicle(tv->engine_type, true, err);
if (!head) return NULL; if (!head) return NULL;
tail = head; tail = head;
tv = tv->GetNextUnit(); tv = tv->GetNextUnit();
while (tv) { while (tv) {
tmp = CmdBuildVirtualRailVehicle(tv->engine_type); tmp = CmdBuildVirtualRailVehicle(tv->engine_type, true, err);
if (tmp) { if (!tmp) {
CmdDeleteVirtualTrain(INVALID_TILE, DC_EXEC, head->index, 0, NULL);
return NULL;
}
tmp->cargo_type = tv->cargo_type; tmp->cargo_type = tv->cargo_type;
tmp->cargo_subtype = tv->cargo_subtype; tmp->cargo_subtype = tv->cargo_subtype;
CmdMoveRailVehicle(INVALID_TILE, DC_EXEC, (1 << 21) | tmp->index, tail->index, 0); CmdMoveRailVehicle(INVALID_TILE, DC_EXEC, (1 << 21) | tmp->index, tail->index, 0);
tail = tmp; tail = tmp;
}
tv = tv->GetNextUnit(); tv = tv->GetNextUnit();
} }
@@ -986,20 +993,25 @@ CommandCost CmdVirtualTrainFromTrain(TileIndex tile, DoCommandFlag flags, uint32
if (should_execute) { if (should_execute) {
CommandCost c; CommandCost c;
Train *tmp, *head, *tail; Train *tmp, *head, *tail;
StringID err = INVALID_STRING_ID;
head = CmdBuildVirtualRailVehicle(train->engine_type); head = CmdBuildVirtualRailVehicle(train->engine_type, false, err);
if (!head) return CMD_ERROR; if (!head) return_cmd_error(err);
tail = head; tail = head;
train = train->GetNextUnit(); train = train->GetNextUnit();
while (train) { while (train) {
tmp = CmdBuildVirtualRailVehicle(train->engine_type); tmp = CmdBuildVirtualRailVehicle(train->engine_type, false, err);
if (tmp) { if (!tmp) {
CmdDeleteVirtualTrain(tile, flags, head->index, 0, NULL);
return_cmd_error(err);
}
tmp->cargo_type = train->cargo_type; tmp->cargo_type = train->cargo_type;
tmp->cargo_subtype = train->cargo_subtype; tmp->cargo_subtype = train->cargo_subtype;
CmdMoveRailVehicle(0, DC_EXEC, (1 << 21) | tmp->index, tail->index, 0); CmdMoveRailVehicle(0, DC_EXEC, (1 << 21) | tmp->index, tail->index, 0);
tail = tmp; tail = tmp;
}
train = train->GetNextUnit(); train = train->GetNextUnit();
} }
@@ -1121,6 +1133,12 @@ CommandCost CmdTemplateVehicleFromTrain(TileIndex tile, DoCommandFlag flags, uin
return CMD_ERROR; return CMD_ERROR;
} }
for (Train *v = clicked; v != NULL; v = v->Next()) {
if (!IsEngineBuildable(v->engine_type, VEH_TRAIN, _current_company)) {
return_cmd_error(STR_ERROR_RAIL_VEHICLE_NOT_AVAILABLE + VEH_TRAIN);
}
}
bool should_execute = (flags & DC_EXEC) != 0; bool should_execute = (flags & DC_EXEC) != 0;
if (should_execute) { if (should_execute) {