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