Beispiel #1
0
 void Start()
 {
     Log.Info("Start");
     if (HighLogic.LoadedSceneIsEditor || HighLogic.LoadedSceneIsFlight)
     {
         techPartResearched = ModSegSRBs.PartAvailable(ModSegSRBs.PPTechName);
         Log.Info("Start, techPartResearched(" + ModSegSRBs.PPTechName + "): " + techPartResearched);
         if (!techPartResearched)
         {
             // remove the fuel since it isn't being used
             if (this.part != null)
             {
                 PartResourceList prl = this.part.Resources;
                 if (prl.Contains(ModSegSRBs.SeparatronFuel))
                 {
                     part.RemoveResource(ModSegSRBs.SeparatronFuel);
                 }
             }
             if (HighLogic.LoadedScene != GameScenes.LOADING && HighLogic.LoadedScene != GameScenes.EDITOR)
             {
                 Log.Info(ModSegSRBs.PPTechName + ", not researched yet");
                 part.RemoveModule(this);
             }
         }
         else
         {
             Log.Info(ModSegSRBs.PPTechName + " researched");
         }
     }
 }
Beispiel #2
0
        public override void OnStart(StartState state)
        {
            base.OnStart(state);
            var cullmix = (1 << 0) | (1 << 4) | (1 << 9) | (1 << 10) | (1 << 15) | (1 << 19) | (1 << 22) | (1 << 23) | (1 << 24) | (1 << 29) | (1 << 30);

            camer    = part.FindModelTransform("CameraICC");
            camerota = part.FindModelTransform("pivotcameraicc");
            _camera  = camer.GetComponent <Camera>();
            _camera.gameObject.AddComponent <FlareLayer>();
            _camera.gameObject.AddComponent <UnderwaterFog>();
            _camera.cullingMask = cullmix;
            camerarm1           = part.FindModelTransform("CameraArm1");
            _cameraarm1         = camerarm1.GetComponent <Camera>();
            _cameraarm1.gameObject.AddComponent <FlareLayer>();
            _cameraarm1.gameObject.AddComponent <UnderwaterFog>();
            _cameraarm1.cullingMask = cullmix;
            camerarm2   = part.FindModelTransform("CameraArm2");
            _cameraarm2 = camerarm2.GetComponent <Camera>();
            _cameraarm2.gameObject.AddComponent <FlareLayer>();
            _cameraarm2.gameObject.AddComponent <UnderwaterFog>();
            _cameraarm2.cullingMask = cullmix;
            resour = part.Resources;
            if (HighLogic.LoadedSceneIsFlight)
            {
                if (bapp2 == null)
                {
                    bapp2 = FindObjectOfType <ButtonApp>();
                    bapp2.Activado();
                }
            }
        }
Beispiel #3
0
        public static float GetResourceCost(Part part, bool maxAmount = false)
        {
            PartResourceDefinitionList definitions = PartResourceLibrary.Instance.resourceDefinitions;
            PartResourceList           resources   = part.Resources;
            PartResourceDefinition     resourceDef;
            float totalCost = 0f;

            foreach (PartResource resource in resources)
            {
                //Find definition
                resourceDef = definitions[resource.resourceName];
                if (resourceDef != null)
                {
                    if (!maxAmount)
                    {
                        totalCost += (float)(resourceDef.unitCost * resource.amount);
                    }
                    else
                    {
                        totalCost += (float)(resourceDef.unitCost * resource.maxAmount);
                    }
                }
            }

            return(totalCost);
        }
Beispiel #4
0
        public static PartResource getResource(Part part, string name)
        {
            PartResourceList resourceList = part.Resources;

            return(resourceList.list.Find(delegate(PartResource cur)
            {
                return (cur.resourceName == name);
            }));
        }
Beispiel #5
0
        private double CalculateFuelCellPower()
        {
            double fuelCellPower = 0;

            bool hasHydrogen = false;
            bool hasOxygen   = false;

            for (int i = 0; i < this.vessel.parts.Count; ++i)
            {
                var part_i = this.vessel.parts[i];
                PartResourceList resources = part_i.Resources;

                for (int j = 0; j < resources.Count; ++j)
                {
                    var resource = resources[j];
                    if (resource.resourceName == "Hydrogen" && resource.maxAmount > 0)
                    {
                        hasHydrogen = true;
                    }
                    if (resource.resourceName == "Oxygen" && resource.maxAmount > 0)
                    {
                        hasOxygen = true;
                    }
                }
            }

            if (hasHydrogen && hasOxygen)
            {
                for (int i = 0; i < this.vessel.parts.Count; ++i)
                {
                    var              part_i    = this.vessel.parts[i];
                    PartModuleList   modules   = part_i.Modules;
                    PartResourceList resources = part_i.Resources;

                    for (int j = 0; j < modules.Count; ++j)
                    {
                        var module = modules[j];
                        if (module.moduleName == "ProcessController")
                        {
                            for (int k = 0; k < resources.Count; ++k)
                            {
                                var resource = resources[k];
                                if (resource.resourceName == "_FuelCell")
                                {
                                    fuelCellPower += resource.amount * 0.5;
                                }
                            }
                        }
                    }
                }
            }
            fuelCellProd = fuelCellPower;
            return(fuelCellPower);
        }
 /// <summary>
 /// Add the resources to our tracked vessel-wide resource database
 /// </summary>
 /// <param name="resourceList">The part resource list to process.</param>
 private void UpdateResourceList(PartResourceList resourceList)
 {
     if (resourceList != null && resourceList.Count > 0)
     {
         for (int i = resourceList.Count - 1; i >= 0; --i)
         {
             var pr = resourceList[i];
             GetResourceData(pr.info.id).partResources.Add(pr);
         }
     }
 }
