Skip to content

Commit

Permalink
Complete CNC-vessel's functions, update some infrastructure workings …
Browse files Browse the repository at this point in the history
…and change some interface things
  • Loading branch information
KSP-TaxiService committed Jun 9, 2017
1 parent 41fea0a commit 36bbdc8
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 79 deletions.
19 changes: 19 additions & 0 deletions src/CommNetConstellation/CommNetLayer/CNCCommNetScenario.cs
Expand Up @@ -309,5 +309,24 @@ public List<short> getFrequencies(CommNode a)

return aFreqs;
}

/// <summary>
/// Convenient method to obtain Comm Power of given frequency from a given CommNode
/// </summary>
public double getCommPower(CommNode a, short frequency)
{
double power = 0.0;

if (a.isHome && findCorrespondingGroundStation(a) != null)
{
power = a.antennaRelay.power;
}
else
{
power = ((CNCCommNetVessel)findCorrespondingVessel(a).Connection).getMaxComPower(frequency);
}

return power;
}
}
}
25 changes: 14 additions & 11 deletions src/CommNetConstellation/CommNetLayer/CNCCommNetUI.cs
Expand Up @@ -86,13 +86,9 @@ private void OnMapNodeUpdateVisible(MapNode node, MapNode.IconData iconData)
if(thisVessel != null && node.mapObject.type == MapObject.ObjectType.Vessel)
{
if (thisVessel.getStrongestFrequency() < 0) // blind vessel
{
iconData.color = Color.grey;
}
else
{
iconData.color = Constellation.getColor(thisVessel.getStrongestFrequency());
}
}
}

Expand All @@ -101,16 +97,23 @@ private void OnMapNodeUpdateVisible(MapNode node, MapNode.IconData iconData)
/// </summary>
private Color getConstellationColor(CommNode a, CommNode b)
{
IEnumerable<short> commonFreqs = CNCCommNetScenario.Instance.getFrequencies(a).Intersect(CNCCommNetScenario.Instance.getFrequencies(b));
//Assume the connection between A and B passes the check test
IEnumerable<short> commonFreqs = CNCCommNetScenario.Instance.getFrequencies(a).Intersect(CNCCommNetScenario.Instance.getFrequencies(b));

if (commonFreqs.Count() == 0)
return Constellation.getColor(CNCSettings.Instance.PublicRadioFrequency); // default
IRangeModel rangeModel = CNCCommNetScenario.RangeModel;
short strongestFreq = -1;
double longestRange = 0.0;

short strongestFreq = commonFreqs.ElementAt(0);
int longestRange = 0;
for(int i = 1; i < commonFreqs.Count(); i++)
for (int i = 0; i < commonFreqs.Count(); i++)
{
//if() //TODO: Finish this
short thisFreq = commonFreqs.ElementAt(i);
double thisRange = rangeModel.GetMaximumRange(CNCCommNetScenario.Instance.getCommPower(a, thisFreq), CNCCommNetScenario.Instance.getCommPower(b, thisFreq));

if(thisRange > longestRange)
{
longestRange = thisRange;
strongestFreq = thisFreq;
}
}

return Constellation.getColor(strongestFreq);
Expand Down
107 changes: 48 additions & 59 deletions src/CommNetConstellation/CommNetLayer/CNCCommNetVessel.cs
Expand Up @@ -53,7 +53,7 @@ public class CNCAntennaPartInfo
public double antennaCombinableExponent;
public bool antennaCombinable;
public AntennaType antennaType;
//public uint GUID; //to be determined later
public uint GUID;
}

/// <summary>
Expand Down Expand Up @@ -114,7 +114,8 @@ protected List<CNCAntennaPartInfo> retrieveAllAntennas()
thisPart = partSnapshot.partInfo.partPrefab;
}

CNCAntennaPartInfo newAntennaPartInfo = null;
bool populatedAntennaInfo = false;
CNCAntennaPartInfo newAntennaPartInfo = new CNCAntennaPartInfo(); ;
ProtoPartModuleSnapshot partModuleSnapshot = null;

//inspect each module of the part
Expand All @@ -124,49 +125,41 @@ protected List<CNCAntennaPartInfo> retrieveAllAntennas()

