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

71 lines
1.9 KiB
Python

from __future__ import annotations
from datetime import datetime, timedelta
from typing import TYPE_CHECKING
from dcs import Point
from game.utils import Distance, Speed
from .inflight import InFlight
from ..starttype import StartType
if TYPE_CHECKING:
from game.sim.combat import FrozenCombat
from game.sim.gameupdateevents import GameUpdateEvents
class InCombat(InFlight):
def __init__(self, previous_state: InFlight, combat: FrozenCombat) -> None:
super().__init__(
previous_state.flight,
previous_state.settings,
previous_state.waypoint_index,
)
self.previous_state = previous_state
self.combat = combat
def estimate_position(self) -> Point:
return self.previous_state.estimate_position()
def estimate_altitude(self) -> tuple[Distance, str]:
return self.previous_state.estimate_altitude()
def estimate_speed(self) -> Speed:
return self.previous_state.estimate_speed()
def on_game_tick(
self, events: GameUpdateEvents, time: datetime, duration: timedelta
) -> None:
raise RuntimeError("Cannot simulate combat")
@property
def is_at_ip(self) -> bool:
return False
@property
def is_waiting_for_start(self) -> bool:
return False
def should_halt_sim(self) -> bool:
return True
@property
def vulnerable_to_intercept(self) -> bool:
# Interception results in the interceptor joining the existing combat rather
# than creating a new combat.
return False
@property
def vulnerable_to_sam(self) -> bool:
# SAM contact results in the SAM joining the existing combat rather than
# creating a new combat.
return False
@property
def spawn_type(self) -> StartType:
return StartType.IN_FLIGHT
@property
def description(self) -> str:
return self.combat.describe()