/// <summary> /// Get the index of a partmodule in the part /// </summary> /// <param name="part">The part to check</param> /// <param name="module">The partmodule to check</param> /// <returns>The index of the partmodule</returns> public static int getModuleIndex(this Part part, PartModule module) { int numModules = part.Modules.Count; for (int i = 0; i < numModules; i++) { if ((module.GetInstanceID() == part.Modules[i].GetInstanceID()) && (module.moduleName == part.Modules[i].moduleName)) { return(i); } } return(-1); }
public static List <PartModule> findStuckHarpoons(Vessel v) { Log.detail("Looking for harpoons stuck in the ground in {0}", v.name); List <PartModule> harpoonList = new List <PartModule> (); foreach (Part p in v.parts) { if (!p.Modules.Contains("KASModuleHarpoon")) { continue; } PartModule pm = p.Modules ["KASModuleHarpoon"]; if (!harpoonHasStaticJoint(pm)) { continue; } Log.detail("Adding haroon {0}", pm.GetInstanceID()); harpoonList.Add(pm); } Log.detail("Found {0} stuck harpoons", harpoonList.Count); return(harpoonList); }
public static void tryDetachHarpoon(Vessel v) { // New plan // - find winch connected to this attached harpoon // - set the winch to undocked state and release it // - upon ground contact set back to docked state if (!hasKASAddOn || isNewKsp) { return; } List <PartModule> stuckHarpoons = findStuckHarpoons(v); if (stuckHarpoons.Count > 0) { harpoons [v.id] = stuckHarpoons; winches [v.id] = new List <PartModule> (); foreach (PartModule harpoon in harpoons[v.id]) { Log.detail("Detaching harpoon {0}", harpoon.GetInstanceID()); // Black Magic starts here Type attachTypeEnum = Type.GetType("KAS.KASModuleAttachCore+AttachType,KAS"); MethodInfo detachMethodInfo = harpoon.GetType().GetMethod("Detach", new Type[] { attachTypeEnum }); object[] param = new object[1]; param [0] = 4; // Magic number - detach from the ground detachMethodInfo.Invoke(harpoon, param); PartModule winch = findConnectedWinch(harpoon); if (winch == null || !isPlugDocked(winch)) { Log.detail("Can't find winch for harpoon {0}", harpoon.GetInstanceID()); continue; } SpringJoint springJoint = winch.part.gameObject.GetComponentInChildren <SpringJoint> (); if (springJoint != null) { JointBackup backupJoint = new JointBackup(); backupJoint.spring = springJoint.spring; backupJoint.tolerance = springJoint.tolerance; backupJoint.maxDistance = springJoint.maxDistance + 0.03f; springs [winch] = backupJoint; springJoint.spring = 0.0f; springJoint.tolerance = 3f; springJoint.maxDistance += 3f; } else { Log.detail("Can't find springJoint in winch {0}", winch.GetInstanceID()); } Log.detail("Adding winch {0} for harpoon {1}", winch.GetInstanceID(), harpoon.GetInstanceID()); toggleDockedState(winch); releaseWinchReel(winch, true); winches [v.id].Add(winch); Rigidbody rb = (Rigidbody)harpoon.part.GetComponentInChildren <Rigidbody> (); if (rb != null) { harpoonsToHold.Add(rb); } } } }
public static void Log(this PartModule pm, string msg, params object[] args) { var vn = pm.vessel == null? "_vessel" : (string.IsNullOrEmpty(pm.vessel.vesselName)? pm.vessel.id.ToString() : pm.vessel.vesselName); Utils.Log(string.Format("{0}:{1}:{2} [{3}]: {4}", vn, pm.part == null? "_part" : pm.part.Title(), pm.moduleName, pm.GetInstanceID(), msg), args); }