From d5eaa4d0918ad5f7b03e25feb524893bdf293010 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Sun, 17 Oct 2021 17:36:22 -0700 Subject: [PATCH] Add a `preferred_start_date` for campaigns. Fixes https://github.com/dcs-liberation/dcs_liberation/issues/1079 --- changelog.md | 1 + game/campaignloader/campaign.py | 22 +++++++++++++++++++++- game/version.py | 9 +++++++-- qt_ui/windows/newgame/QNewGameWizard.py | 8 ++++++++ resources/campaigns/black_sea.yaml | 5 ++++- 5 files changed, 41 insertions(+), 4 deletions(-) diff --git a/changelog.md b/changelog.md index 5ec716fb..70a60145 100644 --- a/changelog.md +++ b/changelog.md @@ -19,6 +19,7 @@ Saves from 4.x are not compatible with 5.0. * **[Campaign AI]** Aircraft will now only be automatically purchased or assigned at appropriate bases. Naval aircraft will default to only operating from carriers, Harriers will default to LHAs and shore bases, helicopters will operate from anywhere. This can be customized per-squadron. * **[Engine]** Support for DCS 2.7.5.10869 and newer, including support for F-16 CBU-105s. * **[Modding]** Can now install custom campaigns to /Liberation/Campaigns instead of the Liberation install directory. +* **[Modding]** Campaigns can now define a default start date. * **[Mission Generation]** EWRs are now also headed towards the center of the conflict * **[Mission Generation]** FACs can now use FC3 compatible laser codes. Note that this setting is global, not per FAC. * **[Modding]** Campaigns now specify the squadrons that are present in the campaign, their roles, and their starting bases. Players can customize this at game start but the campaign will choose the defaults. diff --git a/game/campaignloader/campaign.py b/game/campaignloader/campaign.py index 76184df4..e14d6476 100644 --- a/game/campaignloader/campaign.py +++ b/game/campaignloader/campaign.py @@ -1,11 +1,12 @@ from __future__ import annotations +import datetime import json import logging from collections import Iterator from dataclasses import dataclass from pathlib import Path -from typing import Tuple, Dict, Any +from typing import Optional, Tuple, Dict, Any from packaging.version import Version import yaml @@ -46,6 +47,7 @@ class Campaign: recommended_player_faction: str recommended_enemy_faction: str + recommended_start_date: Optional[datetime.date] performance: int data: Dict[str, Any] path: Path @@ -67,6 +69,23 @@ class Campaign: f"Non-string campaign version in {path}. Parse may be incorrect." ) version = Version(str(version_field)) + + start_date_raw = data.get("recommended_start_date") + + # YAML automatically parses dates, but while we still support JSON campaigns we + # need to be able to handle parsing dates from strings ourselves as well. + start_date: Optional[datetime.date] + if isinstance(start_date_raw, str): + start_date = datetime.date.fromisoformat(start_date_raw) + elif isinstance(start_date_raw, datetime.date): + start_date = start_date_raw + elif start_date_raw is None: + start_date = None + else: + raise RuntimeError( + f"Invalid value for recommended_start_date in {path}: {start_date_raw}" + ) + return cls( data["name"], f"Terrain_{sanitized_theater}", @@ -75,6 +94,7 @@ class Campaign: (version.major, version.minor), data.get("recommended_player_faction", "USA 2005"), data.get("recommended_enemy_faction", "Russia 1990"), + start_date, data.get("performance", 0), data, path, diff --git a/game/version.py b/game/version.py index 11d6cee5..f2f59eca 100644 --- a/game/version.py +++ b/game/version.py @@ -115,7 +115,12 @@ VERSION = _build_version_string() #: * You can now add "Invisible FARP" static to FOB to add helicopter slots #: #: Version 9.0 -#: * Campaign files now define the initial squadron layouts. See TODO. +#: * Campaign files now define the initial squadron layouts. See +#: https://github.com/dcs-liberation/dcs_liberation/wiki/Custom-Campaigns. #: * CV and LHA control points now get their names from the group name in the campaign #: miz. -CAMPAIGN_FORMAT_VERSION = (9, 0) +#: +#: Version 9.1 +#: * Campaign files can optionally define a start date with +#: `recommended_start_date: YYYY-MM-DD`. +CAMPAIGN_FORMAT_VERSION = (9, 1) diff --git a/qt_ui/windows/newgame/QNewGameWizard.py b/qt_ui/windows/newgame/QNewGameWizard.py index 40a25649..75644959 100644 --- a/qt_ui/windows/newgame/QNewGameWizard.py +++ b/qt_ui/windows/newgame/QNewGameWizard.py @@ -380,6 +380,14 @@ class TheaterConfiguration(QtWidgets.QWizardPage): template_perf.render({"performance": campaign.performance}) ) + if (start_date := campaign.recommended_start_date) is not None: + self.calendar.setSelectedDate( + QDate(start_date.year, start_date.month, start_date.day) + ) + timePeriodPreset.setChecked(False) + else: + timePeriodPreset.setChecked(True) + self.campaignList.selectionModel().setCurrentIndex( self.campaignList.indexAt(QPoint(1, 1)), QItemSelectionModel.Rows ) diff --git a/resources/campaigns/black_sea.yaml b/resources/campaigns/black_sea.yaml index abb0c89f..f65ce2d4 100644 --- a/resources/campaigns/black_sea.yaml +++ b/resources/campaigns/black_sea.yaml @@ -3,9 +3,12 @@ name: Caucasus - Black Sea theater: Caucasus authors: Colonel Panic description:

A medium sized theater with bases along the coast of the Black Sea.

+recommended_player_faction: USA 2005 +recommended_enemy_faction: Russia 2010 +recommended_start_date: 2004-01-07 miz: black_sea.miz performance: 2 -version: "9.0" +version: "9.1" squadrons: # Anapa-Vityazevo 12: