summaryrefslogtreecommitdiff
path: root/src/main/java/com/encrox/instancedregions/chunkmap/ChunkMapManager.java
blob: 87fb3a1366fa2235c1d87551c8bd2a9250f38241 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/**
 * @author Aleksey Terzi
 *
 */

package com.encrox.instancedregions.chunkmap;

import java.io.IOException;
import java.util.Arrays;

import com.encrox.instancedregions.types.BlockState;

public class ChunkMapManager {
    private static final ThreadLocal<ChunkMapBuffer> _buffer = new ThreadLocal<ChunkMapBuffer>() {
    	@Override
        protected ChunkMapBuffer initialValue() {
            return new ChunkMapBuffer();
        }
    };
    
    private ChunkMapBuffer buffer;
    private ChunkData chunkData;
    private ChunkReader reader;
    private int sectionCount;
    private int sectionIndex;
    private int y;
    private int minX;
    private int maxX;
    private int minZ;
    private int maxZ;
    private int blockIndex;
    
    public int getSectionCount() {
    	return this.sectionCount;
    }
    
    public int getY() {
    	return this.y;
    }
    
    public ChunkData getChunkData() {
    	return this.chunkData;
    }

    public ChunkMapManager(ChunkData chunkData) {
    	this.buffer = _buffer.get();
    	this.chunkData = chunkData;
    }
    
    public void init() throws IOException {
    	this.reader = new ChunkReader(this.chunkData.data);
    	this.sectionCount = 0;
    	this.sectionIndex = -1;
    	this.minX = this.chunkData.chunkX << 4;
    	this.maxX = this.minX + 15;
    	this.minZ = this.chunkData.chunkZ << 4;
    	this.maxZ = this.minZ + 15;
    	
		this.buffer.lightArrayLength = 2048;
		
		if(this.chunkData.isOverworld) {
			this.buffer.lightArrayLength <<= 1;
		}
    	
    	this.buffer.writer.init();
    	
    	int mask = this.chunkData.primaryBitMask;
    	
    	while(mask != 0) {
    		if((mask & 0x1) != 0) {
    			this.sectionCount++;
    		}
    		
    		mask >>>= 1;
    	}
    	
    	this.buffer.clearLayers();

    	moveToNextLayer();
    }
    
    public boolean inputHasNonAirBlock() {
    	return this.buffer.paletteLength > 1 || this.buffer.palette[0] != 0;
    }
    
    public static void blockDataToState(int blockData, BlockState blockState) {
    	blockState.id = blockData >>> 4;
    	blockState.meta = blockData & 0xf;
    }
    
    public static int getBlockIdFromData(int blockData) {
    	return blockData >>> 4;
    }

    public static int getBlockMetaFromData(int blockData) {
    	return blockData & 0xf;
    }

    public static int blockStateToData(BlockState blockState) {
    	return (blockState.id << 4) | blockState.meta; 
    }
    
    public static int getBlockDataFromId(int id) {
    	return id << 4;
    }

    public boolean initOutputPalette() {
    	if(this.buffer.paletteLength == 0 || this.buffer.paletteLength == 255) {
    		this.buffer.outputPaletteLength = 0;
    		return false;
    	}
    	
    	Arrays.fill(this.buffer.outputPaletteMap, (byte)-1);
    	
    	this.buffer.outputPaletteLength = this.buffer.paletteLength;
    	
    	for(int i = 0; i < this.buffer.paletteLength; i++) {
    		int blockData = this.buffer.palette[i];
    		    		
    		this.buffer.outputPalette[i] = blockData;
    		
    		if(blockData >= 0) {
    			this.buffer.outputPaletteMap[blockData] = (byte)i;
    		}
    	}
    	
    	return true;
    }
    
    public boolean addToOutputPalette(int blockData) {
    	if(this.buffer.outputPaletteMap[blockData] >= 0) return true;
    	
    	//255 (-1 for byte) is special code in my algorithm
    	if(this.buffer.outputPaletteLength == 254) {
    		this.buffer.outputPaletteLength = 0;
    		return false;
    	}
    	
    	this.buffer.outputPalette[this.buffer.outputPaletteLength] = blockData;
    	this.buffer.outputPaletteMap[blockData] = (byte)this.buffer.outputPaletteLength;
    	
    	this.buffer.outputPaletteLength++;
    	
    	return true;
    }
    
