View Javadoc

1   /* 
2    * SkipPhase.java 
3    * Created on 20 févr. 2004
4    * 
5    *   Magic-Project is a turn based strategy simulator
6    *   Copyright (C) 2003-2007 Fabrice Daugan
7    *
8    *   This program is free software; you can redistribute it and/or modify it 
9    * under the terms of the GNU General Public License as published by the Free 
10   * Software Foundation; either version 2 of the License, or (at your option) any
11   * later version.
12   *
13   *   This program is distributed in the hope that it will be useful, but WITHOUT 
14   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
16   * details.
17   *
18   *   You should have received a copy of the GNU General Public License along  
19   * with this program; if not, write to the Free Software Foundation, Inc., 
20   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21   */
22  package net.sf.magicproject.action;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  
27  import net.sf.magicproject.action.handler.StandardAction;
28  import net.sf.magicproject.clickable.ability.Ability;
29  import net.sf.magicproject.clickable.targetable.player.Player;
30  import net.sf.magicproject.event.context.ContextEventListener;
31  import net.sf.magicproject.expression.Expression;
32  import net.sf.magicproject.expression.ExpressionFactory;
33  import net.sf.magicproject.stack.MPhase;
34  import net.sf.magicproject.stack.StackManager;
35  
36  /***
37   * Skip a specified phase following it's identifiant. The specified phase name
38   * must have been previously defined in the source file or include. The target
39   * list is used to known the player(s) phase to skip.
40   * 
41   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
42   * @since 0.1
43   */
44  class SkipPhase extends UserAction implements StandardAction {
45  
46  	/***
47  	 * Create an instance of SkipPhase by reading a file Offset's file must
48  	 * pointing on the first byte of this action <br>
49  	 * <ul>
50  	 * Structure of InputStream : Data[size]
51  	 * <li>phase [Expression]</li>
52  	 * </ul>
53  	 * 
54  	 * @param inputFile
55  	 *          file containing this action
56  	 * @throws IOException
57  	 *           if error occurred during the reading process from the specified
58  	 *           input stream
59  	 */
60  	SkipPhase(InputStream inputFile) throws IOException {
61  		super(inputFile);
62  		phase = ExpressionFactory.readNextExpression(inputFile);
63  	}
64  
65  	@Override
66  	public final Actiontype getIdAction() {
67  		return Actiontype.SKIP_PHASE;
68  	}
69  
70  	public boolean play(ContextEventListener context, Ability ability) {
71  		int idPhase = phase.getValue(ability, null, context);
72  		for (int i = StackManager.getInstance().getTargetedList().size(); i-- > 0;) {
73  			int idPlayer = ((Player) StackManager.getInstance().getTargetedList()
74  					.get(i)).idPlayer;
75  			for (int j = MPhase.phases[idPlayer].length; j-- > 0;) {
76  				if (MPhase.phases[idPlayer][j].phaseType.id == idPhase) {
77  					MPhase.phases[idPlayer][j].skipThisPhase = true;
78  				}
79  			}
80  		}
81  		return true;
82  	}
83  
84  	@Override
85  	public String toString(Ability ability) {
86  		return "skip phase";
87  	}
88  
89  	/***
90  	 * The phase index of the targeted player to skip
91  	 */
92  	private Expression phase;
93  
94  }