View Javadoc

1   /*
2    * Created on 25 feb. 2004
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   */
22  package net.sf.magicproject.test;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.util.List;
27  
28  import net.sf.magicproject.clickable.ability.Ability;
29  import net.sf.magicproject.clickable.targetable.Targetable;
30  import net.sf.magicproject.clickable.targetable.card.MCard;
31  import net.sf.magicproject.event.MEventListener;
32  import net.sf.magicproject.expression.Expression;
33  import net.sf.magicproject.expression.ExpressionFactory;
34  
35  /***
36   * This is a class representing a comparaison of two integer values.
37   * 
38   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
39   * @since 0.54
40   */
41  public abstract class TestExpr extends Test {
42  
43  	/***
44  	 * Create an instance of TestExpr by reading a file. Offset's file must
45  	 * pointing on the first byte of this test <br>
46  	 * <ul>
47  	 * Structure of InputStream : Data[size]
48  	 * <li>idTest [1]</li>
49  	 * <li>left value [...]</li>
50  	 * <li>right value [...]</li>
51  	 * </ul>
52  	 * 
53  	 * @param inputFile
54  	 *          is the file containing this event
55  	 * @throws IOException
56  	 *           if error occurred during the reading process from the specified
57  	 *           input stream
58  	 */
59  	protected TestExpr(InputStream inputFile) throws IOException {
60  		super(inputFile);
61  		leftExpression = ExpressionFactory.readNextExpression(inputFile);
62  		rightExpression = ExpressionFactory.readNextExpression(inputFile);
63  	}
64  
65  	/***
66  	 * Create a new instance of TestExpr given left and right expression.
67  	 * 
68  	 * @param leftExpression
69  	 *          the left expression of this test.
70  	 * @param rightExpression
71  	 *          the right expression of this test.
72  	 */
73  	protected TestExpr(Expression leftExpression, Expression rightExpression) {
74  		super();
75  		this.leftExpression = leftExpression;
76  		this.rightExpression = rightExpression;
77  	}
78  
79  	/***
80  	 * Return the left value of this test.
81  	 * 
82  	 * @param ability
83  	 *          is the ability owning this test. The card component of this
84  	 *          ability should correspond to the card owning this test too.
85  	 * @param tested
86  	 *          the tested card
87  	 * @return the left value of this test.
88  	 */
89  	protected int getLeftValue(Ability ability, Targetable tested) {
90  		return leftExpression.getValue(ability, tested, null);
91  	}
92  
93  	/***
94  	 * Return the right value of this test.
95  	 * 
96  	 * @param ability
97  	 *          is the ability owning this test. The card component of this
98  	 *          ability should correspond to the card owning this test too.
99  	 * @param tested
100 	 *          the tested card
101 	 * @return the right value of this test.
102 	 */
103 	protected int getRightValue(Ability ability, Targetable tested) {
104 		return rightExpression.getValue(ability, tested, null);
105 	}
106 
107 	@Override
108 	public abstract boolean test(Ability ability, Targetable tested);
109 
110 	@Override
111 	public void extractTriggeredEvents(List<MEventListener> res, MCard source,
112 			Test globalTest) {
113 		leftExpression.extractTriggeredEvents(res, source, globalTest);
114 		rightExpression.extractTriggeredEvents(res, source, globalTest);
115 	}
116 
117 	/***
118 	 * left integer expression. The complex expression to use for the left value.
119 	 * Is null if the IdToken number is not a complex expression.
120 	 */
121 	protected final Expression leftExpression;
122 
123 	/***
124 	 * right integer expression. The complex expression to use for the right
125 	 * value. Is null if the IdToken number is not a complex expression.
126 	 */
127 	protected final Expression rightExpression;
128 
129 	@Override
130 	public boolean testPreemption(Ability ability, Targetable tested) {
131 		if (!leftExpression.canBePreempted() || !rightExpression.canBePreempted())
132 			return true;
133 		return super.testPreemption(ability, tested);
134 	}
135 }