Example #1
0
 public new void Awake()
 {
     base.Awake();
     GameEvents.onGamePause.Add(OnPause);
     GameEvents.onGameUnpause.Add(OnUnpause);
     activeExperiment = null;
 }
Example #2
0
        internal void FinalizeExperiment()
        {
            Log.Info("FinalizeExperiment");

            ScienceExperiment labExp = ResearchAndDevelopment.GetExperiment(activeExperiment.activeExpid);


            string displayBiome = "";

            if (vessel.landedAt != string.Empty)
            {
                activeExperiment.biomeSit = Vessel.GetLandedAtString(vessel.landedAt);
                displayBiome = Localizer.Format(vessel.displaylandedAt);
            }
            else
            {
                activeExperiment.biomeSit = ScienceUtil.GetExperimentBiome(vessel.mainBody, vessel.latitude, vessel.longitude);
                displayBiome = ScienceUtil.GetBiomedisplayName(vessel.mainBody, activeExperiment.biomeSit);
            }

            ModuleScienceExperiment exp = activeExperiment.mse;

#if DEBUG
            var step = "Get Subject";
#endif
            ScienceSubject labSub = ResearchAndDevelopment.GetExperimentSubject(labExp, activeExperiment.expSit, vessel.mainBody, activeExperiment.biomeSit, displayBiome);
            labSub.title         = $"{labExp.experimentTitle}";
            labSub.subjectValue *= labBoostScalar;
            labSub.scienceCap    = labExp.scienceCap * labSub.subjectValue;

#if DEBUG
            step = "Calculate Points";
#endif
            float sciencePoints = labExp.baseValue * labExp.dataScale;

            ScienceData labData = new ScienceData(sciencePoints, exp.xmitDataScalar, 0, labSub.id, labSub.title, false, vessel.rootPart.flightID);

#if DEBUG
            step = "Add Experiment";
#endif
            _storedData.Add(labData);

#if DEBUG
            step = "Show Dialog";
#endif
            Utils.DisplayScreenMsg(Localizer.Format("#autoLOC_238419", vessel.rootPart.partInfo.title, labData.dataAmount, labSub.title));
            ReviewDataItem(labData);

            expStatuses.Remove(activeExperiment.Key);
            activeExperiment = null;
        }
Example #3
0
        internal void SetUpActiveExperiment(string expId, string biome, ModuleScienceExperiment exp, string reqResource)
        {
            activeExperiment = new ActiveExperiment(expId, vessel.mainBody.bodyName, ScienceUtil.GetExperimentSituation(vessel), biome, exp);

            ExpStatus es = new ExpStatus(expId, activeExperiment.Key, vessel.mainBody.bodyName, ScienceUtil.GetExperimentSituation(vessel), biome,
                                         reqResource, experiments[expId].resourceAmtRequired);

            es.active = true;
            expStatuses.Add(es.Key, es);
            experimentStarted = true;

            lastUpdateTime = Planetarium.GetUniversalTime();

            StartCoroutine(FixedUpdate2());
        }
