/// <summary>
        /// Gets the resource content percentage on the current planet.
        /// Takes a CrustalResource as a parameter.
        /// Returns boolean true if the data was gotten without trouble and also returns a double with the percentage.
        /// </summary>
        /// <param name="vessel"></param>
        /// <param name="currentResource">A CrustalResource we want to get the percentage for.</param>
        /// <returns></returns>
        private static CrustalResourceAbundance GetResourceAbundance(Vessel vessel, CrustalResource currentResource)
        {
            var abundance = new CrustalResourceAbundance()
            {
                Resource = currentResource
            };

            if (currentResource != null)
            {
                try
                {
                    abundance.Local = GetAbundance(new AbundanceRequest()
                    {
                        ResourceType = HarvestTypes.Planetary,
                        ResourceName = currentResource.ResourceName,
                        BodyId       = FlightGlobals.currentMainBody.flightGlobalsIndex,
                        Latitude     = vessel.latitude,
                        Longitude    = vessel.longitude,
                        CheckForLock = false
                    });
                }
                catch (Exception)
                {
                    Console.WriteLine("[KSPI]: UniversalCrustExtractor - Error while retrieving crustal resource percentage for " + currentResource.ResourceName + " from CrustalResourceHandler. Setting to zero.");
                    return(null); // if the percentage was not gotten correctly, we want to know, so return false
                }

                return(abundance); // if we got here, the percentage-getting went well, so return true
            }
            else
            {
                Console.WriteLine("[KSPI]: UniversalCrustExtractor - Error while calculating percentage, resource null. Setting to zero.");
                return(null); // resource was null, we want to know that we should disregard it, so return false
            }
        }
예제 #2
0
        private static void AddResource(string outputResourceName, string displayName, int refBody, List <CrustalResource> bodyCrustalComposition, string[] variants)
        {
            var abundances = new[] { GetAbundance(outputResourceName, refBody) }.Concat(variants.Select(m => GetAbundance(m, refBody)));

            var crustalResource = new CrustalResource(outputResourceName, abundances.Max(), displayName, variants);

            if (crustalResource.ResourceAbundance > 0)
            {
                var existingResource = bodyCrustalComposition.FirstOrDefault(a => a.ResourceName == outputResourceName);
                if (existingResource != null)
                {
                    Debug.Log("[KSPI]: replaced resource " + outputResourceName + " with stock defined abundance " + crustalResource.ResourceAbundance);
                    bodyCrustalComposition.Remove(existingResource);
                }
                bodyCrustalComposition.Add(crustalResource);
            }
        }
예제 #3
0
        private static void AddResource(int refBody, List <CrustalResource> bodyCrustalComposition, string outputResourceName, string inputResource1, string inputResource2, string inputResource3, string displayName)
        {
            var abundances = new[] { GetAbundance(inputResource1, refBody), GetAbundance(inputResource2, refBody), GetAbundance(inputResource2, refBody) };

            var crustalResource = new CrustalResource(outputResourceName, abundances.Max(), displayName, new[] { inputResource1, inputResource2, inputResource3 });

            if (crustalResource.ResourceAbundance > 0)
            {
                var existingResource = bodyCrustalComposition.FirstOrDefault(a => a.ResourceName == outputResourceName);
                if (existingResource != null)
                {
                    Debug.Log("[KSPI]: replaced resource " + outputResourceName + " with stock defined abundance " + crustalResource.ResourceAbundance);
                    bodyCrustalComposition.Remove(existingResource);
                }
                bodyCrustalComposition.Add(crustalResource);
            }
        }
예제 #4
0
        private static void AddMissingResource(string resourceName, int refBody, List <CrustalResource> bodyCrustalComposition)
        {
            if (resourceName == KITResourceSettings.Regolith)
            {
                Debug.Log("[KSPI]: AddMissingResource : Ignored Regolith");
                return;
            }

            // verify it is a defined resource
            PartResourceDefinition definition = PartResourceLibrary.Instance.GetDefinition(resourceName);

            if (definition == null)
            {
                Debug.LogWarning("[KSPI]: AddMissingResource : Failed to find resource definition for '" + resourceName + "'");
                return;
            }

            // skip it already registered or used as a Synonym
            if (bodyCrustalComposition.Any(m => m.ResourceName == definition.name || m.DisplayName == definition.displayName || m.Synonyms.Contains(definition.name)))
            {
                Debug.Log("[KSPI]: AddMissingResource : Already found existing composition for '" + resourceName + "'");
                return;
            }

            // retrieve abundance
            var abundance = GetAbundance(definition.name, refBody);

            if (abundance <= 0)
            {
                Debug.LogWarning("[KSPI]: AddMissingResource : Abundance for resource '" + resourceName + "' was " + abundance);
            }

            // create Crustal resource from definition and abundance
            var crustalResource = new CrustalResource(definition, abundance);

            // add to Crustal composition
            Debug.Log("[KSPI]: AddMissingResource : add resource '" + resourceName + "'");
            bodyCrustalComposition.Add(crustalResource);
        }