Skip to content

Commit

Permalink
fix magefree#11766 (Corrosive Ooze), add test
Browse files Browse the repository at this point in the history
  • Loading branch information
xenohedron committed Aug 19, 2024
1 parent b6ee7fe commit 77b8030
Show file tree
Hide file tree
Showing 2 changed files with 248 additions and 86 deletions.
138 changes: 52 additions & 86 deletions Mage.Sets/src/mage/cards/c/CorrosiveOoze.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
import mage.game.events.GameEvent;
import mage.game.events.ZoneChangeEvent;
import mage.game.permanent.Permanent;
import mage.players.Player;
import mage.util.CardUtil;
import mage.watchers.Watcher;

import java.util.*;
import java.util.stream.Collectors;

/**
* @author rscoates
* @author rscoates, LevelX2, xenohedron
*/
public final class CorrosiveOoze extends CardImpl {

Expand Down Expand Up @@ -76,43 +75,27 @@ public CorrosiveOozeEffect copy() {

@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
CorrosiveOozeCombatWatcher watcher = game.getState().getWatcher(CorrosiveOozeCombatWatcher.class);
if (controller != null && watcher != null) {
MageObjectReference sourceMor = new MageObjectReference(source.getSourceObject(game), game);
// get equipmentsToDestroy of creatres already left the battlefield
List<Permanent> toDestroy = new ArrayList<>();
Set<MageObjectReference> toDestroyMor = watcher.getEquipmentsToDestroy(sourceMor);
if (toDestroyMor != null) {
for (MageObjectReference mor : toDestroyMor) {
Permanent attachment = mor.getPermanent(game);
if (attachment != null) {
toDestroy.add(attachment);
}
}
}
// get the related creatures
Set<MageObjectReference> relatedCreatures = watcher.getRelatedBlockedCreatures(sourceMor);
if (relatedCreatures != null) {
for (MageObjectReference relatedCreature : relatedCreatures) {
Permanent permanent = relatedCreature.getPermanent(game);
if (permanent != null) {
for (UUID attachmentId : permanent.getAttachments()) {
Permanent attachment = game.getPermanent(attachmentId);
if (attachment != null && attachment.hasSubtype(SubType.EQUIPMENT, game)) {
toDestroy.add(attachment);
}
}
}
}
}
for (Permanent permanent : toDestroy) {
permanent.destroy(source, game, false);
}

return true;
if (watcher == null) {
return false;
}
return false;
MageObjectReference sourceMor = new MageObjectReference(source.getSourceId(), source.getSourceObjectZoneChangeCounter(), game);
List<Permanent> equipments = watcher.getEquipmentsToDestroy(sourceMor)
.stream()
.map(mor -> mor.getPermanent(game))
.filter(Objects::nonNull)
.collect(Collectors.toList());
watcher.getRelatedBlockedCreatures(sourceMor)
.stream()
.map(mor -> mor.getPermanent(game))
.filter(Objects::nonNull)
.flatMap(p -> p.getAttachments().stream())
.map(game::getPermanent)
.filter(Objects::nonNull)
.filter(a -> a.hasSubtype(SubType.EQUIPMENT, game))
.forEach(equipments::add);
equipments.forEach(p -> p.destroy(source, game));
return true;

}
}
Expand All @@ -122,79 +105,62 @@ class CorrosiveOozeCombatWatcher extends Watcher {
private final Map<MageObjectReference, Set<MageObjectReference>> oozeBlocksOrBlocked = new HashMap<>();
private final Map<MageObjectReference, Set<MageObjectReference>> oozeEquipmentsToDestroy = new HashMap<>();

public CorrosiveOozeCombatWatcher() {
CorrosiveOozeCombatWatcher() {
super(WatcherScope.GAME);
}

@Override
public void watch(GameEvent event, Game game) {
if (event.getType() == GameEvent.EventType.BEGIN_COMBAT_STEP_PRE) {
this.oozeBlocksOrBlocked.clear();
return;
}
if (event.getType() == GameEvent.EventType.BLOCKER_DECLARED) {
Permanent attacker = game.getPermanent(event.getTargetId());
Permanent blocker = game.getPermanent(event.getSourceId());
if (attacker != null && CardUtil.haveSameNames(attacker, "Corrosive Ooze", game)) { // To check for name is not working if Ooze is copied but name changed
if (blocker != null && hasAttachedEquipment(game, blocker)) {
MageObjectReference oozeMor = new MageObjectReference(attacker, game);
Set<MageObjectReference> relatedCreatures = oozeBlocksOrBlocked.getOrDefault(oozeMor, new HashSet<>());
relatedCreatures.add(new MageObjectReference(event.getSourceId(), game));
oozeBlocksOrBlocked.put(oozeMor, relatedCreatures);
}
}
if (blocker != null && CardUtil.haveSameNames(blocker, "Corrosive Ooze", game)) {
if (attacker != null && hasAttachedEquipment(game, attacker)) {
MageObjectReference oozeMor = new MageObjectReference(blocker, game);
Set<MageObjectReference> relatedCreatures = oozeBlocksOrBlocked.getOrDefault(oozeMor, new HashSet<>());
relatedCreatures.add(new MageObjectReference(event.getTargetId(), game));
oozeBlocksOrBlocked.put(oozeMor, relatedCreatures);
}
if (attacker == null || blocker == null) {
return;
}
oozeBlocksOrBlocked.computeIfAbsent(new MageObjectReference(attacker, game), k -> new HashSet<>())
.add(new MageObjectReference(blocker, game));
oozeBlocksOrBlocked.computeIfAbsent(new MageObjectReference(blocker, game), k -> new HashSet<>())
.add(new MageObjectReference(attacker, game));
return;
}

if (event.getType() == GameEvent.EventType.ZONE_CHANGE) {
if (((ZoneChangeEvent) event).getFromZone() == Zone.BATTLEFIELD) {
if (game.getTurn() != null && TurnPhase.COMBAT == game.getTurnPhaseType()) {
// Check if a previous blocked or blocked by creatures is leaving the battlefield
for (Map.Entry<MageObjectReference, Set<MageObjectReference>> entry : oozeBlocksOrBlocked.entrySet()) {
for (MageObjectReference mor : entry.getValue()) {
if (mor.refersTo(((ZoneChangeEvent) event).getTarget(), game)) {
// check for equipments and remember
for (UUID attachmentId : ((ZoneChangeEvent) event).getTarget().getAttachments()) {
Permanent attachment = game.getPermanent(attachmentId);
if (attachment != null && attachment.hasSubtype(SubType.EQUIPMENT, game)) {
Set<MageObjectReference> toDestroy = oozeEquipmentsToDestroy.getOrDefault(entry.getKey(), new HashSet<>());
toDestroy.add(new MageObjectReference(attachment, game));
oozeEquipmentsToDestroy.put(entry.getKey(), toDestroy);
}
}
}
ZoneChangeEvent zEvent = (ZoneChangeEvent) event;
if (zEvent.getFromZone() == Zone.BATTLEFIELD) {
// Check if a previous blocked or blocked by creatures is leaving the battlefield
for (Map.Entry<MageObjectReference, Set<MageObjectReference>> entry : oozeBlocksOrBlocked.entrySet()) {
for (MageObjectReference mor : entry.getValue()) {
if (mor.refersTo(zEvent.getTarget(), game)) {
// check for equipments and remember
zEvent.getTarget().getAttachments().stream()
.map(game::getPermanent)
.filter(Objects::nonNull)
.filter(a -> a.hasSubtype(SubType.EQUIPMENT, game))
.map(a -> new MageObjectReference(a, game))
.forEach(oozeEquipmentsToDestroy.computeIfAbsent(entry.getKey(), k -> new HashSet<>())::add);
}
}
}
}
}
}

private boolean hasAttachedEquipment(Game game, Permanent permanent) {
for (UUID attachmentId : permanent.getAttachments()) {
Permanent attachment = game.getPermanent(attachmentId);
if (attachment != null && attachment.hasSubtype(SubType.EQUIPMENT, game)) {
return true;
}
}
return false;
@Override
public void reset() {
super.reset();
oozeBlocksOrBlocked.clear();
oozeEquipmentsToDestroy.clear();
}

public Set<MageObjectReference> getRelatedBlockedCreatures(MageObjectReference ooze) {
Set<MageObjectReference> relatedCreatures = this.oozeBlocksOrBlocked.get(ooze);
oozeBlocksOrBlocked.remove(ooze); // remove here to get no overlap with creatures leaving meanwhile
return relatedCreatures;
Set<MageObjectReference> getRelatedBlockedCreatures(MageObjectReference ooze) {
return oozeBlocksOrBlocked.getOrDefault(ooze, Collections.emptySet());
}

public Set<MageObjectReference> getEquipmentsToDestroy(MageObjectReference ooze) {
Set<MageObjectReference> equipmentsToDestroy = this.oozeEquipmentsToDestroy.get(ooze);
oozeEquipmentsToDestroy.remove(ooze); // remove here to get no overlap with creatures leaving meanwhile
return equipmentsToDestroy;
Set<MageObjectReference> getEquipmentsToDestroy(MageObjectReference ooze) {
return oozeEquipmentsToDestroy.getOrDefault(ooze, Collections.emptySet());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
package org.mage.test.cards.single.dom;

import mage.constants.PhaseStep;
import mage.constants.Zone;
import org.junit.Test;
import org.mage.test.serverside.base.CardTestPlayerBase;

public class CorrosiveOozeTest extends CardTestPlayerBase {

private static final String ooze = "Corrosive Ooze"; // 1G 2/2
// Whenever Corrosive Ooze blocks or becomes blocked by an equipped creature,
// destroy all Equipment attached to that creature at end of combat.
/*
* The set of Equipment to be destroyed is determined only as Corrosive Ooze’s delayed triggered ability resolves
* at the end of combat. The Equipment will be destroyed even if Corrosive Ooze leaves the battlefield before that time.
* --
* If the creature Corrosive Ooze blocks or is blocking leaves the battlefield, the Equipment that was attached to
* that creature immediately before it left the battlefield will be destroyed as Corrosive Ooze’s delayed triggered
* ability resolves at the end of combat.
*/

private static final String sword = "Short Sword"; // Equip 1: +1/+1
private static final String wizard = "Fugitive Wizard"; // 1/1
private static final String kraken = "Kraken Hatchling"; // 0/4
private static final String centaur = "Centaur Courser"; // 3/3
private static final String acolyte = "Acolyte of Xathrid"; // 0/1

@Test
public void testTradeAttacking() {
addCard(Zone.BATTLEFIELD, playerA, ooze);
addCard(Zone.BATTLEFIELD, playerB, wizard);
addCard(Zone.BATTLEFIELD, playerB, sword);
addCard(Zone.BATTLEFIELD, playerB, "Wastes");

activateAbility(2, PhaseStep.PRECOMBAT_MAIN, playerB, "Equip", wizard);

attack(3, playerA, ooze, playerB);
block(3, playerB, wizard, ooze);

setStrictChooseMode(true);
setStopAt(3, PhaseStep.END_TURN);
execute();

assertGraveyardCount(playerA, ooze, 1);
assertGraveyardCount(playerB, wizard, 1);
assertGraveyardCount(playerB, sword, 1);
}

@Test
public void testTradeBlocking() {
addCard(Zone.BATTLEFIELD, playerA, ooze);
addCard(Zone.BATTLEFIELD, playerB, wizard);
addCard(Zone.BATTLEFIELD, playerB, sword);
addCard(Zone.BATTLEFIELD, playerB, "Wastes");

activateAbility(2, PhaseStep.PRECOMBAT_MAIN, playerB, "Equip", wizard);

attack(2, playerB, wizard, playerA);
block(2, playerA, ooze, wizard);

setStrictChooseMode(true);
setStopAt(2, PhaseStep.END_TURN);
execute();

assertGraveyardCount(playerA, ooze, 1);
assertGraveyardCount(playerB, wizard, 1);
assertGraveyardCount(playerB, sword, 1);
}

@Test
public void testBounceAttacking() {
addCard(Zone.BATTLEFIELD, playerA, ooze);
addCard(Zone.BATTLEFIELD, playerB, kraken);
addCard(Zone.BATTLEFIELD, playerB, sword);
addCard(Zone.BATTLEFIELD, playerB, "Wastes");

activateAbility(2, PhaseStep.PRECOMBAT_MAIN, playerB, "Equip", kraken);

attack(3, playerA, ooze, playerB);
block(3, playerB, kraken, ooze);

setStrictChooseMode(true);
setStopAt(3, PhaseStep.END_TURN);
execute();

assertPermanentCount(playerA, ooze, 1);
assertPermanentCount(playerB, kraken, 1);
assertGraveyardCount(playerB, sword, 1);
}

@Test
public void testBounceBlocking() {
addCard(Zone.BATTLEFIELD, playerA, ooze);
addCard(Zone.BATTLEFIELD, playerB, kraken);
addCard(Zone.BATTLEFIELD, playerB, sword);
addCard(Zone.BATTLEFIELD, playerB, "Wastes");

activateAbility(2, PhaseStep.PRECOMBAT_MAIN, playerB, "Equip", kraken);

attack(2, playerB, kraken, playerA);
block(2, playerA, ooze, kraken);

setStrictChooseMode(true);
setStopAt(2, PhaseStep.END_TURN);
execute();

assertPermanentCount(playerA, ooze, 1);
assertPermanentCount(playerB, kraken, 1);
assertGraveyardCount(playerB, sword, 1);
}

@Test
public void testEatenAttacking() {
addCard(Zone.BATTLEFIELD, playerA, ooze);
addCard(Zone.BATTLEFIELD, playerB, centaur);
addCard(Zone.BATTLEFIELD, playerB, sword);
addCard(Zone.BATTLEFIELD, playerB, "Wastes");

activateAbility(2, PhaseStep.PRECOMBAT_MAIN, playerB, "Equip", centaur);

attack(3, playerA, ooze, playerB);
block(3, playerB, centaur, ooze);

setStrictChooseMode(true);
setStopAt(3, PhaseStep.END_TURN);
execute();

assertGraveyardCount(playerA, ooze, 1);
assertPermanentCount(playerB, centaur, 1);
assertGraveyardCount(playerB, sword, 1);
}

@Test
public void testEatenBlocking() {
addCard(Zone.BATTLEFIELD, playerA, ooze);
addCard(Zone.BATTLEFIELD, playerB, centaur);
addCard(Zone.BATTLEFIELD, playerB, sword);
addCard(Zone.BATTLEFIELD, playerB, "Wastes");

activateAbility(2, PhaseStep.PRECOMBAT_MAIN, playerB, "Equip", centaur);

attack(2, playerB, centaur, playerA);
block(2, playerA, ooze, centaur);

setStrictChooseMode(true);
setStopAt(2, PhaseStep.END_TURN);
execute();

assertGraveyardCount(playerA, ooze, 1);
assertPermanentCount(playerB, centaur, 1);
assertGraveyardCount(playerB, sword, 1);
}

@Test
public void testEatAttacking() {
addCard(Zone.BATTLEFIELD, playerA, ooze);
addCard(Zone.BATTLEFIELD, playerB, acolyte);
addCard(Zone.BATTLEFIELD, playerB, sword);
addCard(Zone.BATTLEFIELD, playerB, "Wastes");

activateAbility(2, PhaseStep.PRECOMBAT_MAIN, playerB, "Equip", acolyte);

attack(3, playerA, ooze, playerB);
block(3, playerB, acolyte, ooze);

setStrictChooseMode(true);
setStopAt(3, PhaseStep.END_TURN);
execute();

assertPermanentCount(playerA, ooze, 1);
assertGraveyardCount(playerB, acolyte, 1);
assertGraveyardCount(playerB, sword, 1);
}

@Test
public void testEatBlocking() {
addCard(Zone.BATTLEFIELD, playerA, ooze);
addCard(Zone.BATTLEFIELD, playerB, acolyte);
addCard(Zone.BATTLEFIELD, playerB, sword);
addCard(Zone.BATTLEFIELD, playerB, "Wastes");

activateAbility(2, PhaseStep.PRECOMBAT_MAIN, playerB, "Equip", acolyte);

attack(2, playerB, acolyte, playerA);
block(2, playerA, ooze, acolyte);

setStrictChooseMode(true);
setStopAt(2, PhaseStep.END_TURN);
execute();

assertPermanentCount(playerA, ooze, 1);
assertGraveyardCount(playerB, acolyte, 1);
assertGraveyardCount(playerB, sword, 1);
}

}

0 comments on commit 77b8030

Please sign in to comment.