1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.firemox.action;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25
26 import net.sf.firemox.clickable.ability.Ability;
27 import net.sf.firemox.clickable.target.Target;
28 import net.sf.firemox.clickable.target.card.MCard;
29 import net.sf.firemox.event.AssignedDamage;
30 import net.sf.firemox.event.context.ContextEventListener;
31 import net.sf.firemox.expression.Expression;
32 import net.sf.firemox.expression.ExpressionFactory;
33 import net.sf.firemox.stack.StackManager;
34 import net.sf.firemox.token.IdConst;
35 import net.sf.firemox.token.IdZones;
36 import net.sf.firemox.ui.i18n.LanguageManagerMDB;
37
38 /***
39 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
40 * @since 0.82
41 */
42 class AssignDamageSourceDest extends UserAction implements LoopAction {
43
44 /***
45 * Create an instance of AssignDamageTarget by reading a file Offset's file
46 * must pointing on the first byte of this action <br>
47 * <ul>
48 * Structure of InputStream : Data[size]
49 * <li>super [Action]</li>
50 * <li>amount [Expression]</li>
51 * <li>type [Expression]</li>
52 * </ul>
53 *
54 * @param inputFile
55 * file containing this action
56 * @throws IOException
57 * If some other I/O error occurs
58 * @see net.sf.firemox.token.IdDamageTypes
59 */
60 AssignDamageSourceDest(InputStream inputFile) throws IOException {
61 super(inputFile);
62 valueExpr = ExpressionFactory.readNextExpression(inputFile);
63 type = ExpressionFactory.readNextExpression(inputFile);
64 }
65
66 @Override
67 public final Actiontype getIdAction() {
68 return Actiontype.ASSIGN_DAMAGE_SOURCE_DEST;
69 }
70
71 public int getStartIndex() {
72
73
74 return 0;
75 }
76
77 public boolean continueLoop(ContextEventListener context, int loopingIndex,
78 Ability ability) {
79 final int type = this.type.getValue(ability, null, context);
80 final int value = valueExpr.getValue(ability, ability.getCard(), context);
81 final MCard source = (MCard) StackManager.getInstance().getTargetedList()
82 .get(0);
83 final Target target = StackManager.getInstance().getTargetedList().get(1);
84 if (!target.isCard() || checkTimeStamp(context, (MCard) target)
85 && ((MCard) target).getIdZone() == IdZones.PLAY) {
86 if (!AssignedDamage.tryAction(source, target, value, type)) {
87
88 return false;
89 }
90 if (value > 0) {
91 AssignedDamage.dispatchEvent(source, target, value, type);
92 }
93 }
94 return true;
95 }
96
97 @Override
98 public String toString(Ability ability) {
99 try {
100 if (valueExpr.getValue(ability, null, null) == IdConst.ALL) {
101 return LanguageManagerMDB.getString("assign-damage-target-all");
102 }
103 return LanguageManagerMDB.getString("assign-damage-target", valueExpr
104 .getValue(ability, null, null));
105 } catch (Exception e) {
106 return LanguageManagerMDB.getString("assign-damage-target", "?");
107 }
108 }
109
110 /***
111 * The complex expression to use for the right value. Is null if the IdToken
112 * number is not a complex expression.
113 */
114 private final Expression valueExpr;
115
116 /***
117 * represent the type of damage
118 */
119 private final Expression type;
120
121 }