private static void BuildOreProbabilities(MyPlanetDefinition planetDefinition)
 {
     m_oreCummulativeProbability = 0.0f;
     if (planetDefinition.MetalsOreProbability != null)
     {
         foreach (var oreProbability in planetDefinition.MetalsOreProbability)
         {
             MyOreProbability probability = new MyOreProbability();
             probability.Probability = MyRandom.Instance.NextFloat(oreProbability.Min, oreProbability.Max);
             m_oreCummulativeProbability += probability.Probability;
             probability.CummulativeProbability = m_oreCummulativeProbability;
             probability.OreName = oreProbability.OreName;
             m_oreProbalities.Add(probability);
         }
     }
 }
        private static MyMaterialLayer[] CreateMaterialLayers(MyPlanetDefinition planetDefinition, bool isHostile, MyRandom random, float averagePlanetRadius, float hillHalfDeviation, float canyonHalfDeviation, ref float outerRadius, ref float innerRadius)
        {
            int numLayers = random.Next((int)planetDefinition.NumLayers.Min, (int)planetDefinition.NumLayers.Max);

            float startHeight = averagePlanetRadius - canyonHalfDeviation;
            outerRadius = averagePlanetRadius + hillHalfDeviation;
            innerRadius = averagePlanetRadius - canyonHalfDeviation;

            int layerOffset = 0;

            MyMaterialLayer southPoleLayer = CreatePoleLayer(random, planetDefinition.SouthPole, startHeight, outerRadius, ref layerOffset);
            MyMaterialLayer northPoleLayer = CreatePoleLayer(random, planetDefinition.NorthPole, startHeight, outerRadius, ref layerOffset);


            MyMaterialLayer[] materialLayers = new MyMaterialLayer[numLayers + layerOffset];

            float endAngle = 1;
            float startAngle = -1;
            int currentLayer = 0;

            if (southPoleLayer != null)
            {
                materialLayers[currentLayer] = southPoleLayer;
                endAngle = southPoleLayer.StartAngle;
                currentLayer++;
            }

            if (northPoleLayer != null)
            {
                materialLayers[currentLayer] = northPoleLayer;
                northPoleLayer.EndAngle = northPoleLayer.StartAngle;
                northPoleLayer.StartAngle = -1.0f;
                northPoleLayer.AngleEndDeviation = northPoleLayer.AngleStartDeviation;
                northPoleLayer.AngleStartDeviation = 0.0f;
                startAngle = northPoleLayer.EndAngle;
            }

            float step = (outerRadius - innerRadius) / materialLayers.Length;


            float organicHeightEnd = random.NextFloat(planetDefinition.OrganicHeightEnd.Min, planetDefinition.OrganicHeightEnd.Max);
            float metalsHeightEnd = random.NextFloat(planetDefinition.MetalsHeightEndHostile.Min, planetDefinition.MetalsHeightEndHostile.Max);
            float floraMaterialSpawnProbability = random.NextFloat(planetDefinition.FloraMaterialSpawnProbability.Min, planetDefinition.FloraMaterialSpawnProbability.Max);
            float metalsSpawnProbability = random.NextFloat(planetDefinition.MetalsSpawnProbability.Min, planetDefinition.MetalsSpawnProbability.Max);
            float metalsSpawnValue = random.NextFloat(0, 1);

            for (int i = layerOffset; i < materialLayers.Length; ++i)
            {
                float layerHeight = random.NextFloat(0, step);
                materialLayers[i] = new MyMaterialLayer();
                materialLayers[i].StartHeight = startHeight;
                materialLayers[i].EndHeight = startHeight + layerHeight;
                materialLayers[i].StartAngle = startAngle;
                materialLayers[i].EndAngle = endAngle;
                materialLayers[i].HeightStartDeviation = random.NextFloat(0, 100.0f / (float)(i + 1));
                materialLayers[i].AngleStartDeviation = 0;
                materialLayers[i].HeightEndDeviation = random.NextFloat(0, 100.0f / (float)(i + 1));
                materialLayers[i].AngleEndDeviation = 0;

                MyVoxelMaterialDefinition materialDefinition = null;

                if (m_materialsByOreType.ContainsKey("Stone") == true)
                {
                    materialDefinition = m_materialsByOreType["Stone"][random.Next() % m_materialsByOreType["Stone"].Count];
                }

                if (planetDefinition.HasAtmosphere && isHostile == false)
                {
                    if ((outerRadius - startHeight) > ((outerRadius - innerRadius) * (1 - organicHeightEnd)))
                    {
                        float value = random.NextFloat(0, 1);
                        if (value > floraMaterialSpawnProbability)
                        {
                            materialDefinition = m_organicMaterials[random.Next() % m_organicMaterials.Count];
                        }
                        else
                        {
                            materialDefinition = m_spawningMaterials[random.Next() % m_spawningMaterials.Count];
                        }
                    }
                }
                else
                {
                    if (metalsSpawnValue < metalsSpawnProbability)
                    {
                        if ((outerRadius - startHeight) > ((outerRadius - innerRadius) * (1 - metalsHeightEnd)))
                        {
                            MyOreProbability probablity = GetOre(random.NextFloat(0, 1));
                            if (probablity != null)
                            {
                                materialLayers[i].EndHeight = materialLayers[i].StartHeight - 1;
                                materialLayers[i].HeightStartDeviation *= probablity.Probability;
                                materialLayers[i].HeightEndDeviation *= probablity.Probability;

                                materialDefinition = m_materialsByOreType[probablity.OreName][random.Next() % m_materialsByOreType[probablity.OreName].Count];
                            }
                        }
                    }
                }

                materialLayers[i].MaterialDefinition = materialDefinition;
                startHeight += layerHeight;
            }
            return materialLayers;
        }