From 5e9c6b25d9778d509f461e9c431bf09021deab03 Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Tue, 22 May 2018 21:29:56 +0200 Subject: [PATCH] fixes --- OGP1718-Worms/src/worms/model/Worm.java | 68 +++++++++++++++---------- 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index f44dbf8..9201984 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -490,6 +490,27 @@ public class Worm extends GameObject { if (cost > getActionPoints()) throw new IllegalArgumentException(); + + World world = getWorld(); + if (world != null && world.isAdjacent(newLocation, getRadius())) { + for (Worm w : world.getGameObjectsByClass(Worm.class)) { + if (w.equals(this)) continue; + + Worm smallest = this; + Worm largest = w; + if (smallest.getRadius() > largest.getRadius()) { + smallest = w; + largest = this; + } + double smRadius = smallest.getRadius(); + double lgRadius = largest.getRadius(); + int nb = ThreadLocalRandom.current().nextInt(1, 10); + long smallHp = round(nb / (lgRadius / (smRadius + lgRadius))); + smallest.decreaseHitPoints(smallHp); + largest.decreaseHitPoints((long) nb - smallHp); + } + } + setLocation(newLocation); subtractActionPoints(cost); } @@ -528,9 +549,9 @@ public class Worm extends GameObject { nextLoc = Coordinate.create(nextLoc.getX() + step * cos(direction), nextLoc.getY() + step * sin(direction)); if (!world.isPassable(nextLoc.toArray(), radius)) { - double max = radius; + double max = step; while (!world.isAdjacent(nextLoc.toArray(), radius)) { - max -= 0.01; + max -= step / 100.0; nextLoc = Coordinate.create(nextLoc.getX() - step / 100.0 * cos(direction), nextLoc.getY() - step / 100.0 * sin(direction)); if (max < 0) { @@ -540,7 +561,6 @@ public class Worm extends GameObject { break; } } - return nextLoc; } @@ -571,6 +591,11 @@ public class Worm extends GameObject { maxLocDirection = minDirection; } } + Coordinate tempLoc = getFurthestLocationInDirection(maxDirection, this.getRadius()); + if (getWorld().isAdjacent(tempLoc, getRadius()) && getDistance(location, tempLoc) / Math.abs(direction - minDirection) > getDistance(location, maxLoc) / Math.abs(direction - maxLocDirection)) { + maxLoc = tempLoc; + maxLocDirection = maxDirection; + } if (maxLoc.getX() == location.getX() && maxLoc.getY() == location.getY()) return direction; return maxLocDirection; @@ -924,31 +949,10 @@ public class Worm extends GameObject { attacker = w; } long N = (long) ceil(10.0 * (attacker.getRadius() / attackedWorm.getRadius())); - long loseAttackedWorm = ThreadLocalRandom.current().nextLong(1, 10 * N); - System.out.println(loseAttackedWorm); - System.out.println(attackedWorm.getName()); + long loseAttackedWorm = ThreadLocalRandom.current().nextLong(1, N); attackedWorm.decreaseHitPoints(loseAttackedWorm); } } - -// if (!worm1.isTerminated() && newLocation != oldLocation) { -// for (Worm wormB : worm2) { -// Worm attackedWorm; -// Worm attacker; -// Random rand = new Random(); -// int coin = rand.nextInt(2); -// if (coin == 1) { -// attackedWorm = worm1; -// attacker = wormB; -// } else { -// attackedWorm = wormB; -// attacker = worm1; -// } -// double N = 10 * ceil((attacker.getRadius()) / (attackedWorm.getRadius())); -// long loseAttackedWorm = (long) rand.nextInt((((int) N) - 1) + 1) + 1; -// attackedWorm.decreaseHitPoints(loseAttackedWorm); -// } -// } } @@ -1011,7 +1015,7 @@ public class Worm extends GameObject { */ public void incrementHitPoints(long value) { - double current = getHitPoints(); + long current = getHitPoints(); if (current + value < 0) setHitPoints(0); else setHitPoints(getHitPoints() + value); } @@ -1060,7 +1064,17 @@ public class Worm extends GameObject { long cost = 3 * (long) Math.floor(oldLocation.getY() - getLocation().getY()); decreaseHitPoints(cost); - checkEat(); + if (getWorld() != null) { + for (Worm w : getWorld().getGameObjectsByClass(Worm.class)) { + if (w.equals(this)) continue; + + if (w.getDistance(this) < 0) { + long hp = w.getHitPoints() / 2; + this.incrementHitPoints(hp); + w.decreaseHitPoints((long) ceil(w.getHitPoints() / 2.0)); + } + } + } } /**