protected override void OnStart()
        {
            base.OnStart();
            try
            {
                vesselAsst = new PilotAssistant(this);
                vesselData = new VesselData(this);
                PilotAssistantFlightCore.Instance.AddVessel(this);

                vesselAsst.Start();

                Vessel.OnPreAutopilotUpdate  += new FlightInputCallback(PreAutoPilotUpdate);
                Vessel.OnPostAutopilotUpdate += new FlightInputCallback(PostAutoPilotUpdate);

                GameEvents.onVesselChange.Add(VesselSwitch);
                GameEvents.onTimeWarpRateChanged.Add(WarpHandler);

                if (FlightGlobals.ActiveVessel == Vessel)
                {
                    VesselSwitch(Vessel);
                }
            }
            catch (Exception ex)
            {
                Logger.Log("Startup error", Logger.LogLevel.Error);
                Logger.Log(ex.Message, Logger.LogLevel.Error);
                Logger.Log(ex.InnerException, Logger.LogLevel.Error);
                Logger.Log(ex.StackTrace, Logger.LogLevel.Error);
            }
        }
        public void OnDestroy()
        {
            GameEvents.onVesselChange.Remove(vesselSwitch);
            GameEvents.onTimeWarpRateChanged.Remove(warpHandler);
            GameEvents.onPartCouple.Remove(docking);

            if (vesselRef != null)
            {
                vesselRef.OnPreAutopilotUpdate  -= preAutoPilotUpdate;
                vesselRef.OnPostAutopilotUpdate -= postAutoPilotUpdate;
            }
            if (!ReferenceEquals(vesselAsst, null))
            {
                vesselAsst.OnDestroy();
            }
            if (!ReferenceEquals(vesselData, null) && !ReferenceEquals(PilotAssistantFlightCore.Instance, null))
            {
                PilotAssistantFlightCore.Instance.removeVessel(this);
            }

            vesselRef      = null;
            vesselSSAS     = null;
            vesselStockSAS = null;
            vesselAsst     = null;
            vesselData     = null;
        }
        public void OnDestroy()
        {
            if (Vessel != null)
            {
                GameEvents.onVesselChange.Remove(VesselSwitch);
                GameEvents.onTimeWarpRateChanged.Remove(WarpHandler);

                Vessel.OnPreAutopilotUpdate  -= PreAutoPilotUpdate;
                Vessel.OnPostAutopilotUpdate -= PostAutoPilotUpdate;
                if (!ReferenceEquals(vesselAsst, null))
                {
                    vesselAsst.OnDestroy();
                    if (!ReferenceEquals(PilotAssistantFlightCore.Instance, null))
                    {
                        PilotAssistantFlightCore.Instance.RemoveVessel(this);
                    }
                }
                vesselAsst = null;
                vesselData = null;
            }
        }
        public void Start()
        {
            try
            {
                vesselRef = GetComponent <Vessel>();
                if (vesselRef == null || vesselRef.isEVA || !vesselRef.isCommandable)
                {
                    vesselRef = null;
                    Destroy(this);
                    return;
                }
                else
                {
                    vesselAsst     = new PilotAssistant(this);
                    vesselSSAS     = new SurfSAS(this);
                    vesselStockSAS = new Stock_SAS(this);
                    vesselData     = new VesselData(this);
                }


                PilotAssistantFlightCore.Instance.addVessel(this);

                vesselAsst.Start();
                vesselSSAS.Start();
                vesselStockSAS.Start();
            }
            catch (Exception ex)
            {
                Debug.LogError("Pilot Assistant: Startup error");
                Debug.LogError(ex.InnerException);
            }

            vesselRef.OnPreAutopilotUpdate  += new FlightInputCallback(preAutoPilotUpdate);
            vesselRef.OnPostAutopilotUpdate += new FlightInputCallback(postAutoPilotUpdate);

            GameEvents.onPartCouple.Add(docking);
            GameEvents.onVesselChange.Add(vesselSwitch);
            GameEvents.onTimeWarpRateChanged.Add(warpHandler);
        }
 /// <summary>
 /// called on vessel load to load the correct preset for the vessel being flown
 /// </summary>
 /// <param name="instance">The instance to load for</param>
 public void loadCraftAsstPreset(PilotAssistant instance)
 {
     Utils.Log("loading preset for craft " + instance.Vessel.name);
     string presetName;
     Utils.Log(craftPresetDict.TryGetValue(instance.Vessel.vesselName, out presetName) + " " + presetName);
     if (craftPresetDict.TryGetValue(instance.Vessel.vesselName, out presetName))
         loadAsstPreset(presetName, instance);
     else
         loadAsstPreset(Instance.craftPresetDict[craftDefaultName], instance);
 }
        /// <summary>
        /// loads a preset into the controllers of a PA instance
        /// </summary>
        /// <param name="p">the preset to load</param>
        /// <param name="asstInstance">the PA instance to load to</param>
        public static void loadAsstPreset(string presetName, PilotAssistant asstInstance)
        {
            if (string.IsNullOrEmpty(presetName))
                return;
            AsstPreset p = Instance.AsstPresetList.FirstOrDefault(pr => pr.name == presetName);
            if (ReferenceEquals(p , null))
                return;
            Asst_PID_Controller[] c = asstInstance.controllers;
            for (int i = 0; i < 8; i++)
            {
                c[i].k_proportional = p.PIDGains[i][0];
                c[i].k_integral = p.PIDGains[i][1];
                c[i].k_derivative = p.PIDGains[i][2];
                c[i].outMin = p.PIDGains[i][3];
                c[i].outMax = p.PIDGains[i][4];
                c[i].integralClampLower = p.PIDGains[i][5];
                c[i].integralClampUpper = p.PIDGains[i][6];
                c[i].Scalar = p.PIDGains[i][7];
                c[i].Easing = p.PIDGains[i][8];
            }

            asstInstance.activePreset = p;
            GeneralUI.postMessage("Loaded preset " + p.name);

            if (asstInstance.activePreset.name != Instance.craftPresetDict[craftDefaultName])
                Instance.updateCraftPreset(asstInstance.activePreset, asstInstance.vesModule.Vessel);
            saveToFile();
        }
