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.modifier;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  
24  import net.sf.magicproject.clickable.ability.Ability;
25  import net.sf.magicproject.clickable.targetable.card.MCard;
26  import net.sf.magicproject.expression.IntValue;
27  import net.sf.magicproject.test.Test;
28  
29  /***
30   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
31   * @since 0.85
32   */
33  class ObjectRegisterModifierModel extends RegisterModifierModel implements
34  		ObjectModifierModel {
35  
36  	/***
37  	 * Is this object can be paint.
38  	 */
39  	private final boolean paint;
40  
41  	/***
42  	 * Creates a new instance of ObjectRegisterModifierModel <br>
43  	 * <ul>
44  	 * Structure of InputStream : Data[size]
45  	 * <li>[super]</li>
46  	 * <li>is this object can be paint [boolean]</li>
47  	 * </ul>
48  	 * 
49  	 * @param inputFile
50  	 *          is the file containing this event
51  	 * @throws IOException
52  	 *           if error occurred during the reading process from the specified
53  	 *           input stream
54  	 * @see net.sf.magicproject.token.IdTokens
55  	 */
56  	ObjectRegisterModifierModel(InputStream inputStream) throws IOException {
57  		super(inputStream);
58  		paint = inputStream.read() == 1;
59  
60  		// do not remove modifier if the move is PLAY to PLAY
61  		linked = false;
62  	}
63  
64  	/***
65  	 * Create a new instance from the given model.
66  	 * 
67  	 * @param other
68  	 *          the instance to copy
69  	 */
70  	private ObjectRegisterModifierModel(ObjectModifierModel other) {
71  		super((ObjectRegisterModifierModel) other);
72  		this.paint = ((ObjectRegisterModifierModel) other).paint;
73  	}
74  
75  	@Override
76  	public void addModifierFromModel(Ability ability, MCard target) {
77  		final int index = this.getModifiedIndex();
78  		final ObjectRegisterModifier newModifier = new ObjectRegisterModifier(name,
79  				target, ability, whileCondition, linkedEvents, until, linked, layer,
80  				index, recalculate ? new IntValue(rightExpression.getValue(ability,
81  						target, null)) : rightExpression, op, paint);
82  		target.addModifier(newModifier, index);
83  		newModifier.refresh();
84  		if (next != null) {
85  			next.addModifierFromModel(ability, target);
86  		}
87  	}
88  
89  	@Override
90  	public ObjectRegisterModifierModel clone() {
91  		return new ObjectRegisterModifierModel(this);
92  	}
93  
94  	/***
95  	 * Return the modified index.
96  	 * 
97  	 * @return the modified index.
98  	 */
99  	public int getModifiedIndex() {
100 		return this.index.getValue(null, null, null);
101 	}
102 
103 	public String getObjectName() {
104 		return name;
105 	}
106 
107 	@Override
108 	public void removeObject(MCard fromCard, Test objectTest) {
109 		final int index = this.getModifiedIndex();
110 		if (fromCard.registerModifiers[index] != null) {
111 			fromCard.registerModifiers[index] = (RegisterModifier) fromCard.registerModifiers[index]
112 					.removeObject(name, objectTest);
113 		}
114 		super.removeObject(fromCard, objectTest);
115 	}
116 
117 	public int getNbObject(MCard card, Test objectTest) {
118 		return card.registerModifiers[ObjectFactory.getObjectRegisterModifierModel(
119 				name).getModifiedIndex()] == null ? 0
120 				: card.registerModifiers[ObjectFactory.getObjectRegisterModifierModel(
121 						name).getModifiedIndex()].getNbObjects(name, objectTest);
122 	}
123 
124 }