Skip to content

Commit

Permalink
Resolve the active-flight update issue and make some improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
KSP-TaxiService committed Jan 30, 2017
1 parent 6a92a8a commit 575450f
Show file tree
Hide file tree
Showing 15 changed files with 127 additions and 112 deletions.
Binary file modified GameData/CommNetConstellation/Textures/cnclauncherbutton.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified GameData/CommNetConstellation/Textures/target.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 0 additions & 50 deletions src/CommNetConstellation/CNCUtils.cs

This file was deleted.

1 change: 0 additions & 1 deletion src/CommNetConstellation/CommNetConstellation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
<ItemGroup>
<Compile Include="CNCLog.cs" />
<Compile Include="CNCSettings.cs" />
<Compile Include="CNCUtils.cs" />
<Compile Include="CommNetConstellation.cs" />
<Compile Include="CommNetLayer\CNCCommNetScenario.cs" />
<Compile Include="CommNetLayer\CNCCommNetUI.cs" />
Expand Down
54 changes: 49 additions & 5 deletions src/CommNetConstellation/CommNetLayer/CNCCommNetScenario.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using CommNet;
using Smooth.Algebraics;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
Expand All @@ -14,7 +16,7 @@ public class CNCCommNetScenario : CommNetScenario
* 3) GameScenes.SPACECENTER is recommended so that the constellation data can be verified and error-corrected in advance
*/

private CNCCommNetUI customUI = null;
private CNCCommNetUI CustomCommNetUI = null;
//private CNCCommNetNetwork customNetworkService = null;
public List<Constellation> constellations; // leave the initialisation to OnLoad()

Expand All @@ -29,7 +31,7 @@ protected override void Start()
CNCCommNetScenario.Instance = this;

CommNetUI ui = FindObjectOfType<CommNetUI>();
customUI = ui.gameObject.AddComponent<CNCCommNetUI>();
CustomCommNetUI = ui.gameObject.AddComponent<CNCCommNetUI>();
UnityEngine.Object.Destroy(ui);

CommNetNetwork.Instance.CommNet = new CNCCommNetwork();
Expand Down Expand Up @@ -65,15 +67,14 @@ private void OnDestroy()
//if (this.customNetworkService != null)
// UnityEngine.Object.Destroy(this.customNetworkService);

if (this.customUI != null)
UnityEngine.Object.Destroy(this.customUI);
if (this.CustomCommNetUI != null)
UnityEngine.Object.Destroy(this.CustomCommNetUI);

GameEvents.OnGameSettingsApplied.Remove(new EventVoid.OnEvent(this.customResetNetwork));
}

public void customResetNetwork()
{
CNCLog.Debug("CNCCommNetScenario.customResetNetwork()");
CommNetNetwork.Instance.CommNet = new CNCCommNetwork();
GameEvents.CommNet.OnNetworkInitialized.Fire();
}
Expand Down Expand Up @@ -146,5 +147,48 @@ public override void OnSave(ConfigNode gameNode)
CNCLog.Verbose("Scenario content to be saved:\n{0}", gameNode);
base.OnSave(gameNode);
}

public List<CNCCommNetVessel> getCommNetVessels(short targetRadioFrequency = -1)
{
List<Vessel> vessels = FlightGlobals.fetch.vessels;
List<CNCCommNetVessel> commnetVessels = new List<CNCCommNetVessel>();

for (int i = 0; i < vessels.Count; i++)
{
Vessel thisVessel = vessels[i];
if (thisVessel.Connection != null)
{
CNCCommNetVessel cncVessel = (CNCCommNetVessel)thisVessel.Connection;
if (cncVessel.getRadioFrequency() == targetRadioFrequency || targetRadioFrequency == -1)
{
commnetVessels.Add(cncVessel);
}
}
}

return commnetVessels;
}

