private void TurnOrbit(double theta)
        {
            var maneuverPos = CurrentNode.patch.getRelativePositionAtUT(CurrentNode.UT).xzy;
            var maneuverVel = CurrentNode.patch.getOrbitalVelocityAtUT(CurrentNode.UT).xzy;

            if (maneuverPos.NotNAN() && !maneuverPos.IsZero() && maneuverVel.NotNAN())
            {
                var nprog = maneuverVel.normalized;
                var nnorm = Vector3d.Cross(maneuverVel, maneuverPos).normalized;
                var nrad  = Vector3d.Cross(nnorm, nprog);

                if (!nprog.IsZero() && !nnorm.IsZero() && !nrad.IsZero())
                {
                    var dv      = CurrentNode.DeltaV;
                    var calcVel = maneuverVel + nrad * dv.x + nnorm * dv.y + nprog * dv.z;
                    NodeTools.TurnVector(ref calcVel, maneuverPos, theta);
                    var newDV = calcVel - maneuverVel;

                    CurrentSavedNode.UpdateDvAbs(Vector3d.Dot(newDV, nrad),
                                                 Vector3d.Dot(newDV, nnorm),
                                                 Vector3d.Dot(newDV, nprog));
                    return;
                }
            }
            // position and velocity are perfectly parallel (less probable)
            // or
            // KSP API returned NaN or some other weird shit (much more probable)
            ScreenMessages.PostScreenMessage(KSP.Localization.Localizer.Format("precisemaneuver_erroneous_orbit"), 2.0f, ScreenMessageStyle.UPPER_CENTER);
        }
 internal void ProcessHotkeySet()
 {
     if (expectingConfigHotkey && expectedTime < Planetarium.GetUniversalTime())
     {
         expectingConfigHotkey = false;
         onSetCallback(config.GetHotkey(expectedHotkey));
     }
     if (Input.anyKey && GUIUtility.keyboardControl == 0)
     {
         if (expectingConfigHotkey && Input.anyKeyDown)
         {
             expectingConfigHotkey = false;
             KeyCode key = NodeTools.FetchKey();
             if (key != KeyCode.None && key != KeyCode.Escape)
             {
                 config.SetHotkey(expectedHotkey, key);
                 ScreenMessages.PostScreenMessage("Binded to '" + key.ToString() + "'", 0.5f, ScreenMessageStyle.UPPER_CENTER);
                 onSetCallback(key);
                 // prevent the hotkey being detected as pressed the same moment it was binded
                 expectedHotkeyCooldown = 5;
             }
             else
             {
                 onSetCallback(config.GetHotkey(expectedHotkey));
             }
         }
     }
 }
        public void focus()
        {
            var enc = NodeTools.FindNextEncounter();

            if (enc != null)
            {
                MapView.MapCamera.SetTarget(enc);
            }
        }
        public string suggestPresetName()
        {
            var current = _parent.NodeManager.CurrentNode.patch.referenceBody;
            var next    = NodeTools.FindNextEncounter();

            if (current != null && next != null && current != next)
            {
                return(Localizer.Format("<<1>> - <<2>>", current.GetDisplayName(), next.GetDisplayName()));
            }
            return("");
        }
        internal void UpdateNodes()
        {
            var newtarget = NodeTools.GetTargetOrbit(CurrentNode.patch.referenceBody);

            if (newtarget != target)
            {
                target = newtarget;
                NotifyTargetChanged();
            }

            UpdateCurrentNode();
            bool origSame = CurrentSavedNode.OrigSame(CurrentNode);
            bool changed  = CurrentSavedNode.Changed;

            if (changed)
            {
                if (origSame)
                {
                    Vector3d newdv = CurrentSavedNode.dV;
                    if (CurrentNode.attachedGizmo != null)
                    {
                        CurrentNode.attachedGizmo.DeltaV = newdv;
                        CurrentNode.attachedGizmo.UT     = CurrentSavedNode.UT;
                    }
                    CurrentNode.OnGizmoUpdated(newdv, CurrentSavedNode.UT);
                    CurrentSavedNode.UpdateOrig();
                }
            }
            if (!origSame)
            {
                CurrentSavedNode.ResetSavedNode(CurrentNode);
            }
            if (changed || !origSame)
            {
                NotifyDvUTChanged();
            }
        }
