View Javadoc

1   /*
2    * Created on Nov 15, 2004 
3    * Original filename was ReplacementAbility.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.MCard;
31  import net.sf.magicproject.event.MEventListener;
32  import net.sf.magicproject.event.context.ContextEventListener;
33  import net.sf.magicproject.stack.StackManager;
34  import net.sf.magicproject.token.TrueFalseAuto;
35  
36  /***
37   * TODO is it important to keep cost ?
38   * 
39   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
40   * @since 0.80
41   */
42  public class ReplacementAbility extends TriggeredAbility {
43  
44  	/***
45  	 * Creates a new instance of ReplacementAbility <br>
46  	 * <ul>
47  	 * Structure of InputStream : Data[size]
48  	 * <li>name name + '\0' [...]</li>
49  	 * <li>ability tags [1]</li>
50  	 * <li>event [...]</li>
51  	 * <li>nb of actions for effect part [1]</li>
52  	 * <li>pay action i [...]</li>
53  	 * </ul>
54  	 * 
55  	 * @param inputFile
56  	 * @param card
57  	 * @throws IOException
58  	 *           if error occurred during the reading process from the specified
59  	 *           input stream
60  	 */
61  	public ReplacementAbility(InputStream inputFile, MCard card)
62  			throws IOException {
63  		super(inputFile, card);
64  	}
65  
66  	/***
67  	 * Create an instance of this class.
68  	 * 
69  	 * @param name
70  	 *          Name of card used to display this ability in a stack
71  	 * @param actionList
72  	 *          list of actions to do for activate this ability
73  	 * @param effectList
74  	 *          list of effects of this ability
75  	 * @param optimizer
76  	 *          the optimizer to use.
77  	 * @param priority
78  	 *          the resolution type.
79  	 * @param eventComing
80  	 *          event condition of this ability
81  	 * @param linkedAbilities
82  	 *          the linked abilities. May be null.
83  	 * @param playAsSpell
84  	 *          play-as-spell.
85  	 */
86  	private ReplacementAbility(String name, Optimization optimizer,
87  			Priority priority, MEventListener eventComing, MAction[] actionList,
88  			MAction[] effectList, Collection<Ability> linkedAbilities,
89  			TrueFalseAuto playAsSpell) {
90  		super(name, actionList, effectList, optimizer, priority, eventComing, null,
91  				linkedAbilities, playAsSpell);
92  	}
93  
94  	@Override
95  	public boolean triggerIt(ContextEventListener context) {
96  		throw new InternalError("Replacement ability cannot trigger");
97  	}
98  
99  	@Override
100 	public void resolveStack() {
101 		/*
102 		 * If the current action (the one causing this replacement ability to be
103 		 * played) is an instance of LoopingAction, it's broken. The remaining
104 		 * iterations are skipped.
105 		 */
106 		if (optimizer == Optimization.action) {
107 			StackManager.actionManager.setHop(1);
108 		}
109 		StackManager.resolveStack();
110 	}
111 
112 	@Override
113 	public Ability clone(MCard container) {
114 		return new ReplacementAbility(getName(), optimizer, priority, eventComing
115 				.clone(container), actionList, effectList, linkedAbilities, playAsSpell);
116 	}
117 
118 	@Override
119 	public boolean isMatching() {
120 		/*
121 		 * assuming the event has been checked, we verify this ability has not
122 		 * already been played in order to prevent infinite loop.
123 		 */
124 		return !StackManager.isPlaying(this);
125 	}
126 
127 	@Override
128 	public void removeFromManager() {
129 		priority.removeFromManager(this);
130 		if (delayedCard != null) {
131 			// remove the delayed card from the DBZ, once.
132 			StackManager.getSpellController().zoneManager.delayedBuffer
133 					.remove(delayedCard);
134 			// and remove the linked 'until' abilities to free the useless listeners
135 			delayedCard.removeFromManager();
136 			delayedCard = null;
137 		}
138 	}
139 
140 	@Override
141 	public final boolean isAutoResolve() {
142 		return true;
143 	}
144 
145 	@Override
146 	public final boolean isHidden() {
147 		return true;
148 	}
149 
150 	@Override
151 	public final boolean hasHighPriority() {
152 		return true;
153 	}
154 
155 	@Override
156 	public void registerToManager() {
157 		priority.registerToManager(this);
158 	}
159 }