View Javadoc

1   /*
2    * Created on 2 mars 2005
3    * 
4    *   Firemox is a turn based strategy simulator
5    *   Copyright (C) 2003-2007 Fabrice Daugan
6    *
7    *   This program is free software; you can redistribute it and/or modify it 
8    * under the terms of the GNU General Public License as published by the Free 
9    * Software Foundation; either version 2 of the License, or (at your option) any
10   * later version.
11   *
12   *   This program is distributed in the hope that it will be useful, but WITHOUT 
13   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
15   * details.
16   *
17   *   You should have received a copy of the GNU General Public License along  
18   * with this program; if not, write to the Free Software Foundation, Inc., 
19   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   */
21  package net.sf.firemox.action;
22  
23  import java.io.IOException;
24  import java.io.InputStream;
25  
26  import net.sf.firemox.clickable.ability.Ability;
27  import net.sf.firemox.clickable.target.Target;
28  import net.sf.firemox.clickable.target.card.MCard;
29  import net.sf.firemox.event.AssignedDamage;
30  import net.sf.firemox.event.context.ContextEventListener;
31  import net.sf.firemox.expression.Expression;
32  import net.sf.firemox.expression.ExpressionFactory;
33  import net.sf.firemox.stack.StackManager;
34  import net.sf.firemox.token.IdConst;
35  import net.sf.firemox.token.IdZones;
36  import net.sf.firemox.ui.i18n.LanguageManagerMDB;
37  
38  /***
39   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
40   * @since 0.82
41   */
42  class AssignDamageSourceDest extends UserAction implements LoopAction {
43  
44  	/***
45  	 * Create an instance of AssignDamageTarget by reading a file Offset's file
46  	 * must pointing on the first byte of this action <br>
47  	 * <ul>
48  	 * Structure of InputStream : Data[size]
49  	 * <li>super [Action]</li>
50  	 * <li>amount [Expression]</li>
51  	 * <li>type [Expression]</li>
52  	 * </ul>
53  	 * 
54  	 * @param inputFile
55  	 *          file containing this action
56  	 * @throws IOException
57  	 *           If some other I/O error occurs
58  	 * @see net.sf.firemox.token.IdDamageTypes
59  	 */
60  	AssignDamageSourceDest(InputStream inputFile) throws IOException {
61  		super(inputFile);
62  		valueExpr = ExpressionFactory.readNextExpression(inputFile);
63  		type = ExpressionFactory.readNextExpression(inputFile);
64  	}
65  
66  	@Override
67  	public final Actiontype getIdAction() {
68  		return Actiontype.ASSIGN_DAMAGE_SOURCE_DEST;
69  	}
70  
71  	public int getStartIndex() {
72  		// TODO Implement completely this action
73  		// return StackManager.getInstance().getTargetedList().size() - 1;
74  		return 0;
75  	}
76  
77  	public boolean continueLoop(ContextEventListener context, int loopingIndex,
78  			Ability ability) {
79  		final int type = this.type.getValue(ability, null, context);
80  		final int value = valueExpr.getValue(ability, ability.getCard(), context);
81  		final MCard source = (MCard) StackManager.getInstance().getTargetedList()
82  				.get(0);
83  		final Target target = StackManager.getInstance().getTargetedList().get(1);
84  		if (!target.isCard() || checkTimeStamp(context, (MCard) target)
85  				&& ((MCard) target).getIdZone() == IdZones.PLAY) {
86  			if (!AssignedDamage.tryAction(source, target, value, type)) {
87  				// this action has been replaced
88  				return false;
89  			}
90  			if (value > 0) {
91  				AssignedDamage.dispatchEvent(source, target, value, type);
92  			}
93  		}
94  		return true;
95  	}
96  
97  	@Override
98  	public String toString(Ability ability) {
99  		try {
100 			if (valueExpr.getValue(ability, null, null) == IdConst.ALL) {
101 				return LanguageManagerMDB.getString("assign-damage-target-all");
102 			}
103 			return LanguageManagerMDB.getString("assign-damage-target", valueExpr
104 					.getValue(ability, null, null));
105 		} catch (Exception e) {
106 			return LanguageManagerMDB.getString("assign-damage-target", "?");
107 		}
108 	}
109 
110 	/***
111 	 * The complex expression to use for the right value. Is null if the IdToken
112 	 * number is not a complex expression.
113 	 */
114 	private final Expression valueExpr;
115 
116 	/***
117 	 * represent the type of damage
118 	 */
119 	private final Expression type;
120 
121 }