forge.game.zone.PlayerZone.java Source code

Java tutorial

Introduction

Here is the source code for forge.game.zone.PlayerZone.java

Source

/*
 * Forge: Play Magic: the Gathering.
 * Copyright (C) 2011  Forge Team
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package forge.game.zone;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import forge.game.card.Card;
import forge.game.card.CardCollectionView;
import forge.game.card.CardLists;
import forge.game.player.Player;
import forge.game.spellability.SpellAbility;
import forge.util.Lang;

/**
 * <p>
 * DefaultPlayerZone class.
 * </p>
 * 
 * @author Forge
 * @version $Id: PlayerZone.java 27901 2014-10-13 01:17:02Z drdev $
 */
public class PlayerZone extends Zone {
    private static final long serialVersionUID = -5687652485777639176L;

    // the this is not the owner of the card
    private final class AlienCardsActivationFilter implements Predicate<Card> {
        @Override
        public boolean apply(final Card c) {
            if (c.hasStartOfKeyword("May be played by your opponent")
                    || c.hasKeyword("Your opponent may look at this card.")) {
                return true;
            }
            return false;
        }
    }

    private final class OwnCardsActivationFilter implements Predicate<Card> {
        @Override
        public boolean apply(final Card c) {
            if (c.hasKeyword("You may look at this card.")) {
                return true;
            }

            if (c.isLand() && (c.hasKeyword("May be played")
                    || c.hasKeyword("May be played without paying its mana cost"))) {
                return true;
            }

            for (final SpellAbility sa : c.getSpellAbilities()) {
                final ZoneType restrictZone = sa.getRestrictions().getZone();
                if (PlayerZone.this.is(restrictZone)) {
                    return true;
                }

                if (sa.isSpell()
                        && (c.hasKeyword("May be played")
                                || c.hasKeyword("May be played without paying its mana cost")
                                || (c.hasStartOfKeyword("Flashback") && PlayerZone.this.is(ZoneType.Graveyard)))
                        && restrictZone.equals(ZoneType.Hand)) {
                    return true;
                }
            }
            return false;
        }
    }

    private final Player player;

    public PlayerZone(final ZoneType zone, final Player inPlayer) {
        super(zone, inPlayer.getGame());
        player = inPlayer;
    }

    @Override
    protected void onChanged() {
        player.updateZoneForView(this);
    }

    public final Player getPlayer() {
        return player;
    }

    @Override
    public final String toString() {
        return String.format("%s %s", Lang.getPossesive(player.toString()), zoneType);
    }

    public CardCollectionView getCardsPlayerCanActivate(Player who) {
        CardCollectionView cl = getCards(false);
        boolean checkingForOwner = who == player;

        if (checkingForOwner && (is(ZoneType.Battlefield) || is(ZoneType.Hand))) {
            return cl;
        }

        // Only check the top card of the library
        Iterable<Card> cards = cl;
        if (is(ZoneType.Library)) {
            cards = Iterables.limit(cards, 1);
        }

        final Predicate<Card> filterPredicate = checkingForOwner ? new OwnCardsActivationFilter()
                : new AlienCardsActivationFilter();
        return CardLists.filter(cl, filterPredicate);
    }
}