Fix some typing in preparation for pydcs types.

Not complete, but progress.

(cherry picked from commit 53f6a0b32b)
This commit is contained in:
Dan Albert
2021-07-08 22:50:55 -07:00
parent 4e9d661c0c
commit c0f6974d07
30 changed files with 208 additions and 193 deletions

View File

@@ -5,14 +5,14 @@ import inspect
import logging
from collections import defaultdict
from dataclasses import dataclass, field
from typing import Dict, Iterator, Optional, Set, Tuple, Union, cast
from typing import Dict, Iterator, Optional, Set, Tuple, Union, cast, Any
from dcs.unitgroup import FlyingGroup
from dcs.weapons_data import Weapons, weapon_ids
from game.dcs.aircrafttype import AircraftType
PydcsWeapon = Dict[str, Union[int, str]]
PydcsWeapon = Dict[str, Any]
PydcsWeaponAssignment = Tuple[int, PydcsWeapon]

View File

@@ -106,7 +106,7 @@ class PatrolConfig:
@dataclass(frozen=True)
class AircraftType(UnitType[FlyingType]):
class AircraftType(UnitType[Type[FlyingType]]):
carrier_capable: bool
lha_capable: bool
always_keeps_gun: bool

View File

@@ -15,7 +15,7 @@ from game.dcs.unittype import UnitType
@dataclass(frozen=True)
class GroundUnitType(UnitType[VehicleType]):
class GroundUnitType(UnitType[Type[VehicleType]]):
unit_class: Optional[GroundUnitClass]
spawn_weight: int

View File

@@ -4,12 +4,12 @@ from typing import TypeVar, Generic, Type
from dcs.unittype import UnitType as DcsUnitType
DcsUnitTypeT = TypeVar("DcsUnitTypeT", bound=DcsUnitType)
DcsUnitTypeT = TypeVar("DcsUnitTypeT", bound=Type[DcsUnitType])
@dataclass(frozen=True)
class UnitType(Generic[DcsUnitTypeT]):
dcs_unit_type: Type[DcsUnitTypeT]
dcs_unit_type: DcsUnitTypeT
name: str
description: str
year_introduced: str

View File

@@ -5,7 +5,6 @@ from typing import List, TYPE_CHECKING, Type
from dcs.mapping import Point
from dcs.task import Task
from dcs.unittype import VehicleType
from game import persistency
from game.debriefing import AirLosses, Debriefing

View File

@@ -84,10 +84,10 @@ class Faction:
requirements: Dict[str, str] = field(default_factory=dict)
# possible aircraft carrier units
aircraft_carrier: List[Type[UnitType]] = field(default_factory=list)
aircraft_carrier: List[Type[ShipType]] = field(default_factory=list)
# possible helicopter carrier units
helicopter_carrier: List[Type[UnitType]] = field(default_factory=list)
helicopter_carrier: List[Type[ShipType]] = field(default_factory=list)
# Possible carrier names
carrier_names: List[str] = field(default_factory=list)

View File

@@ -70,20 +70,6 @@ class Operation:
cls._setup_mission_coalitions()
cls.current_mission.options.load_from_dict(options_dict)
@classmethod
def conflicts(cls) -> Iterable[Conflict]:
assert cls.game
for frontline in cls.game.theater.conflicts():
yield Conflict(
cls.game.theater,
frontline,
cls.game.player_faction.name,
cls.game.enemy_faction.name,
cls.game.player_country,
cls.game.enemy_country,
frontline.position,
)
@classmethod
def air_conflict(cls) -> Conflict:
assert cls.game
@@ -97,8 +83,8 @@ class Operation:
FrontLine(player_cp, enemy_cp),
cls.game.player_faction.name,
cls.game.enemy_faction.name,
cls.game.player_country,
cls.game.enemy_country,
cls.current_mission.country(cls.game.player_country),
cls.current_mission.country(cls.game.enemy_country),
mid_point,
)

View File

