Combat.getAttackers and Combat.getBlockers now return a Set instead of a List, so that two-headed blockers aren't included twice

This commit is contained in:
Alex W. Jackson 2022-09-07 22:36:05 -04:00
parent efaccf8564
commit a6c5209a2a
20 changed files with 192 additions and 354 deletions

View file

@ -0,0 +1,44 @@
package mage.abilities.common;
import mage.abilities.effects.Effect;
import mage.constants.AttachmentType;
import mage.constants.SetTargetPointer;
import mage.game.Game;
import mage.game.events.GameEvent;
/**
* @author awjackson
*/
public class AttacksAloneAttachedTriggeredAbility extends AttacksAttachedTriggeredAbility {
public AttacksAloneAttachedTriggeredAbility(Effect effect) {
this(effect, false);
}
public AttacksAloneAttachedTriggeredAbility(Effect effect, boolean optional) {
this(effect, AttachmentType.EQUIPMENT, optional);
}
public AttacksAloneAttachedTriggeredAbility(Effect effect, AttachmentType attachmentType, boolean optional) {
this(effect, attachmentType, optional, SetTargetPointer.NONE);
}
public AttacksAloneAttachedTriggeredAbility(Effect effect, AttachmentType attachmentType, boolean optional, SetTargetPointer setTargetPointer) {
super(effect, attachmentType, optional, setTargetPointer);
setTriggerPhrase("Whenever " + attachmentType.verb().toLowerCase() + " creature attacks alone, ");
}
protected AttacksAloneAttachedTriggeredAbility(final AttacksAloneAttachedTriggeredAbility ability) {
super(ability);
}
@Override
public AttacksAloneAttachedTriggeredAbility copy() {
return new AttacksAloneAttachedTriggeredAbility(this);
}
@Override
public boolean checkTrigger(GameEvent event, Game game) {
return game.getCombat().attacksAlone() && super.checkTrigger(event, game);
}
}

View file

