Cheat engine: fixed that it doesn't init effects and other staff from battlefield permanents (#6888);

This commit is contained in:
Oleg Agafonov 2020-07-27 20:46:21 +04:00
parent e5529d5835
commit 96155ec799
4 changed files with 86 additions and 55 deletions

View file

@ -22,6 +22,7 @@ import mage.game.command.CommandObject;
import mage.game.command.Plane;
import mage.game.permanent.Permanent;
import mage.players.Player;
import mage.util.CardUtil;
import mage.util.RandomUtil;
import java.io.File;
@ -572,7 +573,7 @@ public final class SystemUtil {
// move card from exile to stack
for (Card card : cardsToLoad) {
swapWithAnyCard(game, player, card, Zone.STACK);
putCardToZone(game, player, card, Zone.STACK);
}
continue;
@ -647,7 +648,7 @@ public final class SystemUtil {
} else {
// as other card
for (Card card : cardsToLoad) {
swapWithAnyCard(game, player, card, gameZone);
putCardToZone(game, player, card, gameZone);
}
}
}
@ -657,38 +658,33 @@ public final class SystemUtil {
}
/**
* Swap cards between specified card from library and any hand card.
*
* @param game
* @param card Card to put to player's hand
* Put card to specified (battlefield zone will be tranformed to permanent with initialized effects)
*/
private static void swapWithAnyCard(Game game, Player player, Card card, Zone zone) {
// Put the card in Exile to start. Otherwise the game doesn't know where to remove the card from.
game.getExile().getPermanentExile().add(card);
game.setZone(card.getId(), Zone.EXILED);
private static void putCardToZone(Game game, Player player, Card card, Zone zone) {
// TODO: replace by player.move?
switch (zone) {
case BATTLEFIELD:
card.putOntoBattlefield(game, Zone.EXILED, null, player.getId());
CardUtil.putCardOntoBattlefieldWithEffects(game, card, player);
break;
case LIBRARY:
card.setZone(Zone.LIBRARY, game);
game.getExile().getPermanentExile().remove(card);
player.getLibrary().putOnTop(card, game);
break;
case STACK:
card.cast(game, Zone.EXILED, card.getSpellAbility(), player.getId());
card.cast(game, game.getState().getZone(card.getId()), card.getSpellAbility(), player.getId());
break;
case EXILED:
// nothing to do
card.setZone(Zone.EXILED, game);
game.getExile().getPermanentExile().add(card);
break;
case OUTSIDE:
card.setZone(Zone.OUTSIDE, game);
game.getExile().getPermanentExile().remove(card);
break;
default:
card.moveToZone(zone, null, game, false);
break;
}
game.applyEffects();
logger.info("Added card to player's " + zone.toString() + ": " + card.getName() + ", player = " + player.getName());
}

View file

@ -1,9 +1,5 @@
package mage.game;
import java.io.IOException;
import java.io.Serializable;
import java.util.*;
import java.util.Map.Entry;
import mage.MageException;
import mage.MageObject;
import mage.abilities.*;
@ -64,6 +60,7 @@ import mage.target.Target;
import mage.target.TargetCard;
import mage.target.TargetPermanent;
import mage.target.TargetPlayer;
import mage.util.CardUtil;
import mage.util.GameLog;
import mage.util.MessageToClient;
import mage.util.RandomUtil;
@ -71,6 +68,11 @@ import mage.util.functions.ApplyToPermanent;
import mage.watchers.common.*;
import org.apache.log4j.Logger;
import java.io.IOException;
import java.io.Serializable;
import java.util.*;
import java.util.Map.Entry;
public abstract class GameImpl implements Game, Serializable {
private static final int ROLLBACK_TURNS_MAX = 4;
@ -3063,28 +3065,10 @@ public abstract class GameImpl implements Game, Serializable {
throw new IllegalArgumentException("Command zone supports in commander test games");
}
// warning, permanents go to battlefield without resolve, continuus effects must be init
for (PermanentCard permanentCard : battlefield) {
permanentCard.setZone(Zone.BATTLEFIELD, this);
permanentCard.setOwnerId(ownerId);
PermanentCard newPermanent = new PermanentCard(permanentCard.getCard(), ownerId, this);
getPermanentsEntering().put(newPermanent.getId(), newPermanent);
newPermanent.entersBattlefield(newPermanent.getId(), this, Zone.OUTSIDE, false);
addPermanent(newPermanent, getState().getNextPermanentOrderNumber());
getPermanentsEntering().remove(newPermanent.getId());
newPermanent.removeSummoningSickness();
if (permanentCard.isTapped()) {
newPermanent.setTapped(true);
CardUtil.putCardOntoBattlefieldWithEffects(this, permanentCard, player);
}
// init effects on static abilities (init continuous effects, warning, game state contains copy)
for (ContinuousEffect effect : this.getState().getContinuousEffects().getLayeredEffects(this)) {
Optional<Ability> ability = this.getState().getContinuousEffects().getLayeredEffectAbilities(effect).stream().findFirst();
if (ability.isPresent() && newPermanent.getId().equals(ability.get().getSourceId())) {
effect.init(ability.get(), this, activePlayerId); // game is not setup yet, game.activePlayer is null -- need direct id
}
}
}
applyEffects();
}
}

