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  package net.sf.magicproject.action;
20  
21  import net.sf.magicproject.clickable.ability.Ability;
22  import net.sf.magicproject.event.context.ContextEventListener;
23  import net.sf.magicproject.test.Test;
24  import net.sf.magicproject.token.IdConst;
25  
26  /***
27   * This class is representing an atom action. Each action can generate more than
28   * an event.
29   * 
30   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
31   * @since 0.3
32   * @since 0.85 named action, and complex constraints are supported.
33   */
34  public abstract class MAction {
35  
36  	/***
37  	 * The debug data, may be empty.
38  	 */
39  	protected final String debugData;
40  
41  	/***
42  	 * Creates a new instance of MAction
43  	 * 
44  	 * @param debugData
45  	 *          optional debug info attached to this action.
46  	 */
47  	protected MAction(String debugData) {
48  		super();
49  		this.debugData = debugData;
50  	}
51  
52  	/***
53  	 * Creates a new instance of MAction
54  	 */
55  	protected MAction() {
56  		this(null);
57  	}
58  
59  	/***
60  	 * Return the index of this action. As default, this is a zero id
61  	 * 
62  	 * @return the index of this action.
63  	 * @see net.sf.magicproject.action.Actiontype
64  	 */
65  	public abstract Actiontype getIdAction();
66  
67  	/***
68  	 * Return the amount of mana needed (constant part only) to play this ability
69  	 * As default, we return an empty number for all manas.
70  	 * 
71  	 * @param ability
72  	 *          is the ability owning this test. The card component of this
73  	 *          ability should correspond to the card owning this test too.
74  	 * @param context
75  	 *          the context of playing ability.
76  	 * @return array of mana needed to play this ability
77  	 */
78  	public int[] manaNeeded(Ability ability, ContextEventListener context) {
79  		return IdConst.EMPTY_CODE;
80  	}
81  
82  	/***
83  	 * Return the HTML code representing this action. If no picture is associated
84  	 * to this action, only text will be returned.
85  	 * 
86  	 * @param ability
87  	 *          is the ability owning this test. The card component of this
88  	 *          ability should correspond to the card owning this test too.
89  	 * @param context
90  	 *          is the context attached to this action.
91  	 * @return the HTML code representing this action. If no picture is associated
92  	 *         to this action, only text will be returned.
93  	 * @see #toString(Ability)
94  	 * @see #toHtmlString(Ability, int, ContextEventListener)
95  	 * @since 0.85
96  	 */
97  	public String toHtmlString(Ability ability, ContextEventListener context) {
98  		return toString(ability);
99  	}
100 
101 	/***
102 	 * Return the HTML code representing this action. If no picture is associated
103 	 * to this action, only text will be returned.
104 	 * 
105 	 * @param ability
106 	 *          is the ability owning this test. The card component of this
107 	 *          ability should correspond to the card owning this test too.
108 	 * @param htmlString
109 	 *          the HTML code defined for this string.
110 	 * @return the HTML code representing this action. If no picture is associated
111 	 *         to this action, only text will be returned.
112 	 * @see #toString(Ability)
113 	 * @since 0.85
114 	 */
115 	protected String toHtmlString(Ability ability, String htmlString) {
116 		return htmlString;
117 	}
118 
119 	/***
120 	 * Return the HTML code representing this action. If this action is named,
121 	 * it's name will be returned. If not, if existing, the picture associated to
122 	 * this action is returned. Otherwise, built-in action's text will be
123 	 * returned.
124 	 * 
125 	 * @param ability
126 	 *          is the ability owning this test. The card component of this
127 	 *          ability should correspond to the card owning this test too.
128 	 * @param times
129 	 *          the times to repeat this action.
130 	 * @param context
131 	 *          is the context attached to this action.
132 	 * @return the HTML code representing this action. If this action is named,
133 	 *         it's name will be returned. If not, if existing, the picture
134 	 *         associated to this action is returned. Otherwise, built-in action's
135 	 *         text will be returned.
136 	 * @see #toString(Ability)
137 	 * @see #toHtmlString(Ability, ContextEventListener)
138 	 * @since 0.85
139 	 */
140 	public String toHtmlString(Ability ability, int times,
141 			ContextEventListener context) {
142 		if (times > 1) {
143 			final String res = toHtmlString(ability, context);
144 			if (res.length() == 0) {
145 				return null;
146 			}
147 			return "[" + toHtmlString(ability, context) + "] x " + times;
148 		}
149 		return toHtmlString(ability, context);
150 	}
151 
152 	/***
153 	 * String representation of this action.
154 	 * 
155 	 * @param ability
156 	 *          is the ability owning this test. The card component of this
157 	 *          ability should correspond to the card owning this test too.
158 	 * @return action name.
159 	 * @see Object#toString()
160 	 */
161 	public abstract String toString(Ability ability);
162 
163 	@Override
164 	public String toString() {
165 		if (debugData != null && debugData.length() > 0)
166 			return debugData;
167 		return super.toString();
168 	}
169 
170 	/***
171 	 * Return the given test where values depending on values of this action have
172 	 * been replaced.
173 	 * 
174 	 * @param test
175 	 *          the test containing eventually some values depending on parameters
176 	 *          of this action.
177 	 * @return a parsed test.
178 	 * @since 0.85
179 	 */
180 	public Test parseTest(Test test) {
181 		return test;
182 	}
183 
184 	/***
185 	 * Return true if this action matches with the given action.
186 	 * 
187 	 * @param constraintAction
188 	 *          the constraint action.
189 	 * @return true if this action matches with the given action.
190 	 * @since 0.85
191 	 */
192 	public boolean equal(MAction constraintAction) {
193 		return false;
194 	}
195 
196 	/***
197 	 * Return the name of this action
198 	 * 
199 	 * @return the name of this action
200 	 */
201 	public String getActionName() {
202 		return toString();
203 	}
204 
205 }