@@ -2,8 +2,6 @@ import itertools
import logging
from typing import Any
from dcs.unit import UnitType as DcsUnitType
from game.dcs.aircrafttype import AircraftType
from game.dcs.groundunittype import GroundUnitType
from game.dcs.unittype import UnitType
@@ -33,7 +31,7 @@ class Base:
total += unit_type.price * count
return total
def total_units_of_type(self, unit_type: UnitType[DcsUnitType]) -> int:
def total_units_of_type(self, unit_type: UnitType[Any]) -> int:
return sum(
[
c

View File

@@ -545,7 +545,7 @@ class ConflictTheater:
def find_ground_objects_by_obj_name(
self, obj_name: str
) -> list[TheaterGroundObject]:
) -> list[TheaterGroundObject[Any]]:
found = []
for cp in self.controlpoints:
for g in cp.ground_objects:

View File

@@ -43,6 +43,7 @@ from .missiontarget import MissionTarget
from .theatergroundobject import (
GenericCarrierGroundObject,
TheaterGroundObject,
NavalGroundObject,
)
from ..dcs.aircrafttype import AircraftType
from ..dcs.groundunittype import GroundUnitType
@@ -298,7 +299,7 @@ class ControlPoint(MissionTarget, ABC):
self.id = cp_id
self.full_name = name
self.at = at
self.connected_objectives: List[TheaterGroundObject] = []
self.connected_objectives: List[TheaterGroundObject[Any]] = []
self.preset_locations = PresetLocations()
self.helipads: List[PointWithHeading] = []
@@ -326,7 +327,7 @@ class ControlPoint(MissionTarget, ABC):
return f"<{self.__class__}: {self.name}>"
@property
def ground_objects(self) -> List[TheaterGroundObject]:
def ground_objects(self) -> List[TheaterGroundObject[Any]]:
return list(self.connected_objectives)
@property
@@ -502,7 +503,7 @@ class ControlPoint(MissionTarget, ABC):
def find_ground_objects_by_obj_name(
self, obj_name: str
) -> list[TheaterGroundObject]:
) -> list[TheaterGroundObject[Any]]:
found = []
for g in self.ground_objects:
if g.obj_name == obj_name:
@@ -881,9 +882,12 @@ class NavalControlPoint(ControlPoint, ABC):
def heading(self) -> int:
return 0 # TODO compute heading
def find_main_tgo(self) -> TheaterGroundObject:
def find_main_tgo(self) -> GenericCarrierGroundObject:
for g in self.ground_objects:
if g.dcs_identifier in ["CARRIER", "LHA"]:
if isinstance(g, GenericCarrierGroundObject) and g.dcs_identifier in [
"CARRIER",
"LHA",
]:
return g
raise RuntimeError(f"Found no carrier/LHA group for {self.name}")

View File

