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   */
20  package net.sf.magicproject.test;
21  
22  import java.io.IOException;
23  import java.io.InputStream;
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.SystemCard;
28  import net.sf.magicproject.deckbuilder.Deck;
29  import net.sf.magicproject.expression.Counter;
30  import net.sf.magicproject.expression.Expression;
31  import net.sf.magicproject.expression.ExpressionFactory;
32  import net.sf.magicproject.token.IdConst;
33  import net.sf.magicproject.tools.Log;
34  import net.sf.magicproject.tools.MCardCompare;
35  
36  /***
37   * Count cards in a player's deck until a threshold.
38   * 
39   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
40   * @since 0.94
41   */
42  public class DeckCounter extends Test {
43  
44  	/***
45  	 * The test to use for counters
46  	 */
47  	private final Test test;
48  
49  	/***
50  	 * The last test running for this class.
51  	 */
52  	public static DeckCounter lastRanInstance;
53  
54  	/***
55  	 * The last card tested with a counter.
56  	 */
57  	public static MCardCompare lastRanCard;
58  
59  	/***
60  	 * The threshold of this counter.
61  	 */
62  	private final Expression threshold;
63  
64  	/***
65  	 * Creates a new instance of DeckCounter <br>
66  	 * <ul>
67  	 * Structure of InputStream : Data[size]
68  	 * <li>test used to fill counter [Test]
69  	 * <li>threshold [Expression]
70  	 * </ul>
71  	 * 
72  	 * @param inputFile
73  	 *          file containing this action
74  	 * @throws IOException
75  	 *           if error occurred during the reading process from the specified
76  	 *           input stream
77  	 */
78  	public DeckCounter(InputStream inputFile) throws IOException {
79  		super(inputFile);
80  		test = TestFactory.readNextTest(inputFile);
81  		threshold = ExpressionFactory.readNextExpression(inputFile);
82  	}
83  
84  	@Override
85  	public boolean test(Ability ability, Targetable tested) {
86  		// we count cards, we save the upper counter test.
87  		final Targetable previousTested = Counter.superTested;
88  		final int threshold = this.threshold.getValue(ability, tested, null);
89  		Counter.superTested = tested;
90  		lastRanCard = null;
91  		int res = 0;
92  		for (MCardCompare card : Deck.currentDeck.getCards()) {
93  			try {
94  				if (test.test(ability, card.getCard(Deck.currentDeck.getMdbStream()))) {
95  					res += card.getAmount();
96  					if (res > threshold) {
97  						if (lastRanCard == null) {
98  							lastRanCard = card;
99  							lastRanInstance = this;
100 						}
101 						return false;
102 					}
103 				} else if (threshold == IdConst.ALL) {
104 					// We are in 'all' mode, no faillure allowed here
105 					if (lastRanCard == null) {
106 						lastRanCard = card;
107 						lastRanInstance = this;
108 					}
109 					return false;
110 				}
111 			} catch (IOException e) {
112 				Log.error(e);
113 			}
114 		}
115 		lastRanInstance = this;
116 
117 		// restore the previous upper counter test.
118 		Counter.superTested = previousTested;
119 		return true;
120 	}
121 
122 	/***
123 	 * Return the threshold of this counter.
124 	 * 
125 	 * @return the threshold of this counter.
126 	 */
127 	public Expression getThreshold() {
128 		return this.threshold;
129 	}
130 
131 	@Override
132 	public String toString() {
133 		return "OCCURENCE IN DECK <= "
134 				+ threshold.getPreemptionValue(null, SystemCard.instance, null);
135 	}
136 
137 }