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   */
20  
21  package net.sf.magicproject.clickable.targetable.card;
22  
23  import java.io.InputStream;
24  import java.util.HashSet;
25  import java.util.List;
26  
27  import net.sf.magicproject.clickable.ability.Ability;
28  import net.sf.magicproject.clickable.ability.AbilityFactory;
29  import net.sf.magicproject.modifier.ModifierFactory;
30  import net.sf.magicproject.modifier.ModifierModel;
31  import net.sf.magicproject.token.IdTokens;
32  import net.sf.magicproject.tools.MToolKit;
33  import net.sf.magicproject.ui.i18n.LanguageManager;
34  
35  /***
36   * @version 0.91
37   * @since 0.90
38   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
39   * @author <a href="mailto:kismet-sl@users.sourceforge.net">Stefano "Kismet"
40   *         Lenzi</a>
41   */
42  public class CardModelImpl implements CardModel {
43  	/***
44  	 * The shared modifier models (all cards with same id have the same modifier
45  	 * models objects)
46  	 */
47  	protected ModifierModel modifierModels;
48  
49  	/***
50  	 * List of actions when casting : Ability[]
51  	 */
52  	protected Ability[] abilities;
53  
54  	/***
55  	 * Properties of this card. These properties are these one readable on these
56  	 * card.
57  	 */
58  	protected int[] properties;
59  
60  	/***
61  	 * Shared regiters of this targetable. This register would never be modifier.
62  	 */
63  	protected int[] staticRegisters;
64  
65  	/***
66  	 * Colors of this card.
67  	 */
68  	protected int idColor;
69  
70  	/***
71  	 * Card type of this card.
72  	 */
73  	protected int idCard;
74  
75  	/***
76  	 * Set of keywords.
77  	 */
78  	protected java.util.Set<String> keywords;
79  
80  	/***
81  	 * The translated name. If null or empty, it would be equals to the given
82  	 * english name.
83  	 */
84  	protected String localName;
85  
86  	/***
87  	 * The english name of this card. May contain any character. If null or empty,
88  	 * it would be equals to the card name (id).
89  	 */
90  	protected String cardName;
91  
92  	/***
93  	 * The file-serializable card name.
94  	 */
95  	protected String keyName;
96  
97  	/***
98  	 * XML rule designer of loaded cards.
99  	 */
100 	protected String cardRulesCredits;
101 
102 	/***
103 	 * The language used for this card. Bu default, is
104 	 * <code>LanguageManager.DEFAULT_LANGUAGE</code>
105 	 */
106 	private String language = LanguageManager.DEFAULT_LANGUAGE;
107 
108 	/***
109 	 * 
110 	 */
111 	protected Attachment attachment;
112 
113 	public String getLanguage() {
114 		return language;
115 	}
116 
117 	public String getKeyName() {
118 		return keyName;
119 	}
120 
121 	public String getLocalName() {
122 		return localName;
123 	}
124 
125 	public String getCardName() {
126 		return cardName;
127 	}
128 
129 	public void setRulesCredits(String cardRulesCredits) {
130 		if (cardRulesCredits == null || cardRulesCredits.trim().length() == 0) {
131 			this.cardRulesCredits = null;
132 		} else {
133 			this.cardRulesCredits = cardRulesCredits.intern();
134 		}
135 	}
136 
137 	public void setLocalName(String localName) {
138 		if (isValidValue(localName)) {
139 			this.localName = localName;
140 		}
141 	}
142 
143 	public String getRulesCredit() {
144 		return cardRulesCredits;
145 	}
146 
147 	public boolean hasKeywords(String keyword) {
148 		return keywords != null && keywords.contains(keyword);
149 	}
150 
151 	/***
152 	 * Creates a new instance with the specified card name(id). The translated and
153 	 * english names are initialized to this id.
154 	 * 
155 	 * @param cardName
156 	 *          the card name (id) of this instance.
157 	 */
158 	public CardModelImpl(String cardName) {
159 		setCardName(cardName);
160 		this.abilities = new Ability[0];
161 		this.properties = new int[0];
162 		this.staticRegisters = new int[0];
163 		this.keywords = new HashSet<String>(0);
164 		this.attachment = null;
165 	}
166 
167 	/***
168 	 * Complete this instance with content of specified stream.<br>
169 	 * Structure of InputStream : Data[size]
170 	 * <ul>
171 	 * <li>card name [String]</li>
172 	 * <li>art author [String]</li>
173 	 * <li>rules author [String]</li>
174 	 * <li>nb keywords [1]</li>
175 	 * <li>keyword [String[]]</li>
176 	 * <li>registers [IdTokens.CARD_REGISTER_SIZE]</li>
177 	 * <li>idCard [2]</li>
178 	 * <li>idColor [1]</li>
179 	 * <li>abilities [Ability[]]</li>
180 	 * <li>sorted properties [int[]]</li>
181 	 * <li>modifiers [Modifiers[]]</li>
182 	 * <li>attachement 0/1 [1]</li>
183 	 * <li>attachements [Attachment[]]</li>
184 	 * </ul>
185 	 * Creates a new instance with the specified card name(id). The translated and
186 	 * english names are initialized to this id.
187 	 * 
188 	 * @param cardName
189 	 *          the card name (id) of this instance.
190 	 * @param inputStream
191 	 *          the input stream containing the data of this card.
192 	 */
193 	public CardModelImpl(String cardName, InputStream inputStream) {
194 		this(cardName);
195 		try {
196 			setRulesCredits(MToolKit.readString(inputStream));
197 			int count = inputStream.read();
198 			if (count > 0) {
199 				while (count-- > 0) {
200 					keywords.add(MToolKit.readString(inputStream));
201 				}
202 			}
203 
204 			// read registers (mana cost,can use tap,power, tougthness)
205 			staticRegisters = new int[IdTokens.CARD_REGISTER_SIZE];
206 			for (int i = 0; i < IdTokens.CARD_REGISTER_SIZE; i++) {
207 				staticRegisters[i] = inputStream.read();
208 			}
209 
210 			// Read card type, coded with 2 byte
211 			idCard = MToolKit.readInt16(inputStream);
212 
213 			// Read card color, coded with one byte
214 			idColor = inputStream.read();
215 
216 			// Read list of abilities
217 			abilities = new Ability[inputStream.read()];
218 			for (int a = abilities.length; a-- > 0;) {
219 				abilities[a] = AbilityFactory.readAbility(inputStream,
220 						SystemCard.instance);
221 			}
222 
223 			// Read list of proprieties, i.e. first strike,trample,Legend,Knight,...
224 			properties = new int[inputStream.read()];
225 			for (int a = 0; a < properties.length; a++) {
226 				properties[a] = MToolKit.readInt16(inputStream);
227 			}
228 
229 			// Read list of modifiers
230 			count = inputStream.read();
231 			while (count-- > 0) {
232 				if (modifierModels == null) {
233 					modifierModels = ModifierFactory.readModifier(inputStream);
234 				} else {
235 					modifierModels.addModel(ModifierFactory.readModifier(inputStream));
236 				}
237 			}
238 
239 			// Read attachment of ths card
240 			if (inputStream.read() != 0)
241 				attachment = new Attachment(inputStream);
242 		} catch (Throwable e) {
243 			throw new RuntimeException("reading card " + CardFactory.lastCardName, e);
244 		}
245 	}
246 
247 	/***
248 	 * Create a new instance of this class from another instance.
249 	 * 
250 	 * @param other
251 	 *          the model's properties to copy to this instance.
252 	 */
253 	public CardModelImpl(CardModelImpl other) {
254 		this(other.getCardName());
255 		keywords = other.keywords;
256 		cardRulesCredits = other.cardRulesCredits;
257 		staticRegisters = other.staticRegisters;
258 		idCard = other.idCard;
259 		idColor = other.idColor;
260 		abilities = other.abilities;
261 		properties = other.properties;
262 		modifierModels = other.modifierModels;
263 		attachment = other.attachment;
264 	}
265 
266 	private static boolean isValidValue(String str) {
267 		return str != null && str.length() > 0;
268 	}
269 
270 	@Override
271 	public String toString() {
272 		return localName;
273 	}
274 
275 	public int getIdCard() {
276 		return idCard;
277 	}
278 
279 	public int getIdColor() {
280 		return idColor;
281 	}
282 
283 	public int[] getStaticRegisters() {
284 		return this.staticRegisters;
285 	}
286 
287 	public int[] getProperties() {
288 		return properties;
289 	}
290 
291 	public Ability[] getAbilities() {
292 		return abilities;
293 	}
294 
295 	public ModifierModel getModifierModels() {
296 		return this.modifierModels;
297 	}
298 
299 	public void setModifierModels(ModifierModel modifierModels) {
300 		this.modifierModels = modifierModels;
301 	}
302 
303 	public Attachment getAttachment() {
304 		return this.attachment;
305 	}
306 
307 	public int compareTo(CardModel o) {
308 		return cardName.compareTo(o.getCardName());
309 	}
310 
311 	/***
312 	 * Set idCard.
313 	 * 
314 	 * @param idCard
315 	 *          the new idCard
316 	 */
317 	public void setIdCard(int idCard) {
318 		this.idCard = idCard;
319 	}
320 
321 	/***
322 	 * Set color set.
323 	 * 
324 	 * @param idColor
325 	 *          the new color set.
326 	 */
327 	public void setIdColor(int idColor) {
328 		this.idColor = idColor;
329 	}
330 
331 	/***
332 	 * The the new card name.
333 	 * 
334 	 * @param cardName
335 	 *          the new card name.
336 	 */
337 	public void setCardName(String cardName) {
338 		this.cardName = cardName;
339 		CardFactory.lastCardName = cardName;
340 		this.localName = cardName;
341 		this.keyName = MToolKit.getKeyName(cardName);
342 	}
343 
344 	/***
345 	 * @param abilities
346 	 */
347 	public void setAbilities(List<Ability> abilities) {
348 		this.abilities = abilities.toArray(new Ability[abilities.size()]);
349 	}
350 
351 }