View Javadoc

1   /*
2    *   Magic-Project is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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  		// no optimization for this add
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 					// the last ability is the one to add
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 					// this ability would not be added
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 					// this ability would replace the old one
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 					 * Same ability as already generating one instance of this triggered
141 					 * ability
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 					// Same ability as already generating one instance of this triggered
153 					// ability
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 					// the last ability is the one to add
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 }