View Javadoc

1   /*
2    *   Magic-Project 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   * AssignDamageTarget.java 
20   * Created on 20 févr. 2004
21   */
22  package net.sf.magicproject.action;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  
27  import net.sf.magicproject.clickable.ability.Ability;
28  import net.sf.magicproject.clickable.targetable.Targetable;
29  import net.sf.magicproject.clickable.targetable.card.MCard;
30  import net.sf.magicproject.event.AssignedDamage;
31  import net.sf.magicproject.event.context.ContextEventListener;
32  import net.sf.magicproject.expression.Expression;
33  import net.sf.magicproject.expression.ExpressionFactory;
34  import net.sf.magicproject.stack.StackManager;
35  import net.sf.magicproject.token.IdConst;
36  import net.sf.magicproject.token.IdZones;
37  import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
38  
39  /***
40   * To damage the target list.
41   * 
42   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
43   * @since 0.54
44   * @since 0.72 support counter ability
45   * @since 0.80 support replacement
46   */
47  class AssignDamageTarget extends UserAction implements LoopAction {
48  
49  	/***
50  	 * Create an instance of AssignDamageTarget by reading a file Offset's file
51  	 * must pointing on the first byte of this action <br>
52  	 * <ul>
53  	 * Structure of InputStream : Data[size]
54  	 * <li>[super]</li>
55  	 * <li>amount [Expression]</li>
56  	 * <li>type [Expression]</li>
57  	 * </ul>
58  	 * 
59  	 * @param inputFile
60  	 *          file containing this action
61  	 * @throws IOException
62  	 *           If some other I/O error occurs
63  	 * @see net.sf.magicproject.token.IdDamageTypes
64  	 */
65  	AssignDamageTarget(InputStream inputFile) throws IOException {
66  		super(inputFile);
67  		valueExpr = ExpressionFactory.readNextExpression(inputFile);
68  		type = ExpressionFactory.readNextExpression(inputFile);
69  	}
70  
71  	@Override
72  	public final Actiontype getIdAction() {
73  		return Actiontype.ASSIGN_DAMAGE_TARGET;
74  	}
75  
76  	public int getStartIndex() {
77  		return StackManager.getInstance().getTargetedList().size() - 1;
78  	}
79  
80  	public boolean continueLoop(ContextEventListener context, int loopingIndex,
81  			Ability ability) {
82  		final int value = valueExpr.getValue(ability, ability.getCard(), context);
83  		final Targetable target = StackManager.getInstance().getTargetedList().get(
84  				loopingIndex);
85  		final int type = this.type.getValue(ability, null, context);
86  		final MCard source = StackManager.getRealSource(ability.getCard());
87  		if (!target.isCard() || checkTimeStamp(context, (MCard) target)
88  				&& ((MCard) target).getIdZone() == IdZones.PLAY) {
89  
90  			if (!AssignedDamage.tryAction(source, target, value, type)) {
91  				// this action has been replaced
92  				return false;
93  			}
94  
95  			if (value > 0) {
96  				AssignedDamage.dispatchEvent(source, target, value, type);
97  			}
98  		}
99  		return true;
100 	}
101 
102 	@Override
103 	public String toString(Ability ability) {
104 		try {
105 			if (valueExpr.getValue(ability, null, null) == IdConst.ALL) {
106 				return LanguageManagerMDB.getString("destroy-target");
107 			}
108 			return "" + valueExpr.getValue(ability, null, null) + " "
109 					+ LanguageManagerMDB.getString("damage-target");
110 		} catch (Exception e) {
111 			return "?" + " " + LanguageManagerMDB.getString("damage-target");
112 		}
113 	}
114 
115 	/***
116 	 * The complex expression to use for the right value. Is null if the IdToken
117 	 * number is not a complex expression.
118 	 */
119 	private Expression valueExpr = null;
120 
121 	/***
122 	 * represent the type of damage
123 	 */
124 	private Expression type;
125 
126 }