Wrap the pydcs FlyingType in our own AircraftType.

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.
This commit is contained in:
Dan Albert
2021-06-06 14:37:19 -07:00
parent 88abaef7f9
commit 4a3ef42e67
39 changed files with 1314 additions and 890 deletions

View File

@@ -143,7 +143,7 @@ class PackageModel(QAbstractListModel):
@staticmethod
def icon_for_flight(flight: Flight) -> Optional[QIcon]:
"""Returns the icon that should be displayed for the flight."""
name = db.unit_type_name(flight.unit_type)
name = flight.unit_type.dcs_id
if name in AIRCRAFT_ICONS:
return QIcon(AIRCRAFT_ICONS[name])
return None
@@ -402,7 +402,7 @@ class AirWingModel(QAbstractListModel):
@staticmethod
def icon_for_squadron(squadron: Squadron) -> Optional[QIcon]:
"""Returns the icon that should be displayed for the squadron."""
name = db.unit_type_name(squadron.aircraft)
name = squadron.aircraft.dcs_id
if name in AIRCRAFT_ICONS:
return QIcon(AIRCRAFT_ICONS[name])
return None

View File

@@ -30,10 +30,7 @@ class QAircraftTypeSelector(QComboBox):
self.clear()
for aircraft in aircraft_types:
if aircraft in aircraft_for_task(mission_type):
self.addItem(
f"{db.unit_get_expanded_info(self.country, aircraft, 'name')}",
userData=aircraft,
)
self.addItem(f"{aircraft}", userData=aircraft)
current_aircraft_index = self.findData(current_aircraft)
if current_aircraft_index != -1:
self.setCurrentIndex(current_aircraft_index)

View File