Beispiel #7
0
        public double GetResourceVariable(PartResourceList resources)
        {
            double value = 1.0;

            foreach (PartResource r in resources)
            {
                if (Resource_Variables.ContainsKey(r.resourceName))
                {
                    value *= Resource_Variables[r.resourceName];
                }
            }
            return(value);
        }
        private void AlterResources(PartResourceList prl, string opt)
        {
            switch (opt)
            {
            case "remove":
                if (prl != null)
                {
                    int count = prl.Count;

                    Debug.Log(header + " count = " + count.ToString());

                    for (int i = 0; i < count; i++)
                    {
                        ConfigNode n = new ConfigNode();

                        n.AddValue("name", prl[i].resourceName);

                        n.AddValue("amount", prl[i].amount);

                        n.AddValue("maxAmount", prl[i].maxAmount);

                        n.AddValue("isTweakable", prl[i].isTweakable);

                        backupResources.Add(n);
                    }
                    prl.Clear();
                }
                else
                {
                    Debug.Log(header + " PartResourceList is null. Make sure PartResource is defined in CFG file.");
                }
                break;

            case "reset":
                if (prl != null)
                {
                    prl.Clear();

                    for (int i = 0; i < backupResources.Count; i++)
                    {
                        part.AddResource(backupResources[i]);
                    }
                }
                else
                {
                    Debug.Log(header + " PartResourceList is null. Make sure PartResource is defined in CFG file.");
                }

                break;
            }
        }
 private SwitchableResourceSet(string selectorFieldName, PartResourceList partBaseResources)
 {
     this.selectorFieldName = selectorFieldName;
     if (partBaseResources.Count == 0)
     {
         baseResourceNames = EMPTY_RESOURCES;
     }
     else
     {
         baseResourceNames = new HashSet <string>();
         for (int i = 0; i < partBaseResources.Count; ++i)
         {
             baseResourceNames.Add(partBaseResources[i].resourceName);
         }
     }
 }
        public override void OnStart(PartModule.StartState state)
        {
            if (state == StartState.Editor)
            {
                return;
            }
            this.part.force_activate();

            PartResourceList prl = part.Resources;

            foreach (PartResource wanted_resource in prl)
            {
                if (wanted_resource.resourceName == "IntakeAtm")
                {
                    intake_atm = wanted_resource;
                }
            }
        }
Beispiel #11
0
        private PartResource findCargoBay_partResource(Vessel vesselToSearch, string ResourceToSearch)
        {
            PartResource CargoBayPartResourceRef = null;

            foreach (Part p in vesselToSearch.Parts)
            {
                PartResourceList pRL = p.Resources;
                foreach (PartResource pr in pRL)
                {
                    if (pr.resourceName == ResourceToSearch)
                    {
                        CargoBayPartResourceRef = pr;
                    }
                }
            }

            return(CargoBayPartResourceRef);
        }
Beispiel #12
0
        private Dictionary <string, PartResource> getVesselResources(Vessel vesselToSearch)
        {
            Dictionary <string, PartResource> return_prl = new Dictionary <string, PartResource>()
            {
            };

            foreach (Part p in vesselToSearch.Parts)
            {
                PartResourceList pRL = p.Resources;
                foreach (PartResource pr in pRL)
                {
                    if (!return_prl.ContainsKey(pr.resourceName))
                    {
                        return_prl.Add(pr.resourceName, pr);
                    }
                }
            }
            return(return_prl);
        }
Beispiel #13
0
        public static float GetResourceMass(Part part)
        {
            PartResourceDefinitionList definitions = PartResourceLibrary.Instance.resourceDefinitions;
            PartResourceList           resources   = part.Resources;
            PartResourceDefinition     resourceDef;
            float totalResourceMass = 0f;

            foreach (PartResource resource in resources)
            {
                //Find definition
                resourceDef = definitions[resource.resourceName];

                if (resourceDef != null)
                {
                    totalResourceMass += (float)(resourceDef.density * resource.amount);
                }
            }

            return(totalResourceMass);
        }
Beispiel #14
0
        private void tranfer_resource_shipwide(Vessel SourceShip, Vessel TargetShip, string resourceName, float demand)
        {
            PartResource SourcepartResource = null;
            PartResource TargetpartResource = null;

            foreach (Part p in SourceShip.Parts)
            {
                if (SourcepartResource != null)
                {
                    continue;
                }
                PartResourceList pRL = p.Resources;
                foreach (PartResource pr in pRL)
                {
                    if (pr.resourceName == resourceName && pr.amount > 0)
                    {
                        SourcepartResource = pr;
                    }
                }
            }
            foreach (Part p2 in TargetShip.Parts)
            {
                if (TargetpartResource != null)
                {
                    continue;
                }
                PartResourceList pRL2 = p2.Resources;
                foreach (PartResource pr2 in pRL2)
                {
                    if (pr2.resourceName == resourceName && (pr2.maxAmount - pr2.amount) > 0)
                    {
                        TargetpartResource = pr2;
                    }
                }
            }
            tranfer_resource(SourcepartResource, TargetpartResource, demand);

            float power = demand * PT1.L2_usage;

            this.PowSys.draw(PT1.takerName, power);
        }
        bool CheckPartResources(MSSRB_Part part, bool max = true)
        {
            // Check the nextPart's resources and Modules
            // Check for the correct propellant and that it has the MSSRB_Fuel_Segment module
            // If so, add it to the segments list and accumulate some values for the motor
            // then update the propellant value amount in the fuel segment
            PartResourceList prl = part.Resources;
            PartModuleList   pml = part.Modules;

            if (prl.Contains(ModSegSRBs.Propellant) && pml.Contains <MSSRB_Fuel_Segment>())
            {
                Log.Info("CheckPartResources, " + ModSegSRBs.Propellant + " found, " + " MSSRB_Fuel_Segment found");
                MSSRB_Fuel_Segment moduleFuelSegment = pml["MSSRB_Fuel_Segment"] as MSSRB_Fuel_Segment;

                if (max)
                {
                    totalFuelMass += moduleFuelSegment.MaxSolidFuel();  // prl[ModSegSRBs.Propellant].maxAmount;
                }
                else
                {
                    totalFuelMass += prl[ModSegSRBs.Propellant].amount;
                }
                totalMaxThrust += moduleFuelSegment.GetMaxThrust();


                prl[ModSegSRBs.Propellant].amount         = moduleFuelSegment.MaxSolidFuel();
                prl[ModSegSRBs.BurnablePropellant].amount = 0;

                prl[ModSegSRBs.Propellant].maxAmount             =
                    prl[ModSegSRBs.BurnablePropellant].maxAmount = moduleFuelSegment.MaxSolidFuel();
                Log.Info("totalMaxThrust: " + totalMaxThrust + ", totalFuelMass: " + totalFuelMass);


                return(true);
            }
            else
            {
                Log.Info("CheckPartResources, no Propellant found");
                return(false);
            }
        }