Example #6
0
        internal void updateNodes()
        {
            var newtarget = NodeTools.getTargetOrbit(currentNode.patch.referenceBody);

            if (newtarget != target)
            {
                target = newtarget;
                notifyTargetChanged();
            }

            updateCurrentNode();
            bool origSame = currentSavedNode.origSame(currentNode);
            bool changed  = currentSavedNode.changed;

            if (changed)
            {
                if (origSame)
                {
                    Vector3d newdv = currentSavedNode.dv;
                    if (currentNode.attachedGizmo != null)
                    {
                        currentNode.attachedGizmo.DeltaV = newdv;
                        currentNode.attachedGizmo.UT     = currentSavedNode.ut;
                    }
                    currentNode.OnGizmoUpdated(newdv, currentSavedNode.ut);
                    currentSavedNode.updateOrig();
                }
            }
            if (!origSame)
            {
                currentSavedNode.resetSavedNode(currentNode);
            }
            if (changed || !origSame)
            {
                notifyDvUTChanged();
            }
        }
 internal bool Update(double value)
 {
     value = abs ? Math.Abs(value) : value;
     if (!double.IsNaN(value) && (double.IsNaN(current) || Math.Abs(current - value) > epsilon))
     {
         current = value;
         if (abbriv)
         {
             Value = String.Format(format, NodeTools.AbbriviateWithMetricPrefix(current));
         }
         else
         {
             Value = String.Format(format, current);
         }
         return(true);
     }
     if (double.IsNaN(value) && !double.IsNaN(current))
     {
         Value   = "N/A";
         current = value;
         return(true);
     }
     return(false);
 }
 public void CopyButtonPressed()
 {
     NodeTools.CopyToClipboard(FlightGlobals.ActiveVessel.orbit, _parent.NodeManager.CurrentNode);
 }
        internal void ChangeNodeFromString(string str)
        {
            var    reader = new System.IO.StringReader(str);
            string line;
            double nx         = CurrentNode.DeltaV.x;
            double ny         = CurrentNode.DeltaV.y;
            double nz         = CurrentNode.DeltaV.z;
            double nut        = CurrentNode.UT;
            bool   nextlineut = false;

            while ((line = reader.ReadLine()) != null)
            {
                var splitline = line.Split(' ');
                if (line.Contains("Depart at"))
                {
                    nextlineut = true;
                }
                else
                {
                    if (line.Contains("UT") && nextlineut)
                    {
                        if (!double.TryParse(splitline[splitline.Length - 1], out nut))
                        {
                            nut = CurrentNode.UT;
                        }
                    }
                    else if (line.Contains("Prograde Δv"))
                    {
                        if (!double.TryParse(splitline[splitline.Length - 2], out nz))
                        {
                            nz = CurrentNode.DeltaV.z;
                        }
                    }
                    else if (line.Contains("Normal Δv"))
                    {
                        if (!double.TryParse(splitline[splitline.Length - 2], out ny))
                        {
                            ny = CurrentNode.DeltaV.y;
                        }
                    }
                    else if (line.Contains("Radial Δv"))
                    {
                        if (!double.TryParse(splitline[splitline.Length - 2], out nx))
                        {
                            nx = CurrentNode.DeltaV.x;
                        }
                    }
                    else if (line.Contains("Ejection Angle"))
                    {
                        if (splitline.Length > 3 &&
                            double.TryParse(splitline[splitline.Length - 3].Replace("°", ""), out double target_eangle) &&
                            splitline[splitline.Length - 2] == "to" &&
                            (splitline[splitline.Length - 1] == "prograde" || splitline[splitline.Length - 1] == "retrograde") &&
                            CurrentNode.patch.IsClosed())
                        {
                            if (splitline[splitline.Length - 1] == "retrograde")
                            {
                                target_eangle += 180;
                            }
                            target_eangle *= Orbit.Deg2Rad;
                            Vector3d prograde = CurrentNode.patch.referenceBody.orbit.getOrbitalVelocityAtUT(nut);
                            Vector3d position = CurrentNode.patch.getRelativePositionAtUT(nut);
                            double   eangle   = Math.Atan2(prograde.y, prograde.x) - Math.Atan2(position.y, position.x);
                            if (eangle < 0)
                            {
                                eangle += Math.PI * 2;
                            }

                            nut += NodeTools.GetUTdiffForAngle(CurrentNode.patch, nut, eangle - target_eangle);

                            while (nut < Planetarium.GetUniversalTime())
                            {
                                nut += CurrentNode.patch.period;
                            }
                        }
                    }
                    nextlineut = false;
                }
            }
            if (nx != CurrentNode.DeltaV.x || ny != CurrentNode.DeltaV.y || nz != CurrentNode.DeltaV.z || nut != CurrentNode.UT)
            {
                CurrentSavedNode.BeginAtomicChange();
                CurrentSavedNode.UpdateDvAbs(nx, ny, nz);
                CurrentSavedNode.UpdateUtAbs(nut);
                CurrentSavedNode.EndAtomicChange();
            }
        }
        internal void ProcessRegularHotkeys()
        {
            repeatButtonPressed = false;

            if (expectedHotkeyCooldown > 0)
            {
                expectedHotkeyCooldown--;
                return;
            }

            if (Input.anyKey && GUIUtility.keyboardControl == 0 && !KeyboardLocked)
            {
                ManeuverNode node = nodeManager.CurrentNode;

                // process normal keyboard input
                double dvx     = 0;
                double dvy     = 0;
                double dvz     = 0;
                double dut     = 0;
                bool   changed = false;
                // prograde increment
                if (Input.GetKey(config.GetHotkey(PreciseManeuverConfig.HotkeyType.PROGINC)) &&
                    RepeatButtonDelay(config.GetHotkey(PreciseManeuverConfig.HotkeyType.PROGINC)))
                {
                    dvz    += config.Increment;
                    changed = true;
                }
                // prograde decrement
                if (Input.GetKey(config.GetHotkey(PreciseManeuverConfig.HotkeyType.PROGDEC)) &&
                    RepeatButtonDelay(config.GetHotkey(PreciseManeuverConfig.HotkeyType.PROGDEC)))
                {
                    dvz    -= config.Increment;
                    changed = true;
                }
                // prograde zero
                if (Input.GetKeyDown(config.GetHotkey(PreciseManeuverConfig.HotkeyType.PROGZER)))
                {
                    nodeManager.ChangeNodeDVMult(1, 1, 0);
                }
                // normal increment
                if (Input.GetKey(config.GetHotkey(PreciseManeuverConfig.HotkeyType.NORMINC)) &&
                    RepeatButtonDelay(config.GetHotkey(PreciseManeuverConfig.HotkeyType.NORMINC)))
                {
                    dvy    += config.Increment;
                    changed = true;
                }
                // normal decrement
                if (Input.GetKey(config.GetHotkey(PreciseManeuverConfig.HotkeyType.NORMDEC)) &&
                    RepeatButtonDelay(config.GetHotkey(PreciseManeuverConfig.HotkeyType.NORMDEC)))
                {
                    dvy    -= config.Increment;
                    changed = true;
                }
                // normal zero
                if (Input.GetKeyDown(config.GetHotkey(PreciseManeuverConfig.HotkeyType.NORMZER)))
                {
                    nodeManager.ChangeNodeDVMult(1, 0, 1);
                }
                // radial increment
                if (Input.GetKey(config.GetHotkey(PreciseManeuverConfig.HotkeyType.RADIINC)) &&
                    RepeatButtonDelay(config.GetHotkey(PreciseManeuverConfig.HotkeyType.RADIINC)))
                {
                    dvx    += config.Increment;
                    changed = true;
                }
                // radial decrement
                if (Input.GetKey(config.GetHotkey(PreciseManeuverConfig.HotkeyType.RADIDEC)) &&
                    RepeatButtonDelay(config.GetHotkey(PreciseManeuverConfig.HotkeyType.RADIDEC)))
                {
                    dvx    -= config.Increment;
                    changed = true;
                }
                // radial zero
                if (Input.GetKeyDown(config.GetHotkey(PreciseManeuverConfig.HotkeyType.RADIZER)))
                {
                    nodeManager.ChangeNodeDVMult(0, 1, 1);
                }
                // UT increment
                if (Input.GetKey(config.GetHotkey(PreciseManeuverConfig.HotkeyType.TIMEINC)) &&
                    RepeatButtonDelay(config.GetHotkey(PreciseManeuverConfig.HotkeyType.TIMEINC)))
                {
                    dut    += config.IncrementUt;
                    changed = true;
                }
                // UT decrement
                if (Input.GetKey(config.GetHotkey(PreciseManeuverConfig.HotkeyType.TIMEDEC)) &&
                    RepeatButtonDelay(config.GetHotkey(PreciseManeuverConfig.HotkeyType.TIMEDEC)))
                {
                    dut    -= config.IncrementUt;
                    changed = true;
                }
                if (changed)
                {
                    nodeManager.ChangeNodeDiff(dvx, dvy, dvz, dut);
                }

                // change increment
                if (Input.GetKeyDown(config.GetHotkey(PreciseManeuverConfig.HotkeyType.PAGEINC)))
                {
                    if (Event.current.alt)
                    {
                        config.SetIncrementUp();
                    }
                    else
                    {
                        config.SetIncrementDown();
                    }
                }
                // toggle x10
                if (Input.GetKeyDown(config.GetHotkey(PreciseManeuverConfig.HotkeyType.PAGEX10)))
                {
                    config.X10UTincrement = !config.X10UTincrement;
                }
                // next node
                if (Input.GetKeyDown(config.GetHotkey(PreciseManeuverConfig.HotkeyType.NEXTMAN)))
                {
                    nodeManager.SwitchNextNode();
                }
                // prev node
                if (Input.GetKeyDown(config.GetHotkey(PreciseManeuverConfig.HotkeyType.PREVMAN)))
                {
                    nodeManager.SwitchPreviousNode();
                }
                // delete node
                if (Input.GetKeyDown(config.GetHotkey(PreciseManeuverConfig.HotkeyType.MNVRDEL)))
                {
                    nodeManager.DeleteNode();
                }
                // turn orbit up
                if (Input.GetKey(config.GetHotkey(PreciseManeuverConfig.HotkeyType.TURNOUP)))
                {
                    if (RepeatButtonDelay(config.GetHotkey(PreciseManeuverConfig.HotkeyType.TURNOUP)))
                    {
                        nodeManager.TurnOrbitUp();
                    }
                }
                // turn orbit down
                if (Input.GetKey(config.GetHotkey(PreciseManeuverConfig.HotkeyType.TURNODN)))
                {
                    if (RepeatButtonDelay(config.GetHotkey(PreciseManeuverConfig.HotkeyType.TURNODN)))
                    {
                        nodeManager.TurnOrbitDown();
                    }
                }
                // circularize orbit
                if (Input.GetKeyDown(config.GetHotkey(PreciseManeuverConfig.HotkeyType.CIRCORB)))
                {
                    nodeManager.CircularizeOrbit();
                }
                // focus on target
                if (Input.GetKeyDown(config.GetHotkey(PreciseManeuverConfig.HotkeyType.FOCNENC)))
                {
                    var nextEnc = NodeTools.FindNextEncounter();
                    if (nextEnc != null)
                    {
                        MapObject mapObject = PlanetariumCamera.fetch.targets.Find(o => (o.celestialBody != null) && (o.celestialBody == nextEnc));
                        MapView.MapCamera.SetTarget(mapObject);
                    }
                }
            }
            // manage the repeating hotkeys
            if (repeatButtonPressed)
            {
                if (repeatButtonPressInterval < 50)
                {
                    repeatButtonPressInterval++;
                }
                repeatButtonReleaseInterval = 0;
            }
            else
            {
                if (repeatButtonReleaseInterval < 2)
                {
                    repeatButtonReleaseInterval++;
                }
                else
                {
                    if (repeatButtonReleaseInterval < 100)
                    {
                        nodeManager.EndAtomicChange();
                        repeatButtonReleaseInterval = 100;
                    }
                    repeatButtonPressInterval = 0;
                    repeatButtonPressedCode   = KeyCode.None;
                }
            }
        }
Example #11
0
 public string getManeuverTime(int idx)
 {
     return(NodeTools.ConvertUTtoHumanTime(FlightGlobals.ActiveVessel.patchedConicSolver.maneuverNodes[idx].UT, true));
 }