Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (attemptCrossing() is called on tick() which happens every 256 ticks)
- public boolean attemptCrossing()
- {
- if (IC2.random.nextInt(3) != 0) {
- return false;
- }
- List<TileEntityCrop> cropTes = new ArrayList(4);
- askCropJoinCross(this.xCoord - 1, this.yCoord, this.zCoord, cropTes);
- askCropJoinCross(this.xCoord + 1, this.yCoord, this.zCoord, cropTes);
- askCropJoinCross(this.xCoord, this.yCoord, this.zCoord - 1, cropTes);
- askCropJoinCross(this.xCoord, this.yCoord, this.zCoord + 1, cropTes);
- if (cropTes.size() < 2) {
- return false;
- }
- CropCard[] crops = (CropCard[])Crops.instance.getCrops().toArray(new CropCard[0]);
- if (crops.length == 0) {
- return false;
- }
- int[] ratios = new int[crops.length];
- int total = 0;
- for (int i = 0; i < ratios.length; i++)
- {
- CropCard crop = crops[i];
- if (crop.canGrow(this)) {
- for (TileEntityCrop te : cropTes) {
- total += calculateRatioFor(crop, te.getCrop());
- }
- }
- ratios[i] = total;
- }
- int search = IC2.random.nextInt(total);
- int min = 0;
- int max = ratios.length - 1;
- int cur;
- while (min < max)
- {
- cur = (min + max) / 2;
- int value = ratios[cur];
- if (search < value) {
- max = cur;
- } else {
- min = cur + 1;
- }
- }
- assert (min == max);
- assert ((min >= 0) && (min < ratios.length));
- assert (ratios[min] > search);
- assert ((min == 0) || (ratios[(min - 1)] <= search));
- this.upgraded = false;
- this.crop = crops[min];
- this.dirty = true;
- this.size = 1;
- this.statGrowth = 0;
- this.statResistance = 0;
- this.statGain = 0;
- for (TileEntityCrop te : cropTes)
- {
- this.statGrowth += te.statGrowth;
- this.statResistance += te.statResistance;
- this.statGain += te.statGain;
- }
- int count = cropTes.size();
- this.statGrowth /= count;
- this.statResistance /= count;
- this.statGain /= count;
- this.statGrowth += IC2.random.nextInt(1 + 2 * count) - count;
- this.statGain += IC2.random.nextInt(1 + 2 * count) - count;
- this.statResistance += IC2.random.nextInt(1 + 2 * count) - count;
- this.statGrowth = Util.limit(this.statGrowth, 0, 31);
- this.statGain = Util.limit(this.statGain, 0, 31);
- this.statResistance = Util.limit(this.statResistance, 0, 31);
- return true;
- }
- public void askCropJoinCross(int x, int y, int z, List<TileEntityCrop> crops)
- {
- TileEntity te = this.worldObj.getTileEntity(x, y, z);
- if (!(te instanceof TileEntityCrop)) {
- return;
- }
- TileEntityCrop sideCrop = (TileEntityCrop)te;
- CropCard neighborCrop = sideCrop.getCrop();
- if (neighborCrop == null) {
- return;
- }
- if ((!neighborCrop.canGrow(this)) || (!neighborCrop.canCross(sideCrop))) {
- return;
- }
- int base = 4;
- if (sideCrop.statGrowth >= 16) {
- base++;
- }
- if (sideCrop.statGrowth >= 30) {
- base++;
- }
- if (sideCrop.statResistance >= 28) {
- base += 27 - sideCrop.statResistance;
- }
- if (base >= IC2.random.nextInt(20)) {
- crops.add(sideCrop);
- }
- }
- public int calculateRatioFor(CropCard newCrop, CropCard oldCrop)
- {
- if (newCrop == oldCrop) {
- return 500;
- }
- int value = 0;
- int delta;
- for (int i = 0; i < 5; i++)
- {
- delta = Math.abs(newCrop.stat(i) - oldCrop.stat(i));
- value += -delta + 2;
- }
- for (String attributeNew : newCrop.attributes()) {
- for (String attributeOld : oldCrop.attributes()) {
- if (attributeNew.equalsIgnoreCase(attributeOld)) {
- value += 5;
- }
- }
- }
- int diff = newCrop.tier() - oldCrop.tier();
- if (diff > 1) {
- value -= 2 * diff;
- }
- if (diff < -3) {
- value -= -diff;
- }
- return Math.max(value, 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement