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.action.handler.StandardAction;
26  import net.sf.magicproject.clickable.ability.Ability;
27  import net.sf.magicproject.clickable.targetable.player.Player;
28  import net.sf.magicproject.event.GivenMana;
29  import net.sf.magicproject.event.context.ContextEventListener;
30  import net.sf.magicproject.event.context.MContextCardCardIntIntTest;
31  import net.sf.magicproject.stack.StackManager;
32  import net.sf.magicproject.test.ContextTest;
33  import net.sf.magicproject.test.Test;
34  import net.sf.magicproject.test.TestFactory;
35  import net.sf.magicproject.test.TestOn;
36  
37  /***
38   * It's the mana source action, modify directly the mana pool like : Land, other
39   * mana sources
40   * 
41   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
42   * @since 0.54
43   * @since 0.72 support counter ability
44   */
45  public abstract class GiveMana extends UserAction implements StandardAction {
46  
47  	/***
48  	 * Create an instance of GiveMana by reading a file Offset's file must
49  	 * pointing on the first byte of this action <br>
50  	 * <ul>
51  	 * Structure of stream : Data[size]
52  	 * <li>restriction usage test [...]</li>
53  	 * <li>Player receiving this mana [TestOn]</li>
54  	 * </ul>
55  	 * 
56  	 * @param inputFile
57  	 *          file containing this action
58  	 * @throws IOException
59  	 *           if error occurred during the reading process from the specified
60  	 *           input stream
61  	 */
62  	protected GiveMana(InputStream inputFile) throws IOException {
63  		super(inputFile);
64  		restriction = TestFactory.readNextTest(inputFile);
65  		player = TestOn.deserialize(inputFile);
66  	}
67  
68  	/***
69  	 * Add mana to the current player
70  	 * 
71  	 * @param ability
72  	 *          is the ability owning this action. The card component of this
73  	 *          ability may correspond to the card owning this action too.
74  	 * @param context
75  	 *          is the context attached to this action.
76  	 * @param color
77  	 *          the mana color to add
78  	 * @param value
79  	 *          the amount of mana to add
80  	 */
81  	protected void giveMana(Ability ability, ContextEventListener context,
82  			int color, int value) {
83  		if (value <= 0) {
84  			StackManager.resolveStack();
85  		} else {
86  			final Player destinationPlayer = (Player) player.getTargetable(ability,
87  					context, null);
88  			if (GivenMana.tryAction(ability.getCard(), destinationPlayer, color,
89  					value, getRestrictionTest())) {
90  				destinationPlayer.mana.addMana(color, value, getRestrictionTest());
91  				GivenMana.dispatchEvent(ability.getCard(), destinationPlayer, color,
92  						value, getRestrictionTest());
93  				StackManager.resolveStack();
94  			}
95  		}
96  	}
97  
98  	private Test getRestrictionTest() {
99  		return restriction instanceof ContextTest ? ((MContextCardCardIntIntTest) StackManager
100 				.getInstance().getAbilityContext()).test
101 				: restriction;
102 	}
103 
104 	public abstract boolean play(ContextEventListener context, Ability ability);
105 
106 	@Override
107 	public abstract String toHtmlString(Ability ability,
108 			ContextEventListener context);
109 
110 	@Override
111 	public abstract String toString(Ability ability);
112 
113 	/***
114 	 * The restriction mana usage test
115 	 */
116 	protected Test restriction;
117 
118 	/***
119 	 * Player receiving this mana
120 	 */
121 	protected TestOn player;
122 
123 }