@@ -56,7 +56,8 @@ public class CNCAntennaPartInfo
56
56
public double antennaCombinableExponent ;
57
57
public bool antennaCombinable ;
58
58
public AntennaType antennaType ;
59
- public uint GUID ;
59
+ public Part partReference ;
60
+ public ProtoPartSnapshot partSnapshotReference = null ;
60
61
public bool inUse ; // selected by user to be used
61
62
public bool canComm ; //fixed and deployable antennas
62
63
}
@@ -70,7 +71,7 @@ public enum FrequencyListOperation
70
71
{
71
72
AutoBuild ,
72
73
LockList ,
73
- UpdateOnly
74
+ // UpdateOnly //cannot find any use
74
75
} ;
75
76
76
77
//http://forum.kerbalspaceprogram.com/index.php?/topic/141574-kspfield-questions/&do=findComment&comment=2625815
@@ -177,7 +178,8 @@ protected List<CNCAntennaPartInfo> readAntennaData()
177
178
newAntennaPartInfo . antennaCombinable = thisAntenna . CommCombinable ;
178
179
newAntennaPartInfo . antennaCombinableExponent = thisAntenna . CommCombinableExponent ;
179
180
newAntennaPartInfo . antennaType = thisAntenna . CommType ;
180
- newAntennaPartInfo . GUID = thisPart . craftID ; // good enough
181
+ newAntennaPartInfo . partReference = thisPart ; //unique ID for part is not available
182
+ newAntennaPartInfo . partSnapshotReference = partSnapshot ;
181
183
newAntennaPartInfo . canComm = ( ! this . vessel . loaded ) ? thisAntenna . CanCommUnloaded ( partModuleSnapshot ) : thisAntenna . CanComm ( ) ;
182
184
183
185
populatedAntennaInfo = true ;
@@ -264,19 +266,20 @@ public short getStrongestFrequency()
264
266
/// </summary>
265
267
private short computeStrongestFrequency ( Dictionary < short , double > dict )
266
268
{
267
- short freq = - 1 ;
268
- double power = 0 ;
269
- foreach ( short key in dict . Keys )
270
- {
271
- if ( power < dict [ key ] )
272
- {
273
- power = dict [ key ] ;
274
- freq = key ;
275
- }
276
- }
269
+ if ( dict . Count < 1 )
270
+ return - 1 ;
271
+ else if ( dict . Count == 1 )
272
+ return dict . Keys . First ( ) ;
277
273
278
- //TODO: two freqs of equal comm power? - choose over public; pick lowest freq
274
+ List < KeyValuePair < short , double > > decreasingFreqs = dict . OrderByDescending ( x => x . Value ) . ToList ( ) ;
275
+ short freq = decreasingFreqs [ 0 ] . Key ;
279
276
277
+ if ( freq == CNCSettings . Instance . PublicRadioFrequency )
278
+ {
279
+ if ( decreasingFreqs [ 0 ] . Value == decreasingFreqs [ 1 ] . Value )
280
+ freq = decreasingFreqs [ 1 ] . Key ; // pick next freq of same comm power
281
+ }
282
+
280
283
return freq ;
281
284
}
282
285
@@ -296,7 +299,7 @@ public bool isFreqListEditable()
296
299
/// <summary>
297
300
/// Notify CommNet vessel on antenna change (like changing frequency and deploy/retract antenna)
298
301
/// </summary>
299
- public void OnAntennaChange ( ) //TODO: bad design. replace it with something better
302
+ public void OnAntennaChange ( )
300
303
{
301
304
this . vesselAntennas = readAntennaData ( ) ;
302
305
@@ -308,9 +311,6 @@ public void OnAntennaChange()//TODO: bad design. replace it with something bette
308
311
case FrequencyListOperation . LockList : // dont change current freq dict
309
312
this . strongestFreq = computeStrongestFrequency ( this . FrequencyDict ) ;
310
313
break ;
311
- case FrequencyListOperation . UpdateOnly :
312
- //TODO: complete updateonly function
313
- break ;
314
314
}
315
315
}
316
316
@@ -319,7 +319,11 @@ public void OnAntennaChange()//TODO: bad design. replace it with something bette
319
319
/// </summary>
320
320
public void rebuildFreqList ( bool readAntennaData = false )
321
321
{
322
- if ( ! isFreqListEditable ( ) ) return ;
322
+ if ( ! isFreqListEditable ( ) )
323
+ {
324
+ this . strongestFreq = computeStrongestFrequency ( this . FrequencyDict ) ;
325
+ return ;
326
+ }
323
327
324
328
if ( readAntennaData )
325
329
this . vesselAntennas = this . readAntennaData ( ) ;
@@ -364,26 +368,23 @@ public void clearFreqList()
364
368
/// <summary>
365
369
/// Replace one frequency in the particular antenna
366
370
/// </summary>
367
- public void updateFrequency ( uint GUID , short newFrequency )
371
+ public void updateFrequency ( CNCAntennaPartInfo partInfo , short newFrequency )
368
372
{
369
373
if ( ! Constellation . isFrequencyValid ( newFrequency ) )
370
374
{
371
375
CNCLog . Error ( "New frequency {0} is out of the range [0,{1}]!" , newFrequency , short . MaxValue ) ;
372
376
return ;
373
377
}
374
378
375
- CNCAntennaPartInfo partInfo = this . vesselAntennas . Find ( x => x . GUID == GUID ) ;
376
379
partInfo . frequency = newFrequency ;
377
380
378
381
if ( this . Vessel . loaded )
379
382
{
380
- CNConstellationAntennaModule mod = this . Vessel . FindPartModulesImplementing < CNConstellationAntennaModule > ( ) . Find ( x => x . part . craftID == GUID ) ;
381
- mod . Frequency = newFrequency ;
383
+ partInfo . partReference . FindModuleImplementing < CNConstellationAntennaModule > ( ) . Frequency = newFrequency ;
382
384
}
383
385
else
384
386
{
385
- ProtoPartModuleSnapshot cncAntMod = this . vessel . protoVessel . protoPartSnapshots . Find ( x => x . partInfo . partPrefab . craftID == GUID ) . FindModule ( "CNConstellationAntennaModule" ) ;
386
- cncAntMod . moduleValues . SetValue ( "Frequency" , newFrequency ) ;
387
+ partInfo . partSnapshotReference . FindModule ( "CNConstellationAntennaModule" ) . moduleValues . SetValue ( "Frequency" , newFrequency ) ;
387
388
}
388
389
389
390
CNCLog . Debug ( "Update the antenna of CommNet vessel '{0}' to {1}" , this . Vessel . GetName ( ) , newFrequency ) ;
@@ -423,20 +424,17 @@ public void replaceAllFrequencies(short oldFrequency, short newFrequency)
423
424
/// <summary>
424
425
/// Turn on or off the specific antenna
425
426
/// </summary>
426
- public void toggleAntenna ( uint GUID , bool inUse )
427
+ public void toggleAntenna ( CNCAntennaPartInfo partInfo , bool inUse )
427
428
{
428
- CNCAntennaPartInfo partInfo = this . vesselAntennas . Find ( x => x . GUID == GUID ) ;
429
429
partInfo . inUse = inUse ;
430
430
431
431
if ( this . Vessel . loaded )
432
432
{
433
- CNConstellationAntennaModule mod = this . Vessel . FindPartModulesImplementing < CNConstellationAntennaModule > ( ) . Find ( x => x . part . craftID == GUID ) ;
434
- mod . InUse = inUse ;
433
+ partInfo . partReference . FindModuleImplementing < CNConstellationAntennaModule > ( ) . InUse = inUse ;
435
434
}
436
435
else
437
436
{
438
- ProtoPartModuleSnapshot cncAntMod = this . vessel . protoVessel . protoPartSnapshots . Find ( x => x . partInfo . partPrefab . craftID == GUID ) . FindModule ( "CNConstellationAntennaModule" ) ;
439
- cncAntMod . moduleValues . SetValue ( "InUse" , inUse ) ;
437
+ partInfo . partSnapshotReference . FindModule ( "CNConstellationAntennaModule" ) . moduleValues . SetValue ( "InUse" , inUse ) ;
440
438
}
441
439
442
440
CNCLog . Debug ( "Set the antenna '{0}' of CommNet vessel '{1}' to {2}" , partInfo . name , this . Vessel . GetName ( ) , inUse ) ;
0 commit comments