Example #4
0
        internal void DoScience(string expId)
        {
            string step = "Start";

            string reqResource = experiments[expId].neededResourceName;
            float  reqAmount   = 1;

            try
            {
                string msg = string.Empty;
                //Vessel ves = FlightGlobals.ActiveVessel;
                Part prt = FlightGlobals.ActiveVessel.rootPart;
                ModuleScienceExperiment exp = new ModuleScienceExperiment();
                if (experiments[expId].xmitDataScalar > 0)
                {
                    exp.xmitDataScalar = experiments[expId].xmitDataScalar;
                }
                Log.Info("DoScience, expId: " + expId + ", xmitDataScalar: " + exp.xmitDataScalar);

                // Checks
                step = "Check Boring";
                if (Utils.CheckBoring(vessel, true))
                {
                    return;
                }

                step = "Check CanRun";
                if (!Utils.CanRunExperiment(vessel, expId, ref msg))
                {
                    Utils.DisplayScreenMsg(Localizer.Format("#autoLOC_LTech_Experiment_002", msg));
                    return;
                }

#if false
                step = "Check Insight";
                if (Utils.ResourceAvailable(prt, "Insight") < reqInsight)
                {
                    double current = Utils.ResourceAvailable(prt, "Insight");
                    double needed  = reqInsight - current;

                    Utils.DisplayScreenMsg(Localizer.Format("#autoLOC_LTech_Experiment_003", (int)needed));
                    return;
                }
#endif

                step = "Check Resource";
                if (Utils.ResourceAvailable(prt, reqResource) < reqAmount)
                {
                    double current = Utils.ResourceAvailable(prt, reqResource);
                    double needed  = reqAmount - current;

                    Utils.DisplayScreenMsg(Localizer.Format("#autoLOC_LTech_Experiment_004", (int)needed, reqResource));
                    return;
                }

                // Experiment
                step             = "Get Experiment";
                exp.experimentID = expId;
                ScienceExperiment labExp = ResearchAndDevelopment.GetExperiment(exp.experimentID);
                if (labExp == null)
                {
                    Log.Warning(Localizer.Format("#autoLOC_LTech_Experiment_005", exp.experimentID));
                    Utils.DisplayScreenMsg(Localizer.Format("#autoLOC_LTech_Experiment_006"));
                    return;
                }

                step = "Get Situation";
                ExperimentSituations vesselSit = ScienceUtil.GetExperimentSituation(vessel);
                if (labExp.IsAvailableWhile(vesselSit, vessel.mainBody))
                {
                    step = "Get Biome";
                    string biome, displayBiome;
                    if (vessel.landedAt != string.Empty)
                    {
                        biome        = Vessel.GetLandedAtString(vessel.landedAt);
                        displayBiome = Localizer.Format(vessel.displaylandedAt);
                    }
                    else
                    {
                        biome        = ScienceUtil.GetExperimentBiome(vessel.mainBody, vessel.latitude, vessel.longitude);
                        displayBiome = ScienceUtil.GetBiomedisplayName(vessel.mainBody, biome);
                    }

                    Log.Info("DoScience, expId: " + expId +
                             "body: " + vessel.mainBody.bodyName +
                             ", ScienceUtil.GetExperimentSituation: " + ScienceUtil.GetExperimentSituation(vessel) +
                             ", biome: " + biome);

                    SetUpActiveExperiment(expId, biome, exp, reqResource);
#if false
                    activeExperiment = new ActiveExperiment(expId, vessel.mainBody.bodyName, ScienceUtil.GetExperimentSituation(vessel), biome, exp);

                    // need to add to
                    //                   internal ExperimentSituations vesselSit;
                    //                    internal string biome;
                    ExpStatus es = new ExpStatus(expId, activeExperiment.Key, vessel.mainBody.bodyName, ScienceUtil.GetExperimentSituation(vessel), biome,
                                                 reqResource, experiments[expId].resourceAmtRequired);
                    es.active = true;
                    expStatuses.Add(es.Key, es);
                    experimentStarted = true;
                    StartCoroutine(FixedUpdate2());
#endif
                }
                else
                {
                    Utils.DisplayScreenMsg(Localizer.Format("#autoLOC_238424", labExp.experimentTitle));
                    Utils.DisplayScreenMsg(Localizer.Format("#autoLOC_LTech_Experiment_007", vesselSit.displayDescription()));
                }

                step = "End";
            }
            catch (Exception ex)
            {
                Log.Error($"SkylabExperiment.DoScience at step \"{step}\";. Error: {ex}");
            }
        }
