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.action;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  
24  import net.sf.magicproject.action.context.ActionContextWrapper;
25  import net.sf.magicproject.action.context.ObjectArray;
26  import net.sf.magicproject.action.handler.ChoosenAction;
27  import net.sf.magicproject.action.handler.FollowAction;
28  import net.sf.magicproject.action.handler.StandardAction;
29  import net.sf.magicproject.clickable.ability.Ability;
30  import net.sf.magicproject.clickable.targetable.player.Player;
31  import net.sf.magicproject.event.context.ContextEventListener;
32  import net.sf.magicproject.stack.StackManager;
33  import net.sf.magicproject.token.IdZones;
34  import net.sf.magicproject.token.Visibility;
35  import net.sf.magicproject.token.VisibilityChange;
36  import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
37  
38  /***
39   * Set the player's zone visible/hidden to him/opponent. This action use the
40   * target list to determine the zone owner. The 'toOwner' field is there to
41   * determine whom this zone will be shown/hidden. If this attribute is 'true',
42   * the visibility to targeted player will be modified. Otherwise, the visibility
43   * of opponent of player will be modified.
44   * 
45   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
46   * @since 0.71
47   */
48  class ZoneVisibility extends UserAction implements StandardAction,
49  		FollowAction, ChoosenAction {
50  
51  	/***
52  	 * Create an instance of ZoneVisibility by reading a file Offset's file must
53  	 * pointing on the first byte of this action. <br>
54  	 * <ul>
55  	 * Structure of InputStream : Data[size]
56  	 * <li>zone identifiant [1]</li>
57  	 * <li>show=1,hide=0 [1]</li>
58  	 * <li>to him=0,to opponent=1,to you=2 [1]</li>
59  	 * </ul>
60  	 * 
61  	 * @param inputFile
62  	 *          file containing this action
63  	 * @param card
64  	 *          owning this action
65  	 * @throws IOException
66  	 *           if error occurred during the reading process from the specified
67  	 *           input stream
68  	 */
69  	ZoneVisibility(InputStream inputFile) throws IOException {
70  		super(inputFile);
71  		idZone = inputFile.read();
72  		visible = inputFile.read() != 0;
73  		idFor = VisibilityChange.deserialize(inputFile);
74  	}
75  
76  	@Override
77  	public final Actiontype getIdAction() {
78  		return Actiontype.ZONE_VISIBILITY;
79  	}
80  
81  	public boolean play(ContextEventListener context, Ability ability) {
82  		if (idFor == VisibilityChange.controller) {
83  			// set the visibility of card for the targeted player
84  			for (int i = StackManager.getInstance().getTargetedList().size(); i-- > 0;) {
85  				if (StackManager.getInstance().getTargetedList().get(i).isPlayer()) {
86  					final Player player = (Player) StackManager.getInstance()
87  							.getTargetedList().get(i);
88  					Visibility visibility = player.zoneManager.getContainer(idZone).visibility;
89  					player.zoneManager.getContainer(idZone).setVisibility(
90  							visible ? visibility.increaseFor(player) : visibility
91  									.decreaseFor(player));
92  				}
93  			}
94  		} else if (idFor == VisibilityChange.opponent) {
95  			// set the visibility of card for opponent
96  			for (int i = StackManager.getInstance().getTargetedList().size(); i-- > 0;) {
97  				if (StackManager.getInstance().getTargetedList().get(i).isPlayer()) {
98  					final Player player = (Player) StackManager.getInstance()
99  							.getTargetedList().get(i);
100 					Visibility visibility = player.zoneManager.getContainer(idZone).visibility;
101 					player.zoneManager.getContainer(idZone).setVisibility(
102 							visible ? visibility.increaseFor(player.getOpponent())
103 									: visibility.decreaseFor(player.getOpponent()));
104 				}
105 			}
106 		} else {
107 			// set the visibility of card for you
108 			for (int i = StackManager.getInstance().getTargetedList().size(); i-- > 0;) {
109 				if (StackManager.getInstance().getTargetedList().get(i).isPlayer()) {
110 					final Player player = (Player) StackManager.getInstance()
111 							.getTargetedList().get(i);
112 					Visibility visibility = player.zoneManager.getContainer(idZone).visibility;
113 					player.zoneManager.getContainer(idZone).setVisibility(
114 							visible ? visibility.increaseFor(StackManager
115 									.getSpellController()) : visibility.decreaseFor(StackManager
116 									.getSpellController()));
117 				}
118 			}
119 		}
120 		return true;
121 	}
122 
123 	public void simulate(ActionContextWrapper actionContext,
124 			ContextEventListener context, Ability ability) {
125 		ObjectArray<Visibility> contextN = new ObjectArray<Visibility>(StackManager
126 				.getInstance().getTargetedList().size() * 2);
127 		actionContext.actionContext = contextN;
128 		switch (idFor) {
129 		case controller:
130 			// set the visibility of card for the targeted player
131 			for (int i = StackManager.getInstance().getTargetedList().size(); i-- > 0;) {
132 				if (StackManager.getInstance().getTargetedList().get(i).isPlayer()) {
133 					final Player player = (Player) StackManager.getInstance()
134 							.getTargetedList().get(i);
135 					Visibility visibility = player.zoneManager.getContainer(idZone).visibility;
136 					contextN.setObject(i, visibility);
137 					contextN.setObject(2 * i + 1, visible ? visibility
138 							.increaseFor(player) : visibility.decreaseFor(player));
139 					player.zoneManager.getContainer(idZone).setVisibility(
140 							contextN.getObject(2 * i + 1));
141 				}
142 			}
143 			break;
144 		case opponent:
145 			// set the visibility of card for opponent
146 			for (int i = StackManager.getInstance().getTargetedList().size(); i-- > 0;) {
147 				if (StackManager.getInstance().getTargetedList().get(i).isPlayer()) {
148 					final Player player = (Player) StackManager.getInstance()
149 							.getTargetedList().get(i);
150 					Visibility visibility = player.zoneManager.getContainer(idZone).visibility;
151 					contextN.setObject(i, visibility);
152 					contextN.setObject(2 * i + 1, visible ? visibility.increaseFor(player
153 							.getOpponent()) : visibility.decreaseFor(player.getOpponent()));
154 					player.zoneManager.getContainer(idZone).setVisibility(
155 							contextN.getObject(2 * i + 1));
156 				}
157 			}
158 			break;
159 		default:
160 			// set the visibility of card for you
161 			for (int i = StackManager.getInstance().getTargetedList().size(); i-- > 0;) {
162 				if (StackManager.getInstance().getTargetedList().get(i).isPlayer()) {
163 					final Player player = (Player) StackManager.getInstance()
164 							.getTargetedList().get(i);
165 					Visibility visibility = player.zoneManager.getContainer(idZone).visibility;
166 					contextN.setObject(i, visibility);
167 					contextN.setObject(2 * i + 1, visible ? visibility
168 							.increaseFor(StackManager.getSpellController()) : visibility
169 							.decreaseFor(StackManager.getSpellController()));
170 					player.zoneManager.getContainer(idZone).setVisibility(
171 							contextN.getObject(2 * i + 1));
172 				}
173 			}
174 		}
175 	}
176 
177 	public void rollback(ActionContextWrapper actionContext,
178 			ContextEventListener context, Ability ability) {
179 		ObjectArray<Visibility> contextN = (ObjectArray<Visibility>) actionContext.actionContext;
180 		for (int i = StackManager.getInstance().getTargetedList().size(); i-- > 0;) {
181 			if (StackManager.getInstance().getTargetedList().get(i).isPlayer()) {
182 				final Player player = (Player) StackManager.getInstance()
183 						.getTargetedList().get(i);
184 				player.zoneManager.getContainer(idZone).setVisibility(
185 						contextN.getObject(2 * i));
186 			}
187 		}
188 	}
189 
190 	@Override
191 	public String toString(Ability ability) {
192 		switch (idFor) {
193 		case controller:
194 			return LanguageManagerMDB.getString(visible ? "reveal-to-player"
195 					: "hide-to-player", LanguageManagerMDB
196 					.getString(IdZones.ZONE_NAMES[idZone]));
197 		case opponent:
198 			return LanguageManagerMDB.getString(visible ? "reveal-to-opponent"
199 					: "hide-to-opponent", LanguageManagerMDB
200 					.getString(IdZones.ZONE_NAMES[idZone]));
201 		default:
202 			return LanguageManagerMDB.getString(visible ? "look" : "hide",
203 					LanguageManagerMDB.getString(IdZones.ZONE_NAMES[idZone]));
204 		}
205 	}
206 
207 	public boolean choose(ActionContextWrapper actionContext,
208 			ContextEventListener context, Ability ability) {
209 		simulate(actionContext, context, ability);
210 		return true;
211 	}
212 
213 	public void disactivate(ActionContextWrapper actionContext,
214 			ContextEventListener context, Ability ability) {
215 		// Nothing to do
216 	}
217 
218 	public boolean replay(ActionContextWrapper actionContext,
219 			ContextEventListener context, Ability ability) {
220 		ObjectArray<Visibility> contextN = (ObjectArray<Visibility>) actionContext.actionContext;
221 		for (int i = StackManager.getInstance().getTargetedList().size(); i-- > 0;) {
222 			if (StackManager.getInstance().getTargetedList().get(i).isPlayer()) {
223 				final Player player = (Player) StackManager.getInstance()
224 						.getTargetedList().get(i);
225 				player.zoneManager.getContainer(idZone).setVisibility(
226 						contextN.getObject(i * 2 + 1));
227 			}
228 		}
229 		return true;
230 	}
231 
232 	public String toHtmlString(Ability ability, ContextEventListener context,
233 			ActionContextWrapper actionContext) {
234 		return toString(ability);
235 	}
236 
237 	/***
238 	 * Indicates if the zone of the list of players would be hidden to his/her
239 	 * opponent.
240 	 */
241 	private boolean visible;
242 
243 	/***
244 	 * Indicates the zone identifiant to show/hide.
245 	 */
246 	private int idZone;
247 
248 	/***
249 	 * Indicates if the zone would be hidden/shown to the targeted player, to the
250 	 * opponent of targeted player, or to you
251 	 */
252 	private VisibilityChange idFor;
253 
254 }