Commit 5ce68f2d authored by Michael Kamensky's avatar Michael Kamensky

Merge branch 'setPT' into 'master'

Rework SetPT to use Map, and CharacteristicDefining

See merge request core-developers/forge!670
parents edf5c132 6c0ca4ff
......@@ -183,7 +183,8 @@ public class Card extends GameEntity implements Comparable<Card> {
private long timestamp = -1; // permanents on the battlefield
// stack of set power/toughness
private List<CardPowerToughness> newPT = Lists.newArrayList();
private Map<Long, Pair<Integer,Integer>> newPT = Maps.newTreeMap();
private Map<Long, Pair<Integer,Integer>> newPTCharacterDefining = Maps.newTreeMap();
private int baseLoyalty = 0;
private String basePowerString = null;
private String baseToughnessString = null;
......@@ -2949,14 +2950,14 @@ public class Card extends GameEntity implements Comparable<Card> {
}
public final int getSetPower() {
if (newPT.isEmpty()) {
if (newPTCharacterDefining.isEmpty() && newPT.isEmpty()) {
return Integer.MAX_VALUE;
}
return getLatestPT().getLeft();
}
public final int getSetToughness() {
if (newPT.isEmpty()) {
if (newPTCharacterDefining.isEmpty() && newPT.isEmpty()) {
return Integer.MAX_VALUE;
}
return getLatestPT().getRight();
......@@ -2972,24 +2973,21 @@ public class Card extends GameEntity implements Comparable<Card> {
*/
private synchronized Pair<Integer, Integer> getLatestPT() {
// Find latest set power
// TODO Java 1.8 use comparingLong
Collections.sort(newPT, new Comparator<CardPowerToughness>() {
@Override
public int compare(CardPowerToughness o1, CardPowerToughness o2) {
return Long.compare(o1.getTimestamp(),o2.getTimestamp());
}
});
Integer power = null,
toughness = null;
Integer power = null, toughness = null;
int size = newPT.size();
for(int i = size - 1; i >= 0; i--) {
CardPowerToughness pt = newPT.get(i);
if (power == null && pt.getPower() != null)
power = pt.getPower();
if (toughness == null && pt.getToughness() != null)
toughness = pt.getToughness();
// apply CDA first
for (Pair<Integer,Integer> pt : newPTCharacterDefining.values()) {
if (pt.getLeft() != null)
power = pt.getLeft();
if (pt.getRight() != null)
toughness = pt.getRight();
}
// now real PT
for (Pair<Integer,Integer> pt : newPT.values()) {
if (pt.getLeft() != null)
power = pt.getLeft();
if (pt.getRight() != null)
toughness = pt.getRight();
}
if (power == null)
......@@ -3002,20 +3000,28 @@ public class Card extends GameEntity implements Comparable<Card> {
}
public final void addNewPT(final Integer power, final Integer toughness, final long timestamp) {
newPT.add(new CardPowerToughness(power, toughness, timestamp));
addNewPT(power, toughness, timestamp, false);
}
public final void addNewPT(final Integer power, final Integer toughness, final long timestamp, final boolean cda) {
if (cda) {
newPTCharacterDefining.put(timestamp, Pair.of(power, toughness));
} else {
newPT.put(timestamp, Pair.of(power, toughness));
}
currentState.getView().updatePower(this);
currentState.getView().updateToughness(this);
}
public final void removeNewPT(final long timestamp) {
for (int i = 0; i < newPT.size(); i++) {
final CardPowerToughness cardPT = newPT.get(i);
if (cardPT.getTimestamp() == timestamp) {
if (newPT.remove(cardPT)) {
currentState.getView().updatePower(this);
currentState.getView().updateToughness(this);
}
}
boolean removed = false;
removed |= newPT.remove(timestamp) != null;
removed |= newPTCharacterDefining.remove(timestamp) != null;
if (removed) {
currentState.getView().updatePower(this);
currentState.getView().updateToughness(this);
}
}
......
/*
* Forge: Play Magic: the Gathering.
* Copyright (C) 2011 Forge Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package forge.game.card;
/**
* <p>
* CardPowerToughness class.
* </p>
*
* @author Forge
* @version $Id$
*/
public class CardPowerToughness {
private final Integer power;
private final Integer toughness;
private long timeStamp = 0;
/**
* <p>
* getTimestamp.
* </p>
*
* @return a long.
*/
public final long getTimestamp() {
return this.timeStamp;
}
/**
* <p>
* Constructor for Card_PT.
* </p>
*
* @param newPower
* a int.
* @param newToughness
* a int.
* @param stamp
* a long.
*/
CardPowerToughness(final Integer newPower, final Integer newToughness, final long stamp) {
this.power = newPower;
this.toughness = newToughness;
this.timeStamp = stamp;
}
/**
*
* Get Power.
*
* @return int
*/
public final Integer getPower() {
return this.power;
}
/**
*
* Get Toughness.
*
* @return int
*/
public final Integer getToughness() {
return this.toughness;
}
/**
* <p>
* equals.
* </p>
*
* @param newPower
* a int.
* @param newToughness
* a int.
* @param stamp
* a long.
* @return a boolean.
*/
public final boolean equals(final Integer newPower, final Integer newToughness, final long stamp) {
return (this.timeStamp == stamp) && (this.power == newPower) && (this.toughness == newToughness);
}
}
......@@ -590,7 +590,8 @@ public final class StaticAbilityContinuous {
if (setT.startsWith("AffectedX")) {
setToughness = CardFactoryUtil.xCount(affectedCard, AbilityUtils.getSVar(stAb, setT));
}
affectedCard.addNewPT(setPower, setToughness, hostCard.getTimestamp());
affectedCard.addNewPT(setPower, setToughness,
hostCard.getTimestamp(), stAb.hasParam("CharacteristicDefining"));
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment