diff --git a/src/command.cpp b/src/command.cpp index d43b3e5115..9de3a4e11f 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -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(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(CmdVirtualTrainFromTrain, CMD_ALL_TILES, CMDT_VEHICLE_MANAGEMENT ), // CMD_VIRTUAL_TRAIN_FROM_TRAIN + DEF_CMD(CmdVirtualTrainFromTemplateVehicle, CMD_NO_TEST | CMD_ALL_TILES, CMDT_VEHICLE_MANAGEMENT), // CMD_VIRTUAL_TRAIN_FROM_TEMPLATE_VEHICLE + 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(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 diff --git a/src/lang/english.txt b/src/lang/english.txt index cad97b1066..e188672ba3 100644 --- a/src/lang/english.txt +++ b/src/lang/english.txt @@ -5027,3 +5027,5 @@ STR_TMPL_RPLALLGUI_BUTTON_RPLALL :{BLACK}Replace STR_TMPL_RPLALLGUI_BUTTON_APPLY :{BLACK}Apply 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_CANT_CREATE :{WHITE}Can't create template or virtual vehicle... diff --git a/src/tbtr_template_gui_create.cpp b/src/tbtr_template_gui_create.cpp index 288d511da2..ed35624b31 100644 --- a/src/tbtr_template_gui_create.cpp +++ b/src/tbtr_template_gui_create.cpp @@ -161,7 +161,7 @@ public: this->vehicle_over = INVALID_VEHICLE; 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; @@ -265,7 +265,7 @@ public: } // 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); ResetObjectToPlace(); this->SetDirty(); diff --git a/src/tbtr_template_gui_main.cpp b/src/tbtr_template_gui_main.cpp index 7f8e6d4f02..23960739ce 100644 --- a/src/tbtr_template_gui_main.cpp +++ b/src/tbtr_template_gui_main.cpp @@ -478,7 +478,7 @@ public: 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; diff --git a/src/tbtr_template_vehicle_func.h b/src/tbtr_template_vehicle_func.h index 0e2989ec87..7e3bfa36bf 100644 --- a/src/tbtr_template_vehicle_func.h +++ b/src/tbtr_template_vehicle_func.h @@ -17,7 +17,7 @@ #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); diff --git a/src/train.h b/src/train.h index 99b6699dc2..562b704801 100644 --- a/src/train.h +++ b/src/train.h @@ -353,7 +353,7 @@ CommandCost CmdMoveRailVehicle(TileIndex, DoCommandFlag , uint32, uint32, const CommandCost CmdMoveVirtualRailVehicle(TileIndex, DoCommandFlag, uint32, uint32, const char*); 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) diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 5f8d9ae1f0..eeafb441db 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -4136,19 +4136,19 @@ Train* CmdBuildVirtualRailWagon(const Engine *e) return v; } -/** - * 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) +Train* CmdBuildVirtualRailVehicle(EngineID eid, bool lax_engine_check, StringID &error) { - if (!IsEngineBuildable(eid, VEH_TRAIN, _current_company)) { - return NULL; + 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; + } + } 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); @@ -4156,6 +4156,7 @@ Train* CmdBuildVirtualRailVehicle(EngineID eid) int num_vehicles = (e->u.rail.railveh_type == RAILVEH_MULTIHEAD ? 2 : 1) + CountArticulatedParts(eid, false); if (!Train::CanAllocateItem(num_vehicles)) { + error = STR_ERROR_TOO_MANY_VEHICLES_IN_GAME; return NULL; } @@ -4229,13 +4230,18 @@ CommandCost CmdBuildVirtualRailVehicle(TileIndex tile, DoCommandFlag flags, uint { 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; if (should_execute) { - Train* train = CmdBuildVirtualRailVehicle(eid); + StringID err = INVALID_STRING_ID; + Train* train = CmdBuildVirtualRailVehicle(eid, false, err); if (train == NULL) { - return CMD_ERROR; + return_cmd_error(err); } } @@ -4309,7 +4315,12 @@ CommandCost CmdTemplateReplaceVehicle(TileIndex tile, DoCommandFlag flags, uint3 CommandCost buyCost = TestBuyAllTemplateVehiclesInChain(tv, tile); if (!buyCost.Succeeded() || !CheckCompanyHasMoney(buyCost)) { if (!stayInDepot) incoming->vehstatus &= ~VS_STOPPED; - return_cmd_error(STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY); + + if (!buyCost.Succeeded() && buyCost.GetErrorMessage() != INVALID_STRING_ID) { + return buyCost; + } else { + return_cmd_error(STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY); + } } } diff --git a/src/vehicle_cmd.cpp b/src/vehicle_cmd.cpp index 622cf0d562..aa36f859d7 100644 --- a/src/vehicle_cmd.cpp +++ b/src/vehicle_cmd.cpp @@ -925,34 +925,41 @@ CommandCost CmdVirtualTrainFromTemplateVehicle(TileIndex tile, DoCommandFlag fla bool should_execute = (flags & DC_EXEC) != 0; if (should_execute) { - Train* train = VirtualTrainFromTemplateVehicle(tv); + StringID err = INVALID_STRING_ID; + Train* train = VirtualTrainFromTemplateVehicle(tv, err); if (train == NULL) { - return CMD_ERROR; + return_cmd_error(err); } } 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; Train *tmp, *head, *tail; - head = CmdBuildVirtualRailVehicle(tv->engine_type); + head = CmdBuildVirtualRailVehicle(tv->engine_type, true, err); if (!head) return NULL; tail = head; tv = tv->GetNextUnit(); while (tv) { - tmp = CmdBuildVirtualRailVehicle(tv->engine_type); - if (tmp) { - tmp->cargo_type = tv->cargo_type; - tmp->cargo_subtype = tv->cargo_subtype; - CmdMoveRailVehicle(INVALID_TILE, DC_EXEC, (1 << 21) | tmp->index, tail->index, 0); - tail = tmp; + tmp = CmdBuildVirtualRailVehicle(tv->engine_type, true, err); + if (!tmp) { + CmdDeleteVirtualTrain(INVALID_TILE, DC_EXEC, head->index, 0, NULL); + return NULL; } + + tmp->cargo_type = tv->cargo_type; + tmp->cargo_subtype = tv->cargo_subtype; + CmdMoveRailVehicle(INVALID_TILE, DC_EXEC, (1 << 21) | tmp->index, tail->index, 0); + tail = tmp; + tv = tv->GetNextUnit(); } @@ -986,20 +993,25 @@ CommandCost CmdVirtualTrainFromTrain(TileIndex tile, DoCommandFlag flags, uint32 if (should_execute) { CommandCost c; Train *tmp, *head, *tail; + StringID err = INVALID_STRING_ID; - head = CmdBuildVirtualRailVehicle(train->engine_type); - if (!head) return CMD_ERROR; + head = CmdBuildVirtualRailVehicle(train->engine_type, false, err); + if (!head) return_cmd_error(err); tail = head; train = train->GetNextUnit(); while (train) { - tmp = CmdBuildVirtualRailVehicle(train->engine_type); - if (tmp) { - tmp->cargo_type = train->cargo_type; - tmp->cargo_subtype = train->cargo_subtype; - CmdMoveRailVehicle(0, DC_EXEC, (1 << 21) | tmp->index, tail->index, 0); - tail = tmp; + tmp = CmdBuildVirtualRailVehicle(train->engine_type, false, err); + if (!tmp) { + CmdDeleteVirtualTrain(tile, flags, head->index, 0, NULL); + return_cmd_error(err); } + + tmp->cargo_type = train->cargo_type; + tmp->cargo_subtype = train->cargo_subtype; + CmdMoveRailVehicle(0, DC_EXEC, (1 << 21) | tmp->index, tail->index, 0); + tail = tmp; + train = train->GetNextUnit(); } @@ -1121,6 +1133,12 @@ CommandCost CmdTemplateVehicleFromTrain(TileIndex tile, DoCommandFlag flags, uin 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; if (should_execute) {