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.XmlParser;
34  import net.sf.magicproject.xml.XmlTest;
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   * @since 0.86 unique color give and restriction usage are supported
42   */
43  public class Givemana implements XmlToMDB {
44  
45  	/***
46  	 * <ul>
47  	 * Structure of stream : Data[size]
48  	 * <li>idAction GIVE_MANA_BASIC/GIVE_MANA_MULTI[1]</li>
49  	 * <li>restriction usage test [...]</li>
50  	 * <li>player receiving this mana [TestOn]</li>
51  	 * <li>For GIVE_MANA_BASIC option : mana color to give : idNumber [2]</li>
52  	 * <li>For GIVE_MANA_BASIC option : mana amount to give : expression [...]</li>
53  	 * <li>For GIVE_MANA_MULTI option : COLORLESS : expression [...] or
54  	 * (IdTokens#REGISTERS)[2]</li>
55  	 * <li>For GIVE_MANA_MULTI option : BLACK : expression[...]</li>
56  	 * <li>For GIVE_MANA_MULTI option : BLUE : expression[...]</li>
57  	 * <li>For GIVE_MANA_MULTI option : GREEN : expression[...]</li>
58  	 * <li>For GIVE_MANA_MULTI option : RED : expression[...]</li>
59  	 * <li>For GIVE_MANA_MULTI option : WHITE : expression[...]</li>
60  	 * </ul>
61  	 * 
62  	 * @param node
63  	 *          the XML action structure
64  	 * @param out
65  	 *          outputstream where the card structure will be saved.
66  	 * @return the amount of written action in the output.
67  	 * @throws IOException
68  	 */
69  	public int buildMdb(XmlParser.Node node, OutputStream out) throws IOException {
70  		// write the type
71  		final String uniqueColor = node.getAttribute("color");
72  		if (uniqueColor != null || node.get("color") != null)
73  			XmlAction.buildMdb(Actiontype.GIVE_MANA_BASIC, node, out);
74  		else
75  			XmlAction.buildMdb(Actiontype.GIVE_MANA_MULTI, node, out);
76  
77  		// Write restriction usage test
78  		XmlTest.getTest("test").buildMdb(node.get("restriction"), out);
79  
80  		// Write destination player
81  		if (node.getAttribute("to") == null) {
82  			TestOn.CONTROLLER.serialize(out);
83  		} else {
84  			XmlTools.writeTestOn(out, node.getAttribute("to"));
85  		}
86  
87  		if (uniqueColor != null || node.get("color") != null) {
88  			// only one color to modify, write amount to add
89  			XmlTools.writeAttrOptions(node, "color", out);
90  			XmlTools.writeAttrOptions(node, "value", out);
91  		} else {
92  			final String braAttr = node.getAttribute("value");
93  			if (braAttr != null) {
94  				MToolKit.writeInt16(out, IdTokens.MANA_POOL); // manacost mode
95  				XmlTools.writeTestOn(out, node.getAttribute("card"));
96  			} else {
97  				MToolKit.writeInt16(out, 0); // non manacost mode
98  				for (int i = 0; i < IdCommonToken.COLOR_NAMES.length; i++) {
99  					if (node.getAttribute(IdCommonToken.COLOR_NAMES[i]) == null
100 							&& node.get(IdCommonToken.COLOR_NAMES[i]) == null) {
101 						// no such mana to add for this color, so 0
102 						out.write(IdOperations.INT_VALUE);
103 						MToolKit.writeInt16(out, 0);
104 					} else {
105 						XmlTools.writeAttrOptions(node, IdCommonToken.COLOR_NAMES[i], out);
106 					}
107 				}
108 			}
109 		}
110 		return 1;
111 	}
112 
113 }