if (thisPartModule is CNConstellationAntennaModule) // is it CNConstellationAntennaModule?
{
string name="";
short freq = 0;

if (!this.Vessel.loaded)
{
partModuleSnapshot = partSnapshot.FindModule(thisPartModule, moduleIndex);
freq = short.Parse(partModuleSnapshot.moduleValues.GetValue("Frequency"));

newAntennaPartInfo.frequency = short.Parse(partModuleSnapshot.moduleValues.GetValue("Frequency"));
string oname = partModuleSnapshot.moduleValues.GetValue("OptionalName");
name = (oname.Length == 0) ? partSnapshot.partInfo.title : oname;
newAntennaPartInfo.name = (oname.Length == 0) ? partSnapshot.partInfo.title : oname;
}
else
{
CNConstellationAntennaModule antennaMod = (CNConstellationAntennaModule)thisPartModule;
freq = antennaMod.Frequency;
name = antennaMod.Name;
newAntennaPartInfo.frequency = antennaMod.Frequency;
newAntennaPartInfo.name = antennaMod.Name;
}

if (newAntennaPartInfo == null)
newAntennaPartInfo = new CNCAntennaPartInfo();

newAntennaPartInfo.frequency = freq;
newAntennaPartInfo.name = name;
populatedAntennaInfo = true;
}
else if (thisPartModule is ICommAntenna) // is it ModuleDataTransmitter?
{
ICommAntenna thisAntenna = thisPartModule as ICommAntenna;

if (!this.Vessel.loaded)
{
partModuleSnapshot = partSnapshot.FindModule(thisPartModule, moduleIndex);
}

if (newAntennaPartInfo == null)
newAntennaPartInfo = new CNCAntennaPartInfo();

newAntennaPartInfo.antennaPower = (!this.vessel.loaded) ? thisAntenna.CommPowerUnloaded(partModuleSnapshot) : thisAntenna.CommPower;
newAntennaPartInfo.antennaCombinable = thisAntenna.CommCombinable;
newAntennaPartInfo.antennaCombinableExponent = thisAntenna.CommCombinableExponent;
newAntennaPartInfo.antennaType = thisAntenna.CommType;
newAntennaPartInfo.GUID = thisPart.craftID; // good enough

populatedAntennaInfo = true;
}
}

if(newAntennaPartInfo != null) // valid info?
if(populatedAntennaInfo) // valid info?
antennas.Add(newAntennaPartInfo);
}

Expand All @@ -191,9 +184,10 @@ protected List<CNCAntennaPartInfo> retrieveAllAntennas()
powerDict.Add(antennas[i].frequency, new double[] { 0.0, 0.0 });

if (antennas[i].antennaCombinable)
powerDict[antennas[i].frequency][COMINDEX] += antennas[i].antennaCombinableExponent * antennas[i].antennaPower;
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 @@ -227,10 +221,18 @@ public double getMaxComPower(short frequency)
/// <summary>
/// Get the frequency of the largest Com Power
/// </summary>
public short getStrongestFrequency()
public short getStrongestFrequency(bool rebuildFreqList = false)
{
if (this.strongestFreq < 0)
if (rebuildFreqList)
{
this.vesselAntennas = retrieveAllAntennas();
this.FrequencyDict = buildFrequencyList(vesselAntennas);
this.strongestFreq = computeStrongestFrequency();
}
else if (this.strongestFreq < 0)
{
this.strongestFreq = computeStrongestFrequency();
}

return this.strongestFreq;
}
Expand Down Expand Up @@ -258,63 +260,50 @@ private short computeStrongestFrequency()
}

/// <summary>
/// Independent-implementation information on all antennas of the vessel
/// </summary>
public List<CNCAntennaPartInfo> getAntennaInfo()
{
return this.vesselAntennas;
}

/// <summary>
/// Rename the vessel's one antenna
/// Update the unloaded vessel's one frequency
/// </summary>
public bool updateAntennaName(string newName, bool rebuildAntennaList = false)
{
return false;//TODO: finish this
}

