View Javadoc

1   /*
2    * Created on Sep 10, 2004 
3    * Original filename was RefreshAbility.java
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  package net.sf.magicproject.clickable.ability;
23  
24  import net.sf.magicproject.action.MAction;
25  import net.sf.magicproject.action.RefreshModifier;
26  import net.sf.magicproject.clickable.targetable.card.MCard;
27  import net.sf.magicproject.event.MEventListener;
28  import net.sf.magicproject.event.MovedCard;
29  import net.sf.magicproject.event.TriggeredEvent;
30  import net.sf.magicproject.event.context.ContextEventListener;
31  import net.sf.magicproject.event.context.MContextMtargetable;
32  import net.sf.magicproject.token.TrueFalseAuto;
33  
34  /***
35   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
36   */
37  public class RefreshAbility extends AbstractAbility {
38  
39  	/***
40  	 * Creates a new instance of RefreshAbility <br>
41  	 * 
42  	 * @param event
43  	 *          the event attached to this ability.
44  	 * @param refreshAction
45  	 * @param trigger
46  	 */
47  	public RefreshAbility(TriggeredEvent event, RefreshModifier refreshAction,
48  			MCard trigger) {
49  		super("refresh modifier", event, null, null, TrueFalseAuto.FALSE);
50  		actionList = new MAction[] {};
51  		effectList = new MAction[] { refreshAction };
52  		this.trigger = trigger;
53  		// override the resolver
54  		priority = Priority.hidden_high;
55  		// override the optimizer
56  		optimizer = Optimization.last;
57  	}
58  
59  	@Override
60  	public void optimizeRegisterToManager() {
61  		for (Ability ability : MEventListener.TRIGGRED_ABILITIES.get(eventComing
62  				.getIdEvent())) {
63  			if (equals(ability)) {
64  				/*
65  				 * this refresh modifier is already existing. Instead of registering
66  				 * another time this modifier, we append with 'or' operator the
67  				 * conditionnal test
68  				 */
69  				MEventListener newEvent = ability.eventComing.appendOr(eventComing);
70  				if (newEvent != null) {
71  					ability.eventComing = newEvent;
72  					return;
73  				}
74  			}
75  		}
76  		// This modifier has not yet been registered
77  		MEventListener.TRIGGRED_ABILITIES.get(eventComing.getIdEvent()).add(this);
78  	}
79  
80  	@Override
81  	public boolean equals(ContextEventListener thisContext, Ability ability,
82  			ContextEventListener context) {
83  		if (ability instanceof RefreshAbility) {
84  			RefreshAbility refreshAbility = (RefreshAbility) ability;
85  			if (refreshAbility.effectList[0] == effectList[0]) {
86  				if (refreshAbility.eventComing instanceof MovedCard) {
87  					return refreshAbility.eventComing instanceof MovedCard
88  							&& refreshAbility.eventComing.equals(eventComing)
89  							&& ((MContextMtargetable) thisContext).getTargetable() == ((MContextMtargetable) context)
90  									.getTargetable();
91  				}
92  				return true;
93  			}
94  		}
95  		return false;
96  	}
97  
98  	@Override
99  	public int hashCode() {
100 		if (eventComing != null && eventComing.card != null)
101 			return eventComing.card.hashCode();
102 		return super.hashCode();
103 	}
104 
105 	@Override
106 	public boolean triggerIt(ContextEventListener context) {
107 		super.triggerIt(context);
108 		trigger.getController().zoneManager.triggeredBuffer
109 				.addHidden(this, context);
110 		return true;
111 	}
112 
113 	@Override
114 	public String toString() {
115 		return getName() + ", on card " + eventComing.card + ", from modifier:"
116 				+ trigger;
117 	}
118 
119 	@Override
120 	public String getLog(ContextEventListener context) {
121 		return "refresh modifier of '" + getCard() + "' on " + eventComing.card;
122 	}
123 
124 	@Override
125 	public MCard getCard() {
126 		return trigger;
127 	}
128 
129 	@Override
130 	public String getAbilityTitle() {
131 		return "RefreshAbility" + (getName() != null ? ": " + getName() : "")
132 				+ super.getAbilityTitle();
133 	}
134 
135 	private MCard trigger;
136 
137 }