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