public void AddToUnload(Chunk chunk)
        {
            if (!LoadQueue.Contains(chunk))
            {
                UnloadQueue.Enqueue(chunk);
            }

            LoadQueue.Remove(chunk);
            GenerationQueue.Remove(chunk);
            VegetationQueue.Remove(chunk);
        }
 public static void Vegetate(Chunk chunk)
 {
     for (int x = 0; x < Constants.World.ChunkSize; x++)
     {
         for (int z = 0; z < Constants.World.ChunkSize; z++)
         {
             for (int y = 0; y < Constants.World.ChunkSize; y++)
             {
                 if (chunk[x, y, z] == Block.Grass)
                 {
                     GrowTree(chunk, new BlockIndex(x, y, z), (int)(NoiseMaps.GetByValues(x * x * z + y * z, chunk.Index.X * chunk.Index.X * chunk.Index.Z + chunk.Index.Y * chunk.Index.Z, Seed) * Constants.Landscape.Vegetation.TreeVaryHeight + Constants.Landscape.Vegetation.TreeMinHeight));
                 }
             }
         }
     }
 }
        private static void GrowTree(Chunk chunk, BlockIndex originPoint, int treeSize)
        {
            BlockIndex point = originPoint + BlockIndex.UnitY;

            int trunkHeight = (int)Math.Floor((float)treeSize * 4.0F / 5.0F);
            float leafRadius = (float)trunkHeight / 2.0F;
            int leafCenter = (int)Math.Floor((float)treeSize * 2.0F / 3.0F);

            // Check whether or not tree can be placed at all

            BlockIndex blockWorldPos;

            List<BlockIndex> leafPositions = new List<BlockIndex>();
            List<BlockIndex> trunkPositions = new List<BlockIndex>();

            for (int x = -(int)leafRadius; x <= leafRadius; x++)
            {
                for (int z = -(int)leafRadius; z <= leafRadius; z++)
                {
                    for (int y = 0; y <= treeSize; y++)
                    {
                        blockWorldPos = new BlockIndex(point.X + x, point.Y + y, point.Z + z) + chunk.Index;
                        if (x == 0 && z == 0 && y <= trunkHeight)
                        {
                            if (ChunkManager.GetBlock(blockWorldPos) != Block.Air)
                            {
                                return;
                            }
                            trunkPositions.Add(blockWorldPos);
                        }
                        else
                        {
                            if (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(z, 2) + Math.Pow(y - leafCenter, 2)) < leafRadius)
                            {
                                if (ChunkManager.GetBlock(blockWorldPos) != Block.Air)
                                {
                                    return;
                                }
                                leafPositions.Add(blockWorldPos);
                            }
                        }
                    }
                }
            }

            foreach (BlockIndex index in leafPositions)
            {
                if (index.ToChunkIndex() == chunk.Index)
                {
                    chunk[index - chunk.Index] = Block.Leaves;
                }
                else
                {
                    //ChunkManager.SetBlock(index, Block.Leaves, false);
                }
            }

            foreach (BlockIndex index in trunkPositions)
            {
                if (index.ToChunkIndex() == chunk.Index)
                {
                    chunk[index - chunk.Index] = Block.Log;
                }
                else
                {
                    //Constants.World.Current.SetBlock(index, Block.Log, false);
                }
            }

            chunk[originPoint] = Block.Dirt;
        }
        public void AddSorted(Chunk chunk)
        {
            if (Contains(chunk))
            {
                return;
            }

            if (Count == 0 || chunk == null)
            {
                Enqueue(chunk);
                return;
            }

            for (int i = 0; i < Count; i++)
            {
                Chunk tempChunk;
                try
                {
                    tempChunk = this[i];
                }
                catch (Exception e)
                {
                    try
                    {
                        tempChunk = this[i];
                    }
                    catch (Exception e2)
                    {
                        Popup.Post("EXCEPTION222222222!!!!! " + e2.Message);
                        Enqueue(chunk);
                        return;
                    }
                    Popup.Post("EXCEPTION!!!!! " + e.Message);
                    Enqueue(chunk);
                    return;
                }

                ChunkIndex modified = chunk.Index * new ChunkIndex(1, 1, 1);
                if (tempChunk != null)
                {
                    int val = ((modified.Position - Constants.Engines.Physics.Player.Position).LengthSquared).CompareTo((tempChunk.Index.Position - Constants.Engines.Physics.Player.Position).LengthSquared);

                    if (val > 0)
                    {
                        ChunkList.Insert(i, chunk);
                        return;
                    }
                    else if (val <= 0)
                    {
                        continue;
                    }
                }
            }

            Enqueue(chunk);
        }
 public void Remove(Chunk chunk)
 {
     if (ChunkList.Contains(chunk))
     {
         ChunkList.Remove(chunk);
     }
 }
 public void Enqueue(Chunk chunk)
 {
     ChunkList.Add(chunk);
 }
 public bool Contains(Chunk chunk)
 {
     return ChunkList.Contains(chunk);
 }
        public static void ChangeChunk(Chunk chunk)
        {
            ChunkIndex? arrayIndex = GetArrayIndex(chunk.Index);

            if (arrayIndex.HasValue)
            {
                GL.BindTexture(TextureTarget.Texture3D, DataID);

                GL.TexSubImage3D(TextureTarget.Texture3D, 0,
                    arrayIndex.Value.X * Constants.World.ChunkSize, arrayIndex.Value.Y * Constants.World.ChunkSize, arrayIndex.Value.Z * Constants.World.ChunkSize,
                    Constants.World.ChunkSize, Constants.World.ChunkSize, Constants.World.ChunkSize,
                    OpenTK.Graphics.OpenGL.PixelFormat.Luminance, PixelType.UnsignedByte, chunk.GetRawData());

                GL.BindTexture(TextureTarget.Texture3D, 0);
            }
        }
        static Chunk ObtainChunk(ChunkIndex index, bool setup = false)
        {
            ChunkIndex? actualIndex = GetArrayIndex(index);
            Chunk chunk;

            if (actualIndex.HasValue && !setup)
            {
                chunk = Chunks[actualIndex.Value.X, actualIndex.Value.Y, actualIndex.Value.Z];
            }
            else
            {
                chunk = new Chunk(index);

                if (IsChunkOnDisk(index))
                {
                    Setup.AddToLoad(chunk);
                }
                else
                {
                    TerrainGenerator.SetChunkTerrain(chunk);
                    Console.Write("Chunk terrain generated!");
                }
            }

            return chunk;
        }
        public static void SaveChunk(Chunk chunk)
        {
            FileStream chunkStream = File.OpenWrite(GetChunkFilename(chunk.Index));

            for (int x = 0; x < Constants.World.ChunkSize; x++)
            {
                for (int y = 0; y < Constants.World.ChunkSize; y++)
                {
                    for (int z = 0; z < Constants.World.ChunkSize; z++)
                    {
                        chunkStream.WriteByte(chunk[x, y, z].ByteValue);
                    }
                }
            }
        }
        public static Chunk LoadChunkImmediate(Chunk chunk)
        {
            FileStream chunkStream = File.OpenRead(GetChunkFilename(chunk.Index));

            for (int x = 0; x < Constants.World.ChunkSize; x++)
            {
                for (int y = 0; y < Constants.World.ChunkSize; y++)
                {
                    for (int z = 0; z < Constants.World.ChunkSize; z++)
                    {
                        chunk[x, y, z] = new Block((byte)chunkStream.ReadByte());
                    }
                }
            }

            return chunk;
        }
        public static Chunk LoadChunk(ChunkIndex index)
        {
            Chunk returnChunk = new Chunk(index);

            return returnChunk;
        }
        public static void SetChunkTerrain(Chunk chunk)
        {
            float density;
            int terrainHeight;
            int absoluteHeight;
            int relativeHeight;

            int[,] heightMap = GetHeightMap(chunk.Index);

            for (int x = 0; x < Constants.World.ChunkSize; x++)
            {
                for (int z = 0; z < Constants.World.ChunkSize; z++)
                {
                    for (int y = 0; y < Constants.World.ChunkSize; y++)
                    {
                        absoluteHeight = chunk.Index.Y * 32 + y;
                        terrainHeight = heightMap[x, z];
                        relativeHeight = absoluteHeight - terrainHeight;

                        if (absoluteHeight >= terrainHeight)
                        {
                            if (absoluteHeight <= Constants.Landscape.WaterLevel)
                            {
                                chunk[x, y, z] = Block.Water;
                            }
                            else
                            {
                                chunk[x, y, z] = Block.Air;
                            }
                        }
                        else
                        {
                            if (Constants.Landscape.CavesEnabled)
                            {
                                density = NoiseMaps.GetTrilinearlyInterpolated((int)chunk.Index.Position.X + x, (int)chunk.Index.Position.Y + y, (int)chunk.Index.Position.Z + z, 16, Seed);

                                if (density < GetTresholdFromHeight(relativeHeight))
                                {
                                    chunk[x, y, z] = Block.Air;
                                    continue;
                                }
                            }
                            if (absoluteHeight < Constants.Landscape.SandLevel && relativeHeight >= -4)
                            {
                                chunk[x, y, z] = Block.Sand;
                            }
                            else if (relativeHeight >= -5)
                            {
                                if (relativeHeight == -1)
                                {
                                    chunk[x, y, z] = Block.Leaves;
                                }
                                else
                                {
                                    chunk[x, y, z] = Block.Dirt;
                                }
                            }
                            else
                            {
                                chunk[x, y, z] = Block.Stone;
                            }
                        }
                    }
                }
            }
        }
Beispiel #14
0
 public void AddToVegetation(Chunk chunk)
 {
     VegetationQueue.Enqueue(chunk);
 }
Beispiel #15
0
 public void AddToLoad(Chunk chunk)
 {
     LoadQueue.Enqueue(chunk);
 }
Beispiel #16
0
 public void AddToGeneration(Chunk chunk)
 {
     GenerationQueue.Enqueue(chunk);
 }