1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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
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 }