Refactor front line code to make sides explicit.

A was intended to be the blue point and B was intended to be the red
point. Make this a part of the name so that's clear, and clean up
related code to keep that reliable.
This commit is contained in:
Dan Albert
2021-05-07 20:56:39 -07:00
parent 12f474ecbe
commit b0c24f6e51
13 changed files with 125 additions and 154 deletions

View File

@@ -92,9 +92,9 @@ class AirSupportConflictGenerator:
def generate(self):
player_cp = (
self.conflict.from_cp
if self.conflict.from_cp.captured
else self.conflict.to_cp
self.conflict.blue_cp
if self.conflict.blue_cp.captured
else self.conflict.red_cp
)
fallback_tanker_number = 0
@@ -107,8 +107,8 @@ class AirSupportConflictGenerator:
freq = self.radio_registry.alloc_uhf()
tacan = self.tacan_registry.alloc_for_band(TacanBand.Y)
tanker_heading = (
self.conflict.to_cp.position.heading_between_point(
self.conflict.from_cp.position
self.conflict.red_cp.position.heading_between_point(
self.conflict.blue_cp.position
)
+ TANKER_HEADING_OFFSET * i
)

View File

@@ -134,10 +134,10 @@ class GroundConflictGenerator:
def generate(self):
position = Conflict.frontline_position(
self.conflict.from_cp, self.conflict.to_cp, self.game.theater
self.conflict.front_line, self.game.theater
)
frontline_vector = Conflict.frontline_vector(
self.conflict.from_cp, self.conflict.to_cp, self.game.theater
self.conflict.front_line, self.game.theater
)
# Create player groups at random position
@@ -156,21 +156,21 @@ class GroundConflictGenerator:
player_groups,
enemy_groups,
self.conflict.heading + 90,
self.conflict.from_cp,
self.conflict.to_cp,
self.conflict.blue_cp,
self.conflict.red_cp,
)
self.plan_action_for_groups(
self.enemy_stance,
enemy_groups,
player_groups,
self.conflict.heading - 90,
self.conflict.to_cp,
self.conflict.from_cp,
self.conflict.red_cp,
self.conflict.blue_cp,
)
# Add JTAC
if self.game.player_faction.has_jtac:
n = "JTAC" + str(self.conflict.from_cp.id) + str(self.conflict.to_cp.id)
n = "JTAC" + str(self.conflict.blue_cp.id) + str(self.conflict.red_cp.id)
code = 1688 - len(self.jtacs)
utype = MQ_9_Reaper
@@ -191,7 +191,7 @@ class GroundConflictGenerator:
OrbitAction(5000, 300, OrbitAction.OrbitPattern.Circle)
)
frontline = (
f"Frontline {self.conflict.from_cp.name}/{self.conflict.to_cp.name}"
f"Frontline {self.conflict.blue_cp.name}/{self.conflict.red_cp.name}"
)
# Note: Will need to change if we ever add ground based JTAC.
callsign = callsign_for_support_unit(jtac)
@@ -213,9 +213,9 @@ class GroundConflictGenerator:
logging.warning("Could not find infantry position")
return
if side == self.conflict.attackers_country:
cp = self.conflict.from_cp
cp = self.conflict.blue_cp
else:
cp = self.conflict.to_cp
cp = self.conflict.red_cp
if is_player:
faction = self.game.player_name
@@ -782,9 +782,9 @@ class GroundConflictGenerator:
) -> VehicleGroup:
if side == self.conflict.attackers_country:
cp = self.conflict.from_cp
cp = self.conflict.blue_cp
else:
cp = self.conflict.to_cp
cp = self.conflict.red_cp
logging.info("armorgen: {} for {}".format(unit, side.id))
group = self.mission.vehicle_group(

View File

@@ -36,8 +36,8 @@ class CommInfo:
class FrontLineInfo:
def __init__(self, front_line: FrontLine):
self.front_line: FrontLine = front_line
self.player_base: ControlPoint = front_line.control_point_a
self.enemy_base: ControlPoint = front_line.control_point_b
self.player_base: ControlPoint = front_line.blue_cp
self.enemy_base: ControlPoint = front_line.red_cp
self.player_zero: bool = self.player_base.base.total_armor == 0
self.enemy_zero: bool = self.enemy_base.base.total_armor == 0
self.advantage: bool = (
@@ -164,7 +164,7 @@ class BriefingGenerator(MissionInfoGenerator):
def _generate_frontline_info(self) -> None:
"""Build FrontLineInfo objects from FrontLine type and append to briefing."""
for front_line in self.game.theater.conflicts(from_player=True):
for front_line in self.game.theater.conflicts():
self.add_frontline(FrontLineInfo(front_line))
# TODO: This should determine if runway is friendly through a method more robust than the existing string match

View File

@@ -17,8 +17,7 @@ class Conflict:
def __init__(
self,
theater: ConflictTheater,
from_cp: ControlPoint,
to_cp: ControlPoint,
front_line: FrontLine,
attackers_side: str,
defenders_side: str,
attackers_country: Country,
@@ -33,22 +32,28 @@ class Conflict:
self.attackers_country = attackers_country
self.defenders_country = defenders_country
self.from_cp = from_cp
self.to_cp = to_cp
self.front_line = front_line
self.theater = theater
self.position = position
self.heading = heading
self.size = size
@property
def blue_cp(self) -> ControlPoint:
return self.front_line.blue_cp
@property
def red_cp(self) -> ControlPoint:
return self.front_line.red_cp
@classmethod
def has_frontline_between(cls, from_cp: ControlPoint, to_cp: ControlPoint) -> bool:
return from_cp.has_frontline and to_cp.has_frontline
@classmethod
def frontline_position(
cls, from_cp: ControlPoint, to_cp: ControlPoint, theater: ConflictTheater
cls, frontline: FrontLine, theater: ConflictTheater
) -> Tuple[Point, int]:
frontline = FrontLine(from_cp, to_cp, theater)
attack_heading = frontline.attack_heading
position = cls.find_ground_position(
frontline.position,
@@ -60,12 +65,12 @@ class Conflict:
@classmethod
def frontline_vector(
cls, from_cp: ControlPoint, to_cp: ControlPoint, theater: ConflictTheater
cls, front_line: FrontLine, theater: ConflictTheater
) -> Tuple[Point, int, int]:
"""
Returns a vector for a valid frontline location avoiding exclusion zones.
"""
center_position, heading = cls.frontline_position(from_cp, to_cp, theater)
center_position, heading = cls.frontline_position(front_line, theater)
left_heading = heading_sum(heading, -90)
right_heading = heading_sum(heading, 90)
left_position = cls.extend_ground_position(
@@ -84,18 +89,16 @@ class Conflict:
defender_name: str,
attacker: Country,
defender: Country,
from_cp: ControlPoint,
to_cp: ControlPoint,
front_line: FrontLine,
theater: ConflictTheater,
):
assert cls.has_frontline_between(from_cp, to_cp)
position, heading, distance = cls.frontline_vector(from_cp, to_cp, theater)
assert cls.has_frontline_between(front_line.blue_cp, front_line.red_cp)
position, heading, distance = cls.frontline_vector(front_line, theater)
conflict = cls(
position=position,
heading=heading,
theater=theater,
from_cp=from_cp,
to_cp=to_cp,
front_line=front_line,
attackers_side=attacker_name,
defenders_side=defender_name,
attackers_country=attacker,

View File

@@ -409,13 +409,7 @@ class ObjectiveFinder:
def front_lines(self) -> Iterator[FrontLine]:
"""Iterates over all active front lines in the theater."""
for cp in self.friendly_control_points():
for connected in cp.connected_points:
if connected.is_friendly(self.is_player):
continue
if Conflict.has_frontline_between(cp, connected):
yield FrontLine(cp, connected, self.game.theater)
yield from self.game.theater.conflicts()
def vulnerable_control_points(self) -> Iterator[ControlPoint]:
"""Iterates over friendly CPs that are vulnerable to enemy CPs.
@@ -447,19 +441,19 @@ class ObjectiveFinder:
def convoys(self) -> Iterator[Convoy]:
for front_line in self.front_lines():
if front_line.control_point_a.is_friendly(self.is_player):
enemy_cp = front_line.control_point_a
if front_line.blue_cp.is_friendly(self.is_player):
enemy_cp = front_line.blue_cp
else:
enemy_cp = front_line.control_point_b
enemy_cp = front_line.red_cp
yield from self.game.transfers.convoys.travelling_to(enemy_cp)
def cargo_ships(self) -> Iterator[CargoShip]:
for front_line in self.front_lines():
if front_line.control_point_a.is_friendly(self.is_player):
enemy_cp = front_line.control_point_a
if front_line.blue_cp.is_friendly(self.is_player):
enemy_cp = front_line.blue_cp
else:
enemy_cp = front_line.control_point_b
enemy_cp = front_line.red_cp
yield from self.game.transfers.cargo_ships.travelling_to(enemy_cp)

View File

@@ -1320,11 +1320,9 @@ class FlightPlanBuilder:
def racetrack_for_frontline(
self, origin: Point, front_line: FrontLine
) -> Tuple[Point, Point]:
ally_cp, enemy_cp = front_line.control_points
# Find targets waypoints
ingress, heading, distance = Conflict.frontline_vector(
ally_cp, enemy_cp, self.game.theater
front_line, self.game.theater
)
center = ingress.point_from_heading(heading, distance / 2)
orbit_center = center.point_from_heading(
@@ -1533,7 +1531,7 @@ class FlightPlanBuilder:
raise InvalidObjectiveLocation(flight.flight_type, location)
ingress, heading, distance = Conflict.frontline_vector(
location.control_points[0], location.control_points[1], self.game.theater
location, self.game.theater
)
center = ingress.point_from_heading(heading, distance / 2)
egress = ingress.point_from_heading(heading, distance)

View File

@@ -98,13 +98,13 @@ class VisualGenerator:
def _generate_frontline_smokes(self):
for front_line in self.game.theater.conflicts():
from_cp = front_line.control_point_a
to_cp = front_line.control_point_b
from_cp = front_line.blue_cp
to_cp = front_line.red_cp
if from_cp.is_global or to_cp.is_global:
continue
plane_start, heading, distance = Conflict.frontline_vector(
from_cp, to_cp, self.game.theater
front_line, self.game.theater
)
if not plane_start:
continue