View Javadoc

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