Skip to content

Commit

Permalink
feat: adding new level of indirection
Browse files Browse the repository at this point in the history
Now we have:

- limited number of `Echo`s -- they should be ideally only one per Id,
  for now it is one per one scan; in the future, the label might be also
  stored there
- `BlockEcho` is an `Echo` with a position and ping time.
- `BlockEchoes` is a container for all known `BlockEchoes`
  • Loading branch information
mzdun committed Oct 9, 2024
1 parent fa0b40a commit 1a8b096
Show file tree
Hide file tree
Showing 14 changed files with 362 additions and 288 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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.Colors;
import com.midnightbits.scanner.utils.CacheableValue;
import com.midnightbits.scanner.utils.Manifests;

Expand Down Expand Up @@ -64,7 +65,7 @@ public void playSample(Sample id) {
}

@Override
public Map<Id, Integer> getBlockTagColors() {
public Map<Id, Colors.Proxy> getBlockTagColors() {
return MinecraftColorPalette.BLOCK_TAG_COLORS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,52 @@
package com.midnightbits.scanner.fabric;

import com.midnightbits.scanner.rt.core.Id;
import com.midnightbits.scanner.sonar.graphics.Colors;
import net.minecraft.block.MapColor;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

public class MinecraftColorPalette {
public static final Map<Id, Integer> BLOCK_TAG_COLORS;
public static final Map<Id, Colors.Proxy> BLOCK_TAG_COLORS;

private static class MapColorProxy implements Colors.Proxy {
MapColor target;

MapColorProxy(MapColor target) {
this.target = target;
}

@Override
public int rgb24() {
return MapColor.get(target.id).color;
}


@Override
public boolean equals(Colors.Proxy other) {
return (other instanceof MapColorProxy ref) && ref.target.id == target.id;
}

@Override
public int compareTo(@NotNull Colors.Proxy other) {
if (!(other instanceof MapColorProxy ref)) {
throw new ClassCastException();
}
return target.id - ref.target.id;
}
}

static {
BLOCK_TAG_COLORS = new HashMap<>();
BLOCK_TAG_COLORS.put(Id.ofVanilla("lapis_ores"), MapColor.LAPIS_BLUE.color);
BLOCK_TAG_COLORS.put(Id.ofVanilla("redstone_ores"), MapColor.RED.color);
BLOCK_TAG_COLORS.put(Id.ofVanilla("copper_ores"), MapColor.TERRACOTTA_ORANGE.color);
BLOCK_TAG_COLORS.put(Id.ofVanilla("coal_ores"), MapColor.GRAY.color);
BLOCK_TAG_COLORS.put(Id.ofVanilla("emerald_ores"), MapColor.EMERALD_GREEN.color);
BLOCK_TAG_COLORS.put(Id.ofVanilla("iron_ores"), MapColor.BROWN.color);
BLOCK_TAG_COLORS.put(Id.ofVanilla("diamond_ores"), MapColor.DIAMOND_BLUE.color);
BLOCK_TAG_COLORS.put(Id.ofVanilla("gold_ores"), MapColor.GOLD.color);
BLOCK_TAG_COLORS.put(Id.ofVanilla("lapis_ores"), new MapColorProxy(MapColor.LAPIS_BLUE));
BLOCK_TAG_COLORS.put(Id.ofVanilla("redstone_ores"), new MapColorProxy(MapColor.RED));
BLOCK_TAG_COLORS.put(Id.ofVanilla("copper_ores"), new MapColorProxy(MapColor.TERRACOTTA_ORANGE));
BLOCK_TAG_COLORS.put(Id.ofVanilla("coal_ores"), new MapColorProxy(MapColor.GRAY));
BLOCK_TAG_COLORS.put(Id.ofVanilla("emerald_ores"), new MapColorProxy(MapColor.EMERALD_GREEN));
BLOCK_TAG_COLORS.put(Id.ofVanilla("iron_ores"), new MapColorProxy(MapColor.BROWN));
BLOCK_TAG_COLORS.put(Id.ofVanilla("diamond_ores"), new MapColorProxy(MapColor.DIAMOND_BLUE));
BLOCK_TAG_COLORS.put(Id.ofVanilla("gold_ores"), new MapColorProxy(MapColor.GOLD));
}
}
2 changes: 1 addition & 1 deletion scanner/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies {
implementation 'com.google.code.gson:gson:2.11.0'
implementation 'org.joml:joml:1.10.8'
implementation 'org.slf4j:slf4j-api:2.0.+'
implementation 'org.slf4j:slf4j-log4j13:1.0.+'
implementation 'org.slf4j:slf4j-log4j12:2.0.+'
implementation 'org.apache.commons:commons-text:1.12.0'

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.3'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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.sonar.graphics.Colors;

import java.nio.file.Path;
import java.util.Map;
Expand Down Expand Up @@ -35,7 +36,7 @@ default String getEnvironmentName() {

void playSample(Sample id);

default Map<Id, Integer> getBlockTagColors() {
default Map<Id, Colors.Proxy> getBlockTagColors() {
return ColorDefaults.BLOCK_TAG_COLORS;
}
}
125 changes: 50 additions & 75 deletions scanner/src/main/java/com/midnightbits/scanner/sonar/BlockEcho.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,116 +7,91 @@
import com.midnightbits.scanner.rt.math.V3i;
import com.midnightbits.scanner.sonar.graphics.Colors;
import com.midnightbits.scanner.utils.Clock;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;
public record BlockEcho(V3i position, Echo echo, long pingTime) implements Comparable<BlockEcho> {
public record Partial(V3i position, Echo echo) {
public static Partial of(V3i position, Echo echo) {
return new Partial(position, echo);
}

public record BlockEcho(V3i position, Id id, int argb32, long pingTime) implements Comparable<BlockEcho> {
public record Partial(V3i position, Id id, int argb32) {
public static Partial of(int x, int y, int z, Echo echo) {
return of(new V3i(x, y, z), echo);
}

public boolean equals(Object obj) {
if (obj == null) {
throw new NullPointerException();
}
public Id id() {
return echo.id();
}

public Colors.Proxy color() {
return echo.color();
}

public boolean equals(@NotNull Object obj) {
if (!(obj instanceof Partial other)) {
throw new ClassCastException();
}
return id.equals(other.id) && position.equals(other.position) && argb32 == other.argb32;
return echo.equals(other.echo) && position.equals(other.position);
}

@Override
public String toString() {
final var builder = new StringBuilder();
builder.append("new BlockEcho.Partial(new V3i(")
.append(position.getX()).append(", ")
.append(position.getY()).append(", ")
.append(position.getZ()).append("), Id.of");
if (Objects.equals(id.getNamespace(), Id.DEFAULT_NAMESPACE)) {
builder.append("Vanilla(\"");
} else {
builder.append("(\"").append(id.getNamespace()).append("\", \"");
}
return builder.append(id.getPath()).append("\"), ").append(colorOf(argb32)).append(")").toString();
return "BlockEcho.Partial.of(" +
position.getX() + ", " +
position.getY() + ", " +
position.getZ() + ", " +
echo + ")";
}
}

public static BlockEcho echoFrom(V3i position, Id id, int argb32) {
return new BlockEcho(position, id, argb32, Clock.currentTimeMillis());
public BlockEcho(int x, int y, int z, Echo echo, long pingTime) {
this(new V3i(x, y, z), echo, pingTime);
}

public static BlockEcho echoFrom(Partial partial) {
return echoFrom(partial.position, partial.id, partial.argb32);
return new BlockEcho(partial.position, partial.echo, Clock.currentTimeMillis());
}

public boolean equals(Object obj) {
if (obj == null) {
throw new NullPointerException();
}
if (!(obj instanceof BlockEcho other)) {
throw new ClassCastException();
}
return pingTime == other.pingTime && id.equals(other.id) && position.equals(other.position) && argb32 == other.argb32;
public static BlockEcho echoFrom(int x, int y, int z, Echo echo) {
return echoFrom(Partial.of(x, y, z, echo));
}

@Override
public String toString() {
final var builder = new StringBuilder();
builder.append("\nnew BlockEcho(new V3i(")
.append(position.getX()).append(", ")
.append(position.getY()).append(", ")
.append(position.getZ()).append("), Id.of");
if (Objects.equals(id.getNamespace(), Id.DEFAULT_NAMESPACE)) {
builder.append("Vanilla(\"");
} else {
builder.append("(\"").append(id.getNamespace()).append("\", \"");
}
builder.append(id.getPath()).append("\"), ")
.append(colorOf(argb32)).append(", ")
.append(pingTime).append(")");
return builder.toString();
public Id id() {
return echo.id();
}

private static String colorOf(int argb32) {
String alpha = "";
if ((argb32 & ~Colors.RGB_MASK) == Colors.ECHO_ALPHA) {
argb32 &= Colors.RGB_MASK;
alpha = "Colors.ECHO_ALPHA | ";
}

if ((argb32 & ~Colors.RGB_MASK) == 0)
return alpha + rgbOf(argb32);

return String.format("0x%08X", argb32);
public Colors.Proxy color() {
return echo.color();
}

private static String rgbOf(int rgb24) {
if (rgb24 == Colors.VANILLA) {
return "Colors.VANILLA";
}
if (rgb24 == Colors.BROWN) {
return "Colors.BROWN";
public boolean equals(@NotNull Object obj) {
if (!(obj instanceof BlockEcho other)) {
throw new ClassCastException();
}
return String.format("0x%06X", rgb24);
return pingTime == other.pingTime && echo.equals(other.echo) && position.equals(other.position);
}

@Override
public int compareTo(@Nullable BlockEcho other) {
if (other == null) {
throw new NullPointerException();
}
public String toString() {
return "\nnew BlockEcho(BlockEcho.Partial.of(" +
position.getX() + ", " +
position.getY() + ", " +
position.getZ() + ", " +
echo + "), " +
pingTime + ")";
}

@Override
public int compareTo(@NotNull BlockEcho other) {
var result = (int) (pingTime - other.pingTime);
if (result != 0) {
return result;
}

result = id.compareTo(other.id);
if (result != 0)
return result;

result = position.compareTo(other.position);
result = echo.compareTo(other.echo);
if (result != 0)
return result;

return argb32 - other.argb32;
return position.compareTo(other.position);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
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 @@ -41,8 +40,8 @@ public void refresh(int lifetime) {
* @param id what did the echo bounced off of
* @return resulting block
*/
public BlockEcho echoFrom(V3i position, Id id, int argb32) {
return echoFrom(new BlockEcho.Partial(position, id, argb32));
public BlockEcho echoFrom(int x, int y, int z, Echo echo) {
return echoFrom(new BlockEcho.Partial(new V3i(x, y, z), echo));
}

/**
Expand All @@ -57,11 +56,14 @@ public BlockEcho echoFrom(BlockEcho.Partial partial) {

BlockEcho echo = BlockEcho.echoFrom(partial);
echoes.add(echo);
splitToNuggets();
return echo;
}

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

public Predicate<BlockEcho> oldEchoes(ClientCore client) {
Expand All @@ -84,11 +86,16 @@ private Stream<BlockEcho> stream() {
return echoes.stream();
}

private void evictBlocks(Stream<BlockEcho> stream) {
private boolean evictBlocks(Stream<BlockEcho> stream) {
final var oldSize = echoes.size();
List<BlockEcho> evictions = stream.toList();
for (BlockEcho evicted : evictions) {
echoes.remove(evicted);
}
return oldSize != echoes.size();
}

private void splitToNuggets() {
}

@NotNull
Expand Down
Loading

0 comments on commit 1a8b096

Please sign in to comment.