Beispiel #16
0
        static public bool isFuelTank(Part p)
        {
            if (p == null)
            {
                ASPConsoleStuff.AAprint("isFuelTank.p is null!");
                return(false);
            }

            PartResourceList rl = p.Resources;

            if (rl == null)
            {
                ASPConsoleStuff.printPart("isFuelTank: Part is NOT a fuel tank, no resources", p);
                return(false);
            }

            if (rl.Count == 0)
            {
                ASPConsoleStuff.printPart("isFuelTank: Part is NOT a fuel tank, no resources", p);
                return(false);
            }

            // Check if this part has any resource that would actually flow through a fuel line
            PartResourceDefinition resource;

            foreach (PartResource pr in rl.dict.Values)
            {
                ASPConsoleStuff.printPart("isFuelTank: part has resource named: " + pr.resourceName, p);
                resource = PartResourceLibrary.Instance.GetDefinition(pr.resourceName);
                if (resource.resourceFlowMode == ResourceFlowMode.STACK_PRIORITY_SEARCH)
                {
                    ASPConsoleStuff.printPart("isFuelTank: Part IS a fuel tank, has flowable resource: " + pr.resourceName, p);
                    return(true);
                }
                else
                {
                    ASPConsoleStuff.printPart("isFuelTank: resource is not flowable: " + resource.resourceFlowMode.ToString(), p);
                }
            }
            return(false);
        }
