Dan Albert 783ac18222 Replace existing campaign planner with an HTN.
An HTN (https://en.wikipedia.org/wiki/Hierarchical_task_network) is
similar to a decision tree, but it is able to reset to an earlier stage
if a subtask fails and tasks are able to account for the changes in
world state caused by earlier tasks.

Currently this just uses exactly the same strategy as before so we can
prove the system, but it should make it simpler to improve on task
planning.
2021-07-12 13:02:23 -07:00

56 lines
1.8 KiB
Python

from __future__ import annotations
from dataclasses import dataclass
from typing import TYPE_CHECKING
from game.commander.missionproposals import ProposedMission, ProposedFlight
from game.commander.tasks.theatercommandertask import TheaterCommanderTask
from game.commander.theaterstate import TheaterState
from game.profiling import MultiEventTracer
from game.theater import ControlPoint
from gen.flights.flight import FlightType
if TYPE_CHECKING:
from gen.flights.ai_flight_planner import CoalitionMissionPlanner
@dataclass
class PlanBarcap(TheaterCommanderTask):
target: ControlPoint
def preconditions_met(self, state: TheaterState) -> bool:
return self.target in state.vulnerable_control_points
def apply_effects(self, state: TheaterState) -> None:
state.vulnerable_control_points.remove(self.target)
def execute(
self, mission_planner: CoalitionMissionPlanner, tracer: MultiEventTracer
) -> None:
# Plan enough rounds of CAP that the target has coverage over the expected
# mission duration.
mission_duration = int(
mission_planner.game.settings.desired_player_mission_duration.total_seconds()
)
barcap_duration = int(
mission_planner.faction.doctrine.cap_duration.total_seconds()
)
for _ in range(
0,
mission_duration,
barcap_duration,
):
mission_planner.plan_mission(
ProposedMission(
self.target,
[
ProposedFlight(
FlightType.BARCAP,
2,
mission_planner.doctrine.mission_ranges.cap,
),
],
),
tracer,
)