-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileMarker.java
571 lines (470 loc) · 13.1 KB
/
TileMarker.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/**
* Copyright (c) 2011-2015, 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 io.netty.buffer.ByteBuf;
import net.minecraft.block.Block;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import buildcraft.BuildCraftBuilders;
import buildcraft.api.core.ISerializable;
import buildcraft.api.core.Position;
import buildcraft.api.tiles.ITileAreaProvider;
import buildcraft.core.DefaultProps;
import buildcraft.core.LaserKind;
import buildcraft.core.lib.EntityBlock;
import buildcraft.core.lib.block.TileBuildCraft;
import buildcraft.core.lib.utils.Utils;
import buildcraft.core.proxy.CoreProxy;
public class TileMarker extends TileBuildCraft implements ITileAreaProvider {
public static class TileWrapper implements ISerializable {
public int x, y, z;
private TileMarker marker;
public TileWrapper() {
x = Integer.MAX_VALUE;
y = Integer.MAX_VALUE;
z = Integer.MAX_VALUE;
}
public TileWrapper(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public boolean isSet() {
return x != Integer.MAX_VALUE;
}
public TileMarker getMarker(World world) {
if (!isSet()) {
return null;
}
if (marker == null) {
TileEntity tile = world.getTileEntity(x, y, z);
if (tile instanceof TileMarker) {
marker = (TileMarker) tile;
}
}
return marker;
}
public void reset() {
x = Integer.MAX_VALUE;
y = Integer.MAX_VALUE;
z = Integer.MAX_VALUE;
}
@Override
public void readData(ByteBuf stream) {
x = stream.readInt();
if (isSet()) {
y = stream.readShort();
z = stream.readInt();
}
}
@Override
public void writeData(ByteBuf stream) {
stream.writeInt(x);
if (isSet()) {
// Only X is used for checking if a vector is set, so we can save space on the Y coordinate.
stream.writeShort(y);
stream.writeInt(z);
}
}
}
public static class Origin implements ISerializable {
public TileWrapper vectO = new TileWrapper();
public TileWrapper[] vect = {new TileWrapper(), new TileWrapper(), new TileWrapper()};
public int xMin, yMin, zMin, xMax, yMax, zMax;
public boolean isSet() {
return vectO.isSet();
}
@Override
public void writeData(ByteBuf stream) {
vectO.writeData(stream);
for (TileWrapper tw : vect) {
tw.writeData(stream);
}
stream.writeInt(xMin);
stream.writeShort(yMin);
stream.writeInt(zMin);
stream.writeInt(xMax);
stream.writeShort(yMax);
stream.writeInt(zMax);
}
@Override
public void readData(ByteBuf stream) {
vectO.readData(stream);
for (TileWrapper tw : vect) {
tw.readData(stream);
}
xMin = stream.readInt();
yMin = stream.readShort();
zMin = stream.readInt();
xMax = stream.readInt();
yMax = stream.readShort();
zMax = stream.readInt();
}
}
public Origin origin = new Origin();
public boolean showSignals = false;
private Position initVectO;
private Position[] initVect;
private EntityBlock[] lasers;
private EntityBlock[] signals;
public void updateSignals() {
if (!worldObj.isRemote) {
showSignals = worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord);
sendNetworkUpdate();
}
}
private void switchSignals() {
if (signals != null) {
for (EntityBlock b : signals) {
if (b != null) {
CoreProxy.proxy.removeEntity(b);
}
}
signals = null;
}
if (showSignals) {
signals = new EntityBlock[6];
if (!origin.isSet() || !origin.vect[0].isSet()) {
signals[0] = Utils.createLaser(worldObj, new Position(xCoord, yCoord, zCoord), new Position(xCoord + DefaultProps.MARKER_RANGE - 1, yCoord, zCoord),
LaserKind.Blue);
signals[1] = Utils.createLaser(worldObj, new Position(xCoord - DefaultProps.MARKER_RANGE + 1, yCoord, zCoord), new Position(xCoord, yCoord, zCoord),
LaserKind.Blue);
}
if (!origin.isSet() || !origin.vect[1].isSet()) {
signals[2] = Utils.createLaser(worldObj, new Position(xCoord, yCoord, zCoord), new Position(xCoord, yCoord + DefaultProps.MARKER_RANGE - 1, zCoord),
LaserKind.Blue);
signals[3] = Utils.createLaser(worldObj, new Position(xCoord, yCoord - DefaultProps.MARKER_RANGE + 1, zCoord), new Position(xCoord, yCoord, zCoord),
LaserKind.Blue);
}
if (!origin.isSet() || !origin.vect[2].isSet()) {
signals[4] = Utils.createLaser(worldObj, new Position(xCoord, yCoord, zCoord), new Position(xCoord, yCoord, zCoord + DefaultProps.MARKER_RANGE - 1),
LaserKind.Blue);
signals[5] = Utils.createLaser(worldObj, new Position(xCoord, yCoord, zCoord - DefaultProps.MARKER_RANGE + 1), new Position(xCoord, yCoord, zCoord),
LaserKind.Blue);
}
}
}
@Override
public void initialize() {
super.initialize();
updateSignals();
if (initVectO != null) {
origin = new Origin();
origin.vectO = new TileWrapper((int) initVectO.x, (int) initVectO.y, (int) initVectO.z);
for (int i = 0; i < 3; ++i) {
if (initVect[i] != null) {
linkTo((TileMarker) worldObj.getTileEntity((int) initVect[i].x, (int) initVect[i].y, (int) initVect[i].z), i);
}
}
}
}
public void tryConnection() {
if (worldObj.isRemote) {
return;
}
for (int j = 0; j < 3; ++j) {
if (!origin.isSet() || !origin.vect[j].isSet()) {
setVect(j);
}
}
sendNetworkUpdate();
}
void setVect(int n) {
int[] coords = new int[3];
coords[0] = xCoord;
coords[1] = yCoord;
coords[2] = zCoord;
if (!origin.isSet() || !origin.vect[n].isSet()) {
for (int j = 1; j < DefaultProps.MARKER_RANGE; ++j) {
coords[n] += j;
Block block = worldObj.getBlock(coords[0], coords[1], coords[2]);
if (block == BuildCraftBuilders.markerBlock) {
TileMarker marker = (TileMarker) worldObj.getTileEntity(coords[0], coords[1], coords[2]);
if (linkTo(marker, n)) {
break;
}
}
coords[n] -= j;
coords[n] -= j;
block = worldObj.getBlock(coords[0], coords[1], coords[2]);
if (block == BuildCraftBuilders.markerBlock) {
TileMarker marker = (TileMarker) worldObj.getTileEntity(coords[0], coords[1], coords[2]);
if (linkTo(marker, n)) {
break;
}
}
coords[n] += j;
}
}
}
private boolean linkTo(TileMarker marker, int n) {
if (marker == null) {
return false;
}
if (origin.isSet() && marker.origin.isSet()) {
return false;
}
if (!origin.isSet() && !marker.origin.isSet()) {
origin = new Origin();
marker.origin = origin;
origin.vectO = new TileWrapper(xCoord, yCoord, zCoord);
origin.vect[n] = new TileWrapper(marker.xCoord, marker.yCoord, marker.zCoord);
} else if (!origin.isSet()) {
origin = marker.origin;
origin.vect[n] = new TileWrapper(xCoord, yCoord, zCoord);
} else {
marker.origin = origin;
origin.vect[n] = new TileWrapper(marker.xCoord, marker.yCoord, marker.zCoord);
}
origin.vectO.getMarker(worldObj).createLasers();
updateSignals();
marker.updateSignals();
return true;
}
private void createLasers() {
if (lasers != null) {
for (EntityBlock entity : lasers) {
if (entity != null) {
CoreProxy.proxy.removeEntity(entity);
}
}
}
lasers = new EntityBlock[12];
Origin o = origin;
if (!origin.vect[0].isSet()) {
o.xMin = origin.vectO.x;
o.xMax = origin.vectO.x;
} else if (origin.vect[0].x < xCoord) {
o.xMin = origin.vect[0].x;
o.xMax = xCoord;
} else {
o.xMin = xCoord;
o.xMax = origin.vect[0].x;
}
if (!origin.vect[1].isSet()) {
o.yMin = origin.vectO.y;
o.yMax = origin.vectO.y;
} else if (origin.vect[1].y < yCoord) {
o.yMin = origin.vect[1].y;
o.yMax = yCoord;
} else {
o.yMin = yCoord;
o.yMax = origin.vect[1].y;
}
if (!origin.vect[2].isSet()) {
o.zMin = origin.vectO.z;
o.zMax = origin.vectO.z;
} else if (origin.vect[2].z < zCoord) {
o.zMin = origin.vect[2].z;
o.zMax = zCoord;
} else {
o.zMin = zCoord;
o.zMax = origin.vect[2].z;
}
lasers = Utils.createLaserBox(worldObj, o.xMin, o.yMin, o.zMin, o.xMax, o.yMax, o.zMax, LaserKind.Red);
}
@Override
public int xMin() {
if (origin.isSet()) {
return origin.xMin;
}
return xCoord;
}
@Override
public int yMin() {
if (origin.isSet()) {
return origin.yMin;
}
return yCoord;
}
@Override
public int zMin() {
if (origin.isSet()) {
return origin.zMin;
}
return zCoord;
}
@Override
public int xMax() {
if (origin.isSet()) {
return origin.xMax;
}
return xCoord;
}
@Override
public int yMax() {
if (origin.isSet()) {
return origin.yMax;
}
return yCoord;
}
@Override
public int zMax() {
if (origin.isSet()) {
return origin.zMax;
}
return zCoord;
}
@Override
public void invalidate() {
super.invalidate();
destroy();
}
@Override
public void destroy() {
TileMarker markerOrigin = null;
if (origin.isSet()) {
markerOrigin = origin.vectO.getMarker(worldObj);
Origin o = origin;
if (markerOrigin != null && markerOrigin.lasers != null) {
for (EntityBlock entity : markerOrigin.lasers) {
if (entity != null) {
entity.setDead();
}
}
markerOrigin.lasers = null;
}
for (TileWrapper m : o.vect) {
TileMarker mark = m.getMarker(worldObj);
if (mark != null) {
if (mark.lasers != null) {
for (EntityBlock entity : mark.lasers) {
if (entity != null) {
entity.setDead();
}
}
mark.lasers = null;
}
if (mark != this) {
mark.origin = new Origin();
}
}
}
if (markerOrigin != this && markerOrigin != null) {
markerOrigin.origin = new Origin();
}
for (TileWrapper wrapper : o.vect) {
TileMarker mark = wrapper.getMarker(worldObj);
if (mark != null) {
mark.updateSignals();
}
}
if (markerOrigin != null) {
markerOrigin.updateSignals();
}
}
if (signals != null) {
for (EntityBlock block : signals) {
if (block != null) {
block.setDead();
}
}
}
signals = null;
if (!worldObj.isRemote && markerOrigin != null && markerOrigin != this) {
markerOrigin.sendNetworkUpdate();
}
}
@Override
public void removeFromWorld() {
if (!origin.isSet()) {
return;
}
Origin o = origin;
for (TileWrapper m : o.vect.clone()) {
if (m.isSet()) {
worldObj.setBlockToAir(m.x, m.y, m.z);
BuildCraftBuilders.markerBlock.dropBlockAsItem(worldObj, m.x, m.y, m.z, 0, 0);
}
}
worldObj.setBlockToAir(o.vectO.x, o.vectO.y, o.vectO.z);
BuildCraftBuilders.markerBlock.dropBlockAsItem(worldObj, o.vectO.x, o.vectO.y, o.vectO.z, 0, 0);
}
@Override
public void readFromNBT(NBTTagCompound nbttagcompound) {
super.readFromNBT(nbttagcompound);
if (nbttagcompound.hasKey("vectO")) {
initVectO = new Position(nbttagcompound.getCompoundTag("vectO"));
initVect = new Position[3];
for (int i = 0; i < 3; ++i) {
if (nbttagcompound.hasKey("vect" + i)) {
initVect[i] = new Position(nbttagcompound.getCompoundTag("vect" + i));
}
}
}
}
@Override
public void writeToNBT(NBTTagCompound nbttagcompound) {
super.writeToNBT(nbttagcompound);
if (origin.isSet() && origin.vectO.getMarker(worldObj) == this) {
NBTTagCompound vectO = new NBTTagCompound();
new Position(origin.vectO.getMarker(worldObj)).writeToNBT(vectO);
nbttagcompound.setTag("vectO", vectO);
for (int i = 0; i < 3; ++i) {
if (origin.vect[i].isSet()) {
NBTTagCompound vect = new NBTTagCompound();
new Position(origin.vect[i].x, origin.vect[i].y, origin.vect[i].z).writeToNBT(vect);
nbttagcompound.setTag("vect" + i, vect);
}
}
}
}
@Override
public void writeData(ByteBuf stream) {
origin.writeData(stream);
stream.writeBoolean(showSignals);
}
@Override
public void readData(ByteBuf stream) {
origin.readData(stream);
showSignals = stream.readBoolean();
switchSignals();
if (origin.vectO.isSet() && origin.vectO.getMarker(worldObj) != null) {
origin.vectO.getMarker(worldObj).updateSignals();
for (TileWrapper w : origin.vect) {
TileMarker m = w.getMarker(worldObj);
if (m != null) {
m.updateSignals();
}
}
}
createLasers();
}
@Override
public boolean isValidFromLocation(int x, int y, int z) {
// Rules:
// - one or two, but not three, of the coordinates must be equal to the marker's location
// - one of the coordinates must be either -1 or 1 away
// - it must be physically touching the box
// - however, it cannot be INSIDE the box
int equal = (x == xCoord ? 1 : 0) + (y == yCoord ? 1 : 0) + (z == zCoord ? 1 : 0);
int touching = 0;
if (equal == 0 || equal == 3) {
return false;
}
if (x < (xMin() - 1) || x > (xMax() + 1) || y < (yMin() - 1) || y > (yMax() + 1)
|| z < (zMin() - 1) || z > (zMax() + 1)) {
return false;
}
if (x >= xMin() && x <= xMax() && y >= yMin() && y <= yMax() && z >= zMin() && z <= zMax()) {
return false;
}
if (xMin() - x == 1 || x - xMax() == 1) {
touching++;
}
if (yMin() - y == 1 || y - yMax() == 1) {
touching++;
}
if (zMin() - z == 1 || z - zMax() == 1) {
touching++;
}
return touching == 1;
}
}