@@ -1,7 +1,7 @@
from __future__ import annotations
from dataclasses import dataclass
from typing import Optional, Type, Iterator
from typing import Optional, Iterator
from PySide2.QtCore import (
QItemSelectionModel,
@@ -20,9 +20,7 @@ from PySide2.QtWidgets import (
QTableWidgetItem,
QWidget,
)
from dcs.unittype import FlyingType
from game import db
from game.inventory import ControlPointAircraftInventory
from game.squadrons import Squadron
from gen.flights.flight import Flight
@@ -45,9 +43,7 @@ class SquadronDelegate(TwoColumnRowDelegate):
return self.air_wing_model.data(index, Qt.DisplayRole)
elif (row, column) == (0, 1):
squadron = self.air_wing_model.data(index, AirWingModel.SquadronRole)
return db.unit_get_expanded_info(
squadron.country, squadron.aircraft, "name"
)
return squadron.aircraft.name
elif (row, column) == (1, 0):
return self.squadron(index).nickname
elif (row, column) == (1, 1):
@@ -111,7 +107,6 @@ class AircraftInventoryData:
@classmethod
def from_flight(cls, flight: Flight) -> Iterator[AircraftInventoryData]:
unit_type_name = cls.format_unit_type(flight.unit_type, flight.country)
num_units = flight.count
flight_type = flight.flight_type.value
target = flight.package.target.name
@@ -125,7 +120,7 @@ class AircraftInventoryData:
player = "Player" if pilot.player else "AI"
yield AircraftInventoryData(
flight.departure.name,
unit_type_name,
flight.unit_type.name,
flight_type,
target,
pilot_name,
@@ -134,24 +129,19 @@ class AircraftInventoryData:
@classmethod
def each_from_inventory(
cls, inventory: ControlPointAircraftInventory, country: str
cls, inventory: ControlPointAircraftInventory
) -> Iterator[AircraftInventoryData]:
for unit_type, num_units in inventory.all_aircraft:
unit_type_name = cls.format_unit_type(unit_type, country)
for _ in range(0, num_units):
yield AircraftInventoryData(
inventory.control_point.name,
unit_type_name,
unit_type.name,
"Idle",
"N/A",
"N/A",
"N/A",
)
@staticmethod
def format_unit_type(aircraft: Type[FlyingType], country: str) -> str:
return db.unit_get_expanded_info(country, aircraft, "name")
class AirInventoryView(QWidget):
def __init__(self, game_model: GameModel) -> None:
@@ -201,9 +191,7 @@ class AirInventoryView(QWidget):
game = self.game_model.game
for control_point, inventory in game.aircraft_inventory.inventories.items():
if control_point.captured:
yield from AircraftInventoryData.each_from_inventory(
inventory, game.country_for(player=True)
)
yield from AircraftInventoryData.each_from_inventory(inventory)
def get_data(self, only_unallocated: bool) -> Iterator[AircraftInventoryData]:
yield from self.iter_unallocated_aircraft()

View File

@@ -22,15 +22,7 @@ class LossGrid(QGridLayout):
def __init__(self, debriefing: Debriefing, player: bool) -> None:
super().__init__()
if player:
country = debriefing.player_country
else:
country = debriefing.enemy_country
self.add_loss_rows(
debriefing.air_losses.by_type(player),
lambda u: db.unit_get_expanded_info(country, u, "name"),
)
self.add_loss_rows(debriefing.air_losses.by_type(player), lambda u: u.name)
self.add_loss_rows(
debriefing.front_line_losses_by_type(player),
lambda u: db.unit_type_name(u),

View File

@@ -1,41 +1,40 @@
import logging
from typing import Type
from typing import Type, Union
from PySide2 import QtCore
import dcs
from PySide2.QtCore import Qt
from PySide2.QtGui import QIcon, QMovie, QPixmap
from PySide2.QtGui import QIcon
from PySide2.QtWidgets import (
QDialog,
QGridLayout,
QGroupBox,
QHBoxLayout,
QLabel,
QMessageBox,
QPushButton,
QTextBrowser,
QFrame,
)
from jinja2 import Environment, FileSystemLoader, select_autoescape
from dcs.unittype import UnitType, FlyingType, VehicleType
import dcs
from qt_ui.uiconstants import AIRCRAFT_BANNERS, VEHICLE_BANNERS
from game.game import Game
from game import db
from dcs.unittype import UnitType
import gen.flights.ai_flight_planner_db
from game import db
from game.dcs.aircrafttype import AircraftType
from game.game import Game
from gen.flights.flight import FlightType
from qt_ui.uiconstants import AIRCRAFT_BANNERS, VEHICLE_BANNERS
class QUnitInfoWindow(QDialog):
def __init__(self, game: Game, unit_type: Type[UnitType]) -> None:
super(QUnitInfoWindow, self).__init__()
def __init__(
self, game: Game, unit_type: Union[AircraftType, Type[UnitType]]
) -> None:
super().__init__()
self.setModal(True)
self.game = game
self.unit_type = unit_type
self.setWindowTitle(
f"Unit Info: {db.unit_get_expanded_info(self.game.player_country, self.unit_type, 'name')}"
)
if isinstance(unit_type, AircraftType):
self.name = unit_type.name
else:
self.name = db.unit_get_expanded_info(
self.game.player_country, self.unit_type, "name"
)
self.setWindowTitle(f"Unit Info: {self.name}")
self.setWindowIcon(QIcon("./resources/icon.png"))
self.setMinimumHeight(570)
self.setMaximumWidth(640)
@@ -71,7 +70,7 @@ class QUnitInfoWindow(QDialog):
self.details_grid_layout.setMargin(0)
self.name_box = QLabel(
f"<b>Name:</b> {db.unit_get_expanded_info(self.game.player_country, self.unit_type, 'manufacturer')} {db.unit_get_expanded_info(self.game.player_country, self.unit_type, 'name')}"
f"<b>Name:</b> {db.unit_get_expanded_info(self.game.player_country, self.unit_type, 'manufacturer')} {self.name}"
)
self.name_box.setProperty("style", "info-element")

View File

@@ -1,5 +1,5 @@
import logging
from typing import Type
from typing import Type, Union
from PySide2.QtWidgets import (
QGroupBox,
@@ -10,9 +10,9 @@ from PySide2.QtWidgets import (
QSizePolicy,
QSpacerItem,
)
from dcs.unittype import UnitType
from dcs.unittype import VehicleType
from game import db
from game.dcs.aircrafttype import AircraftType
from game.theater import ControlPoint
from game.unitdelivery import PendingUnitDeliveries
from qt_ui.models import GameModel
@@ -47,7 +47,7 @@ class QRecruitBehaviour:
def add_purchase_row(
self,
unit_type: Type[UnitType],
unit_type: Union[AircraftType, Type[VehicleType]],
layout: QLayout,
row: int,
) -> int:
@@ -61,13 +61,7 @@ class QRecruitBehaviour:
existing_units = self.cp.base.total_units_of_type(unit_type)
scheduled_units = self.pending_deliveries.units.get(unit_type, 0)
unitName = QLabel(
"<b>"
+ db.unit_get_expanded_info(
self.game_model.game.player_country, unit_type, "name"
)
+ "</b>"
)
unitName = QLabel(f"<b>{self.name_of(unit_type)}</b>")
unitName.setSizePolicy(
QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
)
@@ -81,7 +75,7 @@ class QRecruitBehaviour:
self.existing_units_labels[unit_type] = existing_units
self.bought_amount_labels[unit_type] = amount_bought
price = QLabel("<b>$ {:02d}</b> m".format(db.PRICES[unit_type]))
price = QLabel(f"<b>$ {self.price_of(unit_type)}</b> M")
price.setSizePolicy(QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed))
buysell = QGroupBox()
@@ -155,7 +149,7 @@ class QRecruitBehaviour:
return row + 1
def _update_count_label(self, unit_type: Type[UnitType]):
def _update_count_label(self, unit_type: Union[AircraftType, Type[VehicleType]]):
self.bought_amount_labels[unit_type].setText(
"<b>{}</b>".format(
@@ -172,32 +166,31 @@ class QRecruitBehaviour:
def update_available_budget(self) -> None:
GameUpdateSignal.get_instance().updateBudget(self.game_model.game)
def buy(self, unit_type: Type[UnitType]):
def buy(self, unit_type: Union[AircraftType, Type[VehicleType]]):
if not self.enable_purchase(unit_type):
logging.error(f"Purchase of {unit_type.id} not allowed at {self.cp.name}")
return
price = db.PRICES[unit_type]
self.pending_deliveries.order({unit_type: 1})
self.budget -= price
self.budget -= self.price_of(unit_type)
self._update_count_label(unit_type)
self.update_available_budget()
def sell(self, unit_type):
if self.pending_deliveries.available_next_turn(unit_type) > 0:
price = db.PRICES[unit_type]
self.budget += price
self.budget += self.price_of(unit_type)
self.pending_deliveries.sell({unit_type: 1})
if self.pending_deliveries.units[unit_type] == 0:
del self.pending_deliveries.units[unit_type]
self._update_count_label(unit_type)
self.update_available_budget()
def enable_purchase(self, unit_type: Type[UnitType]) -> bool:
price = db.PRICES[unit_type]
return self.budget >= price
def enable_purchase(
self, unit_type: Union[AircraftType, Type[VehicleType]]
) -> bool:
return self.budget >= self.price_of(unit_type)
def enable_sale(self, unit_type: Type[UnitType]) -> bool:
def enable_sale(self, unit_type: Union[AircraftType, Type[VehicleType]]) -> bool:
return True
def info(self, unit_type):
@@ -209,3 +202,9 @@ class QRecruitBehaviour:
Set the maximum number of units that can be bought
"""
self.maximum_units = maximum_units
def name_of(self, unit_type: Union[AircraftType, Type[VehicleType]]) -> str:
raise NotImplementedError
def price_of(self, unit_type: Union[AircraftType, Type[VehicleType]]) -> int:
raise NotImplementedError

View File

@@ -1,5 +1,5 @@
import logging
from typing import Set, Type
from typing import Set
from PySide2.QtCore import Qt
from PySide2.QtWidgets import (
@@ -13,9 +13,8 @@ from PySide2.QtWidgets import (
QWidget,
)
from dcs.helicopters import helicopter_map
from dcs.unittype import FlyingType, UnitType
from game import db
from game.dcs.aircrafttype import AircraftType
from game.theater import ControlPoint, ControlPointType
from qt_ui.models import GameModel
from qt_ui.uiconstants import ICONS
@@ -48,13 +47,11 @@ class QAircraftRecruitmentMenu(QFrame, QRecruitBehaviour):
task_box_layout = QGridLayout()
row = 0
unit_types: Set[Type[FlyingType]] = set()
unit_types: Set[AircraftType] = set()
for unit_type in self.game_model.game.player_faction.aircrafts:
if not issubclass(unit_type, FlyingType):
raise RuntimeError(f"Non-flying aircraft found in faction: {unit_type}")
if self.cp.is_carrier and unit_type not in db.CARRIER_CAPABLE:
if self.cp.is_carrier and not unit_type.carrier_capable:
continue
if self.cp.is_lha and unit_type not in db.LHA_CAPABLE:
if self.cp.is_lha and not unit_type.lha_capable:
continue
if (
self.cp.cptype in [ControlPointType.FOB, ControlPointType.FARP]
@@ -65,9 +62,7 @@ class QAircraftRecruitmentMenu(QFrame, QRecruitBehaviour):
sorted_units = sorted(
unit_types,
key=lambda u: db.unit_get_expanded_info(
self.game_model.game.player_country, u, "name"
),
key=lambda u: u.name,
)
for unit_type in sorted_units:
row = self.add_purchase_row(unit_type, task_box_layout, row)
@@ -85,30 +80,33 @@ class QAircraftRecruitmentMenu(QFrame, QRecruitBehaviour):
main_layout.addWidget(scroll)
self.setLayout(main_layout)
def enable_purchase(self, unit_type: Type[UnitType]) -> bool:
def enable_purchase(self, unit_type: AircraftType) -> bool:
if not super().enable_purchase(unit_type):
return False
if not issubclass(unit_type, FlyingType):
return False
if not self.cp.can_operate(unit_type):
return False
return True
def enable_sale(self, unit_type: Type[UnitType]) -> bool:
if not issubclass(unit_type, FlyingType):
return False
def enable_sale(self, unit_type: AircraftType) -> bool:
if not self.cp.can_operate(unit_type):
return False
return True
def buy(self, unit_type):
def name_of(self, unit_type: AircraftType) -> str:
return unit_type.name
def price_of(self, unit_type: AircraftType) -> int:
return unit_type.price
def buy(self, unit_type: AircraftType) -> None:
if self.maximum_units > 0:
if self.cp.unclaimed_parking(self.game_model.game) <= 0:
logging.debug(f"No space for additional aircraft at {self.cp}.")
QMessageBox.warning(
self,
"No space for additional aircraft",
f"There is no parking space left at {self.cp.name} to accommodate another plane.",
f"There is no parking space left at {self.cp.name} to accommodate "
"another plane.",
QMessageBox.Ok,
)
return
@@ -122,7 +120,7 @@ class QAircraftRecruitmentMenu(QFrame, QRecruitBehaviour):
super().buy(unit_type)
self.hangar_status.update_label()
def sell(self, unit_type: UnitType):
def sell(self, unit_type: AircraftType) -> None:
# Don't need to remove aircraft from the inventory if we're canceling
# orders.
if self.pending_deliveries.units.get(unit_type, 0) <= 0:
@@ -134,7 +132,7 @@ class QAircraftRecruitmentMenu(QFrame, QRecruitBehaviour):
QMessageBox.critical(
self,
"Could not sell aircraft",
f"Attempted to sell one {unit_type.id} at {self.cp.name} "
f"Attempted to sell one {unit_type} at {self.cp.name} "
"but none are available. Are all aircraft currently "
"assigned to a mission?",
QMessageBox.Ok,

View File

@@ -8,9 +8,10 @@ from PySide2.QtWidgets import (
QVBoxLayout,
QWidget,
)
from dcs.unittype import UnitType
from dcs.unittype import UnitType, VehicleType
from game import db
from game.db import PRICES
from game.theater import ControlPoint
from qt_ui.models import GameModel
from qt_ui.windows.basemenu.QRecruitBehaviour import QRecruitBehaviour
@@ -65,3 +66,11 @@ class QArmorRecruitmentMenu(QFrame, QRecruitBehaviour):
def enable_sale(self, unit_type: Type[UnitType]) -> bool:
return self.pending_deliveries.pending_orders(unit_type) > 0
def name_of(self, unit_type: Type[VehicleType]) -> str:
return db.unit_get_expanded_info(
self.game_model.game.player_country, unit_type, "name"
)
def price_of(self, unit_type: Type[VehicleType]) -> int:
return PRICES[unit_type]

View File

@@ -28,10 +28,8 @@ class QIntelInfo(QFrame):
units_by_task: dict[str, dict[str, int]] = defaultdict(lambda: defaultdict(int))
for unit_type, count in self.cp.base.aircraft.items():
if count:
name = db.unit_get_expanded_info(
self.game.enemy_country, unit_type, "name"
)
units_by_task[unit_type.task_default.name][name] += count
task_type = unit_type.dcs_unit_type.task_default.name
units_by_task[task_type][unit_type.name] += count
units_by_task = {
task: units_by_task[task] for task in sorted(units_by_task.keys())

View File

@@ -84,10 +84,7 @@ class AircraftIntelLayout(IntelTableLayout):
for airframe, count in base.aircraft.items():
if not count:
continue
self.add_row(
db.unit_get_expanded_info(game.enemy_country, airframe, "name"),
count,
)
self.add_row(airframe.name, count)
self.add_spacer()
self.add_row("<b>Total</b>", total)

View File

@@ -1,6 +1,5 @@
from PySide2.QtGui import QStandardItem, QIcon
from game import db
from gen.ato import Package
from gen.flights.flight import Flight
from gen.flights.traveltime import TotEstimator
@@ -14,11 +13,8 @@ class QFlightItem(QStandardItem):
self.package = package
self.flight = flight
if (
db.unit_type_name(self.flight.unit_type).replace("/", " ")
in AIRCRAFT_ICONS.keys()
):
icon = QIcon((AIRCRAFT_ICONS[db.unit_type_name(self.flight.unit_type)]))
if self.flight.unit_type.dcs_id in AIRCRAFT_ICONS:
icon = QIcon((AIRCRAFT_ICONS[self.flight.unit_type.dcs_id]))
self.setIcon(icon)
self.setEditable(False)
estimator = TotEstimator(self.package)

View File

@@ -1,6 +1,5 @@
from PySide2.QtWidgets import QLabel, QHBoxLayout, QGroupBox, QSpinBox, QGridLayout
from PySide2.QtWidgets import QLabel, QGroupBox, QGridLayout
from game import db
from qt_ui.uiconstants import AIRCRAFT_ICONS
@@ -12,10 +11,8 @@ class QFlightTypeTaskInfo(QGroupBox):
layout = QGridLayout()
self.aircraft_icon = QLabel()
if db.unit_type_name(self.flight.unit_type) in AIRCRAFT_ICONS:
self.aircraft_icon.setPixmap(
AIRCRAFT_ICONS[db.unit_type_name(self.flight.unit_type)]
)
if self.flight.unit_type.dcs_id in AIRCRAFT_ICONS:
self.aircraft_icon.setPixmap(AIRCRAFT_ICONS[self.flight.unit_type.dcs_id])
self.task = QLabel("Task:")
self.task_type = QLabel(str(flight.flight_type))