-
Notifications
You must be signed in to change notification settings - Fork 497
/
EntityRobot.java
1310 lines (1072 loc) · 33.8 KB
/
EntityRobot.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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* 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.robotics;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.WeakHashMap;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemArmor;
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.util.DamageSource;
import net.minecraft.util.EntityDamageSource;
import net.minecraft.util.IIcon;
import net.minecraft.util.MathHelper;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;
import cpw.mods.fml.common.registry.IEntityAdditionalSpawnData;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;
import buildcraft.BuildCraftCore;
import buildcraft.BuildCraftRobotics;
import buildcraft.api.boards.RedstoneBoardNBT;
import buildcraft.api.boards.RedstoneBoardRegistry;
import buildcraft.api.boards.RedstoneBoardRobot;
import buildcraft.api.boards.RedstoneBoardRobotNBT;
import buildcraft.api.core.BCLog;
import buildcraft.api.core.BlockIndex;
import buildcraft.api.core.IZone;
import buildcraft.api.robots.AIRobot;
import buildcraft.api.robots.DockingStation;
import buildcraft.api.robots.EntityRobotBase;
import buildcraft.api.robots.IRobotOverlayItem;
import buildcraft.api.robots.RobotManager;
import buildcraft.api.statements.StatementSlot;
import buildcraft.api.tiles.IDebuggable;
import buildcraft.core.DefaultProps;
import buildcraft.core.ItemWrench;
import buildcraft.core.LaserData;
import buildcraft.core.lib.RFBattery;
import buildcraft.core.lib.network.command.CommandWriter;
import buildcraft.core.lib.network.command.ICommandReceiver;
import buildcraft.core.lib.network.command.PacketCommand;
import buildcraft.core.lib.utils.NBTUtils;
import buildcraft.core.lib.utils.NetworkUtils;
import buildcraft.robotics.ai.AIRobotMain;
import buildcraft.robotics.ai.AIRobotShutdown;
import buildcraft.robotics.ai.AIRobotSleep;
import buildcraft.robotics.statements.ActionRobotWorkInArea;
import buildcraft.robotics.statements.ActionRobotWorkInArea.AreaType;
public class EntityRobot extends EntityRobotBase implements
IEntityAdditionalSpawnData, IInventory, IFluidHandler, ICommandReceiver, IDebuggable {
public static final ResourceLocation ROBOT_BASE = new ResourceLocation(
DefaultProps.TEXTURE_PATH_ROBOTS + "/robot_base.png");
public LaserData laser = new LaserData();
public DockingStation linkedDockingStation;
public BlockIndex linkedDockingStationIndex;
public ForgeDirection linkedDockingStationSide;
public BlockIndex currentDockingStationIndex;
public ForgeDirection currentDockingStationSide;
public boolean isDocked = false;
public NBTTagCompound originalBoardNBT;
public RedstoneBoardRobot board;
public AIRobotMain mainAI;
public ItemStack itemInUse;
public float itemAngle1 = 0;
public float renderItemAngle1 = 0;
public float itemAngle2 = 0;
public boolean itemActive = false;
public float itemActiveStage = 0;
public long lastUpdateTime = 0;
private DockingStation currentDockingStation;
private List<ItemStack> wearables = new ArrayList<ItemStack>();
private boolean needsUpdate = false;
private ItemStack[] inv = new ItemStack[4];
private FluidStack tank;
private int maxFluid = FluidContainerRegistry.BUCKET_VOLUME * 4;
private ResourceLocation texture;
private WeakHashMap<Entity, Boolean> unreachableEntities = new WeakHashMap<Entity, Boolean>();
private NBTTagList stackRequestNBT;
private RFBattery battery = new RFBattery(MAX_ENERGY, MAX_ENERGY, 100);
private boolean firstUpdateDone = false;
private boolean isActiveClient = false;
private long robotId = EntityRobotBase.NULL_ROBOT_ID;
private int energySpendPerCycle = 0;
private int ticksCharging = 0;
private float energyFX = 0;
private int steamDx = 0;
private int steamDy = -1;
private int steamDz = 0;
public EntityRobot(World world, NBTTagCompound boardNBT) {
this(world);
originalBoardNBT = boardNBT;
board = (RedstoneBoardRobot) RedstoneBoardRegistry.instance.getRedstoneBoard(boardNBT).create(boardNBT, this);
dataWatcher.updateObject(16, board.getNBTHandler().getID());
if (!world.isRemote) {
mainAI = new AIRobotMain(this);
mainAI.start();
}
}
public EntityRobot(World world) {
super(world);
motionX = 0;
motionY = 0;
motionZ = 0;
ignoreFrustumCheck = true;
laser.isVisible = false;
entityCollisionReduction = 1F;
width = 0.25F;
height = 0.25F;
}
@Override
protected void entityInit() {
super.entityInit();
setNullBoundingBox();
preventEntitySpawning = false;
noClip = true;
isImmuneToFire = true;
this.func_110163_bv(); // persistenceRequired = true
dataWatcher.addObject(12, Float.valueOf(0));
dataWatcher.addObject(13, Float.valueOf(0));
dataWatcher.addObject(14, Float.valueOf(0));
dataWatcher.addObject(15, Byte.valueOf((byte) 0));
dataWatcher.addObject(16, "");
dataWatcher.addObject(17, Float.valueOf(0));
dataWatcher.addObject(18, Float.valueOf(0));
dataWatcher.addObject(19, Integer.valueOf(0));
dataWatcher.addObject(20, Byte.valueOf((byte) 0));
dataWatcher.addObject(21, Integer.valueOf(0));
}
protected void updateDataClient() {
laser.tail.x = dataWatcher.getWatchableObjectFloat(12);
laser.tail.y = dataWatcher.getWatchableObjectFloat(13);
laser.tail.z = dataWatcher.getWatchableObjectFloat(14);
laser.isVisible = dataWatcher.getWatchableObjectByte(15) == 1;
RedstoneBoardNBT<?> boardNBT = RedstoneBoardRegistry.instance.getRedstoneBoard(dataWatcher
.getWatchableObjectString(16));
if (boardNBT != null) {
texture = ((RedstoneBoardRobotNBT) boardNBT).getRobotTexture();
}
itemAngle1 = dataWatcher.getWatchableObjectFloat(17);
itemAngle2 = dataWatcher.getWatchableObjectFloat(18);
energySpendPerCycle = dataWatcher.getWatchableObjectInt(19);
isActiveClient = dataWatcher.getWatchableObjectByte(20) == 1;
battery.setEnergy(dataWatcher.getWatchableObjectInt(21));
}
protected void updateDataServer() {
dataWatcher.updateObject(12, Float.valueOf((float) laser.tail.x));
dataWatcher.updateObject(13, Float.valueOf((float) laser.tail.y));
dataWatcher.updateObject(14, Float.valueOf((float) laser.tail.z));
dataWatcher.updateObject(15, Byte.valueOf((byte) (laser.isVisible ? 1 : 0)));
dataWatcher.updateObject(17, Float.valueOf(itemAngle1));
dataWatcher.updateObject(18, Float.valueOf(itemAngle2));
}
public boolean isActive() {
if (worldObj.isRemote) {
return isActiveClient;
} else {
return mainAI.getActiveAI() instanceof AIRobotSleep || mainAI.getActiveAI() instanceof AIRobotShutdown;
}
}
protected void init() {
if (worldObj.isRemote) {
BuildCraftCore.instance.sendToServer(new PacketCommand(this, "requestInitialization", null));
}
}
public void setLaserDestination (float x, float y, float z) {
if (x != laser.tail.x || y != laser.tail.y || z != laser.tail.z) {
laser.tail.x = x;
laser.tail.y = y;
laser.tail.z = z;
needsUpdate = true;
}
}
public void showLaser() {
if (!laser.isVisible) {
laser.isVisible = true;
needsUpdate = true;
}
}
public void hideLaser() {
if (laser.isVisible) {
laser.isVisible = false;
needsUpdate = true;
}
}
protected void firstUpdate() {
if (stackRequestNBT != null) {
}
if (!worldObj.isRemote) {
getRegistry().registerRobot(this);
}
}
@Override
public String getCommandSenderName() {
return StatCollector.translateToLocal("item.robot.name");
}
@Override
public void onEntityUpdate() {
this.worldObj.theProfiler.startSection("bcEntityRobot");
if (!firstUpdateDone) {
firstUpdate();
firstUpdateDone = true;
}
if (ticksCharging > 0) {
ticksCharging--;
}
if (!worldObj.isRemote) {
// The client-side sleep indicator should also display if the robot is charging.
// To not break gates and other things checking for sleep, this is done here.
dataWatcher.updateObject(20, Byte.valueOf((byte) ((isActive() && ticksCharging == 0) ? 1 : 0)));
dataWatcher.updateObject(21, getEnergy());
if (needsUpdate) {
updateDataServer();
needsUpdate = false;
}
}
if (worldObj.isRemote) {
updateDataClient();
updateRotationYaw(60.0f);
updateEnergyFX();
}
if (currentDockingStation != null) {
motionX = 0;
motionY = 0;
motionZ = 0;
posX = currentDockingStation.x() + 0.5F + currentDockingStation.side().offsetX * 0.5F;
posY = currentDockingStation.y() + 0.5F + currentDockingStation.side().offsetY * 0.5F;
posZ = currentDockingStation.z() + 0.5F + currentDockingStation.side().offsetZ * 0.5F;
}
if (!worldObj.isRemote) {
if (linkedDockingStation == null) {
if (linkedDockingStationIndex != null) {
linkedDockingStation = getRegistry().getStation(linkedDockingStationIndex.x,
linkedDockingStationIndex.y, linkedDockingStationIndex.z,
linkedDockingStationSide);
}
if (linkedDockingStation == null
|| linkedDockingStation.robotTaking() != this) {
if (!(mainAI.getDelegateAI() instanceof AIRobotShutdown)) {
BCLog.logger.info("Shutting down robot " + this.toString() + " - no docking station");
mainAI.startDelegateAI(new AIRobotShutdown(this));
}
}
}
if (currentDockingStationIndex != null && currentDockingStation == null) {
currentDockingStation = getRegistry().getStation(
currentDockingStationIndex.x,
currentDockingStationIndex.y,
currentDockingStationIndex.z,
currentDockingStationSide);
}
if (linkedDockingStation == null || linkedDockingStation.isInitialized()) {
this.worldObj.theProfiler.startSection("bcRobotAIMainCycle");
mainAI.cycle();
this.worldObj.theProfiler.endSection();
if (energySpendPerCycle != mainAI.getActiveAI().getEnergyCost()) {
energySpendPerCycle = mainAI.getActiveAI().getEnergyCost();
dataWatcher.updateObject(19, energySpendPerCycle);
}
}
}
super.onEntityUpdate();
this.worldObj.theProfiler.endSection();
}
@Override
protected void updateEntityActionState() {
}
@SideOnly(Side.CLIENT)
private void updateEnergyFX() {
energyFX += energySpendPerCycle;
if (energyFX >= (100 << (2 * Minecraft.getMinecraft().gameSettings.particleSetting))) {
energyFX = 0;
spawnEnergyFX();
}
}
@SideOnly(Side.CLIENT)
private void spawnEnergyFX() {
Minecraft.getMinecraft().effectRenderer.addEffect(new EntityRobotEnergyParticle(
worldObj,
posX + steamDx * 0.25, posY + steamDy * 0.25, posZ + steamDz * 0.25,
steamDx * 0.05, steamDy * 0.05, steamDz * 0.05,
energySpendPerCycle * 0.075F < 1 ? 1 : energySpendPerCycle * 0.075F));
}
@Override
public AxisAlignedBB getBoundingBox() {
return AxisAlignedBB.getBoundingBox(posX - 0.25F, posY - 0.25F, posZ - 0.25F, posX + 0.25F, posY + 0.25F, posZ + 0.25F);
}
public void setNullBoundingBox() {
width = 0F;
height = 0F;
boundingBox.minX = posX;
boundingBox.minY = posY;
boundingBox.minZ = posZ;
boundingBox.maxX = posX;
boundingBox.maxY = posY;
boundingBox.maxZ = posZ;
}
private void iterateBehaviorDocked() {
motionX = 0F;
motionY = 0F;
motionZ = 0F;
setNullBoundingBox();
}
@Override
public void writeSpawnData(ByteBuf data) {
data.writeByte(wearables.size());
for (ItemStack s : wearables) {
NetworkUtils.writeStack(data, s);
}
}
@Override
public void readSpawnData(ByteBuf data) {
int amount = data.readUnsignedByte();
while (amount > 0) {
wearables.add(NetworkUtils.readStack(data));
amount--;
}
init();
}
@Override
public ItemStack getHeldItem() {
return itemInUse;
}
@Override
public void setCurrentItemOrArmor(int i, ItemStack itemstack) {
}
@Override
public ItemStack[] getLastActiveItems() {
return new ItemStack [0];
}
@Override
protected void fall(float par1) {}
@Override
protected void updateFallState(double par1, boolean par3) {}
@Override
public void moveEntityWithHeading(float par1, float par2) {
this.setPosition(posX + motionX, posY + motionY, posZ + motionZ);
}
@Override
public boolean isOnLadder() {
return false;
}
public ResourceLocation getTexture() {
return texture;
}
@Override
public void writeEntityToNBT(NBTTagCompound nbt) {
super.writeEntityToNBT(nbt);
if (linkedDockingStationIndex != null) {
NBTTagCompound linkedStationNBT = new NBTTagCompound();
NBTTagCompound linkedStationIndexNBT = new NBTTagCompound();
linkedDockingStationIndex.writeTo(linkedStationIndexNBT);
linkedStationNBT.setTag("index", linkedStationIndexNBT);
linkedStationNBT.setByte("side", (byte) linkedDockingStationSide.ordinal());
nbt.setTag("linkedStation", linkedStationNBT);
}
if (currentDockingStationIndex != null) {
NBTTagCompound currentStationNBT = new NBTTagCompound();
NBTTagCompound currentStationIndexNBT = new NBTTagCompound();
currentDockingStationIndex.writeTo(currentStationIndexNBT);
currentStationNBT.setTag("index", currentStationIndexNBT);
currentStationNBT.setByte("side", (byte) currentDockingStationSide.ordinal());
nbt.setTag("currentStation", currentStationNBT);
}
NBTTagCompound nbtLaser = new NBTTagCompound();
laser.writeToNBT(nbtLaser);
nbt.setTag("laser", nbtLaser);
NBTTagCompound batteryNBT = new NBTTagCompound();
battery.writeToNBT(batteryNBT);
nbt.setTag("battery", batteryNBT);
if (itemInUse != null) {
NBTTagCompound itemNBT = new NBTTagCompound();
itemInUse.writeToNBT(itemNBT);
nbt.setTag("itemInUse", itemNBT);
nbt.setBoolean("itemActive", itemActive);
}
for (int i = 0; i < inv.length; ++i) {
NBTTagCompound stackNbt = new NBTTagCompound();
if (inv[i] != null) {
nbt.setTag("inv[" + i + "]", inv[i].writeToNBT(stackNbt));
}
}
if (wearables.size() > 0) {
NBTTagList wearableList = new NBTTagList();
for (int i = 0; i < wearables.size(); i++) {
NBTTagCompound item = new NBTTagCompound();
wearables.get(i).writeToNBT(item);
wearableList.appendTag(item);
}
nbt.setTag("wearables", wearableList);
}
nbt.setTag("originalBoardNBT", originalBoardNBT);
NBTTagCompound ai = new NBTTagCompound();
mainAI.writeToNBT(ai);
nbt.setTag("mainAI", ai);
if (mainAI.getDelegateAI() != board) {
NBTTagCompound boardNBT = new NBTTagCompound();
board.writeToNBT(boardNBT);
nbt.setTag("board", boardNBT);
}
nbt.setLong("robotId", robotId);
if (tank != null) {
NBTTagCompound tankNBT = new NBTTagCompound();
tank.writeToNBT(tankNBT);
nbt.setTag("tank", tankNBT);
}
}
@Override
public void readEntityFromNBT(NBTTagCompound nbt) {
super.readEntityFromNBT(nbt);
if (nbt.hasKey("linkedStation")) {
NBTTagCompound linkedStationNBT = nbt.getCompoundTag("linkedStation");
linkedDockingStationIndex = new BlockIndex(linkedStationNBT.getCompoundTag("index"));
linkedDockingStationSide = ForgeDirection.values()[linkedStationNBT.getByte("side")];
}
if (nbt.hasKey("currentStation")) {
NBTTagCompound currentStationNBT = nbt.getCompoundTag("currentStation");
currentDockingStationIndex = new BlockIndex(currentStationNBT.getCompoundTag("index"));
currentDockingStationSide = ForgeDirection.values()[currentStationNBT.getByte("side")];
}
laser.readFromNBT(nbt.getCompoundTag("laser"));
battery.readFromNBT(nbt.getCompoundTag("battery"));
wearables.clear();
if (nbt.hasKey("wearables")) {
NBTTagList list = nbt.getTagList("wearables", 10);
for (int i = 0; i < list.tagCount(); i++) {
wearables.add(ItemStack.loadItemStackFromNBT(list.getCompoundTagAt(i)));
}
}
if (nbt.hasKey("itemInUse")) {
itemInUse = ItemStack.loadItemStackFromNBT(nbt.getCompoundTag("itemInUse"));
itemActive = nbt.getBoolean("itemActive");
}
for (int i = 0; i < inv.length; ++i) {
inv[i] = ItemStack.loadItemStackFromNBT(nbt.getCompoundTag("inv[" + i + "]"));
}
originalBoardNBT = nbt.getCompoundTag("originalBoardNBT");
NBTTagCompound ai = nbt.getCompoundTag("mainAI");
mainAI = (AIRobotMain) AIRobot.loadAI(ai, this);
if (nbt.hasKey("board")) {
board = (RedstoneBoardRobot) AIRobot.loadAI(nbt.getCompoundTag("board"), this);
} else {
board = (RedstoneBoardRobot) mainAI.getDelegateAI();
}
dataWatcher.updateObject(16, board.getNBTHandler().getID());
stackRequestNBT = nbt.getTagList("stackRequests", Constants.NBT.TAG_COMPOUND);
if (nbt.hasKey("robotId")) {
robotId = nbt.getLong("robotId");
}
if (nbt.hasKey("tank")) {
tank = FluidStack.loadFluidStackFromNBT(nbt.getCompoundTag("tank"));
} else {
tank = null;
}
// Restore robot persistence on pre-6.1.9 robotics
this.func_110163_bv();
}
@Override
public void dock(DockingStation station) {
currentDockingStation = station;
setSteamDirection(
currentDockingStation.side.offsetX,
currentDockingStation.side.offsetY,
currentDockingStation.side.offsetZ);
currentDockingStationIndex = currentDockingStation.index();
currentDockingStationSide = currentDockingStation.side();
}
@Override
public void undock() {
if (currentDockingStation != null) {
currentDockingStation.release(this);
currentDockingStation = null;
setSteamDirection(0, -1, 0);
currentDockingStationIndex = null;
currentDockingStationSide = null;
}
}
@Override
public DockingStation getDockingStation() {
return currentDockingStation;
}
@Override
public void setMainStation(DockingStation station) {
if (linkedDockingStation != null && linkedDockingStation != station) {
linkedDockingStation.unsafeRelease(this);
}
linkedDockingStation = station;
if (station != null) {
linkedDockingStationIndex = linkedDockingStation.index();
linkedDockingStationSide = linkedDockingStation.side();
} else {
linkedDockingStationIndex = null;
linkedDockingStationSide = ForgeDirection.UNKNOWN;
}
}
@Override
public ItemStack getEquipmentInSlot(int var1) {
return null;
}
@Override
public int getSizeInventory() {
return inv.length;
}
@Override
public ItemStack getStackInSlot(int var1) {
return inv[var1];
}
@Override
public ItemStack decrStackSize(int var1, int var2) {
ItemStack result = inv[var1].splitStack(var2);
if (inv[var1].stackSize == 0) {
inv[var1] = null;
}
updateClientSlot(var1);
return result;
}
@Override
public ItemStack getStackInSlotOnClosing(int var1) {
return inv[var1].splitStack(var1);
}
@Override
public void setInventorySlotContents(int var1, ItemStack var2) {
inv[var1] = var2;
updateClientSlot(var1);
}
@Override
public String getInventoryName() {
return null;
}
@Override
public boolean hasCustomInventoryName() {
return false;
}
@Override
public int getInventoryStackLimit() {
return 64;
}
@Override
public void markDirty() {
}
public void updateClientSlot(final int slot) {
BuildCraftCore.instance.sendToEntity(new PacketCommand(this, "clientSetInventory", new CommandWriter() {
public void write(ByteBuf data) {
data.writeShort(slot);
NetworkUtils.writeStack(data, inv[slot]);
}
}), this);
}
@Override
public boolean isUseableByPlayer(EntityPlayer var1) {
return false;
}
@Override
public void openInventory() {
}
@Override
public void closeInventory() {
}
@Override
public boolean isItemValidForSlot(int var1, ItemStack var2) {
return inv[var1] == null
|| (inv[var1].isItemEqual(var2) && inv[var1].isStackable() && inv[var1].stackSize
+ var2.stackSize <= inv[var1].getItem().getItemStackLimit(inv[var1]));
}
@Override
public boolean isMoving() {
return motionX != 0 || motionY != 0 || motionZ != 0;
}
@Override
public void setItemInUse(ItemStack stack) {
itemInUse = stack;
BuildCraftCore.instance.sendToEntity(new PacketCommand(this, "clientSetItemInUse", new CommandWriter() {
public void write(ByteBuf data) {
NetworkUtils.writeStack(data, itemInUse);
}
}), this);
}
private void setSteamDirection(final int x, final int y, final int z) {
if (!worldObj.isRemote) {
BuildCraftCore.instance.sendToEntity(new PacketCommand(this, "setSteamDirection", new CommandWriter() {
public void write(ByteBuf data) {
data.writeInt(x);
data.writeShort(y);
data.writeInt(z);
}
}), this);
} else {
Vec3 v = Vec3.createVectorHelper(x, y, z);
v = v.normalize();
steamDx = (int) v.xCoord;
steamDy = (int) v.yCoord;
steamDz = (int) v.zCoord;
}
}
@Override
public void receiveCommand(String command, Side side, Object sender, ByteBuf stream) {
if (side.isClient()) {
if ("clientSetItemInUse".equals(command)) {
itemInUse = NetworkUtils.readStack(stream);
} else if ("clientSetInventory".equals(command)) {
int slot = stream.readUnsignedShort();
inv[slot] = NetworkUtils.readStack(stream);
} else if ("initialize".equals(command)) {
itemInUse = NetworkUtils.readStack(stream);
itemActive = stream.readBoolean();
} else if ("setItemActive".equals(command)) {
itemActive = stream.readBoolean();
itemActiveStage = 0;
lastUpdateTime = new Date().getTime();
if (!itemActive) {
setSteamDirection(0, -1, 0);
}
} else if ("setSteamDirection".equals(command)) {
setSteamDirection(stream.readInt(), stream.readShort(), stream.readInt());
} else if ("syncWearables".equals(command)) {
wearables.clear();
int amount = stream.readUnsignedByte();
while (amount > 0) {
wearables.add(NetworkUtils.readStack(stream));
amount--;
}
}
} else if (side.isServer()) {
EntityPlayer p = (EntityPlayer) sender;
if ("requestInitialization".equals(command)) {
BuildCraftCore.instance.sendToPlayer(p, new PacketCommand(this, "initialize", new CommandWriter() {
public void write(ByteBuf data) {
NetworkUtils.writeStack(data, itemInUse);
data.writeBoolean(itemActive);
}
}));
for (int i = 0; i < inv.length; ++i) {
final int j = i;
BuildCraftCore.instance.sendToPlayer(p, new PacketCommand(this, "clientSetInventory", new CommandWriter() {
public void write(ByteBuf data) {
data.writeShort(j);
NetworkUtils.writeStack(data, inv[j]);
}
}));
}
if (currentDockingStation != null) {
setSteamDirection(
currentDockingStation.side.offsetX,
currentDockingStation.side.offsetY,
currentDockingStation.side.offsetZ);
} else {
setSteamDirection(0, -1, 0);
}
}
}
}
@Override
public void setHealth(float par1) {
// deactivate health management
}
@Override
public boolean attackEntityFrom(DamageSource par1, float par2) {
// deactivate being hit
return false;
}
@Override
public float getAimYaw() {
return itemAngle1;
}
@Override
public float getAimPitch() {
return itemAngle2;
}
@Override
public void aimItemAt(float yaw, float pitch) {
itemAngle1 = yaw;
itemAngle2 = pitch;
updateDataServer();
}
@Override
public void aimItemAt(int x, int y, int z) {
int deltaX = x - (int) Math.floor(posX);
int deltaY = y - (int) Math.floor(posY);
int deltaZ = z - (int) Math.floor(posZ);
if (deltaX != 0 || deltaZ != 0) {
itemAngle1 = (float) (Math.atan2(deltaZ, deltaX) * 180f / Math.PI) + 180f;
}
double d3 = MathHelper.sqrt_double(deltaX * deltaX + deltaZ * deltaZ);
itemAngle2 = (float) (-(Math.atan2(deltaY, d3) * 180.0D / Math.PI));
setSteamDirection(deltaX, deltaY, deltaZ);
updateDataServer();
}
private void updateRotationYaw(float maxStep) {
float step = MathHelper.wrapAngleTo180_float(itemAngle1 - rotationYaw);
if (step > maxStep) {
step = maxStep;
}
if (step < -maxStep) {
step = -maxStep;
}
rotationYaw = rotationYaw + step;
}
@Override
protected float func_110146_f(float targetYaw, float dist) {
if (worldObj.isRemote) {
float f2 = MathHelper.wrapAngleTo180_float(this.rotationYaw - this.renderYawOffset);
this.renderYawOffset += f2 * 0.5F;
float f3 = MathHelper.wrapAngleTo180_float(this.rotationYaw - this.renderYawOffset);
boolean flag = f3 < -90.0F || f3 >= 90.0F;
this.renderYawOffset = this.rotationYaw - f3;
if (f3 * f3 > 2500.0F) {
this.renderYawOffset += f3 * 0.2F;
}
float newDist = dist;
if (flag) {
newDist *= -1.0F;
}
return newDist;
}
return 0;
}
@Override
public void setItemActive(final boolean isActive) {
if (isActive != itemActive) {
itemActive = isActive;
BuildCraftCore.instance.sendToEntity(new PacketCommand(this, "setItemActive", new CommandWriter() {
public void write(ByteBuf data) {
data.writeBoolean(isActive);
}
}), this);
}
}
@Override
public RedstoneBoardRobot getBoard() {
return board;
}
@Override
public DockingStation getLinkedStation() {
return linkedDockingStation;
}
@SideOnly(Side.CLIENT)
@Override
public boolean isInRangeToRenderDist(double par1) {
return true;
}
@Override
public int getEnergy() {
return battery.getEnergyStored();
}
@Override
public RFBattery getBattery() {
return battery;
}
@Override
protected boolean canDespawn() {
return false;
}
public AIRobot getOverridingAI() {
return mainAI.getOverridingAI();
}
public void overrideAI(AIRobot ai) {
mainAI.setOverridingAI(ai);
}
public void attackTargetEntityWithCurrentItem(Entity par1Entity) {
if (par1Entity.canAttackWithItem()) {
if (!par1Entity.hitByEntity(this)) {
this.setLastAttacker(par1Entity);
boolean flag2 = par1Entity.attackEntityFrom(new EntityDamageSource("robot", this), 2.0F);
EnchantmentHelper.func_151385_b(this, par1Entity);
ItemStack itemstack = itemInUse;
Object object = par1Entity;
if (itemstack != null && object instanceof EntityLivingBase) {
itemstack.getItem().hitEntity(itemstack, (EntityLivingBase) object, this);
}
}
}
}
@Override
public IZone getZoneToWork() {
return getZone(ActionRobotWorkInArea.AreaType.WORK);
}
@Override
public IZone getZoneToLoadUnload() {
IZone zone = getZone(ActionRobotWorkInArea.AreaType.LOAD_UNLOAD);
if (zone == null) {
zone = getZoneToWork();
}
return zone;
}
private IZone getZone(AreaType areaType) {
for (StatementSlot s : linkedDockingStation.getActiveActions()) {
if (s.statement instanceof ActionRobotWorkInArea
&& ((ActionRobotWorkInArea) s.statement).getAreaType() == areaType) {
IZone zone = ActionRobotWorkInArea.getArea(s);