Beispiel #1
0
        private uint Resize(int bits, BlockState state)
        {
            var oldStorage = Storage;

            var oldPallete = Pallette;

            _bits = bits;

            Pallette = new IntIdentityHashBiMap <BlockState>(1 << bits);
            Storage  = new FlexibleStorage(bits, 4096);
            IdFor(Air);

            for (int i = 0; i < oldStorage.Length; i++)
            {
                var oldEntry = oldPallete.Get(oldStorage[i]);
                if (oldEntry != null)
                {
                    Set(i, oldEntry);
                }
            }

            oldStorage.Dispose();

            return(IdFor(state));
        }
Beispiel #2
0
        public BlockStorage()
        {
            _bits = 8;

            Storage  = new FlexibleStorage(_bits, 4096);
            Pallette = new IntIdentityHashBiMap <BlockState>((1 << _bits));

            Pallette.Add(Air);
        }
Beispiel #3
0
        public void Read(MinecraftStream ms)
        {
            var blockCount   = ms.ReadShort();
            var bitsPerBlock = (byte)ms.ReadByte();

            int palleteLength = 0;// = ms.ReadVarInt();

            if (bitsPerBlock <= 4)
            {
                bitsPerBlock = 4;
            }

            if (bitsPerBlock <= 8)
            {
                _bits = bitsPerBlock;

                palleteLength = ms.ReadVarInt();

                Pallette = new IntIdentityHashBiMap(palleteLength);
                Pallette.Add(Air);

                for (int id = 0; id < palleteLength; id++)
                {
                    uint       stateId = (uint)ms.ReadVarInt();
                    BlockState state   = BlockFactory.GetBlockState(stateId);
                    Pallette.Put(state, (uint)id);
                }
            }
            else
            {
                _bits    = (int)Math.Ceiling(Math.Log2(BlockFactory.AllBlockstates.Count));
                Pallette = new DirectPallete();
            }

            int length = ms.ReadVarInt();

            long[] dataArray = new long[length];
            for (int i = 0; i < dataArray.Length; i++)
            {
                dataArray[i] = ms.ReadLong();
            }

            Storage = new FlexibleStorage(_bits, 4096);
            var valueMask = (uint)((1L << _bits) - 1);

            for (int index = 0; index < 4096; index++)
            {
                var state = index / (64 / _bits);
                var data  = dataArray[state];

                var shiftedData = data >> (index % (64 / _bits) * _bits);

                Storage[index] = (uint)(shiftedData & valueMask);
            }
        }
Beispiel #4
0
        public void Read(MinecraftStream ms)
        {
            var blockCount   = ms.ReadShort();
            var bitsPerBlock = (byte)ms.ReadByte();

            int palleteLength = 0;// = ms.ReadVarInt();

            if (bitsPerBlock <= 4)
            {
                bitsPerBlock = 4;
            }

            if (bitsPerBlock <= 8)
            {
                _bits = bitsPerBlock;

                palleteLength = ms.ReadVarInt();

                Pallette = new IntIdentityHashBiMap <BlockState>(palleteLength);
                Pallette.Add(Air);

                //else
                //     palleteLength =

                for (int id = 0; id < palleteLength; id++)
                {
                    uint       stateId = (uint)ms.ReadVarInt();
                    BlockState state   = BlockFactory.GetBlockState(stateId);
                    Pallette.Put(state, (uint)id);
                    // idToState.Set(id, state);
                    // stateToId.Set(state, id);
                }
            }
            else
            {
                _bits    = (int)Math.Ceiling(Math.Log2(BlockFactory.AllBlockstates.Count));
                Pallette = new DirectPallete();
            }

            int length = ms.ReadVarInt();

            long[] dataArray = new long[length];
            for (int i = 0; i < dataArray.Length; i++)
            {
                dataArray[i] = ms.ReadLong();
            }

            Storage = new FlexibleStorage(_bits, dataArray);
            //Storage._data = dataArray;
        }
Beispiel #5
0
        private uint IdFor(BlockState state)
        {
            uint i = Pallette.GetId(state);

            if (i == uint.MaxValue)
            {
                i = Pallette.Add(state);

                if (i >= (1 << this._bits))
                {
                    var newBits = _bits + 1;
                    if (newBits < 8)
                    {
                        var old = Storage;
                        Storage = new FlexibleStorage(newBits, 4096);
                        for (int s = 0; s < 4096; s++)
                        {
                            Storage[s] = old[s];
                        }
                    }
                    else
                    {
                        var bits       = (int)Math.Ceiling(Math.Log2(BlockFactory.AllBlockstates.Count));
                        var oldPalette = Pallette;
                        var oldStorage = Storage;
                        Pallette = new DirectPallete();
                        Storage  = new FlexibleStorage(bits, 4096);
                        for (int s = 0; s < 4096; s++)
                        {
                            var oldValue = oldStorage[s];
                            var newValue = oldPalette.GetId(oldPalette.Get(oldValue));
                            Storage[s] = newValue;
                            //data.set(i, newValue);
                        }

                        return(Pallette.GetId(state));
                    }
                    //return Resize(_bits + 1, state);
                }
            }

            return(i);
        }