Fix crash and show error message if group creation fails

This commit is contained in:
Andreas Schmitt
2021-06-21 00:47:50 +02:00
parent 64488fdf29
commit 5d505f2389
2 changed files with 14 additions and 12 deletions

View File

@@ -770,11 +770,12 @@ void GetAutoGroupMostRelevantTowns(const Vehicle *vehicle, Town* &from, Town* &t
for (int x = 0; x < num; x++) for (int x = 0; x < num; x++)
{ {
Order *order = vehicle->GetOrder(x); Order *order = vehicle->GetOrder(x);
if (order->GetType() != OT_GOTO_STATION) continue;
const DestinationID dest = order->GetDestination(); const DestinationID dest = order->GetDestination();
Town *town = GetTownFromDestination(dest); Town *town = GetTownFromDestination(dest);
if (order->GetType() == OT_GOTO_DEPOT) continue;
if (town != nullptr && unique_destinations.end() == std::find(unique_destinations.begin(), unique_destinations.end(), town)) if (town != nullptr && unique_destinations.end() == std::find(unique_destinations.begin(), unique_destinations.end(), town))
{ {
unique_destinations.push_back(town); unique_destinations.push_back(town);
@@ -807,18 +808,19 @@ CommandCost CmdCreateGroupAutoName(TileIndex tile, DoCommandFlag flags, uint32 p
if (vehicle == nullptr) return CMD_ERROR; if (vehicle == nullptr) return CMD_ERROR;
if (vehicle->owner != _current_company || !vehicle->IsPrimaryVehicle()) return CMD_ERROR; if (vehicle->owner != _current_company || !vehicle->IsPrimaryVehicle()) return CMD_ERROR;
if (!Group::CanAllocateItem()) return CMD_ERROR;
Town *town_from = nullptr;
Town *town_to = nullptr;
GetAutoGroupMostRelevantTowns(vehicle, town_from, town_to);
if (town_from == nullptr) return CMD_ERROR;
if (flags & DC_EXEC) { if (flags & DC_EXEC) {
Town *town_from = nullptr;
Town *town_to = nullptr;
GetAutoGroupMostRelevantTowns(vehicle, town_from, town_to);
if (town_from == nullptr) return CMD_ERROR;
const auto new_group = CreateVehicleAutoGroup(vehicle, town_from, town_to); const auto new_group = CreateVehicleAutoGroup(vehicle, town_from, town_to);
if (new_group == nullptr) return CMD_ERROR; assert(new_group != nullptr);
AddVehicleToGroup(vehicle, new_group->index); AddVehicleToGroup(vehicle, new_group->index);

View File

@@ -999,13 +999,13 @@ public:
} }
break; break;
} }
case WID_GL_CREATE_GROUP: { // make new group with vehicle specific name and add vehicle case WID_GL_CREATE_GROUP: { // make new group with auto generated vehicle specific name and add vehicle
const VehicleID vindex = this->vehicle_sel; const VehicleID vindex = this->vehicle_sel;
this->vehicle_sel = INVALID_VEHICLE; this->vehicle_sel = INVALID_VEHICLE;
this->group_over = INVALID_GROUP; this->group_over = INVALID_GROUP;
this->SetDirty(); this->SetDirty();
DoCommandP(0, vindex | (_ctrl_pressed ? 1 << 31 : 0),0 , CMD_CREATE_GROUP_AUTO_NAME, nullptr, nullptr); DoCommandP(0, vindex | (_ctrl_pressed ? 1U << 31 : 0),0 , CMD_CREATE_GROUP_AUTO_NAME | CMD_MSG(STR_ERROR_GROUP_CAN_T_CREATE), nullptr, nullptr);
break; break;
} }