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.test;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.util.List;
24  
25  import net.sf.magicproject.clickable.ability.Ability;
26  import net.sf.magicproject.clickable.targetable.Targetable;
27  import net.sf.magicproject.clickable.targetable.card.MCard;
28  import net.sf.magicproject.event.MEventListener;
29  import net.sf.magicproject.event.MovedCard;
30  import net.sf.magicproject.expression.Counter;
31  import net.sf.magicproject.stack.StackManager;
32  import net.sf.magicproject.token.IdZones;
33  
34  /***
35   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
36   * @since 0.86
37   */
38  public class Has extends Test {
39  
40  	/***
41  	 * Create an instance of Has by reading a file. Offset's file must pointing on
42  	 * the first byte of this test <br>
43  	 * <ul>
44  	 * Structure of InputStream : Data[size]
45  	 * <li>test used to fill counter [...]
46  	 * <li>restriction zone [1]
47  	 * </ul>
48  	 * 
49  	 * @param inputFile
50  	 *          is the file containing this event
51  	 * @throws IOException
52  	 *           if error occurred during the reading process from the specified
53  	 *           input stream
54  	 */
55  	Has(InputStream inputFile) throws IOException {
56  		super(inputFile);
57  		test = TestFactory.readNextTest(inputFile);
58  		restrictionZone = inputFile.read() - 1;
59  	}
60  
61  	@Override
62  	public void extractTriggeredEvents(List<MEventListener> res, MCard source,
63  			Test globalTest) {
64  		if (test != null) {
65  			test.extractTriggeredEvents(res, source, globalTest);
66  			if (restrictionZone != -1) {
67  				res.add(new MovedCard(IdZones.PLAY, new InZone(restrictionZone,
68  						TestOn.TESTED), globalTest, source));
69  				res.add(new MovedCard(IdZones.PLAY, True.getInstance(), And.append(
70  						new InZone(restrictionZone, TestOn.TESTED), globalTest), source));
71  			}
72  		}
73  	}
74  
75  	@Override
76  	public boolean test(Ability ability, Targetable tested) {
77  		return test(ability, tested, true);
78  	}
79  
80  	@Override
81  	public boolean testPreemption(Ability ability, Targetable tested) {
82  		return test(ability, tested, false);
83  	}
84  
85  	private boolean test(Ability ability, Targetable tested,
86  			boolean canBePreempted) {
87  		// we count cards, we save the upper counter test.
88  		Targetable previousTested = Counter.superTested;
89  		Counter.superTested = tested;
90  		try {
91  			if (restrictionZone != -1) {
92  				return StackManager.PLAYERS[0].zoneManager
93  						.getContainer(restrictionZone).countAllCardsOf(test, ability, 1,
94  								canBePreempted) > 0
95  						|| StackManager.PLAYERS[1].zoneManager
96  								.getContainer(restrictionZone).countAllCardsOf(test, ability,
97  										1, canBePreempted) > 0;
98  			}
99  			return StackManager.PLAYERS[0].zoneManager.countAllCardsOf(test, ability,
100 					1, canBePreempted) > 0
101 					|| StackManager.PLAYERS[1].zoneManager.countAllCardsOf(test, ability,
102 							1, canBePreempted) > 0;
103 		} finally {
104 			// restore the previous upper counter test.
105 			Counter.superTested = previousTested;
106 		}
107 	}
108 
109 	/***
110 	 * The test used to count cards
111 	 */
112 	private Test test;
113 
114 	/***
115 	 * The zone identifant where the scan is restricted. If is equal to -1, there
116 	 * would be no restriction zone.
117 	 * 
118 	 * @see net.sf.magicproject.token.IdZones
119 	 */
120 	private int restrictionZone;
121 }