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