Skip to content

Commit

Permalink
Add echo detection
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinthegreat1 committed Sep 30, 2023
1 parent 6c8016c commit dc24812
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 32 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ dependencies {

// EMI
modCompileOnly "dev.emi:emi-fabric:${project.emi_version}:api"
modLocalRuntime "dev.emi:emi-fabric:${project.emi_version}"
// modLocalRuntime "dev.emi:emi-fabric:${project.emi_version}"

// Renderer (https://github.com/0x3C50/Renderer)
include modImplementation("com.github.0x3C50:Renderer:${project.renderer_version}") {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ clothconfig_version=12.0.108
## Mod Menu (https://modrinth.com/mod/modmenu/versions)
mod_menu_version=8.0.0-beta.2
## REI (https://modrinth.com/mod/rei/versions?l=fabric)
rei_version=12.0.625
rei_version=13.0.661
## EMI (https://modrinth.com/mod/emi/versions)
emi_version = 1.0.19+1.20.1
## Renderer (https://github.com/0x3C50/Renderer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@

public class MythologicalRitual {
private static final Pattern GRIFFIN_BURROW_DUG = Pattern.compile("(?<message>You dug out a Griffin Burrow!|You finished the Griffin burrow chain!) \\((?<index>\\d)/4\\)");
private static final float[] WHITE_COLOR_COMPONENTS = {1.0f, 1.0f, 1.0f};
private static final float[] ORANGE_COLOR_COMPONENTS = DyeColor.ORANGE.getColorComponents();
private static final Map<BlockPos, GriffinBurrow> griffinBurrows = new HashMap<>();
@Nullable
private static BlockPos lastDugBurrowPos;
private static GriffinBurrow previousBurrow;
private static GriffinBurrow previousBurrow = new GriffinBurrow();

public static void init() {
WorldRenderEvents.AFTER_TRANSLUCENT.register(MythologicalRitual::render);
Expand All @@ -63,6 +63,10 @@ public static void init() {
}))
)
)));

// Put a root burrow so echo detection works without a previous burrow
previousBurrow.confirmed = TriState.DEFAULT;
griffinBurrows.put(BlockPos.ORIGIN, previousBurrow);
}

public static void onParticle(ParticleS2CPacket packet) {
Expand All @@ -89,24 +93,51 @@ public static void onParticle(ParticleS2CPacket packet) {
if (Double.isNaN(slope)) {
return;
}
Vec3d nextBurrowDirection = new Vec3d(100, 2, slope * 100).normalize().multiply(500);
burrow.nextBurrowPlane = new Vec3d[]{
Vec3d.of(pos).add(nextBurrowDirection),
Vec3d.of(pos).subtract(nextBurrowDirection)
};
Vec3d nextBurrowDirection = new Vec3d(100, 0, slope * 100).normalize().multiply(100);
if (burrow.nextBurrowPlane == null) {
burrow.nextBurrowPlane = new Vec3d[4];
}
burrow.nextBurrowPlane[0] = Vec3d.of(pos).add(nextBurrowDirection).subtract(0, 50, 0);
burrow.nextBurrowPlane[1] = Vec3d.of(pos).subtract(nextBurrowDirection).subtract(0, 50, 0);
burrow.nextBurrowPlane[2] = burrow.nextBurrowPlane[1].add(0, 100, 0);
burrow.nextBurrowPlane[3] = burrow.nextBurrowPlane[0].add(0, 100, 0);
} else if (ParticleTypes.DRIPPING_LAVA.equals(packet.getParameters().getType())) {
if (previousBurrow.echoBurrowDirection == null) {
previousBurrow.echoBurrowDirection = new Vec3d[2];
}
previousBurrow.echoBurrowDirection[0] = previousBurrow.echoBurrowDirection[1];
previousBurrow.echoBurrowDirection[1] = new Vec3d(packet.getX(), packet.getY(), packet.getZ());
if (previousBurrow.echoBurrowDirection[0] == null || previousBurrow.echoBurrowDirection[1] == null) {
return;
}
Vec3d echoBurrowDirection = previousBurrow.echoBurrowDirection[1].subtract(previousBurrow.echoBurrowDirection[0]).normalize().multiply(100);
if (previousBurrow.echoBurrowPlane == null) {
previousBurrow.echoBurrowPlane = new Vec3d[4];
}
previousBurrow.echoBurrowPlane[0] = previousBurrow.echoBurrowDirection[0].add(echoBurrowDirection).subtract(0, 50, 0);
previousBurrow.echoBurrowPlane[1] = previousBurrow.echoBurrowDirection[0].subtract(echoBurrowDirection).subtract(0, 50, 0);
previousBurrow.echoBurrowPlane[2] = previousBurrow.echoBurrowPlane[0].add(0, 100, 0);
previousBurrow.echoBurrowPlane[3] = previousBurrow.echoBurrowPlane[1].add(0, 100, 0);
}
}
}

