View Javadoc

1   /*
2    * Created on 27 févr. 2005
3    * 
4    *   Firemox 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.firemox.xml.tbs;
22  
23  import java.io.IOException;
24  import java.io.OutputStream;
25  import java.util.Iterator;
26  
27  import net.sf.firemox.clickable.ability.AbilityType;
28  import net.sf.firemox.clickable.ability.Optimization;
29  import net.sf.firemox.clickable.ability.Priority;
30  import net.sf.firemox.token.TrueFalseAuto;
31  import net.sf.firemox.tools.MToolKit;
32  import net.sf.firemox.xml.XmlEvent;
33  import net.sf.firemox.xml.XmlParser;
34  import net.sf.firemox.xml.XmlTbs;
35  import net.sf.firemox.xml.XmlToMDB;
36  import net.sf.firemox.xml.XmlTools;
37  
38  /***
39   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
40   * @since 0.82
41   */
42  public class Replacementability implements XmlToMDB {
43  
44  	/***
45  	 * <ul>
46  	 * Structure of stream : Data[size]
47  	 * <li>ability type [1]</li>
48  	 * <li>name [String]</li>
49  	 * <li>ability tags = hidden+unique [1]</li>
50  	 * <li>event [MEventListener]</li>
51  	 * <li>pay actions[Action[]]</li>
52  	 * </ul>
53  	 * 
54  	 * @param node
55  	 *          the XML ability structure
56  	 * @param out
57  	 *          output stream where the card structure will be saved
58  	 * @return the amount of written action in the output.
59  	 * @see net.sf.firemox.clickable.ability.TriggeredAbility
60  	 * @see net.sf.firemox.clickable.ability.ReplacementAbility
61  	 * @throws IOException
62  	 *           error while writing.
63  	 */
64  	public final int buildMdb(XmlParser.Node node, OutputStream out)
65  			throws IOException {
66  		// write the type
67  		AbilityType.REPLACEMENT_ABILITY.write(out);
68  
69  		// write the name
70  		MToolKit.writeString(out, node.getAttribute("name"));
71  
72  		// write the resolution and optimization
73  		String layer = node.getAttribute("layer");
74  		if (layer == null) {
75  			Priority.high.write(out);
76  		} else {
77  			Priority.values()[Integer.parseInt(layer)].write(out);
78  		}
79  
80  		if (Optimization.action.name().equals(node.getAttribute("optimize"))) {
81  			Optimization.action.write(out);
82  		} else {
83  			Optimization.first.write(out);
84  		}
85  		TrueFalseAuto.valueOfXsd(node.getAttribute("play-as-spell")).serialize(out);
86  
87  		// write the event
88  		Iterator<?> it = node.iterator();
89  		XmlTools.defaultOnMeTag = false;
90  		while (it.hasNext()) {
91  			Object obj = it.next();
92  			if (obj instanceof XmlParser.Node) {
93  				XmlParser.Node child = (XmlParser.Node) obj;
94  				if ("text".equals(child.getTag())) {
95  					System.out.println("text element in ability is not yet implement");
96  				} else {
97  					XmlEvent.getEvent(child.getTag()).buildMdb(child, out);
98  					break;
99  				}
100 			}
101 		}
102 
103 		// write the actions cast : none
104 		out.write(0);
105 		// write only the actions effects
106 		XmlTbs.currentInEffect = true;
107 		XmlTbs.writeActionList(node.get("effects"), out);
108 		return 0;
109 	}
110 
111 }