diff --git a/src/main/java/com/hbm/hrist/ConDbg.java b/src/main/java/com/hbm/hrist/ConDbg.java index dcbe66074..9fb48bbfb 100644 --- a/src/main/java/com/hbm/hrist/ConDbg.java +++ b/src/main/java/com/hbm/hrist/ConDbg.java @@ -35,7 +35,7 @@ public class ConDbg { 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); } } } diff --git a/src/main/java/com/hbm/hrist/ConduitLine.java b/src/main/java/com/hbm/hrist/ConduitLine.java index 905455658..8adb2913d 100644 --- a/src/main/java/com/hbm/hrist/ConduitLine.java +++ b/src/main/java/com/hbm/hrist/ConduitLine.java @@ -4,10 +4,14 @@ import java.util.HashSet; import java.util.Set; 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 */ public class ConduitLine { + public World world; protected boolean valid = true; public LineEndpoint[] connectedTo = new LineEndpoint[2]; // a sausage always has two ends public Set constructedFrom = new HashSet(); @@ -16,6 +20,10 @@ public class ConduitLine { public double cachedDistance = 0; public boolean hasChanged = true; + public ConduitLine(World world) { + this.world = world; + } + public void setChanged() { this.hasChanged = true; } public double getDistance() { @@ -30,11 +38,14 @@ public class ConduitLine { public void invalidate() { this.valid = false; } public void destroy() { + ConduitWorld cWorld = ConduitSpace.worlds.get(world); for(ConnectionDefinition def : constructedFrom) { if(def.getLine() == this) { def.setLine(null); + if(cWorld != null && def.parent.valid) cWorld.orphans.add(def); } } + this.constructedFrom.clear(); this.invalidate(); } diff --git a/src/main/java/com/hbm/hrist/ConduitPiece.java b/src/main/java/com/hbm/hrist/ConduitPiece.java index c96f7a255..ac8dc8b48 100644 --- a/src/main/java/com/hbm/hrist/ConduitPiece.java +++ b/src/main/java/com/hbm/hrist/ConduitPiece.java @@ -6,25 +6,20 @@ import com.hbm.util.fauxpointtwelve.DirPos; public class ConduitPiece { 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 ConduitLine[] liveConnections; public ConduitPiece(ConnectionDefinition... defs) { definitions = defs; for(ConnectionDefinition def : defs) def.withParent(this); - liveConnections = new ConduitLine[defs.length]; } 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() { this.valid = false; - for(ConduitLine con : this.liveConnections) { - if(con != null) con.destroy(); + for(ConnectionDefinition def : this.definitions) { + if(def.liveConnection != null) def.liveConnection.destroy(); } } @@ -45,6 +40,7 @@ public class ConduitPiece { public ConduitPiece parent; public final DirPos[] connectors = new DirPos[2]; public final double distance; + public ConduitLine liveConnection; public ConnectionDefinition(DirPos start, DirPos end) { this(start, end, start.distanceTo(end)); @@ -62,18 +58,11 @@ public class ConduitPiece { } public ConduitLine getLine() { - if(parent == null) throw new IllegalStateException("Connection def has been initialized with no parent!"); // never happens - for(int i = 0; i < parent.definitions.length; i++) { - if(parent.definitions[i] == this) return parent.liveConnections[i]; - } - return null; + return liveConnection; } public void setLine(ConduitLine line) { - if(parent == null) throw new IllegalStateException("Connection def has been initialized with no parent!"); // never happens - for(int i = 0; i < parent.definitions.length; i++) { - if(parent.definitions[i] == this) this.parent.liveConnections[i] = line; - } + this.liveConnection = line; } } } diff --git a/src/main/java/com/hbm/hrist/ConduitSpace.java b/src/main/java/com/hbm/hrist/ConduitSpace.java index 4ae777d6f..1cc6409e0 100644 --- a/src/main/java/com/hbm/hrist/ConduitSpace.java +++ b/src/main/java/com/hbm/hrist/ConduitSpace.java @@ -20,7 +20,7 @@ public class ConduitSpace { public static void pushPiece(World world, ConduitPiece piece, BlockPos core) { ConduitWorld cWorld = worlds.get(world); if(cWorld == null) { - cWorld = new ConduitWorld(); + cWorld = new ConduitWorld(world); worlds.put(world, cWorld); } cWorld.push(piece, core); @@ -40,6 +40,8 @@ public class ConduitSpace { public static class ConduitWorld { + public World world; + /** Maps conduit core pos to the actual conduit piece logical unit, for access of the conduit blocks */ public Map pieces = new HashMap(); /** 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 */ public Set orphans = new LinkedHashSet(); + public ConduitWorld(World world) { + this.world = world; + } + public void push(ConduitPiece piece, BlockPos core) { pieces.put(core, piece); @@ -97,9 +103,10 @@ public class ConduitSpace { line = orphan.getLine(); // 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)) { orphan.setLine(connectedLine); + connectedLine.constructedFrom.add(orphan); } // if not, merge } else { @@ -119,7 +126,7 @@ public class ConduitSpace { } if(orphan.getLine() == null) { - ConduitLine newLine = new ConduitLine(); + ConduitLine newLine = new ConduitLine(world); orphan.setLine(newLine); newLine.constructedFrom.add(orphan); newLine.setChanged();