View file

@ -1,6 +1,5 @@
package mage.game;
import java.util.*;
import mage.cards.Card;
import mage.cards.Cards;
import mage.cards.CardsImpl;
@ -19,6 +18,8 @@ import mage.game.stack.Spell;
import mage.players.Player;
import mage.target.TargetCard;
import java.util.*;
/**
* Created by samuelsandeen on 9/6/16.
*/
@ -233,6 +234,7 @@ public final class ZonesHandler {
if (!game.replaceEvent(event)) {
Zone fromZone = event.getFromZone();
if (event.getToZone() == Zone.BATTLEFIELD) {
// prepare card and permanent
// If needed take attributes from the spell (e.g. color of spell was changed)
card = takeAttributesFromSpell(card, event, game);
// controlling player can be replaced so use event player now
@ -245,15 +247,18 @@ public final class ZonesHandler {
} else {
permanent = new PermanentCard(card, event.getPlayerId(), game);
}
// put onto battlefield with possible counters
game.getPermanentsEntering().put(permanent.getId(), permanent);
card.checkForCountersToAdd(permanent, game);
permanent.setTapped(
info instanceof ZoneChangeInfo.Battlefield && ((ZoneChangeInfo.Battlefield) info).tapped);
permanent.setFaceDown(info.faceDown, game);
permanent.setTapped(info instanceof ZoneChangeInfo.Battlefield
&& ((ZoneChangeInfo.Battlefield) info).tapped);
permanent.setFaceDown(info.faceDown, game);
if (info.faceDown) {
card.setFaceDown(false, game);
}
// make sure the controller of all continuous effects of this card are switched to the current controller
game.setScopeRelevant(true);
game.getContinuousEffects().setController(permanent.getId(), permanent.getControllerId());

View file

@ -8,18 +8,20 @@ import mage.abilities.Mode;
import mage.abilities.SpellAbility;
import mage.abilities.costs.VariableCost;
import mage.abilities.costs.mana.*;
import mage.abilities.effects.ContinuousEffect;
import mage.cards.Card;
import mage.constants.ColoredManaSymbol;
import mage.constants.EmptyNames;
import mage.constants.ManaType;
import mage.constants.SpellAbilityType;
import mage.cards.MeldCard;
import mage.constants.*;
import mage.filter.Filter;
import mage.filter.predicate.mageobject.NamePredicate;
import mage.game.CardState;
import mage.game.Game;
import mage.game.permanent.Permanent;
import mage.game.permanent.PermanentCard;
import mage.game.permanent.PermanentMeld;
import mage.game.permanent.token.Token;
import mage.game.stack.Spell;
import mage.players.Player;
import mage.target.Target;
import mage.util.functions.CopyTokenFunction;
@ -829,4 +831,48 @@ public final class CardUtil {
.flatMap(Collection::stream)
.collect(Collectors.toSet());
}
/**
* Put card to battlefield without resolve
*
* @param game
* @param card
* @param player
*/
public static void putCardOntoBattlefieldWithEffects(Game game, Card card, Player player) {
// same logic as ZonesHandler->maybeRemoveFromSourceZone
// prepare card and permanent
card.setZone(Zone.BATTLEFIELD, game);
card.setOwnerId(player.getId());
PermanentCard permanent;
if (card instanceof MeldCard) {
permanent = new PermanentMeld(card, player.getId(), game);
} else {
permanent = new PermanentCard(card, player.getId(), game);
}
// put onto battlefield with possible counters
game.getPermanentsEntering().put(permanent.getId(), permanent);
card.checkForCountersToAdd(permanent, game);
permanent.entersBattlefield(permanent.getId(), game, Zone.OUTSIDE, false);
game.addPermanent(permanent, game.getState().getNextPermanentOrderNumber());
game.getPermanentsEntering().remove(permanent.getId());
// workaround for special tapped status from test framework's command (addCard)
if (card instanceof PermanentCard && ((PermanentCard) card).isTapped()) {
permanent.setTapped(true);
}
// remove sickness
permanent.removeSummoningSickness();
// init effects on static abilities (init continuous effects; warning, game state contains copy)
for (ContinuousEffect effect : game.getState().getContinuousEffects().getLayeredEffects(game)) {
Optional<Ability> ability = game.getState().getContinuousEffects().getLayeredEffectAbilities(effect).stream().findFirst();
if (ability.isPresent() && permanent.getId().equals(ability.get().getSourceId())) {
effect.init(ability.get(), game, player.getId());
}
}
}
}