public Vessel findCorrespondingVessel(CommNode commNodeRef)
{
List<Vessel> allVessels = FlightGlobals.fetch.vessels;
IEqualityComparer<CommNode> comparer = commNodeRef.Comparer;

//brute-force search temporarily until I find a \omega(n) method
for (int i = 0; i < allVessels.Count(); i++)
{
Vessel thisVessel = allVessels[i];
if (thisVessel.connection != null)
{
if (comparer.Equals(commNodeRef, thisVessel.connection.Comm))
{
return thisVessel;
}
}
}

//not found
return null;
}
}
}
10 changes: 7 additions & 3 deletions src/CommNetConstellation/CommNetLayer/CNCCommNetUI.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using CommNet;
using KSP.UI.Screens.Mapview;
using Smooth.Algebraics;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
Expand All @@ -10,6 +12,8 @@ namespace CommNetConstellation.CommNetLayer
{
public class CNCCommNetUI : CommNetUI
{


public static new CNCCommNetUI Instance
{
get;
Expand All @@ -29,7 +33,7 @@ protected override void UpdateDisplay()

private void coloriseEachConstellation(short radioFrequency, Color newColor)
{
List<CNCCommNetVessel> commnetVessels = CNCUtils.getCommNetVessels(radioFrequency);
List<CNCCommNetVessel> commnetVessels = CNCCommNetScenario.Instance.getCommNetVessels(radioFrequency);

for (int i = 0; i < commnetVessels.Count; i++)
{
Expand All @@ -48,8 +52,8 @@ private Color getConstellationColor(CommNode a, CommNode b)
if (a.isHome || b.isHome)
return Constellation.getColor(CNCSettings.Instance.PublicRadioFrequency); // public

CNCCommNetVessel vesselA = (CNCCommNetVessel)CNCUtils.findCorrespondingVessel(a).Connection;
CNCCommNetVessel vesselB = (CNCCommNetVessel)CNCUtils.findCorrespondingVessel(b).Connection;
CNCCommNetVessel vesselA = (CNCCommNetVessel)CNCCommNetScenario.Instance.findCorrespondingVessel(a).Connection;
CNCCommNetVessel vesselB = (CNCCommNetVessel)CNCCommNetScenario.Instance.findCorrespondingVessel(b).Connection;

if(vesselA.getRadioFrequency() == vesselB.getRadioFrequency())
return Constellation.getColor(vesselA.getRadioFrequency());
Expand Down
78 changes: 53 additions & 25 deletions src/CommNetConstellation/CommNetLayer/CNCCommNetVessel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace CommNetConstellation.CommNetLayer
{
//This class is coupled with the MM patch (cnc_module.cfg) that inserts CNConstellationModule into every command part
//This class is coupled with the MM patch (cnc_module_MM.cfg) that inserts CNConstellationModule into every command part
public class CNConstellationModule : PartModule
{
[KSPField(isPersistant = true)]
Expand All @@ -27,8 +27,6 @@ protected override void OnNetworkInitialized()
if (HighLogic.LoadedScene != GameScenes.FLIGHT && HighLogic.LoadedScene != GameScenes.TRACKSTATION)
return;

//CNCLog.Debug("CNCCommNetVessel.OnNetworkInitialized() @ {0}", this.Vessel.GetName());

base.OnNetworkInitialized();
this.radioFrequency = getRadioFrequency(true);
}
Expand All @@ -38,7 +36,6 @@ public override void OnNetworkPostUpdate()
if (HighLogic.LoadedScene != GameScenes.FLIGHT && HighLogic.LoadedScene != GameScenes.TRACKSTATION)
return;

//CNCLog.Debug("CNCCommNetVessel.OnNetworkPostUpdate()");
base.OnNetworkPostUpdate();
}

Expand All @@ -47,7 +44,6 @@ public override void OnNetworkPreUpdate()
if (HighLogic.LoadedScene != GameScenes.FLIGHT && HighLogic.LoadedScene != GameScenes.TRACKSTATION)
return;

//CNCLog.Debug("CNCCommNetVessel.OnNetworkPreUpdate()");
base.OnNetworkPreUpdate();
}

Expand All @@ -56,7 +52,6 @@ protected override void UpdateComm()
if (HighLogic.LoadedScene != GameScenes.FLIGHT && HighLogic.LoadedScene != GameScenes.TRACKSTATION)
return;

//CNCLog.Debug("CNCCommNetVessel.UpdateComm()");
base.UpdateComm();
}

Expand Down Expand Up @@ -103,6 +98,16 @@ public void updateRadioFrequency(short newFrequency)
}
}

/// <summary>
/// Update the frequency of the specific command part of this active vessel only
/// </summary>
public void updateRadioFrequency(short newFrequency, Part commandPart)
{
CNConstellationModule cncModule = commandPart.FindModuleImplementing<CNConstellationModule>();
cncModule.radioFrequency = newFrequency;
getRadioFrequency(true);
}

/// <summary>
/// If multiple command parts of this vessel have different frequencies, pick the frequency of the first part in order of part addition in editor
/// </summary>
Expand All @@ -112,38 +117,61 @@ public short getRadioFrequency(bool forceRetrievalFromModule = false)
{
if (this.Vessel.loaded)
{
List<CNConstellationModule> modules = this.Vessel.FindPartModulesImplementing<CNConstellationModule>();
if (modules.Count >= 1)
this.radioFrequency = modules[0].radioFrequency; // grab the first command part (part list is sorted in order of part addition in editor)
CNConstellationModule cncModule = firstCommandPartSelection(this.Vessel.parts);
if(cncModule != null)
{
this.radioFrequency = cncModule.radioFrequency;
}
else
CNCLog.Error("getRadioFrequency(): CNConstellationModule not found!");
{
CNCLog.Error("Active CommNet vessel '{0}' does not have any CNConstellationModule! Reset to freq {1}", this.Vessel.GetName(), this.radioFrequency);
this.radioFrequency = CNCSettings.Instance.PublicRadioFrequency;
}
}
else
{
bool success = false;
List<ProtoPartSnapshot> parts = this.Vessel.protoVessel.protoPartSnapshots;

for (int i = 0; i < parts.Count && !success; i++)
ProtoPartModuleSnapshot cncModule = firstCommandPartSelection(this.Vessel.protoVessel.protoPartSnapshots);
if (cncModule != null)
{
ProtoPartModuleSnapshot thisModule = parts[i].FindModule("CNConstellationModule");

if (thisModule != null)
{
this.radioFrequency = short.Parse(thisModule.moduleValues.GetValue("radioFrequency")); // grab the first command part
success = true;
break;
}
this.radioFrequency = short.Parse(cncModule.moduleValues.GetValue("radioFrequency"));
}

if (!success) // fallback
else
{
CNCLog.Error("CommNet vessel '{0}' does not have the frequency module-value! Reset to freq {1}", this.Vessel.GetName(), this.radioFrequency);
CNCLog.Error("Unloaded CommNet vessel '{0}' does not have the frequency module-value! Reset to freq {1}", this.Vessel.GetName(), this.radioFrequency);
this.radioFrequency = CNCSettings.Instance.PublicRadioFrequency;
}
}
}

return this.radioFrequency;
}

