-
-
Notifications
You must be signed in to change notification settings - Fork 204
/
ExtentHeightCacher.java
55 lines (47 loc) · 1.63 KB
/
ExtentHeightCacher.java
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
package com.fastasyncworldedit.core.extent;
import com.sk89q.worldedit.extent.Extent;
import java.util.Arrays;
public final class ExtentHeightCacher extends PassthroughExtent {
private transient int cacheBotX = Integer.MIN_VALUE;
private transient int cacheBotZ = Integer.MIN_VALUE;
private transient short[] cacheHeights;
private transient int lastY;
public ExtentHeightCacher(Extent extent) {
super(extent);
}
public void reset() {
cacheBotX = Integer.MIN_VALUE;
cacheBotZ = Integer.MIN_VALUE;
if (cacheHeights != null) {
Arrays.fill(cacheHeights, (short) 0);
}
}
@Override
public int getNearestSurfaceTerrainBlock(int x, int z, int y, int minY, int maxY) {
int rx = x - cacheBotX + 16;
int rz = z - cacheBotZ + 16;
int index;
if ((rx & 0xFF) != rx || (rz & 0xFF) != rz) {
cacheBotX = x - 16;
cacheBotZ = z - 16;
lastY = y;
rx = x - cacheBotX + 16;
rz = z - cacheBotZ + 16;
index = rx + (rz << 8);
if (cacheHeights == null) {
cacheHeights = new short[65536];
Arrays.fill(cacheHeights, (short) minY);
} else {
Arrays.fill(cacheHeights, (short) minY);
}
} else {
index = rx + (rz << 8);
}
int result = cacheHeights[index] & 0xFF;
if (result == minY) {
cacheHeights[index] = (short) (result = lastY = super
.getNearestSurfaceTerrainBlock(x, z, lastY, minY, maxY));
}
return result;
}
}