1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.magicproject.clickable.ability;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.util.List;
25
26 import net.sf.magicproject.clickable.targetable.card.TriggeredCard;
27 import net.sf.magicproject.clickable.targetable.card.TriggeredCardChoice;
28 import net.sf.magicproject.event.context.ContextEventListener;
29 import net.sf.magicproject.stack.StackManager;
30
31 /***
32 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
33 * @since 0.91
34 */
35 public enum Optimization {
36
37 /***
38 * no optimization is done.
39 */
40 none,
41
42 /***
43 * An ability is not added in the TBZ if the last ability is the same. Only
44 * ability is compared, the context is ignored.
45 */
46 follow,
47
48 /***
49 * The ability would not be added if it already exists in the TBZ.
50 */
51 first,
52
53 /***
54 * The ability would replace any existing ability in the TBZ.
55 */
56 last,
57
58 /***
59 * Same ability is added only once per action. This important for
60 * LoopingAction.
61 */
62 action,
63
64 /***
65 * Ability is added to the TBZ but during the resolution a prompt asks to
66 * controller only one ability to be added to the stack.
67 */
68 choice,
69
70 /***
71 * Same ability is added only once per ability triggering this event.
72 */
73 event,
74
75 /***
76 * An ability is not added in the TBZ if the last ability is the same. Ability
77 * and context are compared.
78 */
79 context;
80
81 /***
82 * Add the specified ability to the TBZ.
83 *
84 * @param ability
85 * the ability to add
86 * @param context
87 * the attached context
88 * @param where
89 * the list where the constructed triggered card would be added
90 * @return true if the specified ability has been added
91 */
92 public boolean addTo(Ability ability, ContextEventListener context,
93 List<TriggeredCard> where) {
94
95 switch (this) {
96 case none:
97 default:
98 return where.add(ability.getTriggeredClone(context));
99 case follow:
100 if (!where.isEmpty()) {
101 TriggeredCard triggeredCard = where.get(where.size() - 1);
102 if (ability.equals(context, triggeredCard.triggeredAbility,
103 triggeredCard.getAbilityContext())) {
104
105 return false;
106 }
107 }
108 return where.add(ability.getTriggeredClone(context));
109 case first:
110 for (int i = where.size(); i-- > 0;) {
111 TriggeredCard triggeredCard = where.get(i);
112 if (ability.equals(context, triggeredCard.triggeredAbility,
113 triggeredCard.getAbilityContext())) {
114
115 return false;
116 }
117 }
118 return where.add(ability.getTriggeredClone(context));
119 case last:
120 for (int i = where.size(); i-- > 0;) {
121 TriggeredCard triggeredCard = where.get(i);
122 if (ability.equals(context, triggeredCard.triggeredAbility,
123 triggeredCard.getAbilityContext())) {
124
125 where.remove(i);
126 where.add(ability.getTriggeredClone(context));
127 return false;
128 }
129 }
130 return where.add(ability.getTriggeredClone(context));
131 case action:
132 return where.add(ability.getTriggeredClone(context));
133 case choice:
134 for (int i = where.size(); i-- > 0;) {
135 TriggeredCard triggeredCard = where.get(i);
136 if (triggeredCard.abilityID == StackManager.abilityID
137 && ability.equals(context, triggeredCard.triggeredAbility,
138 triggeredCard.getAbilityContext())) {
139
140
141
142
143 ((TriggeredCardChoice) where.get(i)).addChoice(ability, context);
144 return false;
145 }
146 }
147 return where.add(ability.getTriggeredCloneChoice(context));
148 case event:
149 for (int i = where.size(); i-- > 0;) {
150 if (where.get(i).abilityID == StackManager.abilityID
151 && ability.equals(where.get(i).triggeredAbility)) {
152
153
154 return false;
155 }
156 }
157 return where.add(ability.getTriggeredClone(context));
158 case context:
159 if (!where.isEmpty()) {
160 TriggeredCard triggeredCard = where.get(where.size() - 1);
161 if (ability.equals(context, triggeredCard.triggeredAbility,
162 triggeredCard.getAbilityContext())
163 && context.equals(triggeredCard.getAbilityContext())) {
164
165 return false;
166 }
167 }
168 return where.add(ability.getTriggeredClone(context));
169 }
170 }
171
172 /***
173 * Wrtite this enum to the given outputstream.
174 *
175 * @param out
176 * the stream ths enum would be written.
177 * @throws IOException
178 */
179 public void write(OutputStream out) throws IOException {
180 out.write(ordinal());
181 }
182
183 /***
184 * Read and return the enum from the given inputstream.
185 *
186 * @param input
187 * the stream containing the enum to read.
188 * @return the enum from the given inputstream.
189 * @throws IOException
190 */
191 static Optimization valueOf(InputStream input) throws IOException {
192 return values()[input.read()];
193 }
194 }