public static MyMwcObjectBuilder_StaticAsteroid GenerateStaticAsteroid(float sizeInMeters, MyStaticAsteroidTypeSetEnum typeSet, MyMwcVoxelMaterialsEnum material, Vector3 position, Random rnd, List<MyMwcObjectBuilder_StaticAsteroid_TypesEnum> asteroids)
        {
            int size = FindAsteroidSize(sizeInMeters, MyMwcObjectBuilder_StaticAsteroid.AsteroidSizes);
            asteroids.Clear();
            MyMwcObjectBuilder_StaticAsteroid.GetAsteroids(size, typeSet, asteroids);
            int rndIndex = rnd.Next(0, asteroids.Count);
            
            var builder = new MyMwcObjectBuilder_StaticAsteroid(asteroids[rndIndex], material);
            builder.PositionAndOrientation.Position = position;
            builder.PositionAndOrientation.Forward = rnd.Vector(1);
            builder.PositionAndOrientation.Up = rnd.Vector(1);
            builder.IsDestructible = false;

            return builder;
        }
        public void GenerateSectorObjectBuildersFromSolarEntities(List<MySolarSystemMapEntity> entities, List<MyMwcObjectBuilder_Base> addToList, Random rnd, MyWeightDictionary<MyMwcVoxelMaterialsEnum> primaryMaterials, MyWeightDictionary<MyMwcVoxelMaterialsEnum> secondaryMaterials, MyStaticAsteroidTypeSetEnum staticAsteroidTypesets, MyMwcVoxelMaterialsEnum? fieldMaterial = null, MySolarSystemArea.AreaEnum? areaType = null)
        {
            List<MyMwcObjectBuilder_StaticAsteroid_TypesEnum> asteroids = new List<MyMwcObjectBuilder_StaticAsteroid_TypesEnum>(5);
            List<MyMwcVoxelFilesEnum> voxelAsteroids = new List<MyMwcVoxelFilesEnum>(10);

            int count = addToList.Count;

            foreach (var e in entities)
            {
                if (e.EntityType == MySolarSystemEntityEnum.VoxelAsteroid)
                {
                    int voxelAsteroidSize = FindAsteroidSize(e.Radius, MyVoxelMap.AsteroidSizes);

                    int rndIndex = rnd.Next(0, voxelAsteroids.Count);

                    MyVoxelMap.GetAsteroidsBySizeInMeters(voxelAsteroidSize, voxelAsteroids, false);

                    MyMwcObjectBuilder_VoxelMap builder = GenerateVoxelMap(voxelAsteroidSize, e.PositionInSector, rnd, voxelAsteroids, primaryMaterials, secondaryMaterials);

                    addToList.Add(builder);
                }
                else if (e.EntityType == MySolarSystemEntityEnum.StaticAsteroid)
                {
                    float radius = 100;
                    if (e.Radius == 10000)
                        radius = rnd.Next(2000, 11000);
                    if (e.Radius == 1000)
                        radius = rnd.Next(100, 1100);
                    if (e.Radius == 100)
                        radius = rnd.Next(10, 100);


                    MyMwcVoxelMaterialsEnum asteroidMaterial = MyMwcVoxelMaterialsEnum.Stone_01;
                    if (primaryMaterials.Count > 0)
                        primaryMaterials.GetRandomItem(rnd);

                    MyStaticAsteroidTypeSetEnum asteroidType = MyStaticAsteroidTypeSetEnum.A;

                    //for (int i = 0; i < 40000000; i++)
                    {
                        asteroidType = (MyStaticAsteroidTypeSetEnum)rnd.Item(Enum.GetValues(typeof(MyStaticAsteroidTypeSetEnum)));
                    }

                    if ((staticAsteroidTypesets & MyStaticAsteroidTypeSetEnum.A) == MyStaticAsteroidTypeSetEnum.A)
                        asteroidType = MyStaticAsteroidTypeSetEnum.A;
                    if ((staticAsteroidTypesets & MyStaticAsteroidTypeSetEnum.B) == MyStaticAsteroidTypeSetEnum.B)
                        asteroidType = MyStaticAsteroidTypeSetEnum.B;
                    if ((staticAsteroidTypesets & MyStaticAsteroidTypeSetEnum.All) == MyStaticAsteroidTypeSetEnum.All)
                        asteroidType = rnd.Float(0, 1) > 0.5f ? MyStaticAsteroidTypeSetEnum.A : MyStaticAsteroidTypeSetEnum.B;

                    var builder = GenerateStaticAsteroid(radius, asteroidType, asteroidMaterial, e.PositionInSector, rnd, asteroids);

  
                    builder.AsteroidMaterial1 = fieldMaterial;
                    if (areaType == MySolarSystemArea.AreaEnum.Sun)
                    {
                        builder.FieldDir = MinerWars.AppCode.Game.GUI.MyGuiScreenGamePlay.Static.GetDirectionToSunNormalized();
                    }

                    builder.Generated = true;
                    addToList.Add(builder);

                 
                    //MyEntity ent = MyEntities.CreateFromObjectBuilderAndAdd(null, new MyMwcObjectBuilder_StaticAsteroid(asteroids[rndIndex], mat),
                    //    Matrix.CreateWorld(e.PositionInSector, rnd.Vector(1), rnd.Vector(1)));
                }
                else if (e.EntityType == MySolarSystemEntityEnum.LargeShip)
                {
                    var shipType = rnd.Enum<MyMwcObjectBuilder_PrefabLargeShip_TypesEnum>();
                    MyMwcObjectBuilder_Prefab_AppearanceEnum appearance = rnd.Enum<MyMwcObjectBuilder_Prefab_AppearanceEnum>();

                    var ship = new MyMwcObjectBuilder_PrefabLargeShip(shipType, appearance, new MyMwcVector3Short(0, 0, 0), rnd.Vector(1), null, rnd.FloatNormal(), "Abandoned large ship", 0, false, 0);                    
                    var gamePlayProperties = MyGameplayConstants.GetGameplayProperties(MyMwcObjectBuilderTypeEnum.PrefabLargeShip, (int)shipType, MyMwcObjectBuilder_FactionEnum.Euroamerican);
                    ship.PrefabHealthRatio = MyGameplayConstants.HEALTH_RATIO_MAX;
                    ship.PrefabMaxHealth = gamePlayProperties.MaxHealth;
                    var prefabs = new List<MyMwcObjectBuilder_PrefabBase>();
                    prefabs.Add(ship);
                    var container = new MyMwcObjectBuilder_PrefabContainer(0, MyMwcObjectBuilder_PrefabContainer_TypesEnum.INSTANCE, prefabs, 0, rnd.Enum<MyMwcObjectBuilder_FactionEnum>(), null);
                    container.PositionAndOrientation = new MyMwcPositionAndOrientation(e.PositionInSector, Vector3.Forward, Vector3.Up);
                    addToList.Add(container);
                }
                else if (e.EntityType == MySolarSystemEntityEnum.DebrisField)
                {
                    MyMwcObjectBuilder_LargeDebrisField objectBuilder = new MyMwcObjectBuilder_LargeDebrisField(MyMwcObjectBuilder_LargeDebrisField_TypesEnum.Debris84);
                    objectBuilder.PositionAndOrientation = new MyMwcPositionAndOrientation(e.PositionInSector, rnd.Vector(1), rnd.Vector(1));
                    addToList.Add(objectBuilder);
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="existingEntities"></param>
        /// <param name="rnd"></param>
        /// <param name="radius"></param>
        /// <param name="positionMultiplier">How far from offset shoud be position generated</param>
        /// <param name="spacing"></param>
        /// <param name="offset"></param>
        /// <returns></returns>
        private Vector3? FindEntityPosition(MyDynamicAABBTree existingEntities, Random rnd, float radius, float positionMultiplier = 0.8f, float spacing = 1.0f, Vector3 offset = new Vector3())
        {
            bool collide = true;
            Vector3 pos = new Vector3();
            int testCount = 0;

            //collide = false;
            Vector3 halfSize = MyMwcSectorConstants.SECTOR_SIZE / 2.0f * new Vector3(positionMultiplier);
            halfSize -= new Vector3(radius);

            while (collide && testCount < MaxCollisionsTestsForEntity)
            {
                pos = offset + rnd.Vector(halfSize);
                testCount++;

                collide = false;
                // try detect collisions with safe areas
              //  collide = IsEntityCollideWithSafeAreas(pos, radius);
                //if (collide)
                  //  continue;

                /*
                foreach (var e in existingEntities)
                {
                    if ((e.PositionInSector - pos).Length() < (e.Radius + radius) * spacing)
                    {
                        collide = true;
                        break;
                    }
                } */

                BoundingBox bb = new BoundingBox(pos - new Vector3(radius) * spacing, pos + new Vector3(radius) * spacing);
                existingEntities.OverlapAllBoundingBox(ref bb, m_elements);
                if (m_elements.Count > 0)
                {
                    collide = true;
                    continue;
                }
            }
            return !collide ? (Vector3?)pos : null;
        }
        public static void AddVeins(MyWeightDictionary<MyMwcVoxelMaterialsEnum> secondaryMaterials, Random rnd, Vector3 positionInSector, int veinCount, int voxelAsteroidSize, MyMwcObjectBuilder_VoxelMap builder, float veinAngleDev, int maxLevel, float baseThickness)
        {
            MyMwcVoxelMaterialsEnum material = MyMwcVoxelMaterialsEnum.Magnesium_01;

            if (secondaryMaterials.Count > 0)
            {
                material = secondaryMaterials.GetRandomItem(rnd);
            }

            for (int i = 0; i < veinCount; i++)
            {
                Vector3 position = positionInSector + new Vector3(voxelAsteroidSize / 2);
                position += rnd.Vector(voxelAsteroidSize / 3);
                AddVein(builder, baseThickness, position, rnd, material, veinAngleDev, maxLevel);
            }
        }