/// <summary>
/// Update the vessel's one antenna in frequency
/// </summary>
public bool updateAntennaFrequency(short oldFrequency, short newFrequency, bool rebuildFreqList = false)
public bool updateUnloadedFrequency(short oldFrequency, short newFrequency, bool rebuildFreqList = false)
{
try
{
if (!Constellation.isFrequencyValid(newFrequency))
throw new Exception(string.Format("The new frequency {0} is out of the range [0,{1}]!", newFrequency, short.MaxValue));

/*
//TODO: change this
if (this.Vessel.loaded)
{
for (int i = 0; i < loadedAntennaList.Count; i++)
{
if (loadedAntennaList[i].Frequency == oldFrequency)
loadedAntennaList[i].Frequency = newFrequency;
}
}
else
if(this.Vessel.loaded)
throw new Exception("Updating an active vessel through updateUnloadedFrequency() is disallowed.");

for (int i = 0; i < this.vessel.protoVessel.protoPartSnapshots.Count; i++)
{
for (int i = 0; i < protoAntennaList.Count; i++)
ProtoPartSnapshot part = this.vessel.protoVessel.protoPartSnapshots[i];
ProtoPartModuleSnapshot cncAntMod;

if ((cncAntMod = part.FindModule("CNConstellationAntennaModule")) != null)
{
if (short.Parse(protoAntennaList[i].moduleValues.GetValue("Frequency")) == oldFrequency)
protoAntennaList[i].moduleValues.SetValue("Frequency", newFrequency);
if (short.Parse(cncAntMod.moduleValues.GetValue("Frequency")) == oldFrequency)
cncAntMod.moduleValues.SetValue("Frequency", newFrequency);
}
}
*/
}
catch(Exception e)
catch (Exception e)
{
CNCLog.Error("Error encounted when updating CommNet vessel '{0}''s frequency {2} to {3}: {1}", this.Vessel.GetName() , e.Message, oldFrequency, newFrequency);
CNCLog.Error("Error encounted when updating CommNet vessel '{0}''s frequency {2} to {3}: {1}", this.Vessel.GetName(), e.Message, oldFrequency, newFrequency);
return false;
}

getFrequencies();
getStrongestFrequency(true);

CNCLog.Debug("Update CommNet vessel '{0}''s frequency {1} to {2}", this.Vessel.GetName(), oldFrequency, newFrequency);
return true;
}

/// <summary>
/// Independent-implementation information on all antennas of the vessel
/// </summary>
public List<CNCAntennaPartInfo> getAntennaInfo()
{
return this.vesselAntennas;
}

