mirror of
https://github.com/dcs-retribution/dcs-retribution.git
synced 2025-11-10 15:41:24 +00:00
This is an attempt to remove a lot of our supposedly unnecessary error handling. Every aircraft should have a price, a description, a name, etc; and none of those should require carrying around the faction's country as context. This moves all the data for aircraft into yaml files (only one converted here as an example). Most of the "extended unit info" isn't actually being read yet. To replace the renaming of units based on the county, we instead generate multiple types of each unit when necessary. The CF-18 is just as much a first-class type as the F/A-18 is. This doesn't work in its current state because it does break all the existing names for aircraft that are used in the faction and squadron files, and we no longer let those errors go as a warning. It will be an annoying one time switch, but it allows us to define the names that get used in these files instead of being sensitive to changes as they happen in pydcs, and allows faction designers to specifically choose, for example, the Su-22 instead of the Su-17. One thing not handled by this is aircraft task capability. This is because the lists in ai_flight_planner_db.py are a priority list, and to move it out to a yaml file we'd need to assign a weight to it that would be used to stack rank each aircraft. That's doable, but it makes it much more difficult to see the ordering of aircraft at a glance, and much more annoying to move aircraft around in the priority list. I don't think this is worth doing, and the priority lists will remain in their own separate lists. This includes the converted I used to convert all the old unit info and factions to the new format. This doesn't need to live long, but we may want to reuse it in the future so we want it in the version history.
132 lines
4.6 KiB
Python
132 lines
4.6 KiB
Python
"""Inventory management APIs."""
|
|
from __future__ import annotations
|
|
|
|
from collections import defaultdict
|
|
from typing import Dict, Iterable, Iterator, Set, Tuple, TYPE_CHECKING, Type
|
|
|
|
from dcs.unittype import FlyingType
|
|
|
|
from game.dcs.aircrafttype import AircraftType
|
|
from gen.flights.flight import Flight
|
|
|
|
if TYPE_CHECKING:
|
|
from game.theater import ControlPoint
|
|
|
|
|
|
class ControlPointAircraftInventory:
|
|
"""Aircraft inventory for a single control point."""
|
|
|
|
def __init__(self, control_point: ControlPoint) -> None:
|
|
self.control_point = control_point
|
|
self.inventory: Dict[AircraftType, int] = defaultdict(int)
|
|
|
|
def add_aircraft(self, aircraft: AircraftType, count: int) -> None:
|
|
"""Adds aircraft to the inventory.
|
|
|
|
Args:
|
|
aircraft: The type of aircraft to add.
|
|
count: The number of aircraft to add.
|
|
"""
|
|
self.inventory[aircraft] += count
|
|
|
|
def remove_aircraft(self, aircraft: AircraftType, count: int) -> None:
|
|
"""Removes aircraft from the inventory.
|
|
|
|
Args:
|
|
aircraft: The type of aircraft to remove.
|
|
count: The number of aircraft to remove.
|
|
|
|
Raises:
|
|
ValueError: The control point cannot fulfill the requested number of
|
|
aircraft.
|
|
"""
|
|
available = self.inventory[aircraft]
|
|
if available < count:
|
|
raise ValueError(
|
|
f"Cannot remove {count} {aircraft} from "
|
|
f"{self.control_point.name}. Only have {available}."
|
|
)
|
|
self.inventory[aircraft] -= count
|
|
|
|
def available(self, aircraft: AircraftType) -> int:
|
|
"""Returns the number of available aircraft of the given type.
|
|
|
|
Args:
|
|
aircraft: The type of aircraft to query.
|
|
"""
|
|
try:
|
|
return self.inventory[aircraft]
|
|
except KeyError:
|
|
return 0
|
|
|
|
@property
|
|
def types_available(self) -> Iterator[AircraftType]:
|
|
"""Iterates over all available aircraft types."""
|
|
for aircraft, count in self.inventory.items():
|
|
if count > 0:
|
|
yield aircraft
|
|
|
|
@property
|
|
def all_aircraft(self) -> Iterator[Tuple[AircraftType, int]]:
|
|
"""Iterates over all available aircraft types, including amounts."""
|
|
for aircraft, count in self.inventory.items():
|
|
if count > 0:
|
|
yield aircraft, count
|
|
|
|
def clear(self) -> None:
|
|
"""Clears all aircraft from the inventory."""
|
|
self.inventory.clear()
|
|
|
|
|
|
class GlobalAircraftInventory:
|
|
"""Game-wide aircraft inventory."""
|
|
|
|
def __init__(self, control_points: Iterable[ControlPoint]) -> None:
|
|
self.inventories: Dict[ControlPoint, ControlPointAircraftInventory] = {
|
|
cp: ControlPointAircraftInventory(cp) for cp in control_points
|
|
}
|
|
|
|
def reset(self) -> None:
|
|
"""Clears all control points and their inventories."""
|
|
for inventory in self.inventories.values():
|
|
inventory.clear()
|
|
|
|
def set_from_control_point(self, control_point: ControlPoint) -> None:
|
|
"""Set the control point's aircraft inventory.
|
|
|
|
If the inventory for the given control point has already been set for
|
|
the turn, it will be overwritten.
|
|
"""
|
|
inventory = self.inventories[control_point]
|
|
for aircraft, count in control_point.base.aircraft.items():
|
|
inventory.add_aircraft(aircraft, count)
|
|
|
|
def for_control_point(
|
|
self, control_point: ControlPoint
|
|
) -> ControlPointAircraftInventory:
|
|
"""Returns the inventory specific to the given control point."""
|
|
return self.inventories[control_point]
|
|
|
|
@property
|
|
def available_types_for_player(self) -> Iterator[AircraftType]:
|
|
"""Iterates over all aircraft types available to the player."""
|
|
seen: Set[AircraftType] = set()
|
|
for control_point, inventory in self.inventories.items():
|
|
if control_point.captured:
|
|
for aircraft in inventory.types_available:
|
|
if not control_point.can_operate(aircraft):
|
|
continue
|
|
if aircraft not in seen:
|
|
seen.add(aircraft)
|
|
yield aircraft
|
|
|
|
def claim_for_flight(self, flight: Flight) -> None:
|
|
"""Removes aircraft from the inventory for the given flight."""
|
|
inventory = self.for_control_point(flight.from_cp)
|
|
inventory.remove_aircraft(flight.unit_type, flight.count)
|
|
|
|
def return_from_flight(self, flight: Flight) -> None:
|
|
"""Returns a flight's aircraft to the inventory."""
|
|
inventory = self.for_control_point(flight.from_cp)
|
|
inventory.add_aircraft(flight.unit_type, flight.count)
|