    public void initOutputSection() throws IOException {
    	calcOutputBitsPerBlock();
    	
    	this.buffer.writer.setBitsPerBlock(this.buffer.outputBitsPerBlock);
    	
    	//Bits Per Block
    	this.buffer.writer.writeByte((byte)this.buffer.outputBitsPerBlock);
    	
    	//Palette Length
    	this.buffer.writer.writeVarInt(this.buffer.outputPaletteLength);
    	
    	//Palette
    	for(int i = 0; i < this.buffer.outputPaletteLength; i++) {
    		this.buffer.writer.writeVarInt(this.buffer.outputPalette[i]);
    	}
    	
    	int dataArrayLengthInBits = this.buffer.outputBitsPerBlock << 12;// multiply by 4096
    	int outputDataArrayLength = dataArrayLengthInBits >>> 6;//divide by 64
    	
    	if((dataArrayLengthInBits & 0x3f) != 0) {
    		outputDataArrayLength++;
    	}
    	
    	//Data Array Length
    	this.buffer.writer.writeVarInt(outputDataArrayLength);
    	
    	//Copy Block Light and Sky Light arrays
    	int lightArrayStartIndex = this.buffer.dataArrayStartIndex + (this.buffer.dataArrayLength << 3);
    	int outputLightArrayStartIndex = this.buffer.writer.getByteIndex() + (outputDataArrayLength << 3);
    	
    	System.arraycopy(
    			this.chunkData.data,
    			lightArrayStartIndex,
    			this.buffer.output,
    			outputLightArrayStartIndex,
    			this.buffer.lightArrayLength
    			);
    }
    
    public void writeOutputBlock(int blockData) throws IOException {
    	if(this.buffer.outputPaletteLength > 0) {
    		long paletteIndex = this.buffer.outputPaletteMap[blockData] & 0xffL;
    		
    		if(paletteIndex == 255) {
    			BlockState blockState = new BlockState();
    			blockDataToState(blockData, blockState);
    			throw new IllegalArgumentException("Block " + blockState.id + ":" + blockState.meta + " is absent in output palette.");
    		}
    		
    		this.buffer.writer.writeBlockBits(paletteIndex);
    	} else {
    		this.buffer.writer.writeBlockBits(blockData);
    	}
    }
    
    public void finalizeOutput() throws IOException {
    	if(this.buffer.writer.getByteIndex() == 0) return;
    	
    	this.buffer.writer.save();
    	this.buffer.writer.skip(this.buffer.lightArrayLength);
    }
    
    public byte[] createOutput() {
    	int readerByteIndex = this.reader.getByteIndex();
    	int writerByteIndex = this.buffer.writer.getByteIndex();
    	int biomesSize = this.chunkData.data.length - readerByteIndex;
    	byte[] output = new byte[writerByteIndex + biomesSize];
    	
    	System.arraycopy(this.buffer.output, 0, output, 0, writerByteIndex);
    	
    	if(biomesSize > 0) {
	    	System.arraycopy(
	    			this.chunkData.data,
	    			readerByteIndex,
	    			output,
	    			writerByteIndex,
	    			biomesSize
	    			);
    	}
    	
    	return output;
    }
    
    private void calcOutputBitsPerBlock() {
    	if(this.buffer.outputPaletteLength == 0) {
    		this.buffer.outputBitsPerBlock = 13;
    	} else {
    		byte mask = (byte)this.buffer.outputPaletteLength;
    		int index = 0;
    		
    		while((mask & 0x80) == 0) {
    			index++;
    			mask <<= 1;
    		}
    		
    		this.buffer.outputBitsPerBlock = 8 - index;
  			
  			if(this.buffer.outputBitsPerBlock < 4) {
  				this.buffer.outputBitsPerBlock = 4;
  			}
    	}
    }
    
