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.action;
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import net.sf.magicproject.action.handler.StandardAction;
28  import net.sf.magicproject.clickable.ability.Ability;
29  import net.sf.magicproject.clickable.targetable.Targetable;
30  import net.sf.magicproject.event.context.ContextEventListener;
31  import net.sf.magicproject.stack.EventManager;
32  import net.sf.magicproject.stack.StackManager;
33  import net.sf.magicproject.test.Test;
34  import net.sf.magicproject.test.TestFactory;
35  import net.sf.magicproject.token.IdTokens;
36  import net.sf.magicproject.token.Register;
37  import net.sf.magicproject.tools.Log;
38  
39  /***
40   * Add all valid targets in the list following the specified type. No event is
41   * generated. The friendly test, and the type are required to list the valids
42   * targets. The target list is used by the most actions. <br>
43   * For example, if the next action 'damage', all cards/player from the target
44   * list would be damaged. When a new ability is added to the stack, a new list
45   * is created and attached to it. Actions may modify, acceess it until the
46   * ability is completely resolved.
47   * 
48   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
49   * @since 0.86
50   */
51  class TargetAllNoEvent extends UserAction implements StandardAction {
52  
53  	/***
54  	 * Create an instance by reading a file Offset's file must pointing on the
55  	 * first byte of this action <br>
56  	 * <ul>
57  	 * Structure of InputStream : Data[size]
58  	 * <li>type [Register]</li>
59  	 * <li>test [Test]</li>
60  	 * <li>restriction Zone id [int]</li>
61  	 * <li>can be preempted [boolean]</li>
62  	 * </ul>
63  	 * For the available options, see interface IdTargets
64  	 * 
65  	 * @param inputFile
66  	 *          file containing this action
67  	 * @throws IOException
68  	 *           if error occurred during the reading process from the specified
69  	 *           input stream
70  	 * @see net.sf.magicproject.token.IdTargets
71  	 */
72  	TargetAllNoEvent(InputStream inputFile) throws IOException {
73  		super(inputFile);
74  		type = Register.deserialize(inputFile).ordinal();
75  		test = TestFactory.readNextTest(inputFile);
76  		restrictionZone = inputFile.read() - 1;
77  		// consume the no needed preemption value
78  		inputFile.read();
79  	}
80  
81  	public final boolean play(ContextEventListener context, Ability ability) {
82  		final List<Targetable> validTargets = new ArrayList<Targetable>();
83  		String targetTxt = null;
84  		switch (type) {
85  		case IdTokens.DEALTABLE:
86  			// Target is a player or card
87  			targetTxt = "[all creatures and all players]";
88  			if (test
89  					.test(ability, StackManager.PLAYERS[StackManager.idCurrentPlayer])) {
90  				validTargets.add(StackManager.PLAYERS[StackManager.idCurrentPlayer]);
91  			}
92  			if (test.test(ability,
93  					StackManager.PLAYERS[1 - StackManager.idCurrentPlayer])) {
94  				validTargets
95  						.add(StackManager.PLAYERS[1 - StackManager.idCurrentPlayer]);
96  			}
97  			if (restrictionZone != -1) {
98  				StackManager.PLAYERS[StackManager.idCurrentPlayer].zoneManager
99  						.getContainer(restrictionZone).checkAllCardsOf(test, validTargets,
100 								ability);
101 				StackManager.PLAYERS[1 - StackManager.idCurrentPlayer].zoneManager
102 						.getContainer(restrictionZone).checkAllCardsOf(test, validTargets,
103 								ability);
104 			} else {
105 				StackManager.PLAYERS[StackManager.idCurrentPlayer].zoneManager
106 						.checkAllCardsOf(test, validTargets, ability);
107 				StackManager.PLAYERS[1 - StackManager.idCurrentPlayer].zoneManager
108 						.checkAllCardsOf(test, validTargets, ability);
109 			}
110 			break;
111 		case IdTokens.PLAYER:
112 			// Target is a player
113 			targetTxt = "[all players]";
114 			if (test
115 					.test(ability, StackManager.PLAYERS[StackManager.idCurrentPlayer])) {
116 				validTargets.add(StackManager.PLAYERS[StackManager.idCurrentPlayer]);
117 			}
118 			if (test.test(ability,
119 					StackManager.PLAYERS[1 - StackManager.idCurrentPlayer])) {
120 				validTargets
121 						.add(StackManager.PLAYERS[1 - StackManager.idCurrentPlayer]);
122 			}
123 			break;
124 		case IdTokens.CARD:
125 			// Target is a card
126 			targetTxt = "[all cards]";
127 			if (restrictionZone != -1) {
128 				StackManager.PLAYERS[StackManager.idCurrentPlayer].zoneManager
129 						.getContainer(restrictionZone).checkAllCardsOf(test, validTargets,
130 								ability);
131 				StackManager.PLAYERS[1 - StackManager.idCurrentPlayer].zoneManager
132 						.getContainer(restrictionZone).checkAllCardsOf(test, validTargets,
133 								ability);
134 			} else {
135 				StackManager.PLAYERS[StackManager.idCurrentPlayer].zoneManager
136 						.checkAllCardsOf(test, validTargets, ability);
137 				StackManager.PLAYERS[1 - StackManager.idCurrentPlayer].zoneManager
138 						.checkAllCardsOf(test, validTargets, ability);
139 			}
140 			break;
141 		default:
142 			throw new InternalError("Unknown type :" + type);
143 		}
144 		if (StackManager.getInstance().getTargetedList().list != null) {
145 			validTargets.removeAll(StackManager.getInstance().getTargetedList().list);
146 		}
147 		Log.debug("all mode, size=" + validTargets.size() + ", ignored="
148 				+ StackManager.getInstance().getTargetedList().list);
149 		EventManager.moreInfoLbl.setText(targetTxt + " - all mode)");
150 		if (!validTargets.isEmpty()) {
151 			StackManager.getInstance().getTargetedList().list.addAll(validTargets);
152 		}
153 		// continue the stack process
154 		return true;
155 	}
156 
157 	@Override
158 	public final Actiontype getIdAction() {
159 		return Actiontype.TARGET_ALL;
160 	}
161 
162 	@Override
163 	public String toString(Ability ability) {
164 		switch (type) {
165 		case IdTokens.CARD:
166 			return "[all cards]";
167 		case IdTokens.PLAYER:
168 			return "[all players]";
169 		case IdTokens.DEALTABLE:
170 			return "[all cards and players]";
171 		default:
172 			throw new InternalError("Unknown type : " + type);
173 		}
174 	}
175 
176 	/***
177 	 * represents the test applied to target before to be added to the list
178 	 */
179 	protected Test test;
180 
181 	/***
182 	 * represents the type of target (player, card, dealtable)
183 	 * 
184 	 * @see net.sf.magicproject.token.IdTargets
185 	 */
186 	protected int type;
187 
188 	/***
189 	 * The zone identifant where the scan is restricted. If is equal to -1, there
190 	 * would be no restriction zone.
191 	 * 
192 	 * @see net.sf.magicproject.token.IdZones
193 	 */
194 	protected int restrictionZone;
195 }