-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEntityWorker.java
898 lines (741 loc) · 25 KB
/
EntityWorker.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
package net.minecraft.src;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Vector;
public class EntityWorker extends EntityCreature {
protected static final int actionGetEquipment = 0;
protected static final int actionIdle = 1;
protected static final int actionGetOutOfBuilding = 2;
protected static final int actionDeliverGoods = 3;
protected static final int actionGoBack = 5;
protected int actionFreq = 0;
protected boolean blockJumping = false;
protected int ticksToDelivery = 100;
protected int actualTicksToDelivery = 0;
protected String signText1 = "";
protected String signText2 = "";
protected String signText3 = "";
protected int previousSignX;
protected int previousSignY;
protected int previousSignZ;
protected int initialXOffset = 0;
protected int initialYOffset = 0;
protected int initialZOffset = -6;
protected int currentAction;
protected int checkFreq = 1;
protected int MaxCheckFreq = 8;
protected int animFreq = 1;
protected int maxAnimFreq = 24;
protected int workingRange;
protected PathEntity pathToEntity;
protected double starringPointX;
protected double starringPointY;
protected double starringPointZ;
protected Vec3D destPoint;
protected boolean starringPointSet;
protected int stuckCount = 0;
protected int strafingDir = 1;
protected int strafingDirChange = 3;
protected int freeRoamCount = 0;
protected int roamingStuckLimit;
protected ItemStack toolsList[];
protected int currentlyEquipedTool;
protected int currentToolEfficiency;
public int homePosX;
public int homePosY;
public int homePosZ;
public int iPosX;
public int iPosY;
public int iPosZ;
protected boolean isSwinging = false;
protected float speed = 0;
// For Humans+ Compatibility
public boolean goodMob = true;
public boolean peacefulMob = false;
public void setHomePosition(double x, double y, double z) {
homePosX = MathHelper.floor_double(x);
homePosY = MathHelper.floor_double(y);
homePosZ = MathHelper.floor_double(z);
}
public ItemStack getHeldItem() {
return defaultHoldItem;
}
protected ItemStack defaultHoldItem;
public EntityWorker(World world) {
super(world);
workingRange = 20;
starringPointSet = false;
starringPointX = 0;
starringPointY = 0;
starringPointZ = 0;
destPoint = null;
toolsList = new ItemStack[2];
toolsList[0] = null;
toolsList[1] = null;
currentlyEquipedTool = 0;
roamingStuckLimit = 7;
}
protected void onSwing()
{
}
public void onUpdate() {
if (homePosX == 0 && homePosY == 0 && homePosZ == 0) {
Vec3D chestPos = scanForBlockNearEntity(mod_MineColony.hutLumberjack.blockID,
workingRange, workingRange, workingRange);
if (chestPos != null) {
setHomePosition(chestPos.xCoord, chestPos.yCoord,
chestPos.zCoord);
rotationPitch = defaultPitch;
}
}
if (isSwinging)
swingProgress = 1 - ((float) animFreq / maxAnimFreq);
else
swingProgress = 0;
animFreq--;
if(animFreq==0)
{
animFreq=maxAnimFreq;
onSwing();
}
checkFreq--;
if (checkFreq == 0) {
// iPosX = (int)Math.round(posX);
// iPosY = (int)Math.round(posY);
// iPosZ = (int)Math.round(posZ);
iPosX = MathHelper.floor_double(posX);
iPosY = MathHelper.floor_double(posY);
iPosZ = MathHelper.floor_double(posZ);
cutWayThrough();
if(!this.isDead)
{
EntityItem nearbyItem=gatherItemNearby(Item.sign.shiftedIndex);
if(nearbyItem!=null) { onGetItem();nearbyItem.setEntityDead();}
workerUpdate();
}
checkFreq = MaxCheckFreq;
}
if(destPoint!=null && !starringPointSet)
walkToTargetStraight(destPoint);
if (starringPointSet) {
facePoint(starringPointX, starringPointY, starringPointZ, 10);
}
super.onUpdate();
}
protected void workerUpdate() {
// implement worker's task
}
protected void destroySign()
{
// destroy previous sign
if(worldObj.getBlockId(previousSignX, previousSignY, previousSignZ)==Block.signPost.blockID)
placeBlockAt(previousSignX, previousSignY, previousSignZ, 0);
}
protected void destroySignsInRange(int rx, int ry, int rz)
{
for (int i = iPosX - rx; i <= iPosX + rx; i++)
for (int j = iPosY - ry; j <= iPosY + ry; j++)
for (int k = iPosZ - rz; k <= iPosZ + rz; k++) {
{
if(worldObj.getBlockId(i, j, k)==Block.signPost.blockID)
{
TileEntitySign sign = (TileEntitySign)worldObj.getBlockTileEntity(i,j,k);
if(sign==null)
continue;
for(int cp = 0;cp<sign.signText.length;cp++)
{
String textNr = sign.signText[cp];
if(textNr.startsWith("Out") ||
textNr.startsWith("Sleeping") ||
textNr.startsWith("No trees in range"))
{
placeBlockAt(i, j, k, 0);
continue;
}
}
}
}
}
}
protected void placeSign(int blockID, String t1,String t2,String t3) {
// place sign only near blockId
if(scanForBlockNearPoint(blockID, iPosX, iPosY, iPosZ, 3, 3, 3)==null)
return;
Vec3D signVec = null;
signVec = scanForSignPlace(iPosX, iPosY, iPosZ);
if(signVec!=null)
{
int x = MathHelper.floor_double(signVec.xCoord);
int y = MathHelper.floor_double(signVec.yCoord);
int z = MathHelper.floor_double(signVec.zCoord);
placeBlockAt(x,y,z, Block.signPost.blockID);
TileEntitySign sign = (TileEntitySign)worldObj.getBlockTileEntity(x,y,z);
if(sign!=null)
{
sign.signText = new String[]{t1, t2, t3, ""};
previousSignX = x;
previousSignY = y;
previousSignZ = z;
}
}
}
private Vec3D scanForSignPlace(int x, int y, int z) {
for (int i = x - 1; i <= x + 1; i++)
for (int j = y - 1; j <= y + 1; j++)
for (int k = z - 1; k <= z + 1; k++) {
if(Block.signPost.canPlaceBlockAt(worldObj, i,j,k))
return Vec3D.createVectorHelper(i, j, k);
}
return null;
}
protected void cutWayThrough() {
// check if leaves are nearby and cut them into pieces
Vec3D closestLeaves = scanForBlockNearEntity(Block.leaves.blockID, 1,
2, 1);
if (closestLeaves != null) {
placeBlockAt(
MathHelper.floor_double(closestLeaves.xCoord),
MathHelper.floor_double(closestLeaves.yCoord),
MathHelper.floor_double(closestLeaves.zCoord), 0);
}
}
// scan for block type in the specified range
protected Vec3D scanForBlockNearEntity(int blockId, int rx, int ry, int rz) {
int x = iPosX;
int y = iPosY;
int z = iPosZ;
return scanForBlockNearPoint(blockId, x, y, z, rx, ry, rz);
}
protected Vec3D scanForBlockNearPoint(int blockId, double x, double y, double z,
int rx, int ry, int rz) {
return scanForBlockNearPoint(blockId,
MathHelper.floor_double(x),
MathHelper.floor_double(y),
MathHelper.floor_double(z),
rx, ry, rz);
}
protected Vec3D scanForBlockNearPoint(int blockId, int x, int y, int z,
int rx, int ry, int rz) {
Vec3D entityVec = Vec3D.createVector(x, y, z);
Vec3D closestVec = null;
double minDistance = 999999999;
for (int i = x - rx; i <= x + rx; i++)
for (int j = y - ry; j <= y + ry; j++)
for (int k = z - rz; k <= z + rz; k++) {
if (worldObj.getBlockId(i, j, k) == blockId) {
Vec3D tempVec = Vec3D.createVectorHelper(i, j, k);
if (closestVec == null
|| tempVec.distanceTo(entityVec) < minDistance) {
closestVec = tempVec;
minDistance = closestVec.distanceTo(entityVec);
}
}
}
if(minDistance<999999999)
return closestVec;
else
return null;
}
protected Vec3D scanForFreeSpace(int baseBlockId, int x, int y, int z, int rx, int ry, int rz, int height) {
for (int i = x - rx; i <= x + rx; i++)
for (int j = y - ry; j <= y + ry; j++)
for (int k = z - rz; k <= z + rz; k++) {
if (worldObj.getBlockId(i, j, k) == baseBlockId)
{
boolean isFree = true;
for(int h=height;h<=height;h++)
if(worldObj.getBlockId(i, j+h, k) != 0 && worldObj.getBlockId(i, j+h, k)!=Block.snow.blockID)
{
isFree = false;
break;
}
if(isFree)
return Vec3D.createVectorHelper(i, j, k);
}
}
return null;
}
public void facePoint(double px, double py, double pz, float f) {
double dx = px - posX;
double dz = pz - posZ;
if (Math.abs(dx) > 10 || Math.abs(dz) > 10)
return;
float f1 = (float) ((Math.atan2(dz, dx) * 180D) / 3.1415927410125732D) - 90F;
rotationYaw = updateRotation(rotationYaw, f1, f);
double posEyes = iPosY + 1;
float newRotationPitch = 0;
if(py==posEyes)
newRotationPitch = 0;
else if(py-1==posEyes)
newRotationPitch = -25;
else if(py-1>posEyes)
newRotationPitch = -45;
else if(py+1==posEyes)
newRotationPitch = 25;
else if(py+1<posEyes)
newRotationPitch = 45;
if(newRotationPitch>rotationPitch)
rotationPitch += 5;
else if(newRotationPitch<rotationPitch)
rotationPitch -= 5;
if (rotationPitch > 45)
rotationPitch = 45;
if (rotationPitch < -45)
rotationPitch = -45;
}
protected float updateRotation(float f, float f1, float f2) {
float f3;
for (f3 = f1 - f; f3 < -180F; f3 += 360F) {
}
for (; f3 >= 180F; f3 -= 360F) {
}
if (f3 > f2) {
f3 = f2;
}
if (f3 < -f2) {
f3 = -f2;
}
return f + f3;
}
protected void equipItemFromChest(TileEntityChest tileentitychest, int itemId, int i, int toolSlot) {
ItemStack itemStack = null;
itemStack = getItemFromChest(tileentitychest, itemId, i);
if(itemStack!=null)
equipItem(itemStack, toolSlot);
}
protected boolean equipItem(int toolSlot) {
if(toolsList[toolSlot]!=null)
{
defaultHoldItem = toolsList[toolSlot].copy();
equipItemSpecific(toolsList[toolSlot]);
return true;
}
else return false;
}
protected void equipItem(ItemStack item, int toolSlot) {
if(item!=null)
{
defaultHoldItem = item.copy();
toolsList[toolSlot] = item.copy();
equipItemSpecific(item);
}
}
protected void equipItemSpecific(ItemStack item) {
}
protected ItemStack getItemFromChest(TileEntityChest chest, Class itemClass, int i) {
if(chest==null)
return null;
int slotIndex = 0;
ItemStack slot = null;
while((slot=chest.getStackInSlot(slotIndex))== null || slot.getItem().getClass() != itemClass)
{
slotIndex++;
if(slotIndex>=chest.getSizeInventory()-1)
{
slot = null;
break;
}
}
if(slot!=null && slot.stackSize>0) {
int howManyItems = i;
if(i>slot.stackSize)
howManyItems = slot.stackSize;
slot = chest.decrStackSize(slotIndex, howManyItems);
return new ItemStack(slot.getItem().shiftedIndex, howManyItems,0);
}
return null;
}
protected ItemStack getItemFromChest(TileEntityChest chest, int itemId, int i) {
if(chest==null)
return null;
int slotIndex = 0;
ItemStack slot = null;
while((slot=chest.getStackInSlot(slotIndex))== null || slot.itemID != itemId)
{
slotIndex++;
if(slotIndex>=chest.getSizeInventory()-1)
{
slot = null;
break;
}
}
if(slot!=null && slot.stackSize>0) {
int howManyItems = i;
if(i>slot.stackSize)
howManyItems = slot.stackSize;
slot = chest.decrStackSize(slotIndex, howManyItems);
if(slot.stackSize==0)
slot = null;
return new ItemStack(itemId, howManyItems,0);
}
return null;
}
protected boolean putItemIntoChest(TileEntityChest chest, int itemId, int i) {
int slotIndex = 0;
ItemStack slot = null;
while((slot=chest.getStackInSlot(slotIndex))!= null)
{
if(slot.itemID == itemId && slot.stackSize+i<slot.getMaxStackSize())
break;
slotIndex++;
if(slotIndex>=chest.getSizeInventory())
break;
}
if((slot!=null && slot.stackSize+i<slot.getMaxStackSize())) {
chest.setInventorySlotContents(slotIndex, new ItemStack(itemId, slot.stackSize+i,0));
return true;
}
else if(slot==null)
{
chest.setInventorySlotContents(slotIndex, new ItemStack(itemId, i,0));
return true;
}
return false;
}
protected EntityItem gatherItemNearby(int blockID) {
List list = worldObj.getEntitiesWithinAABBExcludingEntity(this,
boundingBox.expand(3.0D, 3.0D, 3.0D));
if (list != null) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getClass() == EntityItem.class) {
EntityItem entity = (EntityItem) list.get(i);
if (!entity.isDead && entity.item.itemID == blockID) {
return entity;
}
}
}
}
return null;
}
protected EntityItem gatherItemNearby(Class entityType) {
List list = worldObj.getEntitiesWithinAABBExcludingEntity(this,
boundingBox.expand(1.0D, 0.0D, 1.0D));
if (list != null) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getClass() == EntityItem.class) {
EntityItem entity = (EntityItem) list.get(i);
if (!entity.isDead && entity.item!=null && entity.item.stackSize>0 && entity.item.getItem().getClass() == entityType) {
return entity;
}
}
}
}
return null;
}
protected boolean toolWeariness(ItemStack inventoryTool) {
if(toolsList[currentlyEquipedTool]!=null)
{
toolsList[currentlyEquipedTool].damageItem(1);
if(toolsList[currentlyEquipedTool].stackSize==0)
{
isSwinging = false;
rotationPitch = defaultPitch;
toolsList[currentlyEquipedTool] = null;
defaultHoldItem = null;
starringPointSet = false;
return true;
}
}
return false;
}
protected boolean isNumber(String in)
{
try
{
Integer.parseInt(in);
}
catch(NumberFormatException ex)
{
return false;
}
return true;
}
protected Vec3D scanForNextCheckpoint(int rx, int ry, int rz, int chp) {
for (int i = iPosX - rx; i <= iPosX + rx; i++)
for (int j = iPosY - ry; j <= iPosY + ry; j++)
for (int k = iPosZ - rz; k <= iPosZ + rz; k++) {
if (worldObj.getBlockId(i, j, k) == Block.signPost.blockID ||
worldObj.getBlockId(i, j, k) == Block.signWall.blockID) {
TileEntitySign sign = (TileEntitySign)worldObj.getBlockTileEntity(i,j,k);
if(sign==null)
continue;
int checkPoint = -1;
for(int cp = 0;cp<sign.signText.length;cp++)
{
String textNr = sign.signText[cp];
if(isNumber(textNr))
{
checkPoint = Integer.parseInt(textNr);
if(checkPoint == chp)
break;
}
}
if(checkPoint == chp)
return Vec3D.createVectorHelper(i, j, k);
}
}
return null;
}
protected void walkToTargetStraight(Vec3D vec3d) {
if(vec3d == null)
return;
isJumping = false;
// if is close to destination then slow down
// or if there are many block around then also slow down
if(destPoint!=null && destPoint.distanceTo(Vec3D.createVector(posX, posY, posZ))<2)
{
speed = (float) 0.5;
}
if(countBlocksAround(iPosX, iPosY, iPosZ, 1,1,1)>12)
speed = (float) 0.5;
if(stuckCount>8)
{
stuckCount = 0;
}
// check if entity is moving
Vec3D prevPos = Vec3D.createVector(prevPosX, posY, prevPosZ);
double distanceWalked = prevPos.squareDistanceTo(posX, posY, posZ);
if (distanceWalked>=0 && distanceWalked < 0.0001)
stuckCount++;
// else
// stuckCount = 0;
if(!blockJumping && stuckCount<10 && worldObj.getBlockId(iPosX, iPosY+2, iPosZ)==0 && (scanForBlockNearPoint(Block.dirt.blockID, iPosX, iPosY, iPosZ, 1, 0, 1) != null ||
scanForBlockNearPoint(Block.stone.blockID, iPosX, iPosY, iPosZ, 1, 0, 1) != null ||
scanForBlockNearPoint(Block.sand.blockID, iPosX, iPosY, iPosZ, 1, 0, 1) != null ||
scanForBlockNearPoint(Block.sandStone.blockID, iPosX, iPosY, iPosZ, 1, 0, 1) != null ||
scanForBlockNearPoint(Block.grass.blockID, iPosX, iPosY, iPosZ, 1, 0, 1) != null ||
scanForBlockNearPoint(Block.cobblestone.blockID, iPosX, iPosY, iPosZ, 1, 0, 1) != null ||
scanForBlockNearPoint(mod_MineColony.hutWarehouse.blockID, iPosX, iPosY, iPosZ, 1, 0, 1) != null ||
scanForBlockNearPoint(mod_MineColony.hutMiner.blockID, iPosX, iPosY, iPosZ, 1, 0, 1) != null ||
scanForBlockNearPoint(mod_MineColony.hutLumberjack.blockID, iPosX, iPosY, iPosZ, 1, 0, 1) != null ||
scanForBlockNearPoint(mod_MineColony.hutFarmer.blockID, iPosX, iPosY, iPosZ, 1, 0, 1) != null ||
scanForBlockNearPoint(Block.tilledField.blockID, iPosX, iPosY, iPosZ, 1, 0, 1) != null ||
scanForBlockNearPoint(Block.gravel.blockID, iPosX, iPosY, iPosZ, 1, 0, 1) != null))
isJumping = true;
// strafe
Vec3D normVec = vec3d.normalize();
int nx = MathHelper.floor_double(normVec.xCoord);
int nz = MathHelper.floor_double(normVec.zCoord);
moveStrafing *= 0.85;
if(stuckCount>3 && (scanForBlockNearPoint(Block.dirt.blockID, iPosX, iPosY+1, iPosZ, 1, 0, 1) != null ||
scanForBlockNearPoint(Block.stone.blockID, iPosX, iPosY+1, iPosZ, 1, 0, 1) != null ||
scanForBlockNearPoint(Block.sand.blockID, iPosX, iPosY+1, iPosZ, 1, 0, 1) != null ||
scanForBlockNearPoint(Block.sandStone.blockID, iPosX, iPosY, iPosZ, 1, 0, 1) != null ||
scanForBlockNearPoint(Block.grass.blockID, iPosX, iPosY+1, iPosZ, 1, 0, 1) != null ||
scanForBlockNearPoint(Block.wood.blockID, iPosX, iPosY+1, iPosZ, 1, 0, 1) != null ||
scanForBlockNearPoint(Block.planks.blockID, iPosX, iPosY+1, iPosZ, 1, 0, 1) != null ||
scanForBlockNearPoint(Block.stairCompactPlanks.blockID, iPosX, iPosY+1, iPosZ, 1, 0, 1) != null ||
scanForBlockNearPoint(Block.gravel.blockID, iPosX, iPosY+1, iPosZ, 1, 0, 1) != null ||
scanForBlockNearPoint(Block.fence.blockID, iPosX, iPosY, iPosZ, 1, 0, 1) != null ||
scanForBlockNearPoint(Block.cobblestone.blockID, iPosX, iPosY+1, iPosZ, 1, 0, 1) != null))
{
if(strafingDirChange>rand.nextInt(20)+10)
{
strafingDir = -strafingDir;
strafingDirChange=0;
}
strafingDirChange++;
moveStrafing = strafingDir*3;
}
// if stuck then seek for free space around and go for it
if(stuckCount>roamingStuckLimit && freeRoamCount<100)
{
speed = 1;
blockJumping = false;
freeRoamCount++;
int rx = rand.nextInt(2);
int rz = rand.nextInt(2);
vec3d = scanForFreeSpace(0, iPosX, iPosY, iPosZ, rx, 0, rz, 2);
}
if(freeRoamCount>=100)
{
freeRoamCount = 0;
stuckCount = 0;
}
if(!isInHouse())
{
// place dirt to stay on something
if(stuckCount>9 && (worldObj.getBlockId(iPosX,iPosY-1,iPosZ) == 0 ||
worldObj.getBlockId(iPosX,iPosY-1,iPosZ) == Block.waterMoving.blockID ||
worldObj.getBlockId(iPosX,iPosY-1,iPosZ) == Block.waterStill.blockID ||
worldObj.getBlockId(iPosX,iPosY-1,iPosZ) == Block.snow.blockID))
{
//stuckCount = 0;
if(isNaturalBlock(iPosX,iPosY-1,iPosZ))
placeBlockAt(iPosX,iPosY-1,iPosZ, Block.dirt.blockID);
}
// dig upwards if not a human placed block
if(stuckCount>5 && isNaturalBlock(iPosX,iPosY+2,iPosZ) && worldObj.getBlockId(iPosX,iPosY+2,iPosZ) != 0)
{
//stuckCount = 0;
placeBlockAt(iPosX,iPosY+2,iPosZ, 0);
}
}
double d = width;
if(vec3d != null && vec3d.squareDistanceTo(posX, posY, posZ) < d * d * 2)
{
return;
}
if (vec3d != null) {
double dx = vec3d.xCoord - posX;
double dz = vec3d.zCoord - posZ;
double dy = vec3d.yCoord - MathHelper.floor_double(boundingBox.minY);
float f4 = (float) ((Math.atan2(dz, dx) * 180D) / 3.1415927410125732D) - 90F;
float f5 = f4 - rotationYaw;
moveForward = speed;
for (; f5 < -180F; f5 += 360F) {
}
for (; f5 >= 180F; f5 -= 360F) {
}
if (f5 > 30F) {
f5 = 30F;
}
if (f5 < -30F) {
f5 = -30F;
}
rotationYaw += f5;
boolean flag1 = handleWaterMovement();
boolean flag2 = handleLavaMovement();
if (flag1 || flag2) {
isJumping = true;
}
}
vec3d = null;
}
protected boolean isNaturalBlock(int x, int y, int z) {
int blockId = worldObj.getBlockId(x,y,z);
if( blockId == 0 ||
blockId == Block.dirt.blockID ||
blockId == Block.grass.blockID ||
blockId == Block.stone.blockID ||
blockId == Block.gravel.blockID ||
blockId == Block.sand.blockID ||
blockId == Block.wood.blockID ||
blockId == Block.oreCoal.blockID ||
blockId == Block.oreDiamond.blockID ||
blockId == Block.oreGold.blockID ||
blockId == Block.oreIron.blockID ||
blockId == Block.oreRedstone.blockID ||
blockId == Block.oreRedstoneGlowing.blockID ||
//blockId == Block.blockIce.blockID ||
blockId == Block.blockSnow.blockID ||
blockId == Block.pumpkin.blockID
)
return true;
return false;
}
protected boolean isInHouse() {
if(iPosX>=homePosX-8 && iPosX<=homePosX+8 &&
iPosY>=homePosY-1 && iPosY<=homePosY+7 &&
iPosZ>=homePosZ-8 && iPosZ<=homePosZ+8)
return true;
return false;
}
protected int countBlocksAround(int x, int y, int z, int rx, int ry, int rz) {
int counter = 0;
for (int i = x - rx; i <= x + rx; i++)
for (int j = y - ry; j <= y + ry; j++)
for (int k = z - rz; k <= z + rz; k++) {
//if (worldObj.getBlockId(i, j, k) != 0 && worldObj.getBlockId(i, j, k) != Block.snow.blockID)
if (worldObj.isBlockOpaqueCube(i, j, k))
counter++;
}
return counter;
}
protected void updatePlayerActionState() {
// needs to be overridden
}
public void writeEntityToNBT(NBTTagCompound nbttagcompound) {
super.writeEntityToNBT(nbttagcompound);
NBTTagList inventoryList = new NBTTagList();
if(toolsList[0]!=null)
{
NBTTagCompound tagCompound = new NBTTagCompound();
toolsList[0].writeToNBT(tagCompound);
inventoryList.setTag(tagCompound);
}
if(toolsList[1]!=null)
{
NBTTagCompound tagCompound = new NBTTagCompound();
toolsList[1].writeToNBT(tagCompound);
inventoryList.setTag(tagCompound);
}
nbttagcompound.setTag("Inventory", inventoryList);
nbttagcompound.setInteger("currentAction", currentAction);
nbttagcompound.setInteger("homePosX", homePosX);
nbttagcompound.setInteger("homePosY", homePosY);
nbttagcompound.setInteger("homePosZ", homePosZ);
nbttagcompound.setInteger("previousSignX", previousSignX);
nbttagcompound.setInteger("previousSignY", previousSignY);
nbttagcompound.setInteger("previousSignZ", previousSignZ);
nbttagcompound.setInteger("currentlyEquipedTool", currentlyEquipedTool);
nbttagcompound.setInteger("currentToolEfficiency", currentToolEfficiency);
}
public void readEntityFromNBT(NBTTagCompound nbttagcompound) {
super.readEntityFromNBT(nbttagcompound);
NBTTagList nbttaglist = nbttagcompound.getTagList("Inventory");
for(int i = 0; i < nbttaglist.tagCount(); i++)
{
NBTTagCompound tagCompound = (NBTTagCompound)nbttaglist.tagAt(i);
ItemStack itemstack = new ItemStack(tagCompound);
if(itemstack.getItem() == null)
{
continue;
}
toolsList[i] = itemstack;
defaultHoldItem = toolsList[i].copy();
}
currentAction = nbttagcompound.getInteger("currentAction");
homePosX = nbttagcompound.getInteger("homePosX");
homePosY = nbttagcompound.getInteger("homePosY");
homePosZ = nbttagcompound.getInteger("homePosZ");
previousSignX = nbttagcompound.getInteger("previousSignX");
previousSignY = nbttagcompound.getInteger("previousSignY");
previousSignZ = nbttagcompound.getInteger("previousSignZ");
currentlyEquipedTool = nbttagcompound.getInteger("currentlyEquipedTool");
currentToolEfficiency = nbttagcompound.getInteger("currentToolEfficiency");
}
protected int getDropItemId() {
return Item.axeStone.shiftedIndex;
}
protected boolean placeBlockAt(int x, int y, int z, int blockId)
{
if(x < 0xfe17b800 || z < 0xfe17b800 || x >= 0x1e84800 || z > 0x1e84800)
{
return false;
}
if(y < 0)
{
return false;
}
if(y >= 128)
{
return false;
}
worldObj.setBlockWithNotify(x, y, z, blockId);
return true;
}
protected int findTopGround(int x, int z) {
int ySolid = 127;
int blockId = worldObj.getBlockId(x, ySolid, z);
while(blockId==0 || blockId==Block.leaves.blockID ||
blockId==Block.wood.blockID ||
blockId==Block.cactus.blockID ||
blockId==Block.crops.blockID ||
blockId==Block.fence.blockID ||
blockId==Block.fire.blockID ||
blockId==Block.cobblestone.blockID ||
blockId==Block.planks.blockID ||
blockId==Block.brick.blockID)
{
ySolid--;
blockId = worldObj.getBlockId(x, ySolid, z);
}
return ySolid+1;
}
protected void onGetItem()
{
//worldObj.playSoundAtEntity(this, "random.pop", 0.4F, 0.4F / (rand.nextFloat() * 0.4F + 0.8F));
}
}