Feature: Create group of vehicles from manage vehicle list button. (#10890)

This commit is contained in:
PeterN
2023-06-18 20:48:04 +01:00
committed by GitHub
parent 70de70bdcd
commit 2a2443dd01
13 changed files with 104 additions and 52 deletions

View File

@@ -512,48 +512,63 @@ static void AddVehicleToGroup(Vehicle *v, GroupID new_g)
* @param add_shared Add shared vehicles as well.
* @return the cost of this operation or an error
*/
std::tuple<CommandCost, GroupID> CmdAddVehicleGroup(DoCommandFlag flags, GroupID group_id, VehicleID veh_id, bool add_shared)
std::tuple<CommandCost, GroupID> CmdAddVehicleGroup(DoCommandFlag flags, GroupID group_id, VehicleID veh_id, bool add_shared, const VehicleListIdentifier &vli)
{
Vehicle *v = Vehicle::GetIfValid(veh_id);
GroupID new_g = group_id;
if (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g) && new_g != NEW_GROUP) return { CMD_ERROR, INVALID_GROUP };
if (v == nullptr || (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g) && new_g != NEW_GROUP)) return { CMD_ERROR, INVALID_GROUP };
VehicleList list;
if (veh_id == INVALID_VEHICLE && vli.Valid()) {
if (!GenerateVehicleSortList(&list, vli) || list.empty()) return { CMD_ERROR, INVALID_GROUP };
} else {
Vehicle *v = Vehicle::GetIfValid(veh_id);
if (v == nullptr) return { CMD_ERROR, INVALID_GROUP };
list.push_back(v);
}
VehicleType vtype = list.front()->type;
for (const Vehicle *v : list) {
if (v->owner != _current_company || !v->IsPrimaryVehicle()) return { CMD_ERROR, INVALID_GROUP };
}
if (Group::IsValidID(new_g)) {
Group *g = Group::Get(new_g);
if (g->owner != _current_company || g->vehicle_type != v->type) return { CMD_ERROR, INVALID_GROUP };
if (g->owner != _current_company || g->vehicle_type != vtype) return { CMD_ERROR, INVALID_GROUP };
}
if (v->owner != _current_company || !v->IsPrimaryVehicle()) return { CMD_ERROR, INVALID_GROUP };
if (new_g == NEW_GROUP) {
/* Create new group. */
auto [ret, new_group_id] = CmdCreateGroup(flags, v->type, INVALID_GROUP);
auto [ret, new_group_id] = CmdCreateGroup(flags, vtype, INVALID_GROUP);
if (ret.Failed()) return { ret, new_group_id };
new_g = new_group_id;
}
if (flags & DC_EXEC) {
AddVehicleToGroup(v, new_g);
for (const Vehicle *vc : list) {
/* VehicleList is const but we need to modify the vehicle. */
Vehicle *v = Vehicle::Get(vc->index);
AddVehicleToGroup(v, new_g);
if (add_shared) {
/* Add vehicles in the shared order list as well. */
for (Vehicle *v2 = v->FirstShared(); v2 != nullptr; v2 = v2->NextShared()) {
if (v2->group_id != new_g) AddVehicleToGroup(v2, new_g);
if (add_shared) {
/* Add vehicles in the shared order list as well. */
for (Vehicle *v2 = v->FirstShared(); v2 != nullptr; v2 = v2->NextShared()) {
if (v2->group_id != new_g) AddVehicleToGroup(v2, new_g);
}
}
SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
SetWindowDirty(WC_VEHICLE_VIEW, v->index);
SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
InvalidateWindowData(WC_VEHICLE_VIEW, v->index);
InvalidateWindowData(WC_VEHICLE_DETAILS, v->index);
}
GroupStatistics::UpdateAutoreplace(v->owner);
GroupStatistics::UpdateAutoreplace(_current_company);
/* Update the Replace Vehicle Windows */
SetWindowDirty(WC_REPLACE_VEHICLE, v->type);
SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
SetWindowDirty(WC_VEHICLE_VIEW, v->index);
SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
InvalidateWindowData(GetWindowClassForVehicleType(v->type), VehicleListIdentifier(VL_GROUP_LIST, v->type, _current_company).Pack());
InvalidateWindowData(WC_VEHICLE_VIEW, v->index);
InvalidateWindowData(WC_VEHICLE_DETAILS, v->index);
SetWindowDirty(WC_REPLACE_VEHICLE, vtype);
InvalidateWindowData(GetWindowClassForVehicleType(vtype), VehicleListIdentifier(VL_GROUP_LIST, vtype, _current_company).Pack());
}
return { CommandCost(), new_g };
@@ -579,7 +594,7 @@ CommandCost CmdAddSharedVehicleGroup(DoCommandFlag flags, GroupID id_g, VehicleT
/* For each shared vehicles add it to the group */
for (Vehicle *v2 = v->FirstShared(); v2 != nullptr; v2 = v2->NextShared()) {
if (v2->group_id != id_g) Command<CMD_ADD_VEHICLE_GROUP>::Do(flags, id_g, v2->index, false);
if (v2->group_id != id_g) Command<CMD_ADD_VEHICLE_GROUP>::Do(flags, id_g, v2->index, false, VehicleListIdentifier{});
}
}
}
@@ -610,7 +625,7 @@ CommandCost CmdRemoveAllVehiclesGroup(DoCommandFlag flags, GroupID group_id)
if (v->group_id != group_id) continue;
/* Add The Vehicle to the default group */
Command<CMD_ADD_VEHICLE_GROUP>::Do(flags, DEFAULT_GROUP, v->index, false);
Command<CMD_ADD_VEHICLE_GROUP>::Do(flags, DEFAULT_GROUP, v->index, false, VehicleListIdentifier{});
}
}