View Javadoc

1   /* 
2    * IfThenHop.java
3    * Created on 20 févr. 2004
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   */
23  package net.sf.magicproject.action;
24  
25  import java.io.IOException;
26  import java.io.InputStream;
27  
28  import net.sf.magicproject.action.context.ActionContextWrapper;
29  import net.sf.magicproject.action.context.Int;
30  import net.sf.magicproject.action.handler.InitAction;
31  import net.sf.magicproject.action.handler.RollBackAction;
32  import net.sf.magicproject.action.handler.StandardAction;
33  import net.sf.magicproject.clickable.ability.Ability;
34  import net.sf.magicproject.event.context.ContextEventListener;
35  import net.sf.magicproject.expression.Expression;
36  import net.sf.magicproject.expression.ExpressionFactory;
37  import net.sf.magicproject.stack.StackManager;
38  import net.sf.magicproject.test.Test;
39  import net.sf.magicproject.test.TestFactory;
40  
41  /***
42   * Depending on result of test, jump to the next action with hop equal to 1
43   * (normal case) for a false result, and jump to a sepecified hop for the true
44   * result. Like the if...then...else instruction. <br>
45   * 
46   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
47   * @since 0.54
48   * @see net.sf.magicproject.action.Hop
49   */
50  class IfThenHop extends UserAction implements StandardAction, InitAction,
51  		RollBackAction {
52  
53  	/***
54  	 * Create an instance of IfThenHop by reading a file Offset's file must
55  	 * pointing on the first byte of this action <br>
56  	 * <ul>
57  	 * Structure of InputStream : Data[size]
58  	 * <li>test [...]</li>
59  	 * <li>"hop" for 'else' case. Is 1 for 'then' case [2]</li>
60  	 * </ul>
61  	 * 
62  	 * @param inputFile
63  	 *          file containing this action
64  	 * @throws IOException
65  	 *           if error occurred during the reading process from the specified
66  	 *           input stream
67  	 */
68  	IfThenHop(InputStream inputFile) throws IOException {
69  		super(inputFile);
70  		condition = TestFactory.readNextTest(inputFile);
71  		valueExpr = ExpressionFactory.readNextExpression(inputFile);
72  	}
73  
74  	@Override
75  	public final Actiontype getIdAction() {
76  		return Actiontype.IF_THEN_ELSE;
77  	}
78  
79  	public boolean init(ActionContextWrapper actionContext,
80  			ContextEventListener context, Ability ability) {
81  		actionContext.actionContext = new Int(condition.test(ability, ability
82  				.getCard()) ? 1 : 0);
83  		return replay(actionContext, context, ability);
84  	}
85  
86  	public boolean replay(ActionContextWrapper actionContext,
87  			ContextEventListener context, Ability ability) {
88  		if (((Int) actionContext.actionContext).getInt() == 0) {
89  			StackManager.actionManager.setHop(valueExpr.getValue(ability, null,
90  					context));
91  		}
92  		return true;
93  	}
94  
95  	public void rollback(ActionContextWrapper actionContext,
96  			ContextEventListener context, Ability ability) {
97  		// nothing to do
98  	}
99  
100 	public boolean play(ContextEventListener context, Ability ability) {
101 		if (!condition.test(ability, ability.getCard())) {
102 			StackManager.actionManager.setHop(valueExpr.getValue(ability, null,
103 					context));
104 		}
105 		return true;
106 	}
107 
108 	@Override
109 	public String toString(Ability ability) {
110 		return "If ... Then ... Else ...";
111 	}
112 
113 	/***
114 	 * The condition checked to determine the 'hop' value
115 	 */
116 	private Test condition;
117 
118 	/***
119 	 * The number of action to skip in case of false condition. In case of true
120 	 * condition, 'hop' is equal to 1. The complex expression to use for the right
121 	 * value. Is null if the IdToken number is not a complex expression.
122 	 */
123 	private Expression valueExpr = null;
124 
125 }