Skip to content

Commit

Permalink
refactor: TargetCreatureOrPlayer inheritance (#13199)
Browse files Browse the repository at this point in the history
* update TargetCreatureOrPlayer to be a subclass of TargetPermanentOrPlayer

closes #11161

* fix usages
  • Loading branch information
xenohedron authored Jan 9, 2025
1 parent 41b9c95 commit 9c5c394
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 340 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -311,37 +311,6 @@ public boolean choose(Outcome outcome, Target target, Ability source, Game game,
}
}

if (target.getOriginalTarget() instanceof TargetCreatureOrPlayer) {
List<Permanent> targets;
TargetCreatureOrPlayer origTarget = (TargetCreatureOrPlayer) target.getOriginalTarget();
if (outcome.isGood()) {
targets = threats(abilityControllerId, source, ((FilterCreatureOrPlayer) origTarget.getFilter()).getCreatureFilter(), game, target.getTargets());
} else {
targets = threats(randomOpponentId, source, ((FilterCreatureOrPlayer) origTarget.getFilter()).getCreatureFilter(), game, target.getTargets());
}
for (Permanent permanent : targets) {
List<UUID> alreadyTargeted = target.getTargets();
if (target.canTarget(abilityControllerId, permanent.getId(), null, game)) {
if (alreadyTargeted != null && !alreadyTargeted.contains(permanent.getId())) {
target.add(permanent.getId(), game);
return true;
}
}
}
if (outcome.isGood()) {
if (target.canTarget(abilityControllerId, abilityControllerId, null, game)) {
target.add(abilityControllerId, game);
return true;
}
} else if (target.canTarget(abilityControllerId, randomOpponentId, null, game)) {
target.add(randomOpponentId, game);
return true;
}
if (!required) {
return false;
}
}

