View Javadoc

1   /*
2    * Created on Jul 14, 2004 
3    * 
4    *   Magic-Project is a turn based strategy simulator
5    *   Copyright (C) 2003-2007 Fabrice Daugan
6    *
7    *   This program is free software; you can redistribute it and/or modify it 
8    * under the terms of the GNU General Public License as published by the Free 
9    * Software Foundation; either version 2 of the License, or (at your option) any
10   * later version.
11   *
12   *   This program is distributed in the hope that it will be useful, but WITHOUT 
13   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
15   * details.
16   *
17   *   You should have received a copy of the GNU General Public License along  
18   * with this program; if not, write to the Free Software Foundation, Inc., 
19   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   */
21  package net.sf.magicproject.clickable.ability;
22  
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.OutputStream;
26  import java.util.List;
27  
28  import net.sf.magicproject.clickable.targetable.card.TriggeredCard;
29  import net.sf.magicproject.event.MEventListener;
30  
31  /***
32   * Possible optimizations for abilities management in the TBZ <br>
33   * 
34   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
35   */
36  public enum Priority {
37  
38  	/***
39  	 * As HIDDEN tag, but the ability is choosen before the other hidden abilities
40  	 * without this tag .
41  	 */
42  	hidden_high,
43  
44  	/***
45  	 * Indicates wether this ability is resolved completely before player can get
46  	 * priority (like isAutoResolve), and stack is resolved immediatly after it
47  	 * has been added to the stack. No information displayed or prompted to the
48  	 * players. Users will not see this ability played.
49  	 */
50  	hidden,
51  
52  	/***
53  	 * As hidden tag, but the ability is choosen after the other hidden abilities
54  	 * without this tag .
55  	 */
56  	hidden_low,
57  
58  	/***
59  	 * Indicates wether this ability is choosen in priority to the others without
60  	 * this tag.
61  	 */
62  	high,
63  
64  	/***
65  	 * Indicates wether this ability is resolved completely before player can get
66  	 * priority.
67  	 */
68  	auto,
69  
70  	/***
71  	 * Indicates the ability would be resoved depending on the current flow.
72  	 * Often, need user intervention to be played and resolved.
73  	 */
74  	normal;
75  
76  	/***
77  	 * Indicates wether this ability is choosen in priority to the others without
78  	 * this tag.
79  	 * 
80  	 * @return true if this ability is choosen in priority to the others without
81  	 *         this tag.
82  	 */
83  	public boolean hasHighPriority() {
84  		return this == high || isHidden();
85  	}
86  
87  	/***
88  	 * Indicates if this ability is immediatly after it has been added to the
89  	 * stack. Note it's not says immediatly it has been triggered or playable, but
90  	 * says it has been activated - so added directly to the stack -, or has been
91  	 * triggered - so added to the triggered buffer zone - and then has been
92  	 * selected to be moved to the stack.
93  	 * 
94  	 * @return true if this ability is immediatly after it has been added to the
95  	 *         stack.
96  	 */
97  	public boolean isAutoResolve() {
98  		return this == auto || isHidden();
99  	}
100 
101 	/***
102 	 * Indicates wether this ability is resolved completely before player can get
103 	 * priority (like isAutoResolve), and stack is resolved immediatly after it
104 	 * has been added to the stack. No information displayed or prompted to the
105 	 * players. Users will not see this ability played.
106 	 * 
107 	 * @return true if this ability is resolved completely before player can get
108 	 *         priority (like isAutoResolve), and stack is resolved immediatly
109 	 *         after it has been added to the stack.
110 	 */
111 	public boolean isHidden() {
112 		return this == hidden_high || this == hidden || this == hidden_low;
113 	}
114 
115 	/***
116 	 * Unregister the specified replacement ability.
117 	 * 
118 	 * @param ability
119 	 *          the replacement ability to unregister.
120 	 */
121 	public void removeFromManager(ReplacementAbility ability) {
122 		MEventListener.REPLACEMENT_ABILITIES
123 				.get(ability.eventComing().getIdEvent()).get(this).remove(ability);
124 	}
125 
126 	/***
127 	 * Following the class, returns the abstract zone where the specified ability
128 	 * should be added.
129 	 * 
130 	 * @param abstractLowestZone
131 	 *          the abstract zone corresponding to the zone containing abilities
132 	 *          with the lowest priority of hidden abilities.
133 	 * @param abstractZone
134 	 *          the abstract zone corresponding to the zone containing abilities
135 	 *          with the normal priority of hidden abilities.
136 	 * @param abstractHighestZone
137 	 *          the abstract zone corresponding to the zone containing abilities
138 	 *          with the highest priority of hidden abilities.
139 	 * @return the abstract zone where the specified ability should be added.
140 	 */
141 	public List<TriggeredCard> getAbstractZone(
142 			List<TriggeredCard> abstractLowestZone, List<TriggeredCard> abstractZone,
143 			List<TriggeredCard> abstractHighestZone) {
144 		switch (this) {
145 		case hidden_high:
146 			return abstractHighestZone;
147 		case hidden:
148 			return abstractZone;
149 		case hidden_low:
150 			return abstractLowestZone;
151 		default:
152 			throw new InternalError("should not be called");
153 		}
154 	}
155 
156 	/***
157 	 * Register the specified replacement ability.
158 	 * 
159 	 * @param ability
160 	 *          the replacement ability to register.
161 	 */
162 	public void registerToManager(ReplacementAbility ability) {
163 		final List<ReplacementAbility> replacementAbilities = MEventListener.REPLACEMENT_ABILITIES
164 				.get(ability.eventComing().getIdEvent()).get(this);
165 		if (!replacementAbilities.contains(ability)) {
166 			replacementAbilities.add(ability);
167 		}
168 	}
169 
170 	/***
171 	 * Wrtite this enum to the given outputstream.
172 	 * 
173 	 * @param out
174 	 *          the stream ths enum would be written.
175 	 * @throws IOException
176 	 */
177 	public void write(OutputStream out) throws IOException {
178 		out.write(ordinal());
179 	}
180 
181 	/***
182 	 * Read and return the enum from the given inputstream.
183 	 * 
184 	 * @param input
185 	 *          the stream containing the enum to read.
186 	 * @return the enum from the given inputstream.
187 	 * @throws IOException
188 	 */
189 	static Priority valueOf(InputStream input) throws IOException {
190 		return values()[input.read()];
191 	}
192 }