-
Notifications
You must be signed in to change notification settings - Fork 497
/
TilePathMarker.java
328 lines (256 loc) · 7.93 KB
/
TilePathMarker.java
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.builders;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.TreeSet;
import io.netty.buffer.ByteBuf;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import buildcraft.api.core.BlockIndex;
import buildcraft.api.core.Position;
import buildcraft.core.LaserData;
public class TilePathMarker extends TileMarker {
// TODO: this should be moved to default props
// A list with the pathMarkers that aren't fully connected
// It only contains markers within the loaded chunks
public static int searchSize = 64;
private static LinkedList<TilePathMarker> availableMarkers = new LinkedList<TilePathMarker>();
public int x0, y0, z0, x1, y1, z1;
public boolean loadLink0 = false;
public boolean loadLink1 = false;
public LaserData[] lasers = new LaserData[2];
public boolean tryingToConnect = false;
public TilePathMarker[] links = new TilePathMarker[2];
public boolean isFullyConnected() {
return lasers[0] != null && lasers[1] != null;
}
public boolean isLinkedTo(TilePathMarker pathMarker) {
return links[0] == pathMarker || links[1] == pathMarker;
}
public void connect(TilePathMarker marker, LaserData laser) {
if (lasers[0] == null) {
lasers[0] = laser;
links[0] = marker;
} else if (lasers[1] == null) {
lasers[1] = laser;
links[1] = marker;
}
if (isFullyConnected()) {
availableMarkers.remove(this);
}
}
public void createLaserAndConnect(TilePathMarker pathMarker) {
if (worldObj.isRemote) {
return;
}
LaserData laser = new LaserData
(new Position(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5),
new Position(pathMarker.xCoord + 0.5, pathMarker.yCoord + 0.5, pathMarker.zCoord + 0.5));
LaserData laser2 = new LaserData (laser.head, laser.tail);
laser2.isVisible = false;
connect(pathMarker, laser);
pathMarker.connect(this, laser2);
}
/**
* Searches the availableMarkers list for the nearest available that is
* within searchSize
*/
private TilePathMarker findNearestAvailablePathMarker() {
TilePathMarker nearestAvailable = null;
// The initialization of nearestDistance is only to make the compiler shut up
double nearestDistance = 0, distance;
for (TilePathMarker t : availableMarkers) {
if (t == this || t == this.links[0] || t == this.links[1] || t.getWorldObj().provider.dimensionId != this.getWorldObj().provider.dimensionId) {
continue;
}
distance = Math.sqrt(Math.pow(this.xCoord - t.xCoord, 2) + Math.pow(this.yCoord - t.yCoord, 2) + Math.pow(this.zCoord - t.zCoord, 2));
if (distance > searchSize) {
continue;
}
if (nearestAvailable == null || distance < nearestDistance) {
nearestAvailable = t;
nearestDistance = distance;
}
}
return nearestAvailable;
}
@Override
public void tryConnection() {
if (worldObj.isRemote || isFullyConnected()) {
return;
}
// Allow the user to stop the path marker from searching for new path markers to connect
tryingToConnect = !tryingToConnect;
sendNetworkUpdate();
}
@Override
public void updateEntity() {
super.updateEntity();
if (worldObj.isRemote) {
return;
}
if (tryingToConnect) {
TilePathMarker nearestPathMarker = findNearestAvailablePathMarker();
if (nearestPathMarker != null) {
createLaserAndConnect(nearestPathMarker);
}
tryingToConnect = false;
sendNetworkUpdate();
getWorldObj().markBlockRangeForRenderUpdate(xCoord, yCoord, zCoord,
xCoord, yCoord, zCoord);
}
}
public LinkedList<BlockIndex> getPath() {
TreeSet<BlockIndex> visitedPaths = new TreeSet<BlockIndex>();
LinkedList<BlockIndex> res = new LinkedList<BlockIndex>();
TilePathMarker nextTile = this;
while (nextTile != null) {
BlockIndex b = new BlockIndex(nextTile.xCoord, nextTile.yCoord, nextTile.zCoord);
visitedPaths.add(b);
res.add(b);
if (nextTile.links[0] != null
&& !visitedPaths.contains(new BlockIndex(nextTile.links[0].xCoord, nextTile.links[0].yCoord, nextTile.links[0].zCoord))) {
nextTile = nextTile.links[0];
} else if (nextTile.links[1] != null
&& !visitedPaths.contains(new BlockIndex(nextTile.links[1].xCoord, nextTile.links[1].yCoord, nextTile.links[1].zCoord))) {
nextTile = nextTile.links[1];
} else {
nextTile = null;
}
}
return res;
}
@Override
public void invalidate() {
super.invalidate();
if (links[0] != null) {
links[0].unlink(this);
}
if (links[1] != null) {
links[1].unlink(this);
}
availableMarkers.remove(this);
tryingToConnect = false;
}
@Override
public void initialize() {
super.initialize();
if (!worldObj.isRemote && !isFullyConnected()) {
availableMarkers.add(this);
}
if (loadLink0) {
TileEntity e0 = worldObj.getTileEntity(x0, y0, z0);
if (links[0] != e0 && links[1] != e0 && e0 instanceof TilePathMarker) {
createLaserAndConnect((TilePathMarker) e0);
}
loadLink0 = false;
}
if (loadLink1) {
TileEntity e1 = worldObj.getTileEntity(x1, y1, z1);
if (links[0] != e1 && links[1] != e1 && e1 instanceof TilePathMarker) {
createLaserAndConnect((TilePathMarker) e1);
}
loadLink1 = false;
}
sendNetworkUpdate();
}
private void unlink(TilePathMarker tile) {
if (links[0] == tile) {
lasers[0] = null;
links[0] = null;
}
if (links[1] == tile) {
lasers[1] = null;
links[1] = null;
}
if (!isFullyConnected() && !availableMarkers.contains(this) && !worldObj.isRemote) {
availableMarkers.add(this);
}
sendNetworkUpdate();
}
@Override
public void readFromNBT(NBTTagCompound nbttagcompound) {
super.readFromNBT(nbttagcompound);
if (nbttagcompound.hasKey("x0")) {
x0 = nbttagcompound.getInteger("x0");
y0 = nbttagcompound.getInteger("y0");
z0 = nbttagcompound.getInteger("z0");
loadLink0 = true;
}
if (nbttagcompound.hasKey("x1")) {
x1 = nbttagcompound.getInteger("x1");
y1 = nbttagcompound.getInteger("y1");
z1 = nbttagcompound.getInteger("z1");
loadLink1 = true;
}
}
@Override
public void writeToNBT(NBTTagCompound nbttagcompound) {
super.writeToNBT(nbttagcompound);
if (links[0] != null) {
nbttagcompound.setInteger("x0", links[0].xCoord);
nbttagcompound.setInteger("y0", links[0].yCoord);
nbttagcompound.setInteger("z0", links[0].zCoord);
}
if (links[1] != null) {
nbttagcompound.setInteger("x1", links[1].xCoord);
nbttagcompound.setInteger("y1", links[1].yCoord);
nbttagcompound.setInteger("z1", links[1].zCoord);
}
}
@Override
public void onChunkUnload() {
availableMarkers.remove(this);
}
public static void clearAvailableMarkersList() {
availableMarkers.clear();
}
public static void clearAvailableMarkersList(World w) {
for (Iterator<TilePathMarker> it = availableMarkers.iterator(); it.hasNext();) {
TilePathMarker t = it.next();
if (t.getWorldObj().provider.dimensionId != w.provider.dimensionId) {
it.remove();
}
}
}
@Override
public void readData(ByteBuf data) {
boolean previousState = tryingToConnect;
int flags = data.readUnsignedByte();
if ((flags & 1) != 0) {
lasers[0] = new LaserData();
lasers[0].readData(data);
} else {
lasers[0] = null;
}
if ((flags & 2) != 0) {
lasers[1] = new LaserData();
lasers[1].readData(data);
} else {
lasers[1] = null;
}
tryingToConnect = (flags & 4) != 0;
if (previousState != tryingToConnect) {
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
}
@Override
public void writeData(ByteBuf data) {
int flags = (lasers[0] != null ? 1 : 0) | (lasers[1] != null ? 2 : 0) | (tryingToConnect ? 4 : 0);
data.writeByte(flags);
if (lasers[0] != null) {
lasers[0].writeData(data);
}
if (lasers[1] != null) {
lasers[1].writeData(data);
}
}
}