From bb82c6fa02d7a32d8307aed7a8540c49c18f10b9 Mon Sep 17 00:00:00 2001 From: Agetian Date: Sat, 2 Sep 2017 14:19:34 +0000 Subject: [PATCH] - Further work on trick/lure attacks with held pump spell. --- .../src/main/java/forge/ai/AiCardMemory.java | 6 +++--- .../src/main/java/forge/ai/AiController.java | 9 +++++++-- .../src/main/java/forge/ai/ComputerUtilCard.java | 16 ++++++++++++++-- .../src/main/java/forge/ai/ComputerUtilMana.java | 4 ++-- forge-gui/res/ai/Cautious.ai | 3 +++ forge-gui/res/ai/Default.ai | 5 ++++- forge-gui/res/ai/Experimental.ai | 5 ++++- forge-gui/res/ai/Reckless.ai | 5 ++++- 8 files changed, 41 insertions(+), 12 deletions(-) diff --git a/forge-ai/src/main/java/forge/ai/AiCardMemory.java b/forge-ai/src/main/java/forge/ai/AiCardMemory.java index adfe4afc975..6355d50e753 100644 --- a/forge-ai/src/main/java/forge/ai/AiCardMemory.java +++ b/forge-ai/src/main/java/forge/ai/AiCardMemory.java @@ -69,7 +69,7 @@ public class AiCardMemory { MANDATORY_ATTACKERS, TRICK_ATTACKERS, HELD_MANA_SOURCES_FOR_MAIN2, - HELD_MANA_SOURCES_FOR_COMBAT, + HELD_MANA_SOURCES_FOR_DECLBLK, ATTACHED_THIS_TURN, ANIMATED_THIS_TURN, BOUNCED_THIS_TURN, @@ -85,7 +85,7 @@ public class AiCardMemory { return memTrickAttackers; case HELD_MANA_SOURCES_FOR_MAIN2: return memHeldManaSources; - case HELD_MANA_SOURCES_FOR_COMBAT: + case HELD_MANA_SOURCES_FOR_DECLBLK: return memHeldManaSourcesForCombat; case ATTACHED_THIS_TURN: return memAttachedThisTurn; @@ -266,7 +266,7 @@ public class AiCardMemory { clearMemorySet(MemorySet.MANDATORY_ATTACKERS); clearMemorySet(MemorySet.TRICK_ATTACKERS); clearMemorySet(MemorySet.HELD_MANA_SOURCES_FOR_MAIN2); - clearMemorySet(MemorySet.HELD_MANA_SOURCES_FOR_COMBAT); + clearMemorySet(MemorySet.HELD_MANA_SOURCES_FOR_DECLBLK); clearMemorySet(MemorySet.ATTACHED_THIS_TURN); clearMemorySet(MemorySet.ANIMATED_THIS_TURN); clearMemorySet(MemorySet.BOUNCED_THIS_TURN); diff --git a/forge-ai/src/main/java/forge/ai/AiController.java b/forge-ai/src/main/java/forge/ai/AiController.java index 84743e1b023..70a730ab0a1 100644 --- a/forge-ai/src/main/java/forge/ai/AiController.java +++ b/forge-ai/src/main/java/forge/ai/AiController.java @@ -610,13 +610,18 @@ public class AiController { ManaCostBeingPaid cost = ComputerUtilMana.calculateManaCost(sa, true, 0); CardCollection manaSources = ComputerUtilMana.getManaSourcesToPayCost(cost, sa, player); - AiCardMemory.MemorySet memSet = AiCardMemory.MemorySet.HELD_MANA_SOURCES_FOR_MAIN2; + AiCardMemory.MemorySet memSet; switch (phaseType) { + case MAIN2: + memSet = AiCardMemory.MemorySet.HELD_MANA_SOURCES_FOR_MAIN2; + break; case COMBAT_DECLARE_BLOCKERS: - memSet = AiCardMemory.MemorySet.HELD_MANA_SOURCES_FOR_COMBAT; + memSet = AiCardMemory.MemorySet.HELD_MANA_SOURCES_FOR_DECLBLK; break; default: + System.out.println("Warning: unsupported mana reservation phase specified for reserveManaSources: " + + phaseType.name() + ", reserving until Main 2 instead. Consider adding support for the phase if needed."); memSet = AiCardMemory.MemorySet.HELD_MANA_SOURCES_FOR_MAIN2; break; } diff --git a/forge-ai/src/main/java/forge/ai/ComputerUtilCard.java b/forge-ai/src/main/java/forge/ai/ComputerUtilCard.java index e144fb7455e..f495fafa530 100644 --- a/forge-ai/src/main/java/forge/ai/ComputerUtilCard.java +++ b/forge-ai/src/main/java/forge/ai/ComputerUtilCard.java @@ -1167,10 +1167,12 @@ public class ComputerUtilCard { boolean combatTrick = false; boolean holdCombatTricks = false; + int chanceToHoldCombatTricks = -1; if (ai.getController().isAI()) { AiController aic = ((PlayerControllerAi)ai.getController()).getAi(); holdCombatTricks = aic.getBooleanProperty(AiProps.TRY_TO_HOLD_COMBAT_TRICKS_UNTIL_BLOCK); + chanceToHoldCombatTricks = aic.getIntProperty(AiProps.CHANCE_TO_HOLD_COMBAT_TRICKS_UNTIL_BLOCK); } if (!c.canBeTargetedBy(sa)) { @@ -1402,16 +1404,26 @@ public class ComputerUtilCard { } } - boolean isHeldCombatTrick = combatTrick && holdCombatTricks && MyRandom.getRandom().nextFloat() < chance; + boolean wantToHoldTrick = holdCombatTricks; + if (chanceToHoldCombatTricks >= 0) { + // Obey the chance specified in the AI profile for holding combat tricks + wantToHoldTrick &= MyRandom.percentTrue(chanceToHoldCombatTricks); + } else { + // Use standard considerations dependent solely on the buff chance determined above + wantToHoldTrick &= MyRandom.getRandom().nextFloat() < chance; + } + + boolean isHeldCombatTrick = combatTrick && wantToHoldTrick; if (isHeldCombatTrick) { if (AiCardMemory.isMemorySetEmpty(ai, AiCardMemory.MemorySet.TRICK_ATTACKERS)) { // Attempt to hold combat tricks until blockers are declared, and try to lure the opponent into blocking // (The AI will only do it for one attacker at the moment, otherwise it risks running his attackers into // an army of opposing blockers with only one combat trick in hand) - // TODO: somehow ensure that the AI doesn't tap out before it has a chance to buff the attacker AiCardMemory.rememberCard(ai, c, AiCardMemory.MemorySet.MANDATORY_ATTACKERS); AiCardMemory.rememberCard(ai, c, AiCardMemory.MemorySet.TRICK_ATTACKERS); + // Reserve the mana until Declare Blockers such that the AI doesn't tap out before having a chance to use + // the combat trick if (ai.getController().isAI()) { ((PlayerControllerAi) ai.getController()).getAi().reserveManaSources(sa, PhaseType.COMBAT_DECLARE_BLOCKERS); } diff --git a/forge-ai/src/main/java/forge/ai/ComputerUtilMana.java b/forge-ai/src/main/java/forge/ai/ComputerUtilMana.java index 69e798876ce..c2dee3bc473 100644 --- a/forge-ai/src/main/java/forge/ai/ComputerUtilMana.java +++ b/forge-ai/src/main/java/forge/ai/ComputerUtilMana.java @@ -846,10 +846,10 @@ public class ComputerUtilMana { // For combat tricks, always obey mana reservation if (curPhase == PhaseType.COMBAT_DECLARE_BLOCKERS || curPhase == PhaseType.CLEANUP) { - AiCardMemory.clearMemorySet(ai, AiCardMemory.MemorySet.HELD_MANA_SOURCES_FOR_COMBAT); + AiCardMemory.clearMemorySet(ai, AiCardMemory.MemorySet.HELD_MANA_SOURCES_FOR_DECLBLK); } else { - if (AiCardMemory.isRememberedCard(ai, sourceCard, AiCardMemory.MemorySet.HELD_MANA_SOURCES_FOR_COMBAT)) { + if (AiCardMemory.isRememberedCard(ai, sourceCard, AiCardMemory.MemorySet.HELD_MANA_SOURCES_FOR_DECLBLK)) { // This mana source is held elsewhere for a combat trick. return true; } diff --git a/forge-gui/res/ai/Cautious.ai b/forge-gui/res/ai/Cautious.ai index 109f6270643..ff40cfad5ab 100644 --- a/forge-gui/res/ai/Cautious.ai +++ b/forge-gui/res/ai/Cautious.ai @@ -17,6 +17,9 @@ USE_BERSERK_AGGRESSIVELY=false # Try to hold combat tricks until blockers are declared in an attempt to trick the opponent into blocking a weak creature # and dying to it (currently has some limitations, the AI will only try to do it to one creature) TRY_TO_HOLD_COMBAT_TRICKS_UNTIL_BLOCK=false +# Chance to hold combat tricks until blockers are declared. If -1 is specified, this chance is not used, and the standard +# evaluation for offensive pump buff is used instead. +CHANCE_TO_HOLD_COMBAT_TRICKS_UNTIL_BLOCK=-1 # Trade blocking preferences (enabling these will make the AI trade more aggressively when considering blocks, # but only with creatures that are worse in abilities and have lower or the same power as the attacker). Note diff --git a/forge-gui/res/ai/Default.ai b/forge-gui/res/ai/Default.ai index 7ba0ebd9e71..7712ef4e7e5 100644 --- a/forge-gui/res/ai/Default.ai +++ b/forge-gui/res/ai/Default.ai @@ -15,8 +15,11 @@ ATTACK_INTO_TRADE_WHEN_TAPPED_OUT=false # When enabled, the AI will use Berserk on offense to try to severely damage the opponent at the expense of the creature USE_BERSERK_AGGRESSIVELY=false # Try to hold combat tricks until blockers are declared in an attempt to trick the opponent into blocking a weak creature -# and dying to it (currently has some limitations, the AI will only try to do it to one creature) +# and dying to it (currently has some limitations, the AI will only try to do it to one creature per turn) TRY_TO_HOLD_COMBAT_TRICKS_UNTIL_BLOCK=false +# Chance to hold combat tricks until blockers are declared. If -1 is specified, this chance is not used, and the standard +# evaluation for offensive pump buff is used instead. +CHANCE_TO_HOLD_COMBAT_TRICKS_UNTIL_BLOCK=-1 # Trade blocking preferences (enabling these will make the AI trade more aggressively when considering blocks, # but only with creatures that are worse in abilities and have lower or the same power as the attacker). Note diff --git a/forge-gui/res/ai/Experimental.ai b/forge-gui/res/ai/Experimental.ai index 94feed8c336..c61be9b9b3f 100644 --- a/forge-gui/res/ai/Experimental.ai +++ b/forge-gui/res/ai/Experimental.ai @@ -15,8 +15,11 @@ ATTACK_INTO_TRADE_WHEN_TAPPED_OUT=false # When enabled, the AI will use Berserk on offense to try to severely damage the opponent at the expense of the creature USE_BERSERK_AGGRESSIVELY=true # Try to hold combat tricks until blockers are declared in an attempt to trick the opponent into blocking a weak creature -# and dying to it (currently has some limitations, the AI will only try to do it to one creature) +# and dying to it (currently has some limitations, the AI will only try to do it with one creature per turn) TRY_TO_HOLD_COMBAT_TRICKS_UNTIL_BLOCK=true +# Chance to hold combat tricks until blockers are declared. If -1 is specified, this chance is not used, and the standard +# evaluation for offensive pump buff is used instead. +CHANCE_TO_HOLD_COMBAT_TRICKS_UNTIL_BLOCK=30 # Trade blocking preferences (enabling these will make the AI trade more aggressively when considering blocks, # but only with creatures that are worse in abilities and have lower or the same power as the attacker). Note diff --git a/forge-gui/res/ai/Reckless.ai b/forge-gui/res/ai/Reckless.ai index 49d2bfb24b7..1029c5c952f 100644 --- a/forge-gui/res/ai/Reckless.ai +++ b/forge-gui/res/ai/Reckless.ai @@ -15,8 +15,11 @@ ATTACK_INTO_TRADE_WHEN_TAPPED_OUT=true # When enabled, the AI will use Berserk on offense to try to severely damage the opponent at the expense of the creature USE_BERSERK_AGGRESSIVELY=true # Try to hold combat tricks until blockers are declared in an attempt to trick the opponent into blocking a weak creature -# and dying to it (currently has some limitations, the AI will only try to do it to one creature) +# and dying to it (currently has some limitations, the AI will only try to do it to one creature per turn) TRY_TO_HOLD_COMBAT_TRICKS_UNTIL_BLOCK=false +# Chance to hold combat tricks until blockers are declared. If -1 is specified, this chance is not used, and the standard +# evaluation for offensive pump buff is used instead. +CHANCE_TO_HOLD_COMBAT_TRICKS_UNTIL_BLOCK=-1 # Trade blocking preferences (enabling these will make the AI trade more aggressively when considering blocks, # but only with creatures that are worse in abilities and have lower or the same power as the attacker). Note