Try out generic chunk code and 3d biomes on Forge 1.18

This commit is contained in:
Mike Primm 2021-12-04 21:33:02 -06:00
parent 13d2cc05cb
commit 8c51db5608
9 changed files with 1308 additions and 1735 deletions

View file

@ -6,19 +6,19 @@ import org.dynmap.renderer.DynmapBlockState;
import org.dynmap.common.BiomeMap;
// Generic chunk representation
public class DynmapChunk {
public class GenericChunk {
public final int cx, cz; // Chunk coord (world coord / 16)
public final DynmapChunkSection[] sections;
public final GenericChunkSection[] sections;
public final int cy_min; // CY value of first section in sections list (index = (Y >> 4) - cy_min
public final long inhabitedTicks;
private DynmapChunk(int cx, int cz, int cy_min, DynmapChunkSection[] sections, long inhabTicks) {
private GenericChunk(int cx, int cz, int cy_min, GenericChunkSection[] sections, long inhabTicks) {
this.cx = cx;
this.cz = cz;
this.inhabitedTicks = inhabTicks;
this.sections = new DynmapChunkSection[sections.length + 2]; // Add one empty at top and bottom
this.sections = new GenericChunkSection[sections.length + 2]; // Add one empty at top and bottom
this.cy_min = cy_min - 1; // Include empty at bottom
Arrays.fill(this.sections, DynmapChunkSection.EMPTY); // Fill all spots with empty, including pad on bottom/top
Arrays.fill(this.sections, GenericChunkSection.EMPTY); // Fill all spots with empty, including pad on bottom/top
for (int off = 0; off < sections.length; off++) {
if (sections[off] != null) { // If defined, set the section
this.sections[off+1] = sections[off];
@ -26,36 +26,36 @@ public class DynmapChunk {
}
}
// Get section for given block Y coord
public final DynmapChunkSection getSection(int y) {
public final GenericChunkSection getSection(int y) {
try {
return this.sections[(y >> 4) - cy_min];
} catch (IndexOutOfBoundsException ioobx) { // Builder and padding should be avoiding this, but be safe
return DynmapChunkSection.EMPTY;
return GenericChunkSection.EMPTY;
}
}
public final DynmapBlockState getBlockType(int x, int y, int z) {
return getSection(y).blocks.getBlock(x, y, z);
}
public final DynmapBlockState getBlockType(DynmapChunkPos pos) {
public final DynmapBlockState getBlockType(GenericChunkPos pos) {
return getSection(pos.y).blocks.getBlock(pos);
}
public final int getBlockSkyLight(int x, int y, int z) {
return getSection(y).sky.getLight(x, y, z);
}
public final int getBlockSkyLight(DynmapChunkPos pos) {
public final int getBlockSkyLight(GenericChunkPos pos) {
return getSection(pos.y).sky.getLight(pos);
}
public final int getBlockEmittedLight(int x, int y, int z) {
return getSection(y).emitted.getLight(x, y, z);
}
public final int getBlockEmittedLight(DynmapChunkPos pos) {
public final int getBlockEmittedLight(GenericChunkPos pos) {
return getSection(pos.y).emitted.getLight(pos);
}
public final BiomeMap getBiome(int x, int y, int z) {
return getSection(y).biomes.getBiome(x, y, z);
}
public final BiomeMap getBiome(DynmapChunkPos pos) {
public final BiomeMap getBiome(GenericChunkPos pos) {
return getSection(pos.y).biomes.getBiome(pos);
}
public final boolean isSectionEmpty(int cy) {
@ -63,13 +63,17 @@ public class DynmapChunk {
}
public final long getInhabitedTicks() {
return inhabitedTicks;
}
}
// Generic empty (coordinates are wrong, but safe otherwise
public static final GenericChunk EMPTY = new GenericChunk(0, 0, -4, new GenericChunkSection[24], 0);
// Builder for fabricating finalized chunk
public static class Builder {
int x;
int z;
int y_min;
DynmapChunkSection[] sections;
GenericChunkSection[] sections;
long inhabTicks;
public Builder(int world_ymin, int world_ymax) {
@ -79,7 +83,7 @@ public class DynmapChunk {
x = 0; z = 0;
y_min = world_ymin >> 4;
int y_max = (world_ymax + 15) >> 4; // Round up
sections = new DynmapChunkSection[y_max - y_min]; // Range for all potential sections
sections = new GenericChunkSection[y_max - y_min]; // Range for all potential sections
}
// Set inhabited ticks
public Builder inhabitedTicks(long inh) {
@ -87,7 +91,7 @@ public class DynmapChunk {
return this;
}
// Set section
public Builder addSection(int sy, DynmapChunkSection sect) {
public Builder addSection(int sy, GenericChunkSection sect) {
if ((sy >= y_min) && ((sy - y_min) < sections.length)) {
this.sections[sy - y_min] = sect;
}
@ -100,8 +104,8 @@ public class DynmapChunk {
return this;
}
// Build chunk
public DynmapChunk build() {
return new DynmapChunk(x, z, y_min, sections, inhabTicks);
public GenericChunk build() {
return new GenericChunk(x, z, y_min, sections, inhabTicks);
}
}
}

View file

@ -0,0 +1,174 @@
package org.dynmap.common.chunk;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.lang.ref.SoftReference;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import org.dynmap.utils.DynIntHashMap;
// Generic chunk cache
public class GenericChunkCache {
public static class ChunkCacheRec {
public GenericChunk ss;
public DynIntHashMap tileData;
};
private CacheHashMap snapcache;
private ReferenceQueue<ChunkCacheRec> refqueue;
private long cache_attempts;
private long cache_success;
private boolean softref;
private static class CacheRec {
Reference<ChunkCacheRec> ref;
}
@SuppressWarnings("serial")
public class CacheHashMap extends LinkedHashMap<String, CacheRec> {
private int limit;
private IdentityHashMap<Reference<ChunkCacheRec>, String> reverselookup;
public CacheHashMap(int lim) {
super(16, (float)0.75, true);
limit = lim;
reverselookup = new IdentityHashMap<Reference<ChunkCacheRec>, String>();
}
protected boolean removeEldestEntry(Map.Entry<String, CacheRec> last) {
boolean remove = (size() >= limit);
if(remove && (last != null) && (last.getValue() != null)) {
reverselookup.remove(last.getValue().ref);
}
return remove;
}
}
/**
* Create snapshot cache
*/
public GenericChunkCache(int max_size, boolean softref) {
snapcache = new CacheHashMap(max_size);
refqueue = new ReferenceQueue<ChunkCacheRec>();
this.softref = softref;
}
private String getKey(String w, int cx, int cz) {
return w + ":" + cx + ":" + cz;
}
/**
* Invalidate cached snapshot, if in cache
*/
public void invalidateSnapshot(String w, int x, int y, int z) {
String key = getKey(w, x>>4, z>>4);
synchronized(snapcache) {
CacheRec rec = snapcache.remove(key);
if(rec != null) {
snapcache.reverselookup.remove(rec.ref);
rec.ref.clear();
}
}
//processRefQueue();
}
/**
* Invalidate cached snapshot, if in cache
*/
public void invalidateSnapshot(String w, int x0, int y0, int z0, int x1, int y1, int z1) {
for(int xx = (x0>>4); xx <= (x1>>4); xx++) {
for(int zz = (z0>>4); zz <= (z1>>4); zz++) {
String key = getKey(w, xx, zz);
synchronized(snapcache) {
CacheRec rec = snapcache.remove(key);
if(rec != null) {
snapcache.reverselookup.remove(rec.ref);
rec.ref.clear();
}
}
}
}
//processRefQueue();
}
/**
* Look for chunk snapshot in cache
*/
public ChunkCacheRec getSnapshot(String w, int chunkx, int chunkz) {
String key = getKey(w, chunkx, chunkz);
processRefQueue();
ChunkCacheRec ss = null;
CacheRec rec;
synchronized(snapcache) {
rec = snapcache.get(key);
if(rec != null) {
ss = rec.ref.get();
if(ss == null) {
snapcache.reverselookup.remove(rec.ref);
snapcache.remove(key);
}
}
}
cache_attempts++;
if(ss != null) cache_success++;
return ss;
}
/**
* Add chunk snapshot to cache
*/
public void putSnapshot(String w, int chunkx, int chunkz, ChunkCacheRec ss) {
String key = getKey(w, chunkx, chunkz);
processRefQueue();
CacheRec rec = new CacheRec();
if (softref)
rec.ref = new SoftReference<ChunkCacheRec>(ss, refqueue);
else
rec.ref = new WeakReference<ChunkCacheRec>(ss, refqueue);
synchronized(snapcache) {
CacheRec prevrec = snapcache.put(key, rec);
if(prevrec != null) {
snapcache.reverselookup.remove(prevrec.ref);
}
snapcache.reverselookup.put(rec.ref, key);
}
}
/**
* Process reference queue
*/
private void processRefQueue() {
Reference<? extends ChunkCacheRec> ref;
while((ref = refqueue.poll()) != null) {
synchronized(snapcache) {
String k = snapcache.reverselookup.remove(ref);
if(k != null) {
snapcache.remove(k);
}
}
}
}
/**
* Get hit rate (percent)
*/
public double getHitRate() {
if(cache_attempts > 0) {
return (100.0*cache_success)/(double)cache_attempts;
}
return 0.0;
}
/**
* Reset cache stats
*/
public void resetStats() {
cache_attempts = cache_success = 0;
}
/**
* Cleanup
*/
public void cleanup() {
if(snapcache != null) {
snapcache.clear();
snapcache.reverselookup.clear();
snapcache.reverselookup = null;
snapcache = null;
}
}
}

View file

@ -1,7 +1,7 @@
package org.dynmap.common.chunk;
// Generic block location iterator - represents 3D position, but includes fast precomputed chunk and section offsets
public class DynmapChunkPos {
public class GenericChunkPos {
public int x, y, z; // 3D world position
public int cx, cz; // 2D chunk position (x / 16, z / 16)
public int cy; // Vertical section index (Y / 16)
@ -9,7 +9,7 @@ public class DynmapChunkPos {
public int soffset; // Section offset (256 * sy) + (16 * sz) + sx
public int sdiv4offset; // Subsection offset (16 * (sy / 4)) + (4 * (sz / 4)) + (sx / 4) (3D biomes)
public DynmapChunkPos(int x, int y, int z) {
public GenericChunkPos(int x, int y, int z) {
setPos(x, y, z);
}
// Replace X value

View file

@ -6,7 +6,7 @@ import org.dynmap.common.BiomeMap;
import org.dynmap.renderer.DynmapBlockState;
// Generic section: represents 16 x 16 x 16 grid of blocks
public class DynmapChunkSection {
public class GenericChunkSection {
public final BiomeAccess biomes; // Access for biome data
public final BlockStateAccess blocks; // Access for block states
public final LightingAccess sky; // Access for sky light data
@ -16,7 +16,7 @@ public class DynmapChunkSection {
// Block state access interface
public interface BlockStateAccess {
public DynmapBlockState getBlock(int x, int y, int z);
public DynmapBlockState getBlock(DynmapChunkPos pos);
public DynmapBlockState getBlock(GenericChunkPos pos);
}
private static class BlockStateAccess3D implements BlockStateAccess {
private final DynmapBlockState blocks[]; // YZX order
@ -25,9 +25,9 @@ public class DynmapChunkSection {
blocks = bs;
}
public final DynmapBlockState getBlock(int x, int y, int z) {
return blocks[(256 * y) + (16 * z) + x];
return blocks[(256 * (y & 0xF)) + (16 * (z & 0xF)) + (x & 0xF)];
}
public final DynmapBlockState getBlock(DynmapChunkPos pos) {
public final DynmapBlockState getBlock(GenericChunkPos pos) {
return blocks[pos.soffset];
}
}
@ -39,14 +39,14 @@ public class DynmapChunkSection {
public final DynmapBlockState getBlock(int x, int y, int z) {
return block;
}
public final DynmapBlockState getBlock(DynmapChunkPos pos) {
public final DynmapBlockState getBlock(GenericChunkPos pos) {
return block;
}
}
// Biome access interface
public interface BiomeAccess {
public BiomeMap getBiome(int x, int y, int z);
public BiomeMap getBiome(DynmapChunkPos pos);
public BiomeMap getBiome(GenericChunkPos pos);
}
// For classic 2D biome map
private static class BiomeAccess2D implements BiomeAccess {
@ -58,7 +58,7 @@ public class DynmapChunkSection {
public final BiomeMap getBiome(int x, int y, int z) {
return biomes[((z & 0xF) << 4) + (x & 0xF)];
}
public final BiomeMap getBiome(DynmapChunkPos pos) {
public final BiomeMap getBiome(GenericChunkPos pos) {
return biomes[pos.soffset & 0xFF]; // Just ZX portion
}
}
@ -72,7 +72,7 @@ public class DynmapChunkSection {
public final BiomeMap getBiome(int x, int y, int z) {
return biomes[ ((y & 0xC) << 2) | (z & 0xC) | ((x & 0xC) >> 2) ];
}
public final BiomeMap getBiome(DynmapChunkPos pos) {
public final BiomeMap getBiome(GenericChunkPos pos) {
return biomes[pos.sdiv4offset];
}
}
@ -85,14 +85,14 @@ public class DynmapChunkSection {
public final BiomeMap getBiome(int x, int y, int z) {
return biome;
}
public final BiomeMap getBiome(DynmapChunkPos pos) {
public final BiomeMap getBiome(GenericChunkPos pos) {
return biome;
}
}
// Lighting access interface
public interface LightingAccess {
public int getLight(int x, int y, int z);
public int getLight(DynmapChunkPos pos);
public int getLight(GenericChunkPos pos);
}
private static class LightingAccess3D implements LightingAccess {
private final long[] light; // Nibble array (16 * y) * z (nibble at << (4*x))
@ -107,11 +107,10 @@ public class DynmapChunkSection {
}
}
public final int getLight(int x, int y, int z) {
return (int)(light[(16 * (y & 0xF)) + (z & 0xF)] >> (4 * (x & 0xF)) & 0xFL);
return 0xF & (int)(light[(16 * (y & 0xF)) + (z & 0xF)] >> (4 * (x & 0xF)));
}
public final int getLight(DynmapChunkPos pos) {
return (int)(light[pos.soffset >> 4] >> (4 * pos.sx));
public final int getLight(GenericChunkPos pos) {
return 0xF & (int)(light[pos.soffset >> 4] >> (4 * pos.sx));
}
}
private static class LightingAccessSingle implements LightingAccess {
@ -122,11 +121,11 @@ public class DynmapChunkSection {
public final int getLight(int x, int y, int z) {
return light;
}
public final int getLight(DynmapChunkPos pos) {
public final int getLight(GenericChunkPos pos) {
return light;
}
}
private DynmapChunkSection(BlockStateAccess blks, BiomeAccess bio, LightingAccess skyac, LightingAccess emitac, boolean empty) {
private GenericChunkSection(BlockStateAccess blks, BiomeAccess bio, LightingAccess skyac, LightingAccess emitac, boolean empty) {
blocks = blks;
biomes = bio;
sky = skyac;
@ -135,11 +134,10 @@ public class DynmapChunkSection {
}
private static BiomeAccess defaultBiome = new BiomeAccessSingle(BiomeMap.NULL);
private static BlockStateAccess defaultBlockState = new BlockStateAccessSingle(DynmapBlockState.AIR);
private static LightingAccess defaultSky = new LightingAccessSingle(15);
private static LightingAccess defaultEmit = new LightingAccessSingle(0);
private static LightingAccess defaultLight = new LightingAccessSingle(0);
// Shared default empty section
public static final DynmapChunkSection EMPTY = new DynmapChunkSection(defaultBlockState, defaultBiome, defaultSky, defaultEmit, true);
public static final GenericChunkSection EMPTY = new GenericChunkSection(defaultBlockState, defaultBiome, new LightingAccessSingle(15), defaultLight, true);
// Factory for building section
public static class Builder {
@ -160,8 +158,8 @@ public class DynmapChunkSection {
bsaccum = null;
baaccumsingle = BiomeMap.NULL;
baaccum = null;
sk = defaultSky;
em = defaultEmit;
sk = defaultLight;
em = defaultLight;
empty = true;
}
// Set sky lighting to single value
@ -201,13 +199,13 @@ public class DynmapChunkSection {
return this;
}
// Set bipme to 3D style
public Builder xyzBiome(int x, int y, int z, BiomeMap bio) {
public Builder xyzBiome(int xdiv4, int ydiv4, int zdiv4, BiomeMap bio) {
if ((baaccum == null) || (baaccum.length != 64)) {
baaccum = new BiomeMap[64];
Arrays.fill(baaccum, BiomeMap.NULL);
baaccumsingle = BiomeMap.NULL;
}
baaccum[((y & 0xC) << 4) + (z & 0xC) + ((x & 0xC) >> 2)] = bio;
baaccum[((ydiv4 & 0x3) << 4) + ((zdiv4 & 0x3) << 2) + (xdiv4 & 0x3)] = bio;
return this;
}
// Set block state to single value
@ -229,7 +227,7 @@ public class DynmapChunkSection {
return this;
}
// Build section based on current builder state
public DynmapChunkSection build() {
public GenericChunkSection build() {
// Process state access - see if we can reduce
if (bsaccum != null) {
DynmapBlockState v = bsaccum[0]; // Get first
@ -291,7 +289,7 @@ public class DynmapChunkSection {
ba = new BiomeAccessSingle(baaccumsingle);
baaccumsingle = BiomeMap.NULL;
}
return new DynmapChunkSection(bs, ba, sk, em, empty);
return new GenericChunkSection(bs, ba, sk, em, empty);
}
}
}

File diff suppressed because it is too large Load diff