Skip to content

Commit

Permalink
Fix exception when campaign has only off map CPs.
Browse files Browse the repository at this point in the history
This PR fixes an exception in custom campaigns that only contain off map
spawns.
  • Loading branch information
zhexu14 committed Dec 15, 2023
1 parent b014f2e commit a213215
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions game/commander/objectivefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
if TYPE_CHECKING:
from game import Game
from game.transfers import CargoShip, Convoy
from game.threatzones import ThreatZones

MissionTargetType = TypeVar("MissionTargetType", bound=MissionTarget)

Expand Down Expand Up @@ -193,17 +194,36 @@ def friendly_control_points(self) -> Iterator[ControlPoint]:

def farthest_friendly_control_point(self) -> ControlPoint:
"""Finds the friendly control point that is farthest from any threats."""

def find_farthest(
control_points: Iterator[ControlPoint],
threat_zones: ThreatZones,
consider_off_map_spawn: bool,
) -> ControlPoint | None:
farthest = None
max_distance = meters(0)
for cp in control_points:
if isinstance(cp, OffMapSpawn) and not consider_off_map_spawn:
continue
distance = threat_zones.distance_to_threat(cp.position)
if distance > max_distance:
farthest = cp
max_distance = distance
return farthest

threat_zones = self.game.threat_zone_for(not self.is_player)

farthest = None
max_distance = meters(0)
for cp in self.friendly_control_points():
if isinstance(cp, OffMapSpawn):
continue
distance = threat_zones.distance_to_threat(cp.position)
if distance > max_distance:
farthest = cp
max_distance = distance
farthest = find_farthest(
self.friendly_control_points(), threat_zones, consider_off_map_spawn=False
)

# If there are only off-map spawn control points, fall back to the farthest amongst off map spawn points
if farthest is None:
farthest = find_farthest(
self.friendly_control_points(),
threat_zones,
consider_off_map_spawn=True,
)

if farthest is None:
raise RuntimeError("Found no friendly control points. You probably lost.")
Expand Down

0 comments on commit a213215

Please sign in to comment.