View Javadoc

1   /*
2    * MChoiceList.java 
3    * Created on 27 octobre 2002, 14:00
4    * 
5    *   Magic-Project is a turn based strategy simulator
6    *   Copyright (C) 2003-2007 Fabrice Daugan
7    *
8    *   This program is free software; you can redistribute it and/or modify it 
9    * under the terms of the GNU General Public License as published by the Free 
10   * Software Foundation; either version 2 of the License, or (at your option) any
11   * later version.
12   *
13   *   This program is distributed in the hope that it will be useful, but WITHOUT 
14   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
16   * details.
17   *
18   *   You should have received a copy of the GNU General Public License along  
19   * with this program; if not, write to the Free Software Foundation, Inc., 
20   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21   */
22  package net.sf.magicproject.stack;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import net.sf.magicproject.clickable.ability.Ability;
28  import net.sf.magicproject.clickable.targetable.Targetable;
29  import net.sf.magicproject.clickable.targetable.card.CardFactory;
30  import net.sf.magicproject.token.IdZones;
31  import net.sf.magicproject.tools.Log;
32  import net.sf.magicproject.zone.MZone;
33  
34  /***
35   * This class is representing all abilities activated for an event. This
36   * abilities are grouped by cards.
37   * 
38   * @author Fabrice Daugan
39   */
40  public class ActivatedChoiceList {
41  
42  	/***
43  	 * Creates a new instance of MChoiceList with the specified playable
44  	 * spells/abilities.
45  	 * 
46  	 * @param abilityList
47  	 *          is the specified playable spells/abilities
48  	 * @param advancedList
49  	 *          is the specified playable advanced spells/abilities
50  	 */
51  	public ActivatedChoiceList(List<Ability> abilityList,
52  			List<Ability> advancedList) {
53  		if (abilityList == null || abilityList.isEmpty()) {
54  			Log.debug("no playable abilities");
55  		} else {
56  			Log.debug("playable abilities:" + abilityList.size());
57  			choiceList = new ArrayList<ActivatedChoice>(abilityList.size());
58  			for (Ability ability : abilityList) {
59  				boolean found = false;
60  				for (ActivatedChoice choice : choiceList) {
61  					if (choice.getCard() == ability.getCard()) {
62  						// the card has been found, we add this ability to this choice
63  						choice.addAbility(ability);
64  						found = true;
65  						break;
66  					}
67  				}
68  				if (!found) {
69  					// no card was found, so it's a new playable card
70  					choiceList.add(new ActivatedChoice(ability));
71  				}
72  			}
73  			if (advancedList != null && !advancedList.isEmpty()) {
74  				advancedChoiceList = new ArrayList<ActivatedChoice>(advancedList.size());
75  				for (Ability ability : advancedList) {
76  					boolean found = false;
77  					for (ActivatedChoice choice : advancedChoiceList) {
78  						if (choice.getCard() == ability.getCard()) {
79  							// the card has been found, we add this ability to this choice
80  							choice.addAbility(ability);
81  							found = true;
82  							break;
83  						}
84  					}
85  					if (!found) {
86  						// no card was found, so it's a new playable card
87  						advancedChoiceList.add(new ActivatedChoice(ability));
88  					}
89  				}
90  			}
91  		}
92  	}
93  
94  	/***
95  	 * remove all choices from the list
96  	 */
97  	public void clear() {
98  		disHighLight();
99  		for (ActivatedChoice choice : choiceList) {
100 			choice.clear();
101 		}
102 		choiceList.clear();
103 		if (advancedChoiceList != null) {
104 			for (ActivatedChoice choice : advancedChoiceList) {
105 				choice.clear();
106 			}
107 			advancedChoiceList.clear();
108 		}
109 	}
110 
111 	/***
112 	 * return the list of playable abilities of this card
113 	 * 
114 	 * @param targetable
115 	 *          the card shearching it's playable abilities
116 	 * @return list of playable abilities of this card, or null if this
117 	 *         card/player has no playable ability.
118 	 */
119 	public List<Ability> abilitiesOf(Targetable targetable) {
120 		for (ActivatedChoice choice : choiceList) {
121 			if (choice.getCard() == targetable) {
122 				return choice.abilities;
123 			}
124 		}
125 		return null;
126 	}
127 
128 	/***
129 	 * return the list of playable advanced abilities of this card
130 	 * 
131 	 * @param targetable
132 	 *          the card shearching it's playable abilities
133 	 * @return list of playable advanced abilities of this card, or null if this
134 	 *         card/player has no playable ability.
135 	 */
136 	public List<Ability> advancedAbilitiesOf(Targetable targetable) {
137 		if (advancedChoiceList != null) {
138 			for (ActivatedChoice choice : advancedChoiceList) {
139 				if (choice.getCard() == targetable) {
140 					return choice.abilities;
141 				}
142 			}
143 		}
144 		return null;
145 	}
146 
147 	/***
148 	 * Hightlight all cards containing any ability of this list. The cards with
149 	 * only advanced abilities are not highlighted.
150 	 * 
151 	 * @return the list of highlighted zones
152 	 */
153 	public List<MZone> highLight() {
154 		final List<MZone> res = new ArrayList<MZone>();
155 		final boolean[] highlightedZones = new boolean[IdZones.NB_ZONE * 2];
156 		for (int a = choiceList.size(); a-- > 0;) {
157 			choiceList.get(a).highLight(highlightedZones);
158 		}
159 		for (int i = highlightedZones.length; i-- > 0;) {
160 			if (highlightedZones[i]) {
161 				res.add(StackManager.PLAYERS[i / IdZones.NB_ZONE].zoneManager
162 						.getContainer(i % IdZones.NB_ZONE));
163 				res.get(res.size() - 1).highLight(CardFactory.ACTIVATED_COLOR);
164 			}
165 		}
166 		return res;
167 	}
168 
169 	/***
170 	 * Dishightlight all cards containing any ability of this list.
171 	 */
172 	public void disHighLight() {
173 		for (ActivatedChoice choice : choiceList) {
174 			choice.disHighLight();
175 		}
176 		if (advancedChoiceList != null) {
177 			for (ActivatedChoice choice : advancedChoiceList) {
178 				choice.disHighLight();
179 			}
180 		}
181 	}
182 
183 	/***
184 	 * List of MChoice object
185 	 */
186 	private List<ActivatedChoice> choiceList;
187 
188 	/***
189 	 * List of MChoice object (advanced)
190 	 */
191 	private List<ActivatedChoice> advancedChoiceList;
192 }