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:
Dan Albert
2021-12-23 17:46:24 -08:00
parent 43d5dc0528
commit 656a98675e
23 changed files with 146 additions and 114 deletions

View File

@@ -12,7 +12,7 @@ from .atip import AtIp
from .defendingsam import DefendingSam
from .joinablecombat import JoinableCombat
from .samengagementzones import SamEngagementZones
from ..gameupdatecallbacks import GameUpdateCallbacks
from ..gameupdateevents import GameUpdateEvents
if TYPE_CHECKING:
from game import Game
@@ -22,11 +22,11 @@ if TYPE_CHECKING:
class CombatInitiator:
def __init__(
self, game: Game, combats: list[FrozenCombat], callbacks: GameUpdateCallbacks
self, game: Game, combats: list[FrozenCombat], events: GameUpdateEvents
) -> None:
self.game = game
self.combats = combats
self.callbacks = callbacks
self.events = events
def update_active_combats(self) -> None:
blue_a2a = AircraftEngagementZones.from_ato(self.game.blue.ato)
@@ -64,7 +64,7 @@ class CombatInitiator:
logging.info(f"{flight} is joining existing combat {joined}")
joined.join(flight)
own_a2a.remove_flight(flight)
self.callbacks.on_combat_changed(joined)
self.events.update_combat(joined)
elif (combat := self.check_flight_for_new_combat(flight, a2a, sam)) is not None:
logging.info(f"Interrupting simulation because {combat.because()}")
combat.update_flight_states()
@@ -75,7 +75,7 @@ class CombatInitiator:
a2a.update_for_combat(combat)
own_a2a.update_for_combat(combat)
self.combats.append(combat)
self.callbacks.on_add_combat(combat)
self.events.new_combat(combat)
def check_flight_for_joined_combat(
self, flight: Flight