mirror of
https://github.com/dcs-retribution/dcs-retribution.git
synced 2025-11-10 15:41:24 +00:00
Faction rework wip.
This commit is contained in:
124
game/factions/faction.py
Normal file
124
game/factions/faction.py
Normal file
@@ -0,0 +1,124 @@
|
||||
import logging
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional
|
||||
|
||||
from dcs.planes import PlaneType, plane_map
|
||||
from dcs.unittype import VehicleType, UnitType
|
||||
from dcs.vehicles import vehicle_map, Armor, Unarmed, Infantry, Fortification, Artillery, AirDefence
|
||||
|
||||
from game.data.doctrine import Doctrine
|
||||
|
||||
|
||||
@dataclass
|
||||
class Faction:
|
||||
|
||||
# Country used by this faction
|
||||
country: str
|
||||
|
||||
# Nice name of the faction
|
||||
name: str
|
||||
|
||||
# Available aircraft
|
||||
aircrafts: [UnitType]
|
||||
|
||||
# Available awacs aircraft
|
||||
awacs: [UnitType]
|
||||
|
||||
# Available tanker aircraft
|
||||
tankers: [UnitType]
|
||||
|
||||
# Available frontline units
|
||||
frontline_units: [VehicleType]
|
||||
|
||||
# Available artillery units
|
||||
artillery_units: [VehicleType]
|
||||
|
||||
# Infantry units used
|
||||
infantry_units: [VehicleType]
|
||||
|
||||
# List of units that can be deployed as SHORAD
|
||||
shorad_units: [VehicleType]
|
||||
|
||||
# Possible SAMS site generators for this faction
|
||||
sams: [str]
|
||||
|
||||
# Required mods or asset packs
|
||||
requirements: {str: str}
|
||||
|
||||
# Possible carrier names
|
||||
carrier_names: [str]
|
||||
|
||||
# Possible helicopter carrier names
|
||||
lha_names: [str]
|
||||
|
||||
# Navy group generators
|
||||
navy_generators: [str]
|
||||
|
||||
# Available destroyers
|
||||
destroyers: [str]
|
||||
|
||||
# Available cruisers
|
||||
cruisers: [str]
|
||||
|
||||
# JTAC
|
||||
has_jtac: bool
|
||||
|
||||
# Unit to use as JTAC
|
||||
jtac_unit: str
|
||||
|
||||
# doctrine
|
||||
doctrine: Doctrine
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, json):
|
||||
|
||||
faction = Faction()
|
||||
|
||||
faction.country = json.get("country", "USA")
|
||||
faction.name = json.get("name", "???")
|
||||
|
||||
faction.aircrafts = [f for f in [aircraft_loader(aircraft) for aircraft in json.get("aircrafts", [])] is not None]
|
||||
faction.awacs = [f for f in [aircraft_loader(aircraft) for aircraft in json.get("awacs", [])] is not None]
|
||||
faction.tankers = [f for f in [aircraft_loader(aircraft) for aircraft in json.get("tankers", [])] is not None]
|
||||
|
||||
|
||||
def aircraft_loader(aircraft: str) -> Optional[PlaneType]:
|
||||
"""
|
||||
Find aircraft by name
|
||||
:param aircraft: Aircraft name as string
|
||||
:return: The aircraft as a PyDCS type
|
||||
"""
|
||||
if aircraft in plane_map.keys():
|
||||
return plane_map[aircraft]
|
||||
else:
|
||||
for mother_class in [PlaneType, Unarmed, Infantry, Armor, AirDefence, Artillery, Fortification]:
|
||||
if getattr(mother_class, vehicle) is not None:
|
||||
return getattr(mother_class, vehicle)
|
||||
logging.info("FACTION ERROR : Unable to find " + aircraft + " in pydcs")
|
||||
return None
|
||||
|
||||
def vehicle_loader(vehicle: str) -> Optional[VehicleType]:
|
||||
"""
|
||||
Find vehicle by name
|
||||
:param vehicle: Vehicle name as string
|
||||
:return: The vehicle as a PyDCS type
|
||||
"""
|
||||
if vehicle in plane_map.keys():
|
||||
return plane_map[vehicle]
|
||||
else:
|
||||
for mother_class in [Armor, Unarmed, Infantry, Armor, AirDefence, Artillery, Fortification]:
|
||||
if getattr(mother_class, vehicle) is not None:
|
||||
return getattr(mother_class, vehicle)
|
||||
logging.info("FACTION ERROR : Unable to find " + vehicle + " in pydcs")
|
||||
return None
|
||||
|
||||
vehicle_map
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
28
game/factions/faction_loader.py
Normal file
28
game/factions/faction_loader.py
Normal file
@@ -0,0 +1,28 @@
|
||||
import json
|
||||
import os
|
||||
import logging
|
||||
|
||||
FACTION_DIRECTORY = "./resources/factions/"
|
||||
|
||||
def load_factions() -> {}:
|
||||
files = os.listdir(FACTION_DIRECTORY)
|
||||
files = [f for f in files if f.endswith(".json")]
|
||||
|
||||
factions = {}
|
||||
|
||||
for f in files:
|
||||
path = os.path.join(FACTION_DIRECTORY, f)
|
||||
logging.info("Loading faction" + path)
|
||||
try:
|
||||
with open(path, "r") as fdata:
|
||||
data = json.load(fdata)
|
||||
factions[data["name"]] = data
|
||||
logging.info("Loaded faction : " + path)
|
||||
except:
|
||||
logging.error("Unable to load faction : " + path)
|
||||
|
||||
return factions
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
load_factions()
|
||||
Reference in New Issue
Block a user