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   */
20  package net.sf.magicproject.action;
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  
25  import net.sf.magicproject.clickable.ability.Ability;
26  import net.sf.magicproject.event.context.ContextEventListener;
27  import net.sf.magicproject.expression.Expression;
28  import net.sf.magicproject.expression.ExpressionFactory;
29  import net.sf.magicproject.tools.MToolKit;
30  import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
31  
32  /***
33   * It's the mana source action, modify directly the mana pool like : Land, other
34   * mana sources. Only one color is added.
35   * 
36   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
37   * @since 0.86
38   */
39  class GiveManaBasic extends GiveMana {
40  
41  	/***
42  	 * Create an instance of GiveManaBasic by reading a file Offset's file must
43  	 * pointing on the first byte of this action <br>
44  	 * <ul>
45  	 * Structure of stream : Data[size]
46  	 * <li>restriction usage test [...]</li>
47  	 * <li>idPlayer receiving this mana [2]</li>
48  	 * <li>mana color to give : expression [...]</li>
49  	 * <li>mana amount to give : expression [...]</li>
50  	 * </ul>
51  	 * 
52  	 * @param inputFile
53  	 *          file containing this action
54  	 * @throws IOException
55  	 *           if error occurred during the reading process from the specified
56  	 *           input stream
57  	 */
58  	GiveManaBasic(InputStream inputFile) throws IOException {
59  		super(inputFile);
60  		this.manaColor = ExpressionFactory.readNextExpression(inputFile);
61  		this.valueExpr = ExpressionFactory.readNextExpression(inputFile);
62  	}
63  
64  	@Override
65  	public boolean play(ContextEventListener context, Ability ability) {
66  		giveMana(ability, context, manaColor.getValue(ability, null, context),
67  				valueExpr.getValue(ability, null, context));
68  		return false;
69  	}
70  
71  	@Override
72  	public String toHtmlString(Ability ability, ContextEventListener context) {
73  		String res = null;
74  		final int realCode = valueExpr.getValue(ability, null, context);
75  		final int color = manaColor.getValue(ability, null, context);
76  		if (color <= 0) {
77  			if (realCode > 0) {
78  				res = LanguageManagerMDB.getString("add-mana")
79  						+ MToolKit.getHtmlMana(0, realCode);
80  			}
81  		} else if (realCode > 0) {
82  			res = LanguageManagerMDB.getString("add-mana");
83  			if (realCode > PayMana.thresholdColored) {
84  				res += MToolKit.getHtmlMana(color, 1) + "x" + realCode;
85  			} else {
86  				res += MToolKit.getHtmlMana(color, realCode);
87  			}
88  		}
89  		if (res == null) {
90  			return LanguageManagerMDB.getString("add-mana")
91  					+ MToolKit.getHtmlMana(0, 0);
92  		}
93  		return res;
94  	}
95  
96  	@Override
97  	public String toString(Ability ability) {
98  		String res = null;
99  		final int realCode = valueExpr.getValue(ability, null, null);
100 		final int color = manaColor.getValue(ability, null, null);
101 		if (color == 0) {
102 			if (realCode > 0) {
103 				res = "Add " + realCode;
104 			}
105 		} else if (color < 0) {
106 			res = "Add ?? x" + realCode + ",";
107 		} else if (realCode > 0) {
108 			res = "Add color" + color + "x" + realCode + ",";
109 		}
110 		if (res == null) {
111 			return "Add 0";
112 		}
113 		return res;
114 	}
115 
116 	@Override
117 	public final Actiontype getIdAction() {
118 		return Actiontype.GIVE_MANA_BASIC;
119 	}
120 
121 	/***
122 	 * The mana's color
123 	 */
124 	private Expression manaColor;
125 
126 	/***
127 	 * represent the amount of mana to add to the mana pool.
128 	 */
129 	private Expression valueExpr = null;
130 
131 }