View Javadoc

1   /*
2    *   Magic-Project is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  package net.sf.magicproject.token;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.OutputStream;
24  
25  import net.sf.magicproject.clickable.ability.Ability;
26  import net.sf.magicproject.clickable.targetable.Targetable;
27  import net.sf.magicproject.clickable.targetable.card.MCard;
28  import net.sf.magicproject.event.context.ContextEventListener;
29  import net.sf.magicproject.event.context.MContextCardCardIntInt;
30  import net.sf.magicproject.stack.EventManager;
31  import net.sf.magicproject.stack.StackManager;
32  
33  /***
34   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
35   * @since 0.93
36   */
37  public enum AbstractValue {
38  
39  	/***
40  	 * Special access to the integer saved into the context of current spell.
41  	 */
42  	CONTEXT_INT("context.int"),
43  
44  	/***
45  	 * Special access to the second integer saved into the context of current
46  	 * spell.
47  	 */
48  	CONTEXT_INT2("context.int2"),
49  
50  	/***
51  	 * Special access to the current target list size
52  	 */
53  	TARGET_LIST_SIZE("target-list.size"),
54  
55  	/***
56  	 * Special access to the static value correponding to the turn number. The
57  	 * first turn should be 0.
58  	 */
59  	TURN_ID("turnid"),
60  
61  	/***
62  	 * Special access to the first int of current int list
63  	 */
64  	INT_LIST_FIRST("int-list.first"),
65  
66  	/***
67  	 * Special access to the last int of current int list
68  	 */
69  	INT_LIST_LAST("int-list.last"),
70  
71  	/***
72  	 * Special access to the current int list size
73  	 */
74  	INT_LIST_SIZE("int-list.size"),
75  
76  	/***
77  	 * Special access to the saved int list size
78  	 */
79  	SAVED_INT_LIST_SIZE("saved-int-list.size"),
80  
81  	/***
82  	 * Special access to the saved target list size
83  	 */
84  	SAVED_TARGET_LIST_SIZE("saved-target-list.size"),
85  
86  	/***
87  	 * Special access to current phase index.
88  	 */
89  	CURRENT_PHASE_INDEX("current-phase.index"),
90  
91  	/***
92  	 * The owner of the second card saved in the context.
93  	 */
94  	ALL("?");
95  
96  	private final String xsdName;
97  
98  	private AbstractValue(String xsdName) {
99  		this.xsdName = xsdName;
100 	}
101 
102 	/***
103 	 * Return the targetable on which the test would be applied
104 	 * 
105 	 * @param ability
106 	 *          is the ability owning this test. The card component of this
107 	 *          ability should correspond to the card owning this test too.
108 	 * @param card
109 	 *          is the card owning the current ability.
110 	 * @param context
111 	 *          the current context.
112 	 * @param tested
113 	 *          the tested targetable
114 	 * @return the targetable to use for the test
115 	 */
116 	public int getValue(Ability ability, MCard card,
117 			ContextEventListener context, Targetable tested) {
118 		switch (this) {
119 		case TARGET_LIST_SIZE:
120 			if (StackManager.getInstance().getTargetedList() == null) {
121 				return 0;
122 			}
123 			return StackManager.getInstance().getTargetedList().size();
124 		case INT_LIST_SIZE:
125 			if (StackManager.intList == null) {
126 				return 0;
127 			}
128 			return StackManager.intList.size();
129 		case CONTEXT_INT2:
130 			return ((MContextCardCardIntInt) getContext(context)).getValue2();
131 		case CONTEXT_INT:
132 			return ((MContextCardCardIntInt) getContext(context)).getValue();
133 		case INT_LIST_LAST:
134 			return StackManager.intList.getLastInt();
135 		case INT_LIST_FIRST:
136 			return StackManager.intList.getFirstInt();
137 		case SAVED_INT_LIST_SIZE:
138 			return StackManager.SAVED_INT_LISTS.size();
139 		case SAVED_TARGET_LIST_SIZE:
140 			return StackManager.SAVED_TARGET_LISTS.size();
141 		case CURRENT_PHASE_INDEX:
142 			return EventManager.phaseIndex;
143 		case TURN_ID:
144 			return MCommonVars.registers[IdTokens.TURN_ID];
145 		default:
146 			return 0;
147 		}
148 	}
149 
150 	/***
151 	 * Write this enum to the given outputstream.
152 	 * 
153 	 * @param out
154 	 *          the stream ths enum would be written.
155 	 * @param xsdName
156 	 *          the Xsd name of this TestOn.
157 	 * @throws IOException
158 	 *           If some other I/O error occurs
159 	 */
160 	public static void serialize(OutputStream out, String xsdName)
161 			throws IOException {
162 		for (AbstractValue value : values()) {
163 			if (value.xsdName.equals(xsdName)) {
164 				value.serialize(out);
165 				return;
166 			}
167 		}
168 		throw new IllegalArgumentException("Invalid xsd attribute name : "
169 				+ xsdName);
170 	}
171 
172 	/***
173 	 * Return null of enum value corresponding to the given Xsd name.
174 	 * 
175 	 * @param xsdName
176 	 *          the Xsd name of this Aabstract value.
177 	 * @return null of enum value corresponding to the given Xsd name.
178 	 */
179 	public static AbstractValue valueOfXsd(String xsdName) {
180 		for (AbstractValue value : values()) {
181 			if (value.xsdName.equals(xsdName)) {
182 				return value;
183 			}
184 		}
185 		return null;
186 	}
187 
188 	/***
189 	 * Write this enum to the given outputstream.
190 	 * 
191 	 * @param out
192 	 *          the stream ths enum would be written.
193 	 * @throws IOException
194 	 *           If some other I/O error occurs
195 	 */
196 	public void serialize(OutputStream out) throws IOException {
197 		out.write(ordinal());
198 	}
199 
200 	/***
201 	 * Read and return the enum from the given inputstream.
202 	 * 
203 	 * @param input
204 	 *          the stream containing the enum to read.
205 	 * @return the enum from the given inputstream.
206 	 * @throws IOException
207 	 *           If some other I/O error occurs
208 	 */
209 	public static AbstractValue deserialize(InputStream input) throws IOException {
210 		return values()[input.read()];
211 	}
212 
213 	/***
214 	 * Return the given context if not null. Returns the context of current
215 	 * ability otherwise.
216 	 * 
217 	 * @param currentContext
218 	 *          the known context.
219 	 * @return the given context if not null. Returns the context of current
220 	 *         ability otherwise.
221 	 */
222 	private ContextEventListener getContext(ContextEventListener currentContext) {
223 		if (currentContext == null)
224 			return StackManager.getInstance().getAbilityContext();
225 		return currentContext;
226 	}
227 
228 }