1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.magicproject.action;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25
26 import net.sf.magicproject.clickable.ability.Ability;
27 import net.sf.magicproject.clickable.targetable.Targetable;
28 import net.sf.magicproject.clickable.targetable.card.MCard;
29 import net.sf.magicproject.event.AssignedDamage;
30 import net.sf.magicproject.event.context.ContextEventListener;
31 import net.sf.magicproject.expression.Expression;
32 import net.sf.magicproject.expression.ExpressionFactory;
33 import net.sf.magicproject.stack.StackManager;
34 import net.sf.magicproject.token.IdConst;
35 import net.sf.magicproject.token.IdZones;
36 import net.sf.magicproject.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.magicproject.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 Targetable target = StackManager.getInstance().getTargetedList().get(
84 1);
85 if (!target.isCard() || checkTimeStamp(context, (MCard) target)
86 && ((MCard) target).getIdZone() == IdZones.PLAY) {
87 if (!AssignedDamage.tryAction(source, target, value, type)) {
88
89 return false;
90 }
91 if (value > 0) {
92 AssignedDamage.dispatchEvent(source, target, value, type);
93 }
94 }
95 return true;
96 }
97
98 @Override
99 public String toString(Ability ability) {
100 try {
101 if (valueExpr.getValue(ability, null, null) == IdConst.ALL) {
102 return LanguageManagerMDB.getString("destroy-target");
103 }
104 return "" + valueExpr.getValue(ability, null, null) + " "
105 + LanguageManagerMDB.getString("damage-target");
106 } catch (Exception e) {
107 return "?" + " " + LanguageManagerMDB.getString("damage-target");
108 }
109 }
110
111 /***
112 * The complex expression to use for the right value. Is null if the IdToken
113 * number is not a complex expression.
114 */
115 private final Expression valueExpr;
116
117 /***
118 * represent the type of damage
119 */
120 private final Expression type;
121
122 }