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.clickable.ability.Ability;
26 import net.sf.magicproject.clickable.targetable.Targetable;
27 import net.sf.magicproject.event.context.ContextEventListener;
28 import net.sf.magicproject.operation.Add;
29 import net.sf.magicproject.operation.Remove;
30 import net.sf.magicproject.stack.StackManager;
31 import net.sf.magicproject.token.IdCommonToken;
32 import net.sf.magicproject.token.IdTokens;
33
34 /***
35 * This action is used to modifiy a register of a player or a card. <br>
36 * This action can use the target list when is played : the address
37 * (idToken=register name + register index) must be IdTokens#TARGET. So the
38 * target list must set before this action would be played.
39 *
40 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
41 * @since 0.8
42 */
43 class ModifyTargetListRegister extends ModifyRegister implements LoopAction {
44
45 /***
46 * Create an instance of ModifyTargetListRegister by reading a file Offset's
47 * file must pointing on the first byte of this action <br>
48 * <ul>
49 * Structure of InputStream : Data[size]
50 * <li>[super]</li>
51 * </ul>
52 *
53 * @param inputFile
54 * file containing this action
55 * @throws IOException
56 * if error occurred during the reading process from the specified
57 * input stream
58 */
59 ModifyTargetListRegister(InputStream inputFile) throws IOException {
60 super(inputFile);
61 }
62
63 @Override
64 public final Actiontype getIdAction() {
65 return Actiontype.MODIFY_TARGET_LIST_REGISTER;
66 }
67
68 public boolean continueLoop(ContextEventListener context, int loopingIndex,
69 Ability ability) {
70 Targetable target = StackManager.getInstance().getTargetedList().get(
71 loopingIndex);
72 return ModifyTargetableRegister.modifyRegister(ability.getCard(), target,
73 index.getValue(ability, target, context), getValue(ability, target,
74 context), op);
75 }
76
77 public int getStartIndex() {
78 return StackManager.getInstance().getTargetedList().size() - 1;
79 }
80
81 @Override
82 public boolean play(ContextEventListener context, Ability ability) {
83 throw new InternalError("should not been called");
84 }
85
86 @Override
87 public String toString(Ability ability) {
88 final int index = this.index.getValue(ability, null, null);
89 String value = null;
90 try {
91 value = "" + valueExpr.getValue(ability, null, null);
92 } catch (Exception e) {
93 value = "?";
94 }
95 if (op == Add.getInstance()) {
96 if (index == IdTokens.LIFE) {
97 return "Gain " + value + " life";
98 }
99 if (index == IdTokens.POISON) {
100 return "Gain " + value + " poison";
101 }
102 if (index == IdCommonToken.DAMAGE) {
103 return "Add " + value + " damage";
104 }
105 }
106 if (op == Remove.getInstance()) {
107 if (index == IdTokens.LIFE) {
108 return "Lose " + value + " life";
109 }
110 if (index == IdTokens.POISON) {
111 return "Lose " + value + " poison";
112 }
113 if (index == IdCommonToken.DAMAGE) {
114 return "Remove " + value + " damage";
115 }
116 } else {
117 if (index == IdTokens.LIFE) {
118 return op.toString() + " " + value + " life";
119 }
120 if (index == IdTokens.POISON) {
121 return op.toString() + " " + value + " poison";
122 }
123 if (index == IdCommonToken.DAMAGE) {
124 return op.toString() + value + " damage";
125 }
126 }
127
128
129 return "";
130 }
131 }