View Javadoc

1   /*
2    * Created on Jul 27, 2004 
3    * Original filename was TriggeredAbility.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   */
23  package net.sf.magicproject.clickable.ability;
24  
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.util.Collection;
28  
29  import net.sf.magicproject.action.MAction;
30  import net.sf.magicproject.clickable.targetable.card.DelayedCard;
31  import net.sf.magicproject.clickable.targetable.card.MCard;
32  import net.sf.magicproject.event.MEventListener;
33  import net.sf.magicproject.event.context.ContextEventListener;
34  import net.sf.magicproject.stack.StackManager;
35  import net.sf.magicproject.token.TrueFalseAuto;
36  import net.sf.magicproject.ui.i18n.LanguageManager;
37  
38  /***
39   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
40   * @since 0.80 When this ability is unregistered, the corresponding delayed
41   *        card, if specified is also removed from the DBZ.
42   * @since 0.82 Triggered abilities are registered avoiding multiple instance of
43   *        same effects. By default, triggered ability is always registered, but
44   *        refresh and remover modifier are added as needed.
45   * @since 0.86 mana check is not done in the "isMathing" method since many manas
46   *        can be played during the ability cost phase.
47   */
48  public class TriggeredAbility extends UserAbility {
49  
50  	/***
51  	 * Creates a new instance of TriggeredAbility <br>
52  	 * <ul>
53  	 * Structure of InputStream : Data[size]
54  	 * <li>name name + '\0' [...]</li>
55  	 * <li>ability tags [1]</li>
56  	 * <li>event [...]</li>
57  	 * <li>nb of actions for cost part [1]</li>
58  	 * <li>pay action i [...]</li>
59  	 * <li>nb of actions for effect part [1]</li>
60  	 * <li>pay action i [...]</li>
61  	 * </ul>
62  	 * 
63  	 * @param inputFile
64  	 *          file containing this ability
65  	 * @param card
66  	 *          referenced card
67  	 * @throws IOException
68  	 *           if error occurred during the reading process from the specified
69  	 *           input stream
70  	 */
71  	protected TriggeredAbility(InputStream inputFile, MCard card)
72  			throws IOException {
73  		super(inputFile, card);
74  	}
75  
76  	/***
77  	 * Create an instance of TriggeredAbility
78  	 * 
79  	 * @param name
80  	 *          Name of card used to display this ability in a stack
81  	 * @param actionList
82  	 *          list of actions to do for activate this ability
83  	 * @param effectList
84  	 *          list of effects of this ability
85  	 * @param optimizer
86  	 *          the optimizer to use.
87  	 * @param priority
88  	 *          the resolution type.
89  	 * @param eventComing
90  	 *          event condition of this ability
91  	 * @param pictureName
92  	 *          optional picture of this ability.
93  	 * @param linkedAbilities
94  	 *          the linked abilities. May be null.
95  	 * @param playAsSpell
96  	 *          play-as-spell.
97  	 */
98  	protected TriggeredAbility(String name, MAction[] actionList,
99  			MAction[] effectList, Optimization optimizer, Priority priority,
100 			MEventListener eventComing, String pictureName,
101 			Collection<Ability> linkedAbilities, TrueFalseAuto playAsSpell) {
102 		super(name, actionList, effectList, optimizer, priority, eventComing,
103 				pictureName, linkedAbilities, playAsSpell);
104 	}
105 
106 	@Override
107 	public Ability clone(MCard container) {
108 		return new TriggeredAbility(getName(), actionList, effectList, optimizer,
109 				priority, eventComing.clone(container), pictureName, linkedAbilities,
110 				playAsSpell);
111 	}
112 
113 	/***
114 	 * @param delayedCard
115 	 */
116 	public void setDelayedCard(DelayedCard delayedCard) {
117 		this.delayedCard = delayedCard;
118 	}
119 
120 	@Override
121 	public void removeFromManager() {
122 		super.removeFromManager();
123 		if (delayedCard != null) {
124 			// remove the delayed card from the DBZ, once.
125 			StackManager.getSpellController().zoneManager.delayedBuffer
126 					.remove(delayedCard);
127 			// and remove the linked 'until' abilities to free the useless listeners
128 			delayedCard.removeFromManager();
129 			delayedCard = null;
130 		}
131 	}
132 
133 	@Override
134 	public boolean isMatching() {
135 		// check only the target actions of 'cost' part can be played
136 		return checkTargetActions() && checkObjectActions();
137 	}
138 
139 	@Override
140 	public final String toHtmlString(ContextEventListener context) {
141 		return new StringBuilder(eventComing.toHtmlString(this, context)).append(
142 				"<br>").append(super.toHtmlString(context)).toString();
143 	}
144 
145 	@Override
146 	public String getLog(ContextEventListener context) {
147 		return "Triggered '" + toHtmlString(context) + "' of " + getCard();
148 	}
149 
150 	@Override
151 	public String getAbilityTitle() {
152 		return LanguageManager.getString("triggeredability")
153 				+ (getName() != null ? " : &nbsp;" + getName() : "")
154 				+ super.getAbilityTitle();
155 	}
156 
157 	/***
158 	 * Return true if this ability is a delayed one.
159 	 * 
160 	 * @return true if this ability is a delayed one.
161 	 */
162 	public boolean isDelayedAbility() {
163 		return delayedCard != null;
164 	}
165 
166 	/***
167 	 * Return the delayed card. May be null.
168 	 * 
169 	 * @return the delayed card. May be null.
170 	 */
171 	public DelayedCard getDelayedCard() {
172 		return delayedCard;
173 	}
174 
175 	/***
176 	 * The delayed card corresponding to this ability. If is not null, when this
177 	 * ability is unregistered, the delayedcard is removed from the DBZ
178 	 */
179 	protected DelayedCard delayedCard = null;
180 
181 }