1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package net.sf.magicproject.action;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24
25 import net.sf.magicproject.action.context.ActionContextWrapper;
26 import net.sf.magicproject.action.context.Int;
27 import net.sf.magicproject.action.handler.InitAction;
28 import net.sf.magicproject.action.handler.RollBackAction;
29 import net.sf.magicproject.action.handler.StandardAction;
30 import net.sf.magicproject.clickable.ability.Ability;
31 import net.sf.magicproject.event.context.ContextEventListener;
32 import net.sf.magicproject.expression.Expression;
33 import net.sf.magicproject.expression.ExpressionFactory;
34 import net.sf.magicproject.stack.StackManager;
35
36 /***
37 * Change the normal jump(1) in the current actions chain, to another one . This
38 * jump can be positive or negative. Zero mean infinite loop.
39 *
40 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
41 * @since 0.60
42 * @since 0.72 support counter ability
43 */
44 public class Hop extends UserAction implements StandardAction, InitAction,
45 RollBackAction {
46
47 /***
48 * Create an instance of Hop by reading a file Offset's file must pointing on
49 * the first byte of this action
50 * <ul>
51 * Structure of InputStream : Data[size]
52 * <li>idAction [1]</li>
53 * <li>hop value [2]</li>
54 * <li>+ test if counter mode [...]</li>
55 * </ul>
56 *
57 * @param inputFile
58 * file containing this action
59 * @throws IOException
60 * if error occurred during the reading process from the specified
61 * input stream
62 */
63 Hop(InputStream inputFile) throws IOException {
64 super(inputFile);
65 valueExpr = ExpressionFactory.readNextExpression(inputFile);
66 }
67
68 @Override
69 public final Actiontype getIdAction() {
70 return Actiontype.HOP;
71 }
72
73 public boolean init(ActionContextWrapper actionContext,
74 ContextEventListener context, Ability ability) {
75 final int hopValue = valueExpr
76 .getValue(ability, ability.getCard(), context);
77 actionContext.actionContext = new Int(hopValue);
78 StackManager.actionManager.setHop(hopValue);
79 return true;
80 }
81
82 public boolean replay(ActionContextWrapper actionContext,
83 ContextEventListener context, Ability ability) {
84 final int hopValue = ((Int) actionContext.actionContext).getInt();
85 if (hopValue > 0) {
86 StackManager.actionManager.setHop(hopValue);
87 }
88 return true;
89 }
90
91 public void rollback(ActionContextWrapper actionContext,
92 ContextEventListener context, Ability ability) {
93
94 }
95
96 public boolean play(ContextEventListener context, Ability ability) {
97 StackManager.actionManager.setHop(valueExpr.getValue(ability, ability
98 .getCard(), context));
99 return true;
100 }
101
102 @Override
103 public String toString(Ability ability) {
104 return "Hop";
105 }
106
107 /***
108 * The amount of actions to skip. The complex expression to use for the right
109 * value. Is null if the IdToken number is not a complex expression.
110 */
111 private Expression valueExpr;
112
113 }