Beispiel #17
0
        private resData getResourcesInfo(Vessel vesselToSearch, string ResourceToSearch)
        {
            resData tempDat = new resData();

            tempDat.resourceName = ResourceToSearch;
            foreach (Part p in vesselToSearch.Parts)
            {
                PartResourceList pRL = p.Resources;
                foreach (PartResource pr in pRL)
                {
                    if (pr.resourceName == tempDat.resourceName)
                    {
                        tempDat.PartResource    = pr;
                        tempDat.totalAmount    += pr.amount;
                        tempDat.totalWeight    += pr.amount * pr.info.density;
                        tempDat.totalFreeSpace += pr.maxAmount - pr.amount;
                    }
                }
            }
            return(tempDat);
        }
        /// <summary>
        /// Add a set of resources for the specified part.
        /// </summary>
        /// <param name="partName"></param>
        /// <param name="resourcesId"></param>
        /// <param name="displayName"></param>
        /// <param name="selectorFieldName"></param>
        /// <param name="linkedVariants"></param>
        /// <param name="isDefault"></param>
        /// <param name="resources"></param>
        /// <param name="partBaseResources"></param>
        public static void Add(
            string partName,
            string resourcesId,
            string displayName,
            string selectorFieldName,
            HashSet <string> linkedVariants,
            bool isDefault,
            SwitchableResource[] resources,
            PartResourceList partBaseResources)
        {
            SwitchableResourceSet set = null;

            if (!resourcesByPartName.TryGetValue(partName, out set))
            {
                set = new SwitchableResourceSet(selectorFieldName, partBaseResources);
                resourcesByPartName.Add(partName, set);
            }

            set.Add(resourcesId, displayName, linkedVariants, resources);
            if (isDefault && (set.defaultResourcesId == null))
            {
                set.defaultResourcesId = resourcesId;
            }
        }
        void UpdateSegments()
        {
            if (part == null)
            {
                Log.Info("UpdateSegments, part is null");
                return;
            }
            Log.Info("UpdateSegments, updateSegmentsIn: " + updateSegmentsIn);

            // Get a list of all the attached fuel segments, following the nodes from the top node
            // of the engine to the bottom node of the attached segment, and then the top node of the segment, etc
            // Stop when a part does not have the correct fuel resource

            totalMaxThrust     = 0;
            totalSegmentHeight = 0;
            totalFuelMass      = 0;
            segments.Clear();
            Part curPart = this.part;
            Part nextPart;

            AttachNode topAttachNode = this.part.FindAttachNode("top");

            CheckPartResources(this.part as MSSRB_Part);

            while (topAttachNode.attachedPart != null)
            {
                nextPart = topAttachNode.attachedPart;

                // Make sure that the part attached to this part's topNode is that part's bottom node
                AttachNode nextPartBottomNode = nextPart.FindAttachNode("bottom");
                if (nextPartBottomNode == null || nextPartBottomNode.attachedPart != curPart)
                {
                    Log.Info("nextPartBottomNode == null");
                    break;
                }
                if (!CheckPartResources(nextPart as MSSRB_Part))
                {
                    Log.Info("CheckPartResources returns false");
                    break;
                }
                curPart       = nextPart;
                topAttachNode = nextPart.FindAttachNode("top");
                if (topAttachNode == null)
                {
                    break;
                }
            }

            for (int i = 0; i < segments.Count; i++)
            {
                Segment          s   = segments[i];
                PartResourceList prl = s.part.Resources;
                s.ratio = prl[ModSegSRBs.Propellant].maxAmount / totalFuelMass;
                Log.Info("UpdateSegments, segment[" + i + "]: " + s.part.partInfo.title + ", resources: " + prl[ModSegSRBs.Propellant].maxAmount);
            }
            Log.Info("UpdateSegments, totalFuelMass: " + totalFuelMass);

            maxThrust        = (float)totalMaxThrust * thrustModifier;
            maxFuelFlow      = (float)/*Planetarium.fetch.fixedDeltaTime* */ (maxThrust /*  * mixtureDensityRecip  */) / (atmosphereCurve.Evaluate(0f) * g);
            origMaxThrust    = maxThrust;
            maxDisplayThrust = maxThrust;
            PartResource pr = part.Resources[ModSegSRBs.BurnablePropellant];

            if (CoM != null || HighLogic.LoadedSceneIsFlight)
            {
                Log.Info("(1) BurnablePropellant Setting part.amount = 0");
                pr.amount    = 0;
                pr.maxAmount = maxFuelFlow;
            }
            else

            {
                Log.Info("(1) BurnablePropellant Setting part.amount = " + totalFuelMass);
                pr.amount    = totalFuelMass;
                pr.maxAmount = totalFuelMass;
            }

            if (part.segmentWidth > 0)
            {
                float lengthToWidthRatio = totalSegmentHeight / part.segmentWidth;
                heatProduction = baseHeatProduction + 5 * lengthToWidthRatio;

                if (lengthToWidthRatio > HighLogic.CurrentGame.Parameters.CustomParams <MSSRB_1>().maxSafeWidthToLengthRatio)
                {
                    float excess = totalSegmentHeight - part.segmentWidth * HighLogic.CurrentGame.Parameters.CustomParams <MSSRB_1>().maxSafeWidthToLengthRatio;
                    failureChance = Math.Max(failureChance, excess * invTotalSegmentHeight / (50 * 60));
                    if (HighLogic.LoadedSceneIsEditor)
                    {
                        if (HighLogic.CurrentGame.Parameters.CustomParams <MSSRB_1>().warnOnExcessiveHeight)
                        {
                            ScreenMessages.PostScreenMessage("* Warning *  SRB length exceeds safe limits by " + (100 * excess * invTotalSegmentHeight).ToString("F1") + "%", 5);
                        }

                        HighlightStack();
                    }
                }
                else
                {
                    UnHighlightStack();
                    failureChance = HighLogic.CurrentGame.Parameters.CustomParams <MSSRB_1>().failureChance;
                }
            }
            else
            {
                heatProduction = 0;
            }

            totalFuelFlow = maxFuelFlow / Planetarium.fetch.fixedDeltaTime;

#if true
            MonoUtilities.RefreshContextWindows(part);
#else
            MonoUtilities.RefreshPartContextWindow(part);
#endif
            GameEvents.onChangeEngineDVIncludeState.Fire(this);
        }
Beispiel #20
0
        private void GUI_CargoBayFuelTanksMain()
        {
            if (this.VPI.getModules().ContainsKey("LCARS_CargoBay"))
            {
                if (pr_list == null)
                {
                    pr_list = new Dictionary <PartResource, float>()
                    {
                    };
                }
                Part CB_Part = findCargoBay();
                GUILayout.Label("The CargoBay holds the following FuelTanks:");
                GUILayout.BeginVertical();
                PartResourceList pRL = CB_Part.Resources;
                foreach (PartResource pr in pRL)
                {
                    if (!pr_list.ContainsKey(pr))
                    {
                        pr_list.Add(pr, (float)pr.maxAmount);
                    }

                    GUILayout.BeginHorizontal();
                    GUILayout.Label(pr.resourceName);
                    GUILayout.Label(Math.Round(pr.amount, 2).ToString());
                    GUILayout.EndHorizontal();
                    GUILayout.BeginHorizontal();
                    GUILayout.Label("min");
                    GUILayout.Label("current");
                    GUILayout.Label("max");
                    GUILayout.EndHorizontal();
                    GUILayout.BeginHorizontal();
                    GUILayout.Label(Math.Round(pr.amount, 2).ToString());
                    pr_list[pr] = Single.Parse(GUILayout.TextField(Math.Round(pr_list[pr], 2).ToString()
                                                                   , 25));
                    GUILayout.Label(Math.Round((this.STCB.getFreeCargoSpace() / pr.info.density), 2).ToString());
                    GUILayout.EndHorizontal();
                    GUILayout.BeginHorizontal();
                    if (GUILayout.Button("save " + pr.resourceName))
                    {
                        float clamped = Mathf.Clamp(pr_list[pr], (float)pr.amount, (this.STCB.getFreeCargoSpace() / pr.info.density));
                        setTankSize(pr, clamped);
                    }
                    GUILayout.EndHorizontal();
                }
                GUILayout.EndVertical();
                GUILayout.Label(" ");

                GUILayout.Label("The rest of the ship holds the following FuelTanks:");
                GUILayout.BeginVertical();
                GUILayout.BeginHorizontal();
                GUILayout.Label("Name");
                GUILayout.Label("amount");
                GUILayout.Label("max");
                GUILayout.EndHorizontal();
                foreach (Part p in TargetShipSelected.Parts)
                {
                    if (p != CB_Part)
                    {
                        pRL = p.Resources;
                        foreach (PartResource pr in pRL)
                        {
                            GUILayout.BeginHorizontal();
                            GUILayout.Label(pr.resourceName);
                            GUILayout.Label(Math.Round(pr.amount, 2).ToString());
                            GUILayout.Label(Math.Round(pr.maxAmount, 2).ToString());
                            GUILayout.EndHorizontal();
                        }
                    }
                }
                GUILayout.EndVertical();


                float power = PT2.L1_usage;
                this.PowSys.draw(PT2.takerName, power);
            }
            else
            {
                GUILayout.Label("CargoBays? They're not going to be installed until next Tuesday");
            }
        }
