From 850c46c6e1e926f3a7f81f0782a890235d4b4c15 Mon Sep 17 00:00:00 2001 From: punchready Date: Tue, 1 Jun 2021 19:14:44 +0200 Subject: [PATCH] feat: add thanos' ai --- .../gamelibrary/entities/Inventory.java | 16 +++- .../marvelous/gamelibrary/entities/NPC.java | 2 +- .../gamelibrary/gamelogic/GameLogic.java | 86 ++++++++++++++++++- 3 files changed, 98 insertions(+), 6 deletions(-) diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Inventory.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Inventory.java index cf1bbbd..98f9d80 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Inventory.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Inventory.java @@ -80,7 +80,10 @@ public class Inventory implements Iterable { content.remove(stone); } - /** Returns the stones inside of the Inventory as Array of {@link StoneType StoneTypes} */ + /** + * Returns the stones inside of the Inventory as Array of {@link StoneType StoneTypes}. + * @return The array of stones the inventory has + */ public StoneType[] getStonesAsArray() { var toReturn = new StoneType[content.size()]; var iterator = content.iterator(); @@ -90,6 +93,17 @@ public class Inventory implements Iterable { return toReturn; } + /** + * Transfers the stones inside of the Inventory to a different Inventory. + * @param to The {@link Inventory} to transfer to + */ + public void transfer(Inventory to) { + for(StoneType stone: content) { + to.addStone(stone); + } + content.clear(); + } + /** Iterates over the inventory. */ @Override public Iterator iterator() { diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/NPC.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/NPC.java index 15b784b..818180c 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/NPC.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/NPC.java @@ -20,7 +20,7 @@ public class NPC extends Entity { */ public NPC(EntityID id, IntVector2 position, int maxMP, ArrayList inventory) { super(id, position); - solid = false; + solid = true; opaque = true; this.inventory = new Inventory(inventory); this.mp = new Stat(StatType.MP, maxMP); diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java index fdb750a..38e0b16 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java @@ -14,6 +14,7 @@ import java.awt.*; import java.awt.geom.Line2D; import java.awt.geom.Point2D; import java.sql.ResultSetMetaData; +import java.time.OffsetDateTime; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; @@ -424,7 +425,7 @@ class GameLogic { * Verifies that a {@link Character} is alive. */ private static void requireAlive(Character entity) throws InvalidRequestException { - if(entity.hp.getValue() <= 0 || !entity.isActive()) { + if(!entity.isAlive() || !entity.isActive()) { throw new InvalidRequestException(); } } @@ -560,7 +561,7 @@ class GameLogic { ((Character)state.entities.findEntity(((CharacterEvent)event).targetEntity)).ap.decreaseValue(((CharacterEvent)event).amount); } case ConsumedMPEvent -> { - ((Character)state.entities.findEntity(((CharacterEvent)event).targetEntity)).mp.decreaseValue(((CharacterEvent)event).amount); + ((NPC)state.entities.findEntity(((CharacterEvent)event).targetEntity)).mp.decreaseValue(((CharacterEvent)event).amount); } case SpawnEntityEvent -> { state.entities.addEntity(((EntityEvent)event).entity); @@ -569,7 +570,7 @@ class GameLogic { ((Character)state.entities.findEntity(((CharacterEvent)event).targetEntity)).hp.increaseValue(((CharacterEvent)event).amount); } case MoveEvent -> { - Character target = (Character)state.entities.findEntity(((CharacterEvent)event).originEntity); + NPC target = (NPC)state.entities.findEntity(((CharacterEvent)event).originEntity); for(Entity entity: state.entities.findByPosition(((CharacterEvent)event).targetField)) { if(entity instanceof InfinityStone) { target.inventory.addStone(((InfinityStone)entity).type); @@ -912,7 +913,83 @@ class GameLogic { } } } - //TODO: implement thanos ai + + if(picked == null) { + return result; + } + + ArrayList path = GameLogic.Bresenham4Connected(thanos.getPosition(), picked); + + int mp = thanos.mp.getValue(); + IntVector2 current = thanos.getPosition(); + for(IntVector2 pos: path) { + if(pos.equals(thanos.getPosition())) { + continue; + } + + if(!pos.equals(picked)) { + for(Entity entity: state.entities.findByPosition(pos)) { + if(entity instanceof Rock) { + result.add(new EventBuilder(EventType.DestroyedEntityEvent) + .withTargetEntity(entity.id) + .withTargetField(entity.getPosition()) + .buildEntityEvent()); + } + } + } + + result.add(new EventBuilder(EventType.MoveEvent) + .withOriginEntity(thanos.id) + .withOriginField(current) + .withTargetField(pos) + .buildCharacterEvent()); + result.add(new EventBuilder(EventType.ConsumedMPEvent) + .withTargetEntity(thanos.id) + .withTargetField(pos) + .withAmount(1) + .buildCharacterEvent()); + + if(pos.equals(picked)) { + for(Entity entity: state.entities.findByPosition(pos)) { + if(entity instanceof Character) { + result.add(new EventBuilder(EventType.MoveEvent) + .withOriginEntity(entity.id) + .withOriginField(pos) + .withTargetField(current) + .buildCharacterEvent()); + result.add(new EventBuilder(EventType.TakenDamageEvent) + .withTargetEntity(entity.id) + .withTargetField(current) + .withAmount(((Character)entity).hp.getValue()) + .buildEntityEvent()); + ((Character)entity).inventory.transfer(thanos.inventory); + break; + } + if(entity instanceof InfinityStone) { + break; + } + } + } + + current = pos; + if(--mp == 0) { + break; + } + } + + }else { + + for(EntityID id: state.turnOrder) { + if(id.equals(thanos.id)) { + continue; + } + if(rand.nextBoolean()) { + result.add(new EventBuilder(EventType.DestroyedEntityEvent) + .withTargetEntity(id) + .withTargetField(state.entities.findEntity(id).getPosition()) + .buildEntityEvent()); + } + } } @@ -942,6 +1019,7 @@ class GameLogic { .withTargetField(thanos.getPosition()) .withAmount(thanos.mp.getValue() - thanos.mp.getMax()) .buildGameEvent()); + thanos.mp.setValue(thanos.mp.getMax()); } result.add(new EventBuilder(EventType.TurnEvent) .withTurnCount(state.turnOrder.size())