feat: add thanos' ai

This commit is contained in:
punchready 2021-06-01 19:14:44 +02:00
parent 2f45e2b772
commit 850c46c6e1
3 changed files with 98 additions and 6 deletions

View File

@ -80,7 +80,10 @@ public class Inventory implements Iterable<StoneType> {
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<StoneType> {
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<StoneType> iterator() {

View File

@ -20,7 +20,7 @@ public class NPC extends Entity {
*/
public NPC(EntityID id, IntVector2 position, int maxMP, ArrayList<StoneType> inventory) {
super(id, position);
solid = false;
solid = true;
opaque = true;
this.inventory = new Inventory(inventory);
this.mp = new Stat(StatType.MP, maxMP);

View File

@ -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<IntVector2> 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())