mirror of
https://github.com/dcs-retribution/dcs-retribution.git
synced 2025-11-10 15:41:24 +00:00
Rework sim status update to not need a thread.
Rather than polling at 60Hz (which may be faster than the tick rate, wasting cycles; and also makes synchronization annoying), collect events during the tick and emit them after (rate limited, pooling events until it is time for another event to send). This can be improved by paying attention to the aircraft update list, which would allow us to avoid updating aircraft that don't have a status change. To do that we need to be able to quickly lookup a FlightJs matching a Flight through, and Flight isn't hashable. We should also be removing dead events and de-duplicating. Currently each flight has an update for every tick, but only the latest one matters. Combat update events also don't matter if the same combat is new in the update. https://github.com/dcs-liberation/dcs_liberation/issues/1680
This commit is contained in:
@@ -11,6 +11,7 @@ from .flightstate import FlightState, Uninitialized
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from game.dcs.aircrafttype import AircraftType
|
||||
from game.sim.gameupdateevents import GameUpdateEvents
|
||||
from game.squadrons import Squadron, Pilot
|
||||
from game.theater import ControlPoint, MissionTarget
|
||||
from game.transfers import TransferOrder
|
||||
@@ -147,8 +148,10 @@ class Flight:
|
||||
def set_state(self, state: FlightState) -> None:
|
||||
self.state = state
|
||||
|
||||
def on_game_tick(self, time: datetime, duration: timedelta) -> None:
|
||||
self.state.on_game_tick(time, duration)
|
||||
def on_game_tick(
|
||||
self, events: GameUpdateEvents, time: datetime, duration: timedelta
|
||||
) -> None:
|
||||
self.state.on_game_tick(events, time, duration)
|
||||
|
||||
def should_halt_sim(self) -> bool:
|
||||
return self.state.should_halt_sim()
|
||||
|
||||
@@ -1,11 +1,19 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from .flightstate import FlightState
|
||||
from ..starttype import StartType
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from game.sim.gameupdateevents import GameUpdateEvents
|
||||
|
||||
|
||||
class Completed(FlightState):
|
||||
def on_game_tick(self, time: datetime, duration: timedelta) -> None:
|
||||
def on_game_tick(
|
||||
self, events: GameUpdateEvents, time: datetime, duration: timedelta
|
||||
) -> None:
|
||||
return
|
||||
|
||||
@property
|
||||
|
||||
@@ -9,6 +9,7 @@ from game.ato.starttype import StartType
|
||||
if TYPE_CHECKING:
|
||||
from game.ato.flight import Flight
|
||||
from game.settings import Settings
|
||||
from game.sim.gameupdateevents import GameUpdateEvents
|
||||
from game.threatzones import ThreatPoly
|
||||
|
||||
|
||||
@@ -18,7 +19,9 @@ class FlightState(ABC):
|
||||
self.settings = settings
|
||||
|
||||
@abstractmethod
|
||||
def on_game_tick(self, time: datetime, duration: timedelta) -> None:
|
||||
def on_game_tick(
|
||||
self, events: GameUpdateEvents, time: datetime, duration: timedelta
|
||||
) -> None:
|
||||
...
|
||||
|
||||
@property
|
||||
|
||||
@@ -11,6 +11,7 @@ from ..starttype import StartType
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from game.sim.combat import FrozenCombat
|
||||
from game.sim.gameupdateevents import GameUpdateEvents
|
||||
|
||||
|
||||
class InCombat(InFlight):
|
||||
@@ -32,7 +33,9 @@ class InCombat(InFlight):
|
||||
def estimate_speed(self) -> Speed:
|
||||
return self.previous_state.estimate_speed()
|
||||
|
||||
def on_game_tick(self, time: datetime, duration: timedelta) -> None:
|
||||
def on_game_tick(
|
||||
self, events: GameUpdateEvents, time: datetime, duration: timedelta
|
||||
) -> None:
|
||||
raise RuntimeError("Cannot simulate combat")
|
||||
|
||||
@property
|
||||
|
||||
@@ -17,6 +17,7 @@ from gen.flights.flightplan import LoiterFlightPlan
|
||||
if TYPE_CHECKING:
|
||||
from game.ato.flight import Flight
|
||||
from game.settings import Settings
|
||||
from game.sim.gameupdateevents import GameUpdateEvents
|
||||
|
||||
|
||||
class InFlight(FlightState, ABC):
|
||||
@@ -88,7 +89,9 @@ class InFlight(FlightState, ABC):
|
||||
def advance_to_next_waypoint(self) -> None:
|
||||
self.flight.set_state(self.next_waypoint_state())
|
||||
|
||||
def on_game_tick(self, time: datetime, duration: timedelta) -> None:
|
||||
def on_game_tick(
|
||||
self, events: GameUpdateEvents, time: datetime, duration: timedelta
|
||||
) -> None:
|
||||
self.elapsed_time += duration
|
||||
if self.elapsed_time > self.total_time_to_next_waypoint:
|
||||
self.advance_to_next_waypoint()
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from dcs import Point
|
||||
@@ -9,7 +10,7 @@ from game.ato.starttype import StartType
|
||||
from game.utils import Distance, LBS_TO_KG, Speed, meters
|
||||
|
||||
if TYPE_CHECKING:
|
||||
pass
|
||||
from game.sim.gameupdateevents import GameUpdateEvents
|
||||
|
||||
|
||||
def lerp(v0: float, v1: float, t: float) -> float:
|
||||
@@ -17,6 +18,12 @@ def lerp(v0: float, v1: float, t: float) -> float:
|
||||
|
||||
|
||||
class Navigating(InFlight):
|
||||
def on_game_tick(
|
||||
self, events: GameUpdateEvents, time: datetime, duration: timedelta
|
||||
) -> None:
|
||||
super().on_game_tick(events, time, duration)
|
||||
events.update_flight(self.flight)
|
||||
|
||||
def progress(self) -> float:
|
||||
return (
|
||||
self.elapsed_time.total_seconds()
|
||||
|
||||
@@ -11,6 +11,7 @@ from ..starttype import StartType
|
||||
if TYPE_CHECKING:
|
||||
from game.ato.flight import Flight
|
||||
from game.settings import Settings
|
||||
from game.sim.gameupdateevents import GameUpdateEvents
|
||||
|
||||
|
||||
class StartUp(FlightState):
|
||||
@@ -18,7 +19,9 @@ class StartUp(FlightState):
|
||||
super().__init__(flight, settings)
|
||||
self.completion_time = now + flight.flight_plan.estimate_startup()
|
||||
|
||||
def on_game_tick(self, time: datetime, duration: timedelta) -> None:
|
||||
def on_game_tick(
|
||||
self, events: GameUpdateEvents, time: datetime, duration: timedelta
|
||||
) -> None:
|
||||
if time < self.completion_time:
|
||||
return
|
||||
self.flight.set_state(Taxi(self.flight, self.settings, time))
|
||||
|
||||
@@ -12,6 +12,7 @@ from ...utils import LBS_TO_KG
|
||||
if TYPE_CHECKING:
|
||||
from game.ato.flight import Flight
|
||||
from game.settings import Settings
|
||||
from game.sim.gameupdateevents import GameUpdateEvents
|
||||
|
||||
|
||||
class Takeoff(FlightState):
|
||||
@@ -20,7 +21,9 @@ class Takeoff(FlightState):
|
||||
# TODO: Not accounted for in FlightPlan, can cause discrepancy without loiter.
|
||||
self.completion_time = now + timedelta(seconds=30)
|
||||
|
||||
def on_game_tick(self, time: datetime, duration: timedelta) -> None:
|
||||
def on_game_tick(
|
||||
self, events: GameUpdateEvents, time: datetime, duration: timedelta
|
||||
) -> None:
|
||||
if time < self.completion_time:
|
||||
return
|
||||
self.flight.set_state(Navigating(self.flight, self.settings, waypoint_index=0))
|
||||
|
||||
@@ -11,6 +11,7 @@ from ..starttype import StartType
|
||||
if TYPE_CHECKING:
|
||||
from game.ato.flight import Flight
|
||||
from game.settings import Settings
|
||||
from game.sim.gameupdateevents import GameUpdateEvents
|
||||
|
||||
|
||||
class Taxi(FlightState):
|
||||
@@ -18,7 +19,9 @@ class Taxi(FlightState):
|
||||
super().__init__(flight, settings)
|
||||
self.completion_time = now + flight.flight_plan.estimate_ground_ops()
|
||||
|
||||
def on_game_tick(self, time: datetime, duration: timedelta) -> None:
|
||||
def on_game_tick(
|
||||
self, events: GameUpdateEvents, time: datetime, duration: timedelta
|
||||
) -> None:
|
||||
if time < self.completion_time:
|
||||
return
|
||||
self.flight.set_state(Takeoff(self.flight, self.settings, time))
|
||||
|
||||
@@ -1,12 +1,20 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from gen.flights.traveltime import TotEstimator
|
||||
from .flightstate import FlightState
|
||||
from ..starttype import StartType
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from game.sim.gameupdateevents import GameUpdateEvents
|
||||
|
||||
|
||||
class Uninitialized(FlightState):
|
||||
def on_game_tick(self, time: datetime, duration: timedelta) -> None:
|
||||
def on_game_tick(
|
||||
self, events: GameUpdateEvents, time: datetime, duration: timedelta
|
||||
) -> None:
|
||||
raise RuntimeError("Attempted to simulate flight that is not fully initialized")
|
||||
|
||||
@property
|
||||
|
||||
@@ -13,6 +13,7 @@ from .taxi import Taxi
|
||||
if TYPE_CHECKING:
|
||||
from game.ato.flight import Flight
|
||||
from game.settings import Settings
|
||||
from game.sim.gameupdateevents import GameUpdateEvents
|
||||
|
||||
|
||||
class WaitingForStart(FlightState):
|
||||
@@ -29,7 +30,9 @@ class WaitingForStart(FlightState):
|
||||
def start_type(self) -> StartType:
|
||||
return self.flight.start_type
|
||||
|
||||
def on_game_tick(self, time: datetime, duration: timedelta) -> None:
|
||||
def on_game_tick(
|
||||
self, events: GameUpdateEvents, time: datetime, duration: timedelta
|
||||
) -> None:
|
||||
if time < self.start_time:
|
||||
return
|
||||
|
||||
|
||||
Reference in New Issue
Block a user