-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0a3138
commit 394e387
Showing
8 changed files
with
162 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using KSP.Game.Science; | ||
using KSP.Sim; | ||
using KSP.Sim.Definitions; | ||
|
||
namespace WMCCModules.Modules; | ||
|
||
public class Data_ControlRange : ModuleData | ||
{ | ||
public override Type ModuleType => typeof(Module_ControlRange); | ||
|
||
|
||
public class ControlRangeDefinition | ||
{ | ||
public List<string> RequiredTechs; | ||
public Dictionary<string, HashSet<ScienceSitutation>> AllowedScienceSituations; | ||
public string LocalizationKey; | ||
} | ||
|
||
[KSPDefinition] | ||
public List<ControlRangeDefinition> ControlRangeDefinitions; | ||
|
||
[KSPState] public Dictionary<string, HashSet<ScienceSitutation>> UnlockedScienceSituations; | ||
[KSPState] public string CurrentLocalizationKey; | ||
|
||
[LocalizedField("WMCCModules/ControlRange")] [PAMDisplayControl(SortIndex = 1)] | ||
public ModuleProperty<string> ControlRange = new("", true); | ||
|
||
[LocalizedField("WMCCModules/IsControllable")] [PAMDisplayControl(SortIndex = 2)] | ||
public ModuleProperty<bool> IsControllable = new(false, true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using I2.Loc; | ||
using KSP.Game; | ||
using KSP.Game.Science; | ||
using KSP.Sim.Definitions; | ||
|
||
namespace WMCCModules.Modules; | ||
|
||
public class Module_ControlRange : PartBehaviourModule | ||
{ | ||
public override Type PartComponentModuleType => typeof(PartComponentModule_ControlRange); | ||
private Data_ControlRange _dataControlRange; | ||
public override void AddDataModules() | ||
{ | ||
base.AddDataModules(); | ||
_dataControlRange ??= new Data_ControlRange(); | ||
DataModules.TryAddUnique(_dataControlRange, out _dataControlRange); | ||
} | ||
|
||
public override void OnInitialize() | ||
{ | ||
if (PartBackingMode == PartBackingModes.Flight) | ||
{ | ||
moduleIsEnabled = true; | ||
_dataControlRange.SetVisible(_dataControlRange.IsControllable, true); | ||
} | ||
else | ||
{ | ||
_dataControlRange.SetVisible(_dataControlRange.IsControllable, false); | ||
foreach (var definition in _dataControlRange.ControlRangeDefinitions.Where(ComputeControllability)) | ||
{ | ||
foreach (var (key, value) in definition.AllowedScienceSituations) | ||
{ | ||
if (_dataControlRange.UnlockedScienceSituations.TryGetValue(key, out var hs)) | ||
{ | ||
hs.UnionWith(value); | ||
} | ||
else | ||
{ | ||
_dataControlRange.UnlockedScienceSituations.Add(key, new HashSet<ScienceSitutation>(value)); | ||
} | ||
|
||
_dataControlRange.CurrentLocalizationKey = definition.LocalizationKey; | ||
} | ||
} | ||
} | ||
|
||
var str = new LocalizedString(_dataControlRange.CurrentLocalizationKey).ToString() ?? | ||
_dataControlRange.CurrentLocalizationKey; | ||
if (str == "") str = _dataControlRange.CurrentLocalizationKey; | ||
_dataControlRange.ControlRange.SetValue(str); | ||
} | ||
|
||
private bool ComputeControllability(Data_ControlRange.ControlRangeDefinition definition) | ||
{ | ||
if (!GameManager.Instance.GameModeManager.IsGameModeFeatureEnabled("SciencePoints")) return true; | ||
var scienceManager = GameManager.Instance.Game.ScienceManager; | ||
return definition.RequiredTechs.Count == 0 || definition.RequiredTechs.All(tech => scienceManager.IsNodeUnlocked(tech)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/WMCCModules/Modules/PartComponentModule_ControlRange.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using KSP.Game.Science; | ||
using KSP.Sim.impl; | ||
|
||
namespace WMCCModules.Modules; | ||
|
||
public class PartComponentModule_ControlRange : PartComponentModule | ||
{ | ||
public override Type PartBehaviourModuleType => typeof(Module_ControlRange); | ||
private ResearchLocation _lastResearchLocation = null; | ||
public Data_ControlRange DataControlRange; | ||
private PartComponentModule_Command _componentModuleCommand; | ||
public override void OnStart(double universalTime) | ||
{ | ||
if (!DataModules.TryGetByType(out DataControlRange)) | ||
{ | ||
WMCCModulesPlugin.Instance.SWLogger.LogError( | ||
$"Unable to find a Data_ControlRange in the PartComponentModule_ControlRange for {Part.PartName}"); | ||
return; | ||
} | ||
|
||
if (!Part.TryGetModule(out _componentModuleCommand)) | ||
{ | ||
WMCCModulesPlugin.Instance.SWLogger.LogError( | ||
$"Unable to find a PartComponentModule_Command for the PartComponentModule_ControlRange on {Part.PartName}"); | ||
} | ||
} | ||
|
||
public override void OnUpdate(double universalTime, double deltaUniversalTime) | ||
{ | ||
var vessel = Part.PartOwner.SimulationObject.Vessel; | ||
if (vessel.VesselScienceRegionSituation.ResearchLocation.Equals(_lastResearchLocation)) return; | ||
_lastResearchLocation = vessel.VesselScienceRegionSituation.ResearchLocation; | ||
var isControllable = false; | ||
|
||
if (DataControlRange.UnlockedScienceSituations.TryGetValue(_lastResearchLocation.BodyName, out var hs)) | ||
{ | ||
isControllable = hs.Contains(_lastResearchLocation.ScienceSituation); | ||
} | ||
|
||
DataControlRange.IsControllable.SetValue(isControllable); | ||
_componentModuleCommand.UpdateControlStatus(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/WMCCModules/Patches/PartComponentModule_CommandPatch.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using HarmonyLib; | ||
using KSP.Sim.Definitions; | ||
using KSP.Sim.impl; | ||
using WMCCModules.Modules; | ||
|
||
namespace WMCCModules.Patches; | ||
|
||
|
||
[HarmonyPatch(typeof(PartComponentModule_Command))] | ||
public class PartComponentModule_CommandPatch | ||
{ | ||
[HarmonyPatch(nameof(PartComponentModule_Command.UpdateControlStatus))] | ||
[HarmonyPrefix] | ||
public static bool UpdateControlStatus(PartComponentModule_Command __instance) | ||
{ | ||
if (!__instance.Part.Modules.TryGetValue(typeof(PartComponentModule_ControlRange), out var comp)) return true; | ||
var mcr = comp as PartComponentModule_ControlRange; | ||
if (mcr!.DataControlRange.IsControllable.GetValue()) return true; | ||
__instance.dataCommand.controlStatus.SetValue(CommandControlState.NoCommNetConnection); | ||
return false; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters