Exemple #1
0
        public static void Enqueue(Chunk c, bool[] toRender)
        {
            Chunk adjacentChunk;

            if (!_renderQueue.Contains(c))
                _renderQueue.Enqueue(c);

            if (toRender[0] && MapChunks.Map.TryGetValue(new Vector2(c.ChunkX + 1, c.ChunkZ), out adjacentChunk))        // check if there is a chunk (at the vector2 position) and rerender it
                if (!_renderQueue.Contains(adjacentChunk))                                                                   // dont enqueue the chunk if its already in queue
                    _renderQueue.Enqueue(adjacentChunk);

            if (toRender[1] && MapChunks.Map.TryGetValue(new Vector2(c.ChunkX - 1, c.ChunkZ), out adjacentChunk))
                if (!_renderQueue.Contains(adjacentChunk))
                    _renderQueue.Enqueue(adjacentChunk);

            if (toRender[2] && MapChunks.Map.TryGetValue(new Vector2(c.ChunkX, c.ChunkZ + 1), out adjacentChunk))
                if (!_renderQueue.Contains(adjacentChunk))
                    _renderQueue.Enqueue(adjacentChunk);

            if (toRender[3] && MapChunks.Map.TryGetValue(new Vector2(c.ChunkX, c.ChunkZ - 1), out adjacentChunk))
                if (!_renderQueue.Contains(adjacentChunk))
                    _renderQueue.Enqueue(adjacentChunk);

            _queueNotifier.Set();
        }
Exemple #2
0
        //todo: better!
        private void StoreChunks(byte[] uncompressed)
        {
            int proceededSections = 0;
            int chunkcount = 0;
            for (int i = 0; i < _chunkCount; i++)
            {
                int x = BitConverter.ToInt32(_metaData.Skip(i * 12).Take(4).Reverse().ToArray(), 0);
                int z = BitConverter.ToInt32(_metaData.Skip(i * 12 + 4).Take(4).Reverse().ToArray(), 0);
                ushort bitmask = BitConverter.ToUInt16(_metaData.Skip(i * 12 + 8).Take(2).Reverse().ToArray(), 0);
                int sectioncount = Convert.ToString(bitmask, 2).ToCharArray().Count(a => a == '1');
                byte[] chunkdata = uncompressed.Skip(proceededSections * 10240 + chunkcount * 256).Take(10240 * sectioncount + 256).ToArray();
                proceededSections += sectioncount;
                chunkcount++;

                Chunk c;
                if (MapChunks.Map.TryGetValue(new Vector2(x, z), out c))
                {
                    //c.SetChunk(bitmask, chunkdata, sectioncount);
                    //TODO: implement correct way to change chunk sections
                }
                else
                {
                    c = new Chunk(x, z, sectioncount, bitmask, chunkdata);
                    MapChunks.Map.TryAdd(new Vector2(x, z), c);
                }
            }
        }