Example #5
0
        public void Do_SlowUpdate()
        {
            if (activeExperiment != null)
            {
                double curTime = Planetarium.GetUniversalTime();
                double delta   = curTime - lastUpdateTime;

                // Tasks
                // 1. Make sure experiment situation hasn't changed, if it has, then return
                // 2. calcualte resource usage
                // 3. Check to see if exeriment is completed, if so, set a flag

                string biome, displayBiome;
                if (vessel.landedAt != string.Empty)
                {
                    biome        = Vessel.GetLandedAtString(vessel.landedAt);
                    displayBiome = Localizer.Format(vessel.displaylandedAt);
                }
                else
                {
                    biome        = ScienceUtil.GetExperimentBiome(vessel.mainBody, vessel.latitude, vessel.longitude);
                    displayBiome = ScienceUtil.GetBiomedisplayName(vessel.mainBody, biome);
                }

                var curExp = new ActiveExperiment(activeExperiment.activeExpid, vessel.mainBody.bodyName, ScienceUtil.GetExperimentSituation(vessel), biome);

                if ((object)curExp != null && curExp.Key == activeExperiment.Key)
                {
                    if (!expStatuses.ContainsKey(activeExperiment.Key))
                    {
                        Log.Error("Key missing from expStatuses, key: " + activeExperiment.Key);
                        foreach (var e in expStatuses.Keys)
                        {
                            Log.Error("key: " + e);
                        }
                    }
                    double resourceRequest = delta / Planetarium.fetch.fixedDeltaTime;

                    double amtNeeded = Math.Min(
                        experiments[activeExperiment.activeExpid].resourceUsageRate * resourceRequest,
                        experiments[activeExperiment.activeExpid].resourceAmtRequired - expStatuses[activeExperiment.Key].processedResource);
                    amtNeeded = amtNeeded * KCT_Interface.ResearchTimeAdjustment();

                    //Log.Info("SkylabExperiment, amtNeeded: " + amtNeeded.ToString("F3") + ",  activeExperiment.Key: " + activeExperiment.Key +
                    //    ", processedResource: " + expStatuses[activeExperiment.Key].processedResource +
                    //    ", resourceAmtRequired: " + Addon.experiments[activeExperiment.activeExpid].resourceAmtRequired);

                    double resource = part.RequestResource(experiments[activeExperiment.activeExpid].neededResourceName, amtNeeded);
                    expStatuses[activeExperiment.Key].processedResource += resource;

                    int resourceID = GetResourceID(expStatuses[activeExperiment.Key].reqResource);
                    //                    part.GetConnectedResourceTotals(resourceID, out double amount, out double maxAmount);

                    expStatuses[activeExperiment.Key].lastTimeUpdated = Planetarium.GetUniversalTime();

                    if (HighLogic.CurrentGame.Parameters.CustomParams <LTech_1>().exitWarpWhenDone)
                    {
                        var percent = 0.001 + expStatuses[activeExperiment.Key].processedResource / Addon.experiments[activeExperiment.activeExpid].resourceAmtRequired * 100;
                        if (percent >= 100f)
                        {
                            if (TimeWarp.CurrentRateIndex > 0)
                            {
                                StartCoroutine(CancelWarp());
                                //TimeWarp.fetch.CancelAutoWarp();
                                //TimeWarp.SetRate(0, true);
                            }
                        }
                    }
                    // var experiment = experiments[activeExperiment.activeExpid];
                }
                else
                {
                    Log.Info("Situation changed");
                    Utils.DisplayScreenMsg("Vessel Situation Changed, Experiment Paused");
                } // need to decide what to do if something changed
                lastUpdateTime = curTime;
            }
            else
            if (experimentStarted)
            {
                Log.Info("FixedUpdate, activeExperiment is null");
            }
        }