Beispiel #21
0
        private PartResource GetResource(string res)
        {
            PartResourceList resourceList = part.Resources;

            return(resourceList.Get(res));
        }
        public static PartResource getResource(Part part, string name)
        {
            PartResourceList resourceList = part.Resources;

            return(resourceList.Get(name));
        }
        public virtual void AddResourceNodes()
        {
            if (this.part.partInfo.partConfig == null)
            {
                return;
            }
            ConfigNode[] nodes     = this.part.partInfo.partConfig.GetNodes("MODULE");
            ConfigNode   adderNode = null;
            ConfigNode   node      = null;
            string       moduleName;
            string       resourceName;
            PartResourceDefinitionList definitions = PartResourceLibrary.Instance.resourceDefinitions;
            PartResourceList           resources   = part.Resources;
            PartResourceDefinition     resourceDef;
            double maxAmount = 0f;

            //Get the switcher config node.
            for (int index = 0; index < nodes.Length; index++)
            {
                node = nodes[index];
                if (node.HasValue("name"))
                {
                    moduleName = node.GetValue("name");
                    if (moduleName == this.ClassName)
                    {
                        adderNode = node;
                        break;
                    }
                }
            }
            if (adderNode == null)
            {
                return;
            }

            //Get the nodes we're interested in
            nodes = adderNode.GetNodes("RESOURCE");

            //Get the option names
            totalResourceCost = 0f;
            for (int index = 0; index < nodes.Length; index++)
            {
                node = nodes[index];
                if (node.HasValue("name") && node.HasValue("amount") && node.HasValue("maxAmount"))
                {
                    //Get name. If the resource already exists then continue.
                    resourceName = node.GetValue("name");

                    //Get max amount
                    if (node.HasValue("maxAmount"))
                    {
                        if (!double.TryParse(node.GetValue("maxAmount"), out maxAmount))
                        {
                            maxAmount = 0f;
                        }
                    }

                    //Tally up the cost
                    resourceDef = definitions[resourceName];
                    if (resourceDef != null)
                    {
                        totalResourceCost += (float)(resourceDef.unitCost * maxAmount);
                    }

                    //Add the resource
                    if (!this.part.Resources.Contains(resourceName))
                    {
                        this.part.Resources.Add(node);
                    }
                }
            }
        }
