1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.firemox.test;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.List;
24
25 import net.sf.firemox.clickable.ability.Ability;
26 import net.sf.firemox.clickable.target.Target;
27 import net.sf.firemox.clickable.target.card.MCard;
28 import net.sf.firemox.event.MEventListener;
29 import net.sf.firemox.event.MovedCard;
30 import net.sf.firemox.expression.Counter;
31 import net.sf.firemox.stack.StackManager;
32 import net.sf.firemox.token.IdZones;
33 import net.sf.firemox.zone.ZoneManager;
34
35 /***
36 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
37 * @since 0.86
38 */
39 public class Has extends Test {
40
41 /***
42 * Create an instance of Has by reading a file. Offset's file must pointing on
43 * the first byte of this test <br>
44 * <ul>
45 * Structure of InputStream : Data[size]
46 * <li>test used to fill counter [...]
47 * <li>restriction zone [1]
48 * </ul>
49 *
50 * @param inputFile
51 * is the file containing this event
52 * @throws IOException
53 * if error occurred during the reading process from the specified
54 * input stream
55 */
56 Has(InputStream inputFile) throws IOException {
57 super(inputFile);
58 test = TestFactory.readNextTest(inputFile);
59 restrictionZone = inputFile.read() - 1;
60 }
61
62 @Override
63 public void extractTriggeredEvents(List<MEventListener> res, MCard source,
64 Test globalTest) {
65 if (test != null) {
66 test.extractTriggeredEvents(res, source, globalTest);
67 if (restrictionZone != -1) {
68 res.add(new MovedCard(IdZones.PLAY, new InZone(restrictionZone,
69 TestOn.TESTED), globalTest, source));
70 res.add(new MovedCard(IdZones.PLAY, True.getInstance(), And.append(
71 new InZone(restrictionZone, TestOn.TESTED), globalTest), source));
72 }
73 }
74 }
75
76 @Override
77 public boolean test(Ability ability, Target tested) {
78 return test(ability, tested, true);
79 }
80
81 @Override
82 public boolean testPreemption(Ability ability, Target tested) {
83 return test(ability, tested, false);
84 }
85
86 private boolean test(Ability ability, Target tested, boolean canBePreempted) {
87
88 Target previousTested = Counter.superTested;
89 Counter.superTested = tested;
90 try {
91 if (restrictionZone != -1) {
92 return StackManager.PLAYERS[0].zoneManager
93 .getContainer(restrictionZone).countAllCardsOf(test, ability, 1,
94 canBePreempted) > 0
95 || StackManager.PLAYERS[1].zoneManager
96 .getContainer(restrictionZone).countAllCardsOf(test, ability,
97 1, canBePreempted) > 0;
98 }
99 return StackManager.PLAYERS[0].zoneManager.countAllCardsOf(test, ability,
100 1, canBePreempted) > 0
101 || StackManager.PLAYERS[1].zoneManager.countAllCardsOf(test, ability,
102 1, canBePreempted) > 0;
103 } finally {
104
105 Counter.superTested = previousTested;
106 }
107 }
108
109 @Override
110 public String toString() {
111 if (restrictionZone != -1) {
112 return "HAS (" + test.toString() + ") IN "
113 + ZoneManager.getZoneName(restrictionZone);
114 }
115 return "HAS (" + test.toString() + ")";
116 }
117
118 /***
119 * The test used to count cards
120 */
121 private Test test;
122
123 /***
124 * The zone identifier where the scan is restricted. If is equal to -1, there
125 * would be no restriction zone.
126 *
127 * @see net.sf.firemox.token.IdZones
128 */
129 private int restrictionZone;
130 }