Example #6
0
        public void Do_SlowUpdate()
        {
            if ((object)activeExperiment != null)
            {
                var curTime = Planetarium.GetUniversalTime();
                var delta   = curTime - lastUpdateTime;

                // Tasks
                // 1. Make sure experiment situation hasn't changed, if it has, then return
                // 2. calcualte resource usage
                // 3. Check to see if exeriment is completed, if so, set a flag

                string biome, displayBiome;
                if (vessel.landedAt != string.Empty)
                {
                    biome        = Vessel.GetLandedAtString(vessel.landedAt);
                    displayBiome = Localizer.Format(vessel.displaylandedAt);
                }
                else
                {
                    biome        = ScienceUtil.GetExperimentBiome(vessel.mainBody, vessel.latitude, vessel.longitude);
                    displayBiome = ScienceUtil.GetBiomedisplayName(vessel.mainBody, biome);
                }
                var curExp = new ActiveExperiment(activeExperiment.activeExpid, vessel.mainBody.bodyName, ScienceUtil.GetExperimentSituation(vessel), biome);

                if ((object)curExp != null && curExp.Key == activeExperiment.Key)
                {
                    if (!expStatuses.ContainsKey(activeExperiment.Key))
                    {
                        Log.Info("Key missing from expStatuses, key: " + activeExperiment.Key);
                        foreach (var e in expStatuses.Keys)
                        {
                            Log.Info("key: " + e);
                        }
                    }
                    double resourceRequest = delta / Planetarium.fetch.fixedDeltaTime;

                    double amtNeeded = Math.Min(
                        experiments[activeExperiment.activeExpid].resourceUsageRate * resourceRequest,
                        experiments[activeExperiment.activeExpid].resourceAmtRequired - expStatuses[activeExperiment.Key].processedResource);
                    amtNeeded = amtNeeded * KCT_Interface.ResearchTimeAdjustment();

                    double resource = part.RequestResource(experiments[activeExperiment.activeExpid].neededResourceName, amtNeeded);
                    expStatuses[activeExperiment.Key].processedResource += resource;


                    int resourceID = GetResourceID(expStatuses[activeExperiment.Key].reqResource);
                    part.GetConnectedResourceTotals(resourceID, out double amount, out double maxAmount);

                    expStatuses[activeExperiment.Key].lastTimeUpdated = Planetarium.GetUniversalTime();
                    // var experiment = experiments[activeExperiment.activeExpid];
                }
                else
                {
                    Log.Info("Situation changed");
                    Utils.DisplayScreenMsg("Vessel Situation Changed, Experiment Paused");
                } // need to decide what to do if something changed
                lastUpdateTime = curTime;
            }
            else
            if (experimentStarted)
            {
                Log.Info("FixedUpdate, activeExperiment is null");
            }
        }
