|
4 | 4 | import gregtech.api.pipenet.PipeNet;
|
5 | 5 | import gregtech.api.pipenet.WorldPipeNet;
|
6 | 6 | import gregtech.api.unification.material.properties.WireProperties;
|
7 |
| -import gregtech.api.util.PerTickLongCounter; |
8 | 7 | import net.minecraft.nbt.NBTTagCompound;
|
9 | 8 | import net.minecraft.util.EnumFacing;
|
10 | 9 | import net.minecraft.util.math.BlockPos;
|
11 |
| -import net.minecraft.util.math.BlockPos.MutableBlockPos; |
12 | 10 |
|
13 |
| -import java.util.ArrayList; |
14 |
| -import java.util.HashSet; |
| 11 | +import java.util.Comparator; |
| 12 | +import java.util.HashMap; |
15 | 13 | import java.util.List;
|
16 |
| -import java.util.Stack; |
| 14 | +import java.util.Map; |
17 | 15 |
|
18 | 16 | public class EnergyNet extends PipeNet<WireProperties> {
|
19 | 17 |
|
20 |
| - private final PerTickLongCounter currentAmperageCounter = new PerTickLongCounter(0L); |
21 |
| - private final PerTickLongCounter currentMaxVoltageCounter = new PerTickLongCounter(0L); |
| 18 | + private final Map<BlockPos, List<RoutePath>> NET_DATA = new HashMap<>(); |
22 | 19 |
|
23 | 20 | protected EnergyNet(WorldPipeNet<WireProperties, EnergyNet> world) {
|
24 | 21 | super(world);
|
25 | 22 | }
|
26 | 23 |
|
27 |
| - public long getLastAmperage() { |
28 |
| - return currentAmperageCounter.getLast(worldData.getWorld()); |
| 24 | + public List<RoutePath> getNetData(BlockPos pipePos) { |
| 25 | + List<RoutePath> data = NET_DATA.get(pipePos); |
| 26 | + if (data == null) { |
| 27 | + data = EnergyNetWalker.createNetData(this, getWorldData(), pipePos); |
| 28 | + data.sort(Comparator.comparingInt(RoutePath::getDistance)); |
| 29 | + NET_DATA.put(pipePos, data); |
| 30 | + } |
| 31 | + return data; |
29 | 32 | }
|
30 | 33 |
|
31 |
| - public long getLastMaxVoltage() { |
32 |
| - return currentMaxVoltageCounter.getLast(worldData.getWorld()); |
| 34 | + public void nodeNeighbourChanged(BlockPos pos) { |
| 35 | + NET_DATA.clear(); |
33 | 36 | }
|
34 | 37 |
|
35 |
| - public void incrementCurrentAmperage(long amperage, long voltage) { |
36 |
| - currentAmperageCounter.increment(worldData.getWorld(), amperage); |
37 |
| - long currentMaxVoltage = currentMaxVoltageCounter.get(worldData.getWorld()); |
38 |
| - if (voltage > currentMaxVoltage) { |
39 |
| - currentMaxVoltageCounter.set(worldData.getWorld(), voltage); |
40 |
| - } |
| 38 | + @Override |
| 39 | + protected void updateBlockedConnections(BlockPos nodePos, EnumFacing facing, boolean isBlocked) { |
| 40 | + super.updateBlockedConnections(nodePos, facing, isBlocked); |
| 41 | + NET_DATA.clear(); |
41 | 42 | }
|
42 | 43 |
|
43 |
| - public List<RoutePath> computePatches(BlockPos startPos) { |
44 |
| - ArrayList<RoutePath> readyPaths = new ArrayList<>(); |
45 |
| - RoutePath currentPath = new RoutePath(); |
46 |
| - Node<WireProperties> firstNode = getNodeAt(startPos); |
47 |
| - currentPath.path.put(startPos, firstNode.data); |
48 |
| - readyPaths.add(currentPath.cloneAndCompute(startPos)); |
49 |
| - HashSet<BlockPos> observedSet = new HashSet<>(); |
50 |
| - observedSet.add(startPos); |
51 |
| - MutableBlockPos currentPos = new MutableBlockPos(startPos); |
52 |
| - Stack<EnumFacing> moveStack = new Stack<>(); |
53 |
| - main: |
54 |
| - while (true) { |
55 |
| - for (EnumFacing facing : EnumFacing.VALUES) { |
56 |
| - currentPos.move(facing); |
57 |
| - Node<WireProperties> secondNode = getNodeAt(currentPos); |
58 |
| - if (secondNode != null && canNodesConnect(firstNode, facing, secondNode, this) && !observedSet.contains(currentPos)) { |
59 |
| - BlockPos immutablePos = currentPos.toImmutable(); |
60 |
| - observedSet.add(immutablePos); |
61 |
| - firstNode = secondNode; |
62 |
| - moveStack.push(facing.getOpposite()); |
63 |
| - currentPath.path.put(immutablePos, getNodeAt(immutablePos).data); |
64 |
| - if (secondNode.isActive) { |
65 |
| - //if we are on active node, this is end of our path |
66 |
| - RoutePath finalizedPath = currentPath.cloneAndCompute(immutablePos); |
67 |
| - readyPaths.add(finalizedPath); |
68 |
| - } |
69 |
| - continue main; |
70 |
| - } else { |
71 |
| - currentPos.move(facing.getOpposite()); |
72 |
| - } |
73 |
| - } |
74 |
| - if (!moveStack.isEmpty()) { |
75 |
| - currentPos.move(moveStack.pop()); |
76 |
| - //also remove already visited block from path |
77 |
| - currentPath.path.remove(currentPos); |
78 |
| - } else break; |
79 |
| - } |
80 |
| - return readyPaths; |
| 44 | + @Override |
| 45 | + protected void transferNodeData(Map<BlockPos, Node<WireProperties>> transferredNodes, PipeNet<WireProperties> parentNet) { |
| 46 | + super.transferNodeData(transferredNodes, parentNet); |
| 47 | + NET_DATA.clear(); |
| 48 | + ((EnergyNet) parentNet).NET_DATA.clear(); |
81 | 49 | }
|
82 | 50 |
|
83 |
| - |
84 | 51 | @Override
|
85 | 52 | protected void writeNodeData(WireProperties nodeData, NBTTagCompound tagCompound) {
|
86 | 53 | tagCompound.setInteger("voltage", nodeData.voltage);
|
|
0 commit comments