Skip to content

Commit

Permalink
fix: mined echoes remain on screen
Browse files Browse the repository at this point in the history
Fixes: #18
  • Loading branch information
mzdun committed Oct 7, 2024
1 parent 9c3781f commit f14b3c7
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package com.midnightbits.scanner.fabric;

import com.midnightbits.scanner.rt.core.fabric.MinecraftClientCore;
import com.midnightbits.scanner.sonar.Sonar;
import com.midnightbits.scanner.sonar.graphics.AbstractAnimatorHost;
import com.midnightbits.scanner.sonar.graphics.GraphicContext;
Expand All @@ -24,16 +25,11 @@ public void initialize(Sonar source) {
this.source = source;
ClientTickEvents.END_CLIENT_TICK.register((client) -> {
this.tick(Clock.currentTimeMillis());
this.source.remove(this.source.oldEchoes(new MinecraftClientCore(client)));
});
WorldRenderEvents.LAST.register(this::renderLevel);
}

@Override
public void tick(long now) {
super.tick(now);
this.source.removeOldEchoes();
}

private static final class GatherShimmers implements GraphicContext {
List<Shimmers> cloud = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.midnightbits.scanner.platform.PlatformInterface;
import com.midnightbits.scanner.rt.core.Id;
import com.midnightbits.scanner.sonar.graphics.AbstractAnimatorHost;
import com.midnightbits.scanner.sonar.graphics.ColorDefaults;
import com.midnightbits.scanner.utils.CacheableValue;
import com.midnightbits.scanner.utils.Manifests;

Expand Down Expand Up @@ -64,7 +63,6 @@ public void playSample(Sample id) {
FabricSoundManager.playSample(id);
}


@Override
public Map<Id, Integer> getBlockTagColors() {
return MinecraftColorPalette.BLOCK_TAG_COLORS;
Expand Down
49 changes: 31 additions & 18 deletions fabric/any/src/main/java/com/midnightbits/scanner/fabric/Pixel.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import com.midnightbits.scanner.rt.core.Id;
import com.midnightbits.scanner.rt.core.fabric.Minecraft;
import org.joml.Matrix4f;

import com.midnightbits.scanner.rt.math.V3i;
Expand All @@ -16,20 +18,22 @@
import com.mojang.blaze3d.systems.RenderSystem;

import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext;
import net.minecraft.client.render.BufferBuilder;
import net.minecraft.client.render.BufferRenderer;
import net.minecraft.client.render.Camera;
import net.minecraft.client.render.GameRenderer;
import net.minecraft.client.render.Tessellator;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.render.VertexFormat;
import net.minecraft.client.render.VertexFormats;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.ColorHelper;
import net.minecraft.util.math.Vec3d;

public class Pixel {
private record Vertex(int dx, int dy, int dz) {
void apply(BufferBuilder buffer, Matrix4f matrix, int argb) {
void apply(VertexConsumer buffer, Matrix4f matrix, int argb) {
buffer.vertex(matrix, dx, dy, dz).color(argb);
}
}
Expand Down Expand Up @@ -60,31 +64,37 @@ void apply(BufferBuilder buffer, Matrix4f matrix, int argb) {
final static int SIDE_Z1 = 1 << 2;
final static int ALL_SIDES = SIDE_X0 | SIDE_X1 | SIDE_Y0 | SIDE_Y1 | SIDE_Z0 | SIDE_Z1;

private static final Id EMPTY_ID = Id.of("", "");

final V3i position;
final Id id;
final int argb;
int sides = ALL_SIDES;
final double distanceSquared;

Pixel(V3i position, int argb) {
Pixel(V3i position, Id id, int argb, Vec3d camera) {
this.position = position;
this.id = id;
this.argb = argb;
this.distanceSquared = new BlockPos(Minecraft.vec3iOf(position)).toCenterPos().squaredDistanceTo(camera);
}

public V3i position() {
return position;
}

static Pixel of(BlockEcho echo) {
return new Pixel(echo.position(), echo.argb32());
static Pixel of(BlockEcho echo, Vec3d camera) {
return new Pixel(echo.position(), echo.id(), echo.argb32(), camera);
}

void draw(BufferBuilder buffer, MatrixStack stack, Camera camera) {
void draw(VertexConsumer buffer, MatrixStack matrices, Camera camera) {
final var cam = camera.getPos();
final var x = position.getX() - cam.x;
final var y = position.getY() - cam.y;
final var z = position.getZ() - cam.z;
stack.push();
stack.translate(x, y, z);
final var m = stack.peek().getPositionMatrix();
matrices.push();
matrices.translate(x, y, z);
final var m = matrices.peek().getPositionMatrix();

for (var side = 0; side < triangles.length; ++side) {
final var flag = 1 << side;
Expand All @@ -98,30 +108,33 @@ void draw(BufferBuilder buffer, MatrixStack stack, Camera camera) {
}
}

stack.pop();
matrices.pop();
}

public static void renderLevel(WorldRenderContext context, Iterable<BlockEcho> echoes, List<Shimmers> shimmers) {
final var frustum = context.frustum();
assert frustum != null;

final var camera = context.camera();
final var cameraPosF = camera.getPos();

final var allPixels = StreamSupport
.stream(echoes.spliterator(), false)
.map(Pixel::of)
.map((echo) -> Pixel.of(echo, cameraPosF))
.collect(Collectors.toSet());

for (final var shimmer : shimmers) {
final var alpha = (int) (shimmer.alpha() * 255.0 / 8.0 + .5);
int argbWalls = ColorHelper.Argb.withAlpha(alpha, 0x8080FF);

for (final var pos : shimmer.blocks()) {
allPixels.add(new Pixel(pos, argbWalls));
allPixels.add(new Pixel(pos, EMPTY_ID, argbWalls, cameraPosF));
}
}

Mesh.cleanPixels(allPixels);

final var visiblePixels = allPixels
final var visiblePixels = new java.util.ArrayList<>(allPixels
.stream()
.filter((pixel) -> {
if ((pixel.sides & Pixel.ALL_SIDES) == 0) {
Expand All @@ -132,15 +145,15 @@ public static void renderLevel(WorldRenderContext context, Iterable<BlockEcho> e
(pos.getZ() + 1));
return frustum.isVisible(box);
})
.collect(Collectors.toSet());
.toList());
visiblePixels.sort((lhs, rhs) -> Double.compare(rhs.distanceSquared, lhs.distanceSquared));

if (visiblePixels.isEmpty() && shimmers.isEmpty()) {
return;
}

final var camera = context.camera();
final var stack = context.matrixStack();
assert stack != null;
final var matrices = context.matrixStack();
assert matrices != null;

RenderSystem.disableDepthTest();
RenderSystem.enableBlend();
Expand All @@ -154,7 +167,7 @@ public static void renderLevel(WorldRenderContext context, Iterable<BlockEcho> e
final var buffer = tessellator.begin(VertexFormat.DrawMode.TRIANGLES, VertexFormats.POSITION_COLOR);

for (final var pixel : visiblePixels) {
pixel.draw(buffer, stack, camera);
pixel.draw(buffer, matrices, camera);
}

final var end = buffer.endNullable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;
import java.util.function.Predicate;
import java.util.stream.Stream;

import com.midnightbits.scanner.rt.core.ClientCore;
import com.midnightbits.scanner.rt.core.Id;
import com.midnightbits.scanner.rt.math.V3i;
import com.midnightbits.scanner.utils.Clock;
Expand Down Expand Up @@ -58,9 +60,21 @@ public BlockEcho echoFrom(BlockEcho.Partial partial) {
return echo;
}

public void removeOldEchoes() {
public void remove(Predicate<BlockEcho> whichOnes) {
evictBlocks(stream().filter(whichOnes));
}

public Predicate<BlockEcho> oldEchoes(ClientCore client) {
final var now = Clock.currentTimeMillis();
evictBlocks(stream().filter(b -> (now - b.pingTime()) > lifetime));
return ((block) -> {
final var blockLifetime = now - block.pingTime();
if (blockLifetime > lifetime) {
return true;
}

final var info = client.getBlockInfo(block.position());
return !block.id().equals(info.getId());
});
}

private Stream<BlockEcho> stream() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,12 @@ public Iterable<BlockEcho> echoes() {
return echoes;
}

public void removeOldEchoes() {
echoes.removeOldEchoes();
public void remove(Predicate<BlockEcho> whichOnes) {
echoes.remove(whichOnes);
}

public Predicate<BlockEcho> oldEchoes(ClientCore client) {
return echoes.oldEchoes(client);
}

private static final class Reflections {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
import com.midnightbits.scanner.sonar.BlockEcho;
import com.midnightbits.scanner.sonar.BlockEchoes;
import com.midnightbits.scanner.sonar.graphics.Colors;
import com.midnightbits.scanner.test.mocks.MockClientCore;
import com.midnightbits.scanner.test.mocks.MockWorld;
import com.midnightbits.scanner.test.mocks.MockedClock;
import com.midnightbits.scanner.test.support.Iterables;

import org.junit.jupiter.api.Test;

public class BlockEchosTest {
private MockedClock clock = new MockedClock();
private static final MockClientCore client = new MockClientCore(V3i.ZERO, -90, 0, MockWorld.TEST_WORLD);

@Test
public void removesOldEchoes() {
Expand All @@ -23,26 +26,33 @@ public void removesOldEchoes() {
Iterables.assertEquals(new BlockEcho[] {}, echoes);

clock.timeStamp = 0x123456;
echoes.echoFrom(V3i.ZERO, Id.ofVanilla("coal_ore"), Colors.VANILLA);
echoes.echoFrom(new V3i(1, 0, 55), Id.ofVanilla("stone"), Colors.VANILLA);

clock.timeStamp = 0x123457;
echoes.echoFrom(new V3i(1, 1, 1), Id.ofVanilla("coal_ore"), Colors.VANILLA);
echoes.echoFrom(new V3i(1, 0, 56), Id.ofVanilla("diamond_ore"), Colors.VANILLA);

clock.timeStamp = 0x123458;
final var marker = echoes.echoFrom(new V3i(1, 2, 1), Id.ofVanilla("iron_ore"), Colors.VANILLA);
final var marker = echoes.echoFrom(new V3i(1, 0, 57), Id.ofVanilla("stone"), Colors.VANILLA);

clock.timeStamp = 0x123459;
echoes.echoFrom(new V3i(1, 2, 2), Id.ofVanilla("iron_ore"), Colors.VANILLA);
echoes.echoFrom(new V3i(1, 0, 58), Id.ofVanilla("gold_ore"), Colors.VANILLA);

clock.timeStamp = 0x12345A;
echoes.echoFrom(new V3i(1, 0, 59), Id.ofVanilla("diamond_ore"), Colors.VANILLA);

Iterables.assertEquals(new BlockEcho[] {
new BlockEcho(V3i.ZERO, Id.ofVanilla("coal_ore"), Colors.VANILLA, 0x123456),
new BlockEcho(new V3i(1, 1, 1), Id.ofVanilla("coal_ore"), Colors.VANILLA, 0x123457),
new BlockEcho(new V3i(1, 2, 1), Id.ofVanilla("iron_ore"), Colors.VANILLA, 0x123458),
new BlockEcho(new V3i(1, 2, 2), Id.ofVanilla("iron_ore"), Colors.VANILLA, 0x123459),
new BlockEcho(new V3i(1, 0, 55), Id.ofVanilla("stone"), Colors.VANILLA, 0x123456),
new BlockEcho(new V3i(1, 0, 56), Id.ofVanilla("diamond_ore"), Colors.VANILLA, 0x123457),
new BlockEcho(new V3i(1, 0, 57), Id.ofVanilla("stone"), Colors.VANILLA, 0x123458),
new BlockEcho(new V3i(1, 0, 58), Id.ofVanilla("gold_ore"), Colors.VANILLA, 0x123459),
new BlockEcho(new V3i(1, 0, 59), Id.ofVanilla("diamond_ore"), Colors.VANILLA, 0x12345A),
}, echoes);

clock.timeStamp = marker.pingTime() + 10000;
echoes.removeOldEchoes();
echoes.remove(echoes.oldEchoes(client));
Iterables.assertEquals(new BlockEcho[] {
new BlockEcho(new V3i(1, 2, 1), Id.ofVanilla("iron_ore"), Colors.VANILLA, 0x123458),
new BlockEcho(new V3i(1, 2, 2), Id.ofVanilla("iron_ore"), Colors.VANILLA, 0x123459),
new BlockEcho(new V3i(1, 0, 57), Id.ofVanilla("stone"), Colors.VANILLA, 0x123458),
new BlockEcho(new V3i(1, 0, 58), Id.ofVanilla("gold_ore"), Colors.VANILLA, 0x123459),
}, echoes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public class SonarTest {
Id.ofVanilla("netherite_block"),
};
private final MockedClock clock = new MockedClock();
private final static MockWorld TEST_WORLD = MockWorld.ofResource("test_world.txt");

private static class Setup {
Sonar sonar;
Expand Down Expand Up @@ -103,7 +102,7 @@ void checkNewSonarIsEmpty() {

@Test
void checkDownwardsDirectionFromMiddle_narrow() {
final var core = new MockClientCore(V3i.ZERO, -90, 0, TEST_WORLD);
final var core = new MockClientCore(V3i.ZERO, -90, 0, MockWorld.TEST_WORLD);
final var counter = new Counter();

clock.timeStamp = 0x123456;
Expand All @@ -115,7 +114,8 @@ void checkDownwardsDirectionFromMiddle_narrow() {
Iterables.assertEquals(new BlockEcho[] {
new BlockEcho(new V3i(0, 23, 0), Id.ofVanilla("deepslate_iron_ore"), Colors.ECHO_ALPHA | Colors.VANILLA,
offset + 23 * SlicePacer.DURATION),
new BlockEcho(new V3i(0, 25, 0), Id.ofVanilla("deepslate_diamond_ore"), Colors.ECHO_ALPHA | Colors.VANILLA,
new BlockEcho(new V3i(0, 25, 0), Id.ofVanilla("deepslate_diamond_ore"),
Colors.ECHO_ALPHA | Colors.VANILLA,
offset + 25 * SlicePacer.DURATION),
new BlockEcho(new V3i(0, 27, 0), Id.ofVanilla("diamond_ore"), Colors.ECHO_ALPHA | Colors.VANILLA,
offset + 27 * SlicePacer.DURATION),
Expand All @@ -133,7 +133,7 @@ void checkDownwardsDirectionFromMiddle_narrow() {
Assertions.assertEquals(5, counter.get());

clock.timeStamp = offset + 27 * SlicePacer.DURATION + TEST_ECHO_LIFETIME;
setup.sonar.removeOldEchoes();
setup.sonar.remove(setup.sonar.oldEchoes(core));
Iterables.assertEquals(new BlockEcho[] {
new BlockEcho(new V3i(0, 27, 0), Id.ofVanilla("diamond_ore"), Colors.ECHO_ALPHA | Colors.VANILLA,
offset + 27 * SlicePacer.DURATION),
Expand All @@ -146,7 +146,7 @@ void checkDownwardsDirectionFromMiddle_narrow() {

@Test
void checkDownwardsDirectionFromMiddleButOnlyDiamonds_narrow() {
final var core = new MockClientCore(V3i.ZERO, -90f, 0f, TEST_WORLD);
final var core = new MockClientCore(V3i.ZERO, -90f, 0f, MockWorld.TEST_WORLD);

clock.timeStamp = 0x123456;
final var setup = new Setup(narrowSonar(TEST_BLOCK_DISTANCE, Set.of(
Expand All @@ -156,7 +156,8 @@ void checkDownwardsDirectionFromMiddleButOnlyDiamonds_narrow() {

final var offset = 0x123456 + WaveAnimator.DURATION + SlicePacer.DURATION;
Iterables.assertEquals(new BlockEcho[] {
new BlockEcho(new V3i(0, 25, 0), Id.ofVanilla("deepslate_diamond_ore"), Colors.ECHO_ALPHA | Colors.VANILLA,
new BlockEcho(new V3i(0, 25, 0), Id.ofVanilla("deepslate_diamond_ore"),
Colors.ECHO_ALPHA | Colors.VANILLA,
offset + 25 * SlicePacer.DURATION),
new BlockEcho(new V3i(0, 27, 0), Id.ofVanilla("diamond_ore"), Colors.ECHO_ALPHA | Colors.VANILLA,
offset + 27 * SlicePacer.DURATION),
Expand All @@ -167,7 +168,7 @@ void checkDownwardsDirectionFromMiddleButOnlyDiamonds_narrow() {
void searchForGold() {
clock.timeStamp = 0x123456;

final var core = new MockClientCore(new V3i(-60, -60, -51), 0f, 0f, TEST_WORLD);
final var core = new MockClientCore(new V3i(-60, -60, -51), 0f, 0f, MockWorld.TEST_WORLD);
final var setup = new Setup(TEST_BLOCK_DISTANCE, TEST_BLOCK_RADIUS, TEST_ECHO_LIFETIME,
Set.of(Id.ofVanilla("gold_ore")));

Expand Down Expand Up @@ -257,7 +258,7 @@ void searchForGold() {

@Test
void searchForGold_narrow() {
final var core = new MockClientCore(new V3i(-60, -60, -51), 0f, 0f, TEST_WORLD);
final var core = new MockClientCore(new V3i(-60, -60, -51), 0f, 0f, MockWorld.TEST_WORLD);

clock.timeStamp = 0x123456;
final var setup = new Setup(narrowSonar(TEST_BLOCK_DISTANCE, Set.of(Id.ofVanilla("gold_ore"))));
Expand All @@ -278,7 +279,7 @@ void searchForGold_narrow() {

@Test
void checkAnotherDirectionFromMiddle_narrow() {
final var core = new MockClientCore(V3i.ZERO, -75f, 180f, TEST_WORLD);
final var core = new MockClientCore(V3i.ZERO, -75f, 180f, MockWorld.TEST_WORLD);

clock.timeStamp = 0x123456;
final var setup = new Setup(narrowSonar());
Expand Down
Loading

0 comments on commit f14b3c7

Please sign in to comment.