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   * MEventDuringPhase.java
20   * Created on 27 janv. 2004
21   */
22  package net.sf.magicproject.event.phase;
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.card.MCard;
29  import net.sf.magicproject.event.Event;
30  import net.sf.magicproject.event.MEventListener;
31  import net.sf.magicproject.event.TriggeredEvent;
32  import net.sf.magicproject.event.context.MContextCardCardIntInt;
33  import net.sf.magicproject.expression.Expression;
34  import net.sf.magicproject.expression.ExpressionFactory;
35  import net.sf.magicproject.stack.StackManager;
36  import net.sf.magicproject.test.Test;
37  import net.sf.magicproject.token.IdConst;
38  
39  /***
40   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
41   * @since 0.54
42   */
43  abstract class PhaseEvent extends TriggeredEvent {
44  
45  	/***
46  	 * Create an instance of MEventPhase by reading a file Offset's file must
47  	 * pointing on the first byte of this event <br>
48  	 * <ul>
49  	 * Structure of InputStream : Data[size]
50  	 * <li>idZone [1]</li>
51  	 * <li>test [...]</li>
52  	 * <li>idPhase(phase identifiant) : Expression [2]</li>
53  	 * </ul>
54  	 * 
55  	 * @param inputFile
56  	 *          is the file containing this event
57  	 * @param card
58  	 *          is the card owning this event
59  	 * @throws IOException
60  	 *           if error occurred during the reading process from the specified
61  	 *           input stream
62  	 */
63  	protected PhaseEvent(InputStream inputFile, MCard card) throws IOException {
64  		super(inputFile, card);
65  		phaseFilter = PhaseFilter.valueOf(inputFile);
66  		idPhase = ExpressionFactory.readNextExpression(inputFile);
67  	}
68  
69  	/***
70  	 * Creates a new instance of MEventPhase specifying all attributes of this
71  	 * class. All parameters are copied, not cloned. So this new object shares the
72  	 * card and the specified codes
73  	 * 
74  	 * @param idZone
75  	 *          the place constraint to activate this event
76  	 * @param test
77  	 *          the test of this event
78  	 * @param card
79  	 *          is the card owning this card
80  	 * @param idPhase
81  	 *          represents the phase we are looking for
82  	 * @param phaseFilter
83  	 *          the filter.
84  	 */
85  	protected PhaseEvent(int idZone, Test test, MCard card, Expression idPhase,
86  			PhaseFilter phaseFilter) {
87  		super(idZone, test, card);
88  		this.idPhase = idPhase;
89  		this.phaseFilter = phaseFilter;
90  	}
91  
92  	/***
93  	 * Tell if the current event matches with this event. If there is an
94  	 * additional code to check, it'would be checked if the main event matches
95  	 * with the main event
96  	 * 
97  	 * @param ability
98  	 *          is the ability owning this test. The card component of this
99  	 *          ability should correspond to the card owning this test too.
100 	 * @return true if the current event match with this event
101 	 */
102 	protected boolean isMatching(Ability ability) {
103 		final int value = idPhase.getValue(ability, null, null);
104 		return (value == IdConst.NO_CARE || phaseFilter.getPhaseFilter() == value)
105 				&& test(ability, StackManager.currentPlayer());
106 	}
107 
108 	/***
109 	 * Dispatch this event to all active event listeners able to understand this
110 	 * event. The listening events able to understand this event are <code>this
111 	 * </code>
112 	 * and other multiple event listeners. For each event listeners having
113 	 * responded they have been activated, the corresponding ability is added to
114 	 * the triggered buffer zone of player owning this ability. <br>
115 	 * Note that the specified currentIdPhase corresponds to the phase identifier
116 	 * and not to te phase index. So if a same phase is present two times in the
117 	 * turn structure, and we are looking for it's end, this event would be
118 	 * activated twice.
119 	 * 
120 	 * @param event
121 	 *          the phase idEvent
122 	 */
123 	protected static final void dispatchEvent(Event event) {
124 		for (Ability ability : TRIGGRED_ABILITIES.get(event)) {
125 			if (((PhaseEvent) ability.eventComing()).isMatching(ability)
126 					&& ability.isMatching()) {
127 				/***
128 				 * this ability matches with the current event, so we add it to the
129 				 * triggered buffer zone
130 				 */
131 				ability.triggerIt(new MContextCardCardIntInt(StackManager
132 						.currentPlayer(), ((PhaseEvent) ability.eventComing()).phaseFilter
133 						.getPhaseFilter()));
134 			}
135 		}
136 	}
137 
138 	/***
139 	 * Return the idEvent of this event
140 	 * 
141 	 * @return the idEvent of this event
142 	 */
143 	@Override
144 	public abstract Event getIdEvent();
145 
146 	/***
147 	 * Return a copy of this with the specified owner
148 	 * 
149 	 * @param card
150 	 *          is the card of the ability of this event
151 	 * @return copy of this event
152 	 */
153 	@Override
154 	public abstract MEventListener clone(MCard card);
155 
156 	/***
157 	 * Represents the phase we are looking for
158 	 */
159 	protected Expression idPhase;
160 
161 	/***
162 	 * The phase filter to use.
163 	 */
164 	protected PhaseFilter phaseFilter;
165 
166 }