/
VesselWorker.cs
2717 lines (2566 loc) · 122 KB
/
VesselWorker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.IO;
using DarkMultiPlayerCommon;
using UnityEngine;
namespace DarkMultiPlayer
{
public class VesselWorker
{
public bool workerEnabled;
//Hooks enabled
private bool registered;
//Update frequency
private const float VESSEL_PROTOVESSEL_UPDATE_INTERVAL = 30f;
public float safetyBubbleDistance = 100f;
//Debug delay
public float delayTime = 0f;
//Spectate stuff
private const string DARK_SPECTATE_LOCK = "DMP_Spectating";
private const float UPDATE_SCREEN_MESSAGE_INTERVAL = 1f;
private ScreenMessage spectateMessage;
private float lastSpectateMessageUpdate;
private ScreenMessage bannedPartsMessage;
private string bannedPartsString = "";
private float lastBannedPartsMessageUpdate;
private float lastDockingMessageUpdate;
private ScreenMessage dockingMessage;
//Incoming queue
private object updateQueueLock = new object();
private Dictionary<Guid, Queue<VesselRemoveEntry>> vesselRemoveQueue = new Dictionary<Guid, Queue<VesselRemoveEntry>>();
private Dictionary<Guid, Queue<VesselProtoUpdate>> vesselProtoQueue = new Dictionary<Guid, Queue<VesselProtoUpdate>>();
internal Dictionary<Guid, Queue<VesselUpdate>> vesselUpdateQueue = new Dictionary<Guid, Queue<VesselUpdate>>();
internal Dictionary<Guid, Queue<VesselUpdate>> vesselUpdateMeshQueue = new Dictionary<Guid, Queue<VesselUpdate>>();
private Dictionary<string, Queue<KerbalEntry>> kerbalProtoQueue = new Dictionary<string, Queue<KerbalEntry>>();
private Dictionary<Guid, VesselUpdate> previousUpdates = new Dictionary<Guid, VesselUpdate>();
//Incoming revert support
private Dictionary<Guid, List<VesselRemoveEntry>> vesselRemoveHistory = new Dictionary<Guid, List<VesselRemoveEntry>>();
private Dictionary<Guid, double> vesselRemoveHistoryTime = new Dictionary<Guid, double>();
private Dictionary<Guid, List<VesselProtoUpdate>> vesselProtoHistory = new Dictionary<Guid, List<VesselProtoUpdate>>();
private Dictionary<Guid, double> vesselProtoHistoryTime = new Dictionary<Guid, double>();
//private Dictionary<Guid, List<VesselUpdate>> vesselUpdateHistory = new Dictionary<Guid, List<VesselUpdate>>();
//private Dictionary<Guid, double> vesselUpdateHistoryTime = new Dictionary<Guid, double>();
private Dictionary<string, List<KerbalEntry>> kerbalProtoHistory = new Dictionary<string, List<KerbalEntry>>();
private Dictionary<string, double> kerbalProtoHistoryTime = new Dictionary<string, double>();
private Dictionary<Guid, VesselCtrlUpdate> vesselControlUpdates = new Dictionary<Guid, VesselCtrlUpdate>();
private double lastUniverseTime = double.NegativeInfinity;
//Vessel tracking
private Queue<ActiveVesselEntry> newActiveVessels = new Queue<ActiveVesselEntry>();
private HashSet<Guid> serverVessels = new HashSet<Guid>();
private Dictionary<Guid, bool> vesselPartsOk = new Dictionary<Guid, bool>();
//Vessel state tracking
private Guid lastVesselID;
private Dictionary<Guid, int> vesselPartCount = new Dictionary<Guid, int>();
private Dictionary<Guid, string> vesselNames = new Dictionary<Guid, string>();
private Dictionary<Guid, VesselType> vesselTypes = new Dictionary<Guid, VesselType>();
private Dictionary<Guid, Vessel.Situations> vesselSituations = new Dictionary<Guid, Vessel.Situations>();
//Known kerbals
private Dictionary<string, string> serverKerbals = new Dictionary<string, string>();
//Known vessels and last send/receive time
private Dictionary<Guid, float> serverVesselsProtoUpdate = new Dictionary<Guid, float>();
private Dictionary<Guid, float> serverVesselsPositionUpdate = new Dictionary<Guid, float>();
private Dictionary<Guid, float> serverVesselsPositionUpdateMesh = new Dictionary<Guid, float>();
private Dictionary<Guid, float> serverVesselsPositionUpdateMeshReceive = new Dictionary<Guid, float>();
//Track when the vessel was last controlled.
private Dictionary<Guid, double> latestVesselUpdate = new Dictionary<Guid, double>();
private Dictionary<Guid, double> latestUpdateSent = new Dictionary<Guid, double>();
//Track spectating state
private bool wasSpectating;
private int spectateType;
//KillVessel tracking
private Dictionary<Guid, double> lastKillVesselDestroy = new Dictionary<Guid, double>();
private Dictionary<Guid, double> lastLoadVessel = new Dictionary<Guid, double>();
private List<Vessel> delayKillVessels = new List<Vessel>();
private List<Guid> ignoreVessels = new List<Guid>();
//Docking related
private Vessel newActiveVessel;
private int activeVesselLoadUpdates;
private Guid fromDockedVesselID;
private Guid toDockedVesselID;
private bool sentDockingDestroyUpdate;
//Services
private DMPGame dmpGame;
private Settings dmpSettings;
private ModWorker modWorker;
private LockSystem lockSystem;
private NetworkWorker networkWorker;
private WarpWorker warpWorker;
private TimeSyncer timeSyncer;
private DynamicTickWorker dynamicTickWorker;
private ConfigNodeSerializer configNodeSerializer;
private ChatWorker chatWorker;
private KerbalReassigner kerbalReassigner;
private AsteroidWorker asteroidWorker;
private PartKiller partKiller;
private PlayerStatusWorker playerStatusWorker;
private PosistionStatistics posistionStatistics;
private Permissions permissions;
private NamedAction fixedUpdateAction;
private NamedAction updateAction;
private Profiler profiler;
private VesselRangeBumper vesselRangeBumper;
private PlayerColorWorker playerColorWorker;
public VesselWorker(DMPGame dmpGame, Settings dmpSettings, ModWorker modWorker, LockSystem lockSystem, NetworkWorker networkWorker, ConfigNodeSerializer configNodeSerializer, DynamicTickWorker dynamicTickWorker, KerbalReassigner kerbalReassigner, PartKiller partKiller, PosistionStatistics posistionStatistics, Permissions permissions, Profiler profiler, VesselRangeBumper vesselRangeBumper, PlayerColorWorker playerColorWorker)
{
this.dmpGame = dmpGame;
this.dmpSettings = dmpSettings;
this.modWorker = modWorker;
this.lockSystem = lockSystem;
this.networkWorker = networkWorker;
this.configNodeSerializer = configNodeSerializer;
this.dynamicTickWorker = dynamicTickWorker;
this.kerbalReassigner = kerbalReassigner;
this.partKiller = partKiller;
this.posistionStatistics = posistionStatistics;
this.permissions = permissions;
this.profiler = profiler;
this.vesselRangeBumper = vesselRangeBumper;
this.playerColorWorker = playerColorWorker;
fixedUpdateAction = new NamedAction(FixedUpdate);
dmpGame.fixedUpdateEvent.Add(fixedUpdateAction);
updateAction = new NamedAction(Update);
dmpGame.updateEvent.Add(updateAction);
}
public void SetDependencies(TimeSyncer timeSyncer, WarpWorker warpWorker, AsteroidWorker asteroidWorker, ChatWorker chatWorker, PlayerStatusWorker playerStatusWorker)
{
this.timeSyncer = timeSyncer;
this.warpWorker = warpWorker;
this.asteroidWorker = asteroidWorker;
this.chatWorker = chatWorker;
this.playerStatusWorker = playerStatusWorker;
}
private bool PreUpdateCheck()
{
if (HighLogic.LoadedScene == GameScenes.LOADING)
{
return false;
}
if (Time.timeSinceLevelLoad < 1f)
{
return false;
}
if (HighLogic.LoadedScene == GameScenes.FLIGHT)
{
if (!FlightGlobals.ready)
{
return false;
}
}
if (workerEnabled && !registered)
{
RegisterGameHooks();
}
if (!workerEnabled && registered)
{
UnregisterGameHooks();
}
//If we aren't in a DMP game don't do anything.
if (!workerEnabled)
{
return false;
}
return true;
}
private void FixedUpdate()
{
if (!PreUpdateCheck())
{
return;
}
lock (updateQueueLock)
{
ProcessNewVesselMessages(true);
}
//Send updates of needed vessels
SendVesselUpdates(false);
}
private void Update()
{
if (!PreUpdateCheck())
{
return;
}
//Switch to a new active vessel if needed.
if (newActiveVessel != null)
{
if (FlightGlobals.fetch.vessels.Contains(newActiveVessel))
{
//Hold unpack so we don't collide with the old copy
try
{
OrbitPhysicsManager.HoldVesselUnpack(2);
}
catch
{
}
//If the vessel failed to load in a reasonable time, go through the loading screen
if (activeVesselLoadUpdates > 100)
{
DarkLog.Debug("Active vessel must not be within load distance, go through the loading screen method");
activeVesselLoadUpdates = 0;
FlightGlobals.ForceSetActiveVessel(newActiveVessel);
newActiveVessel = null;
}
if (!newActiveVessel.loaded)
{
activeVesselLoadUpdates++;
return;
}
//Wait 10 updates maybe?
if (activeVesselLoadUpdates < 10)
{
activeVesselLoadUpdates++;
return;
}
activeVesselLoadUpdates = 0;
DarkLog.Debug("Switching to active vessel!");
FlightGlobals.ForceSetActiveVessel(newActiveVessel);
newActiveVessel = null;
return;
}
else
{
DarkLog.Debug("switchActiveVesselOnNextUpdate Vessel failed to spawn into game!");
ScreenMessages.PostScreenMessage("Active vessel update failed to load into the game!", 5, ScreenMessageStyle.UPPER_CENTER);
HighLogic.LoadScene(GameScenes.TRACKSTATION);
}
}
//Kill any queued vessels
foreach (Vessel dyingVessel in delayKillVessels.ToArray())
{
if (FlightGlobals.fetch.vessels.Contains(dyingVessel) && dyingVessel.state != Vessel.State.DEAD)
{
DarkLog.Debug("Delay killing " + dyingVessel.id);
KillVessel(dyingVessel);
}
else
{
delayKillVessels.Remove(dyingVessel);
}
}
if (fromDockedVesselID != Guid.Empty || toDockedVesselID != Guid.Empty)
{
HandleDocking();
}
lock (updateQueueLock)
{
ProcessNewVesselMessages(false);
}
//Update the screen spectate message.
UpdateOnScreenSpectateMessage();
//Lock and unlock spectate state
UpdateSpectateLock();
//Release old update locks
ReleaseOldUpdateLocks();
//Tell other players we have taken a vessel
UpdateActiveVesselStatus();
UpdateBannedPartsMessage();
//Check for vessel changes
CheckVesselHasChanged();
//Send updates of needed vessels
SendVesselUpdates(true);
}
private void RegisterGameHooks()
{
registered = true;
GameEvents.onVesselRecovered.Add(this.OnVesselRecovered);
GameEvents.onVesselTerminated.Add(this.OnVesselTerminated);
GameEvents.onVesselDestroy.Add(this.OnVesselDestroyed);
GameEvents.onVesselRename.Add(this.OnVesselRenamed);
GameEvents.onPartCouple.Add(this.OnVesselDock);
GameEvents.onCrewBoardVessel.Add(this.OnCrewBoard);
GameEvents.onKerbalRemoved.Add(OnKerbalRemoved);
}
private void UnregisterGameHooks()
{
registered = false;
GameEvents.onVesselRecovered.Remove(this.OnVesselRecovered);
GameEvents.onVesselTerminated.Remove(this.OnVesselTerminated);
GameEvents.onVesselDestroy.Remove(this.OnVesselDestroyed);
GameEvents.onVesselRename.Remove(this.OnVesselRenamed);
GameEvents.onPartCouple.Remove(this.OnVesselDock);
GameEvents.onCrewBoardVessel.Remove(this.OnCrewBoard);
GameEvents.onKerbalRemoved.Remove(OnKerbalRemoved);
}
private void HandleDocking()
{
if (sentDockingDestroyUpdate)
{
//One of them will be null, the other one will be the docked craft.
Guid dockedID = fromDockedVesselID != Guid.Empty ? fromDockedVesselID : toDockedVesselID;
//Find the docked craft
Vessel dockedVessel = FlightGlobals.fetch.vessels.FindLast(v => v.id == dockedID);
if (dockedVessel != null ? !dockedVessel.packed : false)
{
if (modWorker.modControl != ModControlMode.DISABLED)
{
CheckVesselParts(dockedVessel);
}
ProtoVessel sendProto = new ProtoVessel(dockedVessel);
if (sendProto != null)
{
if (modWorker.modControl == ModControlMode.DISABLED || vesselPartsOk[dockedVessel.id])
{
DarkLog.Debug("Sending docked protovessel " + dockedID);
//Mark the vessel as sent
serverVesselsProtoUpdate[dockedID] = Client.realtimeSinceStartup;
serverVesselsPositionUpdate[dockedID] = Client.realtimeSinceStartup;
RegisterServerVessel(dockedID);
vesselPartCount[dockedID] = dockedVessel.parts.Count;
vesselNames[dockedID] = dockedVessel.vesselName;
vesselTypes[dockedID] = dockedVessel.vesselType;
vesselSituations[dockedID] = dockedVessel.situation;
//Update status if it's us.
if (dockedVessel == FlightGlobals.fetch.activeVessel)
{
//Release old control locks
if (lastVesselID != FlightGlobals.fetch.activeVessel.id)
{
lockSystem.ReleasePlayerLocksWithPrefix(dmpSettings.playerName, "control-");
lastVesselID = FlightGlobals.fetch.activeVessel.id;
}
//Force the control lock off any other player
lockSystem.AcquireLock("control-" + dockedID, true);
playerStatusWorker.myPlayerStatus.vesselText = FlightGlobals.fetch.activeVessel.vesselName;
}
fromDockedVesselID = Guid.Empty;
toDockedVesselID = Guid.Empty;
sentDockingDestroyUpdate = false;
bool isFlyingUpdate = (sendProto.situation == Vessel.Situations.FLYING);
networkWorker.SendVesselProtoMessage(sendProto, true, isFlyingUpdate);
if (dockingMessage != null)
{
dockingMessage.duration = 0f;
}
dockingMessage = ScreenMessages.PostScreenMessage("Docked!", 3f, ScreenMessageStyle.UPPER_CENTER);
DarkLog.Debug("Docking event over!");
}
else
{
fromDockedVesselID = Guid.Empty;
toDockedVesselID = Guid.Empty;
sentDockingDestroyUpdate = false;
if (dockingMessage != null)
{
dockingMessage.duration = 0f;
}
dockingMessage = ScreenMessages.PostScreenMessage("Error sending vessel - vessel contains invalid parts!", 3f, ScreenMessageStyle.UPPER_CENTER);
DarkLog.Debug("Docking event over - vessel contained invalid parts!");
}
}
else
{
DarkLog.Debug("Error sending protovessel!");
PrintDockingInProgress();
}
}
else
{
PrintDockingInProgress();
}
}
else
{
PrintDockingInProgress();
}
}
private void PrintDockingInProgress()
{
if ((Client.realtimeSinceStartup - lastDockingMessageUpdate) > 1f)
{
lastDockingMessageUpdate = Client.realtimeSinceStartup;
if (dockingMessage != null)
{
dockingMessage.duration = 0f;
}
dockingMessage = ScreenMessages.PostScreenMessage("Docking in progress...", 3f, ScreenMessageStyle.UPPER_CENTER);
}
}
private void ReleaseOldUpdateLocks()
{
List<Guid> removeList = new List<Guid>();
foreach (KeyValuePair<Guid, double> entry in latestUpdateSent)
{
if ((Client.realtimeSinceStartup - entry.Value) > 5f)
{
removeList.Add(entry.Key);
}
}
foreach (Guid removeEntry in removeList)
{
latestUpdateSent.Remove(removeEntry);
if (lockSystem.UpdateLockIsOurs(removeEntry))
{
Vessel sendVessel = FlightGlobals.fetch.vessels.FindLast(v => v.id == removeEntry);
if (sendVessel != null)
{
if (modWorker.modControl != ModControlMode.DISABLED)
{
CheckVesselParts(sendVessel);
}
if (modWorker.modControl == ModControlMode.DISABLED || vesselPartsOk[removeEntry])
{
ProtoVessel sendProto = sendVessel.BackupVessel();
if (sendProto != null)
{
bool isFlyingUpdate = (sendProto.situation == Vessel.Situations.FLYING);
networkWorker.SendVesselProtoMessage(sendProto, false, isFlyingUpdate);
}
}
}
lockSystem.ReleaseLock("update-" + removeEntry);
}
}
}
private void ProcessNewVesselMessages(bool isFixedUpdate)
{
double interpolatorDelay = 0f;
if (dmpSettings.interpolatorType == InterpolatorType.INTERPOLATE1S)
{
interpolatorDelay = 1f;
}
if (dmpSettings.interpolatorType == InterpolatorType.INTERPOLATE3S)
{
interpolatorDelay = 3f;
}
double thisPlanetTime = Planetarium.GetUniversalTime();
double thisDelayTime = delayTime - interpolatorDelay;
if (thisDelayTime < 0f)
{
thisDelayTime = 0f;
}
if (!isFixedUpdate)
{
Dictionary<Guid, double> removeList = new Dictionary<Guid, double>();
lock (vesselRemoveQueue)
{
foreach (KeyValuePair<Guid, Queue<VesselRemoveEntry>> vesselRemoveSubspace in vesselRemoveQueue)
{
while (vesselRemoveSubspace.Value.Count > 0 && ((vesselRemoveSubspace.Value.Peek().planetTime + thisDelayTime + interpolatorDelay) < thisPlanetTime))
{
VesselRemoveEntry removeVessel = vesselRemoveSubspace.Value.Dequeue();
RemoveVessel(removeVessel.vesselID, removeVessel.isDockingUpdate, removeVessel.dockingPlayer);
removeList[removeVessel.vesselID] = removeVessel.planetTime;
}
}
}
foreach (KeyValuePair<string, Queue<KerbalEntry>> kerbalProtoSubspace in kerbalProtoQueue)
{
while (kerbalProtoSubspace.Value.Count > 0 && ((kerbalProtoSubspace.Value.Peek().planetTime + thisDelayTime + interpolatorDelay) < thisPlanetTime))
{
KerbalEntry kerbalEntry = kerbalProtoSubspace.Value.Dequeue();
LoadKerbal(kerbalEntry.kerbalNode);
}
}
foreach (KeyValuePair<Guid, Queue<VesselProtoUpdate>> vesselQueue in vesselProtoQueue)
{
VesselProtoUpdate vpu = null;
//Get the latest proto update
bool applyDelay = vesselUpdateQueue.ContainsKey(vesselQueue.Key) && vesselUpdateQueue[vesselQueue.Key].Count > 0 && vesselUpdateQueue[vesselQueue.Key].Peek().isSurfaceUpdate;
double thisInterpolatorDelay = 0f;
if (applyDelay)
{
thisInterpolatorDelay = interpolatorDelay;
}
while (vesselQueue.Value.Count > 0)
{
if ((vesselQueue.Value.Peek().planetTime + thisDelayTime + thisInterpolatorDelay) < thisPlanetTime)
{
VesselProtoUpdate newVpu = vesselQueue.Value.Dequeue();
if (newVpu != null)
{
//Skip any protovessels that have been removed in the future
if (!removeList.ContainsKey(vesselQueue.Key) || removeList[vesselQueue.Key] < newVpu.planetTime)
{
vpu = newVpu;
}
}
}
else
{
break;
}
}
//Apply it if there is any
if (vpu != null && vpu.vesselNode != null)
{
LoadVessel(vpu.vesselNode, vpu.vesselID, false);
}
}
}
if (isFixedUpdate)
{
foreach (KeyValuePair<Guid, Queue<VesselUpdate>> vesselQueue in vesselUpdateQueue)
{
//If we are receiving messages from mesh vessels, do not apply normal updates, and clear them
if (serverVesselsPositionUpdateMeshReceive.ContainsKey(vesselQueue.Key) && (Client.realtimeSinceStartup - serverVesselsPositionUpdateMeshReceive[vesselQueue.Key] < 10f))
{
while (vesselQueue.Value.Count > 0)
{
VesselUpdate deleteVu = vesselQueue.Value.Dequeue();
Recycler<VesselUpdate>.ReleaseObject(deleteVu);
}
continue;
}
VesselUpdate vu = null;
//Get the latest position update
while (vesselQueue.Value.Count > 0)
{
double thisInterpolatorDelay = 0f;
if (vesselQueue.Value.Peek().isSurfaceUpdate)
{
thisInterpolatorDelay = interpolatorDelay;
}
if ((vesselQueue.Value.Peek().planetTime + thisDelayTime + thisInterpolatorDelay) < thisPlanetTime)
{
//More than 1 update applied in a single update, eg if we come out of timewarp.
if (vu != null)
{
Recycler<VesselUpdate>.ReleaseObject(vu);
}
vu = vesselQueue.Value.Dequeue();
}
else
{
break;
}
}
//Apply it if there is any
if (vu != null)
{
VesselUpdate previousUpdate = null;
if (previousUpdates.ContainsKey(vu.vesselID))
{
previousUpdate = previousUpdates[vu.vesselID];
}
VesselUpdate nextUpdate = null;
if (vesselQueue.Value.Count > 0)
{
nextUpdate = vesselQueue.Value.Peek();
}
vesselRangeBumper.ReportVesselUpdate(vu.vesselID);
vu.Apply(posistionStatistics, vesselControlUpdates, previousUpdate, nextUpdate, dmpSettings);
if (previousUpdate != null)
{
Recycler<VesselUpdate>.ReleaseObject(previousUpdate);
}
previousUpdates[vu.vesselID] = vu;
}
}
foreach (KeyValuePair<Guid, Queue<VesselUpdate>> vesselQueue in vesselUpdateMeshQueue)
{
VesselUpdate vu = null;
//Get the latest position update
while (vesselQueue.Value.Count > 0)
{
double thisInterpolatorDelay = 0f;
if (vesselQueue.Value.Peek().isSurfaceUpdate)
{
thisInterpolatorDelay = interpolatorDelay;
}
if ((vesselQueue.Value.Peek().planetTime + thisDelayTime + thisInterpolatorDelay) < thisPlanetTime)
{
if (vu != null)
{
Recycler<VesselUpdate>.ReleaseObject(vu);
}
vu = vesselQueue.Value.Dequeue();
}
else
{
break;
}
}
//Apply it if there is any
if (vu != null)
{
VesselUpdate previousUpdate = null;
if (previousUpdates.ContainsKey(vu.vesselID))
{
previousUpdate = previousUpdates[vu.vesselID];
}
VesselUpdate nextUpdate = null;
if (vesselQueue.Value.Count > 0)
{
nextUpdate = vesselQueue.Value.Peek();
}
vu.Apply(posistionStatistics, vesselControlUpdates, previousUpdate, nextUpdate, dmpSettings);
if (previousUpdate != null)
{
Recycler<VesselUpdate>.ReleaseObject(previousUpdate);
}
previousUpdates[vu.vesselID] = vu;
}
}
}
}
public void DetectReverting()
{
double newUniverseTime = Planetarium.GetUniversalTime();
//10 second fudge to ignore TimeSyncer skips
if (newUniverseTime < (lastUniverseTime - 10f))
{
int updatesReverted = 0;
DarkLog.Debug("Revert detected!");
timeSyncer.UnlockSubspace();
if (!dmpSettings.revertEnabled)
{
DarkLog.Debug("Unsafe revert detected!");
ScreenMessages.PostScreenMessage("Unsafe revert detected!", 5f, ScreenMessageStyle.UPPER_CENTER);
}
else
{
kerbalProtoQueue.Clear();
vesselProtoQueue.Clear();
vesselUpdateQueue.Clear();
vesselUpdateMeshQueue.Clear();
vesselRemoveQueue.Clear();
//Kerbal queue
KerbalEntry lastKerbalEntry = null;
foreach (KeyValuePair<string, List<KerbalEntry>> kvp in kerbalProtoHistory)
{
bool adding = false;
kerbalProtoQueue.Add(kvp.Key, new Queue<KerbalEntry>());
foreach (KerbalEntry ke in kvp.Value)
{
if (ke.planetTime > newUniverseTime)
{
if (!adding)
{
//One shot - add the previous update before the time to apply instantly
if (lastKerbalEntry != null)
{
kerbalProtoQueue[kvp.Key].Enqueue(lastKerbalEntry);
updatesReverted++;
}
}
adding = true;
}
if (adding)
{
kerbalProtoQueue[kvp.Key].Enqueue(ke);
updatesReverted++;
}
lastKerbalEntry = ke;
}
}
//Vessel proto queue
VesselProtoUpdate lastVesselProtoEntry = null;
foreach (KeyValuePair<Guid, List<VesselProtoUpdate>> kvp in vesselProtoHistory)
{
bool adding = false;
vesselProtoQueue.Add(kvp.Key, new Queue<VesselProtoUpdate>());
foreach (VesselProtoUpdate vpu in kvp.Value)
{
if (vpu.planetTime > newUniverseTime)
{
if (!adding)
{
//One shot - add the previous update before the time to apply instantly
if (lastVesselProtoEntry != null)
{
vesselProtoQueue[kvp.Key].Enqueue(lastVesselProtoEntry);
updatesReverted++;
}
}
adding = true;
}
if (adding)
{
vesselProtoQueue[kvp.Key].Enqueue(vpu);
updatesReverted++;
}
lastVesselProtoEntry = vpu;
}
}
//Vessel update queue
/*
VesselUpdate lastVesselUpdateEntry = null;
foreach (KeyValuePair<Guid, List<VesselUpdate>> kvp in vesselUpdateHistory)
{
bool adding = false;
vesselUpdateQueue.Add(kvp.Key, new Queue<VesselUpdate>());
foreach (VesselUpdate vu in kvp.Value)
{
if (vu.planetTime > newUniverseTime)
{
if (!adding)
{
//One shot - add the previous update before the time to apply instantly
if (lastVesselUpdateEntry != null)
{
vesselUpdateQueue[kvp.Key].Enqueue(lastVesselUpdateEntry);
updatesReverted++;
}
}
adding = true;
}
if (adding)
{
vesselUpdateQueue[kvp.Key].Enqueue(vu);
updatesReverted++;
}
lastVesselUpdateEntry = vu;
}
}
*/
//Remove entries
VesselRemoveEntry lastRemoveEntry = null;
foreach (KeyValuePair<Guid, List<VesselRemoveEntry>> kvp in vesselRemoveHistory)
{
bool adding = false;
vesselRemoveQueue.Add(kvp.Key, new Queue<VesselRemoveEntry>());
foreach (VesselRemoveEntry vre in kvp.Value)
{
if (vre.planetTime > newUniverseTime)
{
if (!adding)
{
//One shot - add the previous update before the time to apply instantly
if (lastRemoveEntry != null)
{
vesselRemoveQueue[kvp.Key].Enqueue(lastRemoveEntry);
updatesReverted++;
}
}
adding = true;
}
if (adding)
{
vesselRemoveQueue[kvp.Key].Enqueue(vre);
updatesReverted++;
}
lastRemoveEntry = vre;
}
}
}
DarkLog.Debug("Reverted " + updatesReverted + " updates");
ScreenMessages.PostScreenMessage("Reverted " + updatesReverted + " updates", 5f, ScreenMessageStyle.UPPER_CENTER);
}
lastUniverseTime = newUniverseTime;
}
private void UpdateOnScreenSpectateMessage()
{
if (HighLogic.LoadedScene != GameScenes.FLIGHT && spectateMessage != null)
{
spectateMessage.duration = 0f;
spectateMessage = null;
}
if ((Client.realtimeSinceStartup - lastSpectateMessageUpdate) > UPDATE_SCREEN_MESSAGE_INTERVAL)
{
lastSpectateMessageUpdate = Client.realtimeSinceStartup;
if (isSpectating)
{
if (spectateMessage != null)
{
return;
}
switch (spectateType)
{
case 1:
spectateMessage = ScreenMessages.PostScreenMessage("This vessel is controlled by another player.", float.MaxValue, ScreenMessageStyle.UPPER_CENTER);
break;
case 2:
spectateMessage = ScreenMessages.PostScreenMessage("This vessel has been changed in the future.", float.MaxValue, ScreenMessageStyle.UPPER_CENTER);
break;
case 3:
//Do nothing, this is handled elsewhere
break;
case 4:
spectateMessage = ScreenMessages.PostScreenMessage("This vessel is protected by " + permissions.vesselPermissions[FlightGlobals.fetch.activeVessel.id].owner + ".", float.MaxValue, ScreenMessageStyle.UPPER_CENTER);
break;
}
}
else
{
if (spectateMessage != null)
{
spectateMessage.duration = 0f;
spectateMessage = null;
}
}
}
}
private void UpdateSpectateLock()
{
if (isSpectating != wasSpectating)
{
wasSpectating = isSpectating;
if (isSpectating)
{
DarkLog.Debug("Setting spectate lock");
InputLockManager.SetControlLock(DMPGuiUtil.BLOCK_ALL_CONTROLS, DARK_SPECTATE_LOCK);
}
else
{
DarkLog.Debug("Releasing spectate lock");
InputLockManager.RemoveControlLock(DARK_SPECTATE_LOCK);
}
}
}
private void UpdateActiveVesselStatus()
{
bool isActiveVesselOk = FlightGlobals.fetch.activeVessel != null ? (FlightGlobals.fetch.activeVessel.loaded && !FlightGlobals.fetch.activeVessel.packed) : false;
if (HighLogic.LoadedScene == GameScenes.FLIGHT && isActiveVesselOk)
{
if (isSpectating && spectateType == 2)
{
if (!lockSystem.ControlLockExists(FlightGlobals.fetch.activeVessel.id))
{
lockSystem.ThrottledAcquireLock("control-" + FlightGlobals.fetch.activeVessel.id);
}
}
if (!isSpectating)
{
//When we change vessel, send the previous flown vessel as soon as possible.
if (lastVesselID != FlightGlobals.fetch.activeVessel.id)
{
if (lastVesselID != Guid.Empty)
{
DarkLog.Debug("Resetting last send time for " + lastVesselID);
serverVesselsProtoUpdate[lastVesselID] = 0f;
lockSystem.ReleasePlayerLocksWithPrefix(dmpSettings.playerName, "control-");
}
//Reset the send time of the vessel we just switched to
serverVesselsProtoUpdate[FlightGlobals.fetch.activeVessel.id] = 0f;
//Nobody else is flying the vessel - let's take it
playerStatusWorker.myPlayerStatus.vesselText = FlightGlobals.fetch.activeVessel.vesselName;
lastVesselID = FlightGlobals.fetch.activeVessel.id;
}
if (!lockSystem.ControlLockExists(FlightGlobals.fetch.activeVessel.id))
{
lockSystem.ThrottledAcquireLock("control-" + FlightGlobals.fetch.activeVessel.id);
}
}
else
{
if (lastVesselID != Guid.Empty)
{
lockSystem.ReleasePlayerLocksWithPrefix(dmpSettings.playerName, "control-");
lastVesselID = Guid.Empty;
playerStatusWorker.myPlayerStatus.vesselText = "";
}
}
}
if (HighLogic.LoadedScene != GameScenes.FLIGHT)
{
//Release the vessel if we aren't in flight anymore.
if (lastVesselID != Guid.Empty)
{
DarkLog.Debug("Releasing " + lastVesselID + " - No longer in flight!");
lockSystem.ReleasePlayerLocksWithPrefix(dmpSettings.playerName, "control-");
lastVesselID = Guid.Empty;
playerStatusWorker.myPlayerStatus.vesselText = "";
}
}
}
private void CheckVesselHasChanged()
{
if (HighLogic.LoadedScene == GameScenes.FLIGHT && FlightGlobals.fetch.activeVessel != null)
{
//Check all vessel for part count changes
foreach (Vessel checkVessel in FlightGlobals.fetch.vessels)
{
if (!isSpectating && checkVessel.loaded && !checkVessel.packed)
{
bool partCountChanged = vesselPartCount.ContainsKey(checkVessel.id) ? checkVessel.parts.Count != vesselPartCount[checkVessel.id] : true;
if (partCountChanged)
{
serverVesselsProtoUpdate[checkVessel.id] = 0f;
vesselPartCount[checkVessel.id] = checkVessel.parts.Count;
if (vesselPartsOk.ContainsKey(checkVessel.id))
{
DarkLog.Debug("Forcing parts recheck on " + checkVessel.id);
vesselPartsOk.Remove(checkVessel.id);
}
}
//Add entries to dictionaries if needed
if (!vesselNames.ContainsKey(checkVessel.id))
{
vesselNames.Add(checkVessel.id, checkVessel.vesselName);
}
if (!vesselTypes.ContainsKey(checkVessel.id))
{
vesselTypes.Add(checkVessel.id, checkVessel.vesselType);
}
if (!vesselSituations.ContainsKey(checkVessel.id))
{
vesselSituations.Add(checkVessel.id, checkVessel.situation);
}
//Check active vessel for situation/renames. Throttle send to 10 seconds.
bool vesselNotRecentlyUpdated = serverVesselsPositionUpdate.ContainsKey(checkVessel.id) ? ((Client.realtimeSinceStartup - serverVesselsProtoUpdate[checkVessel.id]) > 10f) : true;
bool recentlyLanded = vesselSituations[checkVessel.id] != Vessel.Situations.LANDED && checkVessel.situation == Vessel.Situations.LANDED;
bool recentlySplashed = vesselSituations[checkVessel.id] != Vessel.Situations.SPLASHED && checkVessel.situation == Vessel.Situations.SPLASHED;
if (vesselNotRecentlyUpdated || recentlyLanded || recentlySplashed)
{
bool nameChanged = (vesselNames[checkVessel.id] != checkVessel.vesselName);
bool typeChanged = (vesselTypes[checkVessel.id] != checkVessel.vesselType);
bool situationChanged = (vesselSituations[checkVessel.id] != checkVessel.situation);
if (nameChanged || typeChanged || situationChanged)
{
vesselNames[checkVessel.id] = checkVessel.vesselName;
vesselTypes[checkVessel.id] = checkVessel.vesselType;
vesselSituations[checkVessel.id] = checkVessel.situation;
serverVesselsProtoUpdate[checkVessel.id] = 0f;
}
}
}
}
}
}
private bool PreSendVesselUpdatesCheck()
{
if (HighLogic.LoadedScene != GameScenes.FLIGHT)
{
//We aren't in flight so we have nothing to send
return false;
}
if (FlightGlobals.fetch.activeVessel == null)
{
//We don't have an active vessel
return false;
}
if (!FlightGlobals.fetch.activeVessel.loaded || FlightGlobals.fetch.activeVessel.packed)
{
//We haven't loaded into the game yet
return false;
}
if (isSpectating)
{
//Don't send updates in spectate mode
return false;
}
if (fromDockedVesselID != Guid.Empty || toDockedVesselID != Guid.Empty)
{
//Don't send updates while docking
return false;
}
return true;
}
private void UpdateBannedPartsMessage()
{
if (modWorker.modControl == ModControlMode.DISABLED)
{
return;
}
if (!HighLogic.LoadedSceneIsFlight)
{
return;
}
if (FlightGlobals.fetch == null || FlightGlobals.fetch.activeVessel == null)
{