Skip to content

Commit

Permalink
internal: use correct neighbor change source position
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTJP committed Nov 30, 2023
1 parent 1ee25a1 commit 196fa19
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 16 deletions.
17 changes: 10 additions & 7 deletions core/src/main/java/mrtjp/projectred/core/RedstonePropagator.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.RedStoneWireBlock;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.*;

public class RedstonePropagator {
/**
Expand Down Expand Up @@ -68,8 +65,12 @@ public static void resetPowerFlags() {
setCanConnectRedwires(true);
}

public static void addNeighborChange(Level world, BlockPos pos) {
currentRun.neighborChanges.put(world, pos);
public static void addNeighborChange(Level world, BlockPos sourcePos, BlockPos neighborPos) {
currentRun.neighborChanges.put(world, neighborPos);

// Track source of change for first time a particular neighbor is changed
if (!currentRun.neighborChangeSources.containsKey(neighborPos))
currentRun.neighborChangeSources.put(neighborPos, sourcePos);
}

public static void addPartChange(MultiPart part) {
Expand Down Expand Up @@ -109,12 +110,14 @@ private static class PropagationRun {

HashMultimap<TileMultipart, MultiPart> partChanges = HashMultimap.create();
HashMultimap<Level, BlockPos> neighborChanges = HashMultimap.create();
HashMap<BlockPos, BlockPos> neighborChangeSources = new HashMap<>();
List<Runnable> propagationTasks = new LinkedList<>();
List<Runnable> analogDropPropagationTasks = new LinkedList<>();

void clear() {
partChanges.clear();
neighborChanges.clear();
neighborChangeSources.clear();
count = 0;
recalcs = 0;
lastCaller = null;
Expand Down Expand Up @@ -167,7 +170,7 @@ void finishRun() {
Level world = entry.getKey();
Collection<BlockPos> positions = entry.getValue();
for (BlockPos pos : positions) {
world.neighborChanged(pos, CBMultipartModContent.MULTIPART_BLOCK.get(), pos);
world.neighborChanged(pos, CBMultipartModContent.MULTIPART_BLOCK.get(), neighborChangeSources.get(pos));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ default void propagateForward(IPropagationPart prev, int mode) {

} else if (maskConnectsOut(s)) {
if (!propagateTo(getStraight(s), prev, mode)) {
RedstonePropagator.addNeighborChange(level(), posOfStraight(s));
RedstonePropagator.addNeighborChange(level(), pos(), posOfStraight(s));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ default void propagateForward(IPropagationPart prev, int mode) {
propagateTo(getInternal(r), prev, mode);
} else if (maskConnectsStraight(r)) {
if (!propagateTo(getStraight(r), prev, mode)) {
RedstonePropagator.addNeighborChange(level(), posOfStraight(r));
RedstonePropagator.addNeighborChange(level(), pos(), posOfStraight(r));
}
} else if (maskConnectsCorner(r)) {
if (!propagateTo(getCorner(r), prev, mode)) {
RedstonePropagator.addNeighborChange(level(), posOfCorner(r));
RedstonePropagator.addNeighborChange(level(), posOfStraight(r), posOfCorner(r));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ public void updateAndPropagate(IPropagationPart prev, int mode) {
}
}

// This means change came on non-redwire side. Simply queue up a normal neighbor update
if (uMask == 0) {
RedstonePropagator.addNeighborChange(level(), pos());
// TODO find better way to do this and sideDiff logic below
BlockPos prevPos = prev instanceof MultiPart ? ((MultiPart) prev).pos() : pos();
RedstonePropagator.addNeighborChange(level(), prevPos, pos());
}
propagationMask = 0xF;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public int calculateSignal() {
public void propagateOther(int mode) {
for (int s = 0; s < 6; s++) {
if (!maskConnects(s)) {
RedstonePropagator.addNeighborChange(level(), posOfStraight(s));
RedstonePropagator.addNeighborChange(level(), pos(), posOfStraight(s));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,22 @@ protected boolean powerUnderside() {
public void propagateOther(int mode) {

// Update block above and below
RedstonePropagator.addNeighborChange(level(), this.pos().relative(Direction.values()[getSide()]));
RedstonePropagator.addNeighborChange(level(), this.pos().relative(Direction.values()[getSide() ^ 1]));
RedstonePropagator.addNeighborChange(level(), pos(), pos().relative(Direction.values()[getSide()]));
RedstonePropagator.addNeighborChange(level(), pos(), pos().relative(Direction.values()[getSide() ^ 1]));

// Update all 4 rotational sides if they are not connected. They are excluded
// because things that actually connect are expected to be part of the propagation anyway
for (int r = 0; r < 4; r++) {
if (!maskConnects(r)) {
RedstonePropagator.addNeighborChange(level(), posOfStraight(r));
RedstonePropagator.addNeighborChange(level(), pos(), posOfStraight(r));
}
}

// Update all neighbors of the block below
BlockPos posUnder = this.pos().relative(Direction.values()[getSide()]);
for (int s = 0; s < 6; s++) {
if (s != (getSide() ^ 1)) {
RedstonePropagator.addNeighborChange(level(), posUnder.relative(Direction.values()[s]));
RedstonePropagator.addNeighborChange(level(), posUnder, posUnder.relative(Direction.values()[s]));
}
}
}
Expand Down

0 comments on commit 196fa19

Please sign in to comment.