Exemplo n.º 1
0
        public void GenerateChunkResources(ByteChunkCursor cursor, FastRandom rnd)
        {
            //Create the various Veins in the chunk
            foreach (CubeVein vein in CubeVeins)
            {
                //Generate the vein X time
                for (int i = 0; i < vein.VeinPerChunk; i++)
                {
                    if (vein.Cube != UtopiaProcessorParams.CubeId.LavaFlow &&
                        vein.Cube != UtopiaProcessorParams.CubeId.WaterFlow)
                    {
                        //Get Rnd chunk Location.
                        int x = rnd.Next(0, 16);
                        int y = rnd.Next(vein.SpawningHeight.Min, vein.SpawningHeight.Max);
                        int z = rnd.Next(0, 16);

                        PopulateChunkWithResource(vein.Cube, cursor, x, y, z, vein.VeinSize, rnd);
                    }
                    else
                    {
                        if (vein.Cube == UtopiaProcessorParams.CubeId.LavaFlow)
                        {
                            //Get Rnd chunk Location.
                            int x = rnd.Next(vein.VeinSize, 16 - vein.VeinSize);
                            int y = rnd.Next(vein.SpawningHeight.Min, vein.SpawningHeight.Max);
                            int z = rnd.Next(vein.VeinSize, 16 - vein.VeinSize);

                            PopulateChunkWithLiquidSources(vein.Cube, cursor, x, y, z, vein.VeinSize);
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void PopulateChunkWithCave(ByteChunkCursor cursor, int x, int y, int z, int layers, byte cubeId, FastRandom rnd)
        {
            cursor.SetInternalPosition(x, y, z);

            int caveRadius = rnd.Next(3, 8);

            int layerRadiusModifier = 0;

            for (int l = 0; l < layers; l++)
            {
                //Generate Lake Layers
                for (int X = x - (caveRadius - layerRadiusModifier); X <= x + (caveRadius - layerRadiusModifier); X++)
                {
                    for (int Z = z - (caveRadius - layerRadiusModifier); Z <= z + (caveRadius - layerRadiusModifier); Z++)
                    {
                        //Create "Noise" at Cave border
                        if ((X == x - (caveRadius - layerRadiusModifier) ||
                             X == x + (caveRadius - layerRadiusModifier) ||
                             Z == z - (caveRadius - layerRadiusModifier) ||
                             Z == z + (caveRadius - layerRadiusModifier)) &&
                            rnd.NextDouble() < 0.2)
                        {
                            continue;
                        }

                        cursor.SetInternalPosition(X, y + l, Z);
                        if (l <= 1 && rnd.NextDouble() < 0.3)
                        {
                            cursor.Write(cubeId);
                        }
                        else
                        {
                            if (l != 0)
                            {
                                if (l == layers - 1)
                                {
                                    if (cursor.Read() == UtopiaProcessorParams.CubeId.Stone)
                                    {
                                        cursor.Write(UtopiaProcessorParams.CubeId.LightWhite);
                                    }
                                }
                                else
                                {
                                    cursor.Write(UtopiaProcessorParams.CubeId.Air);
                                }
                            }
                        }
                    }
                }
                if (layerRadiusModifier < caveRadius)
                {
                    layerRadiusModifier++;
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Will Populate the chunks with various resources
        /// </summary>
        /// <param name="ChunkCubes"></param>
        /// <param name="chunkMetaData"></param>
        private void PopulateChunk(GeneratedChunk chunk, byte[] chunkData, ChunkMetaData chunkMetaData, FastRandom chunkRnd, EntityFactory entityFactory, List <LandscapeEntity> landscapeEntities)
        {
            //Get Chunk Master Biome
            var             masterBiome = _config.ProcessorParam.Biomes[chunkMetaData.ChunkMasterBiomeType];
            ByteChunkCursor dataCursor  = new ByteChunkCursor(chunkData, chunk.BlockData.ColumnsInfo);

            masterBiome.GenerateChunkCaverns(dataCursor, chunkRnd);
            masterBiome.GenerateChunkResources(dataCursor, chunkRnd);
            chunkMetaData.InitialSpawnableEntitiesAmount = masterBiome.GenerateChunkStaticItems(dataCursor, chunk, masterBiome, chunkRnd, entityFactory, _spawnControler);
            InsertMicrolandscapeStaticEntities(dataCursor, chunk, chunkRnd, entityFactory, landscapeEntities);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Will create a single Liquid "Source"
        /// </summary>
        /// <param name="cubeId">the liquid CubeId</param>
        /// <param name="cursor">Class helper to move inside the chunk</param>
        /// <param name="x">InsideChunk X starting position</param>
        /// <param name="y">InsideChunk Y starting position</param>
        /// <param name="z">InsideChunk Z starting position</param>
        private void PopulateChunkWithLiquidSources(byte cubeId, ByteChunkCursor cursor, int x, int y, int z, int liquidPower)
        {
            cursor.SetInternalPosition(x, y, z);

            //Check if this source is candidate as valid source = Must be surrended by 5 solid blocks and ahave one side block going to Air
            if (cursor.Read() != UtopiaProcessorParams.CubeId.Air)
            {
                //Looking Up for Air
                if (_config.BlockProfiles[cursor.Peek(CursorRelativeMovement.Up)].IsBlockingWater == false || cursor.Peek(CursorRelativeMovement.Up) == UtopiaProcessorParams.CubeId.Snow)
                {
                    return;
                }
                if (_config.BlockProfiles[cursor.Peek(CursorRelativeMovement.Down)].IsBlockingWater == false)
                {
                    return;
                }
                int cpt = 0;
                //Counting the number of holes arround the source
                if (_config.BlockProfiles[cursor.Peek(CursorRelativeMovement.East)].IsBlockingWater == false)
                {
                    cpt++;
                }
                if (_config.BlockProfiles[cursor.Peek(CursorRelativeMovement.West)].IsBlockingWater == false)
                {
                    cpt++;
                }
                if (_config.BlockProfiles[cursor.Peek(CursorRelativeMovement.North)].IsBlockingWater == false)
                {
                    cpt++;
                }
                if (_config.BlockProfiles[cursor.Peek(CursorRelativeMovement.South)].IsBlockingWater == false)
                {
                    cpt++;
                }

                //Only one face touching air ==> Createing the Liquid Source !
                if (cpt != 1)
                {
                    return;
                }

                cursor.Write(cubeId);
                Queue <Tuple <ByteChunkCursor, int> > sourcesWithPower = new Queue <Tuple <ByteChunkCursor, int> >();
                sourcesWithPower.Enqueue(new Tuple <ByteChunkCursor, int>(cursor, liquidPower));
                PropagateLiquidSources(sourcesWithPower, cubeId);
            }
        }
Exemplo n.º 5
0
        private void PopulateChunkWithItems(ByteChunkCursor cursor, GeneratedChunk chunk, ref Vector3D chunkWorldPosition, ushort bluePrintId, int x, int y, int z, FastRandom rnd, EntityFactory entityFactory, bool isBlockCentered = true)
        {
            cursor.SetInternalPosition(x, y, z);

            //Check that the block above is "Air"
            if (cursor.Peek(CursorRelativeMovement.Up) != UtopiaProcessorParams.CubeId.Air)
            {
                return;
            }
            //Check that the block below is "solid"
            byte         blockBelow        = cursor.Read();
            BlockProfile blockBelowProfile = _config.BlockProfiles[blockBelow];

            if (blockBelowProfile.IsSolidToEntity)
            {
                //Cloning the Entity Blue Print !
                var entity = entityFactory.CreateFromBluePrint(bluePrintId);

                if (entity is IBlockLinkedEntity)
                {
                    Vector3I linkedCubePosition = new Vector3I(chunkWorldPosition.X + x, y, chunkWorldPosition.Z + z);
                    ((IBlockLinkedEntity)entity).LinkedCube = linkedCubePosition;
                }

                if (entity is BlockLinkedItem)
                {
                    Vector3I LocationCube = new Vector3I(chunkWorldPosition.X + x, y + 1, chunkWorldPosition.Z + z);
                    ((BlockLinkedItem)entity).BlockLocationRoot = LocationCube;
                }

                double XOffset = 0.5;
                double ZOffset = 0.5;
                if (isBlockCentered == false)
                {
                    XOffset = rnd.NextDouble(0.2, 0.8);
                    ZOffset = rnd.NextDouble(0.2, 0.8);
                }

                entity.Position = new Vector3D(chunkWorldPosition.X + x + XOffset, y + 1, chunkWorldPosition.Z + z + ZOffset);

                chunk.Entities.Add((StaticEntity)entity);
            }
        }
Exemplo n.º 6
0
        //Will send back a dictionnary with the entity amount that have been generated
        public Dictionary <ushort, int> GenerateChunkStaticItems(ByteChunkCursor cursor, GeneratedChunk chunk, Biome biome, FastRandom rnd, EntityFactory entityFactory, UtopiaEntitySpawningControler spawnControler)
        {
            Dictionary <ushort, int> entityAmount = new Dictionary <ushort, int>();

            foreach (ChunkSpawnableEntity entity in SpawnableEntities.Where(x => x.IsChunkGenerationSpawning))
            {
                for (int i = 0; i < entity.MaxEntityAmount; i++)
                {
                    Vector3D entityPosition;
                    if (spawnControler.TryGetSpawnLocation(entity, chunk, cursor, rnd, out entityPosition))
                    {
                        //Create the entity
                        var createdEntity      = entityFactory.CreateFromBluePrint(entity.BluePrintId);
                        var chunkWorldPosition = chunk.BlockPosition;

                        //Should take into account the SpawnLocation ! (Ceiling or not !)
                        if (createdEntity is IBlockLinkedEntity)
                        {
                            Vector3I linkedCubePosition = BlockHelper.EntityToBlock(entityPosition);
                            linkedCubePosition.Y--;
                            ((IBlockLinkedEntity)createdEntity).LinkedCube = linkedCubePosition;
                        }

                        if (createdEntity is BlockLinkedItem)
                        {
                            Vector3I LocationCube = BlockHelper.EntityToBlock(entityPosition);
                            ((BlockLinkedItem)createdEntity).BlockLocationRoot = LocationCube;
                        }

                        createdEntity.Position = entityPosition;

                        chunk.Entities.Add((StaticEntity)createdEntity);
                        if (!entityAmount.ContainsKey(createdEntity.BluePrintId))
                        {
                            entityAmount[createdEntity.BluePrintId] = 0;
                        }
                        entityAmount[createdEntity.BluePrintId]++;
                    }
                }
            }

            return(entityAmount);
        }
Exemplo n.º 7
0
 public void GenerateChunkCaverns(ByteChunkCursor cursor, FastRandom rnd)
 {
     //Create the various Cavern in the chunk
     foreach (Cavern cavern in Caverns)
     {
         //Nbr of cavern to generate
         for (int i = 0; i < cavern.CavernPerChunk; i++)
         {
             if (rnd.NextDouble() <= cavern.ChanceOfSpawning)
             {
                 //Get Rnd chunk Location.
                 int x     = rnd.Next(7, 9);
                 int y     = rnd.Next(cavern.SpawningHeight.Min, cavern.SpawningHeight.Max);
                 int z     = rnd.Next(7, 9);
                 int layer = rnd.Next(cavern.CavernHeightSize.Min, cavern.CavernHeightSize.Max + 1);
                 PopulateChunkWithCave(cursor, x, y, z, layer, cavern.Cube, rnd);
             }
         }
     }
 }
Exemplo n.º 8
0
        /// <summary>
        /// Will create a resource vein
        /// </summary>
        /// <param name="cubeId">The resource to be created</param>
        /// <param name="cursor">Class helper to move inside the Chunk cube data</param>
        /// <param name="x">InsideChunk X starting position</param>
        /// <param name="y">InsideChunk Y starting position</param>
        /// <param name="z">InsideChunk Z starting position</param>
        /// <param name="qt">Vein size</param>
        /// <param name="rnd">Random generator for vein creation</param>
        private void PopulateChunkWithResource(byte cubeId, ByteChunkCursor cursor, int x, int y, int z, int qt, FastRandom rnd)
        {
            cursor.SetInternalPosition(x, y, z);
            int nbrCubePlaced;

            if (cursor.Read() == UtopiaProcessorParams.CubeId.Stone)
            {
                cursor.Write(cubeId);
                nbrCubePlaced = 1;
                for (int i = 0; i < qt + 10 && nbrCubePlaced < qt; i++)
                {
                    int relativeMove = rnd.Next(1, 7);
                    cursor.Move(relativeMove);
                    if (cursor.Read() == UtopiaProcessorParams.CubeId.Stone)
                    {
                        cursor.Write(cubeId);
                        nbrCubePlaced++;
                    }
                }
            }
        }
        private bool SurfaceSpawnLogic(int x, int z, ChunkSpawnableEntity entity, AbstractChunk chunk, ByteChunkCursor cursor, FastRandom rnd, out Vector3D entitySpawnLocation)
        {
            entitySpawnLocation = default(Vector3D);

            int y;
            int columnInfoIndex = x * AbstractChunk.ChunkSize.Z + z;

            //Y base = Original world generated ground height (Before any player modification)
            y = chunk.BlockData.ColumnsInfo[columnInfoIndex].MaxGroundHeight;
            cursor.SetInternalPosition(x, y, z);

            // verify that we can spawn here
            var canSpawn = cursor.Read() != WorldConfiguration.CubeId.Air && cursor.Move(CursorRelativeMovement.Up) &&
                           cursor.Read() == WorldConfiguration.CubeId.Air && cursor.Move(CursorRelativeMovement.Up) &&
                           cursor.Read() == WorldConfiguration.CubeId.Air;

            // return cursor to the spawn point
            cursor.Move(CursorRelativeMovement.Down);

            if (!canSpawn)
            {
                return(false);
            }

            // Check that the block is well "Solid to entity"
            BlockProfile blockSpawnProfile = _config.BlockProfiles[cursor.Peek(CursorRelativeMovement.Down)];

            if (!blockSpawnProfile.IsSolidToEntity)
            {
                return(false);
            }
            if (entity.IsWildChunkNeeded)
            {
                //Get Chunk master biome
                byte masterBiomeId = chunk.BlockData.ChunkMetaData.ChunkMasterBiomeType;
                //Get biome surface block layer
                byte surfaceBiomeCube = _config.ProcessorParam.Biomes[masterBiomeId].SurfaceCube;
                //If the entity need a Wild chunk, then it can only spawn on a cube surface equal to the default biome surface cube !
                if (surfaceBiomeCube != blockSpawnProfile.Id)
                {
                    return(false);
                }
            }

            // Hurray it can spawn ! :D
            //Add some randomnes on the cube where it will spawn
            double XOffset, ZOffset;

            XOffset = rnd.NextDouble(0.2, 0.8);
            ZOffset = rnd.NextDouble(0.2, 0.8);

            entitySpawnLocation = new Vector3D(chunk.BlockPosition.X + x + XOffset, cursor.InternalPosition.Y, chunk.BlockPosition.Z + z + ZOffset);

            return(true);
        }
        /// <summary>
        /// Function that will try to get a place where the entity can spawn, it will apply spawning restrictions.
        /// </summary>
        /// <param name="entity">The entity</param>
        /// <param name="chunk">The chunk where the entity must be placed</param>
        /// <param name="cursor">A cursor on the chunk block data</param>
        /// <param name="rnd">Randomnes generator</param>
        /// <param name="entitySpawnLocation">Returned spawn location, will be [0;0;0] if entity cannot be placed</param>
        /// <returns>False if the entity cannot be placed</returns>
        public bool TryGetSpawnLocation(ChunkSpawnableEntity entity, AbstractChunk chunk, ByteChunkCursor cursor, FastRandom rnd, out Vector3D entitySpawnLocation)
        {
            entitySpawnLocation = default(Vector3D);

            //Test spawning chance
            if (rnd.NextDouble() <= entity.SpawningChance)
            {
                return(false);
            }
            //Get Rnd chunk Location X/Z
            int x = rnd.Next(0, 16);
            int z = rnd.Next(0, 16);
            //Column Info index
            int columnInfoIndex = x * AbstractChunk.ChunkSize.Z + z;

            int y;

            switch (entity.SpawningPlace)
            {
            case ChunkSpawningPlace.FloorInsideCave:
            case ChunkSpawningPlace.CeilingInsideCave:
                return(CaveSpawnLogic(x, z, entity, chunk, cursor, rnd, out entitySpawnLocation));

            case ChunkSpawningPlace.AirAboveSurface:
                return(AirSpawnLogic(x, z, entity, chunk, cursor, rnd, out entitySpawnLocation));

            case ChunkSpawningPlace.Surface:
            default:
                return(SurfaceSpawnLogic(x, z, entity, chunk, cursor, rnd, out entitySpawnLocation));
            }
        }
        private bool AirSpawnLogic(int x, int z, ChunkSpawnableEntity entity, AbstractChunk chunk, ByteChunkCursor cursor, FastRandom rnd, out Vector3D entitySpawnLocation)
        {
            entitySpawnLocation = default(Vector3D);
            int y;
            int columnInfoIndex = x * AbstractChunk.ChunkSize.Z + z;

            //Y base = Original world generated ground height (Before any player modification)
            y = rnd.Next(chunk.BlockData.ColumnsInfo[columnInfoIndex].MaxHeight + 5, AbstractChunk.ChunkSize.Y);

            if (y <= 0 || y >= AbstractChunk.ChunkSize.Y)
            {
                return(false);
            }

            cursor.SetInternalPosition(x, y, z);

            if (cursor.Read() != WorldConfiguration.CubeId.Air)
            {
                return(false);
            }

            // Hurray it can spawn ! :D
            //Add some randomnes on the cube where it will spawn
            double XOffset, ZOffset;

            XOffset = rnd.NextDouble(0.2, 0.8);
            ZOffset = rnd.NextDouble(0.2, 0.8);

            entitySpawnLocation = new Vector3D(chunk.BlockPosition.X + x + XOffset, cursor.InternalPosition.Y, chunk.BlockPosition.Z + z + ZOffset);
            return(true);
        }
        private bool CaveSpawnLogic(int x, int z, ChunkSpawnableEntity entity, AbstractChunk chunk, ByteChunkCursor cursor, FastRandom rnd, out Vector3D entitySpawnLocation)
        {
            entitySpawnLocation = default(Vector3D);
            int y;
            int columnInfoIndex = x * AbstractChunk.ChunkSize.Z + z;

            cursor.SetInternalPosition(x, 1, z);
            //Move up until Air Block
            while (cursor.Read() != WorldConfiguration.CubeId.Air)
            {
                //Move up, if top chunk height exit
                if (cursor.Move(CursorRelativeMovement.Up) == false)
                {
                    return(false);
                }
            }

            int YFloorSpawning = cursor.InternalPosition.Y;

            int MaximumSpawningHeight = chunk.BlockData.ColumnsInfo[columnInfoIndex].MaxGroundHeight - 10;

            if (MaximumSpawningHeight <= 0)
            {
                MaximumSpawningHeight = 1;
            }
            //Move up until "solid" Block
            while (cursor.Read() == WorldConfiguration.CubeId.Air && cursor.InternalPosition.Y <= MaximumSpawningHeight)
            {
                //Move up, if top chunk height exit
                if (cursor.Move(CursorRelativeMovement.Up) == false)
                {
                    return(false);
                }
            }

            if (cursor.InternalPosition.Y > MaximumSpawningHeight)
            {
                return(false);
            }

            // Hurray it can spawn ! :D
            //Add some randomnes on the cube where it will spawn
            double XOffset, ZOffset;

            XOffset = rnd.NextDouble(0.2, 0.8);
            ZOffset = rnd.NextDouble(0.2, 0.8);

            if (entity.SpawningPlace == ChunkSpawningPlace.FloorInsideCave)
            {
                entitySpawnLocation = new Vector3D(chunk.BlockPosition.X + x + XOffset, YFloorSpawning, chunk.BlockPosition.Z + z + ZOffset);
            }
            else
            {
                entitySpawnLocation = new Vector3D(chunk.BlockPosition.X + x + XOffset, cursor.InternalPosition.Y - 1, chunk.BlockPosition.Z + z + ZOffset);
            }

            return(true);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Will insert associated static entities from a landscape entity.
        /// Ex :
        /// For the landscape entity "Tree", multiple static entities can be found "around" it : apple, stick, banana, ...
        /// </summary>
        /// <param name="dataCursor"></param>
        /// <param name="chunk"></param>
        /// <param name="chunkRnd"></param>
        /// <param name="entityFactory"></param>
        /// <param name="landscapeEntities"></param>
        private void InsertMicrolandscapeStaticEntities(ByteChunkCursor dataCursor, GeneratedChunk chunk, FastRandom chunkRnd, EntityFactory entityFactory, List <LandscapeEntity> landscapeEntities)
        {
            if (landscapeEntities == null)
            {
                return;
            }

            Vector2I chunkWorldPosition = new Vector2I(chunk.Position.X * AbstractChunk.ChunkSize.X, chunk.Position.Z * AbstractChunk.ChunkSize.Z);

            //The entities are sorted by their origine chunk hashcode value
            foreach (LandscapeEntity entity in landscapeEntities)
            {
                bool isLandscapeEntityRootInsideChunk = (entity.RootLocation.X >= 0 && entity.RootLocation.X < AbstractChunk.ChunkSize.X && entity.RootLocation.Z >= 0 && entity.RootLocation.Z < AbstractChunk.ChunkSize.Z);

                //Get LandscapeEntity
                var landscapeEntity = _worldParameters.Configuration.LandscapeEntitiesDico[entity.LandscapeEntityId];

                foreach (var staticEntity in landscapeEntity.StaticItems)
                {
                    //Get number of object
                    var nbr = chunkRnd.Next(staticEntity.Quantity.Min, staticEntity.Quantity.Max);
                    if (isLandscapeEntityRootInsideChunk)
                    {
                        //This entity location is inside the correct chunk.

                        //Its a tree !
                        if (landscapeEntity is TreeBluePrint)
                        {
                            //Create tree soul and attach this entity to the chunk
                            var soul = entityFactory.CreateEntity <TreeSoul>();
                            soul.Position    = new Vector3D(chunkWorldPosition.X + entity.RootLocation.X + 0.5, entity.RootLocation.Y, chunkWorldPosition.Y + entity.RootLocation.Z + 0.5);
                            soul.TreeRndSeed = entity.GenerationSeed;
                            soul.TreeTypeId  = entity.LandscapeEntityId;
                            chunk.Entities.Add(soul);
                        }
                    }
                    else
                    {
                        //If Root out of chunk, devide the qt of object to spawn by 2
                        nbr = (int)(nbr / 2.0);
                    }

                    //Foreach object to create
                    while (nbr > 0)
                    {
                        //find location of the static entity
                        double x = chunkRnd.NextDouble(-staticEntity.SpawningRange, staticEntity.SpawningRange) + entity.RootLocation.X;
                        double z = chunkRnd.NextDouble(-staticEntity.SpawningRange, staticEntity.SpawningRange) + entity.RootLocation.Z;

                        //If out of current chunk, don't create it
                        if (x < 0 || x >= AbstractChunk.ChunkSize.X || z < 0 || z >= AbstractChunk.ChunkSize.Z)
                        {
                            break;
                        }

                        bool groundSpawing;
                        if (staticEntity.SpawningType == SpawningType.Both)
                        {
                            groundSpawing = chunkRnd.NextFloat() > 0.5;
                        }
                        else
                        {
                            groundSpawing = staticEntity.SpawningType == SpawningType.Ground;
                        }

                        StaticEntity landscapeStaticEntity = null;
                        //Find Y spawning position
                        if (groundSpawing)
                        {
                            dataCursor.SetInternalPosition(MathHelper.Floor(x), entity.RootLocation.Y, MathHelper.Floor(z));
                            //Loop until I hit the ground, with a maximum of Y - 15 blocks
                            for (int y = entity.RootLocation.Y; y > entity.RootLocation.Y - 15 && y > 0; y--)
                            {
                                if (dataCursor.Read() != WorldConfiguration.CubeId.Air)
                                {
                                    //Add Static item here on the ground !
                                    landscapeStaticEntity          = (StaticEntity)entityFactory.CreateFromBluePrint(staticEntity.ItemblueprintId);
                                    landscapeStaticEntity.Position = new Vector3D(x + chunkWorldPosition.X, dataCursor.InternalPosition.Y + 1, z + chunkWorldPosition.Y);
                                    break;
                                }
                                dataCursor.Move(CursorRelativeMovement.Down);
                            }
                        }
                        else
                        {
                            dataCursor.SetInternalPosition(MathHelper.Floor(x), entity.RootLocation.Y + 1, MathHelper.Floor(z));
                            //Loop until I hit the ground, with a maximum of Y + 15 blocks
                            for (int y = entity.RootLocation.Y + 1; y <= entity.RootLocation.Y + 15 && y < AbstractChunk.ChunkSize.Y; y++)
                            {
                                if (dataCursor.Read() != WorldConfiguration.CubeId.Air)
                                {
                                    //Add Static item here on the ground !
                                    landscapeStaticEntity          = (StaticEntity)entityFactory.CreateFromBluePrint(staticEntity.ItemblueprintId);
                                    landscapeStaticEntity.Position = new Vector3D(x + chunkWorldPosition.X, dataCursor.InternalPosition.Y - 0.25, z + chunkWorldPosition.Y);
                                    break;
                                }
                                dataCursor.Move(CursorRelativeMovement.Up);
                            }
                        }

                        if (landscapeStaticEntity != null)
                        {
                            chunk.Entities.Add(landscapeStaticEntity);
                        }

                        nbr--;
                    }
                }
            }
        }
Exemplo n.º 14
0
        private void SpawnBiomeEntities(UtopiaTime gametime, ServerChunk chunk)
        {
            var chunkBiome = _configuration.ProcessorParam.Biomes[chunk.BlockData.ChunkMetaData.ChunkMasterBiomeType];

            foreach (var spawnableEntity in chunkBiome.SpawnableEntities)
            {
                bool isStaticEntity = _configuration.BluePrints[spawnableEntity.BluePrintId] is IStaticEntity;

                //Remark : isChunkGenerationSpawning is set to true for static entities, and false for dynamic entities, maybe worth renaming the properties
                //The aim of it is to avoid dynamic entity creation at chunk generation time (Pure chunk).
                //Apply creation constaint :
                //1) if static entity with is isWildChunkNeeded and chunk is not wild => Do nothing
                if (spawnableEntity.IsWildChunkNeeded && chunk.BlockData.ChunkMetaData.IsWild == false)
                {
                    continue;
                }

                if (chunk.PureGenerated && isStaticEntity)
                {
                    continue;
                }

                var isDayTime = gametime.TimeOfDay > UtopiaTimeSpan.FromHours(8) &&
                                gametime.TimeOfDay < UtopiaTimeSpan.FromHours(20);
                // check daytime constraints
                if (!spawnableEntity.SpawningDayTime.HasFlag(ChunkSpawningDayTime.Day) &&
                    isDayTime)
                {
                    continue;
                }

                if (!spawnableEntity.SpawningDayTime.HasFlag(ChunkSpawningDayTime.Night) &&
                    !isDayTime)
                {
                    continue;
                }

                // check season constraint
                var weatherService = _server.Services.GetService <WeatherService>();

                if (weatherService != null && weatherService.CurrentSeason != null &&
                    spawnableEntity.SpawningSeasons.Count > 0 &&
                    !spawnableEntity.SpawningSeasons.Contains(weatherService.CurrentSeason.Name))
                {
                    continue;
                }

                ByteChunkCursor chunkCursor = new ByteChunkCursor(chunk.BlockData.GetBlocksBytes(), chunk.BlockData.ColumnsInfo);
                // ==> Maybe worth to automaticaly create this specialize cursor at server chunk creation ? Multithreading problem ?
                //It is only use for reading chunk block data in fast way
                Vector3D entityLocation;
                if (_entitySpawningControler.TryGetSpawnLocation(spawnableEntity, chunk, chunkCursor, _fastRandom,
                                                                 out entityLocation))
                {
                    var entity       = _server.EntityFactory.CreateFromBluePrint(spawnableEntity.BluePrintId);
                    var staticEntity = entity as IStaticEntity;

                    if (staticEntity != null)
                    {
                        //Check the maximum amount of static entities;
                        int maxEntityAmount;
                        chunk.BlockData.ChunkMetaData.InitialSpawnableEntitiesAmount.TryGetValue(spawnableEntity.BluePrintId,
                                                                                                 out maxEntityAmount);
                        if (maxEntityAmount == 0)
                        {
                            continue;
                        }
                        if (chunk.Entities.Where(e => e.BluePrintId == spawnableEntity.BluePrintId)
                            .CountAtLeast(maxEntityAmount))
                        {
                            continue;
                        }

                        staticEntity.Position = entityLocation;

                        var cursor = _server.LandscapeManager.GetCursor(entityLocation);
                        logger.Debug("Spawning new static entity : {0} at location {1}", staticEntity.Name, entityLocation);

                        var blockLinkedItem = staticEntity as IBlockLinkedEntity;
                        if (blockLinkedItem != null)
                        {
                            blockLinkedItem.LinkedCube = (Vector3I)entityLocation + Vector3I.Down;
                        }

                        cursor.AddEntity(staticEntity);
                    }

                    var charEntity = entity as CharacterEntity;
                    if (charEntity != null)
                    {
                        if (DisableNPCSpawn)
                        {
                            continue;
                        }

                        var radius = Math.Max(8, spawnableEntity.DynamicEntitySpawnRadius);
                        if (
                            _server.AreaManager.EnumerateAround(entityLocation, radius)
                            .CountAtLeast(spawnableEntity.MaxEntityAmount))
                        {
                            continue;
                        }

                        charEntity.Position = entityLocation;
                        logger.Debug("Spawning new dynamic entity : {0} at location {1}", charEntity.Name, entityLocation);
                        _server.EntityManager.AddNpc(charEntity);
                    }
                }
            }
        }