View Javadoc

1   /*
2    * WaitTriggeredBufferChoice.java
3    * Created on 26 févr. 2004
4    * 
5    *   Magic-Project is a turn based strategy simulator
6    *   Copyright (C) 2003-2007 Fabrice Daugan
7    *
8    *   This program is free software; you can redistribute it and/or modify it 
9    * under the terms of the GNU General Public License as published by the Free 
10   * Software Foundation; either version 2 of the License, or (at your option) any
11   * later version.
12   *
13   *   This program is distributed in the hope that it will be useful, but WITHOUT 
14   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
16   * details.
17   *
18   *   You should have received a copy of the GNU General Public License along  
19   * with this program; if not, write to the Free Software Foundation, Inc., 
20   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21   * 
22   */
23  package net.sf.magicproject.action;
24  
25  import net.sf.magicproject.action.handler.StandardAction;
26  import net.sf.magicproject.action.listener.WaitingTriggeredCard;
27  import net.sf.magicproject.clickable.ability.Ability;
28  import net.sf.magicproject.clickable.targetable.card.TriggeredCard;
29  import net.sf.magicproject.event.context.ContextEventListener;
30  import net.sf.magicproject.stack.EventManager;
31  import net.sf.magicproject.stack.StackManager;
32  import net.sf.magicproject.tools.Log;
33  import net.sf.magicproject.zone.TriggeredBuffer;
34  
35  /***
36   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
37   * @since 0.60
38   */
39  public final class WaitTriggeredBufferChoice extends MAction implements
40  		StandardAction, WaitingTriggeredCard {
41  
42  	/***
43  	 * Create a new instance of WaitTriggeredBufferChoice
44  	 */
45  	private WaitTriggeredBufferChoice() {
46  		super();
47  	}
48  
49  	public void finished() {
50  		StackManager.activePlayer().zoneManager.triggeredBuffer.disHighLightAll();
51  		if (EventManager.parsinfBeforeEnd() || EventManager.parsingEndPhaseEvent) {
52  			/*
53  			 * we are parsing the BEFORE_PHASE_... or END_PHASE_... event : no player
54  			 * gets priority.
55  			 */
56  			/* if (EventManager.isEndOfPhaseEventBroken()) { */
57  			if (StackManager.isEmpty()) {
58  				// the stack is empty, so neither new spell, neither waiting triggered
59  				EventManager.gotoNextPhase();
60  			} else {
61  				// we start resolving the stack
62  				StackManager.resolveStack();
63  			}
64  			// }
65  		} else {
66  			// we set the active action as the WaitActivatedChoice one
67  			StackManager.actionManager.currentAction = WaitActivatedChoice
68  					.getInstance();
69  			// The active player gets priority
70  			if (WaitActivatedChoice.getInstance().play(null, null)) {
71  				StackManager.resolveStack();
72  			}
73  		}
74  	}
75  
76  	public boolean play(ContextEventListener context, Ability ability) {
77  		throw new InternalError("should not be called");
78  	}
79  
80  	public boolean clickOn(TriggeredCard triggeredCard) {
81  		if (StackManager.idHandedPlayer == 0) {
82  			TriggeredBuffer cont = StackManager.PLAYERS[0].zoneManager.triggeredBuffer;
83  			for (int i = cont.getCardCount(); i-- > 0;) {
84  				// this card has to be found in the choice of handed player
85  				if (cont.getTriggeredAbility(i) == triggeredCard) {
86  					// card has been found, we can play it
87  					return true;
88  				}
89  			}
90  			/*
91  			 * TriggeredCard has not been found into the Triggered Buffer zone, we
92  			 * suppose this triggeredcard has been played automatically from the
93  			 * abstractZone of Triggered Buffer. So, we add to the stack this hidden
94  			 * triggered ability
95  			 */
96  			if (!triggeredCard.triggeredAbility.isHidden()) {
97  				// force the resolving stack process
98  				throw new InternalError("Mysterious unhidden triggered ability "
99  						+ triggeredCard.getCardName()
100 						+ ", card="
101 						+ triggeredCard.triggeredAbility.getCard()
102 						+ (triggeredCard.triggeredAbility.getCard() != null ? "@"
103 								+ Integer.toHexString(triggeredCard.triggeredAbility.getCard()
104 										.hashCode()) : ""));
105 			}
106 			throw new InternalError("clicked on abstract triggered ability");
107 		}
108 		return false;
109 	}
110 
111 	public boolean succeedClickOn(TriggeredCard triggeredCard) {
112 		try {
113 			StackManager.PLAYERS[triggeredCard.triggeredAbility.getCard()
114 					.getController().idPlayer].zoneManager.triggeredBuffer
115 					.disHighLightAll();
116 		} catch (Exception e) {
117 			Log.debug("error : " + e);
118 		}
119 		return triggeredCard.newSpell();
120 	}
121 
122 	public boolean manualSkip() {
123 		/*
124 		 * we stack automatically the first waiting triggered buffer choice to the
125 		 * stack in the reverse order they have been added into the triggered buffer
126 		 * zone. Note: the abstract triggered buffer zone is empty when we are here
127 		 * since these hidden abilities have been already played automatically
128 		 * before the played has received the hand.
129 		 */
130 		StackManager.actionManager
131 				.succeedClickOn((TriggeredCard) StackManager.PLAYERS[StackManager.oldIdHandedPlayer].zoneManager.triggeredBuffer
132 						.getComponent(0));
133 		return false;
134 	}
135 
136 	@Override
137 	public Actiontype getIdAction() {
138 		return Actiontype.WAIT_TRIGGERED_BUFFER_CHOICE;
139 	}
140 
141 	@Override
142 	public String toString(Ability ability) {
143 		return "wait triggered ability buffer choice";
144 	}
145 
146 	/***
147 	 * Return the unique instance of this class.
148 	 * 
149 	 * @return the unique instance of this class.
150 	 */
151 	public static WaitTriggeredBufferChoice getInstance() {
152 		if (instance == null)
153 			instance = new WaitTriggeredBufferChoice();
154 		return instance;
155 	}
156 
157 	/***
158 	 * The unique instance of this class
159 	 */
160 	private static WaitTriggeredBufferChoice instance;
161 
162 }