Skip to content

Commit

Permalink
optimize bundled cable propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
asiekierka committed Dec 12, 2015
1 parent 4ebb56f commit f705121
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 108 deletions.
10 changes: 5 additions & 5 deletions src/main/java/pl/asie/charset/wires/TileWireContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void update() {

if (scheduledPropagationUpdate) {
scheduledPropagationUpdate = false;
onWireUpdate(null);
onWireUpdate(null, -1);
}

if (scheduledRenderUpdate) {
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/pl/asie/charset/wires/logic/Wire.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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) {
Expand Down
186 changes: 96 additions & 90 deletions src/main/java/pl/asie/charset/wires/logic/WireBundled.java
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/pl/asie/charset/wires/logic/WireNormal.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() + " ---");
}
Expand Down Expand Up @@ -206,19 +206,19 @@ 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();

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);
Expand All @@ -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());
Expand Down

0 comments on commit f705121

Please sign in to comment.