@ -10,7 +10,6 @@ import mage.game.events.GameEvent;
import mage.game.permanent.Permanent;
import mage.target.targetpointer.FixedTarget;
import mage.util.CardUtil;
import mage.util.CardUtil;
/**
* @author TheElk801
@ -39,7 +38,7 @@ public class AttacksAloneControlledTriggeredAbility extends TriggeredAbilityImpl
setTriggerPhrase("Whenever " + CardUtil.addArticle(filter.getMessage()) + " attacks alone, ");
}
private AttacksAloneControlledTriggeredAbility(final AttacksAloneControlledTriggeredAbility ability) {
protected AttacksAloneControlledTriggeredAbility(final AttacksAloneControlledTriggeredAbility ability) {
super(ability);
this.filter = ability.filter;
this.setTargetPointer = ability.setTargetPointer;
@ -52,7 +51,7 @@ public class AttacksAloneControlledTriggeredAbility extends TriggeredAbilityImpl
@Override
public boolean checkEventType(GameEvent event, Game game) {
return event.getType() == GameEvent.EventType.DECLARED_ATTACKERS;
return event.getType() == GameEvent.EventType.ATTACKER_DECLARED;
}
@Override
@ -60,7 +59,7 @@ public class AttacksAloneControlledTriggeredAbility extends TriggeredAbilityImpl
if (!game.getCombat().attacksAlone()) {
return false;
}
Permanent permanent = game.getPermanent(game.getCombat().getAttackers().get(0));
Permanent permanent = game.getPermanent(event.getSourceId());
if (permanent == null || !filter.match(permanent, getControllerId(), this, game)) {
return false;
}

View file

@ -1,8 +1,5 @@
package mage.abilities.common;
import java.util.Objects;
import java.util.UUID;
import mage.abilities.TriggeredAbilityImpl;
import mage.abilities.effects.Effect;
import mage.constants.Zone;
@ -21,7 +18,7 @@ public class AttacksAloneSourceTriggeredAbility extends TriggeredAbilityImpl {
setTriggerPhrase("Whenever {this} attacks alone, ");
}
public AttacksAloneSourceTriggeredAbility(final AttacksAloneSourceTriggeredAbility ability) {
protected AttacksAloneSourceTriggeredAbility(final AttacksAloneSourceTriggeredAbility ability) {
super(ability);
}
@ -32,25 +29,15 @@ public class AttacksAloneSourceTriggeredAbility extends TriggeredAbilityImpl {
@Override
public boolean checkEventType(GameEvent event, Game game) {
return event.getType() == GameEvent.EventType.DECLARED_ATTACKERS;
return event.getType() == GameEvent.EventType.ATTACKER_DECLARED;
}
@Override
public boolean checkTrigger(GameEvent event, Game game) {
if(game.isActivePlayer(this.controllerId) ) {
UUID creatureId = this.getSourceId();
if(creatureId != null) {
if(game.getCombat().attacksAlone() && Objects.equals(creatureId, game.getCombat().getAttackers().get(0))) {
UUID defender = game.getCombat().getDefenderId(creatureId);
if(defender != null) {
for(Effect effect: getEffects()) {
effect.setTargetPointer(new FixedTarget(defender));
}
}
return true;
}
}
if (!getSourceId().equals(event.getSourceId()) || !game.getCombat().attacksAlone()) {
return false;
}
return false;
getEffects().setTargetPointer(new FixedTarget(game.getCombat().getDefendingPlayerId(getSourceId(), game)));
return true;
}
}

View file

@ -1,16 +1,16 @@
package mage.abilities.common;
import mage.abilities.TriggeredAbilityImpl;
import mage.abilities.effects.Effect;
import mage.constants.AttachmentType;
import mage.constants.SetTargetPointer;
import mage.constants.Zone;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.permanent.Permanent;
import mage.target.targetpointer.FixedTarget;
import java.util.Locale;
import java.util.UUID;
/**
* "When enchanted/equipped creature attacks " triggered ability
@ -20,7 +20,7 @@ import java.util.Locale;
public class AttacksAttachedTriggeredAbility extends TriggeredAbilityImpl {
private final AttachmentType attachmentType;
private final boolean setTargetPointer;
private final SetTargetPointer setTargetPointer;
public AttacksAttachedTriggeredAbility(Effect effect) {
this(effect, false);
@ -31,20 +31,20 @@ public class AttacksAttachedTriggeredAbility extends TriggeredAbilityImpl {
}
public AttacksAttachedTriggeredAbility(Effect effect, AttachmentType attachmentType, boolean optional) {
this(effect, attachmentType, optional, false);
this(effect, attachmentType, optional, SetTargetPointer.NONE);
}
public AttacksAttachedTriggeredAbility(Effect effect, AttachmentType attachmentType, boolean optional, boolean setTargetPointer) {
public AttacksAttachedTriggeredAbility(Effect effect, AttachmentType attachmentType, boolean optional, SetTargetPointer setTargetPointer) {
super(Zone.BATTLEFIELD, effect, optional);
this.attachmentType = attachmentType;
this.setTargetPointer = setTargetPointer;
setTriggerPhrase("Whenever " + attachmentType.verb().toLowerCase(Locale.ENGLISH) + " creature attacks, ");
setTriggerPhrase("Whenever " + attachmentType.verb().toLowerCase() + " creature attacks, ");
}
public AttacksAttachedTriggeredAbility(final AttacksAttachedTriggeredAbility abiltity) {
super(abiltity);
this.attachmentType = abiltity.attachmentType;
this.setTargetPointer = abiltity.setTargetPointer;
protected AttacksAttachedTriggeredAbility(final AttacksAttachedTriggeredAbility ability) {
super(ability);
this.attachmentType = ability.attachmentType;
this.setTargetPointer = ability.setTargetPointer;
}
@Override
@ -59,20 +59,19 @@ public class AttacksAttachedTriggeredAbility extends TriggeredAbilityImpl {
@Override
public boolean checkTrigger(GameEvent event, Game game) {
Permanent equipment = game.getPermanent(this.sourceId);
if (equipment != null && equipment.getAttachedTo() != null
&& event.getSourceId().equals(equipment.getAttachedTo())) {
getEffects().setValue("sourceId", event.getSourceId());
// TODO: Passing a permanent object like this can cause bugs. May need refactoring to use UUID instead.
// See https://github.com/magefree/mage/issues/8377
// 11-08-2021: Added a new constructor to set target pointer. Should probably be using this instead.
Permanent attachedPermanent = game.getPermanent(event.getSourceId());
getEffects().setValue("attachedPermanent", attachedPermanent);
if (setTargetPointer && attachedPermanent != null) {
getEffects().setTargetPointer(new FixedTarget(attachedPermanent, game));
}
return true;
Permanent attachment = getSourcePermanentOrLKI(game);
UUID attackerId = event.getSourceId();
if (attachment == null || !attackerId.equals(attachment.getAttachedTo())) {
return false;
}
return false;
switch (setTargetPointer) {
case PERMANENT:
getEffects().setTargetPointer(new FixedTarget(attackerId, game));
break;
case PLAYER:
getEffects().setTargetPointer(new FixedTarget(game.getCombat().getDefendingPlayerId(attackerId, game)));
break;
}
return true;
}
}

View file

@ -0,0 +1,20 @@
package mage.abilities.condition.common;
import mage.abilities.Ability;
import mage.abilities.condition.Condition;
import mage.constants.TurnPhase;
import mage.game.Game;
public enum FirstCombatPhaseCondition implements Condition {
instance;
@Override
public boolean apply(Game game, Ability source) {
return game.getTurn().getPhase(TurnPhase.COMBAT).getCount() == 0;
}
@Override
public String toString() {
return "it's the first combat phase of the turn";
}
}

View file

@ -1,25 +1,19 @@
package mage.abilities.keyword;
import mage.abilities.TriggeredAbilityImpl;
import mage.abilities.common.AttacksAloneControlledTriggeredAbility;
import mage.abilities.effects.common.continuous.BoostTargetEffect;
import mage.constants.Duration;
import mage.constants.Zone;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.target.targetpointer.FixedTarget;
/**
*
* @author BetaSteward_at_googlemail.com
* @author awjackson
*/
public class ExaltedAbility extends TriggeredAbilityImpl {
public class ExaltedAbility extends AttacksAloneControlledTriggeredAbility {
public ExaltedAbility() {
super(Zone.BATTLEFIELD, new BoostTargetEffect(1, 1, Duration.EndOfTurn));
super(new BoostTargetEffect(1, 1), true, false);
}
public ExaltedAbility(final ExaltedAbility ability) {
private ExaltedAbility(final ExaltedAbility ability) {
super(ability);
}
@ -28,25 +22,8 @@ public class ExaltedAbility extends TriggeredAbilityImpl {
return new ExaltedAbility(this);
}
@Override
public boolean checkEventType(GameEvent event, Game game) {
return event.getType() == GameEvent.EventType.DECLARED_ATTACKERS;
}
@Override
public boolean checkTrigger(GameEvent event, Game game) {
if (game.isActivePlayer(this.controllerId)) {
if (game.getCombat().attacksAlone()) {
this.getEffects().get(0).setTargetPointer(new FixedTarget(game.getCombat().getAttackers().get(0)));
return true;
}
}
return false;
}
@Override
public String getRule() {
return "exalted <i>(Whenever a creature you control attacks alone, that creature gets +1/+1 until end of turn.)</i>";
return "Exalted <i>(Whenever a creature you control attacks alone, that creature gets +1/+1 until end of turn.)</i>";
}
}

View file

@ -115,16 +115,16 @@ public class Combat implements Serializable, Copyable<Combat> {
return defenders;
}
public List<UUID> getAttackers() {
List<UUID> attackers = new ArrayList<>();
public Set<UUID> getAttackers() {
Set<UUID> attackers = new HashSet<>();
for (CombatGroup group : groups) {
attackers.addAll(group.attackers);
}
return attackers;
}
public List<UUID> getBlockers() {
List<UUID> blockers = new ArrayList<>();
public Set<UUID> getBlockers() {
Set<UUID> blockers = new HashSet<>();
for (CombatGroup group : groups) {
blockers.addAll(group.blockers);
}
@ -1160,14 +1160,13 @@ public class Combat implements Serializable, Copyable<Combat> {
Set<UUID> blockedSet = mustBeBlockedByAtLeastX.get(blockedAttackerId);
Set<UUID> toBlockSet = mustBeBlockedByAtLeastX.get(toBeBlockedCreatureId);
if (toBlockSet == null) {
// This should never happen.
// This should never happen.
return null;
} else if (toBlockSet.containsAll(blockedSet)) {
// the creature already blocks alone a creature that has to be blocked by at least one
// and has more possible blockers, so this is ok
// the creature already blocks alone a creature that has to be blocked by at least one
// and has more possible blockers, so this is ok
return null;
}
}
// TODO: Check if the attacker is already blocked by another creature
// and despite there is need that this attacker blocks this attacker also