Package mage.game

Source Code of mage.game.GameState

/*
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
*    1. Redistributions of source code must retain the above copyright notice, this list of
*       conditions and the following disclaimer.
*
*    2. Redistributions in binary form must reproduce the above copyright notice, this list
*       of conditions and the following disclaimer in the documentation and/or other materials
*       provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com.
*/

package mage.game;

import mage.MageObject;
import mage.abilities.*;
import mage.abilities.effects.ContinuousEffect;
import mage.abilities.effects.ContinuousEffects;
import mage.abilities.effects.Effect;
import mage.cards.Card;
import mage.cards.SplitCard;
import mage.choices.Choice;
import mage.constants.Zone;
import mage.game.combat.Combat;
import mage.game.combat.CombatGroup;
import mage.game.command.Command;
import mage.game.command.CommandObject;
import mage.game.events.GameEvent;
import mage.game.permanent.Battlefield;
import mage.game.permanent.Permanent;
import mage.game.stack.SpellStack;
import mage.game.stack.StackObject;
import mage.game.turn.Turn;
import mage.game.turn.TurnMods;
import mage.players.Player;
import mage.players.PlayerList;
import mage.players.Players;
import mage.target.Target;
import mage.util.Copyable;
import mage.watchers.Watcher;
import mage.watchers.Watchers;

import java.io.Serializable;
import java.util.*;

/**
*
* @author BetaSteward_at_googlemail.com
*
* since at any time the game state may be copied and restored you cannot
* rely on any object maintaining it's instance
* it then becomes necessary to only refer to objects by their ids since
* these will always remain constant throughout its lifetime
*
*/
public class GameState implements Serializable, Copyable<GameState> {

    private final Players players;
    private final PlayerList playerList;
    private final Turn turn;
    // revealed cards <Name, <Cards>>, will be reset if all players pass priority
    private final Revealed revealed;
    private final Map<UUID, LookedAt> lookedAt = new HashMap<>();
    private final DelayedTriggeredAbilities delayed;
    private final SpecialActions specialActions;
    private final Map<UUID, Abilities<Ability>> otherAbilities = new HashMap<>();
    private final TurnMods turnMods;
    private final Watchers watchers;
   

    private UUID activePlayerId;
    private UUID priorityPlayerId;
    private SpellStack stack;
    private Command command;
    private Exile exile;
    private Battlefield battlefield;
    private int turnNum = 1;
    private int stepNum = 0;
    private UUID turnId = null;
    private boolean extraTurn = false;
    private boolean legendaryRuleActive = true;
    private boolean gameOver;
    private boolean paused;
    private ContinuousEffects effects;
    private TriggeredAbilities triggers;
    private List<TriggeredAbility> triggered = new ArrayList<>();
    private Combat combat;
    private Map<String, Object> values = new HashMap<>();
    private Map<UUID, Zone> zones = new HashMap<>();
    private List<GameEvent> simultaneousEvents = new ArrayList<>();

    public GameState() {
        players = new Players();
        playerList = new PlayerList();
        turn = new Turn();
        stack = new SpellStack();
        command = new Command();
        exile = new Exile();
        revealed = new Revealed();
        battlefield = new Battlefield();
        effects = new ContinuousEffects();
        triggers = new TriggeredAbilities();
        delayed = new DelayedTriggeredAbilities();
        specialActions = new SpecialActions();
        combat = new Combat();
        turnMods = new TurnMods();
        watchers = new Watchers();
    }

    public GameState(final GameState state) {
        this.players = state.players.copy();
        this.playerList = state.playerList.copy();
        this.activePlayerId = state.activePlayerId;
        this.priorityPlayerId = state.priorityPlayerId;
        this.turn = state.turn.copy();
        this.stack = state.stack.copy();
        this.command = state.command.copy();
        this.exile = state.exile.copy();
        this.revealed = state.revealed.copy();
        this.lookedAt.putAll(state.lookedAt);
        this.battlefield = state.battlefield.copy();
        this.turnNum = state.turnNum;
        this.stepNum = state.stepNum;
        this.extraTurn = state.extraTurn;
        this.legendaryRuleActive = state.legendaryRuleActive;
        this.gameOver = state.gameOver;
        this.effects = state.effects.copy();
        for (TriggeredAbility trigger: state.triggered) {
            this.triggered.add(trigger.copy());
        }
        this.triggers = state.triggers.copy();
        this.delayed = state.delayed.copy();
        this.specialActions = state.specialActions.copy();
        this.combat = state.combat.copy();
        this.turnMods = state.turnMods.copy();
        this.watchers = state.watchers.copy();
        for (Map.Entry<String, Object> entry: state.values.entrySet()) {
            if (entry.getValue() instanceof Boolean) { // AI changed values of Boolean for cards like Wall of Roots TODO: copy other types than Boolean
                this.values.put(entry.getKey(), Boolean.valueOf(((Boolean)entry.getValue()).toString()));
            } else {
                this.values.put(entry.getKey(), entry.getValue());
            }

        }
        this.zones.putAll(state.zones);
        for (Map.Entry<UUID, Abilities<Ability>> entry: state.otherAbilities.entrySet()) {
            otherAbilities.put(entry.getKey(), entry.getValue().copy());
        }
        this.paused = state.paused;
        this.simultaneousEvents.addAll(state.simultaneousEvents);
    }

    @Override
    public GameState copy() {
        return new GameState(this);
    }

    public void addPlayer(Player player) {
        players.put(player.getId(), player);
        playerList.add(player.getId());
    }

    public String getValue(boolean useHidden) {
        StringBuilder sb = new StringBuilder(1024);

        sb.append(turnNum).append(turn.getPhaseType()).append(turn.getStepType()).append(activePlayerId).append(priorityPlayerId);

        for (Player player: players.values()) {
            sb.append("player").append(player.getLife()).append("hand");
            if (useHidden) {
                sb.append(player.getHand());
            }
            else {
                sb.append(player.getHand().size());
            }
            sb.append("library").append(player.getLibrary().size()).append("graveyard").append(player.getGraveyard());
        }

        sb.append("permanents");
        for (Permanent permanent: battlefield.getAllPermanents()) {
            sb.append(permanent.getValue());
        }

        sb.append("spells");
        for (StackObject spell: stack) {
            sb.append(spell.getControllerId()).append(spell.getName());
        }

        for (ExileZone zone: exile.getExileZones()) {
            sb.append("exile").append(zone.getName()).append(zone);
        }

        sb.append("combat");
        for (CombatGroup group: combat.getGroups()) {
            sb.append(group.getDefenderId()).append(group.getAttackers()).append(group.getBlockers());
        }

        return sb.toString();
    }

    public String getValue(boolean useHidden, Game game) {
        StringBuilder sb = new StringBuilder(1024);

        sb.append(turnNum).append(turn.getPhaseType()).append(turn.getStepType()).append(activePlayerId).append(priorityPlayerId);

        for (Player player: players.values()) {
            sb.append("player").append(player.isPassed()).append(player.getLife()).append("hand");
            if (useHidden) {
                sb.append(player.getHand());
            }
            else {
                sb.append(player.getHand().size());
            }
            sb.append("library").append(player.getLibrary().size());
            sb.append("graveyard");
            for (Card card: player.getGraveyard().getCards(game)) {
                sb.append(card.getName());
            }
        }

        sb.append("permanents");
        List<String> perms = new ArrayList<>();
        for (Permanent permanent: battlefield.getAllPermanents()) {
            perms.add(permanent.getValue());
        }
        Collections.sort(perms);
        sb.append(perms);

        sb.append("spells");
        for (StackObject spell: stack) {
            sb.append(spell.getControllerId()).append(spell.getName());
            sb.append(spell.getStackAbility().toString());
            for (Mode mode: spell.getStackAbility().getModes().values()) {
                if (!mode.getTargets().isEmpty()) {
                    sb.append("targets");
                    for (Target target: mode.getTargets()) {
                        sb.append(target.getTargets());
                    }
                }
                if (!mode.getChoices().isEmpty()) {
                    sb.append("choices");
                    for (Choice choice: mode.getChoices()) {
                        sb.append(choice.getChoice());
                    }
                }
            }
        }

        for (ExileZone zone: exile.getExileZones()) {
            sb.append("exile").append(zone.getName()).append(zone);
        }

        sb.append("combat");
        for (CombatGroup group: combat.getGroups()) {
            sb.append(group.getDefenderId()).append(group.getAttackers()).append(group.getBlockers());
        }

        return sb.toString();
    }

    public Players getPlayers() {
        return players;
    }

    public Player getPlayer(UUID playerId) {
        return players.get(playerId);
    }

    public UUID getActivePlayerId() {
        return activePlayerId;
    }

    public void setActivePlayerId(UUID activePlayerId) {
        this.activePlayerId = activePlayerId;
    }

    public UUID getPriorityPlayerId() {
        return priorityPlayerId;
    }

    public void setPriorityPlayerId(UUID priorityPlayerId) {
        this.priorityPlayerId = priorityPlayerId;
    }

    public Battlefield getBattlefield() {
        return this.battlefield;
    }

    public SpellStack getStack() {
        return this.stack;
    }

    public Exile getExile() {
        return exile;
    }

    public Command getCommand() {
        return command;
    }

    public Revealed getRevealed() {
        return revealed;
    }

    public LookedAt getLookedAt(UUID playerId) {
        if (lookedAt.get(playerId) == null) {
            LookedAt lookedAtCards = new LookedAt();
            lookedAt.put(playerId, lookedAtCards);
            return lookedAtCards;
        }
        return lookedAt.get(playerId);
    }

    public void clearLookedAt(UUID playerId) {
        lookedAt.remove(playerId);
    }

    public Turn getTurn() {
        return turn;
    }

    public Combat getCombat() {
        return combat;
    }

    /**
     * Gets the game step counter. This counter isgoing one up for
     * every played step during the game.
     * @return
     */
    public int getStepNum() {
        return stepNum;
    }

    public void increaseStepNum() {
        this.stepNum++;
    }

    public int getTurnNum() {
        return turnNum;
    }

    public void setTurnNum(int turnNum) {
        this.turnNum = turnNum;
    }

    public UUID getTurnId() {
        return this.turnId;
    }

    public void setTurnId(UUID turnId) {
        this.turnId = turnId;
    }

    public boolean isExtraTurn() {
        return extraTurn;
    }

    public void setExtraTurn(boolean extraTurn) {
        this.extraTurn = extraTurn;
    }

    public boolean isGameOver() {
        return this.gameOver;
    }

    public TurnMods getTurnMods() {
        return this.turnMods;
    }

    public Watchers getWatchers() {
        return this.watchers;
    }

    public SpecialActions getSpecialActions() {
        return this.specialActions;
    }

    public void endGame() {
        this.gameOver = true;
    }

    public void applyEffects(Game game) {
        for (Player player: players.values()) {
            player.reset();
        }
        battlefield.reset(game);
        combat.reset();
        this.reset();
        effects.apply(game);
    }

    // Remove End of Combat effects
    public void removeEocEffects(Game game) {
        effects.removeEndOfCombatEffects();
        delayed.removeEndOfCombatAbilities();
        applyEffects(game);
    }

    public void removeEotEffects(Game game) {
        effects.removeEndOfTurnEffects();
        delayed.removeEndOfTurnAbilities();
        applyEffects(game);
    }

    public void addEffect(ContinuousEffect effect, Ability source) {
        effects.addEffect(effect, source);
    }

    public void addEffect(ContinuousEffect effect, UUID sourceId, Ability source) {
        effects.addEffect(effect, sourceId, source);
    }

//    public void addMessage(String message) {
//        this.messages.add(message);
//    }

    /**
     * Returns a list of all players of the game ignoring range or
     * if a player has lost or left the game.
     *
     * @return playerList
     */
    public PlayerList getPlayerList() {
        return playerList;
    }

    /**
     * Returns a list of all active players of the game, setting the
     * playerId to the current player of the list.
     *
     * @param playerId
     * @return playerList
     */
    public PlayerList getPlayerList(UUID playerId) {
        PlayerList newPlayerList = new PlayerList();
        for (Player player: players.values()) {
            if (!player.hasLeft()&& !player.hasLost()) {
                newPlayerList.add(player.getId());
            }
        }
        newPlayerList.setCurrent(playerId);
        return newPlayerList;
    }

    public Permanent getPermanent(UUID permanentId) {
        if (permanentId != null && battlefield.containsPermanent(permanentId)) {
            Permanent permanent = battlefield.getPermanent(permanentId);
            setZone(permanent.getId(), Zone.BATTLEFIELD); // shouldn't this be set anyway? (LevelX2)
            return permanent;
        }
        return null;
    }

    public Zone getZone(UUID id) {
        if (id != null && zones.containsKey(id)) {
            return zones.get(id);
        }
        return null;
    }

    public void setZone(UUID id, Zone zone) {
        zones.put(id, zone);
    }

    public void restore(GameState state) {
        this.stack = state.stack;
        this.command = state.command;
        this.effects = state.effects;
        this.triggers = state.triggers;
        this.triggered = state.triggered;
        this.combat = state.combat;
        this.exile = state.exile;
        this.battlefield = state.battlefield;
        this.zones = state.zones;
        this.values = state.values;
        for (Player copyPlayer: state.players.values()) {
            Player origPlayer = players.get(copyPlayer.getId());
            origPlayer.restore(copyPlayer);
        }
        this.simultaneousEvents = state.simultaneousEvents;
    }

    public void addSimultaneousEvent(GameEvent event, Game game) {
        simultaneousEvents.add(event);
    }

    public void handleSimultaneousEvent(Game game) {
        if (!simultaneousEvents.isEmpty()) {
            // it can happen, that the events add new simultaneous events, so copy the list before
            List<GameEvent> eventsToHandle = new ArrayList<>();
            eventsToHandle.addAll(simultaneousEvents);
            simultaneousEvents.clear();
            for (GameEvent event:eventsToHandle) {
                this.handleEvent(event, game);
            }
        }
    }
    public boolean hasSimultaneousEvents() {
        return !simultaneousEvents.isEmpty();
    }

    public void handleEvent(GameEvent event, Game game) {
        watchers.watch(event, game);
        delayed.checkTriggers(event, game);
        triggers.checkTriggers(event, game);
    }

    public boolean replaceEvent(GameEvent event, Game game) {
        if (effects.preventedByRuleModification(event, null, game, false)) {
            return true;
        }
        return effects.replaceEvent(event, game);
    }

    public void addCard(Card card) {
        setZone(card.getId(), Zone.OUTSIDE);
        for (Watcher watcher: card.getWatchers()) {
            watcher.setControllerId(card.getOwnerId());
            watcher.setSourceId(card.getId());
            watchers.add(watcher);
        }
        for (Ability ability: card.getAbilities()) {
            addAbility(ability, card);
        }
    }
   
    public void removeCard(Card card) {
        zones.remove(card.getId());
        // TODO Watchers?
        // TODO Abilities?
        if (card.isSplitCard()) {
            removeCard( ((SplitCard)card).getLeftHalfCard());
            removeCard( ((SplitCard)card).getRightHalfCard());
        }
    }

    @Deprecated
    public void addAbility(Ability ability, MageObject attachedTo) {
        if (ability instanceof StaticAbility) {
            for (Mode mode: ability.getModes().values()) {
                for (Effect effect: mode.getEffects()) {
                    if (effect instanceof ContinuousEffect) {
                        addEffect((ContinuousEffect)effect, ability);
                    }
                }
            }
        }
        else if (ability instanceof TriggeredAbility) {
            this.triggers.add((TriggeredAbility)ability, attachedTo);
        }
    }

    public void addAbility(Ability ability, UUID sourceId, MageObject attachedTo) {
        if (ability instanceof StaticAbility) {
            for (Mode mode: ability.getModes().values()) {
                for (Effect effect: mode.getEffects()) {
                    if (effect instanceof ContinuousEffect) {
                        addEffect((ContinuousEffect)effect, sourceId, ability);
                    }
                }
            }
        }
        else if (ability instanceof TriggeredAbility) {
            // TODO: add sources for triggers - the same way as in addEffect: sources
            this.triggers.add((TriggeredAbility)ability, sourceId, attachedTo);
        }
    }

    public void addCommandObject(CommandObject commandObject) {
        getCommand().add(commandObject);
        for (Ability ability: commandObject.getAbilities()) {
            addAbility(ability, commandObject);
        }
    }

    public void addTriggeredAbility(TriggeredAbility ability) {
        this.triggered.add(ability);
    }

    public void removeTriggeredAbility(TriggeredAbility ability) {
        this.triggered.remove(ability);
    }

    public void addDelayedTriggeredAbility(DelayedTriggeredAbility ability) {
        this.delayed.add(ability);
    }

    public void removeDelayedTriggeredAbility(UUID abilityId) {
        for (DelayedTriggeredAbility ability: delayed) {
            if (ability.getId().equals(abilityId))  {
                delayed.remove(ability);
                break;
            }
        }
    }

    public List<TriggeredAbility> getTriggered(UUID controllerId) {
        List<TriggeredAbility> triggereds = new ArrayList<>();
        for (TriggeredAbility ability: triggered) {
            if (ability.getControllerId().equals(controllerId)) {
                triggereds.add(ability);
            }
        }
        return triggereds;
    }

    public DelayedTriggeredAbilities getDelayed() {
        return this.delayed;
    }

    public ContinuousEffects getContinuousEffects() {
        return effects;
    }

    public Object getValue(String valueId) {
        return values.get(valueId);
    }

    public void setValue(String valueId, Object value) {
        values.put(valueId, value);
    }

    /**
     * Other abilities are used to implement some special kind of continuous effects that give abilities to non permanents.
     *
     * Crucible of Worlds - You may play land cards from your graveyard.
     * Past in Flames - Each instant and sorcery card in your graveyard gains flashback until end of turn. The flashback cost is equal to its mana cost.
     * Varolz, the Scar-Striped - Each creature card in your graveyard has scavenge. The scavenge cost is equal to its mana cost.
     *
     * @param objectId
     * @param zone
     * @return
     */
    public Abilities<ActivatedAbility> getActivatedOtherAbilities(UUID objectId, Zone zone) {
        if (otherAbilities.containsKey(objectId)) {
            return otherAbilities.get(objectId).getActivatedAbilities(zone);
        }
        return null;
    }
   
    public Abilities<Ability> getAllOtherAbilities(UUID objectId) {
        if (otherAbilities.containsKey(objectId)) {
            return otherAbilities.get(objectId);
        }
        return null;
    }



    public void addOtherAbility(UUID objectId, Ability ability) {
        if (!otherAbilities.containsKey(objectId)) {
            otherAbilities.put(objectId, new AbilitiesImpl(ability));
        } else {
            otherAbilities.get(objectId).add(ability);
        }
    }

    private void resetOtherAbilities() {
        otherAbilities.clear();
    }

    /**
     * Removes Triggered abilities that were gained from sourceId
     *
     * @param sourceId
     */
    public void resetTriggersForSourceId(UUID sourceId) {
        List<String> keysToRemove = triggers.removeGainedAbilitiesForSource(sourceId);
        for (String key : keysToRemove) {
            triggers.remove(key);
        }
    }

    private void reset() {
        this.setLegendaryRuleActive(true);
        this.resetOtherAbilities();
    }

    public void clear() {
        battlefield.clear();
        effects.clear();
        triggers.clear();
        delayed.clear();
        triggered.clear();
        stack.clear();
        exile.clear();
        command.clear();
        revealed.clear();
        lookedAt.clear();
        turnNum = 0;
        stepNum = 0;
        extraTurn = false;
        legendaryRuleActive = true;
        gameOver = false;
        specialActions.clear();
        otherAbilities.clear();
        combat.clear();
        turnMods.clear();
        watchers.clear();
        values.clear();
        zones.clear();
        simultaneousEvents.clear();
    }

    public void pause() {
        this.paused = true;
    }

    public void resume() {
        this.paused = false;
    }

    public boolean isPaused() {
        return this.paused;
    }

    public boolean isLegendaryRuleActive() {
        return legendaryRuleActive;
    }

    public void setLegendaryRuleActive(boolean legendaryRuleActive) {
        this.legendaryRuleActive = legendaryRuleActive;
    }

}
TOP

Related Classes of mage.game.GameState

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.