From 291de032a234b5d1039ede87b85610db5173f996 Mon Sep 17 00:00:00 2001 From: jendave Date: Sat, 6 Aug 2011 14:01:15 +0000 Subject: [PATCH] added AB/SP/DB$DamageAll. Use ValidCards$ and ValidPlayer$ to specify things. --- src/forge/AbilityFactory.java | 10 ++ src/forge/AbilityFactory_DealDamage.java | 212 +++++++++++++++++++++++ src/forge/AllZoneUtil.java | 6 + 3 files changed, 228 insertions(+) diff --git a/src/forge/AbilityFactory.java b/src/forge/AbilityFactory.java index 077f5abf167..5a8632445df 100644 --- a/src/forge/AbilityFactory.java +++ b/src/forge/AbilityFactory.java @@ -189,6 +189,16 @@ public class AbilityFactory { else if (isDb) SA = dd.getDrawback(); } + + if (API.equals("DamageAll")){ + AbilityFactory_DealDamage dd = new AbilityFactory_DealDamage(this); + if (isAb) + SA = dd.getAbilityDamageAll(); + else if (isSp) + SA = dd.getSpellDamageAll(); + else if (isDb) + SA = dd.getDrawbackDamageAll(); + } if (API.equals("PutCounter")){ if (isAb) diff --git a/src/forge/AbilityFactory_DealDamage.java b/src/forge/AbilityFactory_DealDamage.java index 0f693f09fd0..b9e9d825922 100644 --- a/src/forge/AbilityFactory_DealDamage.java +++ b/src/forge/AbilityFactory_DealDamage.java @@ -49,6 +49,37 @@ import java.util.Random; return abDamage; } + + public SpellAbility getAbilityDamageAll(){ + + final SpellAbility abDamageAll = new Ability_Activated(AF.getHostCard(), AF.getAbCost(), AF.getAbTgt()){ + private static final long serialVersionUID = -1831356710492849854L; + final AbilityFactory af = AF; + + @Override + public String getStackDescription(){ + return damageAllStackDescription(af, this); + } + + @Override + public boolean canPlay(){ + // super takes care of AdditionalCosts + return super.canPlay(); + } + + @Override + public boolean canPlayAI() { + return damageAllCanPlayAI(af, this); + } + + @Override + public void resolve() { + damageAllResolve(af, this); + } + + }; + return abDamageAll; + } public SpellAbility getSpell() { final SpellAbility spDealDamage = new Spell(AF.getHostCard(), AF.getAbCost(), AF.getAbTgt()) { @@ -80,6 +111,39 @@ import java.util.Random; return spDealDamage; } + + public SpellAbility getSpellDamageAll(){ + final SpellAbility spDamageAll = new Spell(AF.getHostCard(), AF.getAbCost(), AF.getAbTgt()){ + private static final long serialVersionUID = 8004957182752984818L; + final AbilityFactory af = AF; + final HashMap params = af.getMapParams(); + + @Override + public String getStackDescription(){ + if(params.containsKey("SpellDescription")) + return AF.getHostCard().getName() + " - " + params.get("SpellDescription"); + else + return damageAllStackDescription(af, this); + } + + public boolean canPlay(){ + // super takes care of AdditionalCosts + return super.canPlay(); + } + + public boolean canPlayAI() + { + return damageAllCanPlayAI(af, this); + } + + @Override + public void resolve() { + damageAllResolve(af, this); + } + + }; + return spDamageAll; + } public SpellAbility getDrawback() { final SpellAbility dbDealDamage = new Ability_Sub(AF.getHostCard(), AF.getAbTgt()) { @@ -105,6 +169,31 @@ import java.util.Random; return dbDealDamage; } + + public SpellAbility getDrawbackDamageAll(){ + final SpellAbility dbDamageAll = new Ability_Sub(AF.getHostCard(), AF.getAbTgt()){ + private static final long serialVersionUID = -6169562107675964474L; + final AbilityFactory af = AF; + + @Override + public String getStackDescription(){ + return damageAllStackDescription(af, this); + } + + @Override + public void resolve() { + damageAllResolve(af, this); + } + + @Override + public boolean chkAI_Drawback() { + //check AI life before playing this drawback? + return true; + } + + }; + return dbDamageAll; + } private int getNumDamage(SpellAbility saMe) { return AbilityFactory.calculateAmount(saMe.getSourceCard(), damage, saMe); @@ -365,4 +454,127 @@ import java.util.Random; } } } + + private void damageAllResolve(final AbilityFactory af, final SpellAbility sa){ + HashMap params = af.getMapParams(); + String DrawBack = params.get("SubAbility"); + Card card = sa.getSourceCard(); + + int dmg = getNumDamage(sa); + + String valid = ""; + String players = ""; + + if(params.containsKey("ValidCards")) + valid = params.get("ValidCards"); + if(params.containsKey("ValidPlayers")) + players = params.get("ValidPlayers"); + + CardList list = AllZoneUtil.getCardsInPlay(); + list = list.getValidCards(valid.split(","), card.getController(), card); + + for(Card c:list) c.addDamage(dmg, card); + + if(players.equals("All")) { + for(Player p:AllZoneUtil.getPlayersInGame()) { + p.addDamage(dmg, card); + } + } + else if(players.equals("EachOpponent")) { + for(Player p:AllZoneUtil.getOpponents(card.getController())) p.addDamage(dmg, card); + } + else if(players.equals("Self")) + card.getController().addDamage(dmg, card); + else { + //anything else to go here? + } + + if (af.hasSubAbility()){ + Ability_Sub abSub = sa.getSubAbility(); + if (abSub != null){ + abSub.resolve(); + } + else + CardFactoryUtil.doDrawBack(DrawBack, 0, card.getController(), card.getController().getOpponent(), card.getController(), card, null, sa); + } + } + + private String damageAllStackDescription(final AbilityFactory af, SpellAbility sa){ + StringBuilder sb = new StringBuilder(); + String name = af.getHostCard().getName(); + HashMap params = af.getMapParams(); + String desc = ""; + if(params.containsKey("ValidDescription")) + desc = params.get("ValidDescription"); + int dmg = getNumDamage(sa); + + sb.append(name).append(" - Deals "+dmg+" to "+desc); + + Ability_Sub abSub = sa.getSubAbility(); + if (abSub != null) { + sb.append(abSub.getStackDescription()); + } + + return sb.toString(); + } + + private boolean damageAllCanPlayAI(final AbilityFactory af, final SpellAbility sa){ + return false; + /* + // AI needs to be expanded, since this function can be pretty complex based on what the expected targets could be + Random r = new Random(); + Ability_Cost abCost = sa.getPayCosts(); + final Card source = sa.getSourceCard(); + final HashMap params = af.getMapParams(); + String Valid = ""; + + if(params.containsKey("ValidCards")) + Valid = params.get("ValidCards"); + + CardList humanlist = new CardList(AllZone.getZone(Constant.Zone.Battlefield, AllZone.HumanPlayer).getCards()); + CardList computerlist = new CardList(AllZone.getZone(Constant.Zone.Battlefield, AllZone.ComputerPlayer).getCards()); + + humanlist = humanlist.getValidCards(Valid.split(","), source.getController(), source); + computerlist = computerlist.getValidCards(Valid.split(","), source.getController(), source); + + humanlist = humanlist.getNotKeyword("Indestructible"); + computerlist = computerlist.getNotKeyword("Indestructible"); + + if (abCost != null){ + // AI currently disabled for some costs + if (abCost.getSacCost()){ + //OK + } + if (abCost.getLifeCost()){ + if (AllZone.ComputerPlayer.getLife() - abCost.getLifeAmount() < 4) + return false; + } + if (abCost.getDiscardCost()) //OK + + if (abCost.getSubCounter()){ + // OK + } + } + + if (!ComputerUtil.canPayCost(sa)) + return false; + + // prevent run-away activations - first time will always return true + boolean chance = r.nextFloat() <= Math.pow(.6667, source.getAbilityUsed()); + + // if only creatures are affected evaluate both lists and pass only if human creatures are more valuable + if (humanlist.getNotType("Creature").size() == 0 && computerlist.getNotType("Creature").size() == 0) { + if(CardFactoryUtil.evaluateCreatureList(computerlist) + 200 >= CardFactoryUtil.evaluateCreatureList(humanlist)) + return false; + } // otherwise evaluate both lists by CMC and pass only if human permanents are more valuable + else if(CardFactoryUtil.evaluatePermanentList(computerlist) + 3 >= CardFactoryUtil.evaluateCreatureList(humanlist)) + return false; + + Ability_Sub subAb = sa.getSubAbility(); + if (subAb != null) + chance &= subAb.chkAI_Drawback(); + + return ((r.nextFloat() < .6667) && chance); + */ + } } diff --git a/src/forge/AllZoneUtil.java b/src/forge/AllZoneUtil.java index 317274dcd80..388c5706531 100644 --- a/src/forge/AllZoneUtil.java +++ b/src/forge/AllZoneUtil.java @@ -704,4 +704,10 @@ public class AllZoneUtil { list.add(AllZone.ComputerPlayer); return list; } + + public static ArrayList getOpponents(Player p) { + ArrayList list = new ArrayList(); + list.add(p.getOpponent()); + return list; + } } \ No newline at end of file