mirror of
https://github.com/GTNewHorizons/NotEnoughItems.git
synced 2026-11-18 11:29:35 +00:00
Use IWorldAccess to get instant updates
This commit is contained in:
1 parent
d07059cb5a
commit
d6789c44d4
2 files changed
+190
-70
No files matched your search
@@ -1,7 +1,11 @@
|
||||
package codechicken.nei;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.EnumCreatureType;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.world.IWorldAccess;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.biome.BiomeGenBase;
|
||||
import net.minecraft.world.chunk.Chunk;
|
||||
@@ -10,8 +14,8 @@ import codechicken.lib.math.MathHelper;
|
||||
|
||||
/**
|
||||
* The mob spawn modes of the blocks around the player, scanned a slice per tick into a back buffer. The buffers swap
|
||||
* when a pass is done. The modes come from {@link WorldOverlayRenderer#getSpawnMode}: 0 for no spawns, 1 for night only
|
||||
* and 2 for any time.
|
||||
* when a pass is done. The column of a block or light change is rescanned on the next tick. The modes come from
|
||||
* {@link WorldOverlayRenderer#getSpawnMode}: 0 for no spawns, 1 for night only and 2 for any time.
|
||||
*/
|
||||
public final class MobSpawnScan {
|
||||
|
||||
@@ -23,42 +27,35 @@ public final class MobSpawnScan {
|
||||
public static final int HEIGHT = 2 * VERTICAL_RADIUS + 1;
|
||||
private static final int REFRESH_TICKS = 10;
|
||||
|
||||
private final IWorldAccess changeListener = new ChangeListener();
|
||||
private int radius;
|
||||
private int span;
|
||||
// front buffer: `modes[column * HEIGHT + k]`, valid for `k < columnEnds[column]`
|
||||
private byte[] modes = new byte[0];
|
||||
private int[] columnEnds = new int[0];
|
||||
private int minX;
|
||||
private int minY;
|
||||
private int minZ;
|
||||
// back buffer
|
||||
private byte[] nextModes = new byte[0];
|
||||
private int[] nextColumnEnds = new int[0];
|
||||
private int nextMinX;
|
||||
private int nextMinY;
|
||||
private int nextMinZ;
|
||||
private Buffer front = new Buffer();
|
||||
private Buffer back = new Buffer();
|
||||
// columns of `back` below this index are scanned in the pass in progress
|
||||
private int nextColumn;
|
||||
private int passCount;
|
||||
private boolean hasDirtyColumns;
|
||||
private int changeCount;
|
||||
private boolean hasModes;
|
||||
private long scannedTick;
|
||||
// world of the front buffer
|
||||
// world of `front`, with `changeListener` added
|
||||
private World scannedWorld;
|
||||
|
||||
/** Free the buffers. The next {@link #update} does a full scan. */
|
||||
public void clear() {
|
||||
if (scannedWorld != null) scannedWorld.removeWorldAccess(changeListener);
|
||||
radius = 0;
|
||||
span = 0;
|
||||
modes = new byte[0];
|
||||
columnEnds = new int[0];
|
||||
nextModes = new byte[0];
|
||||
nextColumnEnds = new int[0];
|
||||
front = new Buffer();
|
||||
back = new Buffer();
|
||||
hasModes = false;
|
||||
scannedWorld = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scan the next slice of columns, at most once per tick. After {@link #clear}, a radius change or a world change,
|
||||
* scan every column at once. {@code newRadius} is clamped between {@link #MIN_RADIUS} and {@link #MAX_RADIUS}.
|
||||
* Rescan the columns of changed blocks, and scan the next slice of columns, at most once per tick. After
|
||||
* {@link #clear}, a radius change or a world change, scan every column at once. {@code newRadius} is clamped
|
||||
* between {@link #MIN_RADIUS} and {@link #MAX_RADIUS}.
|
||||
*/
|
||||
public void update(Entity entity, int newRadius) {
|
||||
final World world = entity.worldObj;
|
||||
@@ -73,10 +70,11 @@ public final class MobSpawnScan {
|
||||
if (worldTicks == scannedTick) return;
|
||||
|
||||
scannedTick = worldTicks;
|
||||
if (hasDirtyColumns) rescanDirtyColumns(world);
|
||||
final int columnCount = span * span;
|
||||
final int sliceEnd = Math.min(columnCount, nextColumn + (columnCount + REFRESH_TICKS - 1) / REFRESH_TICKS);
|
||||
for (; nextColumn < sliceEnd; nextColumn++) {
|
||||
scanColumn(world, nextColumn);
|
||||
back.scanColumn(world, nextColumn, span);
|
||||
}
|
||||
if (nextColumn == columnCount) {
|
||||
swapBuffers();
|
||||
@@ -84,9 +82,9 @@ public final class MobSpawnScan {
|
||||
}
|
||||
}
|
||||
|
||||
/** Changes each time the front buffer changes. */
|
||||
public int getPassCount() {
|
||||
return passCount;
|
||||
/** Changes each time the modes of {@link #readSpawnMode} change. */
|
||||
public int getChangeCount() {
|
||||
return changeCount;
|
||||
}
|
||||
|
||||
public int getSpan() {
|
||||
@@ -94,46 +92,50 @@ public final class MobSpawnScan {
|
||||
}
|
||||
|
||||
public int getMinX() {
|
||||
return minX;
|
||||
return front.minX;
|
||||
}
|
||||
|
||||
public int getMinY() {
|
||||
return minY;
|
||||
return front.minY;
|
||||
}
|
||||
|
||||
public int getMinZ() {
|
||||
return minZ;
|
||||
return front.minZ;
|
||||
}
|
||||
|
||||
public int getColumnEnd(int column) {
|
||||
return columnEnds[column];
|
||||
return front.columnEnds[column];
|
||||
}
|
||||
|
||||
/** @return 0 at or above {@link #getColumnEnd} */
|
||||
public byte readSpawnMode(int column, int k) {
|
||||
return k < columnEnds[column] ? modes[column * HEIGHT + k] : 0;
|
||||
return k < front.columnEnds[column] ? front.modes[column * HEIGHT + k] : 0;
|
||||
}
|
||||
|
||||
private void resize(int newRadius) {
|
||||
radius = newRadius;
|
||||
span = 2 * newRadius + 1;
|
||||
modes = new byte[span * span * HEIGHT];
|
||||
columnEnds = new int[span * span];
|
||||
nextModes = new byte[span * span * HEIGHT];
|
||||
nextColumnEnds = new int[span * span];
|
||||
front.allocate(span * span);
|
||||
back.allocate(span * span);
|
||||
hasModes = false;
|
||||
}
|
||||
|
||||
private void scanFullPass(Entity entity) {
|
||||
final long startNanos = System.nanoTime();
|
||||
final World world = entity.worldObj;
|
||||
if (world != scannedWorld) {
|
||||
if (scannedWorld != null) scannedWorld.removeWorldAccess(changeListener);
|
||||
world.addWorldAccess(changeListener);
|
||||
scannedWorld = world;
|
||||
}
|
||||
|
||||
startPass(entity);
|
||||
for (; nextColumn < span * span; nextColumn++) {
|
||||
scanColumn(entity.worldObj, nextColumn);
|
||||
back.scanColumn(world, nextColumn, span);
|
||||
}
|
||||
swapBuffers();
|
||||
hasModes = true;
|
||||
scannedWorld = entity.worldObj;
|
||||
scannedTick = entity.worldObj.getTotalWorldTime();
|
||||
scannedTick = world.getTotalWorldTime();
|
||||
NEIClientConfig.logger.info(
|
||||
"moboverlay.scan.full radius={} columns={} micros={}",
|
||||
radius,
|
||||
@@ -145,41 +147,159 @@ public final class MobSpawnScan {
|
||||
private void startPass(Entity entity) {
|
||||
final int centreY = (int) MathHelper
|
||||
.clip(entity.posY, VERTICAL_RADIUS, entity.worldObj.getHeight() - VERTICAL_RADIUS);
|
||||
nextMinX = (int) entity.posX - radius;
|
||||
nextMinY = centreY - VERTICAL_RADIUS;
|
||||
nextMinZ = (int) entity.posZ - radius;
|
||||
back.minX = (int) entity.posX - radius;
|
||||
back.minY = centreY - VERTICAL_RADIUS;
|
||||
back.minZ = (int) entity.posZ - radius;
|
||||
Arrays.fill(back.dirty, false);
|
||||
nextColumn = 0;
|
||||
}
|
||||
|
||||
private void scanColumn(World world, int column) {
|
||||
final int x = nextMinX + column / span;
|
||||
final int z = nextMinZ + column % span;
|
||||
final BiomeGenBase biome = world.getBiomeGenForCoords(x, z);
|
||||
if (biome.getSpawnableList(EnumCreatureType.monster).isEmpty() || biome.getSpawningChance() <= 0) {
|
||||
nextColumnEnds[column] = 0;
|
||||
return;
|
||||
private void rescanDirtyColumns(World world) {
|
||||
hasDirtyColumns = false;
|
||||
for (int column = 0; column < span * span; column++) {
|
||||
if (front.dirty[column]) {
|
||||
front.scanColumn(world, column, span);
|
||||
front.dirty[column] = false;
|
||||
changeCount++;
|
||||
}
|
||||
if (back.dirty[column]) {
|
||||
back.scanColumn(world, column, span);
|
||||
back.dirty[column] = false;
|
||||
}
|
||||
}
|
||||
|
||||
final Chunk chunk = world.getChunkFromBlockCoords(x, z);
|
||||
final int topY = Math.min(world.getHeight() - 1, world.getHeightValue(x, z));
|
||||
int end = 0;
|
||||
while (end < HEIGHT && nextMinY + end <= topY) {
|
||||
nextModes[column * HEIGHT + end] = WorldOverlayRenderer.getSpawnMode(chunk, x, nextMinY + end, z);
|
||||
end++;
|
||||
}
|
||||
nextColumnEnds[column] = end;
|
||||
}
|
||||
|
||||
private void swapBuffers() {
|
||||
final byte[] scannedModes = nextModes;
|
||||
nextModes = modes;
|
||||
modes = scannedModes;
|
||||
final int[] scannedEnds = nextColumnEnds;
|
||||
nextColumnEnds = columnEnds;
|
||||
columnEnds = scannedEnds;
|
||||
minX = nextMinX;
|
||||
minY = nextMinY;
|
||||
minZ = nextMinZ;
|
||||
passCount++;
|
||||
final Buffer scanned = back;
|
||||
back = front;
|
||||
front = scanned;
|
||||
changeCount++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark the column of a changed block. A block's spawn mode depends on the block below and the block above, so a
|
||||
* change 1 block below or above the scanned height counts too.
|
||||
*/
|
||||
private void markChanged(int x, int y, int z) {
|
||||
final int lowestY = Math.min(front.minY, back.minY) - 1;
|
||||
final int highestY = Math.max(front.minY, back.minY) + HEIGHT;
|
||||
if (y >= lowestY && y <= highestY) markColumn(x, z);
|
||||
}
|
||||
|
||||
private void markColumn(int x, int z) {
|
||||
final int frontColumn = front.findColumn(x, z, span);
|
||||
if (frontColumn >= 0) {
|
||||
front.dirty[frontColumn] = true;
|
||||
hasDirtyColumns = true;
|
||||
}
|
||||
// a column at or above `nextColumn` is scanned later in this pass
|
||||
final int backColumn = back.findColumn(x, z, span);
|
||||
if (backColumn >= 0 && backColumn < nextColumn) {
|
||||
back.dirty[backColumn] = true;
|
||||
hasDirtyColumns = true;
|
||||
}
|
||||
}
|
||||
|
||||
/** The spawn modes of one square of columns. */
|
||||
private static final class Buffer {
|
||||
|
||||
// `modes[column * HEIGHT + k]`, valid for `k < columnEnds[column]`
|
||||
private byte[] modes = new byte[0];
|
||||
private int[] columnEnds = new int[0];
|
||||
private boolean[] dirty = new boolean[0];
|
||||
private int minX;
|
||||
private int minY;
|
||||
private int minZ;
|
||||
|
||||
private void allocate(int columnCount) {
|
||||
modes = new byte[columnCount * HEIGHT];
|
||||
columnEnds = new int[columnCount];
|
||||
dirty = new boolean[columnCount];
|
||||
}
|
||||
|
||||
/** @return -1 outside this buffer's square */
|
||||
private int findColumn(int x, int z, int span) {
|
||||
final int i = x - minX;
|
||||
final int j = z - minZ;
|
||||
return i >= 0 && i < span && j >= 0 && j < span ? i * span + j : -1;
|
||||
}
|
||||
|
||||
private void scanColumn(World world, int column, int span) {
|
||||
final int x = minX + column / span;
|
||||
final int z = minZ + column % span;
|
||||
final BiomeGenBase biome = world.getBiomeGenForCoords(x, z);
|
||||
if (biome.getSpawnableList(EnumCreatureType.monster).isEmpty() || biome.getSpawningChance() <= 0) {
|
||||
columnEnds[column] = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
final Chunk chunk = world.getChunkFromBlockCoords(x, z);
|
||||
final int topY = Math.min(world.getHeight() - 1, world.getHeightValue(x, z));
|
||||
int end = 0;
|
||||
while (end < HEIGHT && minY + end <= topY) {
|
||||
modes[column * HEIGHT + end] = WorldOverlayRenderer.getSpawnMode(chunk, x, minY + end, z);
|
||||
end++;
|
||||
}
|
||||
columnEnds[column] = end;
|
||||
}
|
||||
}
|
||||
|
||||
/** Marks the columns of the block and light changes in {@link #scannedWorld}. */
|
||||
private final class ChangeListener implements IWorldAccess {
|
||||
|
||||
@Override
|
||||
public void markBlockForUpdate(int x, int y, int z) {
|
||||
markChanged(x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markBlockForRenderUpdate(int x, int y, int z) {
|
||||
markChanged(x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markBlockRangeForRenderUpdate(int fromX, int fromY, int fromZ, int toX, int toY, int toZ) {
|
||||
final int startX = Math.max(fromX, Math.min(front.minX, back.minX));
|
||||
final int endX = Math.min(toX, Math.max(front.minX, back.minX) + span - 1);
|
||||
final int startZ = Math.max(fromZ, Math.min(front.minZ, back.minZ));
|
||||
final int endZ = Math.min(toZ, Math.max(front.minZ, back.minZ) + span - 1);
|
||||
for (int x = startX; x <= endX; x++) {
|
||||
for (int z = startZ; z <= endZ; z++) {
|
||||
markColumn(x, z);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playSound(String soundName, double x, double y, double z, float volume, float pitch) {}
|
||||
|
||||
@Override
|
||||
public void playSoundToNearExcept(EntityPlayer player, String soundName, double x, double y, double z,
|
||||
float volume, float pitch) {}
|
||||
|
||||
@Override
|
||||
public void spawnParticle(String particleName, double x, double y, double z, double velocityX,
|
||||
double velocityY, double velocityZ) {}
|
||||
|
||||
@Override
|
||||
public void onEntityCreate(Entity entity) {}
|
||||
|
||||
@Override
|
||||
public void onEntityDestroy(Entity entity) {}
|
||||
|
||||
@Override
|
||||
public void playRecord(String recordName, int x, int y, int z) {}
|
||||
|
||||
@Override
|
||||
public void broadcastSound(int soundId, int x, int y, int z, int data) {}
|
||||
|
||||
@Override
|
||||
public void playAuxSFX(EntityPlayer player, int sfxType, int x, int y, int z, int data) {}
|
||||
|
||||
@Override
|
||||
public void destroyBlockPartially(int breakerId, int x, int y, int z, int progress) {}
|
||||
|
||||
@Override
|
||||
public void onStaticEntitiesChanged() {}
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,7 @@ public class WorldOverlayRenderer implements IKeyStateTracker {
|
||||
private static final MobSpawnMarkedFace markedFace = new MobSpawnMarkedFace();
|
||||
// display list of the overlay quads, compiled when the scan or the style changes
|
||||
private static int mobSpawnList = -1;
|
||||
private static int mobSpawnListPass;
|
||||
private static int mobSpawnListChange;
|
||||
private static MobSpawnOverlayStyle mobSpawnListStyle;
|
||||
|
||||
public static void reset() {
|
||||
@@ -105,7 +105,7 @@ public class WorldOverlayRenderer implements IKeyStateTracker {
|
||||
mobSpawnScan.update(entity, NEIClientConfig.getIntSetting("world.overlays.mobSpawnRadius"));
|
||||
final MobSpawnOverlayStyle style = MobSpawnOverlayStyle
|
||||
.parseStyle(NEIClientConfig.getSetting("world.overlays.mobSpawnStyle").getValue());
|
||||
if (!(mobSpawnList >= 0 && mobSpawnListPass == mobSpawnScan.getPassCount() && mobSpawnListStyle == style)) {
|
||||
if (!(mobSpawnList >= 0 && mobSpawnListChange == mobSpawnScan.getChangeCount() && mobSpawnListStyle == style)) {
|
||||
compileMobSpawnList(entity.worldObj, style);
|
||||
}
|
||||
|
||||
@@ -126,7 +126,7 @@ public class WorldOverlayRenderer implements IKeyStateTracker {
|
||||
*/
|
||||
private static void compileMobSpawnList(World world, MobSpawnOverlayStyle style) {
|
||||
if (mobSpawnList < 0) mobSpawnList = GLAllocation.generateDisplayLists(1);
|
||||
mobSpawnListPass = mobSpawnScan.getPassCount();
|
||||
mobSpawnListChange = mobSpawnScan.getChangeCount();
|
||||
mobSpawnListStyle = style;
|
||||
|
||||
GL11.glNewList(mobSpawnList, GL11.GL_COMPILE);
|
||||
|
||||
Reference in new issue
Block a user