View Javadoc

1   /*
2    * Created on 12 mars 2005
3    * 
4    *   Magic-Project is a turn based strategy simulator
5    *   Copyright (C) 2003-2007 Fabrice Daugan
6    *
7    *   This program is free software; you can redistribute it and/or modify it 
8    * under the terms of the GNU General Public License as published by the Free 
9    * Software Foundation; either version 2 of the License, or (at your option) any
10   * later version.
11   *
12   *   This program is distributed in the hope that it will be useful, but WITHOUT 
13   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
15   * details.
16   *
17   *   You should have received a copy of the GNU General Public License along  
18   * with this program; if not, write to the Free Software Foundation, Inc., 
19   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   */
21  package net.sf.magicproject.test;
22  
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.util.List;
26  import java.util.Map;
27  
28  import net.sf.magicproject.clickable.ability.Ability;
29  import net.sf.magicproject.clickable.ability.Priority;
30  import net.sf.magicproject.clickable.ability.ReplacementAbility;
31  import net.sf.magicproject.clickable.targetable.card.MCard;
32  import net.sf.magicproject.event.MEventListener;
33  import net.sf.magicproject.tools.MToolKit;
34  
35  /***
36   * Is the tested card owns an ability
37   * 
38   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
39   * @since 0.82
40   */
41  class HasAbility extends TestCard {
42  
43  	/***
44  	 * Create an instance of HasAbility by reading a file. Offset's file must
45  	 * pointing on the first byte of this test <br>
46  	 * <ul>
47  	 * Structure of InputStream : Data[size]
48  	 * <li>ability name [...]</li>
49  	 * </ul>
50  	 * 
51  	 * @param inputFile
52  	 *          is the file containing this event
53  	 * @throws IOException
54  	 *           if error occurred during the reading process from the specified
55  	 *           input stream
56  	 */
57  	HasAbility(InputStream inputFile) throws IOException {
58  		super(inputFile);
59  		abilityName = MToolKit.readString(inputFile).intern();
60  	}
61  
62  	@Override
63  	protected boolean testCard(Ability ability, MCard tested) {
64  		MCard card = on.getCard(ability, tested);
65  		for (List<Ability> abilities : MEventListener.CAN_I_CAST_ABILITIES) {
66  			for (Ability tAbility : abilities) {
67  				if (card == tAbility.getCard()
68  						&& abilityName.equals(tAbility.getName())) {
69  					return true;
70  				}
71  			}
72  		}
73  		for (List<Ability> abilities : MEventListener.TRIGGRED_ABILITIES.values()) {
74  			for (Ability tAbility : abilities) {
75  				if (card == tAbility.getCard()
76  						&& abilityName.equals(tAbility.getName())) {
77  					return true;
78  				}
79  			}
80  		}
81  		for (Map<Priority, List<ReplacementAbility>> triggeredList : MEventListener.REPLACEMENT_ABILITIES
82  				.values()) {
83  			for (List<ReplacementAbility> abilities : triggeredList.values()) {
84  				for (Ability tAbility : abilities) {
85  					if (card == tAbility.getCard()
86  							&& abilityName.equals(tAbility.getName())) {
87  						return true;
88  					}
89  				}
90  			}
91  		}
92  		return false;
93  	}
94  
95  	/***
96  	 * The ability name to match during each test
97  	 */
98  	private final String abilityName;
99  
100 }