Ejemplo n.º 1
0
 private void ExpandSearchCheckBlock(int x, int y, int z, UniqueQueue <Position> generateQueue)
 {
     if (worldInstance.IsLoadedBlockLocation(x, y, z) && worldInstance.GetBlock(x, y, z).Type == BlockType.Unknown)
     {
         generateQueue.Enqueue(new Position(x, y, z));
     }
 }
Ejemplo n.º 2
0
        public void GenerateSurroundingCells(int x, int y, int z)
        {
            var generateQueue = new UniqueQueue <Position>();

            generateQueue.Enqueue(new Position(x + 1, y, z));
            generateQueue.Enqueue(new Position(x - 1, y, z));
            generateQueue.Enqueue(new Position(x, y + 1, z));
            generateQueue.Enqueue(new Position(x, y - 1, z));
            generateQueue.Enqueue(new Position(x, y, z + 1));
            generateQueue.Enqueue(new Position(x, y, z - 1));
            GenerateChunkCells(generateQueue);
        }
Ejemplo n.º 3
0
        private Array <byte> DefineTerrain(Array <byte> globalMap)
        {
            var terrain = new Array <byte>(globalMap.Size);

            for (int z = globalMap.Size.minZ; z < globalMap.Size.maxZ; z += globalMap.Size.scale)
            {
                for (int x = globalMap.Size.minX; x < globalMap.Size.maxX; x += globalMap.Size.scale)
                {
                    terrain[x, z] = GRASS;
                }
            }
            UniqueQueue <ChunkCoords> checkChunks = new UniqueQueue <ChunkCoords>();

            for (int z = globalMap.Size.minZ; z < globalMap.Size.maxZ; z += globalMap.Size.scale)
            {
                if (globalMap[globalMap.Size.minX, z] < Settings.waterLevel)
                {
                    checkChunks.Enqueue(new ChunkCoords(globalMap.Size.minX, z));
                }
                if (globalMap[globalMap.Size.maxX - 1, z] < Settings.waterLevel)
                {
                    checkChunks.Enqueue(new ChunkCoords(globalMap.Size.maxX - 1, z));
                }
            }
            for (int x = globalMap.Size.minX; x < globalMap.Size.maxX; x += globalMap.Size.scale)
            {
                if (globalMap[x, globalMap.Size.minZ] < Settings.waterLevel)
                {
                    checkChunks.Enqueue(new ChunkCoords(x, globalMap.Size.minZ));
                }
                if (globalMap[x, globalMap.Size.maxZ - 1] < Settings.waterLevel)
                {
                    checkChunks.Enqueue(new ChunkCoords(x, globalMap.Size.maxZ - 1));
                }
            }
            while (checkChunks.Count > 0)
            {
                var pos = checkChunks.Dequeue();
                if (globalMap.IsValidCoord(pos.X, pos.Z) && terrain[pos.X, pos.Z] != WATER && globalMap[pos.X, pos.Z] < Settings.waterLevel)
                {
                    terrain[pos.X, pos.Z] = WATER;
                    checkChunks.Enqueue(new ChunkCoords(pos.X + globalMap.Size.scale, pos.Z));
                    checkChunks.Enqueue(new ChunkCoords(pos.X - globalMap.Size.scale, pos.Z));
                    checkChunks.Enqueue(new ChunkCoords(pos.X, pos.Z + globalMap.Size.scale));
                    checkChunks.Enqueue(new ChunkCoords(pos.X, pos.Z - globalMap.Size.scale));
                }
            }

            return(terrain);
        }
Ejemplo n.º 4
0
        private void GenerateChunkCells(UniqueQueue <Position> generateQueue)
        {
            while (generateQueue.Count > 0)
            {
                var pos = generateQueue.Dequeue();
                if (pos == null)
                {
                    Log.WriteError("Null position in queue?");
                    continue;
                }
                var x = pos.X;
                var y = pos.Y;
                var z = pos.Z;

                if (worldInstance.GetBlock(x, y, z).Type == BlockType.Unknown)
                {
                    var block = GenerateCell(x, y, z);
                    if (block.Type == BlockType.Unknown)
                    {
                        Log.WriteError("Unknown block type generated?");
                    }
                    worldInstance.SetBlock(x, y, z, block);
                    if (worldInstance.GetBlock(x, y, z).Type == BlockType.Unknown)
                    {
                        Log.WriteError("Block not set?");
                    }
                    if (block.IsTransparent)
                    {
                        ExpandSearchCheckBlock(x - 1, y, z, generateQueue);
                        ExpandSearchCheckBlock(x + 1, y, z, generateQueue);
                        if (y - 1 >= Settings.minNoiseHeight)
                        {
                            ExpandSearchCheckBlock(x, y - 1, z, generateQueue);
                        }
                        if (y + 1 < Settings.maxNoiseHeight)
                        {
                            ExpandSearchCheckBlock(x, y + 1, z, generateQueue);
                        }
                        ExpandSearchCheckBlock(x, y, z - 1, generateQueue);
                        ExpandSearchCheckBlock(x, y, z + 1, generateQueue);
                    }
                }
            }
        }
