Esempio n. 1
0
        public override void OnLoad(ConfigNode node)
        {
            Log.Info("OnLoad");
            _storedData.Clear();
            expStatuses.Clear();

            if (node.HasNode(SCIENCE_DATA))
            {
                foreach (ConfigNode dataNode in node.GetNodes(SCIENCE_DATA))
                {
                    _storedData.Add(new ScienceData(dataNode));
                }
            }


            if (node.HasNode(ExpStatus.EXPERIMENT_STATUS))
            {
                foreach (ConfigNode dataNode in node.GetNodes(ExpStatus.EXPERIMENT_STATUS))
                {
                    var data = ExpStatus.Load(dataNode, this);
                    if (!expStatuses.ContainsKey(data.Key))
                    {
                        expStatuses.Add(data.Key, data);
                    }
                }
            }


            Log.Info("SkylabExperiments, found: " + experiments.Count);
        }
Esempio n. 2
0
        public static ExpStatus Load(ConfigNode node, SkylabExperiment instance = null)
        {
            var expStatus = new ExpStatus();

            node.TryGetValue("expId", ref expStatus.expId);
            node.TryGetValue("key", ref expStatus.key);
            node.TryGetValue("bodyName", ref expStatus.bodyName);
            node.TryGetEnum <ExperimentSituations>("vesselSit", ref expStatus.vesselSit, 0);
            node.TryGetValue("biome", ref expStatus.biome);
            node.TryGetValue("processedResource", ref expStatus.processedResource);
            node.TryGetValue("reqResource", ref expStatus.reqResource);
            node.TryGetValue("reqAmount", ref expStatus.reqAmount);
            node.TryGetValue("active", ref expStatus.active);
            node.TryGetValue("lastTimeUpdated", ref expStatus.lastTimeUpdated);
            if (expStatus.active)
            {
                ModuleScienceExperiment exp = new ModuleScienceExperiment();
                exp.experimentID = expStatus.expId;
                if (experiments[expStatus.expId].xmitDataScalar > 0)
                {
                    exp.xmitDataScalar = experiments[expStatus.expId].xmitDataScalar;
                }

                if (instance != null)
                {
                    instance.SetUpActiveExperiment(expStatus.expId, expStatus.biome, exp, expStatus.reqResource, expStatus.processedResource);
                }
            }
#if false
            Log.Info("ExpStatus.Load, expId: " + expStatus.expId + ", key: " + expStatus.key + ", bodyName: " + expStatus.bodyName +
                     ", vesselSit: " + expStatus.vesselSit + ", biome: " + expStatus.biome + ", processedResource: " + expStatus.processedResource +
                     ", reqAmount: " + expStatus.reqAmount + ", active: " + expStatus.active);
#endif
            return(expStatus);
        }
Esempio n. 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());
        }
Esempio n. 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}");
            }
        }
Esempio n. 5
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));
                }
            }
        }