View Javadoc

1   /*
2    * MEventModifiedRegisterRange.java
3    * Created on 2005/08/23
4    * 
5    *   Magic-Project is a turn based strategy simulator
6    *   Copyright (C) 2003-2007 Fabrice Daugan
7    *
8    *   This program is free software; you can redistribute it and/or modify it 
9    * under the terms of the GNU General Public License as published by the Free 
10   * Software Foundation; either version 2 of the License, or (at your option) any
11   * later version.
12   *
13   *   This program is distributed in the hope that it will be useful, but WITHOUT 
14   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
16   * details.
17   *
18   *   You should have received a copy of the GNU General Public License along  
19   * with this program; if not, write to the Free Software Foundation, Inc., 
20   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21   */
22  package net.sf.magicproject.event;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  
27  import net.sf.magicproject.clickable.ability.Ability;
28  import net.sf.magicproject.clickable.targetable.Targetable;
29  import net.sf.magicproject.clickable.targetable.card.MCard;
30  import net.sf.magicproject.expression.Expression;
31  import net.sf.magicproject.expression.ExpressionFactory;
32  import net.sf.magicproject.operation.Any;
33  import net.sf.magicproject.operation.Operation;
34  import net.sf.magicproject.test.Test;
35  import net.sf.magicproject.token.Register;
36  
37  /***
38   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
39   * @since 0.86
40   */
41  public class ModifiedRegisterRange extends ModifiedRegister {
42  
43  	/***
44  	 * Create an instance of MEventModifiedRegisterRange by reading a file
45  	 * Offset's file must pointing on the first byte of this event <br>
46  	 * <ul>
47  	 * Structure of InputStream : Data[size]
48  	 * <li>[super]</li>
49  	 * <li>sup [Expression]</li>
50  	 * </ul>
51  	 * 
52  	 * @param inputFile
53  	 *          is the file containing this event
54  	 * @param card
55  	 *          is the card owning this event
56  	 * @throws IOException
57  	 *           if error occurred during the reading process from the specified
58  	 *           input stream
59  	 */
60  	ModifiedRegisterRange(InputStream inputFile, MCard card) throws IOException {
61  		super(inputFile, card);
62  		sup = ExpressionFactory.readNextExpression(inputFile);
63  	}
64  
65  	/***
66  	 * Creates a new instance of MEventModifiedRegisterRange specifying all
67  	 * attributes of this class. All parameters are copied, not cloned. So this
68  	 * new object shares the card and the specified codes
69  	 * 
70  	 * @param idZone
71  	 *          the place constraint to activate this event
72  	 * @param sourceTest
73  	 *          the test applied on card modifying the card.
74  	 * @param testModified
75  	 *          the test applied on the modified component.
76  	 * @param card
77  	 *          is the card owning this card
78  	 * @param op
79  	 *          the looked for operation. Any or specific operation instance.
80  	 * @param register
81  	 *          the modified register
82  	 * @param inf
83  	 *          the inferior range looked for this event
84  	 * @param sup
85  	 *          the superior range looked for this event
86  	 */
87  	public ModifiedRegisterRange(int idZone, Test sourceTest, Test testModified,
88  			MCard card, Operation op, Register register, Expression inf,
89  			Expression sup) {
90  		super(idZone, sourceTest, testModified, card, op, register, inf);
91  		this.sup = sup;
92  	}
93  
94  	@Override
95  	public MEventListener clone(MCard card) {
96  		return new ModifiedRegisterRange(idZone, test, testModified, card, op,
97  				register, index, sup);
98  	}
99  
100 	@Override
101 	public boolean isMatching(Ability ability, Targetable modified, MCard source,
102 			Operation op, int register, int index) {
103 		final int indexValue = this.index.getValue(ability, modified, null);
104 		return this.register.ordinal() == register && indexValue <= index
105 				&& indexValue >= index
106 				&& (this.op == op || this.op == Any.getInstance())
107 				&& testModified.test(ability, modified) && test(ability, source);
108 	}
109 
110 	/***
111 	 * The superior range looked for this event
112 	 */
113 	protected Expression sup;
114 
115 }