View Javadoc

1   /* 
2    * GiveManaMulti.java
3    * Created on 2005/08/25
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.action;
24  
25  import java.io.IOException;
26  import java.io.InputStream;
27  
28  import net.sf.magicproject.clickable.ability.Ability;
29  import net.sf.magicproject.event.context.ContextEventListener;
30  import net.sf.magicproject.expression.Expression;
31  import net.sf.magicproject.expression.ExpressionFactory;
32  import net.sf.magicproject.stack.StackManager;
33  import net.sf.magicproject.test.TestOn;
34  import net.sf.magicproject.token.IdCommonToken;
35  import net.sf.magicproject.token.IdTokens;
36  import net.sf.magicproject.tools.MToolKit;
37  import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
38  
39  /***
40   * It's the mana source action, modify directly the mana pool like : Land, other
41   * mana sources. Add several colors at the same time. This is the difference
42   * between GiveManaBasic
43   * 
44   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
45   * @since 0.54
46   * @since 0.72 support counter ability
47   * @since 0.86
48   */
49  class GiveManaMulti extends GiveMana implements LoopAction {
50  
51  	/***
52  	 * Create an instance of GiveManaMulti by reading a file Offset's file must
53  	 * pointing on the first byte of this action <br>
54  	 * <ul>
55  	 * Structure of stream : Data[size]
56  	 * <li>restriction usage test [...]</li>
57  	 * <li>idPlayer receiving this mana [2]</li>
58  	 * <li>COLORLESS : expression [...] or (IdTokens#REGISTERS)[2]</li>
59  	 * <li>BLACK : expression[...]</li>
60  	 * <li>BLUE : expression[...]</li>
61  	 * <li>GREEN : expression[...]</li>
62  	 * <li>RED : expression[...]</li>
63  	 * <li>WHITE : expression[...]</li>
64  	 * </ul>
65  	 * 
66  	 * @param inputFile
67  	 *          file containing this action
68  	 * @throws IOException
69  	 *           if error occurred during the reading process from the specified
70  	 *           input stream
71  	 */
72  	GiveManaMulti(InputStream inputFile) throws IOException {
73  		super(inputFile);
74  		final int code0 = MToolKit.readInt16(inputFile);
75  		if (code0 == IdTokens.MANA_POOL) {
76  			on = TestOn.deserialize(inputFile);
77  		} else {
78  			codeExpr = new Expression[IdCommonToken.COLOR_NAMES.length];
79  			for (int i = 0; i < codeExpr.length; i++) {
80  				codeExpr[i] = ExpressionFactory.readNextExpression(inputFile);
81  			}
82  		}
83  	}
84  
85  	@Override
86  	public final Actiontype getIdAction() {
87  		return Actiontype.GIVE_MANA_MULTI;
88  	}
89  
90  	public boolean continueLoop(ContextEventListener context, int loopingIndex,
91  			Ability ability) {
92  		if (!PayMana.useMana) {
93  			return true;
94  		}
95  		final int realCode = on != null ? on.getCard(StackManager.currentAbility,
96  				null).cachedRegisters[loopingIndex] : codeExpr[loopingIndex].getValue(
97  				ability, null, context);
98  		giveMana(ability, context, loopingIndex, realCode);
99  		return false;
100 	}
101 
102 	public int getStartIndex() {
103 		return codeExpr.length - 1;
104 	}
105 
106 	@Override
107 	public boolean play(ContextEventListener context, Ability ability) {
108 		throw new InternalError("should not been called");
109 	}
110 
111 	@Override
112 	public String toHtmlString(Ability ability, ContextEventListener context) {
113 		String res = null;
114 		int[] realCode = null;
115 		if (on != null) {
116 			realCode = on.getCard(StackManager.currentAbility, null).cachedRegisters;
117 		} else {
118 			realCode = new int[codeExpr.length];
119 			for (int i = realCode.length; i-- > 0;) {
120 				try {
121 					realCode[i] = codeExpr[i].getValue(ability, null, context);
122 				} catch (Throwable t) {
123 					realCode[i] = -1;
124 				}
125 			}
126 		}
127 		if (realCode[0] != 0) {
128 			res = LanguageManagerMDB.getString("add-mana")
129 					+ MToolKit.getHtmlMana(0, realCode[0]);
130 		}
131 
132 		for (int j = IdCommonToken.COLOR_NAMES.length; j-- > 1;) {
133 			if (realCode[j] > 0) {
134 				if (res == null) {
135 					res = LanguageManagerMDB.getString("add-mana");
136 				}
137 				if (realCode[j] > PayMana.thresholdColored) {
138 					res += MToolKit.getHtmlMana(j, 1) + "x" + realCode[j];
139 				} else {
140 					res += MToolKit.getHtmlMana(j, realCode[j]);
141 				}
142 			}
143 		}
144 		if (res == null) {
145 			return LanguageManagerMDB.getString("add-mana")
146 					+ MToolKit.getHtmlMana(0, 0);
147 		}
148 		return res;
149 	}
150 
151 	@Override
152 	public String toString(Ability ability) {
153 		String res = null;
154 		int[] realCode = null;
155 		if (on != null) {
156 			realCode = on.getCard(StackManager.currentAbility, null).cachedRegisters;
157 		} else {
158 			realCode = new int[codeExpr.length];
159 			for (int i = realCode.length; i-- > 0;) {
160 				try {
161 					realCode[i] = codeExpr[i].getValue(ability, null, null);
162 				} catch (Throwable t) {
163 					realCode[i] = -1;
164 				}
165 			}
166 		}
167 		if (realCode[0] != 0) {
168 			res = "Add " + realCode[0];
169 		}
170 
171 		for (int j = IdCommonToken.COLOR_NAMES.length; j-- > 1;) {
172 			if (realCode[j] > 0) {
173 				if (res == null) {
174 					res = "Add ";
175 				}
176 				res += IdCommonToken.COLOR_NAMES[j] + "x" + realCode[j] + ",";
177 			}
178 		}
179 		if (res == null) {
180 			return "Add 0";
181 		}
182 		return res;
183 	}
184 
185 	/***
186 	 * The test manager
187 	 */
188 	private TestOn on;
189 
190 	/***
191 	 * represent the amount of mana to add to the mana pool. The complex
192 	 * expression to use for the right value. Is null if the IdToken number is not
193 	 * a complex expression.
194 	 */
195 	private Expression[] codeExpr = null;
196 
197 }