dcs-retribution/game/sim/gameloop.py
Dan Albert 656a98675e 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
2021-12-23 17:46:24 -08:00

97 lines
3.4 KiB
Python

from __future__ import annotations
import logging
from datetime import datetime, timedelta
from pathlib import Path
from typing import TYPE_CHECKING
from .gamelooptimer import GameLoopTimer
from .gameupdatecallbacks import GameUpdateCallbacks
from .gameupdateevents import GameUpdateEvents
from .missionsimulation import MissionSimulation, SimulationAlreadyCompletedError
from .simspeedsetting import SimSpeedSetting
if TYPE_CHECKING:
from game import Game
from game.debriefing import Debriefing
class GameLoop:
def __init__(self, game: Game, callbacks: GameUpdateCallbacks) -> None:
self.game = game
self.callbacks = callbacks
self.timer = GameLoopTimer(self.tick)
self.sim = MissionSimulation(self.game)
self.events = GameUpdateEvents()
self.last_update_time = datetime.now()
self.started = False
self.completed = False
@property
def current_time_in_sim(self) -> datetime:
return self.sim.time
@property
def elapsed_time(self) -> timedelta:
return self.sim.time - self.game.conditions.start_time
def start(self) -> None:
if self.started:
raise RuntimeError("Cannot start game loop because it has already started")
self.started = True
self.sim.begin_simulation()
def pause(self) -> None:
self.set_simulation_speed(SimSpeedSetting.PAUSED)
def set_simulation_speed(self, simulation_speed: SimSpeedSetting) -> None:
self.timer.stop()
if simulation_speed != self.timer.simulation_speed:
logging.info(f"Speed changed to {simulation_speed}")
if not self.started:
self.start()
self.timer.set_speed(simulation_speed)
def run_to_first_contact(self) -> None:
self.pause()
logging.info("Running sim to first contact")
while not self.completed:
self.tick(suppress_events=True)
def pause_and_generate_miz(self, output: Path) -> None:
self.pause()
self.sim.generate_miz(output)
def pause_and_debrief(self, state_path: Path, force_end: bool) -> Debriefing:
self.pause()
return self.sim.debrief_current_state(state_path, force_end)
def complete_with_results(self, debriefing: Debriefing) -> None:
self.pause()
self.sim.process_results(debriefing)
self.completed = True
def send_update(self, rate_limit: bool) -> None:
now = datetime.now()
time_since_update = now - self.last_update_time
if not rate_limit or time_since_update >= timedelta(seconds=1 / 60):
self.callbacks.on_update(self.events)
self.events = GameUpdateEvents()
self.last_update_time = now
def tick(self, suppress_events: bool = False) -> None:
if not self.started:
raise RuntimeError("Attempted to tick game loop before initialization")
try:
self.sim.tick(self.events)
self.completed = self.events.simulation_complete
if not suppress_events:
self.send_update(rate_limit=True)
if self.completed:
self.pause()
self.send_update(rate_limit=False)
logging.info(f"Simulation completed at {self.sim.time}")
self.callbacks.on_simulation_complete()
except SimulationAlreadyCompletedError:
logging.exception("Attempted to tick already completed sim")