View Javadoc

1   /*
2    * Created on Nov 15, 2004 
3    * Original filename was ReplacementAbility.java
4    * 
5    *   Firemox 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.firemox.clickable.ability;
24  
25  import java.io.IOException;
26  import java.io.InputStream;
27  
28  import net.sf.firemox.clickable.target.card.MCard;
29  import net.sf.firemox.event.MEventListener;
30  import net.sf.firemox.event.context.ContextEventListener;
31  import net.sf.firemox.stack.StackManager;
32  
33  /***
34   * TODO is it important to keep cost ?
35   * 
36   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
37   * @since 0.80
38   */
39  public class ReplacementAbility extends TriggeredAbility {
40  
41  	/***
42  	 * Creates a new instance of ReplacementAbility <br>
43  	 * <ul>
44  	 * Structure of InputStream : Data[size]
45  	 * <li>super [ActivatedAbility]</li>
46  	 * </ul>
47  	 * 
48  	 * @param input
49  	 *          stream containing this ability
50  	 * @param card
51  	 *          referenced card owning this ability.
52  	 * @throws IOException
53  	 *           if error occurred during the reading process from the specified
54  	 *           input stream
55  	 */
56  	public ReplacementAbility(InputStream input, MCard card) throws IOException {
57  		super(input, card);
58  	}
59  
60  	/***
61  	 * Create a fresh instance from another instance of ReplacementAbility
62  	 * 
63  	 * @param other
64  	 *          the instance to clone.
65  	 * @param event
66  	 *          The attached activation event.
67  	 */
68  	private ReplacementAbility(ReplacementAbility other, MEventListener event) {
69  		super(other, event);
70  	}
71  
72  	@Override
73  	public boolean triggerIt(ContextEventListener context) {
74  		throw new InternalError("Replacement ability cannot trigger");
75  	}
76  
77  	@Override
78  	public void resolveStack() {
79  		/*
80  		 * If the current action (the one causing this replacement ability to be
81  		 * played) is an instance of LoopingAction, it's broken. The remaining
82  		 * iterations are skipped.
83  		 */
84  		if (optimizer == Optimization.action) {
85  			StackManager.actionManager.setHop(1);
86  		}
87  		StackManager.resolveStack();
88  	}
89  
90  	@Override
91  	public Ability clone(MCard container) {
92  		return new ReplacementAbility(this, eventComing.clone(container));
93  	}
94  
95  	@Override
96  	public boolean isMatching() {
97  		/*
98  		 * assuming the event has been checked, we verify this ability has not
99  		 * already been played in order to prevent infinite loop.
100 		 */
101 		return !StackManager.isPlaying(this);
102 	}
103 
104 	@Override
105 	public void removeFromManager() {
106 		priority.removeFromManager(this);
107 		if (delayedCard != null) {
108 			// remove the delayed card from the DBZ, once.
109 			StackManager.getSpellController().zoneManager.delayedBuffer
110 					.remove(delayedCard);
111 			// and remove the linked 'until' abilities to free the useless listeners
112 			delayedCard.removeFromManager();
113 			delayedCard = null;
114 		}
115 	}
116 
117 	@Override
118 	public final boolean isAutoResolve() {
119 		return true;
120 	}
121 
122 	@Override
123 	public final boolean isHidden() {
124 		return true;
125 	}
126 
127 	@Override
128 	public final boolean hasHighPriority() {
129 		return true;
130 	}
131 
132 	@Override
133 	public void registerToManager() {
134 		priority.registerToManager(this);
135 	}
136 }