Exemple #3
0
        private void Store_Chunk(int x, int z, byte groundUp, ushort primBit, short addBit, byte[] uncompressedChunkData)
        {
            int sectioncount = Convert.ToString(primBit, 2).ToCharArray().Count(a => a == '1');

            Chunk c;
            if (MapChunks.Map.TryGetValue(new Vector2(x, z), out c))
            {
                //c.SetChunk(bitmask, chunkdata, sectioncount);
            }
            else
            {
                c = new Chunk(x, z, sectioncount, primBit, uncompressedChunkData);
                MapChunks.Map.TryAdd(new Vector2(x, z), c);
            }
        }
        public Mesh[] Generate(Chunk c, GraphicsDevice gd)
        {
            _opaqueVertices = new List<VertexPositionNormalTexture>();
            _opaqueIndices = new List<int>();
            _transparentVertices = new List<VertexPositionNormalTexture>();
            _transparentIndices = new List<int>();

            _x = 0;
            _y = 0;
            _z = 0;

            _chunk = c;

            for (; ; _x++)
            {
                if (_x > 15)
                {
                    _z++;
                    _x = 0;
                    if (_z > 15)
                    {
                        _y++;
                        _z = 0;
                        if (_y > 255)
                        {
                            break;
                        }
                    }
                }

                _id = c.BlockType[_x, _y, _z];
                _meta = c.BlockMetadata[_x, _y, _z];

                if (!Blocks.blocks[_id].NotABlock && !Blocks.blocks[_id].IsTransparent)
                {
                    if (!Blocks.blocks[_id].HasMetadata)
                    {
                        if (Blocks.blocks[_id].IsMultitex)
                        {
                            if (IsTransparent(0))
                                CreateMultiTexBlockSide(0, 0);
                            if (IsTransparent(1))
                                CreateMultiTexBlockSide(1, 1);
                            if (IsTransparent(2))
                                CreateMultiTexBlockSide(2, 2);
                            if (IsTransparent(3))
                                CreateMultiTexBlockSide(3, 3);
                            if (IsTransparent(4))
                                CreateMultiTexBlockSide(4, 4);
                            if (IsTransparent(5))
                                CreateMultiTexBlockSide(5, 5);
                        }
                        else
                        {
                            if (IsTransparent(0))
                                CreateBlockSide(0);
                            if (IsTransparent(1))
                                CreateBlockSide(1);
                            if (IsTransparent(2))
                                CreateBlockSide(2);
                            if (IsTransparent(3))
                                CreateBlockSide(3);
                            if (IsTransparent(4))
                                CreateBlockSide(4);
                            if (IsTransparent(5))
                                CreateBlockSide(5);
                        }
                    }
                    else
                    {
                        if (Blocks.blocks[_id].IsMultitex)
                        {
                            if (IsTransparent(0))
                                CreateMultiTexBlockSide(0, (byte)(0 + _meta * 6));
                            if (IsTransparent(1))
                                CreateMultiTexBlockSide(1, (byte)(1 + _meta * 6));
                            if (IsTransparent(2))
                                CreateMultiTexBlockSide(2, (byte)(2 + _meta * 6));
                            if (IsTransparent(3))
                                CreateMultiTexBlockSide(3, (byte)(3 + _meta * 6));
                            if (IsTransparent(4))
                                CreateMultiTexBlockSide(4, (byte)(4 + _meta * 6));
                            if (IsTransparent(5))
                                CreateMultiTexBlockSide(5, (byte)(5 + _meta * 6));
                        }
                        else
                        {
                            if (IsTransparent(0))
                                CreateBlockSide(0);
                            if (IsTransparent(1))
                                CreateBlockSide(1);
                            if (IsTransparent(2))
                                CreateBlockSide(2);
                            if (IsTransparent(3))
                                CreateBlockSide(3);
                            if (IsTransparent(4))
                                CreateBlockSide(4);
                            if (IsTransparent(5))
                                CreateBlockSide(5);
                        }
                    }
                }
                else if (Blocks.blocks[_id].NotABlock)
                {
                    if (Blocks.blocks[_id].IsXSprite)
                    {
                        CreateXSprite();
                    }
                    else if (_id == 9)//quick hack for water
                    {
                        if (c.BlockType[_x, _y + 1, _z] != 9)
                            CreateWater(4);
                    }
                    else if (_id == 79) //ice
                    {
                        if (!SameBlock(0))
                            TransparentQuad(0);
                        if (!SameBlock(1))
                            TransparentQuad(1);
                        if (!SameBlock(2))
                            TransparentQuad(2);
                        if (!SameBlock(3))
                            TransparentQuad(3);
                        if (!SameBlock(4))
                            TransparentQuad(4);
                        if (!SameBlock(5))
                            TransparentQuad(5);
                    }
                }
                else if (Blocks.blocks[_id].IsTransparent)
                {
                    if (_id == 18) // leaves
                    {
                        CreateBlockSide(0);
                        CreateBlockSide(1);
                        CreateBlockSide(2);
                        CreateBlockSide(3);
                        CreateBlockSide(4);
                        CreateBlockSide(5);
                    }
                    else if (_id == 20)
                    {
                        if (!SameBlock(0))
                            CreateBlockSide(0);
                        if (!SameBlock(1))
                            CreateBlockSide(1);
                        if (!SameBlock(2))
                            CreateBlockSide(2);
                        if (!SameBlock(3))
                            CreateBlockSide(3);
                        if (!SameBlock(4))
                            CreateBlockSide(4);
                        if (!SameBlock(5))
                            CreateBlockSide(5);
                    }
                }
            }

            Mesh[] meshes = new Mesh[2];

            if (_opaqueIndices.Count > 0)
                meshes[0] = new Mesh(gd, _opaqueVertices.ToArray(), _opaqueIndices.ToArray());
            if (_transparentIndices.Count > 0)
                meshes[1] = new Mesh(gd, _transparentVertices.ToArray(), _transparentIndices.ToArray());

            _opaqueVertices = null;
            _opaqueIndices = null;
            _transparentVertices = null;
            _transparentIndices = null;

            return meshes;
        }