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