if (target.getOriginalTarget() instanceof TargetPermanentOrPlayer) {
List<Permanent> targets;
TargetPermanentOrPlayer origTarget = (TargetPermanentOrPlayer) target.getOriginalTarget();
Expand Down Expand Up @@ -752,48 +721,6 @@ public boolean chooseTarget(Outcome outcome, Target target, Ability source, Game
return target.isChosen(game);
}

if (target.getOriginalTarget() instanceof TargetCreatureOrPlayer) {
List<Permanent> targets;
TargetCreatureOrPlayer origTarget = ((TargetCreatureOrPlayer) target.getOriginalTarget());
if (outcome.isGood()) {
targets = threats(abilityControllerId, source, ((FilterCreatureOrPlayer) origTarget.getFilter()).getCreatureFilter(), game, target.getTargets());
} else {
targets = threats(randomOpponentId, source, ((FilterCreatureOrPlayer) origTarget.getFilter()).getCreatureFilter(), game, target.getTargets());
}

if (targets.isEmpty()) {
if (outcome.isGood()) {
if (target.canTarget(abilityControllerId, abilityControllerId, source, game)) {
return tryAddTarget(target, abilityControllerId, source, game);
}
} else if (target.canTarget(abilityControllerId, randomOpponentId, source, game)) {
return tryAddTarget(target, randomOpponentId, source, game);
}
}

if (targets.isEmpty() && target.isRequired(source)) {
targets = game.getBattlefield().getActivePermanents(((FilterCreatureOrPlayer) origTarget.getFilter()).getCreatureFilter(), playerId, game);
}
for (Permanent permanent : targets) {
List<UUID> alreadyTargeted = target.getTargets();
if (target.canTarget(abilityControllerId, permanent.getId(), source, game)) {
if (alreadyTargeted != null && !alreadyTargeted.contains(permanent.getId())) {
return tryAddTarget(target, permanent.getId(), source, game);
}
}
}

if (outcome.isGood()) {
if (target.canTarget(abilityControllerId, abilityControllerId, source, game)) {
return tryAddTarget(target, abilityControllerId, source, game);
}
} else if (target.canTarget(abilityControllerId, randomOpponentId, source, game)) {
return tryAddTarget(target, randomOpponentId, source, game);
}

return false;
}

if (target.getOriginalTarget() instanceof TargetAnyTarget) {
List<Permanent> targets;
TargetAnyTarget origTarget = ((TargetAnyTarget) target.getOriginalTarget());
Expand Down
8 changes: 3 additions & 5 deletions Mage.Sets/src/mage/cards/s/StarDestroyer.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@

package mage.cards.s;

import java.util.UUID;
import mage.MageInt;
import mage.abilities.Ability;
import mage.abilities.common.SimpleActivatedAbility;
Expand All @@ -14,16 +12,16 @@
import mage.cards.CardSetInfo;
import mage.constants.CardType;
import mage.constants.SubType;
import mage.constants.Zone;
import mage.filter.common.FilterCreatureOrPlayer;
import mage.filter.common.FilterCreaturePermanent;
import mage.filter.predicate.Predicates;
import mage.game.permanent.token.TIEFighterToken;
import mage.target.common.TargetCreatureOrPlayer;
import mage.target.common.TargetCreaturePermanent;

import java.util.UUID;

/**
*
* @author Styxo
*/
public final class StarDestroyer extends CardImpl {
Expand All @@ -33,7 +31,7 @@ public final class StarDestroyer extends CardImpl {

static {
filter1.add(CardType.ARTIFACT.getPredicate());
filter3.getCreatureFilter().add(Predicates.not(SubType.STARSHIP.getPredicate()));
filter3.getPermanentFilter().add(Predicates.not(SubType.STARSHIP.getPredicate()));
}

public StarDestroyer(UUID ownerId, CardSetInfo setInfo) {
Expand Down
9 changes: 3 additions & 6 deletions Mage.Sets/src/mage/cards/t/ThermalDetonator.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package mage.cards.t;

import java.util.UUID;
import mage.abilities.Ability;
import mage.abilities.common.SimpleActivatedAbility;
import mage.abilities.costs.common.SacrificeSourceCost;
Expand All @@ -10,24 +9,22 @@
import mage.cards.CardImpl;
import mage.cards.CardSetInfo;
import mage.constants.CardType;
import mage.constants.Zone;
import mage.filter.common.FilterCreatureOrPlayer;
import mage.filter.common.FilterCreaturePermanent;
import mage.filter.predicate.Predicates;
import mage.filter.predicate.mageobject.AbilityPredicate;
import mage.target.common.TargetCreatureOrPlayer;

import java.util.UUID;

/**
*
* @author NinthWorld
*/
public final class ThermalDetonator extends CardImpl {

private static final FilterCreatureOrPlayer filter = new FilterCreatureOrPlayer("creature without spaceflight or target player");
private static final FilterCreaturePermanent filterCreature = new FilterCreaturePermanent();

static {
filter.getCreatureFilter().add(Predicates.not(new AbilityPredicate(SpaceflightAbility.class)));
filter.getPermanentFilter().add(Predicates.not(new AbilityPredicate(SpaceflightAbility.class)));
}

public ThermalDetonator(UUID ownerId, CardSetInfo setInfo) {
Expand Down
10 changes: 0 additions & 10 deletions Mage.Tests/src/test/java/org/mage/test/player/TestPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2286,13 +2286,10 @@ public boolean choose(Outcome outcome, Target target, Ability source, Game game,

// TODO: Allow to choose a player with TargetPermanentOrPlayer
if ((target.getOriginalTarget() instanceof TargetPermanent)
|| (target.getOriginalTarget() instanceof TargetCreatureOrPlayer) // player target not implemented yet
|| (target.getOriginalTarget() instanceof TargetPermanentOrPlayer)) { // player target not implemented yet
FilterPermanent filterPermanent;
if (target.getOriginalTarget() instanceof TargetPermanentOrPlayer) {
filterPermanent = ((TargetPermanentOrPlayer) target.getOriginalTarget()).getFilterPermanent();
} else if (target.getOriginalTarget() instanceof TargetCreatureOrPlayer) {
filterPermanent = ((TargetCreatureOrPlayer) target.getOriginalTarget()).getFilterCreature();
} else {
filterPermanent = ((TargetPermanent) target.getOriginalTarget()).getFilter();
}
Expand Down Expand Up @@ -2516,8 +2513,6 @@ public boolean chooseTarget(Outcome outcome, Target target, Ability source, Game

// player
if (target.getOriginalTarget() instanceof TargetPlayer
|| target.getOriginalTarget() instanceof TargetAnyTarget
|| target.getOriginalTarget() instanceof TargetCreatureOrPlayer
|| target.getOriginalTarget() instanceof TargetPermanentOrPlayer) {
for (String targetDefinition : targets.stream().limit(takeMaxTargetsPerChoose).collect(Collectors.toList())) {
if (!targetDefinition.startsWith("targetPlayer=")) {
Expand All @@ -2539,8 +2534,6 @@ public boolean chooseTarget(Outcome outcome, Target target, Ability source, Game
// permanent in battlefield
if ((target.getOriginalTarget() instanceof TargetPermanent)
|| (target.getOriginalTarget() instanceof TargetPermanentOrPlayer)
|| (target.getOriginalTarget() instanceof TargetAnyTarget)
|| (target.getOriginalTarget() instanceof TargetCreatureOrPlayer)
|| (target.getOriginalTarget() instanceof TargetPermanentOrSuspendedCard)) {
for (String targetDefinition : targets.stream().limit(takeMaxTargetsPerChoose).collect(Collectors.toList())) {
if (targetDefinition.startsWith("targetPlayer=")) {
Expand All @@ -2564,9 +2557,6 @@ public boolean chooseTarget(Outcome outcome, Target target, Ability source, Game
}
}
Filter filter = target.getOriginalTarget().getFilter();
if (filter instanceof FilterCreatureOrPlayer) {
filter = ((FilterCreatureOrPlayer) filter).getCreatureFilter();
}
if (filter instanceof FilterPermanentOrPlayer) {
filter = ((FilterPermanentOrPlayer) filter).getPermanentFilter();
}
Expand Down
61 changes: 2 additions & 59 deletions Mage/src/main/java/mage/filter/common/FilterCreatureOrPlayer.java
Original file line number Diff line number Diff line change
@@ -1,79 +1,22 @@
package mage.filter.common;

import mage.MageItem;
import mage.abilities.Ability;
import mage.filter.FilterImpl;
import mage.filter.FilterInPlay;
import mage.filter.FilterPlayer;
import mage.game.Game;
import mage.game.permanent.Permanent;
import mage.players.Player;

import java.util.UUID;

/**
* @author BetaSteward_at_googlemail.com
*/
public class FilterCreatureOrPlayer extends FilterImpl<MageItem> implements FilterInPlay<MageItem> {

protected FilterCreaturePermanent creatureFilter;
protected final FilterPlayer playerFilter;
public class FilterCreatureOrPlayer extends FilterPermanentOrPlayer {

public FilterCreatureOrPlayer() {
this("creature or player");
}

public FilterCreatureOrPlayer(String name) {
super(name);
creatureFilter = new FilterCreaturePermanent();
playerFilter = new FilterPlayer();
super(name, new FilterCreaturePermanent(), new FilterPlayer());
}

protected FilterCreatureOrPlayer(final FilterCreatureOrPlayer filter) {
super(filter);
this.creatureFilter = filter.creatureFilter.copy();
this.playerFilter = filter.playerFilter.copy();
}

@Override
public boolean checkObjectClass(Object object) {
return true;
}

@Override
public boolean match(MageItem o, Game game) {
if (super.match(o, game)) {
if (o instanceof Player) {
return playerFilter.match((Player) o, game);
} else if (o instanceof Permanent) {
return creatureFilter.match((Permanent) o, game);
}
}
return false;
}

@Override
public boolean match(MageItem o, UUID playerId, Ability source, Game game) {
if (super.match(o, game)) { // process predicates
if (o instanceof Player) {
return playerFilter.match((Player) o, playerId, source, game);
} else if (o instanceof Permanent) {
return creatureFilter.match((Permanent) o, playerId, source, game);
}
}
return false;
}

public FilterCreaturePermanent getCreatureFilter() {
return this.creatureFilter;
}

public FilterPlayer getPlayerFilter() {
return this.playerFilter;
}

public void setCreatureFilter(FilterCreaturePermanent creatureFilter) {
this.creatureFilter = creatureFilter;
}

@Override
Expand Down
Loading

0 comments on commit 9c5c394

Please sign in to comment.