Skip to content

Commit

Permalink
Complete the function of selecting some antennas for vessel's frequen…
Browse files Browse the repository at this point in the history
…cy list
  • Loading branch information
KSP-TaxiService committed Jun 17, 2017
1 parent 59257ef commit 58f9843
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 51 deletions.
99 changes: 76 additions & 23 deletions src/CommNetConstellation/CommNetLayer/CNCCommNetVessel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class CNConstellationAntennaModule : PartModule
{
[KSPField(isPersistant = true)] public short Frequency = CNCSettings.Instance.PublicRadioFrequency;
[KSPField(isPersistant = true)] protected string OptionalName = "";
[KSPField(isPersistant = true)] public bool InUse = true;

public String Name
{
Expand Down Expand Up @@ -56,6 +57,8 @@ public class CNCAntennaPartInfo
public bool antennaCombinable;
public AntennaType antennaType;
public uint GUID;
public bool inUse; // selected by user to be used
public bool canComm; //fixed and deployable antennas
}

/// <summary>
Expand All @@ -77,8 +80,8 @@ public enum FrequencyListOperation
[Persistent] private List<double> FreqDictionaryValues = new List<double>();
[Persistent] public FrequencyListOperation FreqListOperation = FrequencyListOperation.AutoBuild; // initial value

private short strongestFreq = -1;
private List<CNCAntennaPartInfo> vesselAntennas = new List<CNCAntennaPartInfo>();
protected short strongestFreq = -1;
protected List<CNCAntennaPartInfo> vesselAntennas = new List<CNCAntennaPartInfo>();

/// <summary>
/// Retrieve the CNC data from the vessel
Expand Down Expand Up @@ -140,12 +143,14 @@ protected List<CNCAntennaPartInfo> retrieveAllAntennas()
newAntennaPartInfo.frequency = short.Parse(partModuleSnapshot.moduleValues.GetValue("Frequency"));
string oname = partModuleSnapshot.moduleValues.GetValue("OptionalName");
newAntennaPartInfo.name = (oname.Length == 0) ? partSnapshot.partInfo.title : oname;
newAntennaPartInfo.inUse = bool.Parse(partModuleSnapshot.moduleValues.GetValue("InUse"));
}
else
{
CNConstellationAntennaModule antennaMod = (CNConstellationAntennaModule)thisPartModule;
newAntennaPartInfo.frequency = antennaMod.Frequency;
newAntennaPartInfo.name = antennaMod.Name;
newAntennaPartInfo.inUse = antennaMod.InUse;
}

populatedAntennaInfo = true;
Expand All @@ -162,6 +167,7 @@ protected List<CNCAntennaPartInfo> retrieveAllAntennas()
newAntennaPartInfo.antennaCombinableExponent = thisAntenna.CommCombinableExponent;
newAntennaPartInfo.antennaType = thisAntenna.CommType;
newAntennaPartInfo.GUID = thisPart.craftID; // good enough
newAntennaPartInfo.canComm = (!this.vessel.loaded) ? thisAntenna.CanCommUnloaded(partModuleSnapshot) : thisAntenna.CanComm();

populatedAntennaInfo = true;
}
Expand All @@ -188,14 +194,16 @@ protected Dictionary<short, double> buildFrequencyList(List<CNCAntennaPartInfo>
//read each antenna
for(int i=0; i<antennas.Count; i++)
{
if (!antennas[i].inUse || !antennas[i].canComm) // deselected or retracted
continue;

if(!powerDict.ContainsKey(antennas[i].frequency))//not found
powerDict.Add(antennas[i].frequency, new double[] { 0.0, 0.0 });

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);
CNCLog.Debug(antennas[i].GUID+"");
}

