sanity check

This commit is contained in:
walterroach 2020-11-12 19:16:01 -06:00
parent ede5ee60c3
commit 5719b136fe
3 changed files with 30 additions and 14 deletions

View File

@ -15,9 +15,7 @@ from dcs.terrain.terrain import Terrain
from .controlpoint import ControlPoint from .controlpoint import ControlPoint
from .landmap import Landmap, load_landmap, poly_contains from .landmap import Landmap, load_landmap, poly_contains
from .frontline import FrontLine
if TYPE_CHECKING:
from . import FrontLine
SIZE_TINY = 150 SIZE_TINY = 150
SIZE_SMALL = 600 SIZE_SMALL = 600
@ -128,7 +126,6 @@ class ConflictTheater:
return [point for point in self.controlpoints if point.captured] return [point for point in self.controlpoints if point.captured]
def conflicts(self, from_player=True) -> Iterator[FrontLine]: def conflicts(self, from_player=True) -> Iterator[FrontLine]:
from . import FrontLine # Circular import that needs to be resolved.
for cp in [x for x in self.controlpoints if x.captured == from_player]: for cp in [x for x in self.controlpoints if x.captured == from_player]:
for connected_point in [x for x in cp.connected_points if x.captured != from_player]: for connected_point in [x for x in cp.connected_points if x.captured != from_player]:
yield FrontLine(cp, connected_point) yield FrontLine(cp, connected_point)
@ -169,7 +166,7 @@ class ConflictTheater:
cp.captured_invert = False cp.captured_invert = False
return cp return cp
@staticmethod @staticmethod
def from_json(data: Dict[str, Any]) -> ConflictTheater: def from_json(data: Dict[str, Any]) -> ConflictTheater:
theaters = { theaters = {
@ -183,7 +180,6 @@ class ConflictTheater:
theater = theaters[data["theater"]] theater = theaters[data["theater"]]
t = theater() t = theater()
cps = {} cps = {}
for p in data["player_points"]: for p in data["player_points"]:
cp = t.add_json_cp(theater, p) cp = t.add_json_cp(theater, p)
cp.captured = True cp.captured = True

View File

@ -3,9 +3,10 @@ from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
import logging import logging
import json
from pathlib import Path
from itertools import tee from itertools import tee
from typing import Tuple, List, Union, Dict from typing import Tuple, List, Union, Dict, Optional
from dcs.mapping import Point from dcs.mapping import Point
@ -60,18 +61,19 @@ class FrontLine(MissionTarget):
Overwrites the entirety of MissionTarget __init__ method to allow for Overwrites the entirety of MissionTarget __init__ method to allow for
dynamic position calculation. dynamic position calculation.
""" """
frontline_data: Optional[Dict[str, ComplexFrontLine]] = None
def __init__( def __init__(
self, self,
control_point_a: ControlPoint, control_point_a: ControlPoint,
control_point_b: ControlPoint, control_point_b: ControlPoint,
frontline_data: Dict[str, ComplexFrontLine],
) -> None: ) -> None:
self.control_point_a = control_point_a self.control_point_a = control_point_a
self.control_point_b = control_point_b self.control_point_b = control_point_b
self.segments: List[FrontLineSegment] = [] self.segments: List[FrontLineSegment] = []
self._build_segments(frontline_data) self._build_segments()
self.name = f"Front line {control_point_a}/{control_point_b}" self.name = f"Front line {control_point_a}/{control_point_b}"
print(f"FRONTLINE SEGMENTS {len(self.segments)}")
@property @property
def position(self): def position(self):
@ -125,6 +127,24 @@ class FrontLine(MissionTarget):
strength_pct = self.control_point_a.base.strength / total_strength strength_pct = self.control_point_a.base.strength / total_strength
return self._adjust_for_min_dist(strength_pct * self.attack_distance) return self._adjust_for_min_dist(strength_pct * self.attack_distance)
@classmethod
def load_json_frontlines(cls, terrain_name: str) -> None:
try:
path = Path(f"resources/frontlines/{terrain_name.lower()}.json")
with open(path, "r") as file:
logging.debug(f"Loading frontline from {path}...")
data = json.load(file)
cls.frontline_data = {
frontline: ComplexFrontLine(
data[frontline]["start_cp"],
[Point(i[0], i[1]) for i in data[frontline]["points"]],
)
for frontline in data
}
print(cls.frontline_data)
except OSError:
logging.warning(f"Unable to load preset frontlines for {terrain_name}")
def _calculate_position(self) -> Point: def _calculate_position(self) -> Point:
""" """
The position where the conflict should occur The position where the conflict should occur
@ -132,14 +152,14 @@ class FrontLine(MissionTarget):
""" """
return self.point_from_a(self._position_distance) return self.point_from_a(self._position_distance)
def _build_segments(self, frontline_data: Dict[str, ComplexFrontLine]) -> None: def _build_segments(self) -> None:
control_point_ids = "|".join( control_point_ids = "|".join(
[str(self.control_point_a.id), str(self.control_point_b.id)] [str(self.control_point_a.id), str(self.control_point_b.id)]
) )
reversed_cp_ids = "|".join( reversed_cp_ids = "|".join(
[str(self.control_point_b.id), str(self.control_point_a.id)] [str(self.control_point_b.id), str(self.control_point_a.id)]
) )
complex_frontlines = frontline_data complex_frontlines = FrontLine.frontline_data
if (complex_frontlines) and ( if (complex_frontlines) and (
(control_point_ids in complex_frontlines) (control_point_ids in complex_frontlines)
or (reversed_cp_ids in complex_frontlines) or (reversed_cp_ids in complex_frontlines)

View File

@ -40,6 +40,7 @@ from theater.theatergroundobject import (
LhaGroundObject, LhaGroundObject,
MissileSiteGroundObject, ShipGroundObject, MissileSiteGroundObject, ShipGroundObject,
) )
from theater.frontline import FrontLine
GroundObjectTemplates = Dict[str, Dict[str, Any]] GroundObjectTemplates = Dict[str, Dict[str, Any]]
@ -73,7 +74,7 @@ class GameGenerator:
namegen.reset() namegen.reset()
self.prepare_theater() self.prepare_theater()
self.populate_red_airbases() self.populate_red_airbases()
FrontLine.load_json_frontlines(self.theater.terrain.name)
game = Game(player_name=self.player, game = Game(player_name=self.player,
enemy_name=self.enemy, enemy_name=self.enemy,
theater=self.theater, theater=self.theater,
@ -89,7 +90,6 @@ class GameGenerator:
def prepare_theater(self) -> None: def prepare_theater(self) -> None:
to_remove = [] to_remove = []
# Auto-capture half the bases if midgame. # Auto-capture half the bases if midgame.
if self.midgame: if self.midgame:
control_points = self.theater.controlpoints control_points = self.theater.controlpoints