Skip to content

Commit

Permalink
Remove the unintended feature of deleting all vessels and revise the …
Browse files Browse the repository at this point in the history
…bi-connection process
  • Loading branch information
KSP-TaxiService committed Jun 10, 2017
1 parent 36bbdc8 commit a897639
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 30 deletions.
63 changes: 36 additions & 27 deletions src/CommNetConstellation/CommNetLayer/CNCCommNetVessel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ protected Dictionary<short, double> buildFrequencyList(List<CNCAntennaPartInfo>
if(!powerDict.ContainsKey(antennas[i].frequency))//not found
powerDict.Add(antennas[i].frequency, new double[] { 0.0, 0.0 });

if (antennas[i].antennaCombinable)
if (antennas[i].antennaCombinable) // TODO: revise to best antenna power * (total power / best power) * avg(all expo)
powerDict[antennas[i].frequency][COMINDEX] += (powerDict[antennas[i].frequency][COMINDEX]==0.0) ? antennas[i].antennaPower : antennas[i].antennaCombinableExponent * antennas[i].antennaPower;
else
powerDict[antennas[i].frequency][MAXINDEX] = Math.Max(powerDict[antennas[i].frequency][MAXINDEX], antennas[i].antennaPower);
Expand Down Expand Up @@ -316,53 +316,62 @@ public void validateAndUpgrade(Vessel thisVessel)

CNCLog.Debug("Unloaded CommNet vessel '{0}' is validated and upgraded", thisVessel.GetName());

List<ProtoPartSnapshot> parts = thisVessel.protoVessel.protoPartSnapshots;
for (int i = 0; i < parts.Count; i++)
if (thisVessel.protoVessel != null)
{
if (parts[i].FindModule("ModuleCommand") != null) // check command parts only
List<ProtoPartSnapshot> parts = thisVessel.protoVessel.protoPartSnapshots;
for (int i = 0; i < parts.Count; i++)
{
ProtoPartModuleSnapshot cncModule;
if ((cncModule = parts[i].FindModule("CNConstellationModule")) == null) //check if CNConstellationModule is there
if (parts[i].FindModule("ModuleCommand") != null) // check command parts only
{
CNConstellationModule realcncModule = gameObject.AddComponent<CNConstellationModule>(); // don't use new keyword. PartModule is Monobehavior
parts[i].modules.Add(new ProtoPartModuleSnapshot(realcncModule));
ProtoPartModuleSnapshot cncModule;
if ((cncModule = parts[i].FindModule("CNConstellationModule")) == null) //check if CNConstellationModule is there
{
CNConstellationModule realcncModule = gameObject.AddComponent<CNConstellationModule>(); // don't use new keyword. PartModule is Monobehavior
parts[i].modules.Add(new ProtoPartModuleSnapshot(realcncModule));

CNCLog.Verbose("CNConstellationModule is added to CommNet Vessel '{0}'", thisVessel.GetName());
}
else //check if all attributes are or should not be there
{
if (cncModule.moduleValues.HasValue("radioFrequency")) //obsolete
cncModule.moduleValues.RemoveValue("radioFrequency");
CNCLog.Verbose("CNConstellationModule is added to CommNet Vessel '{0}'", thisVessel.GetName());
}
else //check if all attributes are or should not be there
{
if (cncModule.moduleValues.HasValue("radioFrequency")) //obsolete
cncModule.moduleValues.RemoveValue("radioFrequency");

if (cncModule.moduleValues.HasValue("communicationMembershipFlag")) //obsolete
cncModule.moduleValues.RemoveValue("communicationMembershipFlag");
if (cncModule.moduleValues.HasValue("communicationMembershipFlag")) //obsolete
cncModule.moduleValues.RemoveValue("communicationMembershipFlag");
}
}
}

if (parts[i].FindModule("ModuleDataTransmitter") != null) // check antennas, probe cores and manned cockpits
{
ProtoPartModuleSnapshot cncModule;
if ((cncModule = parts[i].FindModule("CNConstellationAntennaModule")) == null) //check if CNConstellationAntennaModule is there
if (parts[i].FindModule("ModuleDataTransmitter") != null) // check antennas, probe cores and manned cockpits
{
CNConstellationAntennaModule realcncModule = gameObject.AddComponent<CNConstellationAntennaModule>(); // don't use new keyword. PartModule is Monobehavior
parts[i].modules.Add(new ProtoPartModuleSnapshot(realcncModule));
ProtoPartModuleSnapshot cncModule;
if ((cncModule = parts[i].FindModule("CNConstellationAntennaModule")) == null) //check if CNConstellationAntennaModule is there
{
CNConstellationAntennaModule realcncModule = gameObject.AddComponent<CNConstellationAntennaModule>(); // don't use new keyword. PartModule is Monobehavior
parts[i].modules.Add(new ProtoPartModuleSnapshot(realcncModule));

CNCLog.Verbose("CNConstellationAntennaModule is added to CommNet Vessel '{0}'", thisVessel.GetName());
CNCLog.Verbose("CNConstellationAntennaModule is added to CommNet Vessel '{0}'", thisVessel.GetName());
}
}
}
} // end of part loop
} // end of part loop
}
}

protected override void OnSave(ConfigNode gameNode)
{
base.OnSave(gameNode);

if (gameNode.HasNode(GetType().FullName))
gameNode.RemoveNode(GetType().FullName);

gameNode.AddNode(ConfigNode.CreateConfigFromObject(this));
}

protected override void OnLoad(ConfigNode gameNode)
{
base.OnLoad(gameNode);
ConfigNode.LoadObjectFromConfig(this, gameNode.GetNode(GetType().FullName));

if(gameNode.HasNode(GetType().FullName))
ConfigNode.LoadObjectFromConfig(this, gameNode.GetNode(GetType().FullName));
}

public void PersistenceSave()
Expand Down
28 changes: 25 additions & 3 deletions src/CommNetConstellation/CommNetLayer/CNCCommNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace CommNetConstellation.CommNetLayer
{
Expand All @@ -27,8 +28,9 @@ public static bool AreSame(CommNode a, CommNode b)
/// </summary>
protected override bool SetNodeConnection(CommNode a, CommNode b)
{
List<short> aFreqs, bFreqs;//TODO: Revise this
List<short> aFreqs, bFreqs;

//each CommNode has at least frequency?
try
{
aFreqs = CNCCommNetScenario.Instance.getFrequencies(a);
Expand All @@ -40,8 +42,28 @@ protected override bool SetNodeConnection(CommNode a, CommNode b)
return false;
}

int numCommonElements = aFreqs.Intersect(bFreqs).Count();
if (numCommonElements == 0) // no common element in two arrays
//share same frequency?
IEnumerable<short> commonFreqs = aFreqs.Intersect(bFreqs);
if (commonFreqs.Count() == 0) // no common element in two arrays
{
this.Disconnect(a, b, true);
return false;
}

IRangeModel rangeModel = CNCCommNetScenario.RangeModel;
double longestRange = 0.0;

for (int i = 0; i < commonFreqs.Count(); i++)
{
short thisFreq = commonFreqs.ElementAt(i);
double thisRange = rangeModel.GetMaximumRange(CNCCommNetScenario.Instance.getCommPower(a, thisFreq), CNCCommNetScenario.Instance.getCommPower(b, thisFreq));

if (thisRange > longestRange)
longestRange = thisRange;
}

//max range equal or exceed physical distance?
if (longestRange < Vector3.Distance(a.precisePosition, b.precisePosition))
{
this.Disconnect(a, b, true);
return false;
Expand Down

0 comments on commit a897639

Please sign in to comment.