/// <summary> /// Here we wait until we fully switched to the dominant vessel and THEN we send the vessel dock information. /// We wait 5 seconds before sending the data to give time to the dominant vessel to detect the dock /// </summary> private static IEnumerator WaitUntilWeSwitchedThenSendDockInfo(VesselDockStructure dockInfo) { var start = DateTime.Now; var currentSubspaceId = WarpSystem.Singleton.CurrentSubspace; var waitInterval = new WaitForSeconds(0.5f); while (FlightGlobals.ActiveVessel?.id != dockInfo.DominantVesselId && DateTime.Now - start < TimeSpan.FromSeconds(30)) { yield return(waitInterval); } if (FlightGlobals.ActiveVessel != null && FlightGlobals.ActiveVessel.id == dockInfo.DominantVesselId) { /* We are NOT the dominant vessel so wait 5 seconds so the dominant vessel detects the docking. * If we send the vessel definition BEFORE the dominant detects it, then the dominant won't be able * to undock properly as he will think that he is the weak vessel. */ yield return(new WaitForSeconds(5)); FlightGlobals.ActiveVessel.BackupVessel(); LunaLog.Log($"[LMP]: Sending dock info to the server! Final dominant vessel parts {FlightGlobals.ActiveVessel.protoVessel.protoPartSnapshots.Count}"); System.MessageSender.SendDockInformation(dockInfo, currentSubspaceId, FlightGlobals.ActiveVessel.protoVessel); } }
public void SendDockInformation(VesselDockStructure dock, int subspaceId) { var vesselBytes = VesselSerializer.SerializeVessel(dock.DominantVessel.BackupVessel()); if (vesselBytes.Length > 0) { CreateAndSendDockMessage(dock, subspaceId, vesselBytes); } }
public void SendDockInformation(VesselDockStructure dock) { var vesselBytes = VesselSerializer.SerializeVessel(dock.DominantVessel.protoVessel); if (vesselBytes.Length > 0) { SendMessage(new VesselDockMsgData { WeakVesselId = dock.WeakVesselId, DominantVesselId = dock.DominantVesselId, FinalVesselData = vesselBytes }); } }
public void OnCrewBoard(GameEvents.FromToAction <Part, Part> partAction) { LunaLog.Log("[LMP]: Crew boarding detected!"); if (!VesselCommon.IsSpectating) { LunaLog.Log($"[LMP]: EVA Boarding, from: {partAction.from.vessel.id }, Name: {partAction.from.vessel.vesselName}"); LunaLog.Log($"[LMP]: EVA Boarding, to: {partAction.to.vessel.id}, Name: {partAction.to.vessel.vesselName}"); var dock = new VesselDockStructure(partAction.from.vessel.id, partAction.to.vessel.id); if (dock.StructureIsOk()) { HandleDocking(dock, true); } } }
public void SendDockInformation(VesselDockStructure dock, int subspaceId, ProtoVessel finalDominantVesselProto) { if (finalDominantVesselProto != null) { var vesselBytes = VesselSerializer.SerializeVessel(finalDominantVesselProto); if (vesselBytes.Length > 0) { CreateAndSendDockMessage(dock, subspaceId, vesselBytes); } } else { SendDockInformation(dock, subspaceId); } }
/// <summary> /// Here we handle the "dock" when a kerbal goes into an external seat /// </summary> private static void HandleExternalSeatBoard(VesselDockStructure dock) { var currentSubspaceId = WarpSystem.Singleton.CurrentSubspace; //Kerbal must never be the dominant var temp = dock.DominantVesselId; dock.DominantVesselId = dock.WeakVesselId; dock.WeakVesselId = temp; LunaLog.Log($"[LMP]: Crewboard to an external seat detected! We own the kerbal {dock.WeakVesselId}"); VesselRemoveSystem.Singleton.AddToKillList(dock.WeakVesselId); dock.DominantVessel = FlightGlobals.FindVessel(dock.DominantVesselId); System.MessageSender.SendDockInformation(dock, currentSubspaceId); MainSystem.Singleton.StartCoroutine(WaitUntilWeSwitchedThenSendDockInfo(dock)); }
private void CreateAndSendDockMessage(VesselDockStructure dock, int subspaceId, byte[] vesselBytes) { if (dock == null) { return; } var msgData = NetworkMain.CliMsgFactory.CreateNewMessageData <VesselDockMsgData>(); msgData.WeakVesselId = dock.WeakVesselId; msgData.DominantVesselId = dock.DominantVesselId; msgData.FinalVesselData = vesselBytes; msgData.NumBytes = vesselBytes.Length; msgData.SubspaceId = subspaceId; SendMessage(msgData); }
/// <summary> /// Called when 2 parts couple /// </summary> /// <param name="partAction"></param> public void OnPartCouple(GameEvents.FromToAction <Part, Part> partAction) { if (!VesselCommon.IsSpectating) { if (partAction.from.vessel != null && partAction.to.vessel != null) { var dock = new VesselDockStructure(partAction.from.vessel.id, partAction.to.vessel.id); if (dock.StructureIsOk()) { //We add it to the event so the event is handled AFTER all the docking event in ksp is over and we can //safely remove the weak vessel from the game and save the updated dominant vessel. VesselDockings.Add(dock.DominantVesselId, dock); } } } else { LunaLog.Log("[LMP]: Spectator docking happened. This needs to be fixed later."); } }
/// <summary> /// This method is called after the docking is over and there /// should be only 1 vessel in the screen (the final one) /// </summary> private static void HandleDocking(VesselDockStructure dock) { if (dock.DominantVesselId == FlightGlobals.ActiveVessel.id) { LunaLog.Log($"[LMP]: Docking detected! We own the dominant vessel {dock.DominantVesselId}"); //Backup the vesselproto with the docked vessel data FlightGlobals.ActiveVessel.BackupVessel(); dock.DominantVessel = FlightGlobals.ActiveVessel; System.MessageSender.SendDockInformation(dock); SystemsContainer.Get <VesselRemoveSystem>().AddToKillList(dock.WeakVessel, true); } else if (dock.WeakVesselId == FlightGlobals.ActiveVessel.id) { LunaLog.Log($"[LMP]: Docking detected! We DON'T own the dominant vessel {dock.DominantVesselId}"); //Switch to the dominant vessel SystemsContainer.Get <VesselSwitcherSystem>().SwitchToVessel(dock.DominantVesselId); System.MessageSender.SendDockInformation(dock); SystemsContainer.Get <VesselRemoveSystem>().AddToKillList(dock.WeakVessel, true); } }
/// <summary> /// This method is called after the docking is over and there /// should be only 1 vessel in the screen (the final one) /// </summary> private static void HandleDocking(VesselDockStructure dock, bool eva) { var currentSubspaceId = WarpSystem.Singleton.CurrentSubspace; if (dock.DominantVesselId == FlightGlobals.ActiveVessel?.id) { JumpIfVesselOwnerIsInFuture(dock.WeakVesselId); if (eva) { //The kerbal should never be the dominant! var temp = dock.DominantVesselId; dock.DominantVesselId = dock.WeakVesselId; dock.WeakVesselId = temp; LunaLog.Log($"[LMP]: Crewboard detected! We own the kerbal {dock.WeakVesselId}"); VesselRemoveSystem.Singleton.AddToKillList(dock.WeakVesselId); dock.DominantVessel = FlightGlobals.FindVessel(dock.DominantVesselId); System.MessageSender.SendDockInformation(dock, currentSubspaceId); } else { LunaLog.Log($"[LMP]: Docking detected! We own the dominant vessel {dock.DominantVesselId}"); VesselRemoveSystem.Singleton.AddToKillList(dock.WeakVesselId); dock.DominantVessel = FlightGlobals.ActiveVessel; System.MessageSender.SendDockInformation(dock, currentSubspaceId); } } else if (dock.WeakVesselId == FlightGlobals.ActiveVessel?.id) { JumpIfVesselOwnerIsInFuture(dock.DominantVesselId); if (eva) { //The kerbal should never be the dominant! var temp = dock.DominantVesselId; dock.DominantVesselId = dock.WeakVesselId; dock.WeakVesselId = temp; LunaLog.Log($"[LMP]: Crewboard detected! We own the vessel {dock.DominantVesselId}"); VesselRemoveSystem.Singleton.AddToKillList(dock.WeakVesselId); dock.DominantVessel = FlightGlobals.FindVessel(dock.DominantVesselId); System.MessageSender.SendDockInformation(dock, currentSubspaceId); } else { LunaLog.Log($"[LMP]: Docking detected! We DON'T own the dominant vessel {dock.DominantVesselId}"); VesselRemoveSystem.Singleton.AddToKillList(dock.WeakVesselId); if (dock.DominantVessel == null) { dock.DominantVessel = FlightGlobals.FindVessel(dock.DominantVesselId); } //Switch to the dominant vessel, but before that save the dominant vessel proto. //We save it as in case the dominant player didn't detected the dock he will send us a //NOT docked protovessel and that would remove the weak vessel because we are going to be an //spectator... VesselSwitcherSystem.Singleton.SwitchToVessel(dock.DominantVesselId); MainSystem.Singleton.StartCoroutine(WaitUntilWeSwitchedThenSendDockInfo(dock)); } } }