ci changes from trunk

This commit is contained in:
Maxmtg
2013-03-25 09:42:54 +00:00
32 changed files with 222 additions and 344 deletions

View File

@@ -18,8 +18,10 @@
package forge;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.File;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import javax.imageio.ImageIO;
@@ -70,22 +72,30 @@ public class ImageCache {
public static final String PRECON_PREFIX = "p:";
public static final String TOURNAMENTPACK_PREFIX = "o:";
static private final LoadingCache<String, BufferedImage> CACHE = CacheBuilder.newBuilder().softValues().build(new ImageLoader());
private static final BufferedImage emptyImage = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB);
private static BufferedImage defaultImage = emptyImage;
static {
private static final Set<String> _missingIconKeys = new HashSet<String>();
private static final LoadingCache<String, BufferedImage> _CACHE = CacheBuilder.newBuilder().softValues().build(new ImageLoader());
private static final BufferedImage _defaultImage;
static {
BufferedImage defImage = null;
try {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
InputStream isNoCardJpg = cl.getResourceAsStream("no_card.jpg");
defaultImage = ImageIO.read(isNoCardJpg);
} catch (IOException e) {
// TODO Auto-generated catch block ignores the exception, but sends it to System.err and probably forge.log.
e.printStackTrace();
defImage = ImageIO.read(isNoCardJpg);
} catch (Exception e) {
// resource not found; perhaps we're running straight from source
try {
defImage = ImageIO.read(new File("src/main/resources/no_card.jpg"));
} catch (Exception ex) {
System.err.println("could not load default card image");
}
} finally {
_defaultImage = (null == defImage) ? new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB) : defImage;
}
}
public static void clear() {
CACHE.invalidateAll();
_CACHE.invalidateAll();
_missingIconKeys.clear();
}
/**
@@ -99,7 +109,7 @@ public class ImageCache {
} else {
key = card.getImageKey();
}
return scaleImage(key, width, height);
return scaleImage(key, width, height, true);
}
/**
@@ -107,7 +117,7 @@ public class ImageCache {
* and cannot be loaded from disk. pass -1 for width and/or height to avoid resizing in that dimension.
*/
public static BufferedImage getImage(InventoryItem ii, int width, int height) {
return scaleImage(getImageKey(ii, false), width, height);
return scaleImage(getImageKey(ii, false), width, height, true);
}
/**
@@ -115,40 +125,42 @@ public class ImageCache {
* in the cache and cannot be loaded from disk.
*/
public static ImageIcon getIcon(IHasIcon ihi) {
BufferedImage i = scaleImage(ihi.getIconImageKey(), -1, -1);
if (null == i) {
String imageKey = ihi.getIconImageKey();
final BufferedImage i;
if (_missingIconKeys.contains(imageKey) ||
null == (i = scaleImage(ihi.getIconImageKey(), -1, -1, false))) {
_missingIconKeys.add(imageKey);
return FSkin.getIcon(FSkin.InterfaceIcons.ICO_UNKNOWN);
}
return new ImageIcon(i);
}
private static BufferedImage scaleImage(String key, final int width, final int height) {
private static BufferedImage scaleImage(String key, final int width, final int height, boolean useDefaultImage) {
if (StringUtils.isEmpty(key) || (3 > width && -1 != width) || (3 > height && -1 != height)) {
// picture too small or key not defined; return a blank
return null;
}
StringBuilder rsKey = new StringBuilder(key);
rsKey.append("#").append(width).append('x').append(height);
String resizedKey = rsKey.toString();
String resizedKey = String.format("%s#%dx%d", key, width, height);
final BufferedImage cached = CACHE.getIfPresent(resizedKey);
final BufferedImage cached = _CACHE.getIfPresent(resizedKey);
if (null != cached) {
//System.out.println("found cached image: " + resizedKey);
return cached;
}
boolean mayEnlarge = Singletons.getModel().getPreferences().getPrefBoolean(FPref.UI_SCALE_LARGER);
BufferedImage original = getImage(key);
if (null == original) {
original = defaultImage;
CACHE.put(key, defaultImage); // This instructs cache to give up finding a picture if it was not found once
}
if (original == emptyImage) { // the found image is a placeholder for missing picture?
return null;
if (!useDefaultImage) {
return null;
}
// henceforth use a default picture for this key if image not found
original = _defaultImage;
_CACHE.put(key, _defaultImage);
}
boolean mayEnlarge = Singletons.getModel().getPreferences().getPrefBoolean(FPref.UI_SCALE_LARGER);
double scale = Math.min(
-1 == width ? 1 : (double)width / original.getWidth(),
-1 == height? 1 : (double)height / original.getHeight());
@@ -162,12 +174,22 @@ public class ImageCache {
} else {
int destWidth = (int)(original.getWidth() * scale);
int destHeight = (int)(original.getHeight() * scale);
ResampleOp resampler = new ResampleOp(destWidth, destHeight);
result = resampler.filter(original, null);
CACHE.put(resizedKey, result);
// if this scale has been used before, get the cached version instead of rescaling
String effectiveResizedKey = String.format("%s#%dx%d", key, destWidth, destHeight);
result = _CACHE.getIfPresent(effectiveResizedKey);
if (null == result) {
ResampleOp resampler = new ResampleOp(destWidth, destHeight);
result = resampler.filter(original, null);
//System.out.println("caching resized image: " + effectiveResizedKey);
_CACHE.put(effectiveResizedKey, result);
//} else {
// System.out.println("retrieved resized image: " + effectiveResizedKey);
}
}
//System.out.println("caching image: " + resizedKey);
_CACHE.put(resizedKey, result);
return result;
}
@@ -176,7 +198,7 @@ public class ImageCache {
*/
private static BufferedImage getImage(final String key) {
try {
return ImageCache.CACHE.get(key);
return ImageCache._CACHE.get(key);
} catch (final ExecutionException ex) {
if (ex.getCause() instanceof NullPointerException) {
return null;

View File

@@ -148,6 +148,9 @@ public class RegenerateAi extends SpellAbilityAi {
}
}
}
if (tgt.getTargets().isEmpty()) {
return false;
}
}
return chance;

View File

@@ -27,11 +27,14 @@ import forge.CardCharacteristicName;
import forge.CardColor;
import forge.CardUtil;
import forge.Color;
import forge.Command;
import forge.CounterType;
import forge.ImageCache;
import forge.card.CardCharacteristics;
import forge.card.CardRules;
import forge.card.CardSplitType;
import forge.card.ICardFace;
import forge.card.ability.AbilityFactory;
import forge.card.cost.Cost;
import forge.card.mana.ManaCost;
import forge.card.replacement.ReplacementHandler;
@@ -40,6 +43,7 @@ import forge.card.spellability.AbilitySub;
import forge.card.spellability.SpellAbility;
import forge.card.spellability.SpellPermanent;
import forge.card.spellability.Target;
import forge.card.trigger.Trigger;
import forge.card.trigger.TriggerHandler;
import forge.game.player.Player;
import forge.item.CardDb;
@@ -296,20 +300,56 @@ public class CardFactory {
if (card.isCreature()) {
CardFactoryCreatures.buildCard(card, cardName);
} else if (card.isPlaneswalker()) {
CardFactoryPlaneswalkers.buildCard(card);
buildPlaneswalkerAbilities(card);
} else if (card.isLand()) {
CardFactoryLands.buildCard(card, cardName);
} else if (card.isSorcery()) {
CardFactorySorceries.buildCard(card, cardName);
} else if (card.isArtifact()) {
CardFactoryArtifacts.buildCard(card, cardName);
} else if (card.isType("Plane")) {
buildPlaneAbilities(card);
}
CardFactoryUtil.setupKeywordedAbilities(card);
} // getCard2
private static void buildPlaneAbilities(Card card) {
StringBuilder triggerSB = new StringBuilder();
triggerSB.append("Mode$ PlanarDice | Result$ Planeswalk | TriggerZones$ Command | Execute$ RolledWalk | ");
triggerSB.append("Secondary$ True | TriggerDescription$ Whenever you roll Planeswalk, put this card on the ");
triggerSB.append("bottom of its owner's planar deck face down, then move the top card of your planar deck off ");
triggerSB.append("that planar deck and turn it face up");
StringBuilder saSB = new StringBuilder();
saSB.append("AB$ RollPlanarDice | Cost$ X | SorcerySpeed$ True | AnyPlayer$ True | ActivationZone$ Command | ");
saSB.append("SpellDescription$ Roll the planar dice. X is equal to the amount of times the planar die has been rolled this turn.");
card.setSVar("RolledWalk", "DB$ Planeswalk | Cost$ 0");
Trigger planesWalkTrigger = TriggerHandler.parseTrigger(triggerSB.toString(), card, true);
card.addTrigger(planesWalkTrigger);
card.setSVar("X", "Count$RolledThisTurn");
SpellAbility planarRoll = AbilityFactory.getAbility(saSB.toString(), card);
card.addSpellAbility(planarRoll);
}
private static void buildPlaneswalkerAbilities(Card card) {
if (card.getBaseLoyalty() > 0) {
Command cmd = CardFactoryUtil.entersBattleFieldWithCounters(card, CounterType.LOYALTY, card.getBaseLoyalty());
card.addComesIntoPlayCommand(cmd);
}
//Planeswalker damage redirection
card.addReplacementEffect(ReplacementHandler.parseReplacement("Event$ DamageDone | ActiveZones$ Battlefield | IsCombat$ False | ValidSource$ Card.YouDontCtrl"
+ " | ValidTarget$ You | Optional$ True | OptionalDecider$ Opponent | ReplaceWith$ DamagePW | Secondary$ True"
+ " | AICheckSVar$ DamagePWAI | AISVarCompare$ GT4 | Description$ Redirect damage to " + card.toString(), card));
card.setSVar("DamagePW", "AB$DealDamage | Cost$ 0 | Defined$ Self | NumDmg$ DamagePWX | DamageSource$ ReplacedSource | References$ DamagePWX,DamagePWAI");
card.setSVar("DamagePWX", "ReplaceCount$DamageAmount");
card.setSVar("DamagePWAI", "ReplaceCount$DamageAmount/NMinus.DamagePWY");
card.setSVar("DamagePWY", "Count$YourLifeTotal");
}
private static Card readCard(final CardRules rules) {
final Card card = new Card();

View File

@@ -1,61 +0,0 @@
/*
* 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.card.cardfactory;
import forge.Card;
import forge.Command;
import forge.CounterType;
import forge.card.replacement.ReplacementHandler;
/**
* <p>
* CardFactory_Planeswalkers class.
* </p>
*
* @author Forge
* @version $Id$
*/
public class CardFactoryPlaneswalkers {
/**
* <p>
* getCard.
* </p>
*
* @param card
* a {@link forge.Card} object.
* @return a {@link forge.Card} object.
*/
public static void buildCard(final Card card) {
// All Planeswalkers set their loyality in the beginning
if (card.getBaseLoyalty() > 0) {
Command cmd = CardFactoryUtil.entersBattleFieldWithCounters(card, CounterType.LOYALTY, card.getBaseLoyalty());
card.addComesIntoPlayCommand(cmd);
}
//Planeswalker damage redirection
card.addReplacementEffect(ReplacementHandler.parseReplacement("Event$ DamageDone | ActiveZones$ Battlefield | IsCombat$ False | ValidSource$ Card.YouDontCtrl"
+ " | ValidTarget$ You | Optional$ True | OptionalDecider$ Opponent | ReplaceWith$ DamagePW | Secondary$ True"
+ " | AICheckSVar$ DamagePWAI | AISVarCompare$ GT4 | Description$ Redirect damage to " + card.toString(), card));
card.setSVar("DamagePW", "AB$DealDamage | Cost$ 0 | Defined$ Self | NumDmg$ DamagePWX | DamageSource$ ReplacedSource | References$ DamagePWX,DamagePWAI");
card.setSVar("DamagePWX", "ReplaceCount$DamageAmount");
card.setSVar("DamagePWAI", "ReplaceCount$DamageAmount/NMinus.DamagePWY");
card.setSVar("DamagePWY", "Count$YourLifeTotal");
}
} // end class CardFactoryPlaneswalkers

View File

@@ -40,9 +40,22 @@ import forge.game.zone.ZoneType;
public class Target {
// Target has two things happening:
// Targeting restrictions (Creature, Min/Maxm etc) which are true for this
// whole Target
// Target Choices (which is specific for the StackInstance)
private Card srcCard;
// What this Object is restricted to targeting
private boolean tgtValid = false;
private String[] validTgts;
private String vtSelection = "";
private List<ZoneType> tgtZone = Arrays.asList(ZoneType.Battlefield);
//SpellAbility Restrictions
// Comma-separated <Spell,Activated,Triggered>
private String targetSpellAbilityType = null;
// The target SA of this SA must be targeting a Valid X
private String saValidTargeting = null;
// Additional restrictions that may not fit into Valid
private boolean uniqueTargets = false;
private boolean singleZone = false;
private boolean differentZone = false;
@@ -50,11 +63,108 @@ public class Target {
private boolean sameController = false;
private boolean withoutSameCreatureType = false;
private String definedController = null;
// How many can be targeted?
private String minTargets;
private String maxTargets;
// What Choices are actually made for targeting
private TargetChoices choice = null;
// For "Divided" cards. Is this better in TargetChoices?
private boolean dividedAsYouChoose = false;
private HashMap<Object, Integer> dividedMap = new HashMap<Object, Integer>();
private int stillToDivide = 0;
// Not sure what's up with Mandatory? Why wouldn't targeting be mandatory?
private boolean bMandatory = false;
/**
* <p>
* Copy Constructor for Target.
* </p>
*
* @param target
* a {@link forge.card.spellability.Target} object.
*/
public Target(final Target target) {
this.tgtValid = true;
this.srcCard = target.getSourceCard();
this.vtSelection = target.getVTSelection();
this.validTgts = target.getValidTgts();
this.minTargets = target.getMinTargets();
this.maxTargets = target.getMaxTargets();
this.tgtZone = target.getZone();
this.targetSpellAbilityType = target.getTargetSpellAbilityType();
this.saValidTargeting = target.getSAValidTargeting();
this.dividedAsYouChoose = target.isDividedAsYouChoose();
this.uniqueTargets = target.isUniqueTargets();
this.singleZone = target.isSingleZone();
this.differentControllers = target.isDifferentControllers();
this.differentZone = target.isDifferentZone();
this.sameController = target.isSameController();
this.withoutSameCreatureType = target.isWithoutSameCreatureType();
this.definedController = target.getDefinedController();
}
/**
* <p>
* Constructor for Target.
* </p>
*
* @param src
* a {@link forge.Card} object.
* @param select
* a {@link java.lang.String} object.
* @param valid
* an array of {@link java.lang.String} objects.
*/
public Target(final Card src, final String select, final String[] valid) {
this(src, select, valid, "1", "1");
}
/**
* <p>
* Constructor for Target.
* </p>
*
* @param src
* a {@link forge.Card} object.
* @param select
* a {@link java.lang.String} object.
* @param valid
* a {@link java.lang.String} object.
*/
public Target(final Card src, final String select, final String valid) {
this(src, select, valid.split(","), "1", "1");
}
/**
* <p>
* Constructor for Target.
* </p>
*
* @param src
* a {@link forge.Card} object.
* @param select
* a {@link java.lang.String} object.
* @param valid
* an array of {@link java.lang.String} objects.
* @param min
* a {@link java.lang.String} object.
* @param max
* a {@link java.lang.String} object.
*/
public Target(final Card src, final String select, final String[] valid, final String min, final String max) {
this.srcCard = src;
this.tgtValid = true;
this.vtSelection = select;
this.validTgts = valid;
this.minTargets = min;
this.maxTargets = max;
}
/**
* <p>
* getSourceCard.
@@ -101,8 +211,6 @@ public class Target {
this.choice = tc;
}
private boolean bMandatory = false;
/**
* <p>
* getMandatory.
@@ -126,10 +234,6 @@ public class Target {
this.bMandatory = m;
}
private boolean tgtValid = false;
private String[] validTgts;
private String vtSelection = "";
/**
* <p>
* doesTarget.
@@ -163,9 +267,6 @@ public class Target {
return this.vtSelection;
}
private String minTargets;
private String maxTargets;
/**
* Gets the min targets.
*
@@ -247,8 +348,6 @@ public class Target {
return (this.choice != null) && (this.getMinTargets(c, sa) <= this.choice.getNumTargeted());
}
private List<ZoneType> tgtZone = Arrays.asList(ZoneType.Battlefield);
/**
* <p>
* setZone.
@@ -282,10 +381,6 @@ public class Target {
return this.tgtZone;
}
// Used for Counters. Currently, Spell,Activated,Triggered can be
// Comma-separated
private String targetSpellAbilityType = null;
/**
* <p>
* Setter for the field <code>targetSpellAbilityType</code>.
@@ -309,9 +404,6 @@ public class Target {
return this.targetSpellAbilityType;
}
// Used for Counters. The target SA of this SA must be targeting a Valid X
private String saValidTargeting = null;
/**
* <p>
* setSAValidTargeting.
@@ -448,141 +540,6 @@ public class Target {
this.choice = null;
}
/**
* <p>
* Constructor for Target.
* </p>
*
* @param target
* a {@link forge.card.spellability.Target} object.
*/
public Target(final Target target) {
this.tgtValid = true;
this.srcCard = target.getSourceCard();
this.vtSelection = target.getVTSelection();
this.validTgts = target.getValidTgts();
this.minTargets = target.getMinTargets();
this.maxTargets = target.getMaxTargets();
this.tgtZone = target.getZone();
this.targetSpellAbilityType = target.getTargetSpellAbilityType();
}
/**
* <p>
* Constructor for Target.
* </p>
* DEPRECATED!!! This will be removed after 1.3.5 is released
*
* @param src
* a {@link forge.Card} object.
* @param parse
* a {@link java.lang.String} object.
* @param min
* a {@link java.lang.String} object.
* @param max
* a {@link java.lang.String} object.
*/
// @Deprecated
// public Target(final Card src, String parse, final String min, final String max) {
// // parse=Tgt{C}{P} - Primarily used for Pump or Damage
// // C = Creature P=Player/Planeswalker
// // CP = All three
//
// this.tgtValid = true;
// this.srcCard = src;
//
// if (parse.contains("Tgt")) {
// parse = parse.replace("Tgt", "");
// }
//
// String valid;
// String prompt;
// final StringBuilder sb = new StringBuilder();
//
// if (parse.equals("CP")) {
// valid = "Creature,Player";
// prompt = "Select target creature or player";
// } else if (parse.equals("C")) {
// valid = "Creature";
// prompt = "Select target creature";
// } else if (parse.equals("P")) {
// valid = "Player";
// prompt = "Select player";
// } else {
// System.out.println("Bad Parsing in Target(parse, min, max): " + parse);
// return;
// }
//
// if (src != null) {
// sb.append(src + " - ");
// }
// sb.append(prompt);
// this.vtSelection = sb.toString();
// this.validTgts = valid.split(",");
//
// this.minTargets = min;
// this.maxTargets = max;
// }
/**
* <p>
* Constructor for Target.
* </p>
*
* @param src
* a {@link forge.Card} object.
* @param select
* a {@link java.lang.String} object.
* @param valid
* an array of {@link java.lang.String} objects.
*/
public Target(final Card src, final String select, final String[] valid) {
this(src, select, valid, "1", "1");
}
/**
* <p>
* Constructor for Target.
* </p>
*
* @param src
* a {@link forge.Card} object.
* @param select
* a {@link java.lang.String} object.
* @param valid
* a {@link java.lang.String} object.
*/
public Target(final Card src, final String select, final String valid) {
this(src, select, valid.split(","), "1", "1");
}
/**
* <p>
* Constructor for Target.
* </p>
*
* @param src
* a {@link forge.Card} object.
* @param select
* a {@link java.lang.String} object.
* @param valid
* an array of {@link java.lang.String} objects.
* @param min
* a {@link java.lang.String} object.
* @param max
* a {@link java.lang.String} object.
*/
public Target(final Card src, final String select, final String[] valid, final String min, final String max) {
this.srcCard = src;
this.tgtValid = true;
this.vtSelection = select;
this.validTgts = valid;
this.minTargets = min;
this.maxTargets = max;
}
/**
* <p>
* getTargetedString.