public static void render(WorldRenderContext context) {
RenderHelper.renderLinesFromPoints(context, new Vec3d[]{new Vec3d(0, 96, -100), new Vec3d(0, 96, 100)}, ORANGE_COLOR_COMPONENTS, 0.25F, 5); // TODO debug
RenderHelper.renderQuad(context, new Vec3d[]{new Vec3d(0, 100, -100), new Vec3d(0, 100, 100), new Vec3d(0, 200, 100), new Vec3d(0, 200, -100)}, ORANGE_COLOR_COMPONENTS, 0.25F, true); // TODO debug
if (isActive()) {
for (Map.Entry<BlockPos, GriffinBurrow> burrowEntry : griffinBurrows.entrySet()) {
GriffinBurrow burrow = burrowEntry.getValue();
if (burrow.confirmed == TriState.TRUE) {
RenderHelper.renderFilledThroughWallsWithBeaconBeam(context, burrowEntry.getKey(), DyeColor.GREEN.getColorComponents(), 0.5F);
RenderHelper.renderFilledThroughWallsWithBeaconBeam(context, burrowEntry.getKey(), ORANGE_COLOR_COMPONENTS, 0.25F);
}
if (burrow.confirmed != TriState.FALSE && burrow.nextBurrowPlane != null) { // TODO try before debug render?
RenderHelper.renderLinesFromPoints(context, burrow.nextBurrowPlane, WHITE_COLOR_COMPONENTS, 1, 5);
if (burrow.confirmed != TriState.FALSE) {
if (burrow.nextBurrowPlane != null) {
RenderHelper.renderLinesFromPoints(context, burrow.nextBurrowPlane, ORANGE_COLOR_COMPONENTS, 0.25F, 5);
}
if (burrow.echoBurrowPlane != null) {
RenderHelper.renderLinesFromPoints(context, burrow.echoBurrowPlane, ORANGE_COLOR_COMPONENTS, 0.25F, 5);
}
}
}
}
Expand All @@ -130,9 +161,7 @@ private static ActionResult onInteractBlock(BlockPos pos) {

public static void onChatMessage(Text message, boolean overlay) {
if (isActive() && GRIFFIN_BURROW_DUG.matcher(message.getString()).matches()) {
if (previousBurrow != null) {
previousBurrow.confirmed = TriState.FALSE;
}
previousBurrow.confirmed = TriState.FALSE;
previousBurrow = griffinBurrows.get(lastDugBurrowPos);
previousBurrow.confirmed = TriState.DEFAULT;
}
Expand All @@ -148,6 +177,9 @@ private static class GriffinBurrow {
private TriState confirmed = TriState.FALSE;
private final SimpleRegression regression = new SimpleRegression();
private Vec3d[] nextBurrowPlane;
@Nullable
private Vec3d[] echoBurrowDirection;
private Vec3d[] echoBurrowPlane;

private void init() {
confirmed = TriState.TRUE;
Expand Down
58 changes: 41 additions & 17 deletions src/main/java/me/xmrvizzy/skyblocker/utils/render/RenderHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import net.minecraft.util.math.Vec3d;
import org.joml.Matrix3f;
import org.joml.Matrix4f;
import org.joml.Vector3f;
import org.lwjgl.opengl.GL11;

import java.awt.*;
Expand Down Expand Up @@ -109,6 +108,8 @@ public static void renderOutline(WorldRenderContext context, Box box, float[] co
* Draws lines from point to point.<br><br>
* <p>
* Tip: To draw lines from the center of a block, offset the X, Y and Z each by 0.5
* <p>
* Note: This is super messed up when drawing long lines. Tried different normals and {@link DrawMode#LINES} but nothing worked.
*
* @param context The WorldRenderContext which supplies the matrices and tick delta
* @param points The points from which to draw lines between
Expand All @@ -125,7 +126,7 @@ public static void renderLinesFromPoints(WorldRenderContext context, Vec3d[] poi

Tessellator tessellator = RenderSystem.renderThreadTesselator();
BufferBuilder buffer = tessellator.getBuffer();
Matrix4f projectionMatrix = matrices.peek().getPositionMatrix();
Matrix4f positionMatrix = matrices.peek().getPositionMatrix();
Matrix3f normalMatrix = matrices.peek().getNormalMatrix();

GL11.glEnable(GL11.GL_LINE_SMOOTH);
Expand All @@ -142,14 +143,11 @@ public static void renderLinesFromPoints(WorldRenderContext context, Vec3d[] poi
buffer.begin(DrawMode.LINE_STRIP, VertexFormats.LINES);

for (int i = 0; i < points.length; i++) {
Vec3d point = points[i];
Vec3d nextPoint = (i + 1 == points.length) ? points[i - 1] : points[i + 1];
Vector3f normalVec = new Vector3f((float) nextPoint.getX(), (float) nextPoint.getY(), (float) nextPoint.getZ()).sub((float) point.getX(), (float) point.getY(), (float) point.getZ()).normalize();

Vec3d normalVec = points[(i + 1) % points.length].subtract(points[i]).normalize();
buffer
.vertex(projectionMatrix, (float) point.getX(), (float) point.getY(), (float) point.getZ())
.vertex(positionMatrix, (float) points[i].getX(), (float) points[i].getY(), (float) points[i].getZ())
.color(colorComponents[0], colorComponents[1], colorComponents[2], alpha)
.normal(normalMatrix, normalVec.x, normalVec.y, normalVec.z)
.normal(normalMatrix, (float) normalVec.x, (float) normalVec.y, (float) normalVec.z)
.next();
}

Expand All @@ -164,24 +162,50 @@ public static void renderLinesFromPoints(WorldRenderContext context, Vec3d[] poi
RenderSystem.disableDepthTest();
}

public static void renderText(WorldRenderContext context, Text text, Vec3d pos, boolean seeThrough) {
renderText(context, text, pos, 1, seeThrough);
public static void renderQuad(WorldRenderContext context, Vec3d[] points, float[] colorComponents, float alpha, boolean throughWalls) {
Vec3d camera = context.camera().getPos();
MatrixStack matrices = context.matrixStack();

matrices.push();
matrices.translate(-camera.x, -camera.y, -camera.z);

Tessellator tessellator = RenderSystem.renderThreadTesselator();
BufferBuilder buffer = tessellator.getBuffer();
Matrix4f positionMatrix = matrices.peek().getPositionMatrix();

RenderSystem.setShader(GameRenderer::getPositionColorProgram);
RenderSystem.setShaderColor(1f, 1f, 1f, 1f);
RenderSystem.depthFunc(throughWalls ? GL11.GL_ALWAYS : GL11.GL_LEQUAL);

buffer.begin(DrawMode.QUADS, VertexFormats.POSITION_COLOR);
for (int i = 0; i < 4; i++) {
buffer.vertex(positionMatrix, (float) points[i].getX(), (float) points[i].getY(), (float) points[i].getZ()).color(colorComponents[0], colorComponents[1], colorComponents[2], alpha).next();
}
tessellator.draw();

RenderSystem.depthFunc(GL11.GL_LEQUAL);

matrices.pop();
}

public static void renderText(WorldRenderContext context, Text text, Vec3d pos, boolean throughWalls) {
renderText(context, text, pos, 1, throughWalls);
}

public static void renderText(WorldRenderContext context, Text text, Vec3d pos, float scale, boolean seeThrough) {
renderText(context, text, pos, scale, 0, seeThrough);
public static void renderText(WorldRenderContext context, Text text, Vec3d pos, float scale, boolean throughWalls) {
renderText(context, text, pos, scale, 0, throughWalls);
}

public static void renderText(WorldRenderContext context, Text text, Vec3d pos, float scale, float yOffset, boolean seeThrough) {
renderText(context, text.asOrderedText(), pos, scale, yOffset, seeThrough);
public static void renderText(WorldRenderContext context, Text text, Vec3d pos, float scale, float yOffset, boolean throughWalls) {
renderText(context, text.asOrderedText(), pos, scale, yOffset, throughWalls);
}

/**
* Renders text in the world space.
*
* @param seeThrough Whether the text should be able to be seen through walls or not.
* @param throughWalls whether the text should be able to be seen through walls or not.
*/
public static void renderText(WorldRenderContext context, OrderedText text, Vec3d pos, float scale, float yOffset, boolean seeThrough) {
public static void renderText(WorldRenderContext context, OrderedText text, Vec3d pos, float scale, float yOffset, boolean throughWalls) {
MatrixStack matrices = context.matrixStack();
Vec3d camera = context.camera().getPos();
TextRenderer textRenderer = client.textRenderer;
Expand All @@ -201,7 +225,7 @@ public static void renderText(WorldRenderContext context, OrderedText text, Vec3
BufferBuilder buffer = tessellator.getBuffer();
VertexConsumerProvider.Immediate consumers = VertexConsumerProvider.immediate(buffer);

RenderSystem.depthFunc(seeThrough ? GL11.GL_ALWAYS : GL11.GL_LEQUAL);
RenderSystem.depthFunc(throughWalls ? GL11.GL_ALWAYS : GL11.GL_LEQUAL);

textRenderer.draw(text, xOffset, yOffset, 0xFFFFFFFF, false, positionMatrix, consumers, TextRenderer.TextLayerType.SEE_THROUGH, 0, LightmapTextureManager.MAX_LIGHT_COORDINATE);
consumers.draw();
Expand Down

0 comments on commit dc24812

Please sign in to comment.