1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
package gregtech.api.graphs;
import static gregtech.api.util.GT_Utility.getOppositeSide;
import gregtech.api.graphs.consumers.ConsumerNode;
import gregtech.api.graphs.paths.NodePath;
import gregtech.api.metatileentity.BaseMetaPipeEntity;
import gregtech.api.metatileentity.MetaPipeEntity;
import java.util.ArrayList;
import java.util.HashSet;
import net.minecraft.tileentity.TileEntity;
// generates the node map
public abstract class GenerateNodeMap {
// clearing the node map to make sure it is gone on reset
public static void clearNodeMap(Node aNode, int aReturnNodeValue) {
if (aNode.mTileEntity instanceof BaseMetaPipeEntity) {
BaseMetaPipeEntity tPipe = (BaseMetaPipeEntity) aNode.mTileEntity;
tPipe.setNode(null);
tPipe.setNodePath(null);
if (aNode.mSelfPath != null) {
aNode.mSelfPath.clearPath();
aNode.mSelfPath = null;
}
}
for (int i = 0; i < 6; i++) {
NodePath tPath = aNode.mNodePaths[i];
if (tPath != null) {
tPath.clearPath();
aNode.mNodePaths[i] = null;
}
Node tNextNode = aNode.mNeighbourNodes[i];
if (tNextNode == null) continue;
if (tNextNode.mNodeValue != aReturnNodeValue) clearNodeMap(tNextNode, aNode.mNodeValue);
aNode.mNeighbourNodes[i] = null;
}
}
// get how many connections the pipe have
private static int getNumberOfConnections(MetaPipeEntity aPipe) {
int tCons = 0;
for (int i = 0; i < 6; i++) {
if (aPipe.isConnectedAtSide(i)) tCons++;
}
return tCons;
}
// gets the next node
protected void generateNextNode(
BaseMetaPipeEntity aPipe,
Node aPipeNode,
byte aInvalidSide,
int aNextNodeValue,
ArrayList<ConsumerNode> tConsumers,
HashSet<Node> tNodeMap) {
MetaPipeEntity tMetaPipe = (MetaPipeEntity) aPipe.getMetaTileEntity();
for (byte i = 0; i < 6; i++) {
if (i == aInvalidSide) {
continue;
}
TileEntity tNextTileEntity = aPipe.getTileEntityAtSide(i);
if (tNextTileEntity == null || (tMetaPipe != null && !tMetaPipe.isConnectedAtSide(i))) continue;
ArrayList<MetaPipeEntity> tNewPipes = new ArrayList<>();
Pair nextTileEntity = getNextValidTileEntity(tNextTileEntity, tNewPipes, i, tNodeMap);
if (nextTileEntity != null) {
Node tNextNode = generateNode(
nextTileEntity.mTileEntity,
aPipeNode,
aNextNodeValue + 1,
tNewPipes,
nextTileEntity.mSide,
tConsumers,
tNodeMap);
if (tNextNode != null) {
aNextNodeValue = tNextNode.mHighestNodeValue;
aPipeNode.mHighestNodeValue = tNextNode.mHighestNodeValue;
aPipeNode.mNeighbourNodes[i] = tNextNode;
aPipeNode.mNodePaths[i] = aPipeNode.returnValues.mReturnPath;
aPipeNode.locks[i] = aPipeNode.returnValues.returnLock;
aPipeNode.mNodePaths[i].reloadLocks();
}
}
}
aPipe.reloadLocks();
}
// on a valid tile entity create a new node
protected Node generateNode(
TileEntity aTileEntity,
Node aPreviousNode,
int aNextNodeValue,
ArrayList<MetaPipeEntity> aPipes,
int aSide,
ArrayList<ConsumerNode> aConsumers,
HashSet<Node> aNodeMap) {
if (aTileEntity.isInvalid()) return null;
byte tSideOp = getOppositeSide(aSide);
byte tInvalidSide = aPreviousNode == null ? -1 : tSideOp;
Node tThisNode = null;
if (isPipe(aTileEntity)) {
BaseMetaPipeEntity tPipe = (BaseMetaPipeEntity) aTileEntity;
MetaPipeEntity tMetaPipe = (MetaPipeEntity) tPipe.getMetaTileEntity();
int tConnections = getNumberOfConnections(tMetaPipe);
Node tPipeNode;
if (tConnections == 1) {
tPipeNode = getEmptyNode(aNextNodeValue, tSideOp, aTileEntity, aConsumers);
if (tPipeNode == null) return null;
} else {
tPipeNode = getPipeNode(aNextNodeValue, tSideOp, aTileEntity, aConsumers);
}
tPipe.setNode(tPipeNode);
aNodeMap.add(tPipeNode);
tPipeNode.mSelfPath = getNewPath(new MetaPipeEntity[] {tMetaPipe});
tThisNode = tPipeNode;
if (tInvalidSide > -1) {
tPipeNode.mNeighbourNodes[tInvalidSide] = aPreviousNode;
tPipeNode.mNodePaths[tInvalidSide] = getNewPath(aPipes.toArray(new MetaPipeEntity[0]));
Lock lock = new Lock();
tPipeNode.mNodePaths[tSideOp].lock = lock;
tPipeNode.locks[tInvalidSide] = lock;
aPreviousNode.returnValues.mReturnPath = tPipeNode.mNodePaths[tInvalidSide];
aPreviousNode.returnValues.returnLock = lock;
}
if (tConnections > 1)
generateNextNode(tPipe, tPipeNode, tInvalidSide, aNextNodeValue, aConsumers, aNodeMap);
} else if (addConsumer(aTileEntity, tSideOp, aNextNodeValue, aConsumers)) {
ConsumerNode tConsumeNode = aConsumers.get(aConsumers.size() - 1);
tConsumeNode.mNeighbourNodes[tSideOp] = aPreviousNode;
tConsumeNode.mNodePaths[tSideOp] = getNewPath(aPipes.toArray(new MetaPipeEntity[0]));
Lock lock = new Lock();
tConsumeNode.mNodePaths[tSideOp].lock = lock;
aPreviousNode.returnValues.mReturnPath = tConsumeNode.mNodePaths[tSideOp];
aPreviousNode.returnValues.returnLock = lock;
tThisNode = tConsumeNode;
}
return tThisNode;
}
// go over the pipes until we see a valid tile entity that needs a node
protected Pair getNextValidTileEntity(
TileEntity aTileEntity, ArrayList<MetaPipeEntity> aPipes, byte aSide, HashSet<Node> aNodeMap) {
if (isPipe(aTileEntity)) {
BaseMetaPipeEntity tPipe = (BaseMetaPipeEntity) aTileEntity;
MetaPipeEntity tMetaPipe = (MetaPipeEntity) tPipe.getMetaTileEntity();
Node tNode = tPipe.getNode();
if (tNode != null) {
if (aNodeMap.contains(tNode)) return null;
}
int tConnections = getNumberOfConnections(tMetaPipe);
if (tConnections == 2) {
byte tSideOp = getOppositeSide(aSide);
for (byte i = 0; i < 6; i++) {
if (i == tSideOp || !(tMetaPipe.isConnectedAtSide(i))) continue;
TileEntity tNewTileEntity = tPipe.getTileEntityAtSide(i);
if (tNewTileEntity == null) continue;
if (isPipe(tNewTileEntity)) {
aPipes.add(tMetaPipe);
return getNextValidTileEntity(tNewTileEntity, aPipes, i, aNodeMap);
} else {
return new Pair(aTileEntity, i);
}
}
} else {
return new Pair(aTileEntity, aSide);
}
} else {
return new Pair(aTileEntity, aSide);
}
return null;
}
// check if the tile entity is the correct pipe
protected boolean isPipe(TileEntity aTileEntity) {
return aTileEntity instanceof BaseMetaPipeEntity;
}
// checks if the tile entity is a consumer and add to the list
protected abstract boolean addConsumer(
TileEntity aTileEntity, byte aSide, int aNodeValue, ArrayList<ConsumerNode> aConsumers);
// get correct pathClass that you need for your node network
protected abstract NodePath getNewPath(MetaPipeEntity[] aPipes);
// used for if you need to use dead ends for something can be null
protected Node getEmptyNode(
int aNodeValue, byte aSide, TileEntity aTileEntity, ArrayList<ConsumerNode> aConsumers) {
return null;
}
// get correct node type you need for your network
protected Node getPipeNode(int aNodeValue, byte aSide, TileEntity aTileEntity, ArrayList<ConsumerNode> aConsumers) {
return new Node(aNodeValue, aTileEntity, aConsumers);
}
private static class Pair {
public byte mSide;
public TileEntity mTileEntity;
public Pair(TileEntity aTileEntity, byte aSide) {
this.mTileEntity = aTileEntity;
this.mSide = aSide;
}
}
}
|