server: improved server stability (#11285) and reworked triggers/playable logic (#8426):

* game: now all playable calculations done in game simulation, outside real game (no more freeze and ruined games by wrong Nyxbloom Ancient and other cards with wrong replacement dialog);
* game: fixed multiple problems with triggers (wrong order, duplicated calls or "too many mana" bugs, see #8426, #12087);
* tests: added data integrity checks for game's triggers (3 enabled and 3 disabled due current game engine logic);
This commit is contained in:
Oleg Agafonov 2024-04-16 23:10:04 +04:00
parent f68e435fc4
commit e8e2f23284
23 changed files with 362 additions and 120 deletions

View file

@ -414,7 +414,7 @@ public class ComputerPlayer6 extends ComputerPlayer {
Target target = effect.getTarget();
if (!target.doneChoosing()) {
for (UUID targetId : target.possibleTargets(stackObject.getControllerId(), stackObject.getStackAbility(), game)) {
Game sim = game.copy();
Game sim = game.createSimulationForAI();
StackAbility newAbility = (StackAbility) stackObject.copy();
SearchEffect newEffect = getSearchEffect(newAbility);
newEffect.getTarget().addTarget(targetId, newAbility, sim);
@ -514,8 +514,7 @@ public class ComputerPlayer6 extends ComputerPlayer {
logger.info("Sim Prio [" + depth + "] -- interrupted");
break;
}
Game sim = game.copy();
sim.setSimulation(true);
Game sim = game.createSimulationForAI();
if (!(action instanceof StaticAbility) //for MorphAbility, etc
&& sim.getPlayer(currentPlayer.getId()).activateAbility((ActivatedAbility) action.copy(), sim)) {
sim.applyEffects();
@ -1067,8 +1066,7 @@ public class ComputerPlayer6 extends ComputerPlayer {
* @return a new game object with simulated players
*/
protected Game createSimulation(Game game) {
Game sim = game.copy();
sim.setSimulation(true);
Game sim = game.createSimulationForAI();
for (Player oldPlayer : sim.getState().getPlayers().values()) {
// replace original player by simulated player and find result (execute/resolve current action)
Player origPlayer = game.getState().getPlayers().get(oldPlayer.getId()).copy();

View file

@ -65,8 +65,7 @@ public final class SimulatedPlayer2 extends ComputerPlayer {
public List<Ability> simulatePriority(Game game) {
allActions = new ConcurrentLinkedQueue<>();
Game sim = game.copy();
sim.setSimulation(true);
Game sim = game.createSimulationForAI();
forced = false;
simulateOptions(sim);
@ -170,17 +169,6 @@ public final class SimulatedPlayer2 extends ComputerPlayer {
}
// protected void simulateAction(Game game, SimulatedAction previousActions, Ability action) {
// List<Ability> actions = new ArrayList<Ability>(previousActions.getAbilities());
// actions.add(action);
// Game sim = game.copy();
// if (sim.getPlayer(playerId).activateAbility((ActivatedAbility) action.copy(), sim)) {
// sim.applyEffects();
// sim.getPlayers().resetPassed();
// allActions.add(new SimulatedAction(sim, actions));
// }
// }
/**
* if suggested abilities exist, return only those from playables
*
@ -322,7 +310,7 @@ public final class SimulatedPlayer2 extends ComputerPlayer {
int powerElements = (int) Math.pow(2, attackersList.size());
StringBuilder binary = new StringBuilder();
for (int i = powerElements - 1; i >= 0; i--) {
Game sim = game.copy();
Game sim = game.createSimulationForAI();
binary.setLength(0);
binary.append(Integer.toBinaryString(i));
while (binary.length() < attackersList.size()) {
@ -360,7 +348,7 @@ public final class SimulatedPlayer2 extends ComputerPlayer {
}
//add a node with no blockers
Game sim = game.copy();
Game sim = game.createSimulationForAI();
engagements.put(sim.getCombat().getValue().hashCode(), sim.getCombat());
sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_BLOCKERS, playerId, playerId));
@ -381,7 +369,7 @@ public final class SimulatedPlayer2 extends ComputerPlayer {
List<Permanent> remaining = remove(blockers, blocker);
for (int i = 0; i < numGroups; i++) {
if (game.getCombat().getGroups().get(i).canBlock(blocker, game)) {
Game sim = game.copy();
Game sim = game.createSimulationForAI();
sim.getCombat().getGroups().get(i).addBlocker(blocker.getId(), playerId, sim);
if (engagements.put(sim.getCombat().getValue().hashCode(), sim.getCombat()) != null) {
logger.debug("simulating -- found redundant block combination");
@ -419,7 +407,7 @@ public final class SimulatedPlayer2 extends ComputerPlayer {
}
protected void addAbilityNode(SimulationNode2 parent, Ability ability, int depth, Game game) {
Game sim = game.copy();
Game sim = game.createSimulationForAI();
sim.getStack().push(new StackAbility(ability, playerId));
if (ability.activate(sim, false) && ability.isUsesStack()) {
game.fireEvent(new GameEvent(GameEvent.EventType.TRIGGERED_ABILITY, ability.getId(), ability, ability.getControllerId()));

View file

@ -202,7 +202,7 @@ public final class CombatUtil {
* @deprecated TODO: unused, can be deleted?
*/
public static SurviveInfo willItSurvive(Game game, UUID attackingPlayerId, UUID defendingPlayerId, Permanent attacker, Permanent blocker) {
Game sim = game.copy();
Game sim = game.createSimulationForAI();
// TODO: bugged, miss combat.clear code (possible bugs - wrong blocker declare by AI on multiple options?)
Combat combat = sim.getCombat();
@ -307,7 +307,7 @@ public final class CombatUtil {
public static SurviveInfo willItSurvive2(Game game, UUID attackingPlayerId, UUID defendingPlayerId, Permanent attacker, Permanent blocker) {
Game sim = game.copy();
Game sim = game.createSimulationForAI();
// TODO: bugged, miss combat.clear code (possible bugs - wrong blocker declare by AI on multiple options?)
Combat combat = sim.getCombat();

View file

@ -274,7 +274,7 @@ public class ComputerPlayerMCTS extends ComputerPlayer {
* @return a new game object with simulated players
*/
protected Game createMCTSGame(Game game) {
Game mcts = game.copy();
Game mcts = game.createSimulationForAI();
for (Player copyPlayer : mcts.getState().getPlayers().values()) {
Player origPlayer = game.getState().getPlayers().get(copyPlayer.getId());
@ -295,7 +295,6 @@ public class ComputerPlayerMCTS extends ComputerPlayer {
}
mcts.getState().getPlayers().put(copyPlayer.getId(), newPlayer);
}
mcts.setSimulation(true);
mcts.resume();
return mcts;
}

View file

@ -245,7 +245,7 @@ public class MCTSNode {
* @return a new game object with simulated players
*/
protected Game createSimulation(Game game, UUID playerId) {
Game sim = game.copy();
Game sim = game.createSimulationForAI();
for (Player oldPlayer: sim.getState().getPlayers().values()) {
Player origPlayer = game.getState().getPlayers().get(oldPlayer.getId()).copy();
@ -254,7 +254,6 @@ public class MCTSNode {
sim.getState().getPlayers().put(oldPlayer.getId(), newPlayer);
}
randomizePlayers(sim, playerId);
sim.setSimulation(true);
return sim;
}

View file

@ -18,7 +18,7 @@ public class PriorityNextAction implements MCTSNodeNextAction{
else
abilities = MCTSNode.getPlayables(player, fullStateValue, game);
for (Ability ability: abilities) {
Game sim = game.copy();
Game sim = game.createSimulationForAI();
MCTSPlayer simPlayer = (MCTSPlayer) sim.getPlayer(player.getId());
simPlayer.activateAbility((ActivatedAbility)ability, sim);
sim.resume();

View file

@ -19,7 +19,7 @@ public class SelectAttackersNextAction implements MCTSNodeNextAction{
attacks = getAttacks(player, fullStateValue, game);
UUID defenderId = game.getOpponents(player.getId()).iterator().next();
for (List<UUID> attack: attacks) {
Game sim = game.copy();
Game sim = game.createSimulationForAI();
MCTSPlayer simPlayer = (MCTSPlayer) sim.getPlayer(player.getId());
for (UUID attackerId: attack) {
simPlayer.declareAttacker(attackerId, defenderId, sim, false);

View file

@ -19,7 +19,7 @@ public class SelectBlockersNextAction implements MCTSNodeNextAction{
else
blocks = getBlocks(player, fullStateValue, game);
for (List<List<UUID>> block : blocks) {
Game sim = game.copy();
Game sim = game.createSimulationForAI();
MCTSPlayer simPlayer = (MCTSPlayer) sim.getPlayer(player.getId());
List<CombatGroup> groups = sim.getCombat().getGroups();
for (int i = 0; i < groups.size(); i++) {