1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.magicproject.expression;
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.magicproject.clickable.ability.Ability;
29 import net.sf.magicproject.clickable.targetable.Targetable;
30 import net.sf.magicproject.clickable.targetable.card.MCard;
31 import net.sf.magicproject.event.MEventListener;
32 import net.sf.magicproject.event.context.ContextEventListener;
33 import net.sf.magicproject.stack.StackManager;
34 import net.sf.magicproject.test.Test;
35 import net.sf.magicproject.test.TestFactory;
36
37 /***
38 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
39 * @since 0.83
40 */
41 public class HighestAmong extends Expression {
42
43 /***
44 * Creates a new instance of HighestAmong <br>
45 *
46 * @param inputFile
47 * file containing this action
48 * @throws IOException
49 */
50 public HighestAmong(InputStream inputFile) throws IOException {
51 super();
52 restrictionZone = inputFile.read() - 1;
53 test = TestFactory.readNextTest(inputFile);
54 expr = ExpressionFactory.readNextExpression(inputFile);
55 }
56
57 @Override
58 public int getValue(Ability ability, Targetable tested,
59 ContextEventListener context) {
60 final List<Targetable> validTargets = new ArrayList<Targetable>();
61 if (restrictionZone != -1) {
62 StackManager.PLAYERS[StackManager.idCurrentPlayer].zoneManager
63 .getContainer(restrictionZone).checkAllCardsOf(test, validTargets,
64 ability);
65 StackManager.PLAYERS[1 - StackManager.idCurrentPlayer].zoneManager
66 .getContainer(restrictionZone).checkAllCardsOf(test, validTargets,
67 ability);
68 } else {
69 StackManager.PLAYERS[StackManager.idCurrentPlayer].zoneManager
70 .checkAllCardsOf(test, validTargets, ability);
71 StackManager.PLAYERS[1 - StackManager.idCurrentPlayer].zoneManager
72 .checkAllCardsOf(test, validTargets, ability);
73 }
74 int highest = 0;
75 for (Targetable targetable : validTargets) {
76 final int value = expr.getValue(ability, targetable, context);
77 if (value > highest) {
78 highest = value;
79 }
80 }
81 return highest;
82 }
83
84 @Override
85 public void extractTriggeredEvents(List<MEventListener> res,
86 MCard source, Test globalTest) {
87 expr.extractTriggeredEvents(res, source, globalTest);
88 }
89
90 /***
91 * The zone identifant where the scan is restricted. If is equal to -1, there
92 * would be no restriction zone.
93 *
94 * @see net.sf.magicproject.token.IdZones
95 */
96 private int restrictionZone;
97
98 /***
99 * The test used to determine which card are considered in evaluation
100 */
101 private Test test;
102
103 /***
104 * The expression to evaluate for each valid card
105 */
106 private Expression expr;
107
108 }