View Javadoc

1   /*
2    * Created on Jul 27, 2004 
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.clickable.ability;
22  
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.util.Collection;
26  
27  import net.sf.magicproject.action.MAction;
28  import net.sf.magicproject.clickable.targetable.card.MCard;
29  import net.sf.magicproject.event.CanICast;
30  import net.sf.magicproject.event.MEventListener;
31  import net.sf.magicproject.event.context.ContextEventListener;
32  import net.sf.magicproject.stack.StackManager;
33  import net.sf.magicproject.token.TrueFalseAuto;
34  import net.sf.magicproject.ui.i18n.LanguageManager;
35  
36  /***
37   * An ability that can be manually played.
38   * 
39   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
40   */
41  public class ActivatedAbility extends UserAbility {
42  
43  	/***
44  	 * Creates a new instance of ActivatedAbility <br>
45  	 * <ul>
46  	 * Structure of InputStream : Data[size]
47  	 * </ul>
48  	 * 
49  	 * @param inputFile
50  	 * @param card
51  	 * @throws IOException
52  	 *           if error occurred during the reading process from the specified
53  	 *           input stream
54  	 */
55  	public ActivatedAbility(InputStream inputFile, MCard card) throws IOException {
56  		super(inputFile, card);
57  		updateManaAbilityTag();
58  	}
59  
60  	@Override
61  	public Ability clone(MCard container) {
62  		return new ActivatedAbility(getName(), actionList, effectList, optimizer,
63  				priority, eventComing.clone(container), pictureName, linkedAbilities,
64  				playAsSpell);
65  	}
66  
67  	/***
68  	 * Create an instance of ActivatedAbility
69  	 * 
70  	 * @param name
71  	 *          Name of card used to display this ability in a stack
72  	 * @param actionList
73  	 *          list of actions to do for activate this ability
74  	 * @param effectList
75  	 *          list of effects of this ability
76  	 * @param optimizer
77  	 *          the optimizer to use.
78  	 * @param priority
79  	 *          the resolution type.
80  	 * @param eventComing
81  	 *          event condition of this ability
82  	 * @param pictureName
83  	 *          the picture name of this ability. If <code>null</code> the card
84  	 *          picture will be used instead.
85  	 * @param linkedAbilities
86  	 *          the linked abilities. May be null.
87  	 * @param playAsSpell
88  	 *          play-as-spell.
89  	 */
90  	protected ActivatedAbility(String name, MAction[] actionList,
91  			MAction[] effectList, Optimization optimizer, Priority priority,
92  			MEventListener eventComing, String pictureName,
93  			Collection<Ability> linkedAbilities, TrueFalseAuto playAsSpell) {
94  		super(name, actionList, effectList, optimizer, priority, eventComing,
95  				pictureName, linkedAbilities, playAsSpell);
96  		updateManaAbilityTag();
97  	}
98  
99  	/***
100 	 * Iterate on actions of effect part looking for a 'give mana' action. If one
101 	 * or several are found, the <code>idCard</code> of canIcast event will be
102 	 * replaced by <code>IdTokens.MANA_ABILITY</code>
103 	 */
104 	private void updateManaAbilityTag() {
105 		if (eventComing instanceof CanICast) {
106 			((CanICast) eventComing).updateManaAbilityTag(effectList);
107 		}
108 	}
109 
110 	@Override
111 	public void resolveStack() {
112 		if (priority.isAutoResolve()) {
113 			if (StackManager.actionManager.advancedMode) {
114 				/*
115 				 * we are in the middle of cost part, and the activated previous ability
116 				 * has been inserted. Instead of resolving the stack, we replay the
117 				 * current action witch must be a Waiting and ChoosenAction action.
118 				 */
119 				StackManager.actionManager.reactivate();
120 			} else {
121 				/*
122 				 * this ability has been played entirely, and removed from stack.
123 				 * Instead of calling the normal 'resolveStack' method as normal, we
124 				 * give to the same active player the priority to play another
125 				 * auto-resolving ability or a 'normal' one
126 				 */
127 				StackManager.activePlayer().waitTriggeredBufferChoice(true);
128 			}
129 		} else {
130 			super.resolveStack();
131 		}
132 	}
133 
134 	@Override
135 	public String getLog(ContextEventListener context) {
136 		return toHtmlString(context) + "' of " + getCard();
137 	}
138 
139 	@Override
140 	public String getAbilityTitle() {
141 		return LanguageManager.getString("activatedability")
142 				+ (getName() != null ? ": " + getName() : "") + super.getAbilityTitle();
143 	}
144 
145 }