Exemplo n.º 1
0
 public IChunk GenerateChunk(IWorld world, Coordinates2D position)
 {
     var chunk = new Chunk(position);
     int y = 0;
     for (int i = 0; i < Layers.Count; i++)
     {
         int height = y + Layers[i].Height;
         while (y < height)
         {
             for (int x = 0; x < 16; x++)
             {
                 for (int z = 0; z < 16; z++)
                 {
                     chunk.SetBlockID(new Coordinates3D(x, y, z), Layers[i].BlockId);
                     chunk.SetMetadata(new Coordinates3D(x, y, z), Layers[i].Metadata);
                 }
             }
             y++;
         }
     }
     for (int i = 0; i < chunk.Biomes.Length; i++)
         chunk.Biomes[i] = (byte)Biome;
     chunk.TerrainPopulated = true;
     chunk.UpdateHeightMap();
     return chunk;
 }
Exemplo n.º 2
0
        public void SetChunk(Coordinates2D coordinates, Chunk chunk)
        {
            int regionX = coordinates.X / Region.Width - ((coordinates.X < 0) ? 1 : 0);
            int regionZ = coordinates.Z / Region.Depth - ((coordinates.Z < 0) ? 1 : 0);

            var region = LoadOrGenerateRegion(new Coordinates2D(regionX, regionZ));
            lock (region)
            {
                chunk.IsModified = true;
                region.SetChunk(new Coordinates2D(coordinates.X - regionX * 32, coordinates.Z - regionZ * 32), chunk);
            }
        }
Exemplo n.º 3
0
        public IChunk GenerateChunk(IWorld world, Coordinates2D coordinates)
        {
            const int featurePointDistance = 400;

            // TODO: Create a terrain generator initializer function that gets passed the seed etc
            int seed = world.Seed;
            var worley = new CellNoise(seed);
            HighNoise.Seed = seed;
            LowNoise.Seed = seed;
            CaveNoise.Seed = seed;

            var chunk = new Chunk(coordinates);

            for (int x = 0; x < 16; x++)
            {
                for (int z = 0; z < 16; z++)
                {
                    var blockX = MathHelper.ChunkToBlockX(x, coordinates.X);
                    var blockZ = MathHelper.ChunkToBlockZ(z, coordinates.Z);

                    const double lowClampRange = 5;
                    double lowClampMid = LowClamp.MaxValue - ((LowClamp.MaxValue + LowClamp.MinValue) / 2);
                    double lowClampValue = LowClamp.Value2D(blockX, blockZ);

                    if (lowClampValue > lowClampMid - lowClampRange && lowClampValue < lowClampMid + lowClampRange)
                    {
                        InvertNoise NewPrimary = new InvertNoise(HighClamp);
                        FinalNoise.PrimaryNoise = NewPrimary;
                    }
                    else
                    {
                        //reset it after modifying the values
                        FinalNoise = new ModifyNoise(HighClamp, LowClamp, NoiseModifier.Add);
                    }
                    FinalNoise = new ModifyNoise(FinalNoise, BottomClamp, NoiseModifier.Subtract);

                    var cellValue = worley.Value2D(blockX, blockZ);
                    var location = new Coordinates2D(blockX, blockZ);
                    if (world.BiomeDiagram.BiomeCells.Count < 1
                        || cellValue.Equals(1)
                        && world.BiomeDiagram.ClosestCellPoint(location) >= featurePointDistance)
                    {
                        byte id = (SingleBiome) ? GenerationBiome : world.BiomeDiagram.GenerateBiome(seed, Biomes, location);
                        var cell = new BiomeCell(id, location);
                        world.BiomeDiagram.AddCell(cell);
                    }

                    var biomeId = GetBiome(world, location);
                    var biome = Biomes.GetBiome(biomeId);
                    chunk.Biomes[x * Chunk.Width + z] = biomeId;

                    var height = GetHeight(blockX, blockZ);
                    var surfaceHeight = height - biome.SurfaceDepth;
                    chunk.HeightMap[x * Chunk.Width + z] = height;

                    // TODO: Do not overwrite blocks if they are already set from adjacent chunks
                    for (int y = 0; y <= height; y++)
                    {
                        double cave = 0;
                        if (!EnableCaves)
                            cave = double.MaxValue;
                        else
                            cave = CaveNoise.Value3D((blockX + x) / 2, y / 2, (blockZ + z) / 2);
                        double threshold = 0.05;
                        if (y < 4)
                            threshold = double.MaxValue;
                        else
                        {
                            if (y > height - 8)
                                threshold = 8;
                        }
                        if (cave < threshold)
                        {
                            if (y == 0)
                                chunk.SetBlockID(new Coordinates3D(x, y, z), BedrockBlock.BlockID);
                            else
                            {
                                if (y.Equals(height) || y < height && y > surfaceHeight)
                                    chunk.SetBlockID(new Coordinates3D(x, y, z), biome.SurfaceBlock);
                                else
                                {
                                    if (y > surfaceHeight - biome.FillerDepth)
                                        chunk.SetBlockID(new Coordinates3D(x, y, z), biome.FillerBlock);
                                    else
                                        chunk.SetBlockID(new Coordinates3D(x, y, z), StoneBlock.BlockID);
                                }
                            }
                        }
                    }
                }
            }
            foreach (var decorator in ChunkDecorators)
                decorator.Decorate(world, chunk, Biomes);
            chunk.TerrainPopulated = true;
            return chunk;
        }
Exemplo n.º 4
0
 internal void SetChunk(Coordinates2D coordinates, Chunk chunk)
 {
     World.SetChunk(coordinates, chunk);
 }
Exemplo n.º 5
0
        public void Deserialize(NbtTag value)
        {
            var chunk = new Chunk();
            var tag = (NbtCompound)value;

            Biomes = chunk.Biomes;
            HeightMap = chunk.HeightMap;
            LastUpdate = chunk.LastUpdate;
            TerrainPopulated = chunk.TerrainPopulated;
            X = tag["X"].IntValue;
            Z = tag["Z"].IntValue;
            Blocks = tag["Blocks"].ByteArrayValue;
            Metadata = new NibbleArray();
            Metadata.Data = tag["Data"].ByteArrayValue;
            BlockLight = new NibbleArray();
            BlockLight.Data = tag["BlockLight"].ByteArrayValue;
            SkyLight = new NibbleArray();
            SkyLight.Data = tag["SkyLight"].ByteArrayValue;

            if (tag.Contains("TileEntities"))
            {
                foreach (var entity in tag["TileEntities"] as NbtList)
                {
                    TileEntities[new Coordinates3D(entity["coordinates"][0].IntValue,
                        entity["coordinates"][1].IntValue,
                        entity["coordinates"][2].IntValue)] = entity["value"][0] as NbtCompound;
                }
            }

            // TODO: Entities
        }
Exemplo n.º 6
0
 public void SetUp()
 {
     var file = new NbtFile("Files/TestChunk.nbt");
     Chunk = Chunk.FromNbt(file);
 }
Exemplo n.º 7
0
 public void SetUp()
 {
     var assemblyDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
     var file = new NbtFile(Path.Combine(assemblyDir, "Files", "TestChunk.nbt"));
     Chunk = Chunk.FromNbt(file);
 }