private void ProcessFixedUpdate()
        {
            if (unusedTargetPartRemovalCounter > 0)
            {
                unusedTargetPartRemovalCounter--;
            }
            else
            {
                unusedTargetPartRemovalCounter = UNUSED_TARGET_PART_REMOVAL_COUNTER_INTERVAL;
                try
                {
                    Utilities.RemoveAllUntargetedFreeAttachTargetsInLoadRange();
                }
                catch (Exception e)
                {
                    Debug.Log("[IRAS] unused target part cleanup threw exception: " + e.Message);
                }
            }
            if (!HighLogic.LoadedSceneIsFlight)
            {
                return;
            }
            var activeVessel = FlightGlobals.ActiveVessel;

            if (activeVessel == null || !activeVessel.isEVA)
            {
                foreach (var module in FlightGlobals.Vessels
                         .Where(v => v.loaded && v.isEVA)
                         .Select(v => v.rootPart.FindModuleImplementing <ModuleKerbalHook>())
                         .Where(module => module != null))
                {
                    module.OnFixedUpdate();
                }
                return;
            }
            var rP = FlightGlobals.ActiveVessel.rootPart;
            ModuleKerbalHook rPMod = null;

            if (rP != null && !rP.TryGetModule(out rPMod))
            {
                Debug.Log("[IRAS] trying to add module to EVA root part");
                try
                {
                    rP.AddModule("ModuleKerbalHook");
                    Debug.Log("[IRAS] module added to EVA root part");
                    StartCoroutine(CatchModuleAddedToEVA(rP));
                }
                catch (NullReferenceException e)
                {
                    Debug.Log("[IRAS] exception thrown: " + e.Message);
                }
            }
            else if (rP != null)
            {
                if (rPMod != null)
                {
                    rPMod.OnFixedUpdate();
                }
            }
        }
        private static void UpdateStraightOutHint(ModuleActiveStrut module, GameObject hint)
        {
            hint.SetActive(false);
            var rayres = Utilities.PerformRaycastIntoDir(module.Origin.position, module.RealModelForward,
                                                         module.RealModelForward, module.part);
            var trans = hint.transform;

            trans.position = module.Origin.position;
            var dist = rayres.HitResult ? rayres.DistanceFromOrigin / 2f : Config.Instance.MaxDistance;

            if (rayres.HitResult)
            {
                trans.LookAt(rayres.Hit.point);
            }
            else
            {
                trans.LookAt(module.Origin.transform.position +
                             (module.IsFlexible ? module.Origin.up : module.RealModelForward));
            }
            trans.Rotate(new Vector3(0, 1, 0), 90f);
            trans.Rotate(new Vector3(0, 0, 1), 90f);
            trans.localScale = new Vector3(0.05f, dist, 0.05f);
            trans.Translate(new Vector3(0f, dist, 0f));
            hint.SetActive(true);
        }
        private void HandleEditorPartDetach(GameEvents.HostTargetAction <Part, Part> hostTargetAction)
        {
            var partList = new List <Part> {
                hostTargetAction.target
            };

            foreach (var child in hostTargetAction.target.children)
            {
                child.RecursePartList(partList);
            }
            var movedModules = (from p in partList
                                where p.Modules.Contains(Config.Instance.ModuleName)
                                select p.Modules[Config.Instance.ModuleName] as ModuleActiveStrut).ToList();
            var movedTargets = (from p in partList
                                where p.Modules.Contains(Config.Instance.ModuleActiveStrutFreeAttachTarget)
                                select p.Modules[Config.Instance.ModuleActiveStrutFreeAttachTarget] as ModuleActiveStrutFreeAttachTarget)
                               .ToList();
            var vesselModules = (from p in Utilities.ListEditorParts(false)
                                 where p.Modules.Contains(Config.Instance.ModuleName)
                                 select p.Modules[Config.Instance.ModuleName] as ModuleActiveStrut).ToList();

            foreach (var module in movedModules)
            {
                module.Unlink();
            }
            foreach (var module in vesselModules.Where(module =>
                                                       (module.Target != null && movedModules.Any(m => m.ID == module.Target.ID) ||
                                                        (module.Targeter != null && movedModules.Any(m => m.ID == module.Targeter.ID))) ||
                                                       (module.IsFreeAttached && !module.StraightOutAttachAppliedInEditor &&
                                                        movedTargets.Any(t => t.ID == module.FreeAttachTarget.ID))))
            {
                module.Unlink();
            }
        }
        private static void HighlightCurrentTargets()
        {
            var targets =
                Utilities.GetAllActiveStruts().Where(m => m.Mode == Util.Mode.Target).Select(m => m.part).ToList();

            foreach (var part in targets)
            {
                part.SetHighlightColor(Color.green);
                part.SetHighlight(true, false);
            }
        }
        private void ProcessUpdate()
        {
            try
            {
                if (HighLogic.LoadedSceneIsFlight)
                {
                    if (NewSpawnedPart != null)
                    {
                        var justDestroyPart = false;
                        if (CurrentTargeter != null || CurrentKerbalTargeter != null)
                        {
                            NewSpawnedPart.transform.position = CurrentKerbalTargeter != null
                                ? CurrentKerbalTargeter.transform.position
                                : CurrentTargeter.transform.position;
                        }
                        else
                        {
                            var module = NewSpawnedPart.Modules[Config.Instance.ModuleActiveStrutFreeAttachTarget];
                            if (module != null)
                            {
                                var target = module as ModuleActiveStrutFreeAttachTarget;
                                if (target != null)
                                {
                                    target.Die();
                                }
                                else
                                {
                                    justDestroyPart = true;
                                }
                            }
                            else
                            {
                                justDestroyPart = true;
                            }
                        }
                        if (justDestroyPart)
                        {
                            Destroy(NewSpawnedPart);
                            NewSpawnedPart = null;
                        }
                    }
                    _processIdResets();
                }
                if (Config.Instance.ShowStraightOutHint && straightOutHintActiveParts != null)
                {
                    var remList   = new List <StraightOutHintActivePart>();
                    var renewList = new List <StraightOutHintActivePart>();
                    foreach (var straightOutHintActivePart in straightOutHintActiveParts)
                    {
                        if (straightOutHintActivePart.HasToBeRemoved)
                        {
                            remList.Add(straightOutHintActivePart);
                        }
                        else
                        {
                            renewList.Add(straightOutHintActivePart);
                        }
                    }
                    foreach (var straightOutHintActivePart in remList)
                    {
                        straightOutHintActiveParts.Remove(straightOutHintActivePart);
                        Destroy(straightOutHintActivePart.HintObject);
                    }
                    foreach (var straightOutHintActivePart in renewList)
                    {
                        UpdateStraightOutHint(straightOutHintActivePart.Module, straightOutHintActivePart.HintObject);
                    }
                }
                var resetList = new List <HighlightedPart>();
                if (targetHighlightedParts != null)
                {
                    resetList =
                        targetHighlightedParts.Where(
                            targetHighlightedPart =>
                            targetHighlightedPart != null && targetHighlightedPart.HasToBeRemoved).ToList();
                }
                foreach (var targetHighlightedPart in resetList)
                {
                    targetHighlightedPart.Part.SetHighlightDefault();
                    try
                    {
                        if (targetHighlightedParts != null)
                        {
                            targetHighlightedParts.Remove(targetHighlightedPart);
                        }
                    }
                    catch (NullReferenceException)
                    {
                        //multithreading issue occured here, don't know if fixed
                    }
                }
                if (targetHighlightedParts != null)
                {
                    foreach (var targetHighlightedPart in targetHighlightedParts)
                    {
                        var part = targetHighlightedPart.Part;
                        part.SetHighlightColor(Color.cyan);
                        part.SetHighlight(true, false);
                    }
                }
                if (Mode == AddonMode.None || (CurrentTargeter == null && CurrentKerbalTargeter == null))
                {
                    if (resetAllHighlighting)
                    {
                        resetAllHighlighting = false;
                        var asList = Utilities.GetAllActiveStruts();
                        foreach (var moduleActiveStrut in asList)
                        {
                            moduleActiveStrut.part.SetHighlightDefault();
                        }
                    }
                    if (connector != null)
                    {
                        connector.SetActive(false);
                    }
                    return;
                }
                resetAllHighlighting = true;
                if (Mode == AddonMode.Link)
                {
                    HighlightCurrentTargets();
                }
                var mp = Utilities.GetMouseWorldPosition();
                var transformForRaycast = Mode == AddonMode.AttachKerbalHook
                    ? CurrentKerbalTargeter.transform
                    : CurrentTargeter.ModelFeatures[ModuleActiveStrut.ModelFeaturesType.HeadExtension]
                        ? CurrentTargeter.StrutOrigin
                        : CurrentTargeter.Origin;
                var rightDir = Mode == AddonMode.AttachKerbalHook ? Vector3.zero : CurrentTargeter.RealModelForward;
                var raycast  = Mode == AddonMode.AttachKerbalHook
                    ? Utilities.PerformRaycastFromKerbal(transformForRaycast.position, mp.Item1, transformForRaycast.up,
                                                         CurrentKerbalTargeter.part.vessel)
                    : Utilities.PerformRaycast(transformForRaycast.position, mp.Item1,
                                               CurrentTargeter.IsFlexible ? transformForRaycast.up : rightDir,
                                               CurrentTargeter.IsFlexible ? CurrentTargeter.part : null);
                PointToMousePosition(mp.Item1, raycast);
                if (!raycast.HitResult)
                {
                    var handled = false;
                    if (Mode == AddonMode.Link && Input.GetKeyDown(KeyCode.Mouse0))
                    {
                        CurrentTargeter.AbortLink();
                        CurrentTargeter.UpdateGui();
                        handled = true;
                    }
                    if ((Mode == AddonMode.FreeAttach || Mode == AddonMode.AttachKerbalHook) &&
                        (Input.GetKeyDown(KeyCode.X) || Input.GetKeyDown(KeyCode.Escape)))
                    {
                        if (HighLogic.LoadedSceneIsFlight && Input.GetKeyDown(KeyCode.Escape))
                        {
                            Input.ResetInputAxes();
                        }
                        Mode = AddonMode.None;
                        if (Mode == AddonMode.AttachKerbalHook)
                        {
                            CurrentKerbalTargeter.Abort();
                        }
                        else
                        {
                            CurrentTargeter.UpdateGui();
                            CurrentTargeter.AbortLink();
                            CurrentTargeter = null;
                        }
                        CheckForInEditorAbort();
                        handled = true;
                    }
                    connector.SetActive(false);
                    if (HighLogic.LoadedSceneIsEditor && handled)
                    {
                        Input.ResetInputAxes();
                        InputLockManager.RemoveControlLock(Config.Instance.EditorInputLockId);
                    }
                    return;
                }
                var validPos = _determineColor(mp.Item1, raycast);
                if (validPos && Mode == AddonMode.FreeAttach && HighLogic.LoadedSceneIsEditor)
                {
                    InputLockManager.RemoveControlLock(Config.Instance.EditorInputLockId);
                    noInputAxesReset = true;
                }
                else if (HighLogic.LoadedSceneIsEditor)
                {
                    InputLockManager.SetControlLock(Config.Instance.EditorInputLockId);
                    noInputAxesReset = false;
                }
                ProcessUserInput(mp.Item1, raycast, validPos);
            }
            catch (NullReferenceException e)
            {
                Debug.Log("[IRAS] addon update exception catched: " + e);

                /*
                 * For no apparent reason an exception is thrown on first load.
                 * I found no way to circumvent this.
                 * Since the exception has to be handled only once we are
                 * "just" entering the try block constantly which I consider
                 * still to be preferred over an unhandled exception.
                 */
            }
        }
Example #6
0
 private void _createStrut()
 {
     //Color = 255, 209, 25
     strut = Utilities.CreateFlexStrut("KerbalHookStrut", true, new Color(1f, 0.81961f, 0.09804f));
 }
Example #7
0
 private void _createLocalAnchor()
 {
     localAnchor = Utilities.CreateLocalAnchor("KerbalHookLocalAnchor", true);
 }