Inheritance: CFGUtilObject
Beispiel #1
0
        public override void OnLoad(ConfigNode node)
        {
            base.OnLoad(node);

            // This will deactivate objects before the part icon is created, avoiding a visual mess
            for (int i = 0; i < subtypes.Count; i++)
            {
                PartSubtype subtype = subtypes[i];
                subtype.SetParent(this);
                subtype.FindObjects();

                subtype.DeactivateObjects();
            }

            CurrentSubtype.ActivateObjects();
        }
Beispiel #2
0
        public float TankVolumeForSubtype(int index)
        {
            if (index < 0 || index >= SubtypesCount)
            {
                throw new IndexOutOfRangeException("Index " + index.ToString() + " is out of range (there are " + SubtypesCount.ToString() + "subtypes.");
            }
            PartSubtype subtype = subtypes[index];

            if (subtype == null || subtype.tankType == null || subtype.tankType.ResourcesCount == 0)
            {
                return(0f);
            }
            else
            {
                return(baseVolume * subtype.volumeMultiplier + subtype.volumeAdded);
            }
        }
        public PartSubtype FindBestSubtype(IEnumerable <PartSubtype> subtypes, IEnumerable <string> resourceNamesOnPart)
        {
            // No managed resources, so no way to guess subtype
            if (!subtypes.Any(s => s.HasTank))
            {
                return(subtypes.MaxBy(subtype => subtype.defaultSubtypePriority));
            }

            // Now use resources
            // This finds all the managed resources that currently exist on teh part
            IEnumerable <string> managedResourceNames = subtypes.SelectMany(s => s.ResourceNames).Distinct();

            string[] managedResourcesOnPart = managedResourceNames.Intersect(resourceNamesOnPart).ToArray();

            // If any of the part's current resources are managed, look for a subtype which has all of the managed resources (and all of its resources exist)
            // Otherwise, look for a structural subtype (no resources)
            if (managedResourcesOnPart.Any())
            {
                PartSubtype bestSubtype = null;

                foreach (PartSubtype subtype in subtypes)
                {
                    if (!subtype.HasTank)
                    {
                        continue;
                    }
                    if (!subtype.ResourceNames.SameElementsAs(managedResourcesOnPart))
                    {
                        continue;
                    }
                    if (bestSubtype.IsNotNull() && subtype.defaultSubtypePriority <= bestSubtype.defaultSubtypePriority)
                    {
                        continue;
                    }
                    bestSubtype = subtype;
                }

                if (bestSubtype.IsNotNull())
                {
                    return(bestSubtype);
                }
            }
            else
            {
                PartSubtype bestSubtype = null;

                foreach (PartSubtype subtype in subtypes)
                {
                    if (subtype.HasTank)
                    {
                        continue;
                    }
                    if (bestSubtype.IsNotNull() && subtype.defaultSubtypePriority <= bestSubtype.defaultSubtypePriority)
                    {
                        continue;
                    }
                    bestSubtype = subtype;
                }

                if (bestSubtype.IsNotNull())
                {
                    return(bestSubtype);
                }
            }

            // No useful way to determine correct subtype, just pick first
            return(subtypes.MaxBy(subtype => subtype.defaultSubtypePriority));
        }
Beispiel #4
0
        public override void OnStart(PartModule.StartState state)
        {
            base.OnStart(state);

            // Initialize stuff

            managedResourceNames  = new List <string>();
            managedTransformNames = new List <string>();
            managedStackNodeIDs   = new List <string>();

            MaxTempManaged     = false;
            SkinMaxTempManaged = false;
            AttachNodeManaged  = false;

            for (int i = 0; i < subtypes.Count; i++)
            {
                PartSubtype subtype = subtypes[i];
                subtype.SetParent(this);
                subtype.OnStart();
                TankType tank = subtype.tankType;

                if (tank == null)
                {
                    LogError("Tank is null on subtype " + subtype.Name);
                }

                if (tank.ResourcesCount > 0 && (TankVolumeForSubtype(i) <= 0f))
                {
                    LogError("Subtype " + subtype.Name + " has a tank type with resources, but no volume is specifified");
                    subtype.tankType = tank = B9TankSettings.StructuralTankType;
                }

                if (tank != null)
                {
                    managedResourceNames.AddRange(tank.ResourceNames);
                }

                managedTransformNames.AddRange(subtype.transformNames);
                managedStackNodeIDs.AddRange(subtype.NodeIDs);

                if (subtype.maxTemp > 0f)
                {
                    MaxTempManaged = true;
                }
                if (subtype.skinMaxTemp > 0f)
                {
                    SkinMaxTempManaged = true;
                }
                if (subtype.attachNode.IsNotNull())
                {
                    if (part.attachRules.allowSrfAttach && part.srfAttachNode != null)
                    {
                        AttachNodeManaged = true;
                    }
                    else
                    {
                        LogError("Error: Part subtype '" + subtype.Name + "' has an attach node defined, but part does not allow surface attachment (or the surface attach node could not be found)");
                    }
                }
            }

            if (currentSubtypeIndex >= subtypes.Count || currentSubtypeIndex < 0)
            {
                currentSubtypeIndex = 0;
            }

            bool editor = (state == StartState.Editor);

            SetupGUI();

            for (int i = 0; i < subtypes.Count; i++)
            {
                subtypes[i].DeactivateObjects();
                if (editor)
                {
                    subtypes[i].DeactivateNodes();
                }
                else
                {
                    subtypes[i].ActivateNodes();
                }
            }

            UpdateSubtype(false);
        }