View Javadoc

1   /*
2    * Created on Sep 3, 2004 
3    * Original filename was RemoveModifier.java
4    * 
5    *   Magic-Project is a turn based strategy simulator
6    *   Copyright (C) 2003-2007 Fabrice Daugan
7    *
8    *   This program is free software; you can redistribute it and/or modify it 
9    * under the terms of the GNU General Public License as published by the Free 
10   * Software Foundation; either version 2 of the License, or (at your option) any
11   * later version.
12   *
13   *   This program is distributed in the hope that it will be useful, but WITHOUT 
14   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
16   * details.
17   *
18   *   You should have received a copy of the GNU General Public License along  
19   * with this program; if not, write to the Free Software Foundation, Inc., 
20   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21   */
22  package net.sf.magicproject.clickable.ability;
23  
24  import net.sf.magicproject.action.MAction;
25  import net.sf.magicproject.action.RemoveMe;
26  import net.sf.magicproject.clickable.targetable.card.MCard;
27  import net.sf.magicproject.event.MEventListener;
28  import net.sf.magicproject.event.TriggeredEvent;
29  import net.sf.magicproject.event.context.ContextEventListener;
30  import net.sf.magicproject.modifier.Unregisterable;
31  import net.sf.magicproject.token.TrueFalseAuto;
32  
33  /***
34   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
35   */
36  public class RemoveModifier extends AbstractAbility {
37  
38  	/***
39  	 * Creates a new instance of RemoveModifier <br>
40  	 * 
41  	 * @param event
42  	 *          the event attached to this ability.
43  	 * @param modifier
44  	 */
45  	public RemoveModifier(TriggeredEvent event, Unregisterable modifier) {
46  		super("removemyself modifier", event, null, null, TrueFalseAuto.FALSE);
47  		actionList = new MAction[] {};
48  		effectList = new MAction[] { new RemoveMe(this) };
49  		this.modifier = modifier;
50  
51  		// override the resolver
52  		priority = Priority.hidden_high;
53  		// override the optimizer
54  		optimizer = Optimization.first;
55  		unregisteringmodifier = false;
56  	}
57  
58  	@Override
59  	public void removeFromManager() {
60  		if (unregisteringmodifier) {
61  			// we are already unregistering this modifier
62  			return;
63  		}
64  		unregisteringmodifier = true;
65  		super.removeFromManager();
66  		modifier.removeFromManager();
67  		unregisteringmodifier = false;
68  	}
69  
70  	@Override
71  	public void optimizeRegisterToManager() {
72  		for (Ability ability : MEventListener.TRIGGRED_ABILITIES.get(eventComing
73  				.getIdEvent())) {
74  			if (equals(ability)) {
75  				/*
76  				 * this remove modifier is already existing. Instead of registering
77  				 * another time this modifier, we append with 'or' operator the
78  				 * conditionnal test
79  				 */
80  				MEventListener newEvent = ability.eventComing.appendOr(eventComing);
81  				if (newEvent != null) {
82  					ability.eventComing = newEvent;
83  					return;
84  				}
85  			}
86  		}
87  		// This modifier has not yet been registered
88  		MEventListener.TRIGGRED_ABILITIES.get(eventComing.getIdEvent()).add(this);
89  	}
90  
91  	@Override
92  	public boolean equals(ContextEventListener thisContext, Ability ability,
93  			ContextEventListener context) {
94  		return ability instanceof RemoveModifier
95  				&& ((RemoveModifier) ability).modifier == modifier;
96  	}
97  
98  	@Override
99  	public int hashCode() {
100 		if (eventComing != null && eventComing.card != null)
101 			return eventComing.card.hashCode();
102 		return super.hashCode();
103 	}
104 
105 	@Override
106 	public MCard getCard() {
107 		return eventComing.card;
108 	}
109 
110 	@Override
111 	public String toString() {
112 		return getName() + ", from card " + eventComing.card;
113 	}
114 
115 	@Override
116 	public boolean triggerIt(ContextEventListener context) {
117 		super.triggerIt(context);
118 		eventComing.card.getController().zoneManager.triggeredBuffer.addHidden(
119 				this, context);
120 		return true;
121 	}
122 
123 	@Override
124 	public String getLog(ContextEventListener context) {
125 		return "removed modifier of '" + getCard() + "'";
126 	}
127 
128 	@Override
129 	public String getAbilityTitle() {
130 		return "RemoveModifierAbility"
131 				+ (getName() != null ? ": " + getName() : "") + super.getAbilityTitle();
132 	}
133 
134 	/***
135 	 * The modifier to remove with this action
136 	 */
137 	public Unregisterable modifier;
138 
139 	/***
140 	 * Tag indicating we are currently unregistering this modifier
141 	 */
142 	protected boolean unregisteringmodifier;
143 
144 }