Ejemplo n.º 1
0
        private static void RepairGrappling(KmlPartDock grapple, KmlPart part)
        {
            int grappleIndex = -1;
            int partIndex    = -1;

            if (grapple.Parent is KmlVessel)
            {
                bool      dockedVesselOk = true;
                KmlVessel vessel         = (KmlVessel)grapple.Parent;
                grappleIndex = vessel.Parts.IndexOf(grapple);
                partIndex    = vessel.Parts.IndexOf(part);
                try
                {
                    KmlNode module = grapple.GetOrCreateChildNode("MODULE", "ModuleGrappleNode");
                    module.GetOrCreateAttrib("isEnabled", "True");
                    //module.GetOrCreateAttrib("stagingEnabled").Value = "False"; // True?!?
                    module.GetOrCreateAttrib("state").Value   = "Grappled";
                    module.GetOrCreateAttrib("dockUId").Value = part.Uid;
                    KmlNode events = module.GetOrCreateChildNode("EVENTS");
                    events.GetOrCreateChildNode("Release").GetOrCreateAttrib("active").Value           = "True";
                    events.GetOrCreateChildNode("ReleaseSameVessel").GetOrCreateAttrib("active").Value = "False";
                    events.GetOrCreateChildNode("Decouple").GetOrCreateAttrib("active").Value          = "False";
                    module.GetOrCreateChildNode("ACTIONS");

                    KmlNode dockedVessel      = module.GetOrCreateChildNode("DOCKEDVESSEL");
                    KmlNode dockedVesselOther = module.GetOrCreateChildNode("DOCKEDVESSEL_Other");

                    string vesselName;
                    string vesselRootUId;
                    string defaultName;
                    if (grapple.Parent is KmlVessel)
                    {
                        vesselName    = (grapple.Parent as KmlVessel).Name;
                        vesselRootUId = (grapple.Parent as KmlVessel).RootPart.Uid;
                        defaultName   = vesselName + " Grappled - repaired by KML";
                    }
                    else
                    {
                        vesselName = "Unknown Vessel - repaired by KML";
                        if (part.ParentPart == grapple)
                        {
                            vesselRootUId = grapple.Uid;
                        }
                        else
                        {
                            vesselRootUId = part.Uid;
                        }
                        defaultName = "Unknown Grappled - repaired by KML";
                    }

                    string thisName;
                    string otherName;
                    string thisUid;
                    string otherUid;
                    if (part.ParentPart == grapple)
                    {
                        thisName  = vesselName;
                        thisUid   = vesselRootUId;
                        otherName = defaultName;
                        otherUid  = part.Uid;
                    }
                    else
                    {
                        thisName  = defaultName;
                        thisUid   = grapple.Uid;
                        otherName = vesselName;
                        otherUid  = vesselRootUId;
                    }
                    if (dockedVessel.GetAttrib("vesselName") == null)
                    {
                        KmlAttrib attrib = dockedVessel.GetOrCreateAttrib("vesselName", thisName);
                        attrib.AttribValueChanged += grapple.DockedVesselName_Changed;
                        grapple.DockedVesselName_Changed(attrib, new System.Windows.RoutedEventArgs());
                    }
                    if (dockedVessel.GetAttrib("vesselType") == null)
                    {
                        KmlAttrib attrib = dockedVessel.GetOrCreateAttrib("vesselType", "6");
                        attrib.AttribValueChanged += grapple.DockedVesselType_Changed;
                        grapple.DockedVesselType_Changed(attrib, new System.Windows.RoutedEventArgs());
                    }
                    dockedVessel.GetOrCreateAttrib("rootUId", thisUid);
                    if (dockedVesselOther.GetAttrib("vesselName") == null)
                    {
                        KmlAttrib attrib = dockedVesselOther.GetOrCreateAttrib("vesselName", otherName);
                        attrib.AttribValueChanged += grapple.DockedVesselOtherName_Changed;
                        grapple.DockedVesselOtherName_Changed(attrib, new System.Windows.RoutedEventArgs());
                    }
                    if (dockedVesselOther.GetAttrib("vesselType") == null)
                    {
                        KmlAttrib attrib = dockedVesselOther.GetOrCreateAttrib("vesselType", "6");
                        attrib.AttribValueChanged += grapple.DockedVesselOtherType_Changed;
                        grapple.DockedVesselOtherType_Changed(attrib, new System.Windows.RoutedEventArgs());
                    }
                    dockedVesselOther.GetOrCreateAttrib("rootUId", otherUid);

                    module = grapple.GetOrCreateChildNode("MODULE", "ModuleAnimateGeneric");
                    module.GetOrCreateAttrib("animSwitch").Value = "False";
                    module.GetOrCreateAttrib("animTime").Value   = "1";
                    events = module.GetOrCreateChildNode("EVENTS");
                    KmlNode toggle = events.GetOrCreateChildNode("Toggle");
                    toggle.GetOrCreateAttrib("active").Value  = "False";
                    toggle.GetOrCreateAttrib("guiName").Value = "Disarm";
                    if (part.ParentPart == grapple)
                    {
                        RepairGrappleAttachment(part, grappleIndex);
                    }
                    else if (grapple.ParentPart == part)
                    {
                        RepairGrappleAttachment(grapple, partIndex);
                    }
                    else
                    {
                        // TODO KmlPartDock:RepairGrappling(): Is there a 'Grappled (same vessel)'?
                    }
                    if (dockedVesselOk)
                    {
                        Syntax.Info(grapple, "Successfully repaired grappling");
                        BuildAttachmentStructure(vessel.Parts);
                    }
                }
                catch (NullReferenceException)
                {
                    Syntax.Warning(grapple, "Couldn't fix grappling node, there are sub-nodes missing.\n" +
                                   "You should copy a MODULE node from a functional state 'Grappled' part\n" +
                                   "grappled part should be: " + part);
                }
            }
            else
            {
                Syntax.Warning(grapple, "Could not search for connected parts, parent vessel is not valid");
            }
        }