Advertisement
Guest User

Untitled

a guest
Jan 8th, 2020
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. (attemptCrossing() is called on tick() which happens every 256 ticks)
  2.  
  3. public boolean attemptCrossing()
  4. {
  5. if (IC2.random.nextInt(3) != 0) {
  6. return false;
  7. }
  8. List<TileEntityCrop> cropTes = new ArrayList(4);
  9.  
  10. askCropJoinCross(this.xCoord - 1, this.yCoord, this.zCoord, cropTes);
  11. askCropJoinCross(this.xCoord + 1, this.yCoord, this.zCoord, cropTes);
  12. askCropJoinCross(this.xCoord, this.yCoord, this.zCoord - 1, cropTes);
  13. askCropJoinCross(this.xCoord, this.yCoord, this.zCoord + 1, cropTes);
  14. if (cropTes.size() < 2) {
  15. return false;
  16. }
  17. CropCard[] crops = (CropCard[])Crops.instance.getCrops().toArray(new CropCard[0]);
  18. if (crops.length == 0) {
  19. return false;
  20. }
  21. int[] ratios = new int[crops.length];
  22. int total = 0;
  23. for (int i = 0; i < ratios.length; i++)
  24. {
  25. CropCard crop = crops[i];
  26. if (crop.canGrow(this)) {
  27. for (TileEntityCrop te : cropTes) {
  28. total += calculateRatioFor(crop, te.getCrop());
  29. }
  30. }
  31. ratios[i] = total;
  32. }
  33. int search = IC2.random.nextInt(total);
  34.  
  35. int min = 0;
  36. int max = ratios.length - 1;
  37. int cur;
  38. while (min < max)
  39. {
  40. cur = (min + max) / 2;
  41. int value = ratios[cur];
  42. if (search < value) {
  43. max = cur;
  44. } else {
  45. min = cur + 1;
  46. }
  47. }
  48. assert (min == max);
  49. assert ((min >= 0) && (min < ratios.length));
  50. assert (ratios[min] > search);
  51. assert ((min == 0) || (ratios[(min - 1)] <= search));
  52.  
  53. this.upgraded = false;
  54. this.crop = crops[min];
  55. this.dirty = true;
  56. this.size = 1;
  57.  
  58. this.statGrowth = 0;
  59. this.statResistance = 0;
  60. this.statGain = 0;
  61. for (TileEntityCrop te : cropTes)
  62. {
  63. this.statGrowth += te.statGrowth;
  64. this.statResistance += te.statResistance;
  65. this.statGain += te.statGain;
  66. }
  67. int count = cropTes.size();
  68.  
  69. this.statGrowth /= count;
  70. this.statResistance /= count;
  71. this.statGain /= count;
  72.  
  73. this.statGrowth += IC2.random.nextInt(1 + 2 * count) - count;
  74. this.statGain += IC2.random.nextInt(1 + 2 * count) - count;
  75. this.statResistance += IC2.random.nextInt(1 + 2 * count) - count;
  76.  
  77. this.statGrowth = Util.limit(this.statGrowth, 0, 31);
  78. this.statGain = Util.limit(this.statGain, 0, 31);
  79. this.statResistance = Util.limit(this.statResistance, 0, 31);
  80.  
  81. return true;
  82. }
  83.  
  84. public void askCropJoinCross(int x, int y, int z, List<TileEntityCrop> crops)
  85. {
  86. TileEntity te = this.worldObj.getTileEntity(x, y, z);
  87. if (!(te instanceof TileEntityCrop)) {
  88. return;
  89. }
  90. TileEntityCrop sideCrop = (TileEntityCrop)te;
  91. CropCard neighborCrop = sideCrop.getCrop();
  92. if (neighborCrop == null) {
  93. return;
  94. }
  95. if ((!neighborCrop.canGrow(this)) || (!neighborCrop.canCross(sideCrop))) {
  96. return;
  97. }
  98. int base = 4;
  99. if (sideCrop.statGrowth >= 16) {
  100. base++;
  101. }
  102. if (sideCrop.statGrowth >= 30) {
  103. base++;
  104. }
  105. if (sideCrop.statResistance >= 28) {
  106. base += 27 - sideCrop.statResistance;
  107. }
  108. if (base >= IC2.random.nextInt(20)) {
  109. crops.add(sideCrop);
  110. }
  111. }
  112.  
  113. public int calculateRatioFor(CropCard newCrop, CropCard oldCrop)
  114. {
  115. if (newCrop == oldCrop) {
  116. return 500;
  117. }
  118. int value = 0;
  119. int delta;
  120. for (int i = 0; i < 5; i++)
  121. {
  122. delta = Math.abs(newCrop.stat(i) - oldCrop.stat(i));
  123.  
  124. value += -delta + 2;
  125. }
  126. for (String attributeNew : newCrop.attributes()) {
  127. for (String attributeOld : oldCrop.attributes()) {
  128. if (attributeNew.equalsIgnoreCase(attributeOld)) {
  129. value += 5;
  130. }
  131. }
  132. }
  133. int diff = newCrop.tier() - oldCrop.tier();
  134. if (diff > 1) {
  135. value -= 2 * diff;
  136. }
  137. if (diff < -3) {
  138. value -= -diff;
  139. }
  140. return Math.max(value, 0);
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement