From f137f9c49cdfc0719927e7f2f87a0f82a6cd76d2 Mon Sep 17 00:00:00 2001
From: LevelX2 <ludwig.hirth@online.de>
Date: Wed, 10 Sep 2014 00:18:03 +0200
Subject: [PATCH] Fixed Morph handling, fixed that face down cards stay face
 down after zone change.

---
 Mage.Common/src/mage/view/PermanentView.java  |  2 +-
 .../mage/sets/khansoftarkir/HoodedHydra.java  |  5 +++--
 Mage/src/mage/abilities/Ability.java          | 20 ++++++++++++++++++-
 Mage/src/mage/abilities/AbilityImpl.java      | 14 +++++++++++++
 .../common/TurnedFaceUpTriggeredAbility.java  |  2 ++
 .../mage/abilities/keyword/MorphAbility.java  |  3 ++-
 Mage/src/mage/cards/CardImpl.java             |  9 +++++++--
 .../mage/game/permanent/PermanentCard.java    |  1 +
 Mage/src/mage/game/stack/StackAbility.java    | 10 ++++++++++
 9 files changed, 59 insertions(+), 7 deletions(-)

diff --git a/Mage.Common/src/mage/view/PermanentView.java b/Mage.Common/src/mage/view/PermanentView.java
index ea7dba6785..abae79b7e9 100644
--- a/Mage.Common/src/mage/view/PermanentView.java
+++ b/Mage.Common/src/mage/view/PermanentView.java
@@ -119,7 +119,7 @@ public class PermanentView extends CardView {
                         if (permanentAbility instanceof TurnFaceUpAbility && !permanentAbility.getRuleVisible()) {
                             this.rules.add(permanentAbility.getRule(true));
                         }
-                        if (permanentAbility instanceof TurnedFaceUpTriggeredAbility) {
+                        if (permanentAbility.getWorksFaceDown()) {
                             this.rules.add(permanentAbility.getRule());
                         }
                     }
diff --git a/Mage.Sets/src/mage/sets/khansoftarkir/HoodedHydra.java b/Mage.Sets/src/mage/sets/khansoftarkir/HoodedHydra.java
index 1f7c0f8e3d..fe06c0b626 100644
--- a/Mage.Sets/src/mage/sets/khansoftarkir/HoodedHydra.java
+++ b/Mage.Sets/src/mage/sets/khansoftarkir/HoodedHydra.java
@@ -82,8 +82,9 @@ public class HoodedHydra extends CardImpl {
         Effect effect = new AddCountersSourceEffect(CounterType.P1P1.createInstance(5));
         effect.setText("put five +1/+1 counters on it");
         // TODO: Does not work because the ability is still removed from permanent while the effect checks if the ability still exosts.
-        this.addAbility(new SimpleStaticAbility(Zone.BATTLEFIELD, 
-                new AsTurnedFaceUpEffect(effect, false)));
+        Ability ability = new SimpleStaticAbility(Zone.BATTLEFIELD, new AsTurnedFaceUpEffect(effect, false));
+        ability.setWorksFaceDown(true);
+        this.addAbility(ability);
     }
 
     public HoodedHydra(final HoodedHydra card) {
diff --git a/Mage/src/mage/abilities/Ability.java b/Mage/src/mage/abilities/Ability.java
index 2bc9a56941..174550102f 100644
--- a/Mage/src/mage/abilities/Ability.java
+++ b/Mage/src/mage/abilities/Ability.java
@@ -385,6 +385,24 @@ public interface Ability extends Controllable, Serializable {
      */
     void setRuleAtTheTop(boolean ruleAtTheTop);
 
+    
+    /**
+     * Returns true if this ability has to work also with face down object
+     * (set to not visible normally).
+     *
+     * @return
+     */
+    boolean getWorksFaceDown();
+
+    /**
+     * Sets the value for the worksFaceDown flag
+     *
+     * true = the ability works also if the object is face down
+     *
+     * @param worksFaceDown
+     */
+    void setWorksFaceDown(boolean worksFaceDown);
+
     /**
      * Returns true if this ability's rule is visible on the card tooltip
      *
@@ -450,7 +468,7 @@ public interface Ability extends Controllable, Serializable {
 
     /**
      * Used to deactivate cost modification logic of ability activation for some special handling
-     * (e.g. FlashbackAbility gets cost modifiaction twice because of how it#s ahndled now)
+     * (e.g. FlashbackAbility gets cost modifiaction twice because of how it's handled now)
      *
      * @param active execute no cost modification
      */
diff --git a/Mage/src/mage/abilities/AbilityImpl.java b/Mage/src/mage/abilities/AbilityImpl.java
index 7aa36238d8..29f081b29f 100644
--- a/Mage/src/mage/abilities/AbilityImpl.java
+++ b/Mage/src/mage/abilities/AbilityImpl.java
@@ -83,6 +83,7 @@ public abstract class AbilityImpl implements Ability {
     protected boolean ruleAdditionalCostsVisible = true;
     protected boolean costModificationActive = true;
     protected boolean activated = false;
+    protected boolean worksFaceDown = false;
 
     public AbilityImpl(AbilityType abilityType, Zone zone) {
         this.id = UUID.randomUUID();
@@ -117,6 +118,7 @@ public abstract class AbilityImpl implements Ability {
         this.ruleVisible = ability.ruleVisible;
         this.ruleAdditionalCostsVisible = ability.ruleAdditionalCostsVisible;
         this.costModificationActive = ability.costModificationActive;
+        this.worksFaceDown = ability.worksFaceDown;
     }
 
     @Override
@@ -945,5 +947,17 @@ public abstract class AbilityImpl implements Ability {
         this.costModificationActive = active;
     }
 
+    @Override
+    public boolean getWorksFaceDown() {
+        return worksFaceDown;
+    }
+
+    @Override
+    public void setWorksFaceDown(boolean worksFaceDown) {
+        this.worksFaceDown = worksFaceDown;
+    }
+
+
+
 }
 
diff --git a/Mage/src/mage/abilities/common/TurnedFaceUpTriggeredAbility.java b/Mage/src/mage/abilities/common/TurnedFaceUpTriggeredAbility.java
index eee1ddea21..5b1b1c35dc 100644
--- a/Mage/src/mage/abilities/common/TurnedFaceUpTriggeredAbility.java
+++ b/Mage/src/mage/abilities/common/TurnedFaceUpTriggeredAbility.java
@@ -22,6 +22,8 @@ public class TurnedFaceUpTriggeredAbility extends TriggeredAbilityImpl {
 
     public TurnedFaceUpTriggeredAbility(Effect effect) {
         super(Zone.BATTLEFIELD, effect);
+        // has to be set so the ability triggers if card is turn faced up
+        this.setWorksFaceDown(true);
     }
 
     public TurnedFaceUpTriggeredAbility(final TurnedFaceUpTriggeredAbility ability) {
diff --git a/Mage/src/mage/abilities/keyword/MorphAbility.java b/Mage/src/mage/abilities/keyword/MorphAbility.java
index 23421135e5..91c841036b 100644
--- a/Mage/src/mage/abilities/keyword/MorphAbility.java
+++ b/Mage/src/mage/abilities/keyword/MorphAbility.java
@@ -48,6 +48,7 @@ import mage.abilities.costs.CostsImpl;
 import mage.abilities.costs.mana.GenericManaCost;
 import mage.abilities.costs.mana.ManaCosts;
 import mage.abilities.costs.mana.ManaCostsImpl;
+import mage.abilities.effects.AsTurnedFaceUpEffect;
 import mage.abilities.effects.ContinuousEffectImpl;
 import mage.abilities.effects.common.continious.SourceEffect;
 import mage.cards.Card;
@@ -301,7 +302,7 @@ class BecomesFaceDownCreatureEffect extends ContinuousEffectImpl implements Sour
                     List<Ability> abilities = new ArrayList<>();
                     for (Ability ability : permanent.getAbilities()) {
                         // TODO: Add flag "works also face down" to ability and use it to control ability removement instead of instanceof check
-                        if (ability instanceof TurnedFaceUpTriggeredAbility) {
+                        if (ability.getWorksFaceDown()) {
                             ability.setRuleVisible(false);
                             continue;
                         }
diff --git a/Mage/src/mage/cards/CardImpl.java b/Mage/src/mage/cards/CardImpl.java
index a468b0ea71..120da860fe 100644
--- a/Mage/src/mage/cards/CardImpl.java
+++ b/Mage/src/mage/cards/CardImpl.java
@@ -571,6 +571,11 @@ public abstract class CardImpl extends MageObjectImpl implements Card {
         if (!game.replaceEvent(event)) {
             setFaceDown(false);
             game.getCard(objectId).setFaceDown(false); // Another instance?
+            for (Ability ability :abilities) { // abilities that were set to not visible face down must be set to visible again
+                if (ability.getWorksFaceDown() && !ability.getRuleVisible()) {
+                    ability.setRuleVisible(true);
+                }
+            }
             game.fireEvent(GameEvent.getEvent(GameEvent.EventType.TURNEDFACEUP, getId(), playerId));
             return true;
         }
@@ -703,9 +708,9 @@ public abstract class CardImpl extends MageObjectImpl implements Card {
         countersEvent.setAppliedEffects(appliedEffects);
         if (!game.replaceEvent(countersEvent)) {
             int amount = countersEvent.getAmount();
-            Counter eventCounter = counter.copy();
-            eventCounter.remove(amount - 1);
             for (int i = 0; i < amount; i++) {
+                Counter eventCounter = counter.copy();
+                eventCounter.remove(amount - 1);
                 GameEvent event = GameEvent.getEvent(GameEvent.EventType.ADD_COUNTER, objectId, ownerId, counter.getName(), 1);
                 event.setAppliedEffects(appliedEffects);
                 if (!game.replaceEvent(event)) {
diff --git a/Mage/src/mage/game/permanent/PermanentCard.java b/Mage/src/mage/game/permanent/PermanentCard.java
index 68b225abae..200e01e074 100644
--- a/Mage/src/mage/game/permanent/PermanentCard.java
+++ b/Mage/src/mage/game/permanent/PermanentCard.java
@@ -134,6 +134,7 @@ public class PermanentCard extends PermanentImpl {
         if (controller != null && controller.removeFromBattlefield(this, game)) {
             if (isFaceDown()) {
                 setFaceDown(false);
+                game.getCard(this.getId()).setFaceDown(false); //TODO: Do this in a better way
             }
             ZoneChangeEvent event = new ZoneChangeEvent(this, sourceId, controllerId, fromZone, toZone, appliedEffects);
             if (!game.replaceEvent(event)) {
diff --git a/Mage/src/mage/game/stack/StackAbility.java b/Mage/src/mage/game/stack/StackAbility.java
index 69ba7abf21..ce7b9ce67d 100644
--- a/Mage/src/mage/game/stack/StackAbility.java
+++ b/Mage/src/mage/game/stack/StackAbility.java
@@ -451,6 +451,16 @@ public class StackAbility implements StackObject, Ability {
     public void setCostModificationActive(boolean active) {
         throw new UnsupportedOperationException("Not supported. Only neede for flashbacked spells"); 
     }
+
+    @Override
+    public boolean getWorksFaceDown() {
+        return this.ability.getWorksFaceDown();
+    }
+
+    @Override
+    public void setWorksFaceDown(boolean worksFaceDown) {
+        this.ability.setWorksFaceDown(worksFaceDown);
+    }
     
     
 }