1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package net.sf.magicproject.action;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import net.sf.magicproject.action.handler.StandardAction;
28 import net.sf.magicproject.clickable.ability.Ability;
29 import net.sf.magicproject.clickable.ability.AbilityFactory;
30 import net.sf.magicproject.clickable.ability.RemoveModifier;
31 import net.sf.magicproject.clickable.targetable.Targetable;
32 import net.sf.magicproject.clickable.targetable.card.MCard;
33 import net.sf.magicproject.clickable.targetable.card.SystemCard;
34 import net.sf.magicproject.clickable.targetable.player.Player;
35 import net.sf.magicproject.event.EventFactory;
36 import net.sf.magicproject.event.MEventListener;
37 import net.sf.magicproject.event.TriggeredEvent;
38 import net.sf.magicproject.event.context.ContextEventListener;
39 import net.sf.magicproject.stack.StackManager;
40
41 /***
42 * Create a new ability and add it to the current target list. This ability is
43 * linked to many optional 'until' events.
44 *
45 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
46 * @since 0.81
47 */
48 class AddAbility extends UserAction implements StandardAction {
49
50 /***
51 * The ability to add
52 */
53 private final Ability abilityToAdd;
54
55 /***
56 * When this event is generated this modifier is removed from the attached
57 * object. If this event is equal to null, this modifier can be removed only
58 * by the attached component.
59 */
60 private final List<MEventListener> until;
61
62 /***
63 * Create an instance of AddAbility by reading a file Offset's file must
64 * pointing on the first byte of this action
65 * <ul>
66 * Structure of InputStream : Data[size]
67 * <li>idAction [1]</li>
68 * <li>ability to Add [Ability]</li>
69 * <li>exists until this triggered event [Event[]]</li>
70 * </ul>
71 *
72 * @param inputFile
73 * file containing this action
74 * @throws IOException
75 * If some other I/O error occurs
76 */
77 AddAbility(InputStream inputFile) throws IOException {
78 super(inputFile);
79
80 abilityToAdd = AbilityFactory.readAbility(inputFile, SystemCard.instance);
81
82
83 int count = inputFile.read();
84 until = new ArrayList<MEventListener>(count);
85 while (count-- > 0) {
86 until.add(EventFactory.readNextEvent(inputFile, SystemCard.instance));
87 }
88
89 }
90
91 @Override
92 public final Actiontype getIdAction() {
93 return Actiontype.ADD_ABILITY;
94 }
95
96 public boolean play(ContextEventListener context, Ability ability) {
97
98 for (int i = StackManager.getInstance().getTargetedList().size(); i-- > 0;) {
99 Targetable to = StackManager.getInstance().getTargetedList().get(i);
100 Ability clonedAbility = null;
101 if (to.isCard()) {
102 clonedAbility = abilityToAdd.clone((MCard) to);
103 } else {
104
105 clonedAbility = abilityToAdd.clone(((Player) to).playerCard);
106 }
107
108
109 for (int j = until.size(); j-- > 0;) {
110 clonedAbility.addLinkedAbility(new RemoveModifier(
111 (TriggeredEvent) until.get(j), clonedAbility));
112 }
113
114
115 clonedAbility.registerToManager();
116 to.cachedAbilities.add(clonedAbility);
117 }
118 return true;
119 }
120
121 @Override
122 public String toString(Ability ability) {
123 return "Add ability";
124 }
125
126 }