From f70512105347c7bc45cac4d6f19553d346e04c8b Mon Sep 17 00:00:00 2001 From: asiekierka Date: Sat, 12 Dec 2015 23:43:22 +0100 Subject: [PATCH] optimize bundled cable propagation --- .../asie/charset/wires/TileWireContainer.java | 10 +- .../pl/asie/charset/wires/logic/Wire.java | 10 +- .../asie/charset/wires/logic/WireBundled.java | 186 +++++++++--------- .../asie/charset/wires/logic/WireNormal.java | 16 +- 4 files changed, 114 insertions(+), 108 deletions(-) diff --git a/src/main/java/pl/asie/charset/wires/TileWireContainer.java b/src/main/java/pl/asie/charset/wires/TileWireContainer.java index 58f033cf..21dbf941 100644 --- a/src/main/java/pl/asie/charset/wires/TileWireContainer.java +++ b/src/main/java/pl/asie/charset/wires/TileWireContainer.java @@ -102,7 +102,7 @@ public void update() { if (scheduledPropagationUpdate) { scheduledPropagationUpdate = false; - onWireUpdate(null); + onWireUpdate(null, -1); } if (scheduledRenderUpdate) { @@ -312,16 +312,16 @@ protected boolean hasWires() { return false; } - public void updateWireLocation(WireFace loc) { + public void updateWireLocation(WireFace loc, int color) { if (wires[loc.ordinal()] != null) { - wires[loc.ordinal()].propagate(); + wires[loc.ordinal()].propagate(color); } } - public void onWireUpdate(EnumFacing side) { + public void onWireUpdate(EnumFacing side, int color) { for (Wire w : wires) { if (w != null && (side == null || w.connects(side))) { - w.propagate(); + w.propagate(color); } } } diff --git a/src/main/java/pl/asie/charset/wires/logic/Wire.java b/src/main/java/pl/asie/charset/wires/logic/Wire.java index 001dae54..6f212254 100644 --- a/src/main/java/pl/asie/charset/wires/logic/Wire.java +++ b/src/main/java/pl/asie/charset/wires/logic/Wire.java @@ -31,7 +31,7 @@ public Wire(WireKind type, WireFace location, TileWireContainer container) { this.container = container; } - public abstract void propagate(); + public abstract void propagate(int color); public abstract int getSignalLevel(); public abstract int getRedstoneLevel(); @@ -88,18 +88,18 @@ public void updateConnections() { } } - protected void propagateNotifyCorner(EnumFacing side, EnumFacing direction) { + protected void propagateNotifyCorner(EnumFacing side, EnumFacing direction, int color) { BlockPos cornerPos = container.getPos().offset(side).offset(direction); TileEntity tile = container.getWorld().getTileEntity(cornerPos); if (tile instanceof TileWireContainer) { - ((TileWireContainer) tile).onWireUpdate(null); + ((TileWireContainer) tile).onWireUpdate(null, color); } } - protected void propagateNotify(EnumFacing facing) { + protected void propagateNotify(EnumFacing facing, int color) { TileEntity nt = container.getNeighbourTile(facing); if (nt instanceof TileWireContainer) { - ((TileWireContainer) nt).updateWireLocation(location); + ((TileWireContainer) nt).updateWireLocation(location, color); } else if (nt instanceof IBundledUpdatable) { ((IBundledUpdatable) nt).onBundledInputChanged(facing.getOpposite()); } else if (nt instanceof IRedstoneUpdatable) { diff --git a/src/main/java/pl/asie/charset/wires/logic/WireBundled.java b/src/main/java/pl/asie/charset/wires/logic/WireBundled.java index bd4b38b3..a1063fd8 100644 --- a/src/main/java/pl/asie/charset/wires/logic/WireBundled.java +++ b/src/main/java/pl/asie/charset/wires/logic/WireBundled.java @@ -12,9 +12,9 @@ import pl.asie.charset.api.wires.IBundledEmitter; import pl.asie.charset.api.wires.IWire; +import pl.asie.charset.api.wires.WireFace; import pl.asie.charset.wires.TileWireContainer; import pl.asie.charset.wires.WireKind; -import pl.asie.charset.api.wires.WireFace; public class WireBundled extends Wire { private int[] signalLevel = new int[16]; @@ -52,123 +52,129 @@ public void writeToNBT(NBTTagCompound nbt, boolean isPacket) { } } - @Override - public void propagate() { - if (DEBUG) { - System.out.println("--- B! PROPAGATE " + container.getPos().toString() + " " + location.name() + " ---"); - } - - byte[][] nValues = new byte[6][]; - - for (EnumFacing facing : EnumFacing.VALUES) { - if (connectsExternal(facing)) { - TileEntity tile = container.getNeighbourTile(facing); - if (tile instanceof IBundledEmitter && !(tile instanceof IWire)) { - nValues[facing.ordinal()] = ((IBundledEmitter) tile).getBundledSignal(location, facing.getOpposite()); + private void propagate(int i, byte[][] nValues) { + int maxSignal = 0; + int[] neighborLevel = new int[7]; + // [bundled] byte[] neighborValue = new byte[7]; + // [bundled] byte maxValue = 0; + + if (internalConnections > 0) { + for (WireFace location : WireFace.VALUES) { + if (connectsInternal(location)) { + neighborLevel[location.ordinal()] = container.getBundledSignalLevel(location, i); + // [bundled] neighborValue[location.ordinal()] = container.getBundledRedstoneLevel(location, i); } } } - for (int i = 0; i < 16; i++) { - int maxSignal = 0; - int oldSignal = signalLevel[i]; - byte oldValue = signalValue[i]; - int[] neighborLevel = new int[7]; - // [bundled] byte[] neighborValue = new byte[7]; - // [bundled] byte maxValue = 0; - - if (internalConnections > 0) { - for (WireFace location : WireFace.VALUES) { - if (connectsInternal(location)) { - neighborLevel[location.ordinal()] = container.getBundledSignalLevel(location, i); - // [bundled] neighborValue[location.ordinal()] = container.getBundledRedstoneLevel(location, i); - } - } - } - - for (EnumFacing facing : EnumFacing.VALUES) { - if (connectsExternal(facing)) { - if (nValues[facing.ordinal()] != null && nValues[facing.ordinal()][i] > 0) { - neighborLevel[facing.ordinal()] = 255; - // [bundled] neighborValue[facing.ordinal()] = nValues[facing.ordinal()][i]; - } else { - TileEntity tile = container.getNeighbourTile(facing); - - if (tile instanceof TileWireContainer) { - neighborLevel[facing.ordinal()] = ((TileWireContainer) tile).getBundledSignalLevel(location, i); - // [bundled] neighborValue[facing.ordinal()] = ((TileWireContainer) tile).getBundledRedstoneLevel(location, i); - } - } - } else if (connectsCorner(facing)) { - BlockPos cornerPos = container.getPos().offset(facing).offset(location.facing()); - TileEntity tile = container.getWorld().getTileEntity(cornerPos); + for (EnumFacing facing : EnumFacing.VALUES) { + if (connectsExternal(facing)) { + if (nValues[facing.ordinal()] != null && nValues[facing.ordinal()][i] > 0) { + neighborLevel[facing.ordinal()] = 255; + // [bundled] neighborValue[facing.ordinal()] = nValues[facing.ordinal()][i]; + } else { + TileEntity tile = container.getNeighbourTile(facing); if (tile instanceof TileWireContainer) { - neighborLevel[facing.ordinal()] = ((TileWireContainer) tile).getBundledSignalLevel(WireFace.get(facing.getOpposite()), i); - // [bundled] neighborValue[facing.ordinal()] = ((TileWireContainer) tile).getBundledRedstoneLevel(WireFace.get(facing.getOpposite()), i); + neighborLevel[facing.ordinal()] = ((TileWireContainer) tile).getBundledSignalLevel(location, i); + // [bundled] neighborValue[facing.ordinal()] = ((TileWireContainer) tile).getBundledRedstoneLevel(location, i); } } - } + } else if (connectsCorner(facing)) { + BlockPos cornerPos = container.getPos().offset(facing).offset(location.facing()); + TileEntity tile = container.getWorld().getTileEntity(cornerPos); - for (int j = 0; j < 7; j++) { - // [bundled] byte v = (byte) Math.min(neighborValue[j], 15); - if (neighborLevel[j] > maxSignal) { - maxSignal = neighborLevel[j]; - // [bundled] maxValue = v; + if (tile instanceof TileWireContainer) { + neighborLevel[facing.ordinal()] = ((TileWireContainer) tile).getBundledSignalLevel(WireFace.get(facing.getOpposite()), i); + // [bundled] neighborValue[facing.ordinal()] = ((TileWireContainer) tile).getBundledRedstoneLevel(WireFace.get(facing.getOpposite()), i); } } + } - if (DEBUG) { - System.out.println("[" + i + "] Levels: " + Arrays.toString(neighborLevel)); + for (int j = 0; j < 7; j++) { + // [bundled] byte v = (byte) Math.min(neighborValue[j], 15); + if (neighborLevel[j] > maxSignal) { + maxSignal = neighborLevel[j]; + // [bundled] maxValue = v; } + } - int newSignal = 0; - signalValue[i] = 0; + if (DEBUG) { + System.out.println("[" + i + "] Levels: " + Arrays.toString(neighborLevel)); + } - if (maxSignal > signalLevel[i] && maxSignal > 1) { - newSignal = maxSignal - 1; - signalValue[i] = 15; - } + int newSignal = 0; + signalValue[i] = 0; - signalLevel[i] = newSignal; + if (maxSignal > signalLevel[i] && maxSignal > 1) { + newSignal = maxSignal - 1; + signalValue[i] = 15; + } - if (newSignal == 0) { - for (WireFace nLoc : WireFace.VALUES) { - if (connectsInternal(nLoc) && neighborLevel[nLoc.ordinal()] > 0) { - container.updateWireLocation(nLoc); + signalLevel[i] = newSignal; + + if (newSignal == 0) { + for (WireFace nLoc : WireFace.VALUES) { + if (connectsInternal(nLoc) && neighborLevel[nLoc.ordinal()] > 0) { + container.updateWireLocation(nLoc, type.color()); + } else if (nLoc != WireFace.CENTER) { + EnumFacing facing = nLoc.facing(); + + if (connectsExternal(facing)) { + TileEntity tileEntity = container.getNeighbourTile(facing); + if (!(tileEntity instanceof TileWireContainer) || neighborLevel[facing.ordinal()] > 0) { + propagateNotify(facing, i); + } + } else if (connectsCorner(facing)) { + if (neighborLevel[facing.ordinal()] > 0) { + propagateNotifyCorner(location.facing(), facing, i); + } + } + } + } + } else { + for (WireFace nLoc : WireFace.VALUES) { + if (neighborLevel[nLoc.ordinal()] < newSignal - 1) { + if (connectsInternal(nLoc)) { + container.updateWireLocation(nLoc, type.color()); } else if (nLoc != WireFace.CENTER) { EnumFacing facing = nLoc.facing(); if (connectsExternal(facing)) { - TileEntity tileEntity = container.getNeighbourTile(facing); - if (!(tileEntity instanceof TileWireContainer) || neighborLevel[facing.ordinal()] > 0) { - propagateNotify(facing); - } + propagateNotify(facing, i); } else if (connectsCorner(facing)) { - if (neighborLevel[facing.ordinal()] > 0) { - propagateNotifyCorner(location.facing(), facing); - } + propagateNotifyCorner(location.facing(), facing, i); } } } - } else { - for (WireFace nLoc : WireFace.VALUES) { - if (neighborLevel[nLoc.ordinal()] < newSignal - 1 || neighborLevel[nLoc.ordinal()] > newSignal + 1) { - if (connectsInternal(nLoc)) { - container.updateWireLocation(nLoc); - } else if (nLoc != WireFace.CENTER) { - EnumFacing facing = nLoc.facing(); - - if (connectsExternal(facing)) { - propagateNotify(facing); - } else if (connectsCorner(facing)) { - propagateNotifyCorner(location.facing(), facing); - } - } - } + } + } + } + + @Override + public void propagate(int color) { + if (DEBUG) { + System.out.println("--- B! PROPAGATE " + container.getPos().toString() + " " + location.name() + " ---"); + } + + byte[][] nValues = new byte[6][]; + + for (EnumFacing facing : EnumFacing.VALUES) { + if (connectsExternal(facing)) { + TileEntity tile = container.getNeighbourTile(facing); + if (tile instanceof IBundledEmitter && !(tile instanceof IWire)) { + nValues[facing.ordinal()] = ((IBundledEmitter) tile).getBundledSignal(location, facing.getOpposite()); } } } + + if (color < 0) { + for (int i = 0; i < 16; i++) { + propagate(i, nValues); + } + } else { + propagate(color, nValues); + } } @Override diff --git a/src/main/java/pl/asie/charset/wires/logic/WireNormal.java b/src/main/java/pl/asie/charset/wires/logic/WireNormal.java index d92de428..da5280b6 100644 --- a/src/main/java/pl/asie/charset/wires/logic/WireNormal.java +++ b/src/main/java/pl/asie/charset/wires/logic/WireNormal.java @@ -68,7 +68,7 @@ protected byte getRedstoneLevel(TileWireContainer container, WireFace location) } @Override - public void propagate() { + public void propagate(int color) { if (DEBUG) { System.out.println("--- PROPAGATE " + container.getPos().toString() + " " + location.name() + " ---"); } @@ -206,7 +206,7 @@ public void propagate() { for (WireFace nLoc : WireFace.VALUES) { if (connectsInternal(nLoc)) { if (neighborLevel[nLoc.ordinal()] > 0) { - container.updateWireLocation(nLoc); + container.updateWireLocation(nLoc, type.color()); } } else if (nLoc != WireFace.CENTER) { EnumFacing facing = nLoc.facing(); @@ -214,11 +214,11 @@ public void propagate() { if (connectsExternal(facing)) { TileEntity tileEntity = container.getNeighbourTile(facing); if (!(tileEntity instanceof TileWireContainer) || neighborLevel[facing.ordinal()] > 0) { - propagateNotify(facing); + propagateNotify(facing, type.color()); } } else if (connectsCorner(facing)) { if (neighborLevel[nLoc.ordinal()] > 0) { - propagateNotifyCorner(location.facing(), facing); + propagateNotifyCorner(location.facing(), facing, type.color()); } } else if (type == WireKind.NORMAL && facing.getOpposite() != location.facing()) { TileEntity nt = container.getNeighbourTile(facing); @@ -232,15 +232,15 @@ public void propagate() { for (WireFace nLoc : WireFace.VALUES) { if (neighborLevel[nLoc.ordinal()] < signalLevel - 1) { if (connectsInternal(nLoc)) { - container.updateWireLocation(nLoc); + container.updateWireLocation(nLoc, type.color()); } else if (nLoc != WireFace.CENTER) { EnumFacing facing = nLoc.facing(); if (connectsExternal(facing)) { - propagateNotify(facing); + propagateNotify(facing, type.color()); } else if (connectsCorner(facing)) { - propagateNotifyCorner(location.facing(), facing); - } else if (type == WireKind.NORMAL) { + propagateNotifyCorner(location.facing(), facing, type.color()); + } else if (type == WireKind.NORMAL && facing.getOpposite() != location.facing()) { TileEntity nt = container.getNeighbourTile(facing); if (!(nt instanceof IRedstoneUpdatable)) { container.getWorld().notifyBlockOfStateChange(container.getPos().offset(facing), container.getBlockType());