Ejemplo n.º 5
0
        public void Generate(Chunk chunk)
        {
            Log.WriteInfo("Generating new chunk: " + chunk.ChunkCoords);
            chunk.FinishedGeneration = false;
            var worldSize = new ArraySize()
            {
                minZ  = chunk.ChunkCoords.WorldCoordsZ,
                maxZ  = chunk.ChunkCoords.WorldCoordsZ + Global.CHUNK_SIZE,
                minX  = chunk.ChunkCoords.WorldCoordsX,
                maxX  = chunk.ChunkCoords.WorldCoordsX + Global.CHUNK_SIZE,
                minY  = Settings.minNoiseHeight,
                maxY  = Settings.maxNoiseHeight,
                scale = 1,
            };

            var generateQueue = new UniqueQueue <Position>();

            generateQueue.Enqueue(new Position(
                                      chunk.ChunkCoords.WorldCoordsX + Global.CHUNK_SIZE / 2,
                                      Settings.maxNoiseHeight,
                                      chunk.ChunkCoords.WorldCoordsZ + Global.CHUNK_SIZE / 2));

            if (worldInstance.IsChunkLoaded(new ChunkCoords(chunk.ChunkCoords.X - 1, chunk.ChunkCoords.Z)))
            {
                for (var z = chunk.MinPosition.Z; z <= chunk.MaxPosition.Z; z++)
                {
                    for (var y = chunk.MinPosition.Y; y < chunk.MaxPosition.Y; y++)
                    {
                        var block = worldInstance.GetBlock(chunk.MinPosition.X - 1, y, z);
                        if (block.IsTransparent)
                        {
                            generateQueue.Enqueue(new Position(chunk.MinPosition.X, y, z));
                        }
                    }
                }
            }
            if (worldInstance.IsChunkLoaded(new ChunkCoords(chunk.ChunkCoords.X + 1, chunk.ChunkCoords.Z)))
            {
                for (var z = chunk.MinPosition.Z; z <= chunk.MaxPosition.Z; z++)
                {
                    for (var y = chunk.MinPosition.Y; y < chunk.MaxPosition.Y; y++)
                    {
                        var block = worldInstance.GetBlock(chunk.MaxPosition.X + 1, y, z);
                        if (block.IsTransparent)
                        {
                            generateQueue.Enqueue(new Position(chunk.MaxPosition.X, y, z));
                        }
                    }
                }
            }
            if (worldInstance.IsChunkLoaded(new ChunkCoords(chunk.ChunkCoords.X, chunk.ChunkCoords.Z - 1)))
            {
                for (var x = chunk.MinPosition.X; x <= chunk.MaxPosition.X; x++)
                {
                    for (var y = chunk.MinPosition.Y; y < chunk.MaxPosition.Y; y++)
                    {
                        var block = worldInstance.GetBlock(x, y, chunk.MinPosition.Z - 1);
                        if (block.IsTransparent)
                        {
                            generateQueue.Enqueue(new Position(x, y, chunk.MinPosition.Z));
                        }
                    }
                }
            }
            if (worldInstance.IsChunkLoaded(new ChunkCoords(chunk.ChunkCoords.X, chunk.ChunkCoords.Z + 1)))
            {
                for (var x = chunk.MinPosition.X; x <= chunk.MaxPosition.X; x++)
                {
                    for (var y = chunk.MinPosition.Y; y < chunk.MaxPosition.Y; y++)
                    {
                        var block = worldInstance.GetBlock(x, y, chunk.MaxPosition.Z + 1);
                        if (block.IsTransparent)
                        {
                            generateQueue.Enqueue(new Position(x, y, chunk.MaxPosition.Z));
                        }
                    }
                }
            }

            GenerateChunkCells(generateQueue);

            chunk.BuildHeightMap();

            TreeGenerator.Generate(worldInstance, chunk);
            chunk.FinishedGeneration = true;
        }