From bd60760f9dae191c54d80375043f059bd8d71965 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Fri, 27 Nov 2020 17:42:59 -0800 Subject: [PATCH] Merge faction sams and shorads into air_defenses. Fixes https://github.com/Khopa/dcs_liberation/issues/473. Air defenses for bases, strike locations, and fixed IADS will now all downgrade to lower tier systems as needed. Strike locations will still be spawned as an equally weighted random generator from either the medium or long range groups, but will use a short range system if none are available to the faction. I've made the change in a way that leaves factions compatible, but will follow up to clean up our built-in factions. --- game/factions/faction.py | 14 ++++++++------ game/theater/start_generator.py | 4 ++-- gen/sam/sam_group_generator.py | 13 +------------ tests/test_factions.py | 4 ++-- 4 files changed, 13 insertions(+), 22 deletions(-) diff --git a/game/factions/faction.py b/game/factions/faction.py index 45071530..9722c11b 100644 --- a/game/factions/faction.py +++ b/game/factions/faction.py @@ -51,11 +51,8 @@ class Faction: # Logistics units used logistics_units: List[VehicleType] = field(default_factory=list) - # List of units that can be deployed as SHORAD - shorads: List[str] = field(default_factory=list) - # Possible SAMS site generators for this faction - sams: List[str] = field(default_factory=list) + air_defenses: List[str] = field(default_factory=list) # Possible EWR generators for this faction. ewrs: List[str] = field(default_factory=list) @@ -146,9 +143,14 @@ class Faction: faction.logistics_units = load_all_vehicles( json.get("logistics_units", [])) - faction.sams = json.get("sams", []) faction.ewrs = json.get("ewrs", []) - faction.shorads = json.get("shorads", []) + + faction.air_defenses = json.get("air_defenses", []) + # Compatibility for older factions. All air defenses now belong to a + # single group and the generator decides what belongs where. + faction.air_defenses.extend(json.get("sams", [])) + faction.air_defenses.extend(json.get("shorads", [])) + faction.missiles = json.get("missiles", []) faction.requirements = json.get("requirements", {}) diff --git a/game/theater/start_generator.py b/game/theater/start_generator.py index ea0331c0..123b1c87 100644 --- a/game/theater/start_generator.py +++ b/game/theater/start_generator.py @@ -39,7 +39,6 @@ from gen.sam.airdefensegroupgenerator import AirDefenseRange from gen.sam.sam_group_generator import ( generate_anti_air_group, generate_ewr_group, - generate_shorad_group, ) from . import ( ConflictTheater, @@ -530,7 +529,8 @@ class BaseDefenseGenerator: g = SamGroundObject(namegen.random_objective_name(), group_id, position, self.control_point, for_airbase=True) - group = generate_shorad_group(self.game, g, self.faction) + group = generate_anti_air_group(self.game, g, self.faction, + ranges=[{AirDefenseRange.Short}]) if group is None: logging.error( f"Could not generate SHORAD group at {self.control_point}") diff --git a/gen/sam/sam_group_generator.py b/gen/sam/sam_group_generator.py index 3b678467..366c63e7 100644 --- a/gen/sam/sam_group_generator.py +++ b/gen/sam/sam_group_generator.py @@ -156,7 +156,7 @@ def get_faction_possible_sams_generator( Return the list of possible SAM generator for the given faction :param faction: Faction name to search units for """ - return [SAM_MAP[s] for s in faction.sams] + return [SAM_MAP[s] for s in faction.air_defenses] def get_faction_possible_ewrs_generator(faction: Faction) -> List[Type[GroupGenerator]]: @@ -233,14 +233,3 @@ def generate_ewr_group(game: Game, ground_object: TheaterGroundObject, generator.generate() return generator.get_generated_group() return None - - -def generate_shorad_group(game: Game, ground_object: SamGroundObject, - faction: Faction) -> Optional[VehicleGroup]: - if len(faction.shorads) > 0: - sam = random.choice(faction.shorads) - generator = SAM_MAP[sam](game, ground_object) - generator.generate() - return generator.get_generated_group() - else: - return generate_anti_air_group(game, ground_object, faction) diff --git a/tests/test_factions.py b/tests/test_factions.py index c5de3e94..f802dbf0 100644 --- a/tests/test_factions.py +++ b/tests/test_factions.py @@ -71,9 +71,9 @@ class TestFactionLoader(unittest.TestCase): self.assertIn(Infantry.Infantry_M4, faction.infantry_units) self.assertIn(Infantry.Soldier_M249, faction.infantry_units) - self.assertIn("AvengerGenerator", faction.shorads) + self.assertIn("AvengerGenerator", faction.air_defenses) - self.assertIn("HawkGenerator", faction.sams) + self.assertIn("HawkGenerator", faction.air_defenses) self.assertIn(CVN_74_John_C__Stennis, faction.aircraft_carrier) self.assertIn(LHA_1_Tarawa, faction.helicopter_carrier)