/// <summary> /// Kills a vessel. /// </summary> private static void KillVessel(Vessel killVessel, string reason) { if (killVessel == null || killVessel.state == Vessel.State.DEAD) { return; } LunaLog.Log($"[LMP]: Killing vessel {killVessel.id}. Reason: {reason}"); SwitchVesselIfKillingActiveVessel(killVessel); try { if (FlightGlobals.fetch.VesselTarget?.GetVessel().id == killVessel.id) { FlightGlobals.fetch.SetVesselTarget(null); } FlightGlobals.RemoveVessel(killVessel); foreach (var part in killVessel.parts) { Object.Destroy(part.gameObject); } Object.Destroy(killVessel.gameObject); HighLogic.CurrentGame.flightState.protoVessels.RemoveAll(v => v == null || v.vesselID == killVessel.id); if (KSCVesselMarkers.fetch) { KSCVesselMarkers.fetch.RefreshMarkers(); } } catch (Exception killException) { LunaLog.LogError($"[LMP]: Error destroying vessel: {killException}"); } }
// static functions, that make live a lot easier /// <summary> /// Stores a Vessel into the specified Hangar /// </summary> internal static void StoreVessel(Vessel vessel, Hangar hangar) { StoredVessel storedVessel = new StoredVessel { uuid = vessel.protoVessel.vesselID, vesselName = vessel.GetDisplayName() }; //get the experience and assign the crew to the rooster foreach (Part part in vessel.parts) { int count = part.protoModuleCrew.Count; if (count != 0) { ProtoCrewMember[] crewList = part.protoModuleCrew.ToArray(); for (int i = 0; i < count; i++) { crewList[i].flightLog.AddEntryUnique(FlightLog.EntryType.Recover); crewList[i].flightLog.AddEntryUnique(FlightLog.EntryType.Land, FlightGlobals.currentMainBody.name); crewList[i].ArchiveFlightLog(); // remove the crew from the ship part.RemoveCrewmember(crewList[i]); } } } // save the ship storedVessel.vesselNode = new ConfigNode("VESSEL"); //create a backup of the current state, then save that state ProtoVessel backup = vessel.BackupVessel(); backup.Save(storedVessel.vesselNode); // save the stored information in the hangar hangar.storedVessels.Add(storedVessel); // remove the stored vessel from the game vessel.MakeInactive(); vessel.Unload(); //vessel.Die(); FlightGlobals.RemoveVessel(vessel); if (vessel != null) { vessel.protoVessel.Clean(); } //UnityEngine.Object.Destroy(vessel.gameObject); GamePersistence.SaveGame("persistent", HighLogic.SaveFolder, SaveMode.OVERWRITE); HighLogic.LoadScene(GameScenes.SPACECENTER); }
public static void Unexist() { print("[IID]: Unexisting active vessel; switching to nearest controllable vessel."); Vessel UnexistingVessel = FlightGlobals.ActiveVessel; Vessel NextVessel = FlightGlobals.FindNearestControllableVessel(UnexistingVessel); if (NextVessel != null) { FlightGlobals.SetActiveVessel(NextVessel); } else { return; } UnexistingVessel.MurderCrew(); FlightGlobals.RemoveVessel(UnexistingVessel); }
/// <summary> /// Loads the vessel proto into the current game /// </summary> private static bool LoadVesselIntoGame(ProtoVessel vesselProto, bool forceReload) { if (HighLogic.CurrentGame?.flightState == null) { return(false); } var reloadingOwnVessel = FlightGlobals.ActiveVessel && vesselProto.vesselID == FlightGlobals.ActiveVessel.id; //In case the vessel exists, silently remove them from unity and recreate it again var existingVessel = FlightGlobals.FindVessel(vesselProto.vesselID); if (existingVessel != null) { if (existingVessel.Parts.Count == vesselProto.protoPartSnapshots.Count && !forceReload) { return(true); } LunaLog.Log($"[LMP]: Reloading vessel {vesselProto.vesselID}"); if (reloadingOwnVessel) { existingVessel.RemoveAllCrew(); } FlightGlobals.RemoveVessel(existingVessel); foreach (var part in existingVessel.parts) { Object.Destroy(part.gameObject); } Object.Destroy(existingVessel.gameObject); } else { LunaLog.Log($"[LMP]: Loading vessel {vesselProto.vesselID}"); } vesselProto.Load(HighLogic.CurrentGame.flightState); if (vesselProto.vesselRef == null) { LunaLog.Log($"[LMP]: Protovessel {vesselProto.vesselID} failed to create a vessel!"); return(false); } VesselPositionSystem.Singleton.ForceUpdateVesselPosition(vesselProto.vesselRef.id); vesselProto.vesselRef.protoVessel = vesselProto; if (vesselProto.vesselRef.isEVA) { var evaModule = vesselProto.vesselRef.FindPartModuleImplementing <KerbalEVA>(); if (evaModule != null && evaModule.fsm != null && !evaModule.fsm.Started) { evaModule.fsm?.StartFSM("Idle (Grounded)"); } vesselProto.vesselRef.GoOnRails(); } if (vesselProto.vesselRef.situation > Vessel.Situations.PRELAUNCH) { vesselProto.vesselRef.orbitDriver.updateFromParameters(); } if (double.IsNaN(vesselProto.vesselRef.orbitDriver.pos.x)) { LunaLog.Log($"[LMP]: Protovessel {vesselProto.vesselID} has an invalid orbit"); return(false); } if (reloadingOwnVessel) { vesselProto.vesselRef.Load(); vesselProto.vesselRef.RebuildCrewList(); //Do not do the setting of the active vessel manually, too many systems are dependant of the events triggered by KSP FlightGlobals.ForceSetActiveVessel(vesselProto.vesselRef); vesselProto.vesselRef.SpawnCrew(); foreach (var crew in vesselProto.vesselRef.GetVesselCrew()) { if (crew.KerbalRef) { crew.KerbalRef.state = Kerbal.States.ALIVE; } } CoroutineUtil.StartDelayedRoutine("ReloadOwnVessel", () => { if (KerbalPortraitGallery.Instance.ActiveCrew.Count == 0) { FlightGlobals.ActiveVessel.SpawnCrew(); foreach (var kerbal in KerbalPortraitGallery.Instance.ActiveCrew) { kerbal.state = Kerbal.States.ALIVE; } KerbalPortraitGallery.Instance.StartRefresh(FlightGlobals.ActiveVessel); } }, 0.5f); } return(true); }
/// <summary> /// Loads the vessel proto into the current game /// </summary> private static bool LoadVesselIntoGame(ProtoVessel vesselProto) { if (HighLogic.CurrentGame?.flightState == null) { return(false); } var reloadingOwnVessel = FlightGlobals.ActiveVessel && vesselProto.vesselID == FlightGlobals.ActiveVessel.id && HighLogic.LoadedSceneIsFlight; //In case the vessel exists, silently remove them from unity and recreate it again var existingVessel = FlightGlobals.fetch.LmpFindVessel(vesselProto.vesselID); if (existingVessel != null) { if (reloadingOwnVessel) { foreach (var part in existingVessel.Parts) { if (part.protoModuleCrew.Any()) { //Serialize to avoid modifying the collection var crewMembers = part.protoModuleCrew.ToArray(); foreach (var crew in crewMembers) { part.RemoveCrew(crew); } } } } FlightGlobals.RemoveVessel(existingVessel); foreach (var part in existingVessel.parts) { Object.Destroy(part.gameObject); } Object.Destroy(existingVessel.gameObject); } vesselProto.Load(HighLogic.CurrentGame.flightState); if (vesselProto.vesselRef == null) { LunaLog.Log($"[LMP]: Protovessel {vesselProto.vesselID} failed to create a vessel!"); return(false); } vesselProto.vesselRef.protoVessel = vesselProto; if (vesselProto.vesselRef.isEVA) { var evaModule = vesselProto.vesselRef.FindPartModuleImplementing <KerbalEVA>(); if (evaModule != null && evaModule.fsm != null && !evaModule.fsm.Started) { evaModule.fsm?.StartFSM("Idle (Grounded)"); } vesselProto.vesselRef.GoOnRails(); } if (vesselProto.vesselRef.situation > Vessel.Situations.PRELAUNCH) { vesselProto.vesselRef.orbitDriver.updateFromParameters(); } if (double.IsNaN(vesselProto.vesselRef.orbitDriver.pos.x)) { LunaLog.Log($"[LMP]: Protovessel {vesselProto.vesselID} has an invalid orbit"); return(false); } PlayerColorSystem.Singleton.SetVesselOrbitColor(vesselProto.vesselRef); KscSceneSystem.Singleton.RefreshTrackingStationVessels(); if (KSCVesselMarkers.fetch) { KSCVesselMarkers.fetch.RefreshMarkers(); } if (reloadingOwnVessel) { vesselProto.vesselRef.Load(); vesselProto.vesselRef.RebuildCrewList(); //Do not do the setting of the active vessel manually, too many systems are dependant of the events triggered by KSP FlightGlobals.ForceSetActiveVessel(vesselProto.vesselRef); if (vesselProto.vesselRef.GetCrewCount() > 0) { foreach (var part in vesselProto.vesselRef.Parts) { if (part.protoModuleCrew.Any()) { //Serialize to avoid modifying the collection var crewMembers = part.protoModuleCrew.ToArray(); foreach (var crew in crewMembers) { part.RemoveCrew(crew); part.AddCrew(crew); } } } vesselProto.vesselRef.SpawnCrew(); if (KerbalPortraitGallery.Instance) { KerbalPortraitGallery.Instance.SetActivePortraitsForVessel(FlightGlobals.ActiveVessel); } } } return(true); }