1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
54 priority = Priority.hidden_high;
55
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
66
67
68
69 MEventListener newEvent = ability.eventComing.appendOr(eventComing);
70 if (newEvent != null) {
71 ability.eventComing = newEvent;
72 return;
73 }
74 }
75 }
76
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 }