public static Part LoadPartSnapshot(Vessel vessel, ConfigNode node, Vector3 position, Quaternion rotation) { ProtoPartSnapshot snapshot = KAS_Shared.LoadProtoPartSnapshot(node); if (HighLogic.CurrentGame.flightState.ContainsFlightID(snapshot.flightID)) { snapshot.flightID = ShipConstruction.GetUniqueFlightID(HighLogic.CurrentGame.flightState); } snapshot.parentIdx = 0; snapshot.position = position; snapshot.rotation = rotation; snapshot.stageIndex = 0; snapshot.defaultInverseStage = 0; snapshot.seqOverride = -1; snapshot.inStageIndex = -1; snapshot.attachMode = (int)AttachModes.SRF_ATTACH; snapshot.attached = true; snapshot.flagURL = vessel.rootPart.flagURL; // Save properties that may be messed up by new colliders RigidbodyInertia rb_backup = new RigidbodyInertia(vessel.rootPart.rb); Part new_part = snapshot.Load(vessel, false); vessel.Parts.Add(new_part); if (vessel.packed) { GameEvents.onVesselWasModified.Fire(vessel); } else { // Request initialization as nonphysical to prevent explosions new_part.physicalSignificance = Part.PhysicalSignificance.NONE; // Disable all sub-objects with colliders List <Collider> re_enable = new List <Collider>(); foreach (var collider in new_part.GetComponentsInChildren <Collider>()) { if (collider.gameObject.activeSelf) { re_enable.Add(collider); collider.gameObject.SetActive(false); } } new_part.StartCoroutine(WaitAndUnpack(new_part, re_enable)); } rb_backup.Restore(vessel.rootPart.rb); return(new_part); }
public static Part SpawnPart(this AvailablePart availablePart, Vector3 position, Quaternion rotation, Vector3 velocity, Vector3 angularVelocity) { ProtoPartSnapshot snapshot = new ProtoPartSnapshot(availablePart.partPrefab, null); if (HighLogic.CurrentGame.flightState.ContainsFlightID(snapshot.flightID) || snapshot.flightID == 0) { snapshot.flightID = ShipConstruction.GetUniqueFlightID(HighLogic.CurrentGame.flightState); } snapshot.parentIdx = 0; snapshot.position = position; snapshot.rotation = rotation; snapshot.stageIndex = 0; snapshot.defaultInverseStage = 0; snapshot.seqOverride = -1; snapshot.inStageIndex = -1; snapshot.attachMode = (int)AttachModes.SRF_ATTACH; snapshot.attached = true; snapshot.connected = true; Part newPart = snapshot.Load(FlightGlobals.ActiveVessel, false); newPart.transform.position = position; newPart.transform.rotation = rotation; FlightGlobals.ActiveVessel.Parts.Add(newPart); newPart.physicalSignificance = Part.PhysicalSignificance.NONE; newPart.PromoteToPhysicalPart(); newPart.Unpack(); newPart.InitializeModules(); newPart.rigidbody.velocity = velocity; newPart.rigidbody.angularVelocity = angularVelocity; newPart.decouple(); newPart.vessel.vesselType = VesselType.Unknown; newPart.vessel.vesselName = availablePart.title; return newPart; }
public static Part CreatePart(ConfigNode partConfig, Vector3 position, Quaternion rotation, Part fromPart, Part coupleToPart = null, string srcAttachNodeID = null, AttachNode tgtAttachNode = null, OnPartCoupled onPartCoupled = null) { ConfigNode node_copy = new ConfigNode(); partConfig.CopyTo(node_copy); ProtoPartSnapshot snapshot = new ProtoPartSnapshot(node_copy, null, HighLogic.CurrentGame); if (HighLogic.CurrentGame.flightState.ContainsFlightID(snapshot.flightID) || snapshot.flightID == 0) { snapshot.flightID = ShipConstruction.GetUniqueFlightID(HighLogic.CurrentGame.flightState); } snapshot.parentIdx = 0; snapshot.position = position; snapshot.rotation = rotation; snapshot.stageIndex = 0; snapshot.defaultInverseStage = 0; snapshot.seqOverride = -1; snapshot.inStageIndex = -1; snapshot.attachMode = (int)AttachModes.SRF_ATTACH; snapshot.attached = true; snapshot.connected = true; snapshot.flagURL = fromPart.flagURL; Part newPart = snapshot.Load(fromPart.vessel, false); newPart.transform.position = position; newPart.transform.rotation = rotation; newPart.missionID = fromPart.missionID; fromPart.vessel.Parts.Add(newPart); newPart.physicalSignificance = Part.PhysicalSignificance.NONE; newPart.PromoteToPhysicalPart(); newPart.Unpack(); newPart.InitializeModules(); if (coupleToPart) { newPart.rigidbody.velocity = coupleToPart.rigidbody.velocity; newPart.rigidbody.angularVelocity = coupleToPart.rigidbody.angularVelocity; } else { if (fromPart.rigidbody) { newPart.rigidbody.velocity = fromPart.rigidbody.velocity; newPart.rigidbody.angularVelocity = fromPart.rigidbody.angularVelocity; } else { // If fromPart is a carried container newPart.rigidbody.velocity = fromPart.vessel.rootPart.rigidbody.velocity; newPart.rigidbody.angularVelocity = fromPart.vessel.rootPart.rigidbody.angularVelocity; } } newPart.decouple(); if (coupleToPart) { newPart.StartCoroutine(WaitAndCouple(newPart, coupleToPart, srcAttachNodeID, tgtAttachNode, onPartCoupled)); } else { newPart.vessel.vesselType = VesselType.Unknown; //name container ModuleKISInventory inv = newPart.GetComponent<ModuleKISInventory>(); if (inv) { if (inv.invName != "") { newPart.vessel.vesselName = inv.part.partInfo.title + " | " + inv.invName; } else { newPart.vessel.vesselName = inv.part.partInfo.title; } } } return newPart; }
public static Part CreatePart(ConfigNode partConfig, Vector3 position, Quaternion rotation, Part fromPart, Part coupleToPart = null, string srcAttachNodeID = null, AttachNode tgtAttachNode = null, OnPartCoupled onPartCoupled = null) { var node_copy = new ConfigNode(); partConfig.CopyTo(node_copy); var snapshot = new ProtoPartSnapshot(node_copy, null, HighLogic.CurrentGame); if (HighLogic.CurrentGame.flightState.ContainsFlightID(snapshot.flightID) || snapshot.flightID == 0) { snapshot.flightID = ShipConstruction.GetUniqueFlightID(HighLogic.CurrentGame.flightState); } snapshot.parentIdx = 0; snapshot.position = position; snapshot.rotation = rotation; snapshot.stageIndex = 0; snapshot.defaultInverseStage = 0; snapshot.seqOverride = -1; snapshot.inStageIndex = -1; snapshot.attachMode = (int)AttachModes.SRF_ATTACH; snapshot.attached = true; snapshot.connected = true; snapshot.flagURL = fromPart.flagURL; Part newPart = snapshot.Load(fromPart.vessel, false); newPart.transform.position = position; newPart.transform.rotation = rotation; newPart.missionID = fromPart.missionID; fromPart.vessel.Parts.Add(newPart); newPart.physicalSignificance = Part.PhysicalSignificance.NONE; newPart.PromoteToPhysicalPart(); newPart.Unpack(); newPart.InitializeModules(); if (coupleToPart) { newPart.Rigidbody.velocity = coupleToPart.Rigidbody.velocity; newPart.Rigidbody.angularVelocity = coupleToPart.Rigidbody.angularVelocity; } else { if (fromPart.Rigidbody) { newPart.Rigidbody.velocity = fromPart.Rigidbody.velocity; newPart.Rigidbody.angularVelocity = fromPart.Rigidbody.angularVelocity; } else { // If fromPart is a carried container newPart.Rigidbody.velocity = fromPart.vessel.rootPart.Rigidbody.velocity; newPart.Rigidbody.angularVelocity = fromPart.vessel.rootPart.Rigidbody.angularVelocity; } } // New part by default is coupled with the active vessel. newPart.decouple(); if (coupleToPart) { newPart.StartCoroutine(WaitAndCouple(newPart, coupleToPart, srcAttachNodeID, tgtAttachNode, onPartCoupled)); } else { RenameAssemblyVessel(newPart); } return(newPart); }
public static Part LoadPartSnapshot(Vessel vessel, ConfigNode node, Vector3 position, Quaternion rotation) { ConfigNode node_copy = new ConfigNode(); node.CopyTo(node_copy); node_copy.RemoveValues("kas_total_mass"); ProtoPartSnapshot snapshot = new ProtoPartSnapshot(node_copy, null, HighLogic.CurrentGame); if (HighLogic.CurrentGame.flightState.ContainsFlightID(snapshot.flightID)) snapshot.flightID = ShipConstruction.GetUniqueFlightID(HighLogic.CurrentGame.flightState); snapshot.parentIdx = 0; snapshot.position = position; snapshot.rotation = rotation; snapshot.stageIndex = 0; snapshot.defaultInverseStage = 0; snapshot.seqOverride = -1; snapshot.inStageIndex = -1; snapshot.attachMode = (int)AttachModes.SRF_ATTACH; snapshot.attached = true; snapshot.connected = true; snapshot.flagURL = vessel.rootPart.flagURL; // Save properties that may be messed up by new colliders RigidbodyInertia rb_backup = new RigidbodyInertia(vessel.rootPart.rb); Part new_part = snapshot.Load(vessel, false); vessel.Parts.Add(new_part); if (vessel.packed) { GameEvents.onVesselWasModified.Fire(vessel); } else { // Request initialization as nonphysical to prevent explosions new_part.physicalSignificance = Part.PhysicalSignificance.NONE; // Disable all sub-objects with colliders List<Collider> re_enable = new List<Collider>(); foreach (var collider in new_part.GetComponentsInChildren<Collider>()) { if (collider.gameObject.activeSelf) { re_enable.Add(collider); collider.gameObject.SetActive(false); } } new_part.StartCoroutine(WaitAndUnpack(new_part, re_enable)); } rb_backup.Restore(vessel.rootPart.rb); return new_part; }
/// <summary>Creates a new part from the config.</summary> /// <param name="partConfig">Config to read part from.</param> /// <param name="position">Initial position of the new part.</param> /// <param name="rotation">Initial rotation of the new part.</param> /// <param name="fromPart"></param> /// <param name="coupleToPart">Optional. Part to couple new part to.</param> /// <param name="srcAttachNodeId"> /// Optional. Attach node ID on the new part to use for coupling. It's required if coupling to /// part is requested. /// </param> /// <param name="tgtAttachNode"> /// Optional. Attach node on the target part to use for coupling. It's required if /// <paramref name="srcAttachNodeId"/> specifies a stack node. /// </param> /// <param name="onPartReady"> /// Callback to call when new part is fully operational and its joint is created (if any). It's /// undetermined how long it may take before the callback is called. The calling code must expect /// that there will be several frame updates and at least one fixed frame update. /// </param> /// <param name="createPhysicsless"> /// Tells if new part must be created without rigidbody and joint. It's only used to create /// equippable parts. Any other use-case is highly unlikely. /// </param> /// <returns></returns> public static Part CreatePart( ConfigNode partConfig, Vector3 position, Quaternion rotation, Part fromPart, Part coupleToPart = null, string srcAttachNodeId = null, AttachNode tgtAttachNode = null, OnPartReady onPartReady = null, bool createPhysicsless = false) { // Sanity checks for the parameters. if (coupleToPart != null) { if (srcAttachNodeId == null || srcAttachNodeId == "srfAttach" && tgtAttachNode != null || srcAttachNodeId != "srfAttach" && (tgtAttachNode == null || tgtAttachNode.id == "srfAttach")) { // Best we can do is falling back to surface attach. srcAttachNodeId = "srfAttach"; tgtAttachNode = null; } } var refVessel = coupleToPart != null ? coupleToPart.vessel : fromPart.vessel; var partNodeCopy = new ConfigNode(); partConfig.CopyTo(partNodeCopy); var snapshot = new ProtoPartSnapshot(partNodeCopy, refVessel.protoVessel, HighLogic.CurrentGame); if (HighLogic.CurrentGame.flightState.ContainsFlightID(snapshot.flightID) || snapshot.flightID == 0) { snapshot.flightID = ShipConstruction.GetUniqueFlightID(HighLogic.CurrentGame.flightState); } snapshot.parentIdx = coupleToPart != null?refVessel.parts.IndexOf(coupleToPart) : 0; snapshot.position = position; snapshot.rotation = rotation; snapshot.stageIndex = 0; snapshot.defaultInverseStage = 0; snapshot.seqOverride = -1; snapshot.inStageIndex = -1; snapshot.attachMode = srcAttachNodeId == "srfAttach" ? (int)AttachModes.SRF_ATTACH : (int)AttachModes.STACK; snapshot.attached = true; snapshot.flagURL = fromPart.flagURL; var newPart = snapshot.Load(refVessel, false); refVessel.Parts.Add(newPart); newPart.transform.position = position; newPart.transform.rotation = rotation; newPart.missionID = fromPart.missionID; newPart.UpdateOrgPosAndRot(newPart.vessel.rootPart); if (coupleToPart != null) { // Wait for part to initialize and then fire ready event. Debug.Log("[ModuleMissileRearm]: Ready to error" + newPart + srcAttachNodeId + tgtAttachNode); newPart.StartCoroutine( WaitAndCouple(newPart, srcAttachNodeId, tgtAttachNode, onPartReady, createPhysicsless: createPhysicsless)); } else { // Create new part as a separate vessel. newPart.StartCoroutine(WaitAndMakeLonePart(newPart, onPartReady)); } return(newPart); }
public static Part CreatePart(ConfigNode partConfig, Vector3 position, Quaternion rotation, Part fromPart, Part coupleToPart = null, string srcAttachNodeID = null, AttachNode tgtAttachNode = null, OnPartCoupled onPartCoupled = null) { ConfigNode node_copy = new ConfigNode(); partConfig.CopyTo(node_copy); ProtoPartSnapshot snapshot = new ProtoPartSnapshot(node_copy, null, HighLogic.CurrentGame); if (HighLogic.CurrentGame.flightState.ContainsFlightID(snapshot.flightID) || snapshot.flightID == 0) { snapshot.flightID = ShipConstruction.GetUniqueFlightID(HighLogic.CurrentGame.flightState); } snapshot.parentIdx = 0; snapshot.position = position; snapshot.rotation = rotation; snapshot.stageIndex = 0; snapshot.defaultInverseStage = 0; snapshot.seqOverride = -1; snapshot.inStageIndex = -1; snapshot.attachMode = (int)AttachModes.SRF_ATTACH; snapshot.attached = true; snapshot.connected = true; snapshot.flagURL = fromPart.flagURL; Part newPart = snapshot.Load(fromPart.vessel, false); newPart.transform.position = position; newPart.transform.rotation = rotation; newPart.missionID = fromPart.missionID; fromPart.vessel.Parts.Add(newPart); newPart.physicalSignificance = Part.PhysicalSignificance.NONE; newPart.PromoteToPhysicalPart(); newPart.Unpack(); newPart.InitializeModules(); if (coupleToPart) { newPart.rigidbody.velocity = coupleToPart.rigidbody.velocity; newPart.rigidbody.angularVelocity = coupleToPart.rigidbody.angularVelocity; } else { if (fromPart.rigidbody) { newPart.rigidbody.velocity = fromPart.rigidbody.velocity; newPart.rigidbody.angularVelocity = fromPart.rigidbody.angularVelocity; } else { // If fromPart is a carried container newPart.rigidbody.velocity = fromPart.vessel.rootPart.rigidbody.velocity; newPart.rigidbody.angularVelocity = fromPart.vessel.rootPart.rigidbody.angularVelocity; } } newPart.decouple(); if (coupleToPart) { newPart.StartCoroutine(WaitAndCouple(newPart, coupleToPart, srcAttachNodeID, tgtAttachNode, onPartCoupled)); } else { newPart.vessel.vesselType = VesselType.Unknown; //name container ModuleKISInventory inv = newPart.GetComponent <ModuleKISInventory>(); if (inv) { if (inv.invName != "") { newPart.vessel.vesselName = inv.part.partInfo.title + " | " + inv.invName; } else { newPart.vessel.vesselName = inv.part.partInfo.title; } } } return(newPart); }