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  package net.sf.magicproject.action.context;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import net.sf.magicproject.clickable.mana.ManaPool;
25  import net.sf.magicproject.stack.StackManager;
26  import net.sf.magicproject.test.Test;
27  import net.sf.magicproject.token.IdCardColors;
28  import net.sf.magicproject.tools.PairIntObject;
29  
30  /***
31   * The mana cost context : initial mana cost, mana paid, required mana
32   * 
33   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
34   * @since 0.90
35   */
36  public class ManaCost implements ActionContext {
37  
38  	/***
39  	 * Create a new instance of this class.
40  	 */
41  	public ManaCost() {
42  		manaCost = new int[IdCardColors.CARD_COLOR_NAMES.length];
43  		requiredMana = new int[manaCost.length];
44  		manaPaid = new int[manaCost.length];
45  	}
46  
47  	/***
48  	 * Is the non required mana.
49  	 * 
50  	 * @return true if no mana is required.
51  	 */
52  	public boolean isNullRequired() {
53  		for (int mana : requiredMana) {
54  			if (mana != 0) {
55  				return false;
56  			}
57  		}
58  		return true;
59  	}
60  
61  	/***
62  	 * The initial mana cost.
63  	 */
64  	public final int[] manaCost;
65  
66  	/***
67  	 * The required mana.
68  	 */
69  	public final int[] requiredMana;
70  
71  	/***
72  	 * The mana paid.
73  	 */
74  	public final int[] manaPaid;
75  
76  	/***
77  	 * The restriction used to paid the mana.
78  	 */
79  	public List<PairIntObject<Test>>[] restrictions = null;
80  
81  	/***
82  	 * Add a restriction
83  	 * 
84  	 * @param color
85  	 *          the restricted color
86  	 * @param test
87  	 *          restriction test to save
88  	 * @param amount
89  	 *          amount of mana remove
90  	 */
91  	@SuppressWarnings("unchecked")
92  	public void addRestriction(int color, Test test, int amount) {
93  		if (restrictions == null) {
94  			restrictions = new List[manaCost.length];
95  			restrictions[color] = new ArrayList<PairIntObject<Test>>();
96  		} else if (restrictions[color] == null) {
97  			restrictions[color] = new ArrayList<PairIntObject<Test>>();
98  		} else {
99  			// memory optimization: is already exist this restriction
100 			for (PairIntObject<Test> pair : restrictions[color]) {
101 				if (pair.value == test) {
102 					// previous restriction found
103 					pair.key += amount;
104 					return;
105 				}
106 			}
107 		}
108 		restrictions[color].add(new PairIntObject<Test>(amount, test));
109 	}
110 
111 	/***
112 	 * Pay a required mana. Given amount of mana is remove from the given mana
113 	 * pool.
114 	 * 
115 	 * @param paidColor
116 	 *          the required mana
117 	 * @param withColor
118 	 *          the used mana
119 	 * @param amount
120 	 *          the amount of mana to remove
121 	 * @param manas
122 	 *          the mana pool
123 	 */
124 	public void payMana(int paidColor, int withColor, int amount, ManaPool manas) {
125 		if (requiredMana[paidColor] < amount) {
126 			throw new InternalError("Cannot pay the required mana : paidColor="
127 					+ paidColor + ", withColor=" + withColor + ", amount=" + amount);
128 		}
129 		requiredMana[paidColor] -= amount;
130 		manaPaid[withColor] += amount;
131 		manas.manaButtons[withColor].removeMana(amount,
132 				StackManager.currentAbility, this);
133 	}
134 
135 	/***
136 	 * Restore mana paid to the given mana pool
137 	 * 
138 	 * @param manas
139 	 *          is the mana pool receiving the restored manas
140 	 */
141 	public void restoreMana(ManaPool manas) {
142 		for (int color = 6; color-- > 0;) {
143 			int paid = manaPaid[color];
144 			if (restrictions != null && restrictions[color] != null) {
145 				for (int i = restrictions[color].size(); i-- > 0;) {
146 					final PairIntObject<Test> restriction = restrictions[color].get(i);
147 					paid -= restriction.key;
148 					manas.addMana(color, restriction.key, restriction.value);
149 				}
150 			}
151 			if (paid > 0) {
152 				manas.addMana(color, paid, null);
153 			}
154 		}
155 	}
156 
157 	/***
158 	 * Add a mana cost.
159 	 * 
160 	 * @param manaCost
161 	 *          the mana cost to add.
162 	 */
163 	public void addManaCost(int[] manaCost) {
164 		for (int i = this.manaCost.length; i-- > 0;) {
165 			this.manaCost[i] += manaCost[i];
166 		}
167 	}
168 }