From 9839787b6d137e23a9962b45e38a4bd5fc7419e3 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Tue, 26 Oct 2021 23:06:58 -0700 Subject: [PATCH] Interrupt fast-forward on air defense contact. https://github.com/dcs-liberation/dcs_liberation/issues/1681 --- game/ato/flight.py | 4 +++- game/ato/flightstate/inflight.py | 9 +++++++++ game/sim/aircraftsimulation.py | 3 ++- game/threatzones.py | 4 ++++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/game/ato/flight.py b/game/ato/flight.py index f611f31b..978079c0 100644 --- a/game/ato/flight.py +++ b/game/ato/flight.py @@ -134,6 +134,8 @@ class Flight: def set_state(self, state: FlightState) -> None: self.state = state - def on_game_tick(self, time: datetime, duration: timedelta) -> bool: + def on_game_tick(self, time: datetime, duration: timedelta) -> None: self.state.on_game_tick(time, duration) + + def should_halt_sim(self) -> bool: return self.state.should_halt_sim() diff --git a/game/ato/flightstate/inflight.py b/game/ato/flightstate/inflight.py index 1ba08710..0b543364 100644 --- a/game/ato/flightstate/inflight.py +++ b/game/ato/flightstate/inflight.py @@ -123,6 +123,15 @@ class InFlight(FlightState): "ingress point" ) return True + + threat_zone = self.flight.squadron.coalition.opponent.threat_zone + if threat_zone.threatened_by_air_defense(self.estimate_position()): + logging.info( + f"Interrupting simulation because {self.flight} has encountered enemy " + "air defenses" + ) + return True + return False @property diff --git a/game/sim/aircraftsimulation.py b/game/sim/aircraftsimulation.py index ae48c2c8..e7b17b2d 100644 --- a/game/sim/aircraftsimulation.py +++ b/game/sim/aircraftsimulation.py @@ -47,7 +47,8 @@ class AircraftSimulation: def tick(self) -> bool: interrupt_sim = False for flight in self.iter_flights(): - if flight.on_game_tick(self.time, TICK): + flight.on_game_tick(self.time, TICK) + if flight.should_halt_sim(): interrupt_sim = True # TODO: Check for SAM or A2A contact. diff --git a/game/threatzones.py b/game/threatzones.py index 4cc2a16d..f916bd8e 100644 --- a/game/threatzones.py +++ b/game/threatzones.py @@ -104,6 +104,10 @@ class ThreatZones: def _threatened_by_air_defense_geom(self, position: BaseGeometry) -> bool: return self.air_defenses.intersects(position) + @threatened_by_air_defense.register + def _threatened_by_air_defense_dcs_point(self, position: DcsPoint) -> bool: + return self.threatened_by_air_defense(self.dcs_to_shapely_point(position)) + @threatened_by_air_defense.register def _threatened_by_air_defense_flight(self, flight: Flight) -> bool: return self.threatened_by_air_defense(