@@ -2,12 +2,12 @@ from __future__ import annotations
import itertools
import logging
from typing import Iterator, List, TYPE_CHECKING, Union
from typing import Iterator, List, TYPE_CHECKING, Union, Generic, TypeVar, Any
from dcs.mapping import Point
from dcs.triggers import TriggerZone
from dcs.unit import Unit
from dcs.unitgroup import Group
from dcs.unitgroup import Group, ShipGroup, VehicleGroup
from dcs.unittype import VehicleType
from .. import db
@@ -47,7 +47,10 @@ NAME_BY_CATEGORY = {
}
class TheaterGroundObject(MissionTarget):
GroupT = TypeVar("GroupT", bound=Group)
class TheaterGroundObject(MissionTarget, Generic[GroupT]):
def __init__(
self,
name: str,
@@ -66,7 +69,7 @@ class TheaterGroundObject(MissionTarget):
self.control_point = control_point
self.dcs_identifier = dcs_identifier
self.sea_object = sea_object
self.groups: List[Group] = []
self.groups: List[GroupT] = []
@property
def is_dead(self) -> bool:
@@ -206,7 +209,7 @@ class TheaterGroundObject(MissionTarget):
raise NotImplementedError
class BuildingGroundObject(TheaterGroundObject):
class BuildingGroundObject(TheaterGroundObject[VehicleGroup]):
def __init__(
self,
name: str,
@@ -253,7 +256,7 @@ class BuildingGroundObject(TheaterGroundObject):
def kill(self) -> None:
self._dead = True
def iter_building_group(self) -> Iterator[TheaterGroundObject]:
def iter_building_group(self) -> Iterator[TheaterGroundObject[Any]]:
for tgo in self.control_point.ground_objects:
if tgo.obj_name == self.obj_name and not tgo.is_dead:
yield tgo
@@ -338,7 +341,7 @@ class FactoryGroundObject(BuildingGroundObject):
)
class NavalGroundObject(TheaterGroundObject):
class NavalGroundObject(TheaterGroundObject[ShipGroup]):
def mission_types(self, for_player: bool) -> Iterator[FlightType]:
from gen.flights.flight import FlightType
@@ -407,7 +410,7 @@ class LhaGroundObject(GenericCarrierGroundObject):
return f"{self.faction_color}|EWR|{super().group_name}"
class MissileSiteGroundObject(TheaterGroundObject):
class MissileSiteGroundObject(TheaterGroundObject[VehicleGroup]):
def __init__(
self, name: str, group_id: int, position: Point, control_point: ControlPoint
) -> None:
@@ -431,7 +434,7 @@ class MissileSiteGroundObject(TheaterGroundObject):
return False
class CoastalSiteGroundObject(TheaterGroundObject):
class CoastalSiteGroundObject(TheaterGroundObject[VehicleGroup]):
def __init__(
self,
name: str,
@@ -463,7 +466,7 @@ class CoastalSiteGroundObject(TheaterGroundObject):
# The SamGroundObject represents all type of AA
# The TGO can have multiple types of units (AAA,SAM,Support...)
# Differentiation can be made during generation with the airdefensegroupgenerator
class SamGroundObject(TheaterGroundObject):
class SamGroundObject(TheaterGroundObject[VehicleGroup]):
def __init__(
self,
name: str,
@@ -535,7 +538,7 @@ class SamGroundObject(TheaterGroundObject):
return True
class VehicleGroupGroundObject(TheaterGroundObject):
class VehicleGroupGroundObject(TheaterGroundObject[VehicleGroup]):
def __init__(
self,
name: str,
@@ -563,7 +566,7 @@ class VehicleGroupGroundObject(TheaterGroundObject):
return True
class EwrGroundObject(TheaterGroundObject):
class EwrGroundObject(TheaterGroundObject[VehicleGroup]):
def __init__(
self,
name: str,

View File

@@ -30,16 +30,16 @@ class PendingUnitDeliveries:
self.destination = destination
# Maps unit type to order quantity.
self.units: dict[UnitType[DcsUnitType], int] = defaultdict(int)
self.units: dict[UnitType[Any], int] = defaultdict(int)
def __str__(self) -> str:
return f"Pending delivery to {self.destination}"
def order(self, units: dict[UnitType[DcsUnitType], int]) -> None:
def order(self, units: dict[UnitType[Any], int]) -> None:
for k, v in units.items():
self.units[k] += v
def sell(self, units: dict[UnitType[DcsUnitType], int]) -> None:
def sell(self, units: dict[UnitType[Any], int]) -> None:
for k, v in units.items():
self.units[k] -= v

View File

@@ -2,7 +2,7 @@
import itertools
import math
from dataclasses import dataclass
from typing import Dict, Optional
from typing import Dict, Optional, Any
from dcs.unit import Unit
from dcs.unitgroup import FlyingGroup, Group, VehicleGroup
@@ -29,7 +29,7 @@ class FrontLineUnit:
@dataclass(frozen=True)
class GroundObjectUnit:
ground_object: TheaterGroundObject
ground_object: TheaterGroundObject[Any]
group: Group
unit: Unit
@@ -100,7 +100,7 @@ class UnitMap:
def add_ground_object_units(
self,
ground_object: TheaterGroundObject,
ground_object: TheaterGroundObject[Any],
persistence_group: Group,
miz_group: Group,
) -> None: