mirror of
https://github.com/dcs-retribution/dcs-retribution.git
synced 2025-11-10 15:41:24 +00:00
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
86 lines
2.9 KiB
Python
86 lines
2.9 KiB
Python
from __future__ import annotations
|
|
|
|
import itertools
|
|
from collections.abc import Iterator
|
|
from datetime import datetime, timedelta
|
|
|
|
from typing_extensions import TYPE_CHECKING
|
|
|
|
from game.ato import Flight
|
|
from game.ato.flightstate import (
|
|
Navigating,
|
|
StartUp,
|
|
Takeoff,
|
|
Taxi,
|
|
Uninitialized,
|
|
WaitingForStart,
|
|
)
|
|
from game.ato.starttype import StartType
|
|
from gen.flights.traveltime import TotEstimator
|
|
from .combat import CombatInitiator, FrozenCombat
|
|
|
|
if TYPE_CHECKING:
|
|
from game import Game
|
|
from .gameupdateevents import GameUpdateEvents
|
|
|
|
|
|
class AircraftSimulation:
|
|
def __init__(self, game: Game) -> None:
|
|
self.game = game
|
|
self.combats: list[FrozenCombat] = []
|
|
|
|
def begin_simulation(self) -> None:
|
|
self.reset()
|
|
self.set_initial_flight_states()
|
|
|
|
def on_game_tick(
|
|
self, events: GameUpdateEvents, time: datetime, duration: timedelta
|
|
) -> None:
|
|
for flight in self.iter_flights():
|
|
flight.on_game_tick(events, time, duration)
|
|
|
|
# Finish updating all flights before checking for combat so that the new
|
|
# positions are used.
|
|
CombatInitiator(self.game, self.combats, events).update_active_combats()
|
|
|
|
# After updating all combat states, check for halts.
|
|
for flight in self.iter_flights():
|
|
if flight.should_halt_sim():
|
|
events.complete_simulation()
|
|
return
|
|
|
|
def set_initial_flight_states(self) -> None:
|
|
now = self.game.conditions.start_time
|
|
for flight in self.iter_flights():
|
|
estimator = TotEstimator(flight.package)
|
|
start_time = estimator.mission_start_time(flight)
|
|
if start_time <= timedelta():
|
|
self.set_active_flight_state(flight, now)
|
|
else:
|
|
flight.set_state(
|
|
WaitingForStart(flight, self.game.settings, now + start_time)
|
|
)
|
|
|
|
def set_active_flight_state(self, flight: Flight, now: datetime) -> None:
|
|
if flight.start_type is StartType.COLD:
|
|
flight.set_state(StartUp(flight, self.game.settings, now))
|
|
elif flight.start_type is StartType.WARM:
|
|
flight.set_state(Taxi(flight, self.game.settings, now))
|
|
elif flight.start_type is StartType.RUNWAY:
|
|
flight.set_state(Takeoff(flight, self.game.settings, now))
|
|
elif flight.start_type is StartType.IN_FLIGHT:
|
|
flight.set_state(Navigating(flight, self.game.settings, waypoint_index=0))
|
|
else:
|
|
raise ValueError(f"Unknown start type {flight.start_type} for {flight}")
|
|
|
|
def reset(self) -> None:
|
|
for flight in self.iter_flights():
|
|
flight.set_state(Uninitialized(flight, self.game.settings))
|
|
|
|
def iter_flights(self) -> Iterator[Flight]:
|
|
packages = itertools.chain(
|
|
self.game.blue.ato.packages, self.game.red.ato.packages
|
|
)
|
|
for package in packages:
|
|
yield from package.flights
|