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