mirror of
https://github.com/dcs-liberation/dcs_liberation.git
synced 2025-11-10 14:22:26 +00:00
This is an attempt to remove a lot of our supposedly unnecessary error handling. Every aircraft should have a price, a description, a name, etc; and none of those should require carrying around the faction's country as context. This moves all the data for aircraft into yaml files (only one converted here as an example). Most of the "extended unit info" isn't actually being read yet. To replace the renaming of units based on the county, we instead generate multiple types of each unit when necessary. The CF-18 is just as much a first-class type as the F/A-18 is. This doesn't work in its current state because it does break all the existing names for aircraft that are used in the faction and squadron files, and we no longer let those errors go as a warning. It will be an annoying one time switch, but it allows us to define the names that get used in these files instead of being sensitive to changes as they happen in pydcs, and allows faction designers to specifically choose, for example, the Su-22 instead of the Su-17. One thing not handled by this is aircraft task capability. This is because the lists in ai_flight_planner_db.py are a priority list, and to move it out to a yaml file we'd need to assign a weight to it that would be used to stack rank each aircraft. That's doable, but it makes it much more difficult to see the ordering of aircraft at a glance, and much more annoying to move aircraft around in the priority list. I don't think this is worth doing, and the priority lists will remain in their own separate lists. This includes the converted I used to convert all the old unit info and factions to the new format. This doesn't need to live long, but we may want to reuse it in the future so we want it in the version history.
171 lines
6.0 KiB
Python
171 lines
6.0 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
from collections import defaultdict
|
|
from dataclasses import dataclass
|
|
from typing import Dict, Optional, TYPE_CHECKING, Type, Any
|
|
|
|
from dcs.unittype import UnitType, VehicleType
|
|
|
|
from game.theater import ControlPoint
|
|
from .db import PRICES
|
|
from .dcs.aircrafttype import AircraftType
|
|
from .theater.transitnetwork import (
|
|
NoPathError,
|
|
TransitNetwork,
|
|
)
|
|
from .transfers import TransferOrder
|
|
|
|
if TYPE_CHECKING:
|
|
from .game import Game
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class GroundUnitSource:
|
|
control_point: ControlPoint
|
|
|
|
|
|
AircraftOrVehicleType = Any
|
|
|
|
|
|
class PendingUnitDeliveries:
|
|
def __init__(self, destination: ControlPoint) -> None:
|
|
self.destination = destination
|
|
|
|
# Maps unit type to order quantity.
|
|
self.units: Dict[AircraftOrVehicleType, int] = defaultdict(int)
|
|
|
|
def __str__(self) -> str:
|
|
return f"Pending delivery to {self.destination}"
|
|
|
|
def order(self, units: Dict[AircraftOrVehicleType, int]) -> None:
|
|
for k, v in units.items():
|
|
self.units[k] += v
|
|
|
|
def sell(self, units: Dict[AircraftOrVehicleType, int]) -> None:
|
|
for k, v in units.items():
|
|
self.units[k] -= v
|
|
|
|
def refund_all(self, game: Game) -> None:
|
|
self.refund(game, self.units)
|
|
self.units = defaultdict(int)
|
|
|
|
def refund(self, game: Game, units: Dict[Type[UnitType], int]) -> None:
|
|
for unit_type, count in units.items():
|
|
try:
|
|
price = PRICES[unit_type]
|
|
except KeyError:
|
|
logging.error(f"Could not refund {unit_type.id}, price unknown")
|
|
continue
|
|
|
|
logging.info(f"Refunding {count} {unit_type.id} at {self.destination.name}")
|
|
game.adjust_budget(price * count, player=self.destination.captured)
|
|
|
|
def pending_orders(self, unit_type: AircraftOrVehicleType) -> int:
|
|
pending_units = self.units.get(unit_type)
|
|
if pending_units is None:
|
|
pending_units = 0
|
|
return pending_units
|
|
|
|
def available_next_turn(self, unit_type: AircraftOrVehicleType) -> int:
|
|
current_units = self.destination.base.total_units_of_type(unit_type)
|
|
return self.pending_orders(unit_type) + current_units
|
|
|
|
def process(self, game: Game) -> None:
|
|
ground_unit_source = self.find_ground_unit_source(game)
|
|
if ground_unit_source is None:
|
|
game.message(
|
|
f"{self.destination.name} lost its source for ground unit "
|
|
"reinforcements. Refunding purchase price."
|
|
)
|
|
self.refund_all(game)
|
|
return
|
|
|
|
bought_units: Dict[AircraftOrVehicleType, int] = {}
|
|
units_needing_transfer: Dict[Type[VehicleType], int] = {}
|
|
sold_units: Dict[AircraftOrVehicleType, int] = {}
|
|
for unit_type, count in self.units.items():
|
|
coalition = "Ally" if self.destination.captured else "Enemy"
|
|
|
|
if isinstance(unit_type, AircraftType):
|
|
name = unit_type.name
|
|
else:
|
|
name = unit_type.id
|
|
|
|
if (
|
|
type(unit_type) == type
|
|
and issubclass(unit_type, VehicleType)
|
|
and self.destination != ground_unit_source
|
|
):
|
|
source = ground_unit_source
|
|
d = units_needing_transfer
|
|
else:
|
|
source = self.destination
|
|
d = bought_units
|
|
|
|
if count >= 0:
|
|
d[unit_type] = count
|
|
game.message(
|
|
f"{coalition} reinforcements: {name} x {count} at {source}"
|
|
)
|
|
else:
|
|
sold_units[unit_type] = -count
|
|
game.message(f"{coalition} sold: {name} x {-count} at {source}")
|
|
|
|
self.units = defaultdict(int)
|
|
self.destination.base.commission_units(bought_units)
|
|
self.destination.base.commit_losses(sold_units)
|
|
|
|
if units_needing_transfer:
|
|
ground_unit_source.base.commission_units(units_needing_transfer)
|
|
self.create_transfer(game, ground_unit_source, units_needing_transfer)
|
|
|
|
def create_transfer(
|
|
self, game: Game, source: ControlPoint, units: Dict[Type[VehicleType], int]
|
|
) -> None:
|
|
game.transfers.new_transfer(TransferOrder(source, self.destination, units))
|
|
|
|
def find_ground_unit_source(self, game: Game) -> Optional[ControlPoint]:
|
|
# This is running *after* the turn counter has been incremented, so this is the
|
|
# reaction to turn 0. On turn zero we allow units to be recruited anywhere for
|
|
# delivery on turn 1 so that turn 1 always starts with units on the front line.
|
|
if game.turn == 1:
|
|
return self.destination
|
|
|
|
# Fast path if the destination is a valid source.
|
|
if self.destination.can_recruit_ground_units(game):
|
|
return self.destination
|
|
|
|
try:
|
|
return self.find_ground_unit_source_in_network(
|
|
game.transit_network_for(self.destination.captured), game
|
|
)
|
|
except NoPathError:
|
|
return None
|
|
|
|
def find_ground_unit_source_in_network(
|
|
self, network: TransitNetwork, game: Game
|
|
) -> Optional[ControlPoint]:
|
|
sources = []
|
|
for control_point in game.theater.control_points_for(self.destination.captured):
|
|
if control_point.can_recruit_ground_units(
|
|
game
|
|
) and network.has_path_between(self.destination, control_point):
|
|
sources.append(control_point)
|
|
|
|
if not sources:
|
|
return None
|
|
|
|
# Fast path to skip the distance calculation if we have only one option.
|
|
if len(sources) == 1:
|
|
return sources[0]
|
|
|
|
closest = sources[0]
|
|
_, cost = network.shortest_path_with_cost(self.destination, closest)
|
|
for source in sources:
|
|
_, new_cost = network.shortest_path_with_cost(self.destination, source)
|
|
if new_cost < cost:
|
|
closest = source
|
|
cost = new_cost
|
|
return closest
|