mirror of
https://github.com/GTNewHorizons/NotEnoughItems.git
synced 2026-11-21 04:32:27 +00:00
Co-authored-by: OrderedSet86 <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Martin Robertz <[email protected]>
420 lines
17 KiB
Java
420 lines
17 KiB
Java
package codechicken.nei;
|
|
|
|
import java.lang.reflect.Field;
|
|
import java.lang.reflect.Method;
|
|
import java.util.concurrent.Callable;
|
|
|
|
import net.minecraft.client.Minecraft;
|
|
import net.minecraft.client.renderer.Tessellator;
|
|
import net.minecraft.entity.Entity;
|
|
import net.minecraft.util.ChatComponentText;
|
|
import net.minecraft.util.ResourceLocation;
|
|
import net.minecraft.world.World;
|
|
|
|
import org.lwjgl.opengl.GL11;
|
|
|
|
import com.cardinalstar.cubicchunks.world.api.IMinMaxHeight;
|
|
|
|
import codechicken.nei.KeyManager.IKeyStateTracker;
|
|
import codechicken.nei.moboverlay.MobOverlayDisplayList;
|
|
import codechicken.nei.moboverlay.MobOverlayFace;
|
|
import codechicken.nei.moboverlay.MobOverlayScan;
|
|
import codechicken.nei.moboverlay.MobOverlayStyle;
|
|
import cpw.mods.fml.relauncher.ReflectionHelper;
|
|
|
|
public class WorldOverlayRenderer implements IKeyStateTracker {
|
|
|
|
public static int chunkOverlay = 0;
|
|
|
|
public static int mobOverlay = 0;
|
|
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();
|
|
|
|
public static void reset() {
|
|
mobOverlay = 0;
|
|
chunkOverlay = 0;
|
|
}
|
|
|
|
public static void onWorldUnload() {
|
|
MOB_OVERLAY_SCAN.clear();
|
|
MOB_OVERLAY_LIST.delete();
|
|
}
|
|
|
|
@Override
|
|
public void tickKeyStates() {
|
|
if (Minecraft.getMinecraft().currentScreen != null) return;
|
|
|
|
if (KeyManager.isPressed("world.moboverlay")) {
|
|
mobOverlay = (mobOverlay + 1) % 2;
|
|
NEIClientUtils
|
|
.printChatMessage(new ChatComponentText(NEIClientUtils.translate("chat.moboverlay." + mobOverlay)));
|
|
}
|
|
|
|
if (KeyManager.isPressed("world.chunkoverlay")) {
|
|
chunkOverlay = (chunkOverlay + 1) % (NEIModContainer.isGT5Loaded() ? 4 : 3);
|
|
// 0-2 (or 3 with gt)
|
|
NEIClientUtils.printChatMessage(
|
|
new ChatComponentText(NEIClientUtils.translate("chat.chunkoverlay." + chunkOverlay)));
|
|
}
|
|
|
|
}
|
|
|
|
public static void render(float frame) {
|
|
GL11.glPushAttrib(GL11.GL_ENABLE_BIT | GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_LINE_BIT);
|
|
GL11.glPushMatrix();
|
|
Entity entity = Minecraft.getMinecraft().renderViewEntity;
|
|
|
|
double interpPosX = entity.lastTickPosX + (entity.posX - entity.lastTickPosX) * frame;
|
|
double interpPosY = entity.lastTickPosY + (entity.posY - entity.lastTickPosY) * frame;
|
|
double interpPosZ = entity.lastTickPosZ + (entity.posZ - entity.lastTickPosZ) * frame;
|
|
|
|
int intOffsetX = (int) interpPosX;
|
|
int intOffsetY = (int) interpPosY;
|
|
int intOffsetZ = (int) interpPosZ;
|
|
|
|
GL11.glTranslated(-interpPosX + intOffsetX, -interpPosY + intOffsetY, -interpPosZ + intOffsetZ);
|
|
|
|
GL11.glEnable(GL11.GL_DEPTH_TEST);
|
|
GL11.glDepthFunc(GL11.GL_LEQUAL);
|
|
renderChunkBounds(entity, intOffsetX, intOffsetY, intOffsetZ);
|
|
renderMobSpawnOverlay(entity, intOffsetX, intOffsetY, intOffsetZ);
|
|
GL11.glPopMatrix();
|
|
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();
|
|
MOB_OVERLAY_LIST.delete();
|
|
return;
|
|
}
|
|
|
|
MOB_OVERLAY_SCAN.update(
|
|
entity,
|
|
NEIClientConfig.getIntSetting("world.overlays.mobOverlayRadius"),
|
|
NEIClientConfig.getIntSetting("world.overlays.mobOverlayVerticalRadius"));
|
|
final MobOverlayStyle style = MobOverlayStyle
|
|
.parseStyle(NEIClientConfig.getSetting("world.overlays.mobOverlayStyle").getValue());
|
|
final long worldTicks = entity.worldObj.getTotalWorldTime();
|
|
if (MOB_OVERLAY_LIST.isCompileDue(MOB_OVERLAY_SCAN.getModesVersion(), style, worldTicks)) {
|
|
compileMobOverlayList(style, worldTicks);
|
|
}
|
|
|
|
beginMobOverlayDraw(style);
|
|
MOB_OVERLAY_LIST.draw(intOffsetX, intOffsetY, intOffsetZ);
|
|
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);
|
|
final float alpha = style.computeAlpha();
|
|
final Tessellator tess = Tessellator.instance;
|
|
tess.startDrawing(style.glMode);
|
|
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;
|
|
|
|
MARKED_FACE.moveTo(i, k + MOB_OVERLAY_SCAN.readOverlayYOffset(i, k, j), j);
|
|
loadNeighbourModes(i, k, j);
|
|
MARKED_FACE.applyColor(style.colouring, alpha);
|
|
style.addVertices(MARKED_FACE);
|
|
}
|
|
}
|
|
}
|
|
tess.draw();
|
|
MOB_OVERLAY_LIST.endCompile();
|
|
}
|
|
|
|
private static void beginMobOverlayDraw(MobOverlayStyle style) {
|
|
final ResourceLocation texture = style.texture;
|
|
if (texture == null) {
|
|
GL11.glDisable(GL11.GL_TEXTURE_2D);
|
|
} else {
|
|
GL11.glEnable(GL11.GL_TEXTURE_2D);
|
|
Minecraft.getMinecraft().getTextureManager().bindTexture(texture);
|
|
}
|
|
GL11.glEnable(GL11.GL_BLEND);
|
|
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
|
|
// test discards most of the outline
|
|
GL11.glDepthMask(false);
|
|
}
|
|
|
|
private static void loadNeighbourModes(int i, int k, int j) {
|
|
final int span = MOB_OVERLAY_SCAN.getSpan();
|
|
for (int offsetX = -1; offsetX <= 1; offsetX++) {
|
|
for (int offsetZ = -1; offsetZ <= 1; offsetZ++) {
|
|
final int neighbourI = i + offsetX;
|
|
final int neighbourJ = j + offsetZ;
|
|
final boolean isInScan = neighbourI >= 0 && neighbourI < span && neighbourJ >= 0 && neighbourJ < span;
|
|
final byte spawnMode = isInScan ? MOB_OVERLAY_SCAN.readSpawnMode(neighbourI, k, neighbourJ)
|
|
: MobOverlayFace.OUTSIDE_SCAN;
|
|
MARKED_FACE.setNeighbourMode(offsetX, offsetZ, spawnMode);
|
|
}
|
|
}
|
|
}
|
|
|
|
/** Undo the GL state changes of {@link #beginMobOverlayDraw}, except the texture binding. */
|
|
private static void endMobOverlayDraw() {
|
|
GL11.glDepthMask(true);
|
|
GL11.glEnable(GL11.GL_LIGHTING);
|
|
GL11.glEnable(GL11.GL_TEXTURE_2D);
|
|
GL11.glDisable(GL11.GL_BLEND);
|
|
}
|
|
|
|
private static void renderChunkBounds(Entity entity, int intOffsetX, int intOffsetY, int intOffsetZ) {
|
|
if (chunkOverlay == 0) return;
|
|
|
|
int worldMinY = worldMinHeight(entity.worldObj);
|
|
int worldMaxY = worldMaxHeight(entity.worldObj);
|
|
|
|
GL11.glDisable(GL11.GL_TEXTURE_2D);
|
|
GL11.glEnable(GL11.GL_BLEND);
|
|
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
|
|
GL11.glDisable(GL11.GL_LIGHTING);
|
|
GL11.glLineWidth(1.5F);
|
|
|
|
final Tessellator tess = Tessellator.instance;
|
|
tess.startDrawing(GL11.GL_LINES);
|
|
|
|
for (int cx = -4; cx <= 4; cx++) for (int cz = -4; cz <= 4; cz++) {
|
|
double x1 = ((entity.chunkCoordX + cx) << 4) - intOffsetX;
|
|
double z1 = ((entity.chunkCoordZ + cz) << 4) - intOffsetZ;
|
|
double x2 = x1 + 16;
|
|
double z2 = z1 + 16;
|
|
|
|
double dy = 128;
|
|
double y1 = Math.floor(entity.posY - dy / 2);
|
|
double y2 = y1 + dy;
|
|
if (y1 < worldMinY) {
|
|
y1 = worldMinY;
|
|
y2 = worldMinY + dy;
|
|
}
|
|
if (y2 > worldMaxY) {
|
|
y2 = worldMaxY;
|
|
y1 = Math.max(worldMaxY - dy, worldMinY);
|
|
}
|
|
|
|
y1 -= intOffsetY;
|
|
y2 -= intOffsetY;
|
|
|
|
double dist = Math.pow(1.5, -(cx * cx + cz * cz));
|
|
|
|
tess.setColorRGBA_F(0.9F, 0, 0, (float) dist);
|
|
if (cx >= 0 && cz >= 0) {
|
|
tess.addVertex(x2, y1, z2);
|
|
tess.addVertex(x2, y2, z2);
|
|
}
|
|
if (cx >= 0 && cz <= 0) {
|
|
tess.addVertex(x2, y1, z1);
|
|
tess.addVertex(x2, y2, z1);
|
|
}
|
|
if (cx <= 0 && cz >= 0) {
|
|
tess.addVertex(x1, y1, z2);
|
|
tess.addVertex(x1, y2, z2);
|
|
}
|
|
if (cx <= 0 && cz <= 0) {
|
|
tess.addVertex(x1, y1, z1);
|
|
tess.addVertex(x1, y2, z1);
|
|
}
|
|
|
|
if (NEIModContainer.isCCLoaded()) {
|
|
tess.setColorRGBA_F(0, 0.5F, 0.9F, (float) dist * 0.5F);
|
|
int cubeBase = (int) Math.floor((y1 + intOffsetY) / 16.0) * 16;
|
|
for (int cubeY = cubeBase; cubeY <= y2 + intOffsetY; cubeY += 16) {
|
|
double ry = cubeY - intOffsetY;
|
|
tess.addVertex(x1, ry, z1);
|
|
tess.addVertex(x2, ry, z1);
|
|
tess.addVertex(x1, ry, z2);
|
|
tess.addVertex(x2, ry, z2);
|
|
tess.addVertex(x1, ry, z1);
|
|
tess.addVertex(x1, ry, z2);
|
|
tess.addVertex(x2, ry, z1);
|
|
tess.addVertex(x2, ry, z2);
|
|
}
|
|
}
|
|
|
|
if (cx == 0 && cz == 0) {
|
|
if (chunkOverlay == 2) {
|
|
dy = 32;
|
|
y1 = Math.floor(entity.posY - dy / 2);
|
|
y2 = y1 + dy;
|
|
if (y1 < worldMinY) {
|
|
y1 = worldMinY;
|
|
y2 = worldMinY + dy;
|
|
}
|
|
if (y2 > worldMaxY) {
|
|
y2 = worldMaxY;
|
|
y1 = Math.max(worldMaxY - dy, worldMinY);
|
|
}
|
|
|
|
y1 -= intOffsetY;
|
|
y2 -= intOffsetY;
|
|
double centerX = (x1 + x2) / 2;
|
|
double centerZ = (z1 + z2) / 2;
|
|
|
|
tess.setColorRGBA_F(0.9F, 0.9F, 0, (float) dist);
|
|
tess.addVertex(centerX, y1, centerZ);
|
|
tess.addVertex(centerX, y2, centerZ);
|
|
|
|
tess.setColorRGBA_F(0, 0.9F, 0, 0.4F);
|
|
for (double y = (int) y1; y <= y2; y++) {
|
|
tess.addVertex(x2, y, z1);
|
|
tess.addVertex(x2, y, z2);
|
|
tess.addVertex(x1, y, z1);
|
|
tess.addVertex(x1, y, z2);
|
|
tess.addVertex(x1, y, z2);
|
|
tess.addVertex(x2, y, z2);
|
|
tess.addVertex(x1, y, z1);
|
|
tess.addVertex(x2, y, z1);
|
|
}
|
|
for (double h = 1; h <= 15; h++) {
|
|
tess.addVertex(x1 + h, y1, z1);
|
|
tess.addVertex(x1 + h, y2, z1);
|
|
tess.addVertex(x1 + h, y1, z2);
|
|
tess.addVertex(x1 + h, y2, z2);
|
|
tess.addVertex(x1, y1, z1 + h);
|
|
tess.addVertex(x1, y2, z1 + h);
|
|
tess.addVertex(x2, y1, z1 + h);
|
|
tess.addVertex(x2, y2, z1 + h);
|
|
}
|
|
} else if (chunkOverlay == 3) {
|
|
int gx1;
|
|
int gz1;
|
|
if (getOregenPatternName().equals("EQUAL_SPACING")) {
|
|
// gt oregen uses new regular pattern
|
|
gx1 = ((Math.floorDiv(entity.chunkCoordX, 3) * 3) << 4) - intOffsetX;
|
|
gz1 = ((Math.floorDiv(entity.chunkCoordZ, 3) * 3) << 4) - intOffsetZ;
|
|
} else {
|
|
// gt oregen uses old bugged pattern
|
|
gx1 = (((entity.chunkCoordX < 0 ? entity.chunkCoordX - 3 : entity.chunkCoordX) / 3 * 3) << 4)
|
|
- intOffsetX;
|
|
gz1 = (((entity.chunkCoordZ < 0 ? entity.chunkCoordZ - 3 : entity.chunkCoordZ) / 3 * 3) << 4)
|
|
- intOffsetZ;
|
|
if (entity.chunkCoordX < 0) {
|
|
gx1 += 16;
|
|
}
|
|
if (entity.chunkCoordZ < 0) {
|
|
gz1 += 16;
|
|
}
|
|
}
|
|
int gx2 = gx1 + 48;
|
|
int gz2 = gz1 + 48;
|
|
|
|
tess.setColorRGBA_F(0, 0.9F, 0, 0.4F);
|
|
for (double y = (int) y1; y <= y2; y++) {
|
|
tess.addVertex(gx2, y, gz1);
|
|
tess.addVertex(gx2, y, gz2);
|
|
tess.addVertex(gx1, y, gz1);
|
|
tess.addVertex(gx1, y, gz2);
|
|
tess.addVertex(gx1, y, gz2);
|
|
tess.addVertex(gx2, y, gz2);
|
|
tess.addVertex(gx1, y, gz1);
|
|
tess.addVertex(gx2, y, gz1);
|
|
}
|
|
for (double h = 4; h <= 44; h += 4) {
|
|
if (h % 16 == 0) {
|
|
continue;
|
|
}
|
|
tess.addVertex(gx1 + h, y1, gz1);
|
|
tess.addVertex(gx1 + h, y2, gz1);
|
|
tess.addVertex(gx1 + h, y1, gz2);
|
|
tess.addVertex(gx1 + h, y2, gz2);
|
|
tess.addVertex(gx1, y1, gz1 + h);
|
|
tess.addVertex(gx1, y2, gz1 + h);
|
|
tess.addVertex(gx2, y1, gz1 + h);
|
|
tess.addVertex(gx2, y2, gz1 + h);
|
|
}
|
|
|
|
tess.setColorRGBA_F(0, 0, 0.9F, 0.4F);
|
|
gx1 += 23;
|
|
gz1 += 23;
|
|
gx2 = gx1 + 1;
|
|
gz2 = gz1 + 1;
|
|
|
|
tess.addVertex(gx1, y1, gz1);
|
|
tess.addVertex(gx1, y2, gz1);
|
|
tess.addVertex(gx2, y1, gz1);
|
|
tess.addVertex(gx2, y2, gz1);
|
|
tess.addVertex(gx1, y1, gz2);
|
|
tess.addVertex(gx1, y2, gz2);
|
|
tess.addVertex(gx2, y1, gz2);
|
|
tess.addVertex(gx2, y2, gz2);
|
|
}
|
|
}
|
|
}
|
|
|
|
tess.draw();
|
|
GL11.glEnable(GL11.GL_LIGHTING);
|
|
GL11.glEnable(GL11.GL_TEXTURE_2D);
|
|
GL11.glDisable(GL11.GL_BLEND);
|
|
}
|
|
|
|
private static int worldMinHeight(World world) {
|
|
if (NEIModContainer.isCCLoaded() && world instanceof IMinMaxHeight worldHeights) {
|
|
return worldHeights.getMinHeight();
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
private static int worldMaxHeight(World world) {
|
|
if (NEIModContainer.isCCLoaded() && world instanceof IMinMaxHeight worldHeights) {
|
|
return worldHeights.getMaxHeight();
|
|
} else {
|
|
return world.getHeight();
|
|
}
|
|
}
|
|
|
|
private static Callable<String> oregenPatternReader;
|
|
|
|
private static String getOregenPatternName() {
|
|
if (oregenPatternReader == null) {
|
|
oregenPatternReader = resolveOregenPatternReader();
|
|
}
|
|
try {
|
|
return oregenPatternReader.call();
|
|
} catch (Exception ignored) {
|
|
return "AXISSYMMETRICAL";
|
|
}
|
|
}
|
|
|
|
private static Callable<String> resolveOregenPatternReader() {
|
|
try {
|
|
final Class<?> gtWorldGenerator = ReflectionHelper.getClass(
|
|
WorldOverlayRenderer.class.getClassLoader(),
|
|
"gregtech.common.GTWorldgenerator",
|
|
"gregtech.common.GT_Worldgenerator");
|
|
|
|
try {
|
|
// GT5-Unofficial >= 5.09.54.29 support
|
|
final Method accessor = gtWorldGenerator.getMethod("getOregenPattern");
|
|
return () -> ((Enum<?>) accessor.invoke(null)).name();
|
|
} catch (NoSuchMethodException legacy) {
|
|
// Older GT
|
|
final Field oregenPattern = gtWorldGenerator.getDeclaredField("oregenPattern");
|
|
oregenPattern.setAccessible(true);
|
|
return () -> ((Enum<?>) oregenPattern.get(null)).name();
|
|
}
|
|
} catch (Exception ignored) {
|
|
return () -> "AXISSYMMETRICAL";
|
|
}
|
|
}
|
|
|
|
public static void load() {
|
|
KeyManager.trackers.add(new WorldOverlayRenderer());
|
|
}
|
|
}
|