Skip to content

Commit

Permalink
Add setting to configure KSC range
Browse files Browse the repository at this point in the history
Now you can go in settings > mods and choose between 2Gm, 10Gm and 50Gm
  • Loading branch information
rockfactory committed Feb 17, 2024
1 parent d7a921a commit 7e8852c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/CommNext/CommNextPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public override void OnInitialized()
MessageListener.StartListening();

// Settings
Settings.SetupConfig();
PluginSettings.SetupConfig();

// Providers
var providers = new GameObject("CommNext_Providers");
Expand Down
6 changes: 3 additions & 3 deletions src/CommNext/Network/Compute/GetNextConnectedNodesJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public struct GetNextConnectedNodesJob : IJob
BepInEx.Logging.Logger.CreateLogSource("CommNext.GetNextConnectedNodesJob");

[ReadOnly]
public Settings.BestPathMode BestPath;
public PluginSettings.BestPathMode BestPath;

[ReadOnly]
public int StartIndex;
Expand Down Expand Up @@ -246,7 +246,7 @@ public void Execute()
if (!isInLineOfSight) continue;

// Calculate the new best distance
var optimum = BestPath == Settings.BestPathMode.NearestRelay
var optimum = BestPath == PluginSettings.BestPathMode.NearestRelay
? distance
: sourceDistance + distance;

Expand All @@ -264,7 +264,7 @@ public void Execute()
}

sw.Stop();
if (Settings.EnableProfileLogs.Value && DateTime.Now.ToUnixTimestamp() - _lastLoggedTime > 4)
if (PluginSettings.EnableProfileLogs.Value && DateTime.Now.ToUnixTimestamp() - _lastLoggedTime > 4)
{
var connectedCount = 0;
for (var i = 0; i < length; i++)
Expand Down
2 changes: 1 addition & 1 deletion src/CommNext/Network/NetworkNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void UpdateFromVessel(VesselComponent? vessel)

DebugVesselName = vessel.Name;
HasEnoughResources = DifficultyUtils.HasInfinitePower ||
!Settings.RelaysRequirePower.Value ||
!PluginSettings.RelaysRequirePower.Value ||
vessel.ControlStatus != VesselControlState.NoControl;
}
}
11 changes: 9 additions & 2 deletions src/CommNext/Patches/CommNetManagerPatches.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using AwesomeTechnologies;
using CommNext.Network;
using CommNext.UI;
using CommNext.Utils;
using HarmonyLib;
using KSP.Game;
using KSP.Sim;
Expand All @@ -18,7 +19,7 @@ public static SimulationObjectModel FindCommNetOrigin(this UniverseModel univers

private const string KerbinCommNetOriginName = "kerbin_CommNetOrigin";
private const string KerbinSpaceCenterName = "kerbin_KSC_Object";
public const int KSCMaxRange = 2_000_000_000; // 2Gm
private const int KSCFallbackMaxRange = 2_000_000_000; // 2Gm

/// <summary>
/// Fix KSCommNetOrigin position and max range. We place it right on KSC, with
Expand All @@ -40,7 +41,13 @@ public static void SetSourceNode(CommNetManager __instance, ref ConnectionGraphN
}

simObj.transform.Position = kscSimObj.transform.Position;
newSourceNode.MaxRange = KSCMaxRange;
newSourceNode.MaxRange = PluginSettings.KSCRange.Value switch
{
PluginSettings.KSCRangeMode.G2 => 2_000_000_000, // 2Gm
PluginSettings.KSCRangeMode.G10 => 10_000_000_000, // 10Gm
PluginSettings.KSCRangeMode.G50 => 50_000_000_000, // 50Gm
_ => KSCFallbackMaxRange
};

NetworkManager.Instance.Nodes[newSourceNode.Owner].DebugVesselName = LocalizedStrings.KSCCommNet;
}
Expand Down
4 changes: 2 additions & 2 deletions src/CommNext/Patches/ConnectionGraphPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public static bool RebuildNextConnectionGraph(ConnectionGraph __instance,

____jobHandle = new GetNextConnectedNodesJob()
{
BestPath = Settings.BestPath.Value,
BestPath = PluginSettings.BestPath.Value,
Nodes = ____nodes,
StartIndex = sourceNodeIndex,
PrevIndices = ____previousIndices,
Expand Down Expand Up @@ -167,7 +167,7 @@ private static NetworkJobNode GetNetworkNodeFrom(ConnectionGraphNode node)
flagsFrom |= NetworkNodeFlags.HasEnoughResources;

networkJobNode.Flags = flagsFrom;
networkJobNode.Name = Settings.EnableProfileLogs.Value ? networkNode.DebugVesselName : string.Empty;
networkJobNode.Name = PluginSettings.EnableProfileLogs.Value ? networkNode.DebugVesselName : string.Empty;
return networkJobNode;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace CommNext.Utils;

public static class Settings
public static class PluginSettings
{
public enum BestPathMode
{
Expand All @@ -20,11 +20,24 @@ public enum BestPathMode
NearestRelay
}

public enum KSCRangeMode
{
[Description("2Gm")]
G2,

[Description("10Gm")]
G10,

[Description("50Gm")]
G50
}

private static CommNextPlugin Plugin => CommNextPlugin.Instance;

// Network
public static ConfigEntry<BestPathMode> BestPath { get; private set; } = null!;
public static ConfigEntry<bool> RelaysRequirePower { get; private set; } = null!;
public static ConfigEntry<KSCRangeMode> KSCRange { get; private set; } = null!;

// Debug
public static ConfigEntry<bool> EnableProfileLogs { get; private set; } = null!;
Expand All @@ -50,6 +63,14 @@ public static void SetupConfig()
"It requires game to be reloaded to take effect."
);

KSCRange = Plugin.Config.Bind(
"Network",
"KSC range",
KSCRangeMode.G2,
"The range of the KSC in the network.\n" +
"It requires game to be reloaded to take effect."
);

// Debug
EnableProfileLogs = Plugin.Config.Bind(
"Debug",
Expand Down

0 comments on commit 7e8852c

Please sign in to comment.