diff --git a/pom.xml b/pom.xml index 2485585..558f039 100644 --- a/pom.xml +++ b/pom.xml @@ -7,8 +7,8 @@ jar UTF-8 - 14 - 14 + 11 + 11 16 0.0.6 @@ -32,4 +32,4 @@ ${javafx.version} - \ No newline at end of file + diff --git a/src/main/java/lol/client/ai/RandomAI.java b/src/main/java/lol/client/ai/RandomAI.java index f60584d..1a2e26f 100644 --- a/src/main/java/lol/client/ai/RandomAI.java +++ b/src/main/java/lol/client/ai/RandomAI.java @@ -28,7 +28,22 @@ public Turn championSelect() { public Turn turn() { Turn turn = new Turn(); - // Try to attack the Nexus first. + // First check if a champion needs to be revived. + arena.teamOf(teamID).forEachChampion((champion, id) -> { + if (!champion.isAlive()){ + turn.registerAction(new Revive(teamID, id, (int) (Math.random()*9), (int) (Math.random()*9))); + } + }); + // Try to attack ennemies next. + arena.teamOf(teamID).forEachChampion((champion, id) -> + battlefield.visitAdjacent(champion.x(), champion.y(), champion.attackRange(), new TileVisitor(){ + public void visitChampion(Champion otherChampion) { + if(arena.teamOf(1-teamID).belongsToTeam(otherChampion)) { + turn.registerAction(new Attack(teamID, id, otherChampion.x(), otherChampion.y())); + } + } + })); + // Try to attack the Nexus next. arena.teamOf(teamID).forEachChampion((champion, id) -> battlefield.visitAdjacent(champion.x(), champion.y(), champion.attackRange(), new TileVisitor(){ public void visitNexus(Nexus nexus) { diff --git a/src/main/java/lol/game/Arena.java b/src/main/java/lol/game/Arena.java index a0b1e0d..8192c66 100644 --- a/src/main/java/lol/game/Arena.java +++ b/src/main/java/lol/game/Arena.java @@ -50,6 +50,19 @@ public void visitSpawn(int teamID, int championID, int x, int y) { } } + public void visitRevive(int teamID, int championID, int x, int y) { + if(phase == Phase.GAME) { + if(!championsWhoActed.contains(championID)) { + if(teams.get(teamID).reviveChampion(championID, x, y)) { + championsWhoActed.add(championID); + } + } + else { + System.out.println("Team " + teamID + " tried to move a champion outside a GAME phase."); + } + } + } + public void visitMove(int teamID, int championID, int x, int y) { if(phase == Phase.GAME) { if(!championsWhoActed.contains(championID)) { diff --git a/src/main/java/lol/game/Battlefield.java b/src/main/java/lol/game/Battlefield.java index ae6ac68..dd3599f 100644 --- a/src/main/java/lol/game/Battlefield.java +++ b/src/main/java/lol/game/Battlefield.java @@ -86,6 +86,12 @@ public boolean allNexusAlive() { return true; } + public void removeChampion(Destructible d, int x, int y) { + if (d instanceof Champion && !d.isAlive()) { + battlefield[y][x] = Optional.empty(); + } + } + // We can place something at (x, y) if: // * The ground tile at (x, y) is walkable. // * No other destructible is present at (x, y). diff --git a/src/main/java/lol/game/Champion.java b/src/main/java/lol/game/Champion.java index 26f7198..41d565a 100644 --- a/src/main/java/lol/game/Champion.java +++ b/src/main/java/lol/game/Champion.java @@ -5,6 +5,7 @@ public class Champion extends Destructible { private int rangeOfAttack; private int damages; private int speed; + private int deathTimer = 0; private Champion(String name, int hp, int rangeOfAttack, int damages, int speed) { super(hp); @@ -62,6 +63,18 @@ public boolean attack(Destructible d) { return false; } + public void increaseTimer() { + deathTimer = deathTimer + 1; + } + + public boolean reachedTimer() { + return deathTimer==3; + } + + public void resetTimer() { + deathTimer = 0; + } + @Override public String toString() { return name(); } diff --git a/src/main/java/lol/game/Destructible.java b/src/main/java/lol/game/Destructible.java index 135e9e8..90817d7 100644 --- a/src/main/java/lol/game/Destructible.java +++ b/src/main/java/lol/game/Destructible.java @@ -33,6 +33,10 @@ public boolean isAlive() { return currentHP > 0; } + public void revive() { + currentHP = initialHP; + } + public void reviveAt(int x, int y) { currentHP = initialHP; place(x, y); diff --git a/src/main/java/lol/game/Team.java b/src/main/java/lol/game/Team.java index 13feaa4..1fcc33a 100644 --- a/src/main/java/lol/game/Team.java +++ b/src/main/java/lol/game/Team.java @@ -31,6 +31,10 @@ public void forEachChampion(BiConsumer f) { } } + public boolean belongsToTeam(Champion champion) { + return champions.contains(champion); + } + public boolean spawnChampion(int championID, int x, int y) { Champion champion = champions.get(championID); boolean placed = battlefield.placeAt(champion, x, y); @@ -40,6 +44,28 @@ public boolean spawnChampion(int championID, int x, int y) { return placed; } + public void removeChampion(Destructible d ,int x ,int y) { + battlefield.removeChampion(d, x, y); + } + + public boolean reviveChampion(int championID ,int x ,int y) { + Champion champion = champions.get(championID); + boolean revived = false; + if(champion.reachedTimer()) { + revived = battlefield.placeAt(champion, x, y); + champion.revive(); + champion.resetTimer(); + } + else { + champion.increaseTimer(); + revived = true; + } + if(!revived && logInvalidMove) { + System.out.println("Invalid respawn position of champion " + champion.name()); + } + return revived; + } + public boolean moveChampion(int championID, int x, int y) { Champion champion = champions.get(championID); boolean moved = false; @@ -58,6 +84,7 @@ public boolean championAttack(int championID, int x, int y) { battlefield.visit(x, y, new TileVisitor(){ @Override public void visitDestructible(Destructible d) { attacked[0] = champion.attack(d); + removeChampion(d, x ,y); } }); if(!attacked[0]) { diff --git a/src/main/java/lol/game/action/ActionVisitor.java b/src/main/java/lol/game/action/ActionVisitor.java index 8f847da..65c51ba 100644 --- a/src/main/java/lol/game/action/ActionVisitor.java +++ b/src/main/java/lol/game/action/ActionVisitor.java @@ -2,6 +2,7 @@ public interface ActionVisitor { public void visitSpawn(int teamID, int championID, int x, int y); + public void visitRevive(int teamID,int championID, int x, int y); public void visitMove(int teamID, int championID, int x, int y); public void visitAttack(int teamID, int championID, int x, int y); public void visitChampionSelect(int teamID, String championName); diff --git a/src/main/java/lol/game/action/Revive.java b/src/main/java/lol/game/action/Revive.java new file mode 100644 index 0000000..39d1ae1 --- /dev/null +++ b/src/main/java/lol/game/action/Revive.java @@ -0,0 +1,15 @@ +package lol.game.action; + +import lol.game.*; +import java.io.Serializable; + +public class Revive extends ChampionAction implements Serializable { + + public Revive(int teamID, int championID, int x, int y) { + super(teamID, championID, x, y); + } + + public void accept(ActionVisitor visitor) { + visitor.visitRevive(teamID, championID, x, y); + } +} \ No newline at end of file