View Javadoc

1   /*
2    * Created on 19 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.expression;
22  
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import net.sf.magicproject.clickable.ability.Ability;
29  import net.sf.magicproject.clickable.targetable.Targetable;
30  import net.sf.magicproject.clickable.targetable.card.MCard;
31  import net.sf.magicproject.event.MEventListener;
32  import net.sf.magicproject.event.context.ContextEventListener;
33  import net.sf.magicproject.stack.StackManager;
34  import net.sf.magicproject.test.Test;
35  import net.sf.magicproject.test.TestFactory;
36  
37  /***
38   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
39   * @since 0.83
40   */
41  public class LowestAmong extends Expression {
42  
43  	/***
44  	 * Creates a new instance of LowestAmong <br>
45  	 * 
46  	 * @param inputFile
47  	 *          file containing this action
48  	 * @throws IOException
49  	 */
50  	public LowestAmong(InputStream inputFile) throws IOException {
51  		super();
52  		restrictionZone = inputFile.read() - 1;
53  		test = TestFactory.readNextTest(inputFile);
54  		expr = ExpressionFactory.readNextExpression(inputFile);
55  	}
56  
57  	@Override
58  	public int getValue(Ability ability, Targetable tested,
59  			ContextEventListener context) {
60  		final List<Targetable> validTargets = new ArrayList<Targetable>();
61  		if (restrictionZone != -1) {
62  			StackManager.PLAYERS[StackManager.idCurrentPlayer].zoneManager
63  					.getContainer(restrictionZone).checkAllCardsOf(test, validTargets,
64  							ability);
65  			StackManager.PLAYERS[1 - StackManager.idCurrentPlayer].zoneManager
66  					.getContainer(restrictionZone).checkAllCardsOf(test, validTargets,
67  							ability);
68  		} else {
69  			StackManager.PLAYERS[StackManager.idCurrentPlayer].zoneManager
70  					.checkAllCardsOf(test, validTargets, ability);
71  			StackManager.PLAYERS[1 - StackManager.idCurrentPlayer].zoneManager
72  					.checkAllCardsOf(test, validTargets, ability);
73  		}
74  		int lowest = 0;
75  		for (Targetable targetable : validTargets) {
76  			final int value = expr.getValue(ability, targetable, context);
77  			if (value < lowest) {
78  				lowest = value;
79  			}
80  		}
81  		return lowest;
82  	}
83  
84  	@Override
85  	public void extractTriggeredEvents(List<MEventListener> res, MCard source,
86  			Test globalTest) {
87  		expr.extractTriggeredEvents(res, source, globalTest);
88  	}
89  
90  	/***
91  	 * The zone identifant where the scan is restricted. If is equal to -1, there
92  	 * would be no restriction zone.
93  	 * 
94  	 * @see net.sf.magicproject.token.IdZones
95  	 */
96  	private int restrictionZone;
97  
98  	/***
99  	 * The test used to determine which card are considered in evaluation
100 	 */
101 	private Test test;
102 
103 	/***
104 	 * The expression to evaluate for each valid card
105 	 */
106 	private Expression expr;
107 
108 }