/// <summary>
/// Check if given vessel has CNConstellationModule and its attributes required, and if not, "upgrade" the vessel data
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/CommNetConstellation/CommNetLayer/CNCCommNetwork.cs
Expand Up @@ -27,7 +27,7 @@ public static bool AreSame(CommNode a, CommNode b)
/// </summary>
protected override bool SetNodeConnection(CommNode a, CommNode b)
{
List<short> aFreqs, bFreqs;
List<short> aFreqs, bFreqs;//TODO: Revise this

try
{
Expand Down
6 changes: 3 additions & 3 deletions src/CommNetConstellation/UI/ConstellationControlDialog.cs
Expand Up @@ -232,7 +232,7 @@ private void deleteConstellation(Constellation deletedConstellation)
List<CNCCommNetVessel> affectedVessels = CNCCommNetScenario.Instance.getCommNetVessels().FindAll(x => x.getFrequencies().Contains(deletedConstellation.frequency));
for (int i = 0; i < affectedVessels.Count; i++)
{
affectedVessels[i].updateAntennaFrequency(deletedConstellation.frequency, publicFrequency);
affectedVessels[i].updateUnloadedFrequency(deletedConstellation.frequency, publicFrequency);
updateVesselGUIRow(affectedVessels[i].Vessel);
}

Expand Down Expand Up @@ -293,7 +293,7 @@ private List<DialogGUIBase> setupVesselList()

DialogGUILabel sortLabel = new DialogGUILabel("Sort by");
DialogGUIButton launchSortBtn = new DialogGUIButton("Launch time", delegate { currentVesselSort = VesselListSort.LAUNCHDATE; mapfilterChanged(MapViewFiltering.vesselTypeFilter); }, false);
DialogGUIButton freqSortBtn = new DialogGUIButton("Frequency list", delegate { currentVesselSort = VesselListSort.RADIOFREQ; mapfilterChanged(MapViewFiltering.vesselTypeFilter); }, false);
DialogGUIButton freqSortBtn = new DialogGUIButton("Strongest frequency", delegate { currentVesselSort = VesselListSort.RADIOFREQ; mapfilterChanged(MapViewFiltering.vesselTypeFilter); }, false);
DialogGUIButton nameSortBtn = new DialogGUIButton("Vessel name", delegate { currentVesselSort = VesselListSort.VESSELNAME; mapfilterChanged(MapViewFiltering.vesselTypeFilter); }, false);
DialogGUIButton bodySortBtn = new DialogGUIButton("Celestial body", delegate { currentVesselSort = VesselListSort.CBODY; mapfilterChanged(MapViewFiltering.vesselTypeFilter); }, false);
vesselComponments.Add(new DialogGUIHorizontalLayout(true, false, 0, new RectOffset(), TextAnchor.MiddleLeft, new DialogGUIBase[] { sortLabel, launchSortBtn, freqSortBtn, nameSortBtn, bodySortBtn }));
Expand Down Expand Up @@ -382,7 +382,7 @@ private List<DialogGUIHorizontalLayout> populateVesselRows(VesselTypeFilter filt
switch (currentVesselSort)
{
case VesselListSort.RADIOFREQ:
sortedVessels = allVessels.OrderBy(x => x.getFrequencies()); //TODO: sort frequency list naively?
sortedVessels = allVessels.OrderBy(x => x.getStrongestFrequency());
break;
case VesselListSort.VESSELNAME:
sortedVessels = allVessels.OrderBy(x => x.Vessel.GetName());
Expand Down
2 changes: 1 addition & 1 deletion src/CommNetConstellation/UI/ConstellationEditDialog.cs
Expand Up @@ -197,7 +197,7 @@ private void actionClick()
List<CNCCommNetVessel> affectedVessels = CNCCommNetScenario.Instance.getCommNetVessels().FindAll(x => x.getFrequencies().Contains(prevFreq));
for (int i = 0; i < affectedVessels.Count; i++)
{
affectedVessels[i].updateAntennaFrequency(prevFreq, this.existingConstellation.frequency);
affectedVessels[i].updateUnloadedFrequency(prevFreq, this.existingConstellation.frequency);
}

updateCallback(this.existingConstellation, prevFreq);
Expand Down
9 changes: 5 additions & 4 deletions src/CommNetConstellation/UI/VesselSetupDialog.cs
Expand Up @@ -76,9 +76,9 @@ protected override List<DialogGUIBase> drawContentComponents()
//tools
listComponments.Add(new DialogGUILabel("\n<b>Management tools</b>", false, false));
//Button tabs
DialogGUIButton deactButton = new DialogGUIButton("Antennas", delegate { displayContent(ToolNames.DEACT_ANTENNAS); }, false);
DialogGUIButton nothingButton = new DialogGUIButton("Coming soon", delegate { }, false);
DialogGUIHorizontalLayout tabbedButtonRow = new DialogGUIHorizontalLayout(true, false, 0, new RectOffset(), TextAnchor.MiddleLeft, new DialogGUIBase[] { deactButton, nothingButton, new DialogGUIFlexibleSpace() });
DialogGUIButton deactButton = new DialogGUIButton("Antennas", delegate { displayContent(ToolNames.DEACT_ANTENNAS); }, 40, 32, false);
DialogGUILabel comingSoonLabel = new DialogGUILabel("More coming tools soon!");
DialogGUIHorizontalLayout tabbedButtonRow = new DialogGUIHorizontalLayout(true, false, 0, new RectOffset(), TextAnchor.MiddleLeft, new DialogGUIBase[] { deactButton, new DialogGUISpace(3), comingSoonLabel, new DialogGUIFlexibleSpace() });
listComponments.Add(tabbedButtonRow);

//Tool content
Expand All @@ -90,13 +90,14 @@ protected override List<DialogGUIBase> drawContentComponents()

private DialogGUIHorizontalLayout createFrequencyRow(short freq)
{
CNCCommNetVessel cncVessel = (CNCCommNetVessel)this.hostVessel.Connection;
Color color = Constellation.getColor(freq);
string name = Constellation.getName(freq);

DialogGUIImage colorImage = new DialogGUIImage(new Vector2(32, 32), Vector2.one, color, colorTexture);
DialogGUILabel nameLabel = new DialogGUILabel(name, 150, 12);
DialogGUILabel eachFreqLabel = new DialogGUILabel(string.Format("(<color={0}>{1}</color>)", UIUtils.colorToHex(color), freq), 20, 12);
DialogGUILabel freqPowerLabel = new DialogGUILabel(string.Format("Combined Comm Power: {0}", UIUtils.RoundToNearestMetricFactor(1234)), 150, 12);//TODO: do the comm power combination
DialogGUILabel freqPowerLabel = new DialogGUILabel(string.Format("Combined Comm Power: {0}", UIUtils.RoundToNearestMetricFactor(cncVessel.getMaxComPower(freq))), 150, 12);
return new DialogGUIHorizontalLayout(true, false, 0, new RectOffset(), TextAnchor.MiddleLeft, new DialogGUIBase[] { colorImage, nameLabel, eachFreqLabel, freqPowerLabel });
}

Expand Down

0 comments on commit 36bbdc8

Please sign in to comment.