public CNConstellationModule firstCommandPartSelection(List<Part> parts)
{
for (int i = 0; i < parts.Count; i++) // grab the first command part (part list is sorted in order of part addition in editor)
{
CNConstellationModule thisModule;
if ((thisModule = parts[i].FindModuleImplementing<CNConstellationModule>()) != null)
{
return thisModule;
}
}

return null;
}

public ProtoPartModuleSnapshot firstCommandPartSelection(List<ProtoPartSnapshot> parts)
{
for (int i = 0; i < parts.Count; i++)
{
ProtoPartModuleSnapshot partModule;
if ((partModule = parts[i].FindModule("CNConstellationModule")) != null)
{
return partModule;
}
}

return null;
}
}
}
4 changes: 2 additions & 2 deletions src/CommNetConstellation/CommNetLayer/CNCCommNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public class CNCCommNetwork : CommNetwork

protected override bool SetNodeConnection(CommNode a, CommNode b)
{
short aFreq = (a.isHome)?publicFreq : ((CNCCommNetVessel)CNCUtils.findCorrespondingVessel(a).Connection).getRadioFrequency();
short bFreq = (b.isHome)?publicFreq : ((CNCCommNetVessel)CNCUtils.findCorrespondingVessel(b).Connection).getRadioFrequency();
short aFreq = (a.isHome)?publicFreq : ((CNCCommNetVessel)CNCCommNetScenario.Instance.findCorrespondingVessel(a).Connection).getRadioFrequency();
short bFreq = (b.isHome)?publicFreq : ((CNCCommNetVessel)CNCCommNetScenario.Instance.findCorrespondingVessel(b).Connection).getRadioFrequency();

if (aFreq != bFreq && aFreq != publicFreq && bFreq != publicFreq)
{
Expand Down
2 changes: 1 addition & 1 deletion src/CommNetConstellation/Constellation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static bool isFrequencyValid(int givenFreq)

public static int countVesselsOf(Constellation thisConstellation)
{
List<CNCCommNetVessel> allVessels = CNCUtils.getCommNetVessels();
List<CNCCommNetVessel> allVessels = CNCCommNetScenario.Instance.getCommNetVessels();
return allVessels.Sum(i => (i.getRadioFrequency() == thisConstellation.frequency) ? 1 : 0);
}

Expand Down
8 changes: 4 additions & 4 deletions src/CommNetConstellation/UI/ConstellationControlDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private List<DialogGUIBase> setupVesselList()
{
List<DialogGUIBase> vesselComponments = new List<DialogGUIBase>();
vesselComponments.Add(new DialogGUIHorizontalLayout(true, false, 0, new RectOffset(), TextAnchor.UpperCenter, new DialogGUIBase[] { new DialogGUILabel("\n<b>You can edit the constellation configuration of an eligible vessel</b>", false, false) }));
List<CNCCommNetVessel> allVessels = CNCUtils.getCommNetVessels();
List<CNCCommNetVessel> allVessels = CNCCommNetScenario.Instance.getCommNetVessels();

List<DialogGUIHorizontalLayout> eachRowGroupList = new List<DialogGUIHorizontalLayout>();
for (int i = 0; i < allVessels.Count; i++)
Expand Down Expand Up @@ -106,7 +106,7 @@ private DialogGUIHorizontalLayout createConstellationRow(Constellation thisConst
private DialogGUIHorizontalLayout createVesselRow(CNCCommNetVessel thisVessel)
{
short radioFreq = thisVessel.getRadioFrequency();
Color color = Constellation.getColor(CNCCommNetScenario.Instance.constellations, radioFreq);
Color color = Constellation.getColor(radioFreq);

UIStyle focusStyle = UIUtils.createImageButtonStyle(focusTexture);
DialogGUIButton focusButton = new DialogGUIButton("", delegate { vesselFocusClick(thisVessel.Vessel); }, null, 32, 32, false, focusStyle);
Expand Down Expand Up @@ -186,7 +186,7 @@ private void deleteConstellation(Constellation deletedConstellation)
return;

short publicFrequency = CNCSettings.Instance.PublicRadioFrequency;
List<CNCCommNetVessel> affectedVessels = CNCUtils.getCommNetVessels().FindAll(x => x.getRadioFrequency() == deletedConstellation.frequency);
List<CNCCommNetVessel> affectedVessels = CNCCommNetScenario.Instance.getCommNetVessels().FindAll(x => x.getRadioFrequency() == deletedConstellation.frequency);
for (int i = 0; i < affectedVessels.Count; i++)
{
affectedVessels[i].updateRadioFrequency(publicFrequency);
Expand Down Expand Up @@ -230,7 +230,7 @@ private void createNewConstellation(Constellation newConstellation)

private void updateConstellation(Constellation updatedConstellation, short previousFrequency)
{
List<CNCCommNetVessel> affectedVessels = CNCUtils.getCommNetVessels().FindAll(x => x.getRadioFrequency() == updatedConstellation.frequency);
List<CNCCommNetVessel> affectedVessels = CNCCommNetScenario.Instance.getCommNetVessels().FindAll(x => x.getRadioFrequency() == updatedConstellation.frequency);
for (int i = 0; i < affectedVessels.Count; i++)
updateVesselGUIRow(affectedVessels[i].Vessel);

Expand Down
2 changes: 1 addition & 1 deletion src/CommNetConstellation/UI/ConstellationEditDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private void actionClick()
if(this.existingConstellation.frequency != CNCSettings.Instance.PublicRadioFrequency) // this is not the public one
this.existingConstellation.frequency = this.conFreq;

List<CNCCommNetVessel> affectedVessels = CNCUtils.getCommNetVessels().FindAll(x => x.getRadioFrequency() == prevFreq);
List<CNCCommNetVessel> affectedVessels = CNCCommNetScenario.Instance.getCommNetVessels().FindAll(x => x.getRadioFrequency() == prevFreq);
for (int i = 0; i < affectedVessels.Count; i++)
affectedVessels[i].updateRadioFrequency(this.existingConstellation.frequency);

Expand Down
Loading

0 comments on commit 575450f

Please sign in to comment.