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 import java.util.ArrayList;
25 import java.util.List;
26
27 import net.sf.magicproject.action.handler.StandardAction;
28 import net.sf.magicproject.clickable.ability.Ability;
29 import net.sf.magicproject.clickable.targetable.Targetable;
30 import net.sf.magicproject.event.context.ContextEventListener;
31 import net.sf.magicproject.stack.EventManager;
32 import net.sf.magicproject.stack.StackManager;
33 import net.sf.magicproject.test.Test;
34 import net.sf.magicproject.test.TestFactory;
35 import net.sf.magicproject.token.IdTokens;
36 import net.sf.magicproject.token.Register;
37 import net.sf.magicproject.tools.Log;
38
39 /***
40 * Add all valid targets in the list following the specified type. No event is
41 * generated. The friendly test, and the type are required to list the valids
42 * targets. The target list is used by the most actions. <br>
43 * For example, if the next action 'damage', all cards/player from the target
44 * list would be damaged. When a new ability is added to the stack, a new list
45 * is created and attached to it. Actions may modify, acceess it until the
46 * ability is completely resolved.
47 *
48 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
49 * @since 0.86
50 */
51 class TargetAllNoEvent extends UserAction implements StandardAction {
52
53 /***
54 * Create an instance by reading a file Offset's file must pointing on the
55 * first byte of this action <br>
56 * <ul>
57 * Structure of InputStream : Data[size]
58 * <li>type [Register]</li>
59 * <li>test [Test]</li>
60 * <li>restriction Zone id [int]</li>
61 * <li>can be preempted [boolean]</li>
62 * </ul>
63 * For the available options, see interface IdTargets
64 *
65 * @param inputFile
66 * file containing this action
67 * @throws IOException
68 * if error occurred during the reading process from the specified
69 * input stream
70 * @see net.sf.magicproject.token.IdTargets
71 */
72 TargetAllNoEvent(InputStream inputFile) throws IOException {
73 super(inputFile);
74 type = Register.deserialize(inputFile).ordinal();
75 test = TestFactory.readNextTest(inputFile);
76 restrictionZone = inputFile.read() - 1;
77
78 inputFile.read();
79 }
80
81 public final boolean play(ContextEventListener context, Ability ability) {
82 final List<Targetable> validTargets = new ArrayList<Targetable>();
83 String targetTxt = null;
84 switch (type) {
85 case IdTokens.DEALTABLE:
86
87 targetTxt = "[all creatures and all players]";
88 if (test
89 .test(ability, StackManager.PLAYERS[StackManager.idCurrentPlayer])) {
90 validTargets.add(StackManager.PLAYERS[StackManager.idCurrentPlayer]);
91 }
92 if (test.test(ability,
93 StackManager.PLAYERS[1 - StackManager.idCurrentPlayer])) {
94 validTargets
95 .add(StackManager.PLAYERS[1 - StackManager.idCurrentPlayer]);
96 }
97 if (restrictionZone != -1) {
98 StackManager.PLAYERS[StackManager.idCurrentPlayer].zoneManager
99 .getContainer(restrictionZone).checkAllCardsOf(test, validTargets,
100 ability);
101 StackManager.PLAYERS[1 - StackManager.idCurrentPlayer].zoneManager
102 .getContainer(restrictionZone).checkAllCardsOf(test, validTargets,
103 ability);
104 } else {
105 StackManager.PLAYERS[StackManager.idCurrentPlayer].zoneManager
106 .checkAllCardsOf(test, validTargets, ability);
107 StackManager.PLAYERS[1 - StackManager.idCurrentPlayer].zoneManager
108 .checkAllCardsOf(test, validTargets, ability);
109 }
110 break;
111 case IdTokens.PLAYER:
112
113 targetTxt = "[all players]";
114 if (test
115 .test(ability, StackManager.PLAYERS[StackManager.idCurrentPlayer])) {
116 validTargets.add(StackManager.PLAYERS[StackManager.idCurrentPlayer]);
117 }
118 if (test.test(ability,
119 StackManager.PLAYERS[1 - StackManager.idCurrentPlayer])) {
120 validTargets
121 .add(StackManager.PLAYERS[1 - StackManager.idCurrentPlayer]);
122 }
123 break;
124 case IdTokens.CARD:
125
126 targetTxt = "[all cards]";
127 if (restrictionZone != -1) {
128 StackManager.PLAYERS[StackManager.idCurrentPlayer].zoneManager
129 .getContainer(restrictionZone).checkAllCardsOf(test, validTargets,
130 ability);
131 StackManager.PLAYERS[1 - StackManager.idCurrentPlayer].zoneManager
132 .getContainer(restrictionZone).checkAllCardsOf(test, validTargets,
133 ability);
134 } else {
135 StackManager.PLAYERS[StackManager.idCurrentPlayer].zoneManager
136 .checkAllCardsOf(test, validTargets, ability);
137 StackManager.PLAYERS[1 - StackManager.idCurrentPlayer].zoneManager
138 .checkAllCardsOf(test, validTargets, ability);
139 }
140 break;
141 default:
142 throw new InternalError("Unknown type :" + type);
143 }
144 if (StackManager.getInstance().getTargetedList().list != null) {
145 validTargets.removeAll(StackManager.getInstance().getTargetedList().list);
146 }
147 Log.debug("all mode, size=" + validTargets.size() + ", ignored="
148 + StackManager.getInstance().getTargetedList().list);
149 EventManager.moreInfoLbl.setText(targetTxt + " - all mode)");
150 if (!validTargets.isEmpty()) {
151 StackManager.getInstance().getTargetedList().list.addAll(validTargets);
152 }
153
154 return true;
155 }
156
157 @Override
158 public final Actiontype getIdAction() {
159 return Actiontype.TARGET_ALL;
160 }
161
162 @Override
163 public String toString(Ability ability) {
164 switch (type) {
165 case IdTokens.CARD:
166 return "[all cards]";
167 case IdTokens.PLAYER:
168 return "[all players]";
169 case IdTokens.DEALTABLE:
170 return "[all cards and players]";
171 default:
172 throw new InternalError("Unknown type : " + type);
173 }
174 }
175
176 /***
177 * represents the test applied to target before to be added to the list
178 */
179 protected Test test;
180
181 /***
182 * represents the type of target (player, card, dealtable)
183 *
184 * @see net.sf.magicproject.token.IdTargets
185 */
186 protected int type;
187
188 /***
189 * The zone identifant where the scan is restricted. If is equal to -1, there
190 * would be no restriction zone.
191 *
192 * @see net.sf.magicproject.token.IdZones
193 */
194 protected int restrictionZone;
195 }