//consolidate into vessel's list of frequencies and their com powers
Expand Down Expand Up @@ -229,17 +237,12 @@ public double getMaxComPower(short frequency)
/// <summary>
/// Get the frequency of the largest Com Power
/// </summary>
public short getStrongestFrequency(bool rebuildFreqList = false)
public short getStrongestFrequency()
{
if (rebuildFreqList)
{
this.vesselAntennas = retrieveAllAntennas();
this.FrequencyDict = buildFrequencyList(vesselAntennas);
this.strongestFreq = computeStrongestFrequency();
}
else if (this.strongestFreq < 0)
if (this.strongestFreq < 0)
{
this.strongestFreq = computeStrongestFrequency();
this.FrequencyDict = buildFrequencyList(this.vesselAntennas);
this.strongestFreq = computeStrongestFrequency(this.FrequencyDict);
}

return this.strongestFreq;
Expand All @@ -248,21 +251,21 @@ public short getStrongestFrequency(bool rebuildFreqList = false)
/// <summary>
/// Find the frequency with the largest Com Power
/// </summary>
private short computeStrongestFrequency()
private short computeStrongestFrequency(Dictionary<short, double> dict)
{
short freq = -1;
double power = 0;
foreach(short key in this.FrequencyDict.Keys)
foreach(short key in dict.Keys)
{
if (power < this.FrequencyDict[key])
if (power < dict[key])
{
power = this.FrequencyDict[key];
power = dict[key];
freq = key;
}
}

if (freq == -1)
CNCLog.Error("No frequency found on this vessel '{0}'", this.Vessel.vesselName);
//if (freq == -1)
// CNCLog.Error("No frequency found on this vessel '{0}'", this.Vessel.vesselName);

return freq;
}
Expand All @@ -277,18 +280,28 @@ public void OnAntennaChange(uint GUID = 0, bool antennaDeployment = true, short
switch (this.FreqListOperation)
{
case FrequencyListOperation.AutoBuild:
this.FrequencyDict = buildFrequencyList(vesselAntennas);
getStrongestFrequency(true);
this.FrequencyDict = buildFrequencyList(this.vesselAntennas);
this.strongestFreq = computeStrongestFrequency(this.FrequencyDict);
break;
case FrequencyListOperation.LockList:
//do nothing
case FrequencyListOperation.LockList: // dont change current freq dict
this.strongestFreq = computeStrongestFrequency(this.FrequencyDict);
break;
case FrequencyListOperation.UpdateOnly:
//TODO: complete updateonly function
break;
}
}

/// <summary>
/// Action to rebuild the frequency list from all antennas
/// </summary>
public void rebuildFreqList()
{
this.vesselAntennas = retrieveAllAntennas();
this.FrequencyDict = buildFrequencyList(this.vesselAntennas);
this.strongestFreq = computeStrongestFrequency(this.FrequencyDict);
}

/// <summary>
/// Replace one frequency in the vessel's antenna
/// </summary>
Expand Down Expand Up @@ -338,11 +351,51 @@ public bool replaceFrequency(short oldFrequency, short newFrequency)
/// <summary>
/// Independent-implementation information on all antennas of the vessel
/// </summary>
public List<CNCAntennaPartInfo> getAntennaInfo()
public List<CNCAntennaPartInfo> getAllAntennaInfo(bool readAntennaData = false)
{
if (readAntennaData)
this.vesselAntennas = retrieveAllAntennas();

return this.vesselAntennas;
}

public void toggleAntenna(uint GUID, bool inUse)
{
CNCAntennaPartInfo partInfo = this.vesselAntennas.Find(x => x.GUID == GUID);
if(partInfo != null)
{
partInfo.inUse = inUse;

int numParts = (!this.vessel.loaded) ? this.vessel.protoVessel.protoPartSnapshots.Count : this.vessel.Parts.Count;
for (int partIndex = 0; partIndex < numParts; partIndex++)
{
if (this.Vessel.loaded)
{
if (this.vessel.Parts[partIndex].craftID == GUID)
{
this.vessel.Parts[partIndex].FindModuleImplementing<CNConstellationAntennaModule>().InUse = inUse;
break;
}
}
else
{
ProtoPartSnapshot partSnapshot = this.vessel.protoVessel.protoPartSnapshots[partIndex];
if (partSnapshot.partInfo.partPrefab.craftID == GUID)
{
ProtoPartModuleSnapshot cncAntMod;
if ((cncAntMod = partSnapshot.FindModule("CNConstellationAntennaModule")) != null)
cncAntMod.moduleValues.SetValue("InUse", inUse);
break;
}
}
}
}
else
{
CNCLog.Error("Cannot find the antenna with GUID {0} to set to be used or not!", GUID);
}
}

/// <summary>
/// Check if given vessel has CNConstellationModule and its attributes required, and if not, "upgrade" the vessel data
/// </summary>
Expand Down
10 changes: 7 additions & 3 deletions src/CommNetConstellation/UI/ConstellationControlDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ private DialogGUIHorizontalLayout createVesselRow(CNCCommNetVessel thisVessel)
}

DialogGUILabel vesselLabel = new DialogGUILabel(thisVessel.Vessel.vesselName, 150, 12);
DialogGUILabel freqLabel = new DialogGUILabel(getFreqString(thisVessel.getFrequencies()), 110, 12);
DialogGUILabel locationLabel = new DialogGUILabel(string.Format("Orbiting: {0}", thisVessel.Vessel.mainBody.name), 140, 12);
DialogGUILabel freqLabel = new DialogGUILabel(getFreqString(thisVessel.getFrequencies()), 150, 12);
DialogGUILabel locationLabel = new DialogGUILabel(string.Format("Orbiting: {0}", thisVessel.Vessel.mainBody.name), 100, 12);
DialogGUIButton setupButton = new DialogGUIButton("Setup", delegate { vesselSetupClick(thisVessel.Vessel); }, 70, 32, false);

DialogGUIHorizontalLayout vesselGroup = new DialogGUIHorizontalLayout(true, false, 4, new RectOffset(), TextAnchor.MiddleCenter, new DialogGUIBase[] { focusButton, vesselLabel, freqLabel, locationLabel, setupButton });
Expand All @@ -347,7 +347,7 @@ private void updateVesselGUIRow(Vessel updatedVessel)

private void vesselSetupClick(Vessel thisVessel)
{
new VesselSetupDialog("Vessel - <color=#00ff00>Setup</color>", thisVessel, null).launch();
new VesselSetupDialog("Vessel - <color=#00ff00>Setup</color>", thisVessel, updateVesselGUIRow).launch();
}

private void vesselFocusClick(Vessel thisVessel)
Expand Down Expand Up @@ -466,6 +466,10 @@ private DialogGUIHorizontalLayout createGroundStationRow(CNCCommNetHome thisStat
private string getFreqString(List<short> frequencies)
{
string freqString = "Frequencies: ";

if (frequencies.Count == 0) // nothing
return "No frequency assigned";

for (int i = 0; i < frequencies.Count; i++)
{
Color color = Constellation.getColor(frequencies[i]);
Expand Down
Loading

0 comments on commit 58f9843

Please sign in to comment.