fix: remove major flaw in state manipulation

This commit is contained in:
punchready 2021-06-04 06:41:59 +02:00
parent 4eb71fa972
commit b20b428705
2 changed files with 66 additions and 23 deletions

View File

@ -187,6 +187,7 @@ public class GameLogic {
} }
} }
}catch(InvalidRequestException exception) { }catch(InvalidRequestException exception) {
System.out.println("Request denied: " + exception.getMessage());
return false; return false;
}catch(Exception ignored) { }catch(Exception ignored) {
return false; return false;
@ -505,6 +506,11 @@ public class GameLogic {
if(!target.isAlive()) { if(!target.isAlive()) {
state.winConditions.increaseValue(opposing, WinCondition.TotalKnockouts, 1); state.winConditions.increaseValue(opposing, WinCondition.TotalKnockouts, 1);
} }
if(state.activeCharacter != null && state.activeCharacter.type == EntityType.NPC && state.activeCharacter.id == NPCType.Thanos.getID()) {
NPC thanos = (NPC)state.entities.findEntity(state.activeCharacter);
target.inventory.transfer(thanos.inventory);
}
} }
case ConsumedAPEvent -> { case ConsumedAPEvent -> {
EntityEvent data = (EntityEvent)event; EntityEvent data = (EntityEvent)event;
@ -520,6 +526,21 @@ public class GameLogic {
((NPC)state.entities.findEntity(data.targetEntity)).mp.decreaseValue(data.amount); ((NPC)state.entities.findEntity(data.targetEntity)).mp.decreaseValue(data.amount);
} }
} }
case RoundSetupEvent -> {
GameEvent data = (GameEvent)event;
state.roundNumber++;
state.turnNumber = 0;
state.turnOrder = new ArrayList<>();
for(EntityID turn: data.characterOrder) {
if(turn.type != EntityType.NPC) {
state.turnOrder.add(turn);
}
}
state.stoneCooldown.update();
}
case TurnEvent -> { case TurnEvent -> {
GameEvent data = (GameEvent)event; GameEvent data = (GameEvent)event;
@ -529,11 +550,23 @@ public class GameLogic {
target.mp.setValue(target.mp.getMax()); target.mp.setValue(target.mp.getMax());
}else if(data.nextCharacter.id == NPCType.Thanos.getID()) { }else if(data.nextCharacter.id == NPCType.Thanos.getID()) {
NPC target = (NPC)state.entities.findEntity(data.nextCharacter); NPC target = (NPC)state.entities.findEntity(data.nextCharacter);
if(state.roundNumber > state.partyConfig.maxRounds + 1) {
target.mp.setMax(target.mp.getMax() + 1);
}
target.mp.setValue(target.mp.getMax()); target.mp.setValue(target.mp.getMax());
} }
state.turnNumber++;
state.activeCharacter = data.nextCharacter.clone();
} }
case SpawnEntityEvent -> { case SpawnEntityEvent -> {
state.entities.addEntity(((EntityEvent)event).entity); EntityEvent data = (EntityEvent)event;
state.entities.addEntity(data.entity);
if(state.activeCharacter != null && state.activeCharacter.type == EntityType.NPC && state.activeCharacter.id == NPCType.Goose.getID()) {
state.unvomitedStones.remove(((InfinityStone)data.entity).type);
}
} }
case HealedEvent -> { case HealedEvent -> {
CharacterEvent data = (CharacterEvent)event; CharacterEvent data = (CharacterEvent)event;
@ -577,6 +610,9 @@ public class GameLogic {
state.winConditions.updateValue(target.id.type, WinCondition.MaxStones, target.inventory.getSize()); state.winConditions.updateValue(target.id.type, WinCondition.MaxStones, target.inventory.getSize());
} }
case WinEvent -> {
state.won = true;
}
case GamestateEvent -> { case GamestateEvent -> {
GamestateEvent data = (GamestateEvent)event; GamestateEvent data = (GamestateEvent)event;
@ -584,7 +620,7 @@ public class GameLogic {
state.entities.addEntities(data.entities); state.entities.addEntities(data.entities);
state.mapSize.set(data.mapSize); state.mapSize.set(data.mapSize);
state.turnOrder = ArrayTools.toArrayList(data.turnOrder); state.turnOrder = ArrayTools.toArrayList(data.turnOrder);
state.activeCharacter = data.activeCharacter; state.activeCharacter = data.activeCharacter == null ? null : data.activeCharacter.clone();
state.stoneCooldown.import_(data.stoneCooldowns); state.stoneCooldown.import_(data.stoneCooldowns);
state.won = data.winCondition; state.won = data.winCondition;
} }
@ -680,6 +716,8 @@ public class GameLogic {
* @return The list of resulting {@link Event Events} * @return The list of resulting {@link Event Events}
*/ */
protected static ArrayList<Event> startGame(GameState state, ArrayList<Integer> selectedCharacters1, ArrayList<Integer> selectedCharacters2) { protected static ArrayList<Event> startGame(GameState state, ArrayList<Integer> selectedCharacters1, ArrayList<Integer> selectedCharacters2) {
System.out.println("Starting game");
ArrayList<Event> result = new ArrayList<>(); ArrayList<Event> result = new ArrayList<>();
ArrayList<IntVector2> free = new ArrayList<>(); ArrayList<IntVector2> free = new ArrayList<>();
@ -771,6 +809,8 @@ public class GameLogic {
* @return The list of resulting {@link Event Events} * @return The list of resulting {@link Event Events}
*/ */
private static ArrayList<Event> handleTurnEnd(GameState state) { private static ArrayList<Event> handleTurnEnd(GameState state) {
System.out.println("Turn ended");
ArrayList<Event> result = new ArrayList<>(); ArrayList<Event> result = new ArrayList<>();
ArrayList<EntityID> alive = new ArrayList<>(); ArrayList<EntityID> alive = new ArrayList<>();
@ -824,10 +864,11 @@ public class GameLogic {
* @return The list of resulting {@link Event Events} * @return The list of resulting {@link Event Events}
*/ */
private static ArrayList<Event> handleRoundStart(GameState state) { private static ArrayList<Event> handleRoundStart(GameState state) {
System.out.println("Starting round " + (state.roundNumber + 1));
ArrayList<Event> result = new ArrayList<>(); ArrayList<Event> result = new ArrayList<>();
state.roundNumber++; state.roundNumber++;
state.turnNumber = 0;
result.add(new EventBuilder(EventType.RoundSetupEvent) result.add(new EventBuilder(EventType.RoundSetupEvent)
.withRoundCount(state.roundNumber) .withRoundCount(state.roundNumber)
@ -865,13 +906,13 @@ public class GameLogic {
} }
} }
state.stoneCooldown.update();
turns.addAll(state.turnOrder); turns.addAll(state.turnOrder);
// RoundSetupEvent has to be sent first, but the contents of it are determined later... // RoundSetupEvent has to be sent first, but the contents of it are determined later...
((GameEvent)result.get(0)).characterOrder = turns.toArray(new EntityID[0]); ((GameEvent)result.get(0)).characterOrder = turns.toArray(new EntityID[0]);
System.out.println(turns);
return result; return result;
} }
@ -881,13 +922,14 @@ public class GameLogic {
* @return The list of resulting {@link Event Events} * @return The list of resulting {@link Event Events}
*/ */
private static ArrayList<Event> handleGoose(GameState state) { private static ArrayList<Event> handleGoose(GameState state) {
System.out.println("Handling goose");
ArrayList<Event> result = new ArrayList<>(); ArrayList<Event> result = new ArrayList<>();
ArrayList<StoneType> inventory = new ArrayList<>(state.unvomitedStones); ArrayList<StoneType> inventory = new ArrayList<>(state.unvomitedStones);
int picked = rand.nextInt(inventory.size()); int picked = rand.nextInt(inventory.size());
StoneType stone = inventory.get(picked); StoneType stone = inventory.get(picked);
inventory.remove(picked); inventory.remove(picked);
state.unvomitedStones.remove(stone);
ArrayList<IntVector2> free = new ArrayList<>(); ArrayList<IntVector2> free = new ArrayList<>();
for(int x = 0; x < state.mapSize.getX(); x++) { for(int x = 0; x < state.mapSize.getX(); x++) {
@ -925,6 +967,8 @@ public class GameLogic {
* @return The list of resulting {@link Event Events} * @return The list of resulting {@link Event Events}
*/ */
private static ArrayList<Event> handleStan(GameState state, HashSet<EntityID> revived) { private static ArrayList<Event> handleStan(GameState state, HashSet<EntityID> revived) {
System.out.println("Handling stan");
ArrayList<Event> result = new ArrayList<>(); ArrayList<Event> result = new ArrayList<>();
ArrayList<Character> characters = new ArrayList<>(); ArrayList<Character> characters = new ArrayList<>();
@ -989,6 +1033,8 @@ public class GameLogic {
* @return The list of resulting {@link Event Events} * @return The list of resulting {@link Event Events}
*/ */
private static ArrayList<Event> spawnThanos(GameState state) { private static ArrayList<Event> spawnThanos(GameState state) {
System.out.println("Spawning thanos");
ArrayList<Event> result = new ArrayList<>(); ArrayList<Event> result = new ArrayList<>();
ArrayList<IntVector2> free = getFreeFields(state); ArrayList<IntVector2> free = getFreeFields(state);
@ -1019,6 +1065,8 @@ public class GameLogic {
* @return The list of resulting {@link Event Events} * @return The list of resulting {@link Event Events}
*/ */
private static ArrayList<Event> handleThanos(GameState state, NPC thanos) { private static ArrayList<Event> handleThanos(GameState state, NPC thanos) {
System.out.println("Handling thanos");
ArrayList<Event> result = new ArrayList<>(); ArrayList<Event> result = new ArrayList<>();
result.add(new EventBuilder(EventType.TurnEvent) result.add(new EventBuilder(EventType.TurnEvent)
@ -1099,7 +1147,6 @@ public class GameLogic {
.withTargetField(current) .withTargetField(current)
.withAmount(((Character)entity).hp.getValue()) .withAmount(((Character)entity).hp.getValue())
.buildEntityEvent()); .buildEntityEvent());
((Character)entity).inventory.transfer(thanos.inventory);
break; break;
} }
if(entity instanceof InfinityStone) { if(entity instanceof InfinityStone) {
@ -1139,24 +1186,16 @@ public class GameLogic {
* @return The list of resulting {@link Event Events} * @return The list of resulting {@link Event Events}
*/ */
private static ArrayList<Event> handleTurnStart(GameState state) { private static ArrayList<Event> handleTurnStart(GameState state) {
ArrayList<Event> result = new ArrayList<>(); System.out.println("Turn of " + state.activeCharacter + " started");
state.turnNumber++; ArrayList<Event> result = new ArrayList<>();
if(state.activeCharacter.type == EntityType.NPC && state.activeCharacter.id == NPCType.Thanos.getID()) { if(state.activeCharacter.type == EntityType.NPC && state.activeCharacter.id == NPCType.Thanos.getID()) {
NPC thanos = (NPC)state.entities.findEntity(state.activeCharacter); NPC thanos = (NPC)state.entities.findEntity(state.activeCharacter);
if(state.roundNumber > state.partyConfig.maxRounds + 1) {
thanos.mp.setMax(thanos.mp.getMax() + 1);//TODO: use event for this...
}
result.addAll(handleThanos(state, thanos)); result.addAll(handleThanos(state, thanos));
return result; return result;
} }
Character activeCharacter = (Character)state.entities.findEntity(state.activeCharacter);
result.add(new EventBuilder(EventType.TurnEvent) result.add(new EventBuilder(EventType.TurnEvent)
.withTurnCount(state.turnOrder.size()) .withTurnCount(state.turnOrder.size())
.withNextCharacter(state.activeCharacter) .withNextCharacter(state.activeCharacter)
@ -1174,8 +1213,6 @@ public class GameLogic {
private static ArrayList<Event> handlePlayerWin(GameState state, EntityType winner) { private static ArrayList<Event> handlePlayerWin(GameState state, EntityType winner) {
ArrayList<Event> result = new ArrayList<>(); ArrayList<Event> result = new ArrayList<>();
state.won = true;
result.add(new EventBuilder(EventType.WinEvent) result.add(new EventBuilder(EventType.WinEvent)
.withPlayerWon(winner == EntityType.P1 ? 1 : 2) .withPlayerWon(winner == EntityType.P1 ? 1 : 2)
.buildGameEvent()); .buildGameEvent());

View File

@ -99,7 +99,8 @@ class GameStateManager {
* @return The optionally resulting {@link Event Events} * @return The optionally resulting {@link Event Events}
*/ */
public ArrayList<Event> checkPostPhase() { public ArrayList<Event> checkPostPhase() {
ArrayList<Event> result = GameLogic.checkTurnEnd(state); GameState snapshot = state.snapshot();
ArrayList<Event> result = GameLogic.checkTurnEnd(snapshot);
applyEvents(result); applyEvents(result);
return result; return result;
} }
@ -111,10 +112,14 @@ class GameStateManager {
* @return The resulting {@link Event Events} * @return The resulting {@link Event Events}
*/ */
public ArrayList<Event> startGame(ArrayList<Integer> selectedCharacters1, ArrayList<Integer> selectedCharacters2) { public ArrayList<Event> startGame(ArrayList<Integer> selectedCharacters1, ArrayList<Integer> selectedCharacters2) {
ArrayList<Event> result = GameLogic.startGame(state, selectedCharacters1, selectedCharacters2); GameState snapshot = state.snapshot();
ArrayList<Event> result = GameLogic.startGame(snapshot, selectedCharacters1, selectedCharacters2);
applyEvents(result); applyEvents(result);
ArrayList<Event> result2 = GameLogic.startRound(state); snapshot = state.snapshot();
ArrayList<Event> result2 = GameLogic.startRound(snapshot);
applyEvents(result2); applyEvents(result2);
result.addAll(result2); result.addAll(result2);
@ -127,7 +132,8 @@ class GameStateManager {
* @return The resulting {@link Event Events} * @return The resulting {@link Event Events}
*/ */
public ArrayList<Event> endTurn() { public ArrayList<Event> endTurn() {
ArrayList<Event> result = GameLogic.endTurn(state); GameState snapshot = state.snapshot();
ArrayList<Event> result = GameLogic.endTurn(snapshot);
applyEvents(result); applyEvents(result);
return result; return result;
} }