Card: add IsEquippedBy like isEnchantedBy from GameEntity

CardPredicates: use isEquippedBy and isEnchantedBy for Predicate<Card>
This commit is contained in:
Hanmac
2016-05-25 07:18:16 +00:00
parent 395cc70b3e
commit d14e94b609
4 changed files with 44 additions and 65 deletions

View File

@@ -473,12 +473,8 @@ public class ComputerUtilCombat {
public static int totalDamageOfBlockers(final Card attacker, final List<Card> defenders) {
int damage = 0;
if (attacker.isEquipped()) {
for (Card equipment : attacker.getEquippedBy(false)) {
if (equipment.getName().equals("Godsend") && !defenders.isEmpty()) {
defenders.remove(0);
}
}
if (attacker.isEquippedBy("Godsend") && !defenders.isEmpty()) {
defenders.remove(0);
}
for (final Card defender : defenders) {
@@ -495,10 +491,8 @@ public class ComputerUtilCombat {
public static int totalFirstStrikeDamageOfBlockers(final Card attacker, final List<Card> defenders) {
int damage = 0;
for (Card equipment : attacker.getEquippedBy(false)) {
if (equipment.getName().equals("Godsend") && !defenders.isEmpty()) {
defenders.remove(0);
}
if (attacker.isEquippedBy("Godsend") && !defenders.isEmpty()) {
defenders.remove(0);
}
for (final Card defender : defenders) {
@@ -1527,12 +1521,8 @@ public class ComputerUtilCombat {
// check whether the attacker will be destroyed by triggered abilities before First Strike damage
public static boolean canDestroyAttackerBeforeFirstStrike(final Card attacker, final Card blocker, final Combat combat,
final boolean withoutAbilities) {
if (blocker.isEquipped()) {
for (Card equipment : blocker.getEquippedBy(false)) {
if (equipment.getName().equals("Godsend")) {
return true;
}
}
if (blocker.isEquippedBy("Godsend")) {
return true;
}
if (attacker.hasKeyword("Indestructible") || ComputerUtil.canRegenerate(attacker.getController(), attacker)) {
return false;
@@ -1747,12 +1737,8 @@ public class ComputerUtilCombat {
public static boolean canDestroyBlockerBeforeFirstStrike(final Card blocker, final Card attacker, final boolean withoutAbilities) {
if (attacker.isEquipped()) {
for (Card equipment : attacker.getEquippedBy(false)) {
if (equipment.getName().equals("Godsend")) {
return true;
}
}
if (attacker.isEquippedBy("Godsend")) {
return true;
}
if (attacker.getName().equals("Elven Warhounds")) {

View File

@@ -664,17 +664,9 @@ public class AttachAi extends SpellAbilityAi {
//some auras aren't useful in multiples
if (attachSource.hasSVar("NonStackingAttachEffect")) {
prefList = CardLists.filter(prefList, new Predicate<Card>() {
@Override
public boolean apply(final Card c) {
for (Card aura : c.getEnchantedBy(false)) {
if (aura.getName().equals(attachSource.getName())) {
return false;
}
}
return true;
}
});
prefList = CardLists.filter(prefList,
Predicates.not(CardPredicates.isEnchantedBy(attachSource.getName()))
);
}
c = ComputerUtilCard.getBestAI(prefList);
@@ -723,12 +715,8 @@ public class AttachAi extends SpellAbilityAi {
return false;
}
// don't equip creatures that don't gain anything
if (card.hasSVar("NonStackingAttachEffect")) {
for (Card equipment : newTarget.getEquippedBy(false)) {
if (equipment.getName().equals(card.getName())) {
return false;
}
}
if (card.hasSVar("NonStackingAttachEffect") && newTarget.isEquippedBy(equipment.getName())) {
return false;
}
}
}
@@ -958,26 +946,10 @@ public class AttachAi extends SpellAbilityAi {
//some auras/equipments aren't useful in multiples
if (attachSource.hasSVar("NonStackingAttachEffect")) {
prefList = CardLists.filter(prefList, new Predicate<Card>() {
@Override
public boolean apply(final Card c) {
if (c.isEquipped()) {
for (Card equipment : c.getEquippedBy(false)) {
if (equipment.getName().equals(attachSource.getName())) {
return false;
}
}
}
if (c.isEnchanted()) {
for (Card aura : c.getEnchantedBy(false)) {
if (aura.getName().equals(attachSource.getName())) {
return false;
}
}
}
return true;
}
});
prefList = CardLists.filter(prefList, Predicates.not(Predicates.or(
CardPredicates.isEquippedBy(attachSource.getName()),
CardPredicates.isEnchantedBy(attachSource.getName())
)));
}
// Don't pump cards that will die.

View File

@@ -2281,6 +2281,14 @@ public class Card extends GameEntity implements Comparable<Card> {
public final boolean isEquippedBy(Card c) {
return FCollection.hasElement(equippedBy, c);
}
public final boolean isEquippedBy(final String cardName) {
for (final Card card : getEquippedBy(false)) {
if (card.getName().equals(cardName)) {
return true;
}
}
return false;
}
public final CardCollectionView getFortifiedBy(boolean allowModify) {
return CardCollection.getView(fortifiedBy, allowModify);
@@ -3926,14 +3934,9 @@ public class Card extends GameEntity implements Comparable<Card> {
}
} else if (property.equals("NameNotEnchantingEnchantedPlayer")) {
Player enchantedPlayer = source.getEnchantingPlayer();
if (enchantedPlayer == null) {
if (enchantedPlayer == null || enchantedPlayer.isEnchantedBy(getName())) {
return false;
}
for (Card c : enchantedPlayer.getEnchantedBy(false)) {
if (getName().equals(c.getName())) {
return false;
}
}
} else if (property.equals("NotAttachedTo")) {
if (equipping == source || source.equals(enchanting) || fortifying == source) {
return false;

View File

@@ -171,6 +171,24 @@ public final class CardPredicates {
};
} // getColor()
public static final Predicate<Card> isEquippedBy(final String name) {
return new Predicate<Card>() {
@Override
public boolean apply(final Card c) {
return c.isEquippedBy(name);
}
};
}
public static final Predicate<Card> isEnchantedBy(final String name) {
return new Predicate<Card>() {
@Override
public boolean apply(final Card c) {
return c.isEnchantedBy(name);
}
};
}
public static final Predicate<Card> hasCMC(final int cmc) {
return new Predicate<Card>() {
@Override