private bool EnsureY(int y, out IDynBitArray bitarray, out List <NamespacedBlock> palette) { bitarray = _blockStates[y]; if (bitarray == null) { var longs = _blockStateRaw[y]; if (longs != null) { palette = _palette[y] = ToNamespacedBlockList(_paletteList[y]); bitarray = _blockStates[y] = DynBitArray.CreateFromLongArray(longs); _paletteList[y] = null; _blockStateRaw[y] = null; return(true); } else { bitarray = null; palette = null; return(false); } } palette = _palette[y]; return(true); }
public void LongResultTest() { var longs = new[] { 2L, -1L, 2181234892347L, 10000L }; var dyn = DynBitArray.CreateFromLongArray(longs, 4); var ordinalDyn = new DynBitArray(dyn, 4); Assert.AreEqual(DynBitArray.ToLongArray(ordinalDyn), longs); }
protected void SaveSection(int section) { // Clear the section in chunk if it is cleared in this snapshot if (_palette[section] == null || _palette[section].Count == 0) { _chunk._palette[section]?.Clear(); _chunk._palette[section] = null; _chunk._blockStates[section] = null; return; } // It's possible the section has not been created in chunk var oldCellSize = _chunk._blockStates[section] == null ? -1 : _chunk._blockStates[section].CellSize; // Only update the palette when it is changed if (_paletteChanged[section]) { _chunk._palette[section]?.Clear(); _chunk._palette[section] = _palette[section].Clone(); } var newCellSize = Math.Max(4, NumericUtility.GetRequiredBitLength(_palette[section].Count)); if ((newCellSize == oldCellSize) || (!CompactBlockBitsIfPossible && (newCellSize < oldCellSize))) { // Do not create new DynArray if the cellSize doesn't change to improve performance. var dyn = _chunk._blockStates[section]; for (var i = 0; i < 4096; i++) { dyn[i] = _blocks[section][i]; } } else { // Create new DynArray when cellSize is changed var dyn = DynBitArray.CreateEmpty(newCellSize, 4096); for (var i = 0; i < 4096; i++) { // Because the assignment of dynArray[i] can be time-consuming, // index(0) is skipped because DynBitArray.CreateEmpty is guaranteed to create a zero-filled array if (_blocks[section][i] == 0) { continue; } dyn[i] = _blocks[section][i]; } _chunk._blockStates[section]?.Clear(); _chunk._blockStates[section] = dyn; } }
private void ReadFromNewFormat(NbtCompound heightmaps) { for (var id = 0; id < HeightMapTypes.Length; id++) { if (heightmaps.TryGet <NbtLongArray>(HeightMapTypes[id], out var longarray)) { HeightMaps[id] = DynBitArray.CreateFromLongArray(longarray.Value, 9); } } }
public void Specialized5Test() { var dyn = DynBitArray.CreateEmpty(5, 100); for (var i = 0; i < 100; i++) { dyn[i] = i % 31; } for (var i = 0; i < 100; i++) { Assert.AreEqual(i % 31, dyn[i]); } }
public void DynBitArrayEquivalentTest() { var bytes = new byte[16]; for (var i = 0; i < 32; i++) { EndianHelper.SetHalfInt(bytes, i, BaseData[i]); } var dynarray = DynBitArray.CreateFromByteArray(bytes, 4); Assert.AreEqual(32, dynarray.Length); for (var i = 0; i < 32; i++) { Assert.AreEqual(dynarray[i], EndianHelper.GetHalfInt(bytes, i)); } }
internal void EnsureAllSections() { for (var sy = 0; sy < 16; sy++) { var y = sy; var longs = _blockStateRaw[sy]; if (longs == null) { continue; } _palette[y] = ToNamespacedBlockList(_paletteList[y]); _blockStates[y] = DynBitArray.CreateFromLongArray(longs); _paletteList[y] = null; _blockStateRaw[y] = null; } }
public void ConversionTest() { var dyn = DynBitArray.CreateEmpty(5, 100); for (var i = 0; i < 100; i++) { dyn[i] = i % 31; } Assert.Throws <NotSupportedException>(() => { DynBitArray.CreateCloneFrom(3, dyn); }); var dyn2 = DynBitArray.CreateCloneFrom(11, dyn); for (var i = 0; i < 100; i++) { Assert.AreEqual(i % 31, dyn2[i]); } }
private bool EnsureY(int y) { if (_blockStates[y] == null) { var longs = _blockStateRaw[y]; if (longs != null) { _palette[y] = ToNamespacedBlockList(_paletteList[y]); _blockStates[y] = DynBitArray.CreateFromLongArray(longs); _paletteList[y] = null; _blockStateRaw[y] = null; } else { return(false); } } return(true); }
protected override bool GetBlockData(NbtCompound section) { if (!section.TryGet(FieldY, out NbtByte nbtY)) { return(false); } int y = nbtY.Value; if (y == 255) { return(true); } if (y >= 16) { return(false); } if (!section.TryGet(FieldPalette, out NbtList list)) { return(false); } if (!section.TryGet(FieldBlockStates, out NbtLongArray blocks)) { return(false); } if (SnapshotStrategy == NbtSnapshotStrategy.Enable) { _paletteList[y] = list; _blockStateRaw[y] = blocks.Value; } else { _palette[y] = ToNamespacedBlockList(list); _blockStates[y] = DynBitArray.CreateFromLongArray(blocks.Value); } return(true); }
public void GeneralVersionTest() { Assert.Throws <ArgumentException>(() => { DynBitArray.CreateFromByteArray(new byte[] { 1 }, 17); }); var dyn = DynBitArray.CreateEmpty(12, 100); Assert.AreEqual(dyn.Length, 100); Assert.AreEqual(dyn.CellSize, 12); for (var i = 0; i < 100; i++) { dyn[i] = i; } for (var i = 0; i < 100; i++) { Assert.AreEqual(i, dyn[i]); } }
public void Calculate(LowLevelChunk chunk) { var maxHeight = (chunk.GetExistingYs().Max() << 4) + 15; var heightMap = new int[256]; for (var z = 0; z < 16; z++) { for (var x = 0; x < 16; x++) { for (var y = maxHeight; y >= 0; y--) { if (!chunk.IsAirBlock(x, y, z)) { heightMap[GetIndexByXZ(x, z)] = y; break; } } } } if (chunk is NamespacedChunk) { var arr = DynBitArray.CreateEmpty(9, 256); for (var i = 0; i < 256; i++) { arr[i] = heightMap[i]; } HeightMaps[(int)HeightmapType.WorldSurface] = arr; State = AttributeVersion.Post113; } else { ClassicHeightMap = heightMap; State = AttributeVersion.Pre113; } }