|
| 1 | + |
| 2 | +class BitArray |
| 3 | + constructor: (options)-> |
| 4 | + if options is null |
| 5 | + return |
| 6 | + if not options.bitsPerValue > 0 |
| 7 | + console.error 'bits per value must at least 1' |
| 8 | + if not (options.bitsPerValue <= 32) |
| 9 | + console.error 'bits per value exceeds 32' |
| 10 | + valuesPerLong = Math.floor 64 / options.bitsPerValue |
| 11 | + length = Math.ceil(options.capacity / valuesPerLong) |
| 12 | + if not options.data |
| 13 | + options.data = Array(length * 2).fill(0) |
| 14 | + valueMask = (1 << options.bitsPerValue) - 1 |
| 15 | + |
| 16 | + @data = options.data |
| 17 | + @capacity = options.capacity |
| 18 | + @bitsPerValue = options.bitsPerValue |
| 19 | + @valuesPerLong = valuesPerLong |
| 20 | + @valueMask = valueMask |
| 21 | + return |
| 22 | + get:(index)-> |
| 23 | + if not (index >= 0 && index < @capacity) |
| 24 | + console.error 'index is out of bounds' |
| 25 | + startLongIndex = Math.floor index / @valuesPerLong |
| 26 | + indexInLong = (index - startLongIndex * @valuesPerLong) * @bitsPerValue |
| 27 | + if indexInLong >= 32 |
| 28 | + indexInStartLong = indexInLong - 32 |
| 29 | + startLong = @data[startLongIndex * 2 + 1] |
| 30 | + return (startLong >>> indexInStartLong) & @valueMask |
| 31 | + startLong = @data[startLongIndex * 2] |
| 32 | + indexInStartLong = indexInLong |
| 33 | + result = startLong >>> indexInStartLong |
| 34 | + endBitOffset = indexInStartLong + @bitsPerValue |
| 35 | + if endBitOffset > 32 |
| 36 | + endLong = @data[startLongIndex * 2 + 1] |
| 37 | + result |= endLong << (32 - indexInStartLong) |
| 38 | + return result & @valueMask |
| 39 | + |
| 40 | +class ChunkDecoder |
| 41 | + getBlockIndex: (pos)-> |
| 42 | + return (pos.y << 8) | (pos.z << 4) | pos.x |
| 43 | + cvo: (voxelX,voxelY,voxelZ) -> |
| 44 | + x=voxelX %% 16|0 |
| 45 | + y=voxelY %% 16|0 |
| 46 | + z=voxelZ %% 16|0 |
| 47 | + return y*16*16+z*16+x |
| 48 | + computeSections: (packet)-> |
| 49 | + sections=packet.sections |
| 50 | + num=0 |
| 51 | + result=[] |
| 52 | + for i in sections |
| 53 | + num+=1 |
| 54 | + if i isnt null |
| 55 | + solidBlockCount=i.solidBlockCount |
| 56 | + palette=i.palette |
| 57 | + data=new BitArray i.data |
| 58 | + pos={ |
| 59 | + x:0 |
| 60 | + y:0 |
| 61 | + z:0 |
| 62 | + } |
| 63 | + cell=new Uint32Array 16*16*16 |
| 64 | + for x in [0..15] |
| 65 | + for y in [0..15] |
| 66 | + for z in [0..15] |
| 67 | + cell[@cvo(x,y,z)]=palette[data.get(@getBlockIndex({x,y,z}))] |
| 68 | + result.push { |
| 69 | + x:packet.x |
| 70 | + y:num |
| 71 | + z:packet.z |
| 72 | + cell |
| 73 | + } |
| 74 | + else |
| 75 | + result.push(null) |
| 76 | + return result |
| 77 | + |
| 78 | +addEventListener "message", (e)-> |
| 79 | + fn = handlers[e.data.type] |
| 80 | + if not fn |
| 81 | + throw new Error('no handler for type: ' + e.data.type) |
| 82 | + fn(e.data.data) |
| 83 | + return |
| 84 | + |
| 85 | +cd=new ChunkDecoder |
| 86 | + |
| 87 | +SectionComputer=(data)-> |
| 88 | + return cd.computeSections data |
| 89 | + |
| 90 | +export {SectionComputer} |
| 91 | + |
0 commit comments