Merge pull request #114 from DanAlbert/kneeboard

Generate kneeboards for player flights.
This commit is contained in:
C. Perreau 2020-09-01 12:51:04 +02:00 committed by GitHub
commit 7e60a43f53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 437 additions and 5 deletions

1
.gitignore vendored
View File

@ -12,6 +12,7 @@ tests/**
# User-specific stuff
.idea/
/kneeboards
/liberation_preferences.json
/state.json

View File

@ -221,17 +221,28 @@ class Operation:
load_dcs_libe.add_action(DoScript(String(script)))
self.current_mission.triggerrules.triggers.append(load_dcs_libe)
kneeboard_generator = KneeboardGenerator(self.current_mission, self.game)
# Briefing Generation
for i, tanker_type in enumerate(self.airsupportgen.generated_tankers):
self.briefinggen.append_frequency("Tanker {} ({})".format(TANKER_CALLSIGNS[i], tanker_type), "{}X/{} MHz AM".format(60+i, 130+i))
callsign = TANKER_CALLSIGNS[i]
tacan = f"{60 + i}X"
freq = f"{130 + i} MHz AM"
self.briefinggen.append_frequency(f"Tanker {callsign} ({tanker_type})", f"{tacan}/{freq}")
kneeboard_generator.add_tanker(callsign, tanker_type, freq, tacan)
if self.is_awacs_enabled:
self.briefinggen.append_frequency("AWACS", "233 MHz AM")
callsign = "AWACS"
freq = "233 MHz AM"
self.briefinggen.append_frequency(callsign, freq)
kneeboard_generator.add_awacs(callsign, freq)
self.briefinggen.append_frequency("Flight", "251 MHz AM")
kneeboard_generator.add_comm("Flight", "251 MHz AM")
# Generate the briefing
self.briefinggen.generate()
for region, code, name in self.game.jtacs:
kneeboard_generator.add_jtac(name, region, code)
kneeboard_generator.generate()

View File

@ -9,6 +9,7 @@ from .environmentgen import *
from .groundobjectsgen import *
from .briefinggen import *
from .forcedoptionsgen import *
from .kneeboard import *
from . import naming

45
gen/airfields.py Normal file
View File

@ -0,0 +1,45 @@
"""Extra airfield data that is not exposed by pydcs.
Remove once https://github.com/pydcs/dcs/issues/69 tracks getting the missing
data added to pydcs. Until then, missing data can be manually filled in here.
"""
from dataclasses import dataclass, field
from typing import Dict, List, Optional
RadioFrequency = str
@dataclass
class AtcData:
hf: RadioFrequency
vhf_fm: RadioFrequency
vhf_am: RadioFrequency
uhf: RadioFrequency
@dataclass
class AirfieldData:
"""Additional airfield data not included in pydcs."""
#: Radio channels used by the airfield's ATC.
atc: AtcData
#: TACAN channel as a string, i.e. "74X".
tacan: Optional[str] = None
#: Dict of runway heading -> ILS frequency.
ils: Dict[str, RadioFrequency] = field(default_factory=dict)
def ils_freq(self, runway: str) -> Optional[RadioFrequency]:
return self.ils.get(runway)
# TODO: Add more airfields.
AIRFIELD_DATA = {
"Incirlik": AirfieldData(
AtcData("3.85", "38.6", "129.4", "360.1"),
"21X",
{"050": "109.3", "230": "111.7"}
),
}

327
gen/kneeboard.py Normal file
View File

@ -0,0 +1,327 @@
"""Generates kneeboard pages relevant to the player's mission.
The player kneeboard includes the following information:
* Airfield (departure, arrival, divert) info.
* Flight plan (waypoint numbers, names, altitudes).
* Comm channels.
* AWACS info.
* Tanker info.
* JTAC info.
Things we should add:
* Flight plan ToT and fuel ladder (current have neither available).
* Support for planning an arrival/divert airfield separate from departure.
* Mission package infrastructure to include information about the larger
mission, i.e. information about the escort flight for a strike package.
* Target information. Steerpoints, preplanned objectives, ToT, etc.
For multiplayer missions, a kneeboard will be generated per flight.
https://forums.eagle.ru/showthread.php?t=206360 claims that kneeboard pages can
only be added per airframe, so PvP missions where each side have the same
aircraft will be able to see the enemy's kneeboard for the same airframe.
"""
from collections import defaultdict
from dataclasses import dataclass
from pathlib import Path
from typing import Dict, List, Optional, Tuple
from PIL import Image, ImageDraw, ImageFont
from tabulate import tabulate
from pydcs.dcs.mission import Mission
from pydcs.dcs.terrain.terrain import Airport
from pydcs.dcs.unittype import FlyingType
from .airfields import AIRFIELD_DATA
from .flights.flight import Flight
from . import units
class KneeboardPageWriter:
"""Creates kneeboard images."""
def __init__(self, page_margin: int = 24, line_spacing: int = 12) -> None:
self.image = Image.new('RGB', (768, 1024), (0xff, 0xff, 0xff))
# These font sizes create a relatively full page for current sorties. If
# we start generating more complicated flight plans, or start including
# more information in the comm ladder (the latter of which we should
# probably do), we'll need to split some of this information off into a
# second page.
self.title_font = ImageFont.truetype("arial.ttf", 32)
self.heading_font = ImageFont.truetype("arial.ttf", 24)
self.content_font = ImageFont.truetype("arial.ttf", 20)
self.table_font = ImageFont.truetype(
"resources/fonts/Inconsolata.otf", 20)
self.draw = ImageDraw.Draw(self.image)
self.x = page_margin
self.y = page_margin
self.line_spacing = line_spacing
@property
def position(self) -> Tuple[int, int]:
return self.x, self.y
def text(self, text: str, font=None,
fill: Tuple[int, int, int] = (0, 0, 0)) -> None:
if font is None:
font = self.content_font
self.draw.text(self.position, text, font=font, fill=fill)
width, height = self.draw.textsize(text, font=font)
self.y += height + self.line_spacing
def title(self, title: str) -> None:
self.text(title, font=self.title_font)
def heading(self, text: str) -> None:
self.text(text, font=self.heading_font)
def table(self, cells: List[List[str]],
headers: Optional[List[str]] = None) -> None:
table = tabulate(cells, headers=headers, numalign="right")
self.text(table, font=self.table_font)
def write(self, path: Path) -> None:
self.image.save(path)
class KneeboardPage:
"""Base class for all kneeboard pages."""
def write(self, path: Path) -> None:
"""Writes the kneeboard page to the given path."""
raise NotImplementedError
class AirfieldInfo:
def __init__(self, airfield: Airport) -> None:
self.airport = airfield
# TODO: Implement logic for picking preferred runway.
runway = airfield.runways[0]
runway_side = ["", "L", "R"][runway.leftright]
self.runway = f"{runway.heading}{runway_side}"
try:
extra_data = AIRFIELD_DATA[airfield.name]
self.atc = extra_data.atc.uhf or ""
self.tacan = extra_data.tacan or ""
self.ils = extra_data.ils_freq(self.runway) or ""
except KeyError:
self.atc = ""
self.ils = ""
self.tacan = ""
@dataclass
class CommInfo:
"""Communications information for the kneeboard."""
name: str
freq: str
@dataclass
class AwacsInfo:
"""AWACS information for the kneeboard."""
callsign: str
freq: str
@dataclass
class TankerInfo:
"""Tanker information for the kneeboard."""
callsign: str
variant: str
freq: str
tacan: str
@dataclass
class JtacInfo:
"""JTAC information for the kneeboard."""
callsign: str
region: str
code: str
class BriefingPage(KneeboardPage):
"""A kneeboard page containing briefing information."""
def __init__(self, flight: Flight, comms: List[CommInfo],
awacs: List[AwacsInfo], tankers: List[TankerInfo],
jtacs: List[JtacInfo]) -> None:
self.flight = flight
self.comms = comms
self.awacs = awacs
self.tankers = tankers
self.jtacs = jtacs
self.departure = flight.from_cp.airport
self.arrival = flight.from_cp.airport
self.divert: Optional[Airport] = None
def write(self, path: Path) -> None:
writer = KneeboardPageWriter()
# TODO: Assign callsigns to flights and include that info.
# https://github.com/Khopa/dcs_liberation/issues/113
writer.title(f"Mission Info")
# TODO: Handle carriers.
writer.heading("Airfield Info")
writer.table([
self.airfield_info_row("Departure", self.departure),
self.airfield_info_row("Arrival", self.arrival),
self.airfield_info_row("Divert", self.divert),
], headers=["", "Airbase", "ATC", "TCN", "ILS", "RWY"])
writer.heading("Flight Plan")
flight_plan = []
for num, waypoint in enumerate(self.flight.points):
alt = int(units.meters_to_feet(waypoint.alt))
flight_plan.append([num, waypoint.pretty_name, str(alt)])
writer.table(flight_plan, headers=["STPT", "Action", "Alt"])
writer.heading("Comm Ladder")
comms = []
for comm in self.comms:
comms.append([comm.name, comm.freq])
writer.table(comms, headers=["Name", "UHF"])
writer.heading("AWACS")
awacs = []
for a in self.awacs:
awacs.append([a.callsign, a.freq])
writer.table(awacs, headers=["Callsign", "UHF"])
writer.heading("Tankers")
tankers = []
for tanker in self.tankers:
tankers.append([
tanker.callsign,
tanker.variant,
tanker.tacan,
tanker.freq,
])
writer.table(tankers, headers=["Callsign", "Type", "TACAN", "UHF"])
writer.heading("JTAC")
jtacs = []
for jtac in self.jtacs:
jtacs.append([jtac.callsign, jtac.region, jtac.code])
writer.table(jtacs, headers=["Callsign", "Region", "Laser Code"])
writer.write(path)
def airfield_info_row(self, row_title: str,
airfield: Optional[Airport]) -> List[str]:
"""Creates a table row for a given airfield.
Args:
row_title: Purpose of the airfield. e.g. "Departure", "Arrival" or
"Divert".
airfield: The airfield described by this row.
Returns:
A list of strings to be used as a row of the airfield table.
"""
if airfield is None:
return [row_title, "", "", "", "", ""]
info = AirfieldInfo(airfield)
return [
row_title,
airfield.name,
info.atc,
info.tacan,
info.ils,
info.runway,
]
class KneeboardGenerator:
"""Creates kneeboard pages for each client flight in the mission."""
def __init__(self, mission: Mission, game) -> None:
self.mission = mission
self.game = game
self.comms: List[CommInfo] = []
self.awacs: List[AwacsInfo] = []
self.tankers: List[TankerInfo] = []
self.jtacs: List[JtacInfo] = []
def add_comm(self, name: str, freq: str) -> None:
"""Adds communications info to the kneeboard.
Args:
name: Name of the radio channel.
freq: Frequency of the radio channel.
"""
self.comms.append(CommInfo(name, freq))
def add_awacs(self, callsign: str, freq: str) -> None:
"""Adds an AWACS/GCI to the kneeboard.
Args:
callsign: Callsign of the AWACS/GCI.
freq: Radio frequency used by the AWACS/GCI.
"""
self.awacs.append(AwacsInfo(callsign, freq))
def add_tanker(self, callsign: str, variant: str, freq: str,
tacan: str) -> None:
"""Adds a tanker to the kneeboard.
Args:
callsign: Callsign of the tanker.
variant: Aircraft type.
freq: Radio frequency used by the tanker.
tacan: TACAN channel of the tanker.
"""
self.tankers.append(TankerInfo(callsign, variant, freq, tacan))
def add_jtac(self, callsign: str, region: str, code: str) -> None:
"""Adds a JTAC to the kneeboard.
Args:
callsign: Callsign of the JTAC.
region: JTAC's area of responsibility.
code: Laser code used by the JTAC.
"""
# TODO: Radio info? Type?
self.jtacs.append(JtacInfo(callsign, region, code))
def generate(self) -> None:
"""Generates a kneeboard per client flight."""
temp_dir = Path("kneeboards")
temp_dir.mkdir(exist_ok=True)
for aircraft, pages in self.pages_by_airframe().items():
aircraft_dir = temp_dir / aircraft.id
aircraft_dir.mkdir(exist_ok=True)
for idx, page in enumerate(pages):
page_path = aircraft_dir / f"page{idx:02}.png"
page.write(page_path)
self.mission.add_aircraft_kneeboard(aircraft, page_path)
def pages_by_airframe(self) -> Dict[FlyingType, List[KneeboardPage]]:
"""Returns a list of kneeboard pages per airframe in the mission.
Only client flights will be included, but because DCS does not support
group-specific kneeboard pages, flights (possibly from opposing sides)
will be able to see the kneeboards of all aircraft of the same type.
Returns:
A dict mapping aircraft types to the list of kneeboard pages for
that aircraft.
"""
all_flights: Dict[FlyingType, List[KneeboardPage]] = defaultdict(list)
for cp in self.game.theater.controlpoints:
if cp.id in self.game.planners.keys():
for flight in self.game.planners[cp.id].flights:
if flight.client_count > 0:
all_flights[flight.unit_type].extend(
self.generate_flight_kneeboard(flight))
return all_flights
def generate_flight_kneeboard(self, flight: Flight) -> List[KneeboardPage]:
"""Returns a list of kneeboard pages for the given flight."""
return [
BriefingPage(
flight, self.comms, self.awacs, self.tankers, self.jtacs
),
]

6
gen/units.py Normal file
View File

@ -0,0 +1,6 @@
"""Unit conversions."""
def meters_to_feet(meters: float) -> float:
"""Convers meters to feet."""
return meters * 3.28084

2
pydcs

@ -1 +1 @@
Subproject commit dcc3d846316af2925c93ae09840c3ab4a1150e59
Subproject commit d278df68eee2f486f840c178e17893f58313efb8

View File

@ -2,3 +2,6 @@
Pyside2>=5.13.0
pyinstaller==3.6
pyproj==2.6.1.post1
Pillow~=7.2.0
tabulate~=0.8.7

Binary file not shown.

38
resources/fonts/OFL.txt Normal file
View File

@ -0,0 +1,38 @@
—————————————————————————————-
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
—————————————————————————————-
PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide development of collaborative font projects, to support the font creation efforts of academic and linguistic communities, and to provide a free and open framework in which fonts may be shared and improved in partnership with others.
The OFL allows the licensed fonts to be used, studied, modified and redistributed freely as long as they are not sold by themselves. The fonts, including any derivative works, can be bundled, embedded, redistributed and/or sold with any software provided that any reserved names are not used by derivative works. The fonts and derivatives, however, cannot be released under any other type of license. The requirement for fonts to remain under this license does not apply to any document created using the fonts or their derivatives.
DEFINITIONS
“Font Software” refers to the set of files released by the Copyright Holder(s) under this license and clearly marked as such. This may include source files, build scripts and documentation.
“Reserved Font Name” refers to any names specified as such after the copyright statement(s).
“Original Version” refers to the collection of Font Software components as distributed by the Copyright Holder(s).
“Modified Version” refers to any derivative made by adding to, deleting, or substituting—in part or in whole—any of the components of the Original Version, by changing formats or by porting the Font Software to a new environment.
“Author” refers to any designer, engineer, programmer, technical writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining a copy of the Font Software, to use, study, copy, merge, embed, modify, redistribute, and sell modified and unmodified copies of the Font Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components, in Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled, redistributed and/or sold with any software, provided that each copy contains the above copyright notice and this license. These can be included either as stand-alone text files, human-readable headers or in the appropriate machine-readable metadata fields within text or binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font Name(s) unless explicit written permission is granted by the corresponding Copyright Holder. This restriction only applies to the primary font name as presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font Software shall not be used to promote, endorse or advertise any Modified Version, except to acknowledge the contribution(s) of the Copyright Holder(s) and the Author(s) or with their explicit written permission.
5) The Font Software, modified or unmodified, in part or in whole, must be distributed entirely under this license, and must not be distributed under any other license. The requirement for fonts to remain under this license does not apply to any document created using the Font Software.
TERMINATION
This license becomes null and void if any of the above conditions are not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE FONT SOFTWARE.