From 5474787641a7f19cc5fc471327cf73549d7d7c06 Mon Sep 17 00:00:00 2001 From: Oleg Agafonov Date: Fri, 21 Apr 2023 10:08:33 +0400 Subject: [PATCH] Refactor: added miss Ability source in some choose methods --- .../src/mage/player/ai/ComputerPlayer6.java | 4 ++-- .../main/java/mage/player/ai/ComputerPlayer.java | 6 +++--- .../src/mage/player/ai/SimulatedPlayerMCTS.java | 6 +++--- .../src/mage/player/human/HumanPlayer.java | 14 +++++++------- .../test/java/org/mage/test/player/TestPlayer.java | 8 +++----- .../test/java/org/mage/test/stub/PlayerStub.java | 2 +- .../src/main/java/mage/filter/FilterPermanent.java | 2 +- Mage/src/main/java/mage/players/Player.java | 6 +++++- Mage/src/main/java/mage/players/StubPlayer.java | 2 +- Mage/src/main/java/mage/target/TargetCard.java | 4 ++-- .../target/common/TargetCardInYourGraveyard.java | 8 ++++++-- 11 files changed, 34 insertions(+), 28 deletions(-) diff --git a/Mage.Server.Plugins/Mage.Player.AI.MA/src/mage/player/ai/ComputerPlayer6.java b/Mage.Server.Plugins/Mage.Player.AI.MA/src/mage/player/ai/ComputerPlayer6.java index 42a00000c2..d6fe5755a5 100644 --- a/Mage.Server.Plugins/Mage.Player.AI.MA/src/mage/player/ai/ComputerPlayer6.java +++ b/Mage.Server.Plugins/Mage.Player.AI.MA/src/mage/player/ai/ComputerPlayer6.java @@ -762,9 +762,9 @@ public class ComputerPlayer6 extends ComputerPlayer /*implements Player*/ { } @Override - public boolean choose(Outcome outcome, Cards cards, TargetCard target, Game game) { + public boolean choose(Outcome outcome, Cards cards, TargetCard target, Ability source, Game game) { if (targets.isEmpty()) { - return super.choose(outcome, cards, target, game); + return super.choose(outcome, cards, target, source, game); } if (!target.doneChoosing()) { for (UUID targetId : targets) { diff --git a/Mage.Server.Plugins/Mage.Player.AI/src/main/java/mage/player/ai/ComputerPlayer.java b/Mage.Server.Plugins/Mage.Player.AI/src/main/java/mage/player/ai/ComputerPlayer.java index 4f601ad2ff..00fa753048 100644 --- a/Mage.Server.Plugins/Mage.Player.AI/src/main/java/mage/player/ai/ComputerPlayer.java +++ b/Mage.Server.Plugins/Mage.Player.AI/src/main/java/mage/player/ai/ComputerPlayer.java @@ -2030,7 +2030,7 @@ public class ComputerPlayer extends PlayerImpl implements Player { } @Override - public boolean choose(Outcome outcome, Cards cards, TargetCard target, Game game) { + public boolean choose(Outcome outcome, Cards cards, TargetCard target, Ability source, Game game) { log.debug("choose 2"); if (cards == null || cards.isEmpty()) { return true; @@ -2043,9 +2043,9 @@ public class ComputerPlayer extends PlayerImpl implements Player { abilityControllerId = target.getAbilityController(); } - List cardChoices = new ArrayList<>(cards.getCards(target.getFilter(), game)); + List cardChoices = new ArrayList<>(cards.getCards(target.getFilter(), abilityControllerId, source, game)); while (!target.doneChoosing()) { - Card card = pickTarget(abilityControllerId, cardChoices, outcome, target, null, game); + Card card = pickTarget(abilityControllerId, cardChoices, outcome, target, source, game); if (card != null) { target.add(card.getId(), game); cardChoices.remove(card); diff --git a/Mage.Server.Plugins/Mage.Player.AIMCTS/src/mage/player/ai/SimulatedPlayerMCTS.java b/Mage.Server.Plugins/Mage.Player.AIMCTS/src/mage/player/ai/SimulatedPlayerMCTS.java index 4eb502940c..d6edd5d7f8 100644 --- a/Mage.Server.Plugins/Mage.Player.AIMCTS/src/mage/player/ai/SimulatedPlayerMCTS.java +++ b/Mage.Server.Plugins/Mage.Player.AIMCTS/src/mage/player/ai/SimulatedPlayerMCTS.java @@ -261,12 +261,12 @@ public class SimulatedPlayerMCTS extends MCTSPlayer { } @Override - public boolean choose(Outcome outcome, Cards cards, TargetCard target, Game game) { + public boolean choose(Outcome outcome, Cards cards, TargetCard target, Ability source, Game game) { if (this.isHuman()) { if (cards.isEmpty()) { return false; } - Set possibleTargets = target.possibleTargets(playerId, cards, game); + Set possibleTargets = target.possibleTargets(playerId, cards, source, game); if (possibleTargets.isEmpty()) { return false; } @@ -279,7 +279,7 @@ public class SimulatedPlayerMCTS extends MCTSPlayer { target.add(targetId, game); return true; } - return super.choose(outcome, cards, target, game); + return super.choose(outcome, cards, target, source, game); } @Override diff --git a/Mage.Server.Plugins/Mage.Player.Human/src/mage/player/human/HumanPlayer.java b/Mage.Server.Plugins/Mage.Player.Human/src/mage/player/human/HumanPlayer.java index 856f3bcd28..61ee63d2e3 100644 --- a/Mage.Server.Plugins/Mage.Player.Human/src/mage/player/human/HumanPlayer.java +++ b/Mage.Server.Plugins/Mage.Player.Human/src/mage/player/human/HumanPlayer.java @@ -700,7 +700,7 @@ public class HumanPlayer extends PlayerImpl { } @Override - public boolean choose(Outcome outcome, Cards cards, TargetCard target, Game game) { + public boolean choose(Outcome outcome, Cards cards, TargetCard target, Ability source, Game game) { if (gameInCheckPlayableState(game)) { return true; } @@ -719,8 +719,8 @@ public class HumanPlayer extends PlayerImpl { } while (canRespond()) { - boolean required = target.isRequired(null, game); - int count = cards.count(target.getFilter(), abilityControllerId, game); + boolean required = target.isRequired(source != null ? source.getSourceId() : null, game); + int count = cards.count(target.getFilter(), abilityControllerId, source, game); if (count == 0 || target.getTargets().size() >= target.getNumberOfTargets()) { required = false; @@ -729,7 +729,7 @@ public class HumanPlayer extends PlayerImpl { List chosenTargets = target.getTargets(); List possibleTargets = new ArrayList<>(); for (UUID cardId : cards) { - if (target.canTarget(abilityControllerId, cardId, null, cards, game)) { + if (target.canTarget(abilityControllerId, cardId, source, cards, game)) { possibleTargets.add(cardId); } } @@ -738,7 +738,7 @@ public class HumanPlayer extends PlayerImpl { required = false; } - UUID responseId = target.tryToAutoChoose(abilityControllerId, null, game, possibleTargets); + UUID responseId = target.tryToAutoChoose(abilityControllerId, source, game, possibleTargets); if (responseId == null) { Map options = getOptions(target, null); @@ -761,7 +761,7 @@ public class HumanPlayer extends PlayerImpl { if (target.getTargets().contains(responseId)) { // if already included remove it with target.remove(responseId); } else { - if (target.canTarget(abilityControllerId, responseId, null, cards, game)) { + if (target.canTarget(abilityControllerId, responseId, source, cards, game)) { target.add(responseId, game); if (target.doneChoosing()) { return true; @@ -802,7 +802,7 @@ public class HumanPlayer extends PlayerImpl { while (canRespond()) { boolean required = target.isRequiredExplicitlySet() ? target.isRequired() : target.isRequired(source); - int count = cards.count(target.getFilter(), abilityControllerId, game); + int count = cards.count(target.getFilter(), abilityControllerId, source, game); if (count == 0 || target.getTargets().size() >= target.getNumberOfTargets()) { required = false; diff --git a/Mage.Tests/src/test/java/org/mage/test/player/TestPlayer.java b/Mage.Tests/src/test/java/org/mage/test/player/TestPlayer.java index 77f58cd7e6..0e78e326ac 100644 --- a/Mage.Tests/src/test/java/org/mage/test/player/TestPlayer.java +++ b/Mage.Tests/src/test/java/org/mage/test/player/TestPlayer.java @@ -3965,9 +3965,7 @@ public class TestPlayer implements Player { } @Override - public boolean choose(Outcome outcome, Cards cards, - TargetCard target, Game game - ) { + public boolean choose(Outcome outcome, Cards cards, TargetCard target, Ability source, Game game) { assertAliasSupportInChoices(false); if (!choices.isEmpty()) { @@ -3995,7 +3993,7 @@ public class TestPlayer implements Player { continue; } if (hasObjectTargetNameOrAlias(card, targetName)) { - if (target.isNotTarget() || target.canTarget(card.getId(), game)) { + if (target.isNotTarget() || target.canTarget(card.getId(), source, game)) { target.add(card.getId(), game); targetFound = true; break; @@ -4013,7 +4011,7 @@ public class TestPlayer implements Player { } this.chooseStrictModeFailed("choice", game, getInfo(target)); - return computerPlayer.choose(outcome, cards, target, game); + return computerPlayer.choose(outcome, cards, target, source, game); } @Override diff --git a/Mage.Tests/src/test/java/org/mage/test/stub/PlayerStub.java b/Mage.Tests/src/test/java/org/mage/test/stub/PlayerStub.java index 56cf85952a..11555aa7cb 100644 --- a/Mage.Tests/src/test/java/org/mage/test/stub/PlayerStub.java +++ b/Mage.Tests/src/test/java/org/mage/test/stub/PlayerStub.java @@ -835,7 +835,7 @@ public class PlayerStub implements Player { } @Override - public boolean choose(Outcome outcome, Cards cards, TargetCard target, Game game) { + public boolean choose(Outcome outcome, Cards cards, TargetCard target, Ability source, Game game) { return false; } diff --git a/Mage/src/main/java/mage/filter/FilterPermanent.java b/Mage/src/main/java/mage/filter/FilterPermanent.java index 3c0d8dc30e..4442349b9c 100644 --- a/Mage/src/main/java/mage/filter/FilterPermanent.java +++ b/Mage/src/main/java/mage/filter/FilterPermanent.java @@ -54,7 +54,7 @@ public class FilterPermanent extends FilterObject implements FilterIn if (!this.match(permanent, game) || !permanent.isPhasedIn()) { return false; } - ObjectSourcePlayer osp = new ObjectSourcePlayer(permanent, playerId, source); + ObjectSourcePlayer osp = new ObjectSourcePlayer<>(permanent, playerId, source); return extraPredicates.stream().allMatch(p -> p.apply(osp, game)); } diff --git a/Mage/src/main/java/mage/players/Player.java b/Mage/src/main/java/mage/players/Player.java index 093f11c516..e6a0ee02c5 100644 --- a/Mage/src/main/java/mage/players/Player.java +++ b/Mage/src/main/java/mage/players/Player.java @@ -625,7 +625,11 @@ public interface Player extends MageItem, Copyable { boolean choose(Outcome outcome, Target target, Ability source, Game game, Map options); - boolean choose(Outcome outcome, Cards cards, TargetCard target, Game game); // TODO: remove to use choose with "Ability source" + // TODO: remove to use choose with "Ability source" + default boolean choose(Outcome outcome, Cards cards, TargetCard target, Game game) { + return choose(outcome, cards, target, null, game); + } + boolean choose(Outcome outcome, Cards cards, TargetCard target, Ability source, Game game); boolean chooseTarget(Outcome outcome, Target target, Ability source, Game game); diff --git a/Mage/src/main/java/mage/players/StubPlayer.java b/Mage/src/main/java/mage/players/StubPlayer.java index 493ad985a2..4018c5c7ad 100644 --- a/Mage/src/main/java/mage/players/StubPlayer.java +++ b/Mage/src/main/java/mage/players/StubPlayer.java @@ -50,7 +50,7 @@ public class StubPlayer extends PlayerImpl implements Player { } @Override - public boolean choose(Outcome outcome, Cards cards, TargetCard target, Game game) { + public boolean choose(Outcome outcome, Cards cards, TargetCard target, Ability source, Game game) { cards.getCards(game).stream().map(MageItem::getId).forEach(cardId -> target.add(cardId, game)); return true; } diff --git a/Mage/src/main/java/mage/target/TargetCard.java b/Mage/src/main/java/mage/target/TargetCard.java index 752a1572aa..cb6f8dd7f7 100644 --- a/Mage/src/main/java/mage/target/TargetCard.java +++ b/Mage/src/main/java/mage/target/TargetCard.java @@ -211,8 +211,8 @@ public class TargetCard extends TargetObject { return possibleTargets; } - public Set possibleTargets(UUID sourceControllerId, Cards cards, Game game) { - return cards.getCards(filter, game).stream().map(MageItem::getId).collect(Collectors.toSet()); + public Set possibleTargets(UUID sourceControllerId, Cards cards, Ability source, Game game) { + return cards.getCards(filter, sourceControllerId, source, game).stream().map(MageItem::getId).collect(Collectors.toSet()); } @Override diff --git a/Mage/src/main/java/mage/target/common/TargetCardInYourGraveyard.java b/Mage/src/main/java/mage/target/common/TargetCardInYourGraveyard.java index 382decab56..5b22a4abf4 100644 --- a/Mage/src/main/java/mage/target/common/TargetCardInYourGraveyard.java +++ b/Mage/src/main/java/mage/target/common/TargetCardInYourGraveyard.java @@ -88,10 +88,14 @@ public class TargetCardInYourGraveyard extends TargetCard { } @Override - public Set possibleTargets(UUID sourceControllerId, Cards cards, Game game) { + public Set possibleTargets(UUID sourceControllerId, Cards cards, Ability source, Game game) { Set possibleTargets = new HashSet<>(); Player player = game.getPlayer(sourceControllerId); - for (Card card : cards.getCards(filter, game)) { + if (player == null) { + return possibleTargets; + } + + for (Card card : cards.getCards(filter, sourceControllerId, source, game)) { if (player.getGraveyard().getCards(game).contains(card)) { possibleTargets.add(card.getId()); }