Exemple #7
0
 public static AsstController GetAsst(this AsstList id, PilotAssistant instance)
 {
     return(instance.controllers[(int)id]);
 }
 public static void updateAsstPreset(PilotAssistant instance)
 {
     Instance.activeAsstPreset.Update(instance.controllers);
     saveToFile();
 }
 // called on vessel load
 public static void loadCraftAsstPreset(PilotAssistant instance)
 {
     if (Instance.craftPresetDict.ContainsKey(FlightGlobals.ActiveVessel.vesselName) && !ReferenceEquals(Instance.craftPresetDict[FlightGlobals.ActiveVessel.vesselName].AsstPreset, null))
         loadAsstPreset(Instance.craftPresetDict[FlightGlobals.ActiveVessel.vesselName].AsstPreset, instance);
     else
         loadAsstPreset(Instance.craftPresetDict[craftDefaultName].AsstPreset, instance);
 }
        public static void loadAsstPreset(AsstPreset p, PilotAssistant instance)
        {
            AsstController[] c = instance.controllers;
            for (int i = 0; i < 8; i++)
            {
                c[i].PGain = p.PIDGains[i][0];
                c[i].IGain = p.PIDGains[i][1];
                c[i].DGain = p.PIDGains[i][2];
                c[i].OutMin = p.PIDGains[i][3];
                c[i].OutMax = p.PIDGains[i][4];
                c[i].ClampLower = p.PIDGains[i][5];
                c[i].ClampUpper = p.PIDGains[i][6];
                c[i].Scalar = p.PIDGains[i][7];
                c[i].Easing = p.PIDGains[i][8];
            }

            Instance.activeAsstPreset = p;
            GeneralUI.postMessage("Loaded preset " + p.name);

            if (Instance.activeAsstPreset != Instance.craftPresetDict[craftDefaultName].AsstPreset)
                updateCraftPreset(Instance.activeAsstPreset, instance.vesModule.vesselRef);
            saveToFile();
        }