View Javadoc

1   /*
2    * Created on 27 févr. 2005
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.xml.action;
22  
23  import java.io.IOException;
24  import java.io.OutputStream;
25  
26  import net.sf.magicproject.action.Actiontype;
27  import net.sf.magicproject.operation.IdOperations;
28  import net.sf.magicproject.test.TestOn;
29  import net.sf.magicproject.token.IdCommonToken;
30  import net.sf.magicproject.token.IdTokens;
31  import net.sf.magicproject.tools.MToolKit;
32  import net.sf.magicproject.xml.XmlAction;
33  import net.sf.magicproject.xml.XmlConfiguration;
34  import net.sf.magicproject.xml.XmlParser;
35  import net.sf.magicproject.xml.XmlToMDB;
36  import net.sf.magicproject.xml.XmlTools;
37  
38  /***
39   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
40   * @since 0.82
41   */
42  public class Paymana implements XmlToMDB {
43  
44  	/***
45  	 * <ul>
46  	 * Structure of stream : Data[size]
47  	 * <li>idAction [1]</li>
48  	 * <li>the player paying this mana [TestOn]</li>
49  	 * <li>idNumber COLORLESS or IdTokens#REGISTERS[2]</li>
50  	 * <li>idNumber BLACK [2]</li>
51  	 * <li>idNumber BLUE [2]</li>
52  	 * <li>idNumber GREEN [2]</li>
53  	 * <li>idNumber RED [2]</li>
54  	 * <li>idNumber WHITE [2]</li>
55  	 * <li>colors of X = 2^ID__Color1+2^ID__Color2...[2]</li>
56  	 * </ul>
57  	 * 
58  	 * @param node
59  	 *          the XML action structure
60  	 * @param out
61  	 *          outputstream where the card structure will be saved.
62  	 * @return the amount of written action in the output.
63  	 * @throws IOException
64  	 */
65  	public final int buildMdb(XmlParser.Node node, OutputStream out)
66  			throws IOException {
67  		XmlAction.buildMdb(Actiontype.PAY_MANA, node, out);
68  		// write the specified controller
69  		String controllerAttr = node.getAttribute("controller");
70  		if (controllerAttr == null) {
71  			TestOn.CONTROLLER.serialize(out);
72  		} else {
73  			TestOn.serialize(out, controllerAttr);
74  		}
75  
76  		if (XmlConfiguration.noPayMana) {
77  			// we write this action all required mana set to 0
78  			MToolKit.writeInt16(out, 0); // dummy mode
79  			for (int i = 0; i < IdCommonToken.COLOR_NAMES.length; i++) {
80  				out.write(IdOperations.INT_VALUE);
81  				MToolKit.writeInt16(out, 0);
82  			}
83  		} else {
84  			String braAttr = node.getAttribute("value");
85  			if (braAttr != null) {
86  				MToolKit.writeInt16(out, IdTokens.MANA_POOL); // registe mode
87  				XmlTools.writeTestOn(out, node.getAttribute("card"));
88  			} else {
89  				MToolKit.writeInt16(out, 0); // dummy mode
90  				for (int i = 0; i < IdCommonToken.COLOR_NAMES.length; i++) {
91  					if (node.getAttribute(IdCommonToken.COLOR_NAMES[i]) == null
92  							&& node.get(IdCommonToken.COLOR_NAMES[i]) == null) {
93  						// no such mana to pay for this color, so 0
94  						out.write(IdOperations.INT_VALUE);
95  						MToolKit.writeInt16(out, 0);
96  					} else {
97  						XmlTools.writeAttrOptions(node, IdCommonToken.COLOR_NAMES[i], out);
98  					}
99  				}
100 			}
101 		}
102 		return 1;
103 	}
104 
105 }