View Javadoc

1   /*
2    * Created on 2 mars 2005
3    * 
4    *   Magic-Project 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.magicproject.action;
22  
23  import java.io.IOException;
24  import java.io.InputStream;
25  
26  import net.sf.magicproject.clickable.ability.Ability;
27  import net.sf.magicproject.clickable.targetable.Targetable;
28  import net.sf.magicproject.clickable.targetable.card.MCard;
29  import net.sf.magicproject.event.AssignedDamage;
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.StackManager;
34  import net.sf.magicproject.token.IdConst;
35  import net.sf.magicproject.token.IdZones;
36  import net.sf.magicproject.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.magicproject.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 Targetable target = StackManager.getInstance().getTargetedList().get(
84  				1);
85  		if (!target.isCard() || checkTimeStamp(context, (MCard) target)
86  				&& ((MCard) target).getIdZone() == IdZones.PLAY) {
87  			if (!AssignedDamage.tryAction(source, target, value, type)) {
88  				// this action has been replaced
89  				return false;
90  			}
91  			if (value > 0) {
92  				AssignedDamage.dispatchEvent(source, target, value, type);
93  			}
94  		}
95  		return true;
96  	}
97  
98  	@Override
99  	public String toString(Ability ability) {
100 		try {
101 			if (valueExpr.getValue(ability, null, null) == IdConst.ALL) {
102 				return LanguageManagerMDB.getString("destroy-target");
103 			}
104 			return "" + valueExpr.getValue(ability, null, null) + " "
105 					+ LanguageManagerMDB.getString("damage-target");
106 		} catch (Exception e) {
107 			return "?" + " " + LanguageManagerMDB.getString("damage-target");
108 		}
109 	}
110 
111 	/***
112 	 * The complex expression to use for the right value. Is null if the IdToken
113 	 * number is not a complex expression.
114 	 */
115 	private final Expression valueExpr;
116 
117 	/***
118 	 * represent the type of damage
119 	 */
120 	private final Expression type;
121 
122 }