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.event.context;
23
24 import net.sf.magicproject.clickable.targetable.Targetable;
25 import net.sf.magicproject.clickable.targetable.card.MCard;
26 import net.sf.magicproject.clickable.targetable.player.Player;
27 import net.sf.magicproject.stack.StackManager;
28
29 /***
30 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
31 * @since 0.54
32 * @since 0.82 timeStamp supported
33 */
34 public class MContextMtargetable implements ContextEventListener {
35
36 /***
37 * Creates a new instance of MContextMtargetable <br>
38 *
39 * @param targetToSave
40 * is the object to store
41 */
42 public MContextMtargetable(Targetable targetToSave) {
43 this(targetToSave, -1);
44 }
45
46 /***
47 * Creates a new instance of MContextMtargetable <br>
48 *
49 * @param inTargetToSave
50 * is the object to store
51 * @param maxTimeStamp
52 * is the maximum timestamp allowed for destination card during the
53 * resolution.
54 */
55 public MContextMtargetable(Targetable inTargetToSave, int maxTimeStamp) {
56 this.targetToSave = inTargetToSave.getOriginalTargetable();
57 if (maxTimeStamp == -1 && this.targetToSave != null) {
58 this.timeStamp = this.targetToSave.getTimestamp();
59 } else {
60 this.timeStamp = maxTimeStamp;
61 }
62 this.targetToSave.addTimestampReference();
63 if (StackManager.currentAbility != null) {
64 this.eventSourceCard = StackManager.currentAbility.getCard();
65 }
66 }
67
68 /***
69 * Return the MTargetable object of this context considering it's timstamp.
70 * The returned object is the same as it was when this context has been
71 * created.
72 *
73 * @return the MTargetable object of this context as it was when this context
74 * has been created.
75 */
76 public final Targetable getTargetable() {
77 if (timeStamp > targetToSave.getTimestamp()) {
78 return targetToSave.getLastKnownTargetable(targetToSave.getTimestamp());
79 }
80 return targetToSave.getLastKnownTargetable(timeStamp);
81 }
82
83 /***
84 * Return the MTargetable cast to MCard object of this context considering
85 * it's timstamp. The returned object is the same as it was when this context
86 * has been created.
87 *
88 * @return the MTargetable cast to MCard object of this context as it was when
89 * this context has been created.
90 * @see #getTargetable()
91 */
92 public final MCard getCard() {
93 return (MCard) getTargetable();
94 }
95
96 public void removeTimestamp() {
97 if (timeStamp > targetToSave.getTimestamp()) {
98 targetToSave.decrementTimestampReference(targetToSave.getTimestamp());
99 } else {
100 targetToSave.decrementTimestampReference(timeStamp);
101 }
102 }
103
104 public boolean checkTimeStamp(MCard card) {
105 if (targetToSave == card) {
106 return card.getTimestamp() <= timeStamp;
107 }
108 return true;
109 }
110
111 /***
112 * Return the MTargetable object cast into MPlayer instance.
113 *
114 * @return the MTargetable object cast into MPlayer instance.
115 */
116 public final Player getPlayer() {
117 return (Player) targetToSave;
118 }
119
120 /***
121 * Return the MTargetable object of this context without considering it's
122 * timstamp.
123 *
124 * @return the MTargetable object of this context.
125 */
126 public final Targetable getOriginalTargetable() {
127 return targetToSave;
128 }
129
130 /***
131 * Return the MTargetable cast to MCard object of this context without
132 * considering it's timstamp.
133 *
134 * @return the MTargetable cast to MCard object of this context without
135 * considering it's timstamp.
136 */
137 public final MCard getOriginalCard() {
138 return (MCard) getOriginalTargetable();
139 }
140
141 @Override
142 public boolean equals(Object context) {
143 return super.equals(context)
144 || (context instanceof MContextMtargetable
145 && ((MContextMtargetable) context).targetToSave == targetToSave && ((MContextMtargetable) context).timeStamp == timeStamp);
146 }
147
148 @Override
149 public int hashCode() {
150 return targetToSave.hashCode();
151 }
152
153 public final MCard getEventSource() {
154 return eventSourceCard;
155 }
156
157 @Override
158 public String toString() {
159 return new StringBuilder("{source=" + eventSourceCard).append(",context=")
160 .append(targetToSave).append("}").toString();
161 }
162
163 public int getZoneContext() {
164
165 return 0;
166 }
167
168 /***
169 * The stored MTargetable object.
170 */
171 protected Targetable targetToSave;
172
173 /***
174 * This timestamp corresponds to the amount of card movements.
175 */
176 private int timeStamp;
177
178 /***
179 * the card source of the event attached to this context.
180 */
181 protected MCard eventSourceCard;
182
183 }