public static void loadPlanetaryResourceData(int body) { string celestial_body_name = FlightGlobals.Bodies[body].bodyName; UrlDir.UrlConfig[] configs = GameDatabase.Instance.GetConfigs("PLANETARY_RESOURCE_DEFINITION"); Debug.Log("[KSP Interstellar] Loading Planetary Resource Data. Length: " + configs.Length); foreach (ResourceAbundanceMarker abundance_marker in abundance_markers) { removeAbundanceSphere(abundance_marker.getPlanetarySphere()); removeAbundanceSphere(abundance_marker.getScaledSphere()); } sphere = null; sphere_texture = null; body_resource_maps.Clear(); body_abudnance_angles.Clear(); map_body = -1; current_body = -1; foreach (UrlDir.UrlConfig config in configs) { ConfigNode planetary_resource_config_node = config.config; if (planetary_resource_config_node.GetValue("celestialBodyName") == celestial_body_name && planetary_resource_config_node != null) { Debug.Log("[KSP Interstellar] Loading Planetary Resource Data for " + celestial_body_name); Texture2D map = GameDatabase.Instance.GetTexture(planetary_resource_config_node.GetValue("mapUrl"), false); if (map == null) { continue; } string resource_gui_name = planetary_resource_config_node.GetValue("name"); FNPlanetaryResourceInfo resource_info = new FNPlanetaryResourceInfo(resource_gui_name, map, body); if (planetary_resource_config_node.HasValue("resourceName")) { string resource_name = planetary_resource_config_node.GetValue("resourceName"); resource_info.setResourceName(resource_name); } if (planetary_resource_config_node.HasValue("resourceScale")) { string resource_scale = planetary_resource_config_node.GetValue("resourceScale"); resource_info.setResourceScale(resource_scale); } if (planetary_resource_config_node.HasValue("scaleFactor")) { string scale_factorstr = planetary_resource_config_node.GetValue("scaleFactor"); double scale_factor = double.Parse(scale_factorstr); resource_info.setScaleFactor(scale_factor); } if (planetary_resource_config_node.HasValue("scaleMultiplier")) { string scale_multstr = planetary_resource_config_node.GetValue("scaleMultiplier"); double scale_mult = double.Parse(scale_multstr); resource_info.setScaleMultiplier(scale_mult); } if (planetary_resource_config_node.HasValue("displayTexture")) { string tex_path = planetary_resource_config_node.GetValue("displayTexture"); resource_info.setDisplayTexture(tex_path); } else { string tex_path = planetary_resource_config_node.GetValue("WarpPlugin/resource_point"); resource_info.setDisplayTexture(tex_path); } if (planetary_resource_config_node.HasValue("displayThreshold")) { string display_threshold_str = planetary_resource_config_node.GetValue("displayThreshold"); double display_threshold = double.Parse(display_threshold_str); resource_info.setDisplayThreshold(display_threshold); } body_resource_maps.Add(resource_gui_name, resource_info); List<Vector2d> abundance_points_list = new List<Vector2d>(); for (int i = 0; i < map.height; ++i) { for (int j = 0; j < map.width; ++j) { if (getPixelAbundanceValue(j,i, resource_info) >= resource_info.getDisplayThreshold()) { //high value location, mark it double theta = (j - map.width / 2)*2.0*180.0/map.width; double phi = (i - map.height / 2)*2.0*90.0/map.height; Vector2d angles = new Vector2d(theta, phi); //body_abudnance_angles.Add(resource_gui_name, angles); abundance_points_list.Add(angles); } } } body_abudnance_angles.Add(resource_gui_name, abundance_points_list.ToArray()); Debug.Log("[KSP Interstellar] " + abundance_points_list.Count + " high value " + resource_gui_name + " locations detected"); } } current_body = body; }
protected static double getPixelAbundanceValue(int pix_x, int pix_y, FNPlanetaryResourceInfo resource_info) { Texture2D map = resource_info.getResourceMap(); Color pix_color = map.GetPixel(pix_x, pix_y); double resource_val = 0; double scale_factor = resource_info.getScaleFactor(); double scale_multiplier = resource_info.getScaleMultiplier(); if (resource_info.getResourceScale() == FNPlanetaryResourceInfo.LOG_SCALE) { resource_val = Math.Pow(scale_factor, pix_color.grayscale * 255.0) / 1000000 * scale_multiplier; }else if (resource_info.getResourceScale() == FNPlanetaryResourceInfo.LINEAR_SCALE) { resource_val = pix_color.grayscale * scale_multiplier; } return resource_val; }