use of mage.game.permanent.PermanentCard in project mage by magefree.
the class CustomTestCard method parseLine.
private void parseLine(String line) {
if (parserState == ParserState.EXPECTED) {
// just remember for future use
expectedResults.add(line);
return;
}
Matcher m = pattern.matcher(line);
if (m.matches()) {
String zone = m.group(1);
String nickname = m.group(2);
if (nickname.startsWith("Computer")) {
List<Card> cards = null;
List<PermanentCard> perms = null;
Zone gameZone;
if ("hand".equalsIgnoreCase(zone)) {
gameZone = Zone.HAND;
cards = getHandCards(getPlayer(nickname));
} else if ("battlefield".equalsIgnoreCase(zone)) {
gameZone = Zone.BATTLEFIELD;
perms = getBattlefieldCards(getPlayer(nickname));
} else if ("graveyard".equalsIgnoreCase(zone)) {
gameZone = Zone.GRAVEYARD;
cards = getGraveCards(getPlayer(nickname));
} else if ("library".equalsIgnoreCase(zone)) {
gameZone = Zone.LIBRARY;
cards = getLibraryCards(getPlayer(nickname));
} else if ("command".equalsIgnoreCase(zone)) {
gameZone = Zone.COMMAND;
cards = getCommandCards(getPlayer(nickname));
} else if ("player".equalsIgnoreCase(zone)) {
String command = m.group(3);
if ("life".equals(command)) {
getCommands(getPlayer(nickname)).put(Zone.OUTSIDE, "life:" + m.group(4));
}
return;
} else {
// go parse next line
return;
}
String cardName = m.group(3);
Integer amount = Integer.parseInt(m.group(4));
boolean tapped = m.group(5) != null && m.group(5).equals(":{tapped}");
if (cardName.equals("clear")) {
getCommands(getPlayer(nickname)).put(gameZone, "clear");
} else {
for (int i = 0; i < amount; i++) {
CardInfo cardInfo = CardRepository.instance.findCard(cardName);
Card newCard = cardInfo != null ? cardInfo.getCard() : null;
if (newCard != null) {
if (gameZone == Zone.BATTLEFIELD) {
Card permCard = CardUtil.getDefaultCardSideForBattlefield(currentGame, newCard);
PermanentCard p = new PermanentCard(permCard, null, currentGame);
p.setTapped(tapped);
perms.add(p);
} else {
cards.add(newCard);
}
} else {
logger.fatal("Couldn't find a card: " + cardName);
logger.fatal("line: " + line);
}
}
}
} else {
logger.warn("Unknown player: " + nickname);
}
} else {
logger.warn("Init string wasn't parsed: " + line);
}
}
use of mage.game.permanent.PermanentCard in project mage by magefree.
the class SerializationTest method test_PermanentImpl_MarkedDamageInfo.
@Test
public void test_PermanentImpl_MarkedDamageInfo() {
CardInfo cardInfo = CardRepository.instance.findCard("Balduvian Bears");
Card newCard = cardInfo.getCard();
Card permCard = CardUtil.getDefaultCardSideForBattlefield(currentGame, newCard);
PermanentImpl permanent = new PermanentCard(permCard, playerA.getId(), currentGame);
currentGame.addPermanent(permanent, 0);
// mark damage from infected ability
permanent.addAbility(InfectAbility.getInstance(), null, currentGame);
permanent.markDamage(1, permanent.getId(), null, currentGame, false, false);
// test compress (it uses default java serialization)
Object compressed = CompressUtil.compress(permanent);
Assert.assertTrue("Must be zip", compressed instanceof ZippedObjectImpl);
PermanentImpl uncompressed = (PermanentImpl) CompressUtil.decompress(compressed);
Assert.assertEquals("Must be same", permanent.getName(), uncompressed.getName());
// ensure that it was marked damage
permanent.applyDamage(currentGame);
Assert.assertEquals("Must get infected counter", 1, permanent.getCounters(currentGame).getCount(CounterType.M1M1));
}
use of mage.game.permanent.PermanentCard in project mage by magefree.
the class SerializationTest method processSingleCard.
private void processSingleCard(CardInfo cardInfo) {
// compress each card's part
Card newCard = cardInfo.getCard();
CardUtil.getObjectPartsAsObjects(newCard).stream().map(Card.class::cast).forEach(card -> {
Card testCard = CardUtil.getDefaultCardSideForBattlefield(currentGame, newCard);
Card testPermanent = null;
if (!testCard.isInstantOrSorcery()) {
testPermanent = new PermanentCard(testCard, playerA.getId(), currentGame);
}
// card
{
Object compressed = CompressUtil.compress(testCard);
Assert.assertTrue("Must be zip", compressed instanceof ZippedObjectImpl);
Card uncompressed = (Card) CompressUtil.decompress(compressed);
Assert.assertEquals("Must be same", testCard.getName(), uncompressed.getName());
}
// permanent
if (testPermanent != null) {
Object compressed = CompressUtil.compress(testPermanent);
Assert.assertTrue("Must be zip", compressed instanceof ZippedObjectImpl);
Card uncompressed = (Card) CompressUtil.decompress(compressed);
Assert.assertEquals("Must be same", testPermanent.getName(), uncompressed.getName());
}
});
}
use of mage.game.permanent.PermanentCard in project mage by magefree.
the class CardImpl method addAbility.
/**
* Public in order to support adding abilities to SplitCardHalf's
*
* @param ability
*/
@Override
public void addAbility(Ability ability) {
ability.setSourceId(this.getId());
abilities.add(ability);
abilities.addAll(ability.getSubAbilities());
// reason: triggered abilities are not processing here
if (this instanceof PermanentCard) {
throw new IllegalArgumentException("Wrong code usage. Don't use that method for permanents, use permanent.addAbility(a, source, game) instead.");
}
// verify test will catch that errors
if (ability instanceof ActivatedManaAbilityImpl) {
ActivatedManaAbilityImpl manaAbility = (ActivatedManaAbilityImpl) ability;
String rule = manaAbility.getRule().toLowerCase(Locale.ENGLISH);
if (manaAbility.getEffects().stream().anyMatch(e -> e.getOutcome().equals(Outcome.DrawCard)) || rule.contains("reveal ") || rule.contains("draw ")) {
if (manaAbility.isUndoPossible()) {
throw new IllegalArgumentException("Ability contains draw/reveal effect, but isUndoPossible is true. Ability: " + ability.getClass().getSimpleName() + "; " + ability.getRule());
}
}
}
}
use of mage.game.permanent.PermanentCard in project mage by magefree.
the class AuraReplacementEffect method replaceEvent.
@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
Zone fromZone = ((ZoneChangeEvent) event).getFromZone();
Card card = game.getCard(event.getTargetId());
if (card == null) {
return false;
}
Card firstCardFace = null;
if (game.getState().getValue(TransformAbility.VALUE_KEY_ENTER_TRANSFORMED + card.getId()) != null) {
firstCardFace = card;
card = card.getSecondCardFace();
if (!card.isEnchantment(game) || !card.hasSubtype(SubType.AURA, game)) {
return false;
}
}
// Aura cards that go to battlefield face down (Manifest) don't have to select targets
if (card.isFaceDown(game)) {
return false;
}
// Aura enters the battlefield attached
Object object = game.getState().getValue("attachTo:" + card.getId());
if (object != null) {
if (object instanceof Permanent) {
// Aura is attached to a permanent on the battlefield
return false;
}
if (object instanceof UUID) {
Player player = game.getPlayer((UUID) object);
if (player != null) {
// Aura is attached to a player
return false;
}
}
}
UUID targetId = null;
MageObject sourceObject = game.getObject(event.getSourceId());
boolean enchantCardInGraveyard = false;
if (sourceObject instanceof StackAbility) {
StackAbility stackAbility = (StackAbility) sourceObject;
if (!stackAbility.getEffects().isEmpty()) {
targetId = stackAbility.getEffects().get(0).getTargetPointer().getFirst(game, stackAbility);
}
}
// So continuousEffects are removed if previous effect of the same ability did move objects that cause continuous effects
game.applyEffects();
Player controllingPlayer = null;
if (targetId == null) {
SpellAbility spellAbility = card.getSpellAbility();
if (spellAbility.getTargets().isEmpty()) {
for (Ability ability : card.getAbilities(game)) {
if ((ability instanceof SpellAbility) && SpellAbilityType.BASE_ALTERNATE == ((SpellAbility) ability).getSpellAbilityType() && !ability.getTargets().isEmpty()) {
spellAbility = (SpellAbility) ability;
break;
}
}
}
if (spellAbility.getTargets().isEmpty()) {
return false;
}
Target target = spellAbility.getTargets().get(0).copy();
Outcome auraOutcome = Outcome.BoostCreature;
for (Effect effect : spellAbility.getEffects()) {
if (effect instanceof AttachEffect) {
auraOutcome = effect.getOutcome();
break;
}
}
enchantCardInGraveyard = target instanceof TargetCardInGraveyard;
if (target != null) {
// always not target because this way it's not handled targeted
target.setNotTarget(true);
// necessary if e.g. aura is blinked multiple times
target.clearChosen();
}
if (event.getPlayerId() != null) {
controllingPlayer = game.getPlayer(event.getPlayerId());
} else {
controllingPlayer = game.getPlayer(card.getOwnerId());
}
if (target != null && controllingPlayer != null && controllingPlayer.choose(auraOutcome, target, card.getId(), game)) {
targetId = target.getFirstTarget();
}
}
Card targetCard = null;
Permanent targetPermanent = null;
if (enchantCardInGraveyard) {
targetCard = game.getCard(targetId);
} else {
targetPermanent = game.getPermanent(targetId);
}
Player targetPlayer = game.getPlayer(targetId);
if (targetCard != null || targetPermanent != null || targetPlayer != null) {
if (firstCardFace != null) {
// transforming card. remove first face (original card) from old zone
firstCardFace.removeFromZone(game, fromZone, source);
} else {
card.removeFromZone(game, fromZone, source);
}
PermanentCard permanent = new PermanentCard(card, (controllingPlayer == null ? card.getOwnerId() : controllingPlayer.getId()), game);
ZoneChangeEvent zoneChangeEvent = new ZoneChangeEvent(permanent, event.getPlayerId(), fromZone, Zone.BATTLEFIELD);
permanent.updateZoneChangeCounter(game, zoneChangeEvent);
game.addPermanent(permanent, 0);
card.setZone(Zone.BATTLEFIELD, game);
if (permanent.entersBattlefield(source, game, fromZone, true)) {
String attachToName = null;
if (targetCard != null) {
permanent.attachTo(targetCard.getId(), source, game);
attachToName = targetCard.getLogName();
} else if (targetPermanent != null) {
targetPermanent.addAttachment(permanent.getId(), source, game);
attachToName = targetPermanent.getLogName();
} else if (targetPlayer != null) {
targetPlayer.addAttachment(permanent.getId(), source, game);
attachToName = targetPlayer.getLogName();
}
game.applyEffects();
game.fireEvent(zoneChangeEvent);
if (!game.isSimulation()) {
if (controllingPlayer != null && fromZone != null && permanent != null) {
game.informPlayers(controllingPlayer.getLogName() + " puts " + (card.getLogName()) + " from " + fromZone.toString().toLowerCase(Locale.ENGLISH) + " onto the Battlefield attached to " + attachToName);
}
}
return true;
}
}
return false;
}
Aggregations