Compare commits

...
7 changed files with 299 additions and 101 deletions

No files matched your search

@@ -713,6 +713,8 @@ public class LayoutManager implements IContainerInputHandler, IContainerTooltipH
@Override
public void load(GuiContainer gui) {
NEIController.load(gui);
if (isEnabled()) {
setInputFocused(null);
@@ -726,8 +728,6 @@ public class LayoutManager implements IContainerInputHandler, IContainerTooltipH
AutoFocusWidget.instance.load(gui);
}
NEIController.load(gui);
if (checkCreativeInv(gui) && gui.mc.currentScreen instanceof GuiContainerCreative) // override creative with
// creative+
gui.mc.displayGuiScreen(null); // close the screen and wait for the server to open it for us
@@ -30,6 +30,7 @@ public class WorldOverlayRenderer implements IKeyStateTracker {
private static final MobOverlayScan MOB_OVERLAY_SCAN = new MobOverlayScan();
private static final MobOverlayFace MARKED_FACE = new MobOverlayFace();
private static final MobOverlayDisplayList MOB_OVERLAY_LIST = new MobOverlayDisplayList();
private static final byte[] COLUMN_CELLS = new byte[2 * MobOverlayScan.MAX_VERTICAL_RADIUS + 1];
public static void reset() {
mobOverlay = 0;
@@ -83,10 +84,6 @@ public class WorldOverlayRenderer implements IKeyStateTracker {
GL11.glPopAttrib();
}
/**
* The model view matrix is relative to ({@code intOffsetX}, {@code intOffsetY}, {@code intOffsetZ}), the camera
* position cast to {@code int}.
*/
private static void renderMobSpawnOverlay(Entity entity, int intOffsetX, int intOffsetY, int intOffsetZ) {
if (mobOverlay == 0) {
MOB_OVERLAY_SCAN.clear();
@@ -110,29 +107,49 @@ public class WorldOverlayRenderer implements IKeyStateTracker {
endMobOverlayDraw();
}
/**
* Add a marked face to {@link #MOB_OVERLAY_LIST} for each block of {@link #MOB_OVERLAY_SCAN} where mobs can spawn.
*/
private static void compileMobOverlayList(MobOverlayStyle style, long worldTicks) {
MOB_OVERLAY_LIST.beginCompile(MOB_OVERLAY_SCAN, style, worldTicks);
for (int chunkX = MOB_OVERLAY_SCAN.getMinChunkX(); chunkX <= MOB_OVERLAY_SCAN.getMaxChunkX(); chunkX++) {
for (int chunkZ = MOB_OVERLAY_SCAN.getMinChunkZ(); chunkZ <= MOB_OVERLAY_SCAN.getMaxChunkZ(); chunkZ++) {
if (!MOB_OVERLAY_LIST.beginChunk(MOB_OVERLAY_SCAN, chunkX, chunkZ)) continue;
MOB_OVERLAY_LIST.endChunk(compileChunkFaces(style, chunkX, chunkZ));
}
}
MOB_OVERLAY_LIST.endCompile(MOB_OVERLAY_SCAN);
}
private static boolean compileChunkFaces(MobOverlayStyle style, int chunkX, int chunkZ) {
final int CHUNK_WIDTH = 1 << MobOverlayScan.CHUNK_SHIFT;
final float alpha = style.computeAlpha();
final Tessellator tess = Tessellator.instance;
tess.startDrawing(style.glMode);
final int cornerX = chunkX << MobOverlayScan.CHUNK_SHIFT;
final int cornerZ = chunkZ << MobOverlayScan.CHUNK_SHIFT;
final int minX = MOB_OVERLAY_SCAN.getMinX();
final int minZ = MOB_OVERLAY_SCAN.getMinZ();
final int span = MOB_OVERLAY_SCAN.getSpan();
for (int i = 0; i < span; i++) {
for (int j = 0; j < span; j++) {
for (int k = 0; k < MOB_OVERLAY_SCAN.getHeight(); k++) {
if (MOB_OVERLAY_SCAN.readSpawnMode(i, k, j) == 0) continue;
final int height = MOB_OVERLAY_SCAN.getHeight();
boolean hasFaces = false;
tess.startDrawing(style.glMode);
for (int i = Math.max(cornerX - minX, 0); i < Math.min(cornerX + CHUNK_WIDTH - minX, span); i++) {
for (int j = Math.max(cornerZ - minZ, 0); j < Math.min(cornerZ + CHUNK_WIDTH - minZ, span); j++) {
MOB_OVERLAY_SCAN.readColumn(i, j, COLUMN_CELLS);
for (int k = 0; k < height; k++) {
if (MobOverlayScan.extractSpawnMode(COLUMN_CELLS[k]) == 0) continue;
MARKED_FACE.moveTo(i, k + MOB_OVERLAY_SCAN.readOverlayYOffset(i, k, j), j);
MARKED_FACE.moveTo(
minX + i - cornerX,
k + MobOverlayScan.computeOverlayYOffset(COLUMN_CELLS[k]),
minZ + j - cornerZ);
loadNeighbourModes(i, k, j);
MARKED_FACE.applyColor(style.colouring, alpha);
style.addVertices(MARKED_FACE);
hasFaces = true;
}
}
}
tess.draw();
MOB_OVERLAY_LIST.endCompile();
return hasFaces;
}
private static void beginMobOverlayDraw(MobOverlayStyle style) {
@@ -147,7 +164,7 @@ public class WorldOverlayRenderer implements IKeyStateTracker {
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glLineWidth(1.5F);
// the outline of `MobOverlayStyle.CONNECTED` lies in the plane of the tint. With depth writes on, the depth
// the outline of `MobOverlayStyle.CONNECTED` lies in the plane of the tint. When depth writes are on, the depth
// test discards most of the outline
GL11.glDepthMask(false);
}
@@ -1,65 +1,117 @@
package codechicken.nei.moboverlay;
import java.util.Arrays;
import net.minecraft.client.renderer.GLAllocation;
import org.lwjgl.opengl.GL11;
/** The mob spawn overlay quads, compiled into a GL display list. */
/**
* {@code MobOverlayDisplayList} compiles the overlay quads of each chunk column of a {@link MobOverlayScan} box into
* one GL display list. A compile rebuilds only the lists whose {@link MobOverlayScan#readChunkVersion} changed.
*/
public final class MobOverlayDisplayList {
private int listId = -1;
private static final int NO_LIST = -1;
private int firstListId = NO_LIST;
private int parentListId = NO_LIST;
private int anchorX;
private int anchorY;
private int anchorZ;
private int chunkSpan;
private boolean[] isCompiled = new boolean[0];
private boolean[] hasFaces = new boolean[0];
private int[] compiledChunkVersion = new int[0];
private int modesVersion;
private MobOverlayStyle style;
private int minX;
private int minY;
private int minZ;
private long compiledTicks;
private int compilingChunk;
/**
* Compiles after a {@link MobOverlayScan#getModesVersion} change are debounced to one per {@code MIN_COMPILE_TICKS}
* ticks, because the version changes several times per second while the player moves fast, and a compile runs for a
* millisecond or more.
*/
public boolean isCompileDue(int currentModesVersion, MobOverlayStyle currentStyle, long worldTicks) {
final int MIN_COMPILE_TICKS = 5;
if (listId < 0 || style != currentStyle) return true;
if (firstListId == NO_LIST || style != currentStyle) return true;
// after a dimension change or a server switch, `worldTicks` can be lower than `compiledTicks`
final boolean isWaitOver = worldTicks - compiledTicks >= MIN_COMPILE_TICKS || worldTicks < compiledTicks;
return modesVersion != currentModesVersion && isWaitOver;
}
/**
* Quad positions are relative to block ({@link MobOverlayScan#getMinX}, {@link MobOverlayScan#getMinY},
* {@link MobOverlayScan#getMinZ}) of {@code scan}.
*/
public void beginCompile(MobOverlayScan scan, MobOverlayStyle newStyle, long worldTicks) {
if (listId < 0) listId = GLAllocation.generateDisplayLists(1);
if (chunkSpan != scan.getChunkSpan()) resize(scan.getChunkSpan());
if (style != newStyle) Arrays.fill(isCompiled, false);
modesVersion = scan.getModesVersion();
style = newStyle;
minX = scan.getMinX();
minY = scan.getMinY();
minZ = scan.getMinZ();
compiledTicks = worldTicks;
GL11.glNewList(listId, GL11.GL_COMPILE);
anchorX = scan.getMinX();
anchorY = scan.getMinY();
anchorZ = scan.getMinZ();
}
public void endCompile() {
public boolean beginChunk(MobOverlayScan scan, int chunkX, int chunkZ) {
final int chunk = scan.findChunk(chunkX, chunkZ);
final int chunkVersion = scan.readChunkVersion(chunkX, chunkZ);
if (isCompiled[chunk] && compiledChunkVersion[chunk] == chunkVersion) return false;
isCompiled[chunk] = true;
compiledChunkVersion[chunk] = chunkVersion;
compilingChunk = chunk;
GL11.glNewList(firstListId + chunk, GL11.GL_COMPILE);
return true;
}
public void endChunk(boolean hasChunkFaces) {
GL11.glEndList();
hasFaces[compilingChunk] = hasChunkFaces;
}
public void endCompile(MobOverlayScan scan) {
GL11.glNewList(parentListId, GL11.GL_COMPILE);
for (int chunkX = scan.getMinChunkX(); chunkX <= scan.getMaxChunkX(); chunkX++) {
for (int chunkZ = scan.getMinChunkZ(); chunkZ <= scan.getMaxChunkZ(); chunkZ++) {
final int chunk = scan.findChunk(chunkX, chunkZ);
if (!hasFaces[chunk]) continue;
GL11.glPushMatrix();
GL11.glTranslated(
(chunkX << MobOverlayScan.CHUNK_SHIFT) - anchorX,
0,
(chunkZ << MobOverlayScan.CHUNK_SHIFT) - anchorZ);
GL11.glCallList(firstListId + chunk);
GL11.glPopMatrix();
}
}
GL11.glEndList();
}
/** The model view matrix is relative to block ({@code cameraX}, {@code cameraY}, {@code cameraZ}). */
public void draw(int cameraX, int cameraY, int cameraZ) {
GL11.glPushMatrix();
GL11.glTranslated(minX - cameraX, minY - cameraY, minZ - cameraZ);
GL11.glCallList(listId);
GL11.glTranslated(anchorX - cameraX, anchorY - cameraY, anchorZ - cameraZ);
GL11.glCallList(parentListId);
GL11.glPopMatrix();
}
public void delete() {
if (listId < 0) return;
private void resize(int newChunkSpan) {
final int chunkCount = newChunkSpan * newChunkSpan;
delete();
GLAllocation.deleteDisplayLists(listId);
listId = -1;
chunkSpan = newChunkSpan;
firstListId = GLAllocation.generateDisplayLists(chunkCount + 1);
parentListId = firstListId + chunkCount;
isCompiled = new boolean[chunkCount];
hasFaces = new boolean[chunkCount];
compiledChunkVersion = new int[chunkCount];
}
public void delete() {
if (firstListId == NO_LIST) return;
GLAllocation.deleteDisplayLists(firstListId);
firstListId = NO_LIST;
parentListId = NO_LIST;
chunkSpan = 0;
Arrays.fill(isCompiled, false);
}
}
@@ -2,7 +2,7 @@ package codechicken.nei.moboverlay;
import net.minecraft.client.renderer.Tessellator;
/** Vertex builder for one block's top face in the mob spawn overlay. */
/** {@code MobOverlayFace} adds the vertices of the mark on one block's top face to {@link Tessellator#instance}. */
public final class MobOverlayFace {
public static final byte OUTSIDE_SCAN = -2;
@@ -36,7 +36,7 @@ public final class MobOverlayFace {
public boolean isJoined(int offsetX, int offsetZ) {
final byte neighbourMode = neighbourModes[indexNeighbour(offsetX, offsetZ)];
// no outline along the edge of the scan
// `MobOverlayStyle.CONNECTED` doesn't outline the edge of the scan
return neighbourMode == neighbourModes[indexNeighbour(0, 0)] || neighbourMode == OUTSIDE_SCAN;
}
@@ -67,7 +67,6 @@ public final class MobOverlayFace {
addTexturedVertex(1, 0);
}
/** @param offsetX 0 at the west edge, 1 at the east edge */
public void addVertex(double offsetX, double offsetZ) {
Tessellator.instance.addVertex(x + offsetX, y, z + offsetZ);
}
@@ -1,5 +1,7 @@
package codechicken.nei.moboverlay;
import java.util.Arrays;
import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EnumCreatureType;
@@ -16,11 +18,8 @@ import net.minecraft.world.chunk.Chunk;
import codechicken.lib.math.MathHelper;
/**
* Spawn modes, and the tops of flat blocks such as carpets, in a box around the player. The spawn modes are from
* {@link Column#getSpawnMode}: 0 for no spawns, 1 for night only and 2 for any time. The box moves with the player once
* the player is a few blocks from the box's centre, and {@link #update} scans only the blocks that enter the box.
* {@link #update} rescans the column of a block or light change on the next tick. On each tick, {@link #update} also
* rescans a slice of the columns, for changes with no block update, such as a boat that moves into the box.
* Spawn modes of the blocks in a box around the player. {@link #update} scans the blocks that enter the box, the
* columns of block and light changes, and a slice of the box on each tick.
*/
public final class MobOverlayScan {
@@ -31,6 +30,7 @@ public final class MobOverlayScan {
public static final int MIN_VERTICAL_RADIUS = 4;
public static final int MAX_VERTICAL_RADIUS = 64;
public static final int DEFAULT_VERTICAL_RADIUS = 16;
public static final int CHUNK_SHIFT = 4;
private static final int RECENTRE_DISTANCE = 4;
private static final int VERTICAL_RECENTRE_DISTANCE = 2;
private static final int RESCAN_TICKS = 40;
@@ -47,18 +47,21 @@ public final class MobOverlayScan {
// a ring buffer over the box, indexed by `findCell()`. A cell has the spawn mode in the bits of `SPAWN_MODE_MASK`,
// and `Column.readCoverTop()` shifted left by `COVER_TOP_SHIFT`
private byte[] modes = new byte[0];
// indexed by `findColumn(x, z)`
private boolean[] dirty = new boolean[0];
private boolean hasDirtyColumns;
private int minX;
private int minY;
private int minZ;
// the slice rescan of `update()` continues from this column, numbered `(x - minX) * span + (z - minZ)`
private int nextRescanColumn;
private int modesVersion;
private int chunkSpan;
// The version of a chunk column changes when a cell in or beside the chunk column changes, and when an edge of the
// box moves into, out of, within or across the chunk column. A full scan and a vertical move change the version of
// every chunk column
private int[] chunkVersions = new int[0];
private int chunkVersionCount;
private boolean hasModes;
private long scannedTick;
// world of `modes`, with `changeListener` added
private World scannedWorld;
public void clear() {
@@ -71,17 +74,17 @@ public final class MobOverlayScan {
height = 0;
modes = new byte[0];
dirty = new boolean[0];
chunkSpan = 0;
chunkVersions = new int[0];
hasModes = false;
scannedWorld = null;
// `Chunk.worldObj` references the world
// a world isn't garbage-collected while `column.chunk` references a chunk of the world
column.chunk = null;
}
/**
* Scan every column in this call after {@link #clear}, a change of either radius, a world change, or a move of
* {@link #getSpan} blocks or more across or {@link #getHeight} blocks or more up or down. {@code newRadius} is
* clamped between {@link #MIN_RADIUS} and {@link #MAX_RADIUS}, and {@code newVerticalRadius} between
* {@link #MIN_VERTICAL_RADIUS} and {@link #MAX_VERTICAL_RADIUS}.
* {@link #getSpan} blocks or more across or {@link #getHeight} blocks or more up or down.
*/
public void update(Entity entity, int newRadius, int newVerticalRadius) {
final World world = entity.worldObj;
@@ -112,11 +115,14 @@ public final class MobOverlayScan {
if (isChanged) modesVersion++;
}
/** The version changes when a spawn mode or the top of a flat block changes, or the box moves. */
public int getModesVersion() {
return modesVersion;
}
public int getChunkSpan() {
return chunkSpan;
}
public int getSpan() {
return span;
}
@@ -137,19 +143,44 @@ public final class MobOverlayScan {
return minZ;
}
/** {@code i} and {@code j} are from 0 to {@link #getSpan}, and {@code k} is from 0 to {@link #getHeight}. */
public byte readSpawnMode(int i, int k, int j) {
return (byte) (modes[findCell(minX + i, minY + k, minZ + j)] & SPAWN_MODE_MASK);
public int getMinChunkX() {
return minX >> CHUNK_SHIFT;
}
/**
* Height of the overlay above the bottom of the block, so that a flat block such as a carpet does not cover the
* overlay. The indices are those of {@link #readSpawnMode}.
*/
public double readOverlayYOffset(int i, int k, int j) {
public int getMaxChunkX() {
return (minX + span - 1) >> CHUNK_SHIFT;
}
public int getMinChunkZ() {
return minZ >> CHUNK_SHIFT;
}
public int getMaxChunkZ() {
return (minZ + span - 1) >> CHUNK_SHIFT;
}
public int readChunkVersion(int chunkX, int chunkZ) {
return chunkVersions[findChunk(chunkX, chunkZ)];
}
public byte readSpawnMode(int i, int k, int j) {
return extractSpawnMode(modes[findCell(minX + i, minY + k, minZ + j)]);
}
public void readColumn(int i, int j, byte[] columnCells) {
final int columnStart = findColumn(minX + i, minZ + j) * height;
final int bottomCell = Math.floorMod(minY, height);
System.arraycopy(modes, columnStart + bottomCell, columnCells, 0, height - bottomCell);
System.arraycopy(modes, columnStart, columnCells, height - bottomCell, bottomCell);
}
public static byte extractSpawnMode(byte cell) {
return (byte) (cell & SPAWN_MODE_MASK);
}
public static double computeOverlayYOffset(byte cell) {
final double OVERLAY_GAP = 0.02;
final int coverTopSteps = modes[findCell(minX + i, minY + k, minZ + j)] >> COVER_TOP_SHIFT;
return (double) coverTopSteps / COVER_TOP_STEPS + OVERLAY_GAP;
return (double) (cell >> COVER_TOP_SHIFT) / COVER_TOP_STEPS + OVERLAY_GAP;
}
private void resize(int newRadius, int newVerticalRadius) {
@@ -159,6 +190,8 @@ public final class MobOverlayScan {
height = 2 * newVerticalRadius + 1;
modes = new byte[span * span * height];
dirty = new boolean[span * span];
chunkSpan = ((span - 1) >> CHUNK_SHIFT) + 2;
chunkVersions = new int[chunkSpan * chunkSpan];
hasModes = false;
nextRescanColumn = 0;
}
@@ -172,6 +205,7 @@ public final class MobOverlayScan {
scanColumn(x, z);
}
}
markAllChunks();
hasModes = true;
modesVersion++;
scannedTick = scannedWorld.getTotalWorldTime();
@@ -189,6 +223,7 @@ public final class MobOverlayScan {
final int offsetX = centreX - radius - minX;
if (Math.abs(offsetX) > RECENTRE_DISTANCE) {
final int enteringX = offsetX > 0 ? minX + span : minX + offsetX;
markMovedEdgeChunks(minX, offsetX, Axis.X);
minX += offsetX;
for (int x = enteringX; x < enteringX + Math.abs(offsetX); x++) {
for (int z = minZ; z < minZ + span; z++) {
@@ -201,6 +236,7 @@ public final class MobOverlayScan {
final int offsetZ = centreZ - radius - minZ;
if (Math.abs(offsetZ) > RECENTRE_DISTANCE) {
final int enteringZ = offsetZ > 0 ? minZ + span : minZ + offsetZ;
markMovedEdgeChunks(minZ, offsetZ, Axis.Z);
minZ += offsetZ;
for (int x = minX; x < minX + span; x++) {
for (int z = enteringZ; z < enteringZ + Math.abs(offsetZ); z++) {
@@ -220,6 +256,7 @@ public final class MobOverlayScan {
column.scan(enteringY, enteringY + Math.abs(offsetY));
}
}
markAllChunks();
isMoved = true;
}
return isMoved;
@@ -230,7 +267,7 @@ public final class MobOverlayScan {
boolean isChanged = false;
for (int x = minX; x < minX + span; x++) {
for (int z = minZ; z < minZ + span; z++) {
if (dirty[findColumn(x, z)]) isChanged |= scanColumn(x, z);
if (dirty[findColumn(x, z)]) isChanged |= rescanColumn(x, z);
}
}
return isChanged;
@@ -243,7 +280,7 @@ public final class MobOverlayScan {
for (; nextRescanColumn < sliceEnd; nextRescanColumn++) {
final int x = minX + nextRescanColumn / span;
final int z = minZ + nextRescanColumn % span;
isChanged |= scanColumn(x, z);
isChanged |= rescanColumn(x, z);
}
if (nextRescanColumn >= columnCount) nextRescanColumn = 0;
return isChanged;
@@ -256,6 +293,60 @@ public final class MobOverlayScan {
return column.scan(minY, minY + height);
}
private boolean rescanColumn(int x, int z) {
final boolean isChanged = scanColumn(x, z);
// the connected outline of a cell depends on the spawn modes of the cells beside the cell
if (isChanged) {
for (int offsetX = -1; offsetX <= 1; offsetX++) {
for (int offsetZ = -1; offsetZ <= 1; offsetZ++) {
markChunk((x + offsetX) >> CHUNK_SHIFT, (z + offsetZ) >> CHUNK_SHIFT);
}
}
}
return isChanged;
}
private void markMovedEdgeChunks(int oldMin, int offset, Axis moveAxis) {
final int lowFrom = Math.min(oldMin, oldMin + offset);
final int lowTo = Math.max(oldMin, oldMin + offset);
markStripChunks(lowFrom, lowTo, moveAxis);
markStripChunks(lowFrom + span - 1, lowTo + span - 1, moveAxis);
}
private void markStripChunks(int from, int to, Axis moveAxis) {
final int acrossMin = switch (moveAxis) {
case X -> minZ;
case Z -> minX;
};
final int minAcrossChunk = acrossMin >> CHUNK_SHIFT;
final int maxAcrossChunk = (acrossMin + span - 1) >> CHUNK_SHIFT;
for (int alongChunk = from >> CHUNK_SHIFT; alongChunk <= to >> CHUNK_SHIFT; alongChunk++) {
for (int acrossChunk = minAcrossChunk; acrossChunk <= maxAcrossChunk; acrossChunk++) {
final int chunkX = switch (moveAxis) {
case X -> alongChunk;
case Z -> acrossChunk;
};
final int chunkZ = switch (moveAxis) {
case X -> acrossChunk;
case Z -> alongChunk;
};
markChunk(chunkX, chunkZ);
}
}
}
private void markAllChunks() {
Arrays.fill(chunkVersions, ++chunkVersionCount);
}
private void markChunk(int chunkX, int chunkZ) {
chunkVersions[findChunk(chunkX, chunkZ)] = ++chunkVersionCount;
}
public int findChunk(int chunkX, int chunkZ) {
return Math.floorMod(chunkX, chunkSpan) * chunkSpan + Math.floorMod(chunkZ, chunkSpan);
}
private int findColumn(int x, int z) {
return Math.floorMod(x, span) * span + Math.floorMod(z, span);
}
@@ -264,10 +355,6 @@ public final class MobOverlayScan {
return findColumn(x, z) * height + Math.floorMod(y, height);
}
/**
* {@link #moveTo} reads the chunk, the height map and the biome of a column, and {@link #scan} reads the column's
* blocks.
*/
private final class Column {
private final Entity dummyEntity = new EntityPig(null);
@@ -285,15 +372,12 @@ public final class MobOverlayScan {
canBiomeSpawn = !biome.getSpawnableList(EnumCreatureType.monster).isEmpty()
&& biome.getSpawningChance() > 0;
chunk = scannedWorld.getChunkFromBlockCoords(x, z);
// on the client, `Chunk.generateHeightMap()` never tests the top layer of the highest non-empty section.
// `getHeightValue()` can then be below a block on that layer, so test that layer here
final int topLayerY = chunk.getTopFilledSegment() + 15;
final boolean isTopLayerFilled = !chunk.isEmpty() && chunk.func_150808_b(x & 15, topLayerY, z & 15) != 0;
final int heightValue = isTopLayerFilled ? topLayerY + 1 : scannedWorld.getHeightValue(x, z);
// `getHeightValue()` is the height above the highest block with a light opacity above 0. A snow layer and a
// hopper have a light opacity of 0, and a mob can spawn on top of a full snow block or a hopper, so scan
// one block above the height value
topY = Math.min(scannedWorld.getHeight() - 1, heightValue + 1);
// `getHeightValue()` skips blocks with a light opacity of 0. A mob can spawn on some of these blocks, such
// as Tinkers' Construct slime grass, a hopper or a full snow block. Search for the highest non-air block
final int SECTION_HEIGHT = 16;
int y = Math.min(minY + height - 1, chunk.getTopFilledSegment() + SECTION_HEIGHT - 1);
while (y >= minY && chunk.getBlock(x & 15, y, z & 15).isAir(chunk.worldObj, x, y, z)) y--;
topY = y + 1;
}
/** @return whether a spawn mode or the top of a flat block changed */
@@ -309,12 +393,15 @@ public final class MobOverlayScan {
return isChanged;
}
/** The spawn mode of the block at height {@code y} in this column. */
private byte getSpawnMode(int y) {
if (y >= chunk.worldObj.getHeight()) {
return 0;
}
// vanilla `SpawnerAnimals.canCreatureTypeSpawnAtLocation()` rejects a normal cube in the cell (such as the
// inside of a stone block). Test the block before the light read
if (chunk.getBlock(x & 15, y, z & 15).isNormalCube()) return 0;
if (chunk.getSavedLightValue(EnumSkyBlock.Block, x & 15, y, z & 15) >= 8 || !SpawnerAnimals
.canCreatureTypeSpawnAtLocation(EnumCreatureType.monster, chunk.worldObj, x, y, z)) {
return 0;
@@ -338,10 +425,6 @@ public final class MobOverlayScan {
return (byte) (chunk.getSavedLightValue(EnumSkyBlock.Sky, x & 15, y, z & 15) >= 8 ? 1 : 2);
}
/**
* The top of the block at height {@code y}, in fractions of a block with {@link MobOverlayScan#COVER_TOP_STEPS}
* as the denominator, or 0 for a tall block.
*/
private int readCoverTop(int y) {
final double MAX_COVER_TOP = 0.25;
final Block block = chunk.getBlock(x & 15, y, z & 15);
@@ -353,7 +436,6 @@ public final class MobOverlayScan {
}
}
/** Marks the columns of the block and light changes in {@link #scannedWorld}. */
private final class ChangeListener implements IWorldAccess {
@Override
@@ -418,4 +500,9 @@ public final class MobOverlayScan {
hasDirtyColumns = true;
}
}
private enum Axis {
X,
Z
}
}
@@ -27,9 +27,7 @@ public enum MobOverlayStyle {
}
},
/**
* A light tint, with a stronger outline around each area of one spawn mode, like a connected texture.
*/
/** A light tint and a stronger connected-texture outline around each area of one spawn mode. */
CONNECTED(GL11.GL_QUADS) {
@Override
@@ -39,7 +37,8 @@ public enum MobOverlayStyle {
face.applyColor(Colouring.OVERLAY, MARK_ALPHA);
for (int offsetX = -1; offsetX <= 1; offsetX++) {
for (int offsetZ = -1; offsetZ <= 1; offsetZ++) {
// a corner cell is drawn when either edge next to the corner is outlined, and on an inside corner
// a corner cell is drawn when either edge next to the corner is outlined or when the corner is an
// inside corner
final boolean isOutline = !(face.isJoined(offsetX, offsetZ) && face.isJoined(offsetX, 0)
&& face.isJoined(0, offsetZ));
if (isOutline) face.addCell(offsetX, offsetZ, OUTLINE_WIDTH);
@@ -48,7 +47,7 @@ public enum MobOverlayStyle {
}
},
/** A small square in the middle of the block, with no tint. */
/** A small square in the middle of an untinted block. */
SQUARE(GL11.GL_QUADS) {
@Override
@@ -62,7 +61,7 @@ public enum MobOverlayStyle {
}
},
/** The pufferfish sticker. */
/** A pufferfish sticker on the block. */
PUFFERFISH("pufferfish", Colouring.STICKER) {
@Override
@@ -102,7 +101,6 @@ public enum MobOverlayStyle {
return DEFAULT;
}
/** @return the {@link #formatName} of every style, separated by commas */
public static String formatAllNames() {
final StringJoiner names = new StringJoiner(", ");
for (MobOverlayStyle style : values()) {
@@ -119,7 +117,7 @@ public enum MobOverlayStyle {
return switch (this) {
case CONNECTED -> TINT_ALPHA;
case SQUARE -> MARK_ALPHA;
// opaque lines for X, and the alpha of the PNG for PUFFERFISH
// the lines of X are opaque, and the alpha of PUFFERFISH comes from the PNG
case X, PUFFERFISH -> 1.0F;
};
}
@@ -127,9 +125,9 @@ public enum MobOverlayStyle {
public abstract void addVertices(MobOverlayFace face);
public enum Colouring {
/** Yellow where mobs spawn only at night, red where mobs spawn at any time. */
/** Yellow marks where mobs spawn only at night and red marks where mobs spawn at any time. */
OVERLAY,
/** The texture's colours, tinted red where mobs spawn at any time. */
/** Texture colours with a red tint where mobs spawn at any time. */
STICKER
}
}
@@ -0,0 +1,45 @@
# Mob overlay TODO: spawn rules from other mods
`MobOverlayScan` reads the block, the light and the biome of each cell. Some mods set the result of Forge's
`LivingSpawnEvent.CheckSpawn` from the monster's position, and `MobOverlayScan` doesn't call these handlers.
A monster spawns when the result is `ALLOW`, or when the result is `DEFAULT` and `getCanSpawnHere()` returns
true. So `ALLOW` skips the light test.
## DENY: marked cells where a monster can't spawn
GregTech and Extra Utilities measure a range from the centre of the block.
| mod in 2.9.0 | handler | range | on the client? |
|---|---|---|---|
| GregTech Monster Repellator | `GTSpawnEventHandler.denyMobSpawn()` | sphere. The radius is 4 + 12 × tier without power and 16 + 48 × tier with power | no. `MTEMonsterRepellent.onPostTick()` registers the range on the server only |
| Extra Utilities magnum torch | `EventHandlerServer.magnumTorchDenySpawn()` | ellipsoid 128 blocks out and 32 blocks up and down | maybe. `TileEntityAntiMobTorch` adds itself to the static `magnumTorchRegistry` and doesn't check the side. The bytecode is the source of this row, and the row isn't tested in game |
| Extra Utilities chandelier | the same handler | sphere, radius 16 | maybe. The chandelier's tile entity is a `TileEntityAntiMobTorch` too |
| Gadomancy aura pylon with Lux | `EventHandlerEntityServer.on(CheckSpawn)` | sphere, radius `AuraEffects.LUX.getRange()` | not checked. `AuraEffects.LUX` adds the pylon to `registeredLuxPylons` when the pylon's effect runs on an entity |
| Blood Magic Ward of Sacrosanctity (`AW028SpawnWard`) | `AlchemicalWizardryEventHooks.onLivingSpawnEvent()` | box from the ritual's horizontal and vertical radii | not checked. `RitualEffectSpawnWard` fills `respawnMap` |
| Botania Emptiness potion | `PotionEmptiness.onSpawn()` | sphere around each player with the effect | the player's potion effects |
| ServerUtilities | `ServerUtilitiesWorldEventHandler.onMobSpawned()` | chunks of the safe spawn area, and claimed chunks by the team's settings | no. The claims are server data |
| Et Futurum Requiem | `ServerEventHandler.spawnEvent()` | in the Nether, a cell above `ModBlocks.NETHER_WART` at meta 0, for a mob that doesn't fly | yes. The block below is in the client world |
## ALLOW: monsters spawn in unmarked lit cells
| mod in 2.9.0 | handler | range |
|---|---|---|
| Blood Magic Veil of Evil (`AW029VeilOfEvil`) | `AlchemicalWizardryEventHooks.onLivingSpawnEvent()` | box from the ritual's radii. Creepers are excluded |
| Botania Bloodthirst potion | `PotionBloodthirst.onSpawn()` | sphere around each player with the effect |
Thaumcraft 4.2.3.5 doesn't have a `CheckSpawn` handler. The list comes from a scan of the 2.9.0 pack's jars for
`LivingSpawnEvent$CheckSpawn`. Handlers that test only the mob type, the dimension or the mob count are left out:
AMDIForge, EnderZoo, Automagy, the Extra Utilities Underdark, and the slime handlers of Biomes O' Plenty and Botania.
The Extra Utilities siege handler (`EventHandlerSiege.SiegeCheckSpawn()`) isn't checked.
## To do
- [ ] Add an NEI API: a mod registers a cell test that returns `DENY`, `ALLOW` or `DEFAULT`. `MobOverlayScan` calls
each test for each cell in the box.
- [ ] Add an API call to mark an area dirty. `MobOverlayScan` then rescans the area when a range is added or
removed.
- [ ] Ask GregTech, Gadomancy, Blood Magic and ServerUtilities to send their ranges to the client.
- [ ] Test the Extra Utilities registry on a client with a dedicated server.
- [ ] Find out how the handlers combine. `GTSpawnEventHandler` returns early on `DENY`, and Blood Magic's handler
changes `ALLOW` to `DEFAULT`, so the result depends on the order of the handlers on the event bus.