Skip to content

Commit

Permalink
Revert "Use the actual Country type instead of the name."
Browse files Browse the repository at this point in the history
This reverts commit bd2ec12.

Country is both the data (name, ID, etc) and the container for groups
added to the miz, so it can't be used across multiple mission
generations. See pydcs/dcs#314 for potential
follow up work that would let us do this.

Fixes dcs-liberation#2864.
  • Loading branch information
DanAlbert authored and Lordeath19 committed Jun 2, 2023
1 parent 6520604 commit 26dc1b1
Show file tree
Hide file tree
Showing 24 changed files with 83 additions and 65 deletions.
2 changes: 2 additions & 0 deletions game/ato/flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Flight(SidcDescribable):
def __init__(
self,
package: Package,
country: str,
squadron: Squadron,
count: int,
flight_type: FlightType,
Expand All @@ -48,6 +49,7 @@ def __init__(
) -> None:
self.id = uuid.uuid4()
self.package = package
self.country = country
self.coalition = squadron.coalition
self.squadron = squadron
self.squadron.claim_inventory(count)
Expand Down
4 changes: 4 additions & 0 deletions game/coalition.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ def coalition_id(self) -> int:
return 2
return 1

@property
def country_name(self) -> str:
return self.faction.country

@property
def opponent(self) -> Coalition:
assert self._opponent is not None
Expand Down
3 changes: 3 additions & 0 deletions game/commander/packagebuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ def __init__(
air_wing: AirWing,
flight_db: Database[Flight],
is_player: bool,
package_country: str,
start_type: StartType,
asap: bool,
) -> None:
self.closest_airfields = closest_airfields
self.is_player = is_player
self.package_country = package_country
self.package = Package(location, flight_db, auto_asap=asap)
self.air_wing = air_wing
self.start_type = start_type
Expand Down Expand Up @@ -62,6 +64,7 @@ def plan_flight(self, plan: ProposedFlight) -> bool:

flight = Flight(
self.package,
self.package_country,
squadron,
plan.num_aircraft,
plan.task,
Expand Down
1 change: 1 addition & 0 deletions game/commander/packagefulfiller.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def plan_mission(
self.air_wing,
self.flight_db,
self.is_player,
self.coalition.country_name,
self.default_start_type,
mission.asap,
)
Expand Down
9 changes: 0 additions & 9 deletions game/dcs/countries.py

This file was deleted.

4 changes: 2 additions & 2 deletions game/debriefing.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ def __init__(
self.game = game
self.unit_map = unit_map

self.player_country = game.blue.faction.country.name
self.enemy_country = game.red.faction.country.name
self.player_country = game.blue.country_name
self.enemy_country = game.red.country_name

self.air_losses = self.dead_aircraft()
self.ground_losses = self.dead_ground_units()
Expand Down
23 changes: 11 additions & 12 deletions game/factions/faction.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import Any, Dict, Iterator, List, Optional, TYPE_CHECKING, Type

import dcs
from dcs.country import Country
from dcs.countries import country_dict
from dcs.unittype import ShipType, StaticType, UnitType as DcsUnitType

from game.armedforces.forcegroup import ForceGroup
Expand All @@ -28,7 +28,6 @@
from game.data.groups import GroupRole
from game.data.units import UnitClass
from game.dcs.aircrafttype import AircraftType
from game.dcs.countries import country_with_name
from game.dcs.groundunittype import GroundUnitType
from game.dcs.shipunittype import ShipUnitType
from game.dcs.unittype import UnitType
Expand All @@ -44,7 +43,7 @@ class Faction:
locales: Optional[List[str]]

# Country used by this faction
country: Country
country: str = field(default="")

# Nice name of the faction
name: str = field(default="")
Expand Down Expand Up @@ -169,15 +168,15 @@ def air_defenses(self) -> list[str]:

@classmethod
def from_dict(cls: Type[Faction], json: Dict[str, Any]) -> Faction:
try:
country = country_with_name(json["country"])
except KeyError as ex:
raise KeyError(
f'Faction\'s country ("{json.get("country")}") is not a valid DCS '
"country ID"
) from ex

faction = Faction(locales=json.get("locales"), country=country)
faction = Faction(locales=json.get("locales"))

faction.country = json.get("country", "/")
if faction.country not in [c.name for c in country_dict.values()]:
raise AssertionError(
'Faction\'s country ("{}") is not a valid DCS country ID'.format(
faction.country
)
)

faction.name = json.get("name", "")
if not faction.name:
Expand Down
18 changes: 9 additions & 9 deletions game/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from .campaignloader import CampaignAirWingConfig
from .coalition import Coalition
from .db.gamedb import GameDb
from .dcs.countries import country_with_name
from .infos.information import Information
from .persistence import SaveManager
from .profiling import logged_duration
Expand Down Expand Up @@ -180,15 +179,13 @@ def sanitize_sides(player_faction: Faction, enemy_faction: Faction) -> None:
Make sure the opposing factions are using different countries
:return:
"""
# TODO: This should just be rejected and sent back to the user to fix.
# This isn't always something that the original faction can support.
if player_faction.country == enemy_faction.country:
if player_faction.country.name == "USA":
enemy_faction.country = country_with_name("USAF Aggressors")
elif player_faction.country.name == "Russia":
enemy_faction.country = country_with_name("USSR")
if player_faction.country == "USA":
enemy_faction.country = "USAF Aggressors"
elif player_faction.country == "Russia":
enemy_faction.country = "USSR"
else:
enemy_faction.country = country_with_name("Russia")
enemy_faction.country = "Russia"

def faction_for(self, player: bool) -> Faction:
return self.coalition_for(player).faction
Expand All @@ -199,10 +196,13 @@ def faker_for(self, player: bool) -> Faker:
def air_wing_for(self, player: bool) -> AirWing:
return self.coalition_for(player).air_wing

def country_for(self, player: bool) -> str:
return self.coalition_for(player).country_name

@property
def neutral_country(self) -> Type[Country]:
"""Return the best fitting country that can be used as neutral faction in the generated mission"""
countries_in_use = {self.red.faction.country, self.blue.faction.country}
countries_in_use = [self.red.country_name, self.blue.country_name]
if UnitedNationsPeacekeepers not in countries_in_use:
return UnitedNationsPeacekeepers
elif Switzerland.name not in countries_in_use:
Expand Down
3 changes: 2 additions & 1 deletion game/missiongenerator/aircraft/aircraftgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
from game.ato.package import Package
from game.ato.starttype import StartType
from game.factions.faction import Faction
from game.missiongenerator.lasercoderegistry import LaserCodeRegistry
from game.missiongenerator.missiondata import MissionData
from game.missiongenerator.lasercoderegistry import LaserCodeRegistry
from game.radio.radios import RadioRegistry
from game.radio.tacan import TacanRegistry
from game.runways import RunwayData
Expand Down Expand Up @@ -143,6 +143,7 @@ def _spawn_unused_for(
# TODO: Special flight type?
flight = Flight(
Package(squadron.location, self.game.db.flights),
faction.country,
squadron,
1,
FlightType.BARCAP,
Expand Down
2 changes: 1 addition & 1 deletion game/missiongenerator/airsupportgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def generate(self) -> None:
else self.conflict.red_cp
)

country = self.game.blue.faction.country
country = self.mission.country(self.game.blue.country_name)

if not self.game.settings.disable_legacy_tanker:
fallback_tanker_number = 0
Expand Down
5 changes: 4 additions & 1 deletion game/missiongenerator/cargoshipgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ def generate(self) -> None:
self.generate_cargo_ship(ship)

def generate_cargo_ship(self, ship: CargoShip) -> ShipGroup:
country = self.mission.country(
self.game.coalition_for(ship.player_owned).country_name
)
waypoints = ship.route
group = self.mission.ship_group(
self.game.coalition_for(ship.player_owned).faction.country,
country,
ship.name,
HandyWind,
position=waypoints[0],
Expand Down
4 changes: 3 additions & 1 deletion game/missiongenerator/convoygenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@ def _create_mixed_unit_group(
units: dict[GroundUnitType, int],
for_player: bool,
) -> VehicleGroup:
country = self.mission.country(self.game.coalition_for(for_player).country_name)

unit_types = list(units.items())
main_unit_type, main_unit_count = unit_types[0]

group = self.mission.vehicle_group(
self.game.coalition_for(for_player).faction.country,
country,
name,
main_unit_type.dcs_unit_type,
position=position,
Expand Down
8 changes: 4 additions & 4 deletions game/missiongenerator/flotgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def generate(self) -> None:
utype = AircraftType.named("MQ-9 Reaper")

jtac = self.mission.flight_group(
country=self.game.blue.faction.country,
country=self.mission.country(self.game.blue.country_name),
name=namegen.next_jtac_name(),
aircraft_type=utype.dcs_unit_type,
position=position[0],
Expand Down Expand Up @@ -716,7 +716,7 @@ def _generate_groups(
spawn_heading = (
self.conflict.heading.left if is_player else self.conflict.heading.right
)
country = self.game.coalition_for(is_player).faction.country
country = self.game.coalition_for(is_player).country_name
for group in groups:
if group.role == CombatGroupRole.ARTILLERY:
distance_from_frontline = (
Expand All @@ -734,7 +734,7 @@ def _generate_groups(

g = self._generate_group(
is_player,
country,
self.mission.country(country),
group.unit_type,
group.size,
final_position,
Expand All @@ -750,7 +750,7 @@ def _generate_groups(
self.gen_infantry_group_for_group(
g,
is_player,
country,
self.mission.country(country),
spawn_heading.opposite,
)

Expand Down
3 changes: 2 additions & 1 deletion game/missiongenerator/logisticsgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ def generate_logistics(self) -> LogisticsInfo:
"ctld", "logisticunit"
):
# Spawn logisticsunit at pickup zones
country = self.mission.country(self.flight.country)
logistic_unit = self.mission.static_group(
self.flight.squadron.coalition.faction.country,
country,
f"{self.group.name}logistic",
Fortification.FARP_Ammo_Dump_Coating,
pickup_point,
Expand Down
27 changes: 17 additions & 10 deletions game/missiongenerator/missiongenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,19 @@ def setup_mission_coalitions(self) -> None:
"neutrals", bullseye=Bullseye(Point(0, 0, self.mission.terrain)).to_pydcs()
)

p_country = self.game.blue.faction.country
e_country = self.game.red.faction.country
self.mission.coalition["blue"].add_country(p_country)
self.mission.coalition["red"].add_country(e_country)
p_country = self.game.blue.country_name
e_country = self.game.red.country_name
self.mission.coalition["blue"].add_country(
country_dict[country_id_from_name(p_country)]()
)
self.mission.coalition["red"].add_country(
country_dict[country_id_from_name(e_country)]()
)

belligerents = {p_country, e_country}
belligerents = [
country_id_from_name(p_country),
country_id_from_name(e_country),
]
for country in country_dict.keys():
if country not in belligerents:
self.mission.coalition["neutrals"].add_country(country_dict[country]())
Expand Down Expand Up @@ -284,18 +291,18 @@ def generate_air_units(self, tgo_generator: TgoGenerator) -> None:
aircraft_generator.clear_parking_slots()

aircraft_generator.generate_flights(
self.game.blue.faction.country,
self.mission.country(self.game.blue.country_name),
self.game.blue.ato,
tgo_generator.runways,
)
aircraft_generator.generate_flights(
self.game.red.faction.country,
self.mission.country(self.game.red.country_name),
self.game.red.ato,
tgo_generator.runways,
)
aircraft_generator.spawn_unused_aircraft(
self.game.blue.faction.country,
self.game.red.faction.country,
self.mission.country(self.game.blue.country_name),
self.mission.country(self.game.red.country_name),
)

for flight in aircraft_generator.flights:
Expand Down Expand Up @@ -327,7 +334,7 @@ def generate_destroyed_units(self) -> None:
pos = Point(cast(float, d["x"]), cast(float, d["z"]), self.mission.terrain)
if utype is not None and not self.game.position_culled(pos):
self.mission.static_group(
country=self.game.blue.faction.country,
country=self.mission.country(self.game.blue.country_name),
name="",
_type=utype,
hidden=True,
Expand Down
4 changes: 2 additions & 2 deletions game/missiongenerator/tgogenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ def generate(self) -> None:
return
# Note: Helipad are generated as neutral object in order not to interfer with
# capture triggers
country = self.game.coalition_for(self.cp.captured).faction.country
country = self.m.country(self.game.coalition_for(self.cp.captured).country_name)

for i, helipad in enumerate(self.cp.helipads):
heading = helipad.heading.degrees
Expand Down Expand Up @@ -675,7 +675,7 @@ def __init__(

def generate(self) -> None:
for cp in self.game.theater.controlpoints:
country = self.game.coalition_for(cp.captured).faction.country
country = self.m.country(self.game.coalition_for(cp.captured).country_name)

# Generate helipads
helipad_gen = HelipadGenerator(
Expand Down
2 changes: 1 addition & 1 deletion game/missiongenerator/visualsgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def _generate_frontline_smokes(self) -> None:
break

self.mission.static_group(
self.game.red.faction.country,
self.mission.country(self.game.red.country_name),
"",
_type=v,
position=pos,
Expand Down
6 changes: 3 additions & 3 deletions game/squadrons/squadron.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from datetime import datetime
from typing import Optional, Sequence, TYPE_CHECKING

from dcs.country import Country
from faker import Faker

from game.ato import Flight, FlightType, Package
Expand All @@ -29,7 +28,7 @@
class Squadron:
name: str
nickname: Optional[str]
country: Country
country: str
role: str
aircraft: AircraftType
max_size: int
Expand Down Expand Up @@ -72,7 +71,7 @@ def __hash__(self) -> int:
(
self.name,
self.nickname,
self.country.id,
self.country,
self.role,
self.aircraft,
)
Expand Down Expand Up @@ -419,6 +418,7 @@ def plan_ferry_flight(self, package: Package, size: int) -> None:

flight = Flight(
package,
self.coalition.country_name,
self,
size,
FlightType.FERRY,
Expand Down
Loading

0 comments on commit 26dc1b1

Please sign in to comment.