Dan Albert 5db1b94ac4 Add option to fast forward to first contact.
This is the first step in a larger project to add play/pause buttons to
the Liberation UI so the mission can be generated at any point.
docs/design/turnless.md describes the plan.

This adds an option to fast forward the turn to first contact before
generating the mission. None of that is reflected in the UI (for now),
but the miz will be generated with many flights in the air.

For now "first contact" means as soon as any flight reaches its IP. I'll
follow up to add threat checking so that air-to-air combat also triggers
this, as will entering a SAM's threat zone.

This also includes an option to halt fast-forward whenever a player
flight reaches a certain mission-prep phase. This can be used to avoid
fast forwarding past the player's startup time, taxi time, or takeoff
time. By default this option is disabled so player aircraft may start in
the air (possibly even at their IP if they're the first mission to reach
IP).

Fuel states do not currently account for distance traveled during fast
forward. That will come later.

https://github.com/dcs-liberation/dcs_liberation/issues/1681
2021-10-24 17:40:45 -07:00

135 lines
5.1 KiB
Python

from __future__ import annotations
import logging
from datetime import datetime, timedelta
from typing import TYPE_CHECKING
from dcs import Point
from game.ato.flightstate import Completed
from game.ato.flightstate.flightstate import FlightState
from game.ato.flightwaypointtype import FlightWaypointType
from game.ato.starttype import StartType
from game.utils import Distance, Speed, meters
from gen.flights.flightplan import LoiterFlightPlan, PatrollingFlightPlan
if TYPE_CHECKING:
from game.ato.flight import Flight
from game.settings import Settings
def lerp(v0: float, v1: float, t: float) -> float:
return (1 - t) * v0 + t * v1
class InFlight(FlightState):
def __init__(self, flight: Flight, settings: Settings) -> None:
super().__init__(flight, settings)
self.waypoints = flight.flight_plan.iter_waypoints()
# TODO: Error checking for stupid flight plans with fewer than two waypoints.
self.current_waypoint = next(self.waypoints)
self.next_waypoint = next(self.waypoints)
self.passed_waypoints = {self.current_waypoint}
self.total_time_to_next_waypoint = self._total_time()
self.elapsed_time = timedelta()
def _total_time(self) -> timedelta:
if isinstance(self.flight.flight_plan, PatrollingFlightPlan):
# Racetracks should remain at the first waypoint until the patrol ends so
# that the waypoint generation doesn't need to reverse the orbit direction.
if self.current_waypoint == self.flight.flight_plan.patrol_start:
return self.flight.flight_plan.patrol_duration
# Loiter time is already built into travel_time_between_waypoints.
return self.flight.flight_plan.travel_time_between_waypoints(
self.current_waypoint, self.next_waypoint
)
def progress(self) -> float:
return (
self.elapsed_time.total_seconds()
/ self.total_time_to_next_waypoint.total_seconds()
)
def estimate_position(self) -> Point:
# TODO: Make Loiter and RaceTrack distinct FlightStates.
if isinstance(self.flight.flight_plan, PatrollingFlightPlan):
# Prevent spawning racetracks in the middle of a leg. For simplicity we
# always start the aircraft at the beginning of the racetrack.
if self.current_waypoint == self.flight.flight_plan.patrol_start:
return self.current_waypoint.position
elif isinstance(self.flight.flight_plan, LoiterFlightPlan):
if (
self.current_waypoint == self.flight.flight_plan.hold
and self.elapsed_time < self.flight.flight_plan.hold_duration
):
return self.current_waypoint.position
x0 = self.current_waypoint.position.x
y0 = self.current_waypoint.position.y
x1 = self.next_waypoint.position.x
y1 = self.next_waypoint.position.y
progress = self.progress()
return Point(lerp(x0, x1, progress), lerp(y0, y1, progress))
def estimate_altitude(self) -> tuple[Distance, str]:
# TODO: Should count progress as 0 until departing a hold.
return (
meters(
lerp(
self.current_waypoint.alt.meters,
self.next_waypoint.alt.meters,
self.progress(),
)
),
self.current_waypoint.alt_type,
)
def estimate_speed(self) -> Speed:
# TODO: Patrol/loiter speeds may be different.
return self.flight.flight_plan.speed_between_waypoints(
self.current_waypoint, self.next_waypoint
)
def update_waypoints(self) -> None:
self.current_waypoint = self.next_waypoint
self.passed_waypoints.add(self.current_waypoint)
try:
self.next_waypoint = next(self.waypoints)
except StopIteration:
self.flight.set_state(Completed(self.flight, self.settings))
self.total_time_to_next_waypoint = self._total_time()
self.elapsed_time = timedelta()
def on_game_tick(self, time: datetime, duration: timedelta) -> None:
self.elapsed_time += duration
if self.elapsed_time > self.total_time_to_next_waypoint:
self.update_waypoints()
def should_halt_sim(self) -> bool:
contact_types = {
FlightWaypointType.INGRESS_BAI,
FlightWaypointType.INGRESS_CAS,
FlightWaypointType.INGRESS_DEAD,
FlightWaypointType.INGRESS_OCA_AIRCRAFT,
FlightWaypointType.INGRESS_OCA_RUNWAY,
FlightWaypointType.INGRESS_SEAD,
FlightWaypointType.INGRESS_STRIKE,
}
# TODO: Check against enemy threats.
if self.current_waypoint.waypoint_type in contact_types:
logging.info(
f"Interrupting simulation because {self.flight} has reached its "
"ingress point"
)
return True
return False
@property
def is_waiting_for_start(self) -> bool:
return False
@property
def spawn_type(self) -> StartType:
return StartType.IN_FLIGHT