you know what, it's an improvement

This commit is contained in:
Boblet 2026-02-12 16:07:36 +01:00
parent d490bdd63e
commit f957a0fedf
4 changed files with 28 additions and 21 deletions

View File

@ -35,7 +35,7 @@ public class ConDbg {
int color = line.hashCode() & 0xFFFFFF; int color = line.hashCode() & 0xFFFFFF;
ParticleUtil.spawnDroneLine(world, x, y, z, dx, dy, dz, color, true); ParticleUtil.spawnDroneLine(world, x, y, z, dx, dy, dz, !line.valid ? 0xff0000 : color, true);
} }
} }
} }

View File

@ -4,10 +4,14 @@ import java.util.HashSet;
import java.util.Set; import java.util.Set;
import com.hbm.hrist.ConduitPiece.ConnectionDefinition; import com.hbm.hrist.ConduitPiece.ConnectionDefinition;
import com.hbm.hrist.ConduitSpace.ConduitWorld;
import net.minecraft.world.World;
/** Generated out of multiple ConnectionDefinitions to form a straight segment, ends at a branch */ /** Generated out of multiple ConnectionDefinitions to form a straight segment, ends at a branch */
public class ConduitLine { public class ConduitLine {
public World world;
protected boolean valid = true; protected boolean valid = true;
public LineEndpoint[] connectedTo = new LineEndpoint[2]; // a sausage always has two ends public LineEndpoint[] connectedTo = new LineEndpoint[2]; // a sausage always has two ends
public Set<ConnectionDefinition> constructedFrom = new HashSet(); public Set<ConnectionDefinition> constructedFrom = new HashSet();
@ -16,6 +20,10 @@ public class ConduitLine {
public double cachedDistance = 0; public double cachedDistance = 0;
public boolean hasChanged = true; public boolean hasChanged = true;
public ConduitLine(World world) {
this.world = world;
}
public void setChanged() { this.hasChanged = true; } public void setChanged() { this.hasChanged = true; }
public double getDistance() { public double getDistance() {
@ -30,11 +38,14 @@ public class ConduitLine {
public void invalidate() { this.valid = false; } public void invalidate() { this.valid = false; }
public void destroy() { public void destroy() {
ConduitWorld cWorld = ConduitSpace.worlds.get(world);
for(ConnectionDefinition def : constructedFrom) { for(ConnectionDefinition def : constructedFrom) {
if(def.getLine() == this) { if(def.getLine() == this) {
def.setLine(null); def.setLine(null);
if(cWorld != null && def.parent.valid) cWorld.orphans.add(def);
} }
} }
this.constructedFrom.clear();
this.invalidate(); this.invalidate();
} }

View File

@ -6,25 +6,20 @@ import com.hbm.util.fauxpointtwelve.DirPos;
public class ConduitPiece { public class ConduitPiece {
protected boolean valid = true; protected boolean valid = true;
// definitions and lines are always 1:1, definition of index 0 is associated with line of index 0
// could have probably solved this with a new class, now that i think of it
public ConnectionDefinition[] definitions; public ConnectionDefinition[] definitions;
public ConduitLine[] liveConnections;
public ConduitPiece(ConnectionDefinition... defs) { public ConduitPiece(ConnectionDefinition... defs) {
definitions = defs; definitions = defs;
for(ConnectionDefinition def : defs) def.withParent(this); for(ConnectionDefinition def : defs) def.withParent(this);
liveConnections = new ConduitLine[defs.length];
} }
public boolean isValid() { return this.valid; } public boolean isValid() { return this.valid; }
// if a piece goes offline, the entire connection dies with it ad has to be recalculated out of the surviving pieces // if a piece goes offline, the entire connection dies with it and has to be recalculated out of the surviving pieces
public void invalidate() { public void invalidate() {
this.valid = false; this.valid = false;
for(ConduitLine con : this.liveConnections) { for(ConnectionDefinition def : this.definitions) {
if(con != null) con.destroy(); if(def.liveConnection != null) def.liveConnection.destroy();
} }
} }
@ -45,6 +40,7 @@ public class ConduitPiece {
public ConduitPiece parent; public ConduitPiece parent;
public final DirPos[] connectors = new DirPos[2]; public final DirPos[] connectors = new DirPos[2];
public final double distance; public final double distance;
public ConduitLine liveConnection;
public ConnectionDefinition(DirPos start, DirPos end) { public ConnectionDefinition(DirPos start, DirPos end) {
this(start, end, start.distanceTo(end)); this(start, end, start.distanceTo(end));
@ -62,18 +58,11 @@ public class ConduitPiece {
} }
public ConduitLine getLine() { public ConduitLine getLine() {
if(parent == null) throw new IllegalStateException("Connection def has been initialized with no parent!"); // never happens return liveConnection;
for(int i = 0; i < parent.definitions.length; i++) {
if(parent.definitions[i] == this) return parent.liveConnections[i];
}
return null;
} }
public void setLine(ConduitLine line) { public void setLine(ConduitLine line) {
if(parent == null) throw new IllegalStateException("Connection def has been initialized with no parent!"); // never happens this.liveConnection = line;
for(int i = 0; i < parent.definitions.length; i++) {
if(parent.definitions[i] == this) this.parent.liveConnections[i] = line;
}
} }
} }
} }

View File

@ -20,7 +20,7 @@ public class ConduitSpace {
public static void pushPiece(World world, ConduitPiece piece, BlockPos core) { public static void pushPiece(World world, ConduitPiece piece, BlockPos core) {
ConduitWorld cWorld = worlds.get(world); ConduitWorld cWorld = worlds.get(world);
if(cWorld == null) { if(cWorld == null) {
cWorld = new ConduitWorld(); cWorld = new ConduitWorld(world);
worlds.put(world, cWorld); worlds.put(world, cWorld);
} }
cWorld.push(piece, core); cWorld.push(piece, core);
@ -40,6 +40,8 @@ public class ConduitSpace {
public static class ConduitWorld { public static class ConduitWorld {
public World world;
/** Maps conduit core pos to the actual conduit piece logical unit, for access of the conduit blocks */ /** Maps conduit core pos to the actual conduit piece logical unit, for access of the conduit blocks */
public Map<BlockPos, ConduitPiece> pieces = new HashMap(); public Map<BlockPos, ConduitPiece> pieces = new HashMap();
/** Maps a connection pos to a conduit piece, used for calculating connections. /** Maps a connection pos to a conduit piece, used for calculating connections.
@ -48,6 +50,10 @@ public class ConduitSpace {
/** Set of all definitions not yet part of a line */ /** Set of all definitions not yet part of a line */
public Set<ConnectionDefinition> orphans = new LinkedHashSet(); public Set<ConnectionDefinition> orphans = new LinkedHashSet();
public ConduitWorld(World world) {
this.world = world;
}
public void push(ConduitPiece piece, BlockPos core) { public void push(ConduitPiece piece, BlockPos core) {
pieces.put(core, piece); pieces.put(core, piece);
@ -97,9 +103,10 @@ public class ConduitSpace {
line = orphan.getLine(); line = orphan.getLine();
// if the current line is null // if the current line is null
if(line == null) { if(line == null || !line.valid) {
if(connectedDef.connectors[0].equals(connection) || connectedDef.connectors[1].equals(connection)) { if(connectedDef.connectors[0].equals(connection) || connectedDef.connectors[1].equals(connection)) {
orphan.setLine(connectedLine); orphan.setLine(connectedLine);
connectedLine.constructedFrom.add(orphan);
} }
// if not, merge // if not, merge
} else { } else {
@ -119,7 +126,7 @@ public class ConduitSpace {
} }
if(orphan.getLine() == null) { if(orphan.getLine() == null) {
ConduitLine newLine = new ConduitLine(); ConduitLine newLine = new ConduitLine(world);
orphan.setLine(newLine); orphan.setLine(newLine);
newLine.constructedFrom.add(orphan); newLine.constructedFrom.add(orphan);
newLine.setChanged(); newLine.setChanged();