-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileBuilder.java
646 lines (503 loc) · 14.5 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
/**
* Copyright (c) SpaceToad, 2011
* 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.minecraftforge.common.ForgeDirection;
import buildcraft.BuildCraftBuilders;
import buildcraft.api.core.LaserKind;
import buildcraft.api.core.Position;
import buildcraft.api.gates.IAction;
import buildcraft.api.power.IPowerProvider;
import buildcraft.api.power.IPowerReceptor;
import buildcraft.api.power.PowerFramework;
import buildcraft.core.BlockIndex;
import buildcraft.core.Box;
import buildcraft.core.DefaultProps;
import buildcraft.core.EntityLaser;
import buildcraft.core.EntityPowerLaser;
import buildcraft.core.EntityRobot;
import buildcraft.core.IBuilderInventory;
import buildcraft.core.IMachine;
import buildcraft.core.TileBuildCraft;
import buildcraft.core.blueprints.BptBase;
import buildcraft.core.blueprints.BptBlueprint;
import buildcraft.core.blueprints.BptBuilderBase;
import buildcraft.core.blueprints.BptBuilderBlueprint;
import buildcraft.core.blueprints.BptBuilderTemplate;
import buildcraft.core.blueprints.BptContext;
import buildcraft.core.network.PacketUpdate;
import buildcraft.core.network.TileNetworkData;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.Utils;
public class TileBuilder extends TileBuildCraft implements IBuilderInventory, IPowerReceptor, IMachine {
private final ItemStack items[] = new ItemStack[28];
private BptBuilderBase bluePrintBuilder;
public @TileNetworkData
Box box = new Box();
private IPowerProvider powerProvider;
private LinkedList<BlockIndex> path;
private LinkedList<EntityLaser> pathLasers;
private EntityRobot builderRobot;
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.i - from.i;
double dy = to.j - from.j;
double dz = to.k - from.k;
double size = Math.sqrt(dx * dx + dy * dy + dz * dz);
cx = dx / size / 10;
cy = dy / size / 10;
cz = dz / size / 10;
ix = from.i;
iy = from.j;
iz = from.k;
lastDistance = (ix - to.i) * (ix - to.i) + (iy - to.j) * (iy - to.j) + (iz - to.k) * (iz - to.k);
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.i) * (ix - to.i) + (iy - to.j) * (iy - to.j) + (iz - to.k) * (iz - to.k);
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();
powerProvider = PowerFramework.currentFramework.createPowerProvider();
powerProvider.configure(10, 25, 25, 25, 25);
}
@Override
public void initialize() {
super.initialize();
if (CoreProxy.proxy.isRenderWorld(worldObj))
return;
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.getBlockTileEntity(x, y, z);
if (tile instanceof TilePathMarker) {
path = ((TilePathMarker) tile).getPath();
for (BlockIndex b : path) {
worldObj.setBlock(b.i, b.j, b.k, 0);
BuildCraftBuilders.pathMarkerBlock.dropBlockAsItem(worldObj, b.i, b.j, b.k, BuildCraftBuilders.pathMarkerBlock.blockID, 0);
}
break;
}
}
}
}
if (path != null && pathLasers == null) {
path.getFirst().i = xCoord;
path.getFirst().j = yCoord;
path.getFirst().k = zCoord;
createLasersForPath();
}
iterateBpt();
}
public void createLasersForPath() {
pathLasers = new LinkedList<EntityLaser>();
BlockIndex previous = null;
for (BlockIndex b : path) {
if (previous != null) {
EntityPowerLaser laser = new EntityPowerLaser(worldObj, new Position(previous.i + 0.5, previous.j + 0.5, previous.k + 0.5), new Position(
b.i + 0.5, b.j + 0.5, b.k + 0.5));
laser.setTexture(DefaultProps.TEXTURE_PATH_ENTITIES + "/laser_1.png");
laser.show();
worldObj.spawnEntityInWorld(laser);
pathLasers.add(laser);
}
previous = b;
}
}
public BptBuilderBase instanciateBluePrint(int x, int y, int z, ForgeDirection o) {
BptBase bpt = BuildCraftBuilders.getBptRootIndex().getBluePrint(items[0].getItemDamage());
if (bpt == null)
return null;
bpt = bpt.clone();
BptContext context = new BptContext(worldObj, null, bpt.getBoxForPos(x, y, z));
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);
}
if (items[0].getItem() instanceof ItemBptTemplate)
return new BptBuilderTemplate(bpt, worldObj, x, y, z);
else if (items[0].getItem() instanceof ItemBptBluePrint)
return new BptBuilderBlueprint((BptBlueprint) bpt, worldObj, x, y, z);
else
return null;
}
@Override
public void doWork() {
if (CoreProxy.proxy.isRenderWorld(worldObj))
return;
if (done)
return;
if (builderRobot != null && !builderRobot.readyToBuild())
return;
if (powerProvider.useEnergy(25, 25, true) < 25)
return;
iterateBpt();
/* Temp fix to make Builders impotent as the World Destroyers they are
if (bluePrintBuilder != null && !bluePrintBuilder.done) {
if (!box.isInitialized()) {
box.initialize(bluePrintBuilder);
}
if (builderRobot == null) {
builderRobot = new EntityRobot(worldObj, box);
worldObj.spawnEntityInWorld(builderRobot);
}
box.createLasers(worldObj, LaserKind.Stripes);
builderRobot.scheduleContruction(bluePrintBuilder.getNextBlock(worldObj, new SurroundingInventory(worldObj, xCoord, yCoord, zCoord)),
bluePrintBuilder.getContext());
}
*/
}
public void iterateBpt() {
if (items[0] == null || !(items[0].getItem() instanceof ItemBptBase)) {
if (bluePrintBuilder != null) {
bluePrintBuilder = null;
}
if (builderRobot != null) {
builderRobot.setDead();
builderRobot = null;
}
if (box.isInitialized()) {
box.deleteLasers();
box.reset();
}
if (currentPathIterator != null) {
currentPathIterator = null;
}
return;
}
if (bluePrintBuilder == null || bluePrintBuilder.done) {
if (path != null && path.size() > 1) {
if (currentPathIterator == null) {
Iterator<BlockIndex> it = path.iterator();
BlockIndex start = it.next();
currentPathIterator = new PathIterator(start, it);
}
if (bluePrintBuilder != null && builderRobot != null) {
builderRobot.markEndOfBlueprint(bluePrintBuilder);
}
bluePrintBuilder = currentPathIterator.next();
if (bluePrintBuilder != null) {
box.deleteLasers();
box.reset();
/*
box.initialize(bluePrintBuilder);
box.createLasers(worldObj, LaserKind.Stripes);
*/
}
if (builderRobot != null) {
builderRobot.setBox(box);
}
if (bluePrintBuilder == null) {
currentPathIterator = currentPathIterator.iterate();
}
if (currentPathIterator == null) {
done = true;
}
} else {
if (bluePrintBuilder != null && bluePrintBuilder.done) {
if (builderRobot != null) {
builderRobot.markEndOfBlueprint(bluePrintBuilder);
}
done = true;
bluePrintBuilder = null;
} else {
bluePrintBuilder = instanciateBluePrint(xCoord, yCoord, zCoord,
ForgeDirection.values()[worldObj.getBlockMetadata(xCoord, yCoord, zCoord)].getOpposite());
if (bluePrintBuilder != null) {
box.initialize(bluePrintBuilder);
box.createLasers(worldObj, LaserKind.Stripes);
}
}
}
}
}
@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 (i == 0) {
iterateBpt();
}
return result;
}
@Override
public void setInventorySlotContents(int i, ItemStack itemstack) {
items[i] = itemstack;
if (i == 0) {
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 getInvName() {
return "Builder";
}
@Override
public int getInventoryStackLimit() {
return 64;
}
@Override
public boolean isStackValidForSlot(int i, ItemStack itemstack) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer) {
return worldObj.getBlockTileEntity(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");
for (int i = 0; i < list.tagCount(); ++i) {
path.add(new BlockIndex((NBTTagCompound) list.tagAt(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 destroy() {
if (box.isInitialized()) {
box.deleteLasers();
}
if (builderRobot != null) {
builderRobot.setDead();
builderRobot = null;
}
cleanPathLasers();
}
@Override
public void setPowerProvider(IPowerProvider provider) {
powerProvider = provider;
}
@Override
public IPowerProvider getPowerProvider() {
return powerProvider;
}
@Override
public void handleDescriptionPacket(PacketUpdate packet) {
boolean initialized = box.isInitialized();
super.handleDescriptionPacket(packet);
if (!initialized && box.isInitialized()) {
box.createLasers(worldObj, LaserKind.Stripes);
}
}
@Override
public void handleUpdatePacket(PacketUpdate packet) {
boolean initialized = box.isInitialized();
super.handleUpdatePacket(packet);
if (!initialized && box.isInitialized()) {
box.createLasers(worldObj, LaserKind.Stripes);
}
}
@Override
public void openChest() {
}
@Override
public void closeChest() {
}
@Override
public int powerRequest(ForgeDirection from) {
if ((bluePrintBuilder != null || currentPathIterator != null) && !done)
return powerProvider.getMaxEnergyReceived();
else
return 0;
}
@Override
public void updateEntity() {
super.updateEntity();
if ((bluePrintBuilder == null || bluePrintBuilder.done) && box.isInitialized() && (builderRobot == null || builderRobot.done())) {
box.deleteLasers();
box.reset();
if (CoreProxy.proxy.isSimulating(worldObj)) {
sendNetworkUpdate();
}
return;
}
if (!box.isInitialized() && bluePrintBuilder == null && builderRobot != null) {
builderRobot.setDead();
builderRobot = null;
}
}
@Override
public boolean isActive() {
return !done;
}
@Override
public boolean manageLiquids() {
return false;
}
@Override
public boolean manageSolids() {
return true;
}
public void cleanPathLasers() {
if (pathLasers != null) {
for (EntityLaser laser : pathLasers) {
laser.setDead();
}
pathLasers = null;
}
}
public boolean isBuildingBlueprint() {
return getStackInSlot(0) != null && getStackInSlot(0).getItem() instanceof ItemBptBluePrint;
}
public Collection<ItemStack> getNeededItems() {
if (bluePrintBuilder instanceof BptBuilderBlueprint)
return ((BptBuilderBlueprint) bluePrintBuilder).neededItems;
else
return null;
}
@Override
public boolean isBuildingMaterial(int i) {
return i != 0;
}
@Override
public boolean allowAction(IAction action) {
return false;
}
}