Beispiel #24
0
        private void GUI_transfer_gui()
        {
            resData      resDat  = getResourcesInfo(this.SourceShipSelected, ResourceToMoveSelected);
            resData      resDat2 = getResourcesInfo(this.TargetShipSelected, ResourceToMoveSelected);
            PartResource CargoBayPartResourceRef = findCargoBay_partResource(this.TargetShipSelected, ResourceToMoveSelected);

            GUILayout.BeginVertical();

            GUILayout.Label("Source Ship Data:");

            GUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            GUILayout.Label("Amount pesent: " + Math.Round(resDat.totalAmount, 2));
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            GUILayout.Label("Free Capacity: " + Math.Round(resDat.totalFreeSpace, 2));
            GUILayout.EndHorizontal();

            GUILayout.EndVertical();

            GUILayout.BeginVertical(scrollview_style2, GUILayout.Width(280));
            FTscrollPosition1 = GUILayout.BeginScrollView(FTscrollPosition1);
            foreach (Part p in this.SourceShipSelected.Parts)
            {
                PartResourceList pRL = p.Resources;
                foreach (PartResource pr in pRL)
                {
                    if (pr.resourceName == resDat.resourceName)
                    {
                        GUILayout.Label(pr.part.partInfo.title + " (" + Math.Round(pr.amount, 2) + ")");
                    }
                }
            }
            GUILayout.EndScrollView();
            GUILayout.EndVertical();

            GUILayout.BeginHorizontal();
            GUILayout.Label("Drain");
            GUILayout.FlexibleSpace();
            GUILayout.Label("Fill");
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            if (GUILayout.RepeatButton("-100"))
            {
                //tranfer_resource(pr, CargoBayPartResourceRef,100);
                tranfer_resource_shipwide(this.SourceShipSelected, this.TargetShipSelected, ResourceToMoveSelected, 100);
            }
            if (GUILayout.RepeatButton("-10"))
            {
                //tranfer_resource(pr, CargoBayPartResourceRef, 10);
                tranfer_resource_shipwide(this.SourceShipSelected, this.TargetShipSelected, ResourceToMoveSelected, 10);
            }
            if (GUILayout.RepeatButton("-1"))
            {
                //tranfer_resource(pr, CargoBayPartResourceRef, 1);
                tranfer_resource_shipwide(this.SourceShipSelected, this.TargetShipSelected, ResourceToMoveSelected, 1);
            }
            if (GUILayout.RepeatButton("+1"))
            {
                //tranfer_resource(CargoBayPartResourceRef, pr, 1);
                tranfer_resource_shipwide(this.TargetShipSelected, this.SourceShipSelected, ResourceToMoveSelected, 1);
            }
            if (GUILayout.RepeatButton("+10"))
            {
                //tranfer_resource(CargoBayPartResourceRef, pr, 10);
                tranfer_resource_shipwide(this.TargetShipSelected, this.SourceShipSelected, ResourceToMoveSelected, 10);
            }
            if (GUILayout.RepeatButton("+100"))
            {
                //tranfer_resource(CargoBayPartResourceRef, pr, 100);
                tranfer_resource_shipwide(this.TargetShipSelected, this.SourceShipSelected, ResourceToMoveSelected, 100);
            }
            GUILayout.EndHorizontal();
        }
        internal void scanVessel()
        {
            List <ProtoCrewMember> VesselCrew = this.vessel.GetVesselCrew();

            this.VPIT.vesselCrew = VesselCrew;

            List <Part> vesselParts = this.vessel.Parts;

            this.VPIT.vesselParts = vesselParts;


            this.VPIT.vesselResourceGenerators = new Dictionary <int, ModuleGenerator>()
            {
            };

            //ModuleGenerator[] MG_list = UnityEngine.Object.FindObjectsOfType<ModuleGenerator>();
            List <ModuleGenerator> MG_list = this.VPIT.vessel.FindPartModulesImplementing <ModuleGenerator>();
            int i = 0;

            foreach (ModuleGenerator MG in MG_list)
            {
                List <ModuleGenerator.GeneratorResource> MG_GR = MG.outputList;
                if (!this.VPIT.vesselResourceGenerators.ContainsKey(i))
                {
                    this.VPIT.vesselResourceGenerators.Add(i, MG);
                }
                i++;
            }

            this.VPIT.vesselPartModules = new Dictionary <string, PartModule>()
            {
            };
            this.VPIT.vesselPartResources = new Dictionary <string, PartResource>()
            {
            };
            this.VPIT.AveragePartTemperature    = 0f;
            this.VPIT.AveragePartTemperatureMax = 0f;
            foreach (Part p in this.VPIT.vesselParts)
            {
                this.VPIT.AveragePartTemperature    += p.temperature;
                this.VPIT.AveragePartTemperatureMax += p.maxTemp;

                this.VPIT.vesselResourceMass += p.GetResourceMass();

                PartModuleList pML = p.Modules;

                foreach (PartModule pm in pML)
                {
                    if (!this.VPIT.vesselPartModules.ContainsKey(pm.moduleName))
                    {
                        this.VPIT.vesselPartModules.Add(pm.moduleName, pm);
                    }
                }

                PartResourceList pRL = p.Resources;
                foreach (PartResource pr in pRL)
                {
                    if (!this.VPIT.vesselPartResources.ContainsKey(pr.resourceName))
                    {
                        this.VPIT.vesselPartResources.Add(pr.resourceName, pr);
                    }
                }
            }
            this.VPIT.heat_percentage          = this.VPIT.AveragePartTemperature / (this.VPIT.AveragePartTemperatureMax / 100);
            this.VPIT.hullintegrity_percentage = 100 - this.VPIT.heat_percentage;
            this.VPIT.vesselWetMass            = this.vessel.GetTotalMass();
            this.VPIT.vesselDryMass           += this.VPIT.vesselWetMass - this.VPIT.vesselResourceMass;
            //UnityEngine.Debug.Log("VesselPartsInventory: print_Modules  vesselResourceMass=" + this.VPIT.vesselResourceMass + "  vesselWetMass=" + this.VPIT.vesselWetMass + "  vesselDryMass=" + this.VPIT.vesselDryMass);
        }
        public void Start()
        {
            Log.Info("Start");
            part.RestoreVariant();
#if false
            SetThrustCurve(grainName, null);
#endif

            Events["TriggerEngineFailureEvent"].active   = HighLogic.CurrentGame.Parameters.CustomParams <MSSRB_1>().devMode;
            Actions["TriggerEngineFailureAction"].active = HighLogic.CurrentGame.Parameters.CustomParams <MSSRB_1>().devMode;

#if false
            foreach (var e in Events)
            {
                Log.Info("Event: " + e.name + ", guiName = " + e.guiName);
            }
            foreach (var a in Actions)
            {
                Log.Info("Action: " + a.name + ", guiName: " + a.guiName);
            }
            foreach (var f in Fields)
            {
                Log.Info("Field: " + f.name + ", guiName: " + f.guiName);
            }
#endif

            Fields["independentThrottle"].guiActive           =
                Fields["independentThrottle"].guiActiveEditor = false;
            Fields["thrustPercentage"].guiActive           =
                Fields["thrustPercentage"].guiActiveEditor = false;



            origAtmosphereCurve = atmosphereCurve;

            PartResourceDefinition prd = PartResourceLibrary.Instance.GetDefinition(ModSegSRBs.Propellant);
            density            = prd.density;
            densityInverse     = 1.0 / prd.density;
            fuelFlowMultiplier = densityInverse * Planetarium.fetch.fixedDeltaTime;

            phl = PartHighlighter.CreatePartHighlighter();
            if (phl)
            {
                highlightID = phl.CreateHighlightList();
            }
            if (highlightID >= 0)
            {
                UpdateHighlightColors();
            }

            PartModuleList pml = part.Modules;
            if (pml.Contains("MSSRB_Fuel_Segment"))
            {
                MSSRB_Fuel_Segment moduleFuelSegment = pml["MSSRB_Fuel_Segment"] as MSSRB_Fuel_Segment;
                part.segmentWidth = moduleFuelSegment.part.segmentWidth;
            }
            else
            {
                Log.Info("MSSRB_Engine, missing MSSRB_Fuel_Segment");
            }

            foreach (var p in propellants)
            {
                if (p.name == ModSegSRBs.BurnablePropellant)
                {
                    break;
                }
                burnablePropIndx++;
            }
            foreach (var p in propellants)
            {
                if (p.name == ModSegSRBs.Propellant)
                {
                    break;
                }
                propIndx++;
            }
            ModSegSRBs.GetExtraInfo(part.baseVariant, ref part.segmentHeight, ref part.segmentWidth);

            switch (HighLogic.LoadedScene)
            {
            case GameScenes.EDITOR:
                GameEvents.onEditorPartPicked.Add(onEditorPartPicked);
                GameEvents.onEditorPartPlaced.Add(onEditorPartPlaced);
                GameEvents.onEditorPartDeleted.Add(onEditorPartDeleted);
                GameEvents.onVariantApplied.Add(onEditorVariantApplied);
                ScheduleSegmentUpdate("Start");
                break;

            case GameScenes.FLIGHT:
                failureChance = HighLogic.CurrentGame.Parameters.CustomParams <MSSRB_1>().failureChance;
                if (this.vessel.situation <= Vessel.Situations.PRELAUNCH)
                {
                    UpdateSegments();
                    foreach (var s in segments)
                    {
                        /* PartModuleList */
                        pml = s.part.Modules;
                        MSSRB_Fuel_Segment moduleFuelSegment = pml["MSSRB_Fuel_Segment"] as MSSRB_Fuel_Segment;

                        PartResourceList prl = s.part.Resources;
                        if (prl.Contains(ModSegSRBs.Propellant))
                        {
                            //Log.Info("Segment: " + s.part.partInfo.title + " contains Propellant");
                            prl[ModSegSRBs.Propellant].amount = moduleFuelSegment.MaxSolidFuel();
                        }
                        if (prl.Contains(ModSegSRBs.BurnablePropellant))
                        {
                            prl[ModSegSRBs.BurnablePropellant].amount = 0;
                            //prl[ModSegSRBs.BurnablePropellant].amount = moduleFuelSegment.MaxSolidFuel();

                            //Log.Info("Segment: " + s.part.partInfo.title + " contains BurnablePropellant");
                        }
                    }
                }
                break;
            } // switch
        }
        bool CheckPartResources(MSSRB_Part part)
        {
            // Check the nextPart's resources and Modules
            // Check for the correct propellant and that it has the MSSRB_Fuel_Segment module
            // If so, add it to the segments list and accumulate some values for the motor
            // then update the propellant value amount in the fuel segment
            PartResourceList prl = part.Resources;
            PartModuleList   pml = part.Modules;

            if (prl.Contains(ModSegSRBs.Propellant) && pml.Contains <MSSRB_Fuel_Segment>())
            {
                Log.Info("CheckPartResources, Part: " + part.partInfo.title + ", " + ModSegSRBs.Propellant + " found, " + " MSSRB_Fuel_Segment found");
                MSSRB_Fuel_Segment moduleFuelSegment = pml["MSSRB_Fuel_Segment"] as MSSRB_Fuel_Segment;
                segments.Add(new Segment(part, moduleFuelSegment.part.segmentHeight));
                totalFuelMass  += moduleFuelSegment.MaxSolidFuel(); // prl[ModSegSRBs.Propellant].maxAmount;
                totalMaxThrust += moduleFuelSegment.GetMaxThrust();
                Log.Info("CheckPartResources, moduleFuelSegment.part.segmentHeight: " + moduleFuelSegment.part.segmentHeight);
                totalSegmentHeight += moduleFuelSegment.part.segmentHeight;

                Log.Info("CheckPartResources, totalFuelMass: " + totalFuelMass + ", totalMaxThrust: " + totalMaxThrust + ", totalSegmentHeight: " + totalSegmentHeight);

                if (totalSegmentHeight > 0)
                {
                    invTotalSegmentHeight = 1 / totalSegmentHeight;
                }
                else
                {
                    invTotalSegmentHeight = 0;
                }

                moduleFuelSegment.baseEngine = this;

                if (CoM == null && HighLogic.LoadedSceneIsEditor)
                {
                    prl[ModSegSRBs.Propellant].amount        =
                        prl[ModSegSRBs.Propellant].maxAmount = 0;

                    prl[ModSegSRBs.BurnablePropellant].amount        =
                        prl[ModSegSRBs.BurnablePropellant].maxAmount = moduleFuelSegment.MaxSolidFuel();
                    Log.Info("(2) Propellant Setting part.amount = 0, BurnablePropellant maxAmount set to: " + prl[ModSegSRBs.BurnablePropellant].maxAmount);
                }
                else
                {
                    prl[ModSegSRBs.Propellant].amount        =
                        prl[ModSegSRBs.Propellant].maxAmount = moduleFuelSegment.MaxSolidFuel();

                    prl[ModSegSRBs.BurnablePropellant].amount        =
                        prl[ModSegSRBs.BurnablePropellant].maxAmount = 0;
                }

                Log.Info("totalMaxThrust: " + totalMaxThrust + ", totalFuelMass: " + totalFuelMass);

                // Set the baseEngine in the segment ends here, do it in a loop
                // because there can be either one or two ends
                foreach (var p in pml)
                {
                    if (p is MSSRB_SegmentEnds)
                    {
                        MSSRB_SegmentEnds mssrb = p as MSSRB_SegmentEnds;
                        mssrb.baseEngine      = this;
                        mssrb.maxThrust       = this.maxThrust;
                        mssrb.atmosphereCurve = this.atmosphereCurve;
                    }
                }

                return(true);
            }
            else
            {
                Log.Info("CheckPartResources, no Propellant found");
                return(false);
            }
        }
        void UpdateSegmentsInFlight()
        {
            if (brokenSRB)
            {
                Log.Info("UpdateSegmentsInFlight, part.Resources[ModSegSRBs.BurnablePropellant].amount: " + part.Resources[ModSegSRBs.BurnablePropellant].amount);
                Log.Info("UpdateSegmentsInFlight, part.Resources[ModSegSRBs.AbortedPropellant].amount: " + part.Resources[ModSegSRBs.AbortedPropellant].amount);
            }
            if (this.EngineIgnited && !brokenSRB)
            {
                if (failureChance > 0)
                {
                    if (!sickEngine && (UnityEngine.Random.Range(0f, 1f) <= failureChance || triggerEngineFailure))
                    {
                        ScreenMessages.PostScreenMessage("***** Warning ***** Warning ***** Warning *****", 5);
                        ScreenMessages.PostScreenMessage("***** SRB failure in progress *****", 5);

                        HighlightStack();
                        AudibleAlertCore.fetch.ActivateAlarm(5);

                        if (UnityEngine.Random.Range(0, 1f) <= HighLogic.CurrentGame.Parameters.CustomParams <MSSRB_1>().explosionChance)
                        {
                            // Part will explode in 5 seconds
                            explode = fEngRunTime;
                        }
                        else
                        {
                            // Start reducing thrust
                            sickEngine     = true;
                            heatProduction = 0;
                        }
                    }
                }
                if (explode > 0 && fEngRunTime - explode > 5)
                {
                    // Pick a part and destroy it
                    int i = UnityEngine.Random.Range(0, segments.Count - 1);
                    segments[i].part.explode();
                }
                if (sickEngine)
                {
                    engineHealth = Math.Max(0.1f, engineHealth - UnityEngine.Random.Range(0, 0.01f));
                }
#if THRUSTVARIABILITY
                if (engineHealth < oldEngineHealth || HighLogic.CurrentGame.Parameters.CustomParams <MSSRB_1>().hasThrustVariability)
#else
                if (engineHealth < oldEngineHealth)
#endif
                {
                    oldEngineHealth = engineHealth;
#if THRUSTVARIABILITY
                    if (HighLogic.CurrentGame.Parameters.CustomParams <MSSRB_1>().hasThrustVariability)
                    {
                        fEngRunTime += TimeWarp.fixedDeltaTime;
                        var v = HighLogic.CurrentGame.Parameters.CustomParams <MSSRB_1>().maxVariability *Mathf.Sin(fEngRunTime * Mathf.PI * 2 / ModSegSRBs.pressurePulseHertz);
                        Log.Info("ThrustVariability: " + v);
                        ChangeUsage(engineHealth, v);
                    }
                    else
#endif
                    {
                        ChangeUsage(engineHealth);
                    }
                }

                PartResource secondPartResource = null;

                totalFuelFlow = 0;

                double totalCurrentFuel = 0;
                double fuelFlow         = 0;
                double totalMaxFuel     = 0;
                var    maxFuelFlow      = getMaxFuelFlow(propellants[burnablePropIndx]) * Time.fixedDeltaTime - part.Resources[ModSegSRBs.BurnablePropellant].amount + 0.05f;


                for (int i = 0; i < segments.Count; i++)
                {
                    Segment            segment           = segments[i];
                    MSSRB_Fuel_Segment moduleFuelSegment = segment.part.Modules["MSSRB_Fuel_Segment"] as MSSRB_Fuel_Segment;
                    Log.Info("UpdateSegmentsInFlight, segmentheight: " + segment.segmentHeight);
                    if (segment.segmentHeight > 0)
                    {
                        fuelFlow = segment.segmentHeight * invTotalSegmentHeight * maxFuelFlow;

                        fuelFlow    *= engineHealth;
                        maxFuelFlow *= engineHealth;

                        PartResourceList prl = segment.part.Resources;
                        double           availableResource = Math.Min(fuelFlow, prl[ModSegSRBs.Propellant].amount);
                        prl[ModSegSRBs.Propellant].amount -= availableResource;
                        totalFuelFlow    += availableResource;
                        totalCurrentFuel += prl[ModSegSRBs.Propellant].amount;
                        totalMaxFuel     += prl[ModSegSRBs.Propellant].maxAmount;

                        if (((MSSRB_SegmentEnds)segment.part.Modules["MSSRB_SegmentEnds"]).EngineIgnited)
                        {
                            secondPartResource = segment.part.Resources[ModSegSRBs.BurnablePropellant];
                        }
                        else
                        {
                            secondPartResource = null;
                        }
                    }
                }

                totalFuelFlow *= Time.timeScale;
                if (totalFuelFlow > 0)
                {
                    if (secondPartResource != null)
                    {
                        double t = (secondPartResource.amount + totalFuelFlow + part.Resources[ModSegSRBs.BurnablePropellant].amount) / 2;
                        secondPartResource.amount =
                            part.Resources[ModSegSRBs.BurnablePropellant].amount = t;
                    }
                    else
                    {
                        // Update resources in the engine, and then update
                        // the maxAmount with the inverse of the percentage of fuel remaining
                        // This fools the game into showing the correct amount in the staging toolbar
                        part.Resources[ModSegSRBs.BurnablePropellant].amount += totalFuelFlow;
                    }
                    // Now update the maxAmount with the inverse of the percentage of fuel remaining
                    // This fools the game into showing the correct amount in the staging toolbar

                    part.Resources[ModSegSRBs.BurnablePropellant].maxAmount =
                        part.Resources[ModSegSRBs.BurnablePropellant].amount * totalFuelMass / (part.Resources[ModSegSRBs.BurnablePropellant].amount + totalCurrentFuel);
                    percentLeft = (float)(totalCurrentFuel / totalMaxFuel);
                }
            }
        }