Example #7
0
        IEnumerator SlowUpdate()
        {
            Log.Info("Addon.SlowUpdate started");
            if (vesselsWithSkylab != null)
            {
                while (true)
                {
                    // Look at each vessel with one or more Skylab parts
                    Log.Info("SlowUpdate, vesselsWithSkylab.Count(): " + vesselsWithSkylab.Count());

                    foreach (var v in vesselsWithSkylab)
                    {
                        if (v.packed) // Only packed vessels, which are on rails
                        {
                            // Look at all the parts in the vessel
                            foreach (ProtoPartSnapshot ppsSkylab in v.protoVessel.protoPartSnapshots)
                            {
                                // Find the part(s) which have the SkylabExperiment
                                ProtoPartModuleSnapshot pms = ppsSkylab.FindModule("SkylabExperiment");
                                if (pms != null)
                                {
                                    // Check for active experiments
                                    if (pms.moduleValues.HasNode(LtScience.Modules.ExpStatus.EXPERIMENT_STATUS))
                                    {
                                        // Loop through all nodes which are active experiments
                                        var activeExpNodes = pms.moduleValues.GetNodes(LtScience.Modules.ExpStatus.EXPERIMENT_STATUS);
                                        int nodeCnt        = activeExpNodes.Count();
                                        for (var i = 0; i < nodeCnt; i++)
                                        {
                                            ConfigNode        dataNode = activeExpNodes[i];
                                            Modules.ExpStatus data     = LtScience.Modules.ExpStatus.Load(dataNode);
                                            if (data.active)
                                            {
                                                var activeExperiment = new Modules.ActiveExperiment(data.expId, v.mainBody.bodyName,
                                                                                                    ScienceUtil.GetExperimentSituation(v), data.biome);
                                                //Log.Info("Addon.SlowUpdate, stored key: " + data.key + ", calculated key: " + activeExperiment.KeyUnpacked(data.expId));

                                                if (activeExperiment.KeyUnpacked(data.expId) == data.key)
                                                {
                                                    if (data.processedResource < Addon.experiments[data.expId].resourceAmtRequired)
                                                    {
                                                        double delta = Planetarium.GetUniversalTime() - data.lastTimeUpdated;
                                                        //Log.Info("Addon.SlowUpdate, Active experiment is in valid situations");

                                                        double resourceRequest = delta / Planetarium.fetch.fixedDeltaTime;

                                                        double amtNeeded = Math.Min(
                                                            experiments[activeExperiment.activeExpid].resourceUsageRate * resourceRequest,
                                                            experiments[activeExperiment.activeExpid].resourceAmtRequired - data.processedResource);

                                                        //Log.Info("Planetarium.fetch.fixedDeltaTime: " + Planetarium.fetch.fixedDeltaTime +
                                                        //    ", resourceUsageRate: " + experiments[activeExperiment.activeExpid].resourceUsageRate +
                                                        //    ", resourceRequest: " + resourceRequest + ", amtNeeded: " + amtNeeded);

                                                        double totalAmtNeeded = amtNeeded;

                                                        // look at skylab part first, then rest of vessel

                                                        foreach (var pprs in ppsSkylab.resources)
                                                        {
                                                            if (pprs.resourceName == experiments[activeExperiment.activeExpid].neededResourceName)
                                                            {
                                                                if (pprs.amount > amtNeeded)
                                                                {
                                                                    pprs.amount -= amtNeeded;
                                                                    amtNeeded    = 0;
                                                                }
                                                                else
                                                                {
                                                                    amtNeeded  -= pprs.amount;
                                                                    pprs.amount = 0;
                                                                }
                                                                break;
                                                            }
                                                        }

                                                        // If there wasn't enough of the resource in the Skylab part, then look at the rest of the vessel
                                                        if (amtNeeded > 0)
                                                        {
                                                            foreach (ProtoPartSnapshot pps in v.protoVessel.protoPartSnapshots)
                                                            {
                                                                foreach (var pprs in pps.resources)
                                                                {
                                                                    if (pprs.resourceName == experiments[activeExperiment.activeExpid].neededResourceName)
                                                                    {
                                                                        if (pprs.amount > amtNeeded)
                                                                        {
                                                                            pprs.amount -= amtNeeded;
                                                                            amtNeeded    = 0;
                                                                        }
                                                                        else
                                                                        {
                                                                            amtNeeded  -= pprs.amount;
                                                                            pprs.amount = 0;
                                                                        }

                                                                        break;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        data.processedResource += totalAmtNeeded - Math.Max(0, amtNeeded);

                                                        Log.Info("processedResource: " + data.processedResource);

                                                        data.lastTimeUpdated = Planetarium.GetUniversalTime();
                                                        data.Save(dataNode);
                                                        activeExpNodes[i] = dataNode;
                                                    }
                                                }
                                            }
                                        }
                                        //var activeExpNodes = pms.moduleValues.GetNodes(LtScience.Modules.ExpStatus.EXPERIMENT_STATUS);
                                        pms.moduleValues.RemoveNodes(LtScience.Modules.ExpStatus.EXPERIMENT_STATUS);
                                        for (int i = 0; i < nodeCnt; i++)
                                        {
                                            pms.moduleValues.AddNode(LtScience.Modules.ExpStatus.EXPERIMENT_STATUS, activeExpNodes[i]);
                                        }
                                    }
                                }
                            }
                        }
                        // Being a nice citizen by yielding after each vessel is processed
                        yield return(null);
                    }
                    yield return(new WaitForSecondsRealtime(5.0f));
                }
            }
        }