Fix ForceGroup merging and PresetGroup handling

This commit is contained in:
RndName 2022-03-22 19:08:24 +01:00
parent d0fe058a24
commit 892bd9f069
3 changed files with 29 additions and 28 deletions

View File

@ -28,7 +28,7 @@ class ArmedForces:
for force_group in self.forces:
if force_group.tasks == new_group.tasks:
# Update existing group if tasks are equal
force_group.update_group(new_group)
force_group.merge_group(new_group)
return
# Add a new force group
self.forces.append(new_group)
@ -38,7 +38,10 @@ class ArmedForces:
This will create a ForceGroup for each generic Layout and PresetGroup"""
# Initialize with preset_groups from the faction
self.forces = [preset_group for preset_group in faction.preset_groups]
self.forces = [
preset_group.initialize_for_faction(faction)
for preset_group in faction.preset_groups
]
# Generate ForceGroup for all generic layouts by iterating over
# all layouts which are usable by the given faction.

View File

@ -92,26 +92,24 @@ class ForceGroup:
return True
return False
@classmethod
def for_faction_by_name(cls, name: str, faction: Faction) -> ForceGroup:
"""Load a PresetGroup as ForceGroup with faction sensitive handling"""
force_group = cls.named(name)
for layout in force_group.layouts:
for groups in layout.groups.values():
for group in groups:
if group.fill and not force_group.has_unit_for_layout_group(group):
for unit_type in group.possible_types_for_faction(faction):
if issubclass(unit_type, VehicleType):
force_group.units.append(
next(GroundUnitType.for_dcs_type(unit_type))
)
elif issubclass(unit_type, ShipType):
force_group.units.append(
next(ShipUnitType.for_dcs_type(unit_type))
)
elif issubclass(unit_type, StaticType):
force_group.statics.append(unit_type)
return force_group
def initialize_for_faction(self, faction: Faction) -> ForceGroup:
"""Initialize a ForceGroup for the given Faction.
This adds accessible units to LayoutGroups with the fill property"""
for layout in self.layouts:
for group in layout.all_groups:
if group.fill and not self.has_unit_for_layout_group(group):
for unit_type in group.possible_types_for_faction(faction):
if issubclass(unit_type, VehicleType):
self.units.append(
next(GroundUnitType.for_dcs_type(unit_type))
)
elif issubclass(unit_type, ShipType):
self.units.append(
next(ShipUnitType.for_dcs_type(unit_type))
)
elif issubclass(unit_type, StaticType):
self.statics.append(unit_type)
return self
@classmethod
def named(cls, name: str) -> ForceGroup:
@ -159,9 +157,11 @@ class ForceGroup:
"""Return random DCS Unit Type which can be used in the given TgoLayoutGroup"""
return random.choice(self.dcs_unit_types_for_group(group))
def update_group(self, new_group: ForceGroup) -> None:
"""Update the group from another group.
This will merge units, statics and layouts."""
def merge_group(self, new_group: ForceGroup) -> None:
"""Merge the group with another similar group."""
# Unified name for the resulting group
self.name = ", ".join([t.description for t in self.tasks])
# merge units, statics and layouts
self.units = list(set(self.units + new_group.units))
self.statics = list(set(self.statics + new_group.statics))
self.layouts = list(set(self.layouts + new_group.layouts))

View File

@ -214,10 +214,8 @@ class Faction:
ShipUnitType.named(n) for n in json.get("naval_units", [])
]
# This has to be loaded AFTER GroundUnitType and ShipUnitType to work properly
faction.preset_groups = [
ForceGroup.for_faction_by_name(n, faction)
for n in json.get("preset_groups", [])
ForceGroup.named(n) for n in json.get("preset_groups", [])
]
faction.requirements = json.get("requirements", {})