Add a CLI tool for viewing default loadouts.

This commit is contained in:
Dan Albert
2022-05-29 15:23:21 -07:00
parent c5efc908de
commit 22c3d4ebc5
6 changed files with 165 additions and 18 deletions

View File

@@ -2,10 +2,13 @@ from __future__ import annotations
import datetime
from collections.abc import Iterable
from typing import Iterator, Mapping, Optional, TYPE_CHECKING
from typing import Iterator, Mapping, Optional, TYPE_CHECKING, Type
from dcs.unittype import FlyingType
from game.data.weapons import Pylon, Weapon, WeaponType
from game.dcs.aircrafttype import AircraftType
from .flighttype import FlightType
if TYPE_CHECKING:
from .flight import Flight
@@ -103,6 +106,10 @@ class Loadout:
@classmethod
def iter_for(cls, flight: Flight) -> Iterator[Loadout]:
return cls.iter_for_aircraft(flight.unit_type)
@classmethod
def iter_for_aircraft(cls, aircraft: AircraftType) -> Iterator[Loadout]:
# Dict of payload ID (numeric) to:
#
# {
@@ -111,7 +118,7 @@ class Loadout:
# {"CLSID": class ID, "num": pylon number}
# "tasks": List (as a dict) of task IDs the payload is used by.
# }
payloads = flight.unit_type.dcs_unit_type.load_payloads()
payloads = aircraft.dcs_unit_type.load_payloads()
for payload in payloads.values():
name = payload["name"]
pylons = payload["pylons"]
@@ -122,9 +129,7 @@ class Loadout:
)
@classmethod
def default_loadout_names_for(cls, flight: Flight) -> Iterator[str]:
from game.ato.flighttype import FlightType
def default_loadout_names_for(cls, task: FlightType) -> Iterator[str]:
# This is a list of mappings from the FlightType of a Flight to the type of
# payload defined in the resources/payloads/UNIT_TYPE.lua file. A Flight has no
# concept of a PyDCS task, so COMMON_OVERRIDE cannot be used here. This is used
@@ -164,17 +169,25 @@ class Loadout:
loadout_names[FlightType.DEAD].extend(loadout_names[FlightType.BAI])
# OCA/Runway falls back to Strike
loadout_names[FlightType.OCA_RUNWAY].extend(loadout_names[FlightType.STRIKE])
yield from loadout_names[flight.flight_type]
yield from loadout_names[task]
@classmethod
def default_for(cls, flight: Flight) -> Loadout:
return cls.default_for_task_and_aircraft(
flight.flight_type, flight.unit_type.dcs_unit_type
)
@classmethod
def default_for_task_and_aircraft(
cls, task: FlightType, dcs_unit_type: Type[FlyingType]
) -> Loadout:
# Iterate through each possible payload type for a given aircraft.
# Some aircraft have custom loadouts that in aren't the standard set.
for name in cls.default_loadout_names_for(flight):
for name in cls.default_loadout_names_for(task):
# This operation is cached, but must be called before load_by_name will
# work.
flight.unit_type.dcs_unit_type.load_payloads()
payload = flight.unit_type.dcs_unit_type.loadout_by_name(name)
dcs_unit_type.load_payloads()
payload = dcs_unit_type.loadout_by_name(name)
if payload is not None:
return Loadout(
name,

View File

@@ -4,7 +4,7 @@ import logging
from dataclasses import dataclass
from functools import cached_property
from pathlib import Path
from typing import Any, Iterator, Optional, TYPE_CHECKING, Type, Dict
from typing import Any, Dict, Iterator, Optional, TYPE_CHECKING, Type
import yaml
from dcs.helicopters import helicopter_map
@@ -315,7 +315,7 @@ class AircraftType(UnitType[Type[FlyingType]]):
yield unit
@staticmethod
def _each_unit_type() -> Iterator[Type[FlyingType]]:
def each_dcs_type() -> Iterator[Type[FlyingType]]:
yield from helicopter_map.values()
yield from plane_map.values()

View File

@@ -3,7 +3,7 @@ from __future__ import annotations
import logging
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Optional, Type, Iterator
from typing import Any, Iterator, Optional, Type
import yaml
from dcs.unittype import VehicleType
@@ -76,7 +76,7 @@ class GroundUnitType(UnitType[Type[VehicleType]]):
yield unit
@staticmethod
def _each_unit_type() -> Iterator[Type[VehicleType]]:
def each_dcs_type() -> Iterator[Type[VehicleType]]:
yield from vehicle_map.values()
@classmethod

View File

@@ -3,7 +3,7 @@ from __future__ import annotations
import logging
from dataclasses import dataclass
from pathlib import Path
from typing import Type, Iterator
from typing import Iterator, Type
import yaml
from dcs.ships import ship_map
@@ -32,7 +32,7 @@ class ShipUnitType(UnitType[Type[ShipType]]):
yield unit
@staticmethod
def _each_unit_type() -> Iterator[Type[ShipType]]:
def each_dcs_type() -> Iterator[Type[ShipType]]:
yield from ship_map.values()
@classmethod

View File

@@ -4,7 +4,7 @@ from abc import ABC
from collections import defaultdict
from dataclasses import dataclass
from functools import cached_property
from typing import TypeVar, Generic, Type, ClassVar, Any, Iterator
from typing import Any, ClassVar, Generic, Iterator, Type, TypeVar
from dcs.unittype import UnitType as DcsUnitType
@@ -52,7 +52,7 @@ class UnitType(ABC, Generic[DcsUnitTypeT]):
raise NotImplementedError
@staticmethod
def _each_unit_type() -> Iterator[DcsUnitTypeT]:
def each_dcs_type() -> Iterator[DcsUnitTypeT]:
raise NotImplementedError
@classmethod
@@ -61,7 +61,7 @@ class UnitType(ABC, Generic[DcsUnitTypeT]):
@classmethod
def _load_all(cls) -> None:
for unit_type in cls._each_unit_type():
for unit_type in cls.each_dcs_type():
for data in cls._each_variant_of(unit_type):
cls.register(data)
cls._loaded = True