Return base defense locations to pool on capture.

When a base is captured we clear its defenses. Those locations need to
be returned to the preset location pool so they can be used for the new
base defenses.
This commit is contained in:
Dan Albert 2020-11-19 21:50:29 -08:00
parent 1e70e654ed
commit 5e4802f05e
4 changed files with 26 additions and 5 deletions

View File

@ -23,7 +23,10 @@ from .base import Base
from .missiontarget import MissionTarget
from .theatergroundobject import (
BaseDefenseGroundObject,
EwrGroundObject,
SamGroundObject,
TheaterGroundObject,
VehicleGroupGroundObject,
)
if TYPE_CHECKING:
@ -282,6 +285,24 @@ class ControlPoint(MissionTarget):
def is_friendly(self, to_player: bool) -> bool:
return self.captured == to_player
def clear_base_defenses(self) -> None:
for base_defense in self.base_defenses:
if isinstance(base_defense, EwrGroundObject):
self.preset_locations.ewrs.append(base_defense.position)
elif isinstance(base_defense, SamGroundObject):
self.preset_locations.base_air_defense.append(
base_defense.position)
elif isinstance(base_defense, VehicleGroupGroundObject):
self.preset_locations.base_garrisons.append(
base_defense.position)
else:
logging.error(
"Could not determine preset location type for "
f"{base_defense}. Assuming garrison type.")
self.preset_locations.base_garrisons.append(
base_defense.position)
self.base_defenses = []
def capture(self, game: Game, for_player: bool) -> None:
if for_player:
self.captured = True
@ -293,9 +314,8 @@ class ControlPoint(MissionTarget):
self.base.aircraft = {}
self.base.armor = {}
# Handle cyclic dependency.
self.clear_base_defenses()
from .start_generator import BaseDefenseGenerator
self.base_defenses = []
BaseDefenseGenerator(game, self).generate()
def mission_types(self, for_player: bool) -> Iterator[FlightType]:

View File

@ -502,7 +502,8 @@ class BaseDefenseGenerator:
self.control_point.base_defenses.append(g)
def generate_shorad(self) -> None:
position = self.location_finder.location_for(LocationType.Garrison)
position = self.location_finder.location_for(
LocationType.BaseAirDefense)
if position is None:
return

View File

@ -243,8 +243,8 @@ class BaseDefenseGroundObject(TheaterGroundObject):
# TODO: Differentiate types.
# This type gets used both for AA sites (SAM, AAA, or SHORAD) but also for the
# armor garrisons at airbases. These should each be split into their own types.
# This type gets used both for AA sites (SAM, AAA, or SHORAD). These should each
# be split into their own types.
class SamGroundObject(BaseDefenseGroundObject):
def __init__(self, name: str, group_id: int, position: Point,
control_point: ControlPoint, for_airbase: bool) -> None: