-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileBuilder.java
657 lines (519 loc) · 14.2 KB
/
TileBuilder.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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/**
* 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.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.WorldSettings.GameType;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.BuildCraftBuilders;
import buildcraft.api.core.Position;
import buildcraft.api.core.SafeTimeTracker;
import buildcraft.api.gates.IAction;
import buildcraft.core.BlockIndex;
import buildcraft.core.Box;
import buildcraft.core.Box.Kind;
import buildcraft.core.IBoxProvider;
import buildcraft.core.IMachine;
import buildcraft.core.LaserData;
import buildcraft.core.blueprints.Blueprint;
import buildcraft.core.blueprints.BlueprintBase;
import buildcraft.core.blueprints.BptBuilderBase;
import buildcraft.core.blueprints.BptBuilderBlueprint;
import buildcraft.core.blueprints.BptBuilderTemplate;
import buildcraft.core.blueprints.BptContext;
import buildcraft.core.network.NetworkData;
import buildcraft.core.network.RPC;
import buildcraft.core.network.RPCHandler;
import buildcraft.core.network.RPCSide;
import buildcraft.core.utils.Utils;
public class TileBuilder extends TileAbstractBuilder implements IMachine, IBoxProvider {
private static int POWER_ACTIVATION = 50;
private final ItemStack items[] = new ItemStack[28];
private BptBuilderBase bluePrintBuilder;
@NetworkData
public Box box = new Box();
private LinkedList<BlockIndex> path;
private LinkedList <ItemStack> requiredToBuild;
private SafeTimeTracker buildTracker = new SafeTimeTracker(5);
private class PathIterator {
public Iterator<BlockIndex> currentIterator;
public double cx, cy, cz;
public float ix, iy, iz;
public BlockIndex to;
public double lastDistance;
AxisAlignedBB oldBoundingBox = null;
ForgeDirection o = null;
public PathIterator(BlockIndex from, Iterator<BlockIndex> it) {
this.to = it.next();
currentIterator = it;
double dx = to.x - from.x;
double dy = to.y - from.y;
double dz = to.z - from.z;
double size = Math.sqrt(dx * dx + dy * dy + dz * dz);
cx = dx / size / 10;
cy = dy / size / 10;
cz = dz / size / 10;
ix = from.x;
iy = from.y;
iz = from.z;
lastDistance = (ix - to.x) * (ix - to.x) + (iy - to.y)
* (iy - to.y) + (iz - to.z) * (iz - to.z);
if (Math.abs(dx) > Math.abs(dz)) {
if (dx > 0) {
o = ForgeDirection.EAST;
} else {
o = ForgeDirection.WEST;
}
} else {
if (dz > 0) {
o = ForgeDirection.SOUTH;
} else {
o = ForgeDirection.NORTH;
}
}
}
/**
* Return false when reached the end of the iteration
*/
public BptBuilderBase next() {
while (true) {
BptBuilderBase bpt;
int newX = Math.round(ix);
int newY = Math.round(iy);
int newZ = Math.round(iz);
bpt = instanciateBluePrint(newX, newY, newZ, o);
if (bpt == null) {
return null;
}
AxisAlignedBB boundingBox = bpt.getBoundingBox();
if (oldBoundingBox == null || !collision(oldBoundingBox, boundingBox)) {
oldBoundingBox = boundingBox;
if (bpt != null) {
return bpt;
}
}
ix += cx;
iy += cy;
iz += cz;
double distance = (ix - to.x) * (ix - to.x) + (iy - to.y)
* (iy - to.y) + (iz - to.z) * (iz - to.z);
if (distance > lastDistance) {
return null;
} else {
lastDistance = distance;
}
}
}
public PathIterator iterate() {
if (currentIterator.hasNext()) {
PathIterator next = new PathIterator(to, currentIterator);
next.oldBoundingBox = oldBoundingBox;
return next;
} else {
return null;
}
}
public boolean collision(AxisAlignedBB left, AxisAlignedBB right) {
if (left.maxX < right.minX || left.minX > right.maxX) {
return false;
}
if (left.maxY < right.minY || left.minY > right.maxY) {
return false;
}
if (left.maxZ < right.minZ || left.minZ > right.maxZ) {
return false;
}
return true;
}
}
public PathIterator currentPathIterator;
private boolean done = true;
public TileBuilder() {
super();
box.kind = Kind.STRIPES;
}
@Override
public void initialize() {
super.initialize();
if (worldObj.isRemote) {
return;
}
box.kind = Kind.STRIPES;
for (int x = xCoord - 1; x <= xCoord + 1; ++x) {
for (int y = yCoord - 1; y <= yCoord + 1; ++y) {
for (int z = zCoord - 1; z <= zCoord + 1; ++z) {
TileEntity tile = worldObj.getTileEntity(x, y, z);
if (tile instanceof TilePathMarker) {
path = ((TilePathMarker) tile).getPath();
for (BlockIndex b : path) {
worldObj.setBlockToAir(b.x, b.y, b.z);
BuildCraftBuilders.pathMarkerBlock.dropBlockAsItem(
worldObj, b.x, b.y, b.z,
0, 0);
}
break;
}
}
}
}
if (path != null && pathLasers.size() == 0) {
path.getFirst().x = xCoord;
path.getFirst().y = yCoord;
path.getFirst().z = zCoord;
createLasersForPath();
sendNetworkUpdate();
}
iterateBpt();
}
public void createLasersForPath() {
pathLasers = new LinkedList<LaserData>();
BlockIndex previous = null;
for (BlockIndex b : path) {
if (previous != null) {
LaserData laser = new LaserData(new Position(previous.x + 0.5,
previous.y + 0.5, previous.z + 0.5), new Position(
b.x + 0.5, b.y + 0.5, b.z + 0.5));
pathLasers.add(laser);
}
previous = b;
}
}
public BptBuilderBase instanciateBluePrint(int x, int y, int z, ForgeDirection o) {
BlueprintBase bpt = null;
try {
bpt = ItemBlueprint.loadBlueprint(items [0]);
} catch (Throwable t) {
setInventorySlotContents(0, null);
t.printStackTrace();
return null;
}
if (bpt == null) {
return null;
}
BptContext context = bpt.getContext(worldObj, bpt.getBoxForPos(x, y, z));
if (bpt.rotate) {
if (o == ForgeDirection.EAST) {
// Do nothing
} else if (o == ForgeDirection.SOUTH) {
bpt.rotateLeft(context);
} else if (o == ForgeDirection.WEST) {
bpt.rotateLeft(context);
bpt.rotateLeft(context);
} else if (o == ForgeDirection.NORTH) {
bpt.rotateLeft(context);
bpt.rotateLeft(context);
bpt.rotateLeft(context);
}
}
BptBuilderBase result = null;
if (items[0].getItem() instanceof ItemBlueprintStandard) {
result = new BptBuilderBlueprint((Blueprint) bpt, worldObj, x, y, z);
} else if (items[0].getItem() instanceof ItemBlueprintTemplate) {
return new BptBuilderTemplate(bpt, worldObj, x, y, z);
} else {
result = null;
}
return result;
}
public void iterateBpt() {
if (items[0] == null || !(items[0].getItem() instanceof ItemBlueprint)) {
if (bluePrintBuilder != null) {
bluePrintBuilder = null;
}
if (box.isInitialized()) {
box.reset();
}
if (currentPathIterator != null) {
currentPathIterator = null;
}
return;
}
if (bluePrintBuilder == null || bluePrintBuilder.isDone(this)) {
if (path != null && path.size() > 1) {
if (currentPathIterator == null) {
Iterator<BlockIndex> it = path.iterator();
BlockIndex start = it.next();
currentPathIterator = new PathIterator(start, it);
}
bluePrintBuilder = currentPathIterator.next();
if (bluePrintBuilder != null) {
box.reset();
box.initialize(bluePrintBuilder);
sendNetworkUpdate();
}
if (bluePrintBuilder == null) {
currentPathIterator = currentPathIterator.iterate();
}
if (currentPathIterator == null) {
done = true;
}
} else {
if (bluePrintBuilder != null && bluePrintBuilder.isDone(this)) {
done = true;
bluePrintBuilder = null;
} else {
bluePrintBuilder = instanciateBluePrint(xCoord, yCoord, zCoord,
ForgeDirection.values()[worldObj.getBlockMetadata(xCoord, yCoord, zCoord)].getOpposite());
if (bluePrintBuilder != null) {
box.initialize(bluePrintBuilder);
sendNetworkUpdate();
}
}
}
}
}
@Override
public int getSizeInventory() {
return items.length;
}
@Override
public ItemStack getStackInSlot(int i) {
return items[i];
}
@Override
public ItemStack decrStackSize(int i, int j) {
ItemStack result;
if (items[i] == null) {
result = null;
} else if (items[i].stackSize > j) {
result = items[i].splitStack(j);
} else {
ItemStack tmp = items[i];
items[i] = null;
result = tmp;
}
if (!worldObj.isRemote) {
if (i == 0) {
RPCHandler.rpcBroadcastPlayers(this, "setItemRequirements",
null, null);
iterateBpt();
}
}
return result;
}
@Override
public void setInventorySlotContents(int i, ItemStack itemstack) {
items[i] = itemstack;
if (!worldObj.isRemote) {
if (i == 0) {
RPCHandler.rpcBroadcastPlayers(this, "setItemRequirements", null, null);
iterateBpt();
done = false;
}
}
}
@Override
public ItemStack getStackInSlotOnClosing(int slot) {
if (items[slot] == null) {
return null;
}
ItemStack toReturn = items[slot];
items[slot] = null;
return toReturn;
}
@Override
public String getInventoryName() {
return "Builder";
}
@Override
public int getInventoryStackLimit() {
return 64;
}
@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer) {
return worldObj.getTileEntity(xCoord, yCoord, zCoord) == this;
}
@Override
public void readFromNBT(NBTTagCompound nbttagcompound) {
super.readFromNBT(nbttagcompound);
Utils.readStacksFromNBT(nbttagcompound, "Items", items);
if (nbttagcompound.hasKey("box")) {
box.initialize(nbttagcompound.getCompoundTag("box"));
}
if (nbttagcompound.hasKey("path")) {
path = new LinkedList<BlockIndex>();
NBTTagList list = nbttagcompound.getTagList("path",
Utils.NBTTag_Types.NBTTagCompound.ordinal());
for (int i = 0; i < list.tagCount(); ++i) {
path.add(new BlockIndex(list.getCompoundTagAt(i)));
}
}
done = nbttagcompound.getBoolean("done");
}
@Override
public void writeToNBT(NBTTagCompound nbttagcompound) {
super.writeToNBT(nbttagcompound);
Utils.writeStacksToNBT(nbttagcompound, "Items", items);
if (box.isInitialized()) {
NBTTagCompound boxStore = new NBTTagCompound();
box.writeToNBT(boxStore);
nbttagcompound.setTag("box", boxStore);
}
if (path != null) {
NBTTagList list = new NBTTagList();
for (BlockIndex i : path) {
NBTTagCompound c = new NBTTagCompound();
i.writeTo(c);
list.appendTag(c);
}
nbttagcompound.setTag("path", list);
}
nbttagcompound.setBoolean("done", done);
}
@Override
public void invalidate() {
super.invalidate();
destroy();
}
@Override
public void openInventory() {
}
@Override
public void closeInventory() {
}
@Override
public void updateEntity() {
super.updateEntity();
if (worldObj.isRemote) {
return;
}
if (bluePrintBuilder != null) {
bluePrintBuilder.removeDoneBuilders(this);
}
if ((bluePrintBuilder == null || bluePrintBuilder.isDone(this))
&& box.isInitialized()) {
box.reset();
sendNetworkUpdate();
return;
}
iterateBpt();
if (getWorld().getWorldInfo().getGameType() == GameType.CREATIVE) {
build();
} else {
if (mjStored > POWER_ACTIVATION) {
build();
}
}
if (done) {
return;
} else if (mjStored < 25) {
return;
}
}
@Override
public boolean isActive() {
return !done;
}
@Override
public boolean manageFluids() {
return false;
}
@Override
public boolean manageSolids() {
return true;
}
public boolean isBuildingBlueprint() {
return getStackInSlot(0) != null && getStackInSlot(0).getItem() instanceof ItemBlueprint;
}
public Collection<ItemStack> getNeededItems() {
return requiredToBuild;
}
@RPC (RPCSide.CLIENT)
public void setItemRequirements (LinkedList <ItemStack> rq, LinkedList <Integer> realSizes) {
// Item stack serialized are represented through bytes, so 0-255. In
// order to get the real amounts, we need to pass the real sizes of the
// stacks as a separate list.
requiredToBuild = rq;
if (rq != null && rq.size() > 0) {
Iterator <ItemStack> itStack = rq.iterator();
Iterator <Integer> size = realSizes.iterator();
while (true) {
ItemStack stack = itStack.next();
stack.stackSize = size.next();
if (stack.stackSize > 999) {
stack.stackSize = 999;
}
if (!itStack.hasNext()) {
break;
}
}
}
}
@Override
public boolean isBuildingMaterialSlot(int i) {
return i != 0;
}
@Override
public boolean allowAction(IAction action) {
return false;
}
@Override
public boolean hasCustomInventoryName() {
return false;
}
@Override
public boolean isItemValidForSlot(int var1, ItemStack var2) {
return false;
}
@Override
public Box getBox() {
return box;
}
@Override
public AxisAlignedBB getRenderBoundingBox() {
Box renderBox = new Box (this).extendToEncompass(box);
for (LaserData l : pathLasers) {
renderBox = renderBox.extendToEncompass(l.head);
renderBox = renderBox.extendToEncompass(l.tail);
}
return renderBox.expand(50).getBoundingBox();
}
public void build () {
if (!buildTracker.markTimeIfDelay(worldObj)) {
return;
}
if (bluePrintBuilder != null) {
bluePrintBuilder.buildNextSlot(worldObj, this, xCoord, yCoord, zCoord);
if (bluePrintBuilder.isDone(this)) {
bluePrintBuilder.postProcessing(worldObj);
bluePrintBuilder = null;
for (int i = 1; i < items.length; ++i) {
if (items [i] == null) {
items [i] = items [0];
break;
}
}
items [0] = null;
}
if (bluePrintBuilder instanceof BptBuilderBlueprint) {
LinkedList <Integer> realSize = new LinkedList<Integer>();
for (ItemStack stack : ((BptBuilderBlueprint) bluePrintBuilder).neededItems) {
realSize.add(stack.stackSize);
stack.stackSize = 0;
}
RPCHandler.rpcBroadcastPlayers(this, "setItemRequirements",
((BptBuilderBlueprint) bluePrintBuilder).neededItems, realSize);
}
}
}
public BptBuilderBase getBlueprint () {
if (bluePrintBuilder != null) {
return bluePrintBuilder;
} else {
return null;
}
}
}