    public int readNextBlock() throws IOException {
    	if(this.blockIndex == 16 * 16) {
    		if(!moveToNextLayer()) return -1;
    	}
    	
    	return this.buffer.curLayer.map[this.blockIndex++];
    }
    
    public int get(int x, int y, int z) throws IOException {
    	if(x < minX || x > maxX
    			|| z < minZ || z > maxZ
    			|| y > 255 || y < this.y - 1 || y > this.y + 1
    			) {
    		return -1;
    	}
    	
    	ChunkLayer layer;
    	
    	if(y == this.y) layer = this.buffer.curLayer;
    	else if(y == this.y - 1) layer = this.buffer.prevLayer;
    	else layer = this.buffer.nextLayer;
    	
    	if(!layer.hasData) return -1;
    	
    	int blockIndex = ((z - this.minZ) << 4) | (x - this.minX);

    	return layer.map[blockIndex];
    }
        
    private boolean moveToNextLayer() throws IOException {
    	if(!increaseY()) return false;
		
		shiftLayersDown();
		
		if(!this.buffer.curLayer.hasData) {
			readLayer(this.buffer.curLayer);
		}

		if(((this.y + 1) >>> 4) > this.sectionIndex) {
			int oldSectionIndex = this.sectionIndex;
			
			moveToNextSection();
			
			if(this.sectionIndex < 16 && oldSectionIndex + 1 == this.sectionIndex) {
				readLayer(this.buffer.nextLayer);
			}
		} else {
			readLayer(this.buffer.nextLayer);
		}
		
		this.blockIndex = 0;
		
		return true;
    }
    
    private boolean increaseY() throws IOException {
		if(this.sectionIndex < 0) {
			if(!moveToNextSection()) return false;
			
			this.y = this.sectionIndex << 4;
		}
		else {
			this.y++;
			
			if((this.y & 0xf) == 0) {
				if(this.sectionIndex > 15) return false;

	    		if((this.y >>> 4) != this.sectionIndex) {
					this.buffer.clearLayers();
		    		this.y = this.sectionIndex << 4;
	    		}
			}
		}
		
		return true;
	}
    
    private void shiftLayersDown() {
		ChunkLayer temp = this.buffer.prevLayer;
		
		this.buffer.prevLayer = this.buffer.curLayer;
		this.buffer.curLayer = this.buffer.nextLayer;
		this.buffer.nextLayer = temp;
		this.buffer.nextLayer.hasData = false;
	}
    
    private boolean moveToNextSection() throws IOException {
    	if(this.sectionIndex >= 0) {
    		this.reader.skip(this.buffer.lightArrayLength);
    	}
    	
		do {
			this.sectionIndex++;
		} while(this.sectionIndex < 16 && (this.chunkData.primaryBitMask & (1 << this.sectionIndex)) == 0);
		
		if(this.sectionIndex >= 16) return false;
		
		readSectionHeader();
    	    	
    	return true;
    }
    
    private void readLayer(ChunkLayer layer) throws IOException {
    	for(int i = 0; i < 16 * 16; i++) {
    		int blockData = this.reader.readBlockBits();
    		
        	if(this.buffer.paletteLength > 0) {
        		blockData = blockData >= 0 && blockData < this.buffer.paletteLength
        				? this.buffer.palette[blockData]
        				: 0;
        	}

        	layer.map[i] = blockData;
    	}
    	
    	layer.hasData = true;
    }
    
    private void readSectionHeader() throws IOException {
    	this.buffer.bitsPerBlock = this.reader.readByte();
    	this.buffer.paletteLength = this.reader.readVarInt();
    	
    	for(int i = 0; i < this.buffer.paletteLength; i++) {
    		int paletteData = this.reader.readVarInt();
    		
    		this.buffer.palette[i] = paletteData;
    	}
    	
    	this.buffer.dataArrayLength = this.reader.readVarInt();
    	
    	this.buffer.dataArrayStartIndex = this.reader.getByteIndex();
    	
    	this.reader.setBitsPerBlock(this.buffer.bitsPerBlock);
    }
}