Skip to content

Commit

Permalink
configure difficulty presets from config, fix #130
Browse files Browse the repository at this point in the history
  • Loading branch information
dkavolis committed Dec 12, 2021
1 parent 7739aad commit f41be9e
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 50 deletions.
95 changes: 45 additions & 50 deletions FerramAerospaceResearch/FARSettingsScenarioModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,41 @@ You should have received a copy of the GNU General Public License
using FerramAerospaceResearch.FARGUI.FARFlightGUI;
using FerramAerospaceResearch.FARPartGeometry;
using FerramAerospaceResearch.Geometry;
using FerramAerospaceResearch.Reflection;
using UniLinq;
using UnityEngine;

namespace FerramAerospaceResearch
{
[KSPScenario(ScenarioCreationOptions.AddToAllGames, GameScenes.SPACECENTER, GameScenes.EDITOR, GameScenes.FLIGHT)]
public class FARSettingsScenarioModule : ScenarioModule
{
private static List<string> presetNames;
[ConfigNode("FARDifficultyPresets", shouldSave: false, isRoot: true)]
public static class FARDifficultyPresetsConfig
{
// reflection shows lots of false positives...

// ReSharper disable once FieldCanBeMadeReadOnly.Global
// ReSharper disable once ConvertToConstant.Global
[ConfigValue("index")] public static int Index = 4;

// ReSharper disable once CollectionNeverUpdated.Global
[ConfigValue] public static readonly List<FARDifficultyAndExactnessSettings> Values = new();

[ConfigValueIgnore] public static string[] Names { get; private set; }

public static void AfterLoaded()
{
Names = Values.Select(preset => preset.name).ToArray();
for (int i = 0; i < Values.Count; ++i)
Values[i].index = i;
}
}

public bool newGame;
public FARDifficultyAndExactnessSettings settings;

public FARDifficultyAndExactnessSettings settings;
public FARDifficultyAndExactnessSettings customSettings;
public List<FARDifficultyAndExactnessSettings> presets;
public FARVoxelSettings voxelSettings;

public List<ConfigNode> flightGUISettings;
Expand Down Expand Up @@ -97,9 +119,9 @@ public static void MainMenuBuildDefaultScenarioModule()
if (Instance != null)
return;
Instance = new GameObject().AddComponent<FARSettingsScenarioModule>();
DontDestroyOnLoad(Instance);
FARLogger.Info("Creating new setting module for tutorial/scenario");
Instance.OnLoad(new ConfigNode());
Instance.Start();
}

private void Start()
Expand Down Expand Up @@ -144,16 +166,15 @@ public override void OnSave(ConfigNode node)
public override void OnLoad(ConfigNode node)
{
Instance = this;
GeneratePresets();
int index = 4;
int index = FARDifficultyPresetsConfig.Index;
if (node.HasValue("newGame"))
newGame = bool.Parse(node.GetValue("newGame"));

if (node.HasValue("index"))
index = int.Parse(node.GetValue("index"));

dropdown = new GUIDropDown<FARDifficultyAndExactnessSettings>(presetNames.ToArray(),
presets.ToArray(),
dropdown = new GUIDropDown<FARDifficultyAndExactnessSettings>(FARDifficultyPresetsConfig.Names,
FARDifficultyPresetsConfig.Values.ToArray(),
index < 0 ? 2 : index);
voxelSettings = new FARVoxelSettings();

Expand Down Expand Up @@ -183,18 +204,16 @@ public override void OnLoad(ConfigNode node)
if (node.HasValue("numDerivSmoothingPasses"))
settings.numDerivSmoothingPasses = int.Parse(node.GetValue("numDerivSmoothingPasses"));


customSettings = settings;
}
else
{
settings = presets[index];
settings = FARDifficultyPresetsConfig.Values[index];
customSettings = new FARDifficultyAndExactnessSettings(-1);
}

currentIndex = index;


FARLogger.Info("Loading FAR Data");
flightGUISettings = new List<ConfigNode>();
if (node.HasNode("FlightGUISettings"))
Expand All @@ -204,36 +223,6 @@ public override void OnLoad(ConfigNode node)
FARDebugAndSettings.LoadConfigs(node);
}

private void GeneratePresets()
{
presets = new List<FARDifficultyAndExactnessSettings>();
presetNames = new List<string>();

var tmp = new FARDifficultyAndExactnessSettings(0.6, 0.03, 2, 2, 0);
presets.Add(tmp);
presetNames.Add("Low Drag, Lenient Area Ruling");

tmp = new FARDifficultyAndExactnessSettings(0.7, 0.03, 2, 2, 1);
presets.Add(tmp);
presetNames.Add("Moderate Drag, Lenient Area Ruling");

tmp = new FARDifficultyAndExactnessSettings(0.7, 0.015, 2, 1, 2);
presets.Add(tmp);
presetNames.Add("Moderate Drag, Moderate Area Ruling");

tmp = new FARDifficultyAndExactnessSettings(0.85, 0.015, 2, 1, 3);
presets.Add(tmp);
presetNames.Add("High Drag, Moderate Area Ruling");

tmp = new FARDifficultyAndExactnessSettings(1, 0.015, 2, 1, 4);
presets.Add(tmp);
presetNames.Add("Full Drag, Moderate Area Ruling");

tmp = new FARDifficultyAndExactnessSettings(1, 0.010, 1, 1, 5);
presets.Add(tmp);
presetNames.Add("Full Drag, Strict Area Ruling");
}

public void DisplaySelection()
{
GUILayout.BeginVertical();
Expand Down Expand Up @@ -278,7 +267,7 @@ public void DisplaySelection()
if (currentIndex >= 0)
currentIndex = -1;
else
currentIndex = 4;
currentIndex = FARDifficultyPresetsConfig.Index;
}

GUILayout.EndHorizontal();
Expand Down Expand Up @@ -321,16 +310,20 @@ public void DisplaySelection()
}
}

[ConfigNode("FARDifficultyAndExactnessSettings", shouldSave: false, isRoot: false, allowMultiple: true)]
public class FARDifficultyAndExactnessSettings
{
// TODO: index should be the index in the list, allow multiple custom settings
public readonly int index;
public double fractionTransonicDrag = 0.7;
public double gaussianVehicleLengthFractionForSmoothing = 0.015;
public int numAreaSmoothingPasses = 2;

public int numDerivSmoothingPasses = 1;

[ConfigValueIgnore] public int index = -1;
[ConfigValue] public double fractionTransonicDrag = 0.7;
[ConfigValue] public double gaussianVehicleLengthFractionForSmoothing = 0.015;
[ConfigValue] public int numAreaSmoothingPasses = 2;
[ConfigValue] public int numDerivSmoothingPasses = 1;
[ConfigValue] public string name = string.Empty;

public FARDifficultyAndExactnessSettings()
{
}

public FARDifficultyAndExactnessSettings(int index)
{
Expand All @@ -342,14 +335,16 @@ public FARDifficultyAndExactnessSettings(
double gaussianLength,
int areaPass,
int derivPass,
int index
int index,
string name = ""
)
{
this.index = index;
fractionTransonicDrag = transDrag;
gaussianVehicleLengthFractionForSmoothing = gaussianLength;
numAreaSmoothingPasses = areaPass;
numDerivSmoothingPasses = derivPass;
this.name = name;
}
}

Expand Down
58 changes: 58 additions & 0 deletions GameData/FerramAerospaceResearch/FARDifficultyPresets.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
FARDifficultyPresets {
// the preset used when starting a new game
index = 4

FARDifficultyAndExactnessSettings {
// the name that will be used in the GUI
// index = 0
name = Low Drag, Lenient Area Ruling

// not used anywhere in the code
fractionTransonicDrag = 0.6

// cross sectional area smoothing parameters
gaussianVehicleLengthFractionForSmoothing = 0.03
numAreaSmoothingPasses = 2
numDerivSmoothingPasses = 2
}

FARDifficultyAndExactnessSettings {
name = Moderate Drag, Lenient Area Ruling
fractionTransonicDrag = 0.7
gaussianVehicleLengthFractionForSmoothing = 0.03
numAreaSmoothingPasses = 2
numDerivSmoothingPasses = 2
}

FARDifficultyAndExactnessSettings {
name = Moderate Drag, Moderate Area Ruling
fractionTransonicDrag = 0.7
gaussianVehicleLengthFractionForSmoothing = 0.015
numAreaSmoothingPasses = 2
numDerivSmoothingPasses = 1
}

FARDifficultyAndExactnessSettings {
name = High Drag, Moderate Area Ruling
fractionTransonicDrag = 0.85
gaussianVehicleLengthFractionForSmoothing = 0.015
numAreaSmoothingPasses = 2
numDerivSmoothingPasses = 1
}

FARDifficultyAndExactnessSettings {
name = Full Drag, Moderate Area Ruling
fractionTransonicDrag = 1
gaussianVehicleLengthFractionForSmoothing = 0.015
numAreaSmoothingPasses = 2
numDerivSmoothingPasses = 1
}

FARDifficultyAndExactnessSettings {
name = Full Drag, Strict Area Ruling
fractionTransonicDrag = 1
gaussianVehicleLengthFractionForSmoothing = 0.010
numAreaSmoothingPasses = 1
numDerivSmoothingPasses = 1
}
}
Binary file not shown.
Binary file not shown.
Binary file modified GameData/FerramAerospaceResearch/Plugins/ferramGraph.dll
Binary file not shown.

0 comments on commit f41be9e

Please sign in to comment.