Refactor: added miss Ability source in some choose methods

This commit is contained in:
Oleg Agafonov 2023-04-21 10:08:33 +04:00
parent 09a4294466
commit 5474787641
11 changed files with 34 additions and 28 deletions

View file

@ -762,9 +762,9 @@ public class ComputerPlayer6 extends ComputerPlayer /*implements Player*/ {
} }
@Override @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()) { if (targets.isEmpty()) {
return super.choose(outcome, cards, target, game); return super.choose(outcome, cards, target, source, game);
} }
if (!target.doneChoosing()) { if (!target.doneChoosing()) {
for (UUID targetId : targets) { for (UUID targetId : targets) {

View file

@ -2030,7 +2030,7 @@ public class ComputerPlayer extends PlayerImpl implements Player {
} }
@Override @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"); log.debug("choose 2");
if (cards == null || cards.isEmpty()) { if (cards == null || cards.isEmpty()) {
return true; return true;
@ -2043,9 +2043,9 @@ public class ComputerPlayer extends PlayerImpl implements Player {
abilityControllerId = target.getAbilityController(); abilityControllerId = target.getAbilityController();
} }
List<Card> cardChoices = new ArrayList<>(cards.getCards(target.getFilter(), game)); List<Card> cardChoices = new ArrayList<>(cards.getCards(target.getFilter(), abilityControllerId, source, game));
while (!target.doneChoosing()) { while (!target.doneChoosing()) {
Card card = pickTarget(abilityControllerId, cardChoices, outcome, target, null, game); Card card = pickTarget(abilityControllerId, cardChoices, outcome, target, source, game);
if (card != null) { if (card != null) {
target.add(card.getId(), game); target.add(card.getId(), game);
cardChoices.remove(card); cardChoices.remove(card);

View file

@ -261,12 +261,12 @@ public class SimulatedPlayerMCTS extends MCTSPlayer {
} }
@Override @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 (this.isHuman()) {
if (cards.isEmpty()) { if (cards.isEmpty()) {
return false; return false;
} }
Set<UUID> possibleTargets = target.possibleTargets(playerId, cards, game); Set<UUID> possibleTargets = target.possibleTargets(playerId, cards, source, game);
if (possibleTargets.isEmpty()) { if (possibleTargets.isEmpty()) {
return false; return false;
} }
@ -279,7 +279,7 @@ public class SimulatedPlayerMCTS extends MCTSPlayer {
target.add(targetId, game); target.add(targetId, game);
return true; return true;
} }
return super.choose(outcome, cards, target, game); return super.choose(outcome, cards, target, source, game);
} }
@Override @Override

View file

@ -700,7 +700,7 @@ public class HumanPlayer extends PlayerImpl {
} }
@Override @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)) { if (gameInCheckPlayableState(game)) {
return true; return true;
} }
@ -719,8 +719,8 @@ public class HumanPlayer extends PlayerImpl {
} }
while (canRespond()) { while (canRespond()) {
boolean required = target.isRequired(null, game); boolean required = target.isRequired(source != null ? source.getSourceId() : null, game);
int count = cards.count(target.getFilter(), abilityControllerId, game); int count = cards.count(target.getFilter(), abilityControllerId, source, game);
if (count == 0 if (count == 0
|| target.getTargets().size() >= target.getNumberOfTargets()) { || target.getTargets().size() >= target.getNumberOfTargets()) {
required = false; required = false;
@ -729,7 +729,7 @@ public class HumanPlayer extends PlayerImpl {
List<UUID> chosenTargets = target.getTargets(); List<UUID> chosenTargets = target.getTargets();
List<UUID> possibleTargets = new ArrayList<>(); List<UUID> possibleTargets = new ArrayList<>();
for (UUID cardId : cards) { for (UUID cardId : cards) {
if (target.canTarget(abilityControllerId, cardId, null, cards, game)) { if (target.canTarget(abilityControllerId, cardId, source, cards, game)) {
possibleTargets.add(cardId); possibleTargets.add(cardId);
} }
} }
@ -738,7 +738,7 @@ public class HumanPlayer extends PlayerImpl {
required = false; required = false;
} }
UUID responseId = target.tryToAutoChoose(abilityControllerId, null, game, possibleTargets); UUID responseId = target.tryToAutoChoose(abilityControllerId, source, game, possibleTargets);
if (responseId == null) { if (responseId == null) {
Map<String, Serializable> options = getOptions(target, null); Map<String, Serializable> options = getOptions(target, null);
@ -761,7 +761,7 @@ public class HumanPlayer extends PlayerImpl {
if (target.getTargets().contains(responseId)) { // if already included remove it with if (target.getTargets().contains(responseId)) { // if already included remove it with
target.remove(responseId); target.remove(responseId);
} else { } else {
if (target.canTarget(abilityControllerId, responseId, null, cards, game)) { if (target.canTarget(abilityControllerId, responseId, source, cards, game)) {
target.add(responseId, game); target.add(responseId, game);
if (target.doneChoosing()) { if (target.doneChoosing()) {
return true; return true;
@ -802,7 +802,7 @@ public class HumanPlayer extends PlayerImpl {
while (canRespond()) { while (canRespond()) {
boolean required = target.isRequiredExplicitlySet() ? target.isRequired() : target.isRequired(source); 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 if (count == 0
|| target.getTargets().size() >= target.getNumberOfTargets()) { || target.getTargets().size() >= target.getNumberOfTargets()) {
required = false; required = false;

View file

@ -3965,9 +3965,7 @@ public class TestPlayer implements Player {
} }
@Override @Override
public boolean choose(Outcome outcome, Cards cards, public boolean choose(Outcome outcome, Cards cards, TargetCard target, Ability source, Game game) {
TargetCard target, Game game
) {
assertAliasSupportInChoices(false); assertAliasSupportInChoices(false);
if (!choices.isEmpty()) { if (!choices.isEmpty()) {
@ -3995,7 +3993,7 @@ public class TestPlayer implements Player {
continue; continue;
} }
if (hasObjectTargetNameOrAlias(card, targetName)) { 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); target.add(card.getId(), game);
targetFound = true; targetFound = true;
break; break;
@ -4013,7 +4011,7 @@ public class TestPlayer implements Player {
} }
this.chooseStrictModeFailed("choice", game, getInfo(target)); this.chooseStrictModeFailed("choice", game, getInfo(target));
return computerPlayer.choose(outcome, cards, target, game); return computerPlayer.choose(outcome, cards, target, source, game);
} }
@Override @Override

View file

@ -835,7 +835,7 @@ public class PlayerStub implements Player {
} }
@Override @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; return false;
} }

View file

@ -54,7 +54,7 @@ public class FilterPermanent extends FilterObject<Permanent> implements FilterIn
if (!this.match(permanent, game) || !permanent.isPhasedIn()) { if (!this.match(permanent, game) || !permanent.isPhasedIn()) {
return false; return false;
} }
ObjectSourcePlayer<Permanent> osp = new ObjectSourcePlayer<Permanent>(permanent, playerId, source); ObjectSourcePlayer<Permanent> osp = new ObjectSourcePlayer<>(permanent, playerId, source);
return extraPredicates.stream().allMatch(p -> p.apply(osp, game)); return extraPredicates.stream().allMatch(p -> p.apply(osp, game));
} }

View file

@ -625,7 +625,11 @@ public interface Player extends MageItem, Copyable<Player> {
boolean choose(Outcome outcome, Target target, Ability source, Game game, Map<String, Serializable> options); boolean choose(Outcome outcome, Target target, Ability source, Game game, Map<String, Serializable> 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); boolean chooseTarget(Outcome outcome, Target target, Ability source, Game game);

View file

@ -50,7 +50,7 @@ public class StubPlayer extends PlayerImpl implements Player {
} }
@Override @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)); cards.getCards(game).stream().map(MageItem::getId).forEach(cardId -> target.add(cardId, game));
return true; return true;
} }

View file

@ -211,8 +211,8 @@ public class TargetCard extends TargetObject {
return possibleTargets; return possibleTargets;
} }
public Set<UUID> possibleTargets(UUID sourceControllerId, Cards cards, Game game) { public Set<UUID> possibleTargets(UUID sourceControllerId, Cards cards, Ability source, Game game) {
return cards.getCards(filter, game).stream().map(MageItem::getId).collect(Collectors.toSet()); return cards.getCards(filter, sourceControllerId, source, game).stream().map(MageItem::getId).collect(Collectors.toSet());
} }
@Override @Override

View file

@ -88,10 +88,14 @@ public class TargetCardInYourGraveyard extends TargetCard {
} }
@Override @Override
public Set<UUID> possibleTargets(UUID sourceControllerId, Cards cards, Game game) { public Set<UUID> possibleTargets(UUID sourceControllerId, Cards cards, Ability source, Game game) {
Set<UUID> possibleTargets = new HashSet<>(); Set<UUID> possibleTargets = new HashSet<>();
Player player = game.getPlayer(sourceControllerId); 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)) { if (player.getGraveyard().getCards(game).contains(card)) {
possibleTargets.add(card.getId()); possibleTargets.add(card.getId());
} }