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   */
20  package net.sf.magicproject.action;
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  
25  import net.sf.magicproject.clickable.ability.Ability;
26  import net.sf.magicproject.clickable.targetable.Targetable;
27  import net.sf.magicproject.event.context.ContextEventListener;
28  import net.sf.magicproject.operation.Add;
29  import net.sf.magicproject.operation.Remove;
30  import net.sf.magicproject.stack.StackManager;
31  import net.sf.magicproject.token.IdCommonToken;
32  import net.sf.magicproject.token.IdTokens;
33  
34  /***
35   * This action is used to modifiy a register of a player or a card. <br>
36   * This action can use the target list when is played : the address
37   * (idToken=register name + register index) must be IdTokens#TARGET. So the
38   * target list must set before this action would be played.
39   * 
40   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
41   * @since 0.8
42   */
43  class ModifyTargetListRegister extends ModifyRegister implements LoopAction {
44  
45  	/***
46  	 * Create an instance of ModifyTargetListRegister by reading a file Offset's
47  	 * file must pointing on the first byte of this action <br>
48  	 * <ul>
49  	 * Structure of InputStream : Data[size]
50  	 * <li>[super]</li>
51  	 * </ul>
52  	 * 
53  	 * @param inputFile
54  	 *          file containing this action
55  	 * @throws IOException
56  	 *           if error occurred during the reading process from the specified
57  	 *           input stream
58  	 */
59  	ModifyTargetListRegister(InputStream inputFile) throws IOException {
60  		super(inputFile);
61  	}
62  
63  	@Override
64  	public final Actiontype getIdAction() {
65  		return Actiontype.MODIFY_TARGET_LIST_REGISTER;
66  	}
67  
68  	public boolean continueLoop(ContextEventListener context, int loopingIndex,
69  			Ability ability) {
70  		Targetable target = StackManager.getInstance().getTargetedList().get(
71  				loopingIndex);
72  		return ModifyTargetableRegister.modifyRegister(ability.getCard(), target,
73  				index.getValue(ability, target, context), getValue(ability, target,
74  						context), op);
75  	}
76  
77  	public int getStartIndex() {
78  		return StackManager.getInstance().getTargetedList().size() - 1;
79  	}
80  
81  	@Override
82  	public boolean play(ContextEventListener context, Ability ability) {
83  		throw new InternalError("should not been called");
84  	}
85  
86  	@Override
87  	public String toString(Ability ability) {
88  		final int index = this.index.getValue(ability, null, null);
89  		String value = null;
90  		try {
91  			value = "" + valueExpr.getValue(ability, null, null);
92  		} catch (Exception e) {
93  			value = "?";
94  		}
95  		if (op == Add.getInstance()) {
96  			if (index == IdTokens.LIFE) {
97  				return "Gain " + value + " life";
98  			}
99  			if (index == IdTokens.POISON) {
100 				return "Gain " + value + " poison";
101 			}
102 			if (index == IdCommonToken.DAMAGE) {
103 				return "Add " + value + " damage";
104 			}
105 		}
106 		if (op == Remove.getInstance()) {
107 			if (index == IdTokens.LIFE) {
108 				return "Lose " + value + " life";
109 			}
110 			if (index == IdTokens.POISON) {
111 				return "Lose " + value + " poison";
112 			}
113 			if (index == IdCommonToken.DAMAGE) {
114 				return "Remove " + value + " damage";
115 			}
116 		} else {
117 			if (index == IdTokens.LIFE) {
118 				return op.toString() + " " + value + " life";
119 			}
120 			if (index == IdTokens.POISON) {
121 				return op.toString() + " " + value + " poison";
122 			}
123 			if (index == IdCommonToken.DAMAGE) {
124 				return op.toString() + value + " damage";
125 			}
126 		}
127 		// return op.getClass().getSimpleName() + " (L=" + index + ",R=" + idNumber
128 		// + ")";
129 		return "";
130 	}
131 }