It is currently 25 Apr 2024, 07:55
   
Text Size

Fix for computer AI with creatures which sacrifice at eot.

Moderators: timmermac, Blacksmith, KrazyTheFox, Agetian, friarsol, CCGHQ Admins

Fix for computer AI with creatures which sacrifice at eot.

Postby jpb » 13 Jan 2009, 05:12

There are some times when the AI will not attack with creatures it must sacrifice at the end of the turn. This more often than not doesn't make sense. Here is the fix so that the computer will always attack with creatures that must be sacrificed at the end of the turn. First add the following to Card.java

Code: Select all
private boolean sacrificeAtEOT = false;
//...
  public boolean getSacrificeAtEOT() {
   return sacrificeAtEOT;
  }
 
  public void setSacrificeAtEOT(boolean sacrificeAtEOT) {
     this.sacrificeAtEOT = sacrificeAtEOT;
  }
Now replace the current getAttackers() in ComputerUtil_Attack2.java with this one.

Code: Select all
  public Combat getAttackers()
  {
    Combat combat = new Combat();
    if(doAssault() /*|| (blockers.size() == 1 && 1 < attackers.size())*/)
    {
      for(int i = 0; i < attackers.size(); i++)
        combat.addAttacker(attackers.get(i));
    }
    else
    {
      Card bigDef;
      Card bigAtt;
      int nNotAttacking = 0;

      //so the biggest creature will usually attack
      //I think this works, not sure, may have to change it
      //sortNonFlyingFirst has to be done first, because it reverses everything
      CardListUtil.sortNonFlyingFirst(attackers);
      CardListUtil.sortAttackLowFirst(attackers);
/*
      //if under 7 life, always don't attack with smallest creature
      if(AllZone.Computer_Life.getLife() < 7 && 2 < attackers.size())
      {
        nNotAttacking++;
        attackers.remove(0);
      }
*/
      for(int i = 0; i < attackers.size(); i++)
      {
        //try to make the computer a little unpredictable
        //do not attack 9% of the time
        //always attack if 3 creatures are not already attacking
        if(MyRandom.percentTrue(9) && nNotAttacking < 3 && !blockers.isEmpty())
        {
          nNotAttacking++;
          continue;
        }

        bigAtt = getBiggestAttack(attackers.get(i));
        bigDef = getBiggestDefense(attackers.get(i));

        //if attacker can destroy biggest blocker or
        //biggest blocker cannot destroy attacker
        if(CombatUtil.canDestroyAttacker(bigDef, attackers.get(i)) /*||
           (! CombatUtil.canDestroyAttacker(attackers.get(i), bigAtt))*/){
          combat.addAttacker(attackers.get(i));
        }
        else if(attackers.get(i).getSacrificeAtEOT()){
          combat.addAttacker(attackers.get(i));
        }
        else{
          nNotAttacking++;
        }
      }
    }//getAttackers()
Then make the cards which must be sacrificed at end of turn (eot) call setSacrificedAtEOT(true) on themselves. Here are all the ones I found in CardFactory.java. Replace them with the following.

Code: Select all
    //*************** START *********** START **************************
    if(cardName.equals("Groundbreaker") || cardName.equals("Ball Lightning") ||
       cardName.equals("Blistering Firecat") || cardName.equals("Spark Elemental"))
    {
      final SpellAbility spell = new Ability(card, "0")
      {
        public void resolve()
        {
          if(AllZone.GameAction.isCardInPlay(card))
            AllZone.GameAction.sacrifice(card);
        }
      };
      spell.setStackDescription("Sacrifice " +card);

      final Command destroy = new Command()
      {
        public void execute()
        {
          if(AllZone.GameAction.isCardInPlay(card))
            AllZone.Stack.add(spell);
        }
      };

      Command intoPlay = new Command()
      {
        public void execute()
        {
          AllZone.EndOfTurn.addAt(destroy);
        }
      };
      card.setComesIntoPlay(intoPlay);
      card.setSacrificeAtEOT(true);
    }//*************** END ************ END **************************
Code: Select all
    //*************** START *********** START **************************
    if(cardName.equals("Feral Lightning"))
    {
      SpellAbility spell = new Spell(card)
      {
        public boolean canPlayAI()
        {
          return AllZone.Phase.getPhase().equals(Constant.Phase.Main1);
        }


        public void resolve()
        {
          final Card[] token = new Card[3];
          final Command atEOT = new Command()
          {
            public void execute()
            {
              //destroy tokens at end of turn
              for(int i = 0; i < token.length; i++)
                if(AllZone.GameAction.isCardInPlay(token[i]))
                  AllZone.GameAction.destroy(token[i]);
            }
          };
          AllZone.EndOfTurn.addAt(atEOT);

          for(int i = 0; i < token.length; i++)
            token[i] = makeToken();
        }//resolve()
        Card makeToken()
        {
          Card c = new Card();

          c.setOwner(card.getController());
          c.setController(card.getController());

          c.setManaCost("R");
          c.setToken(true);

          c.addType("Creature");
          c.addType("Elemental");
          c.setAttack(3);
          c.setDefense(1);
          c.addKeyword("Haste");
          c.setSacrificeAtEOT(true);

          PlayerZone play = AllZone.getZone(Constant.Zone.Play, card.getController());
          play.add(c);

          return c;
        }//makeToken()
      };//SpellAbility
      card.clearSpellAbility();
      card.addSpellAbility(spell);
    }//*************** END ************ END **************************
This will also fix Sparkspitter. The only one I tested was Sparkspitter. Before this fix the computer would not attack with the 3/1 tokens that must be sacrificed at the end of turn if I had a 4/4 in play and 20 life. After this fix the computer will now attack with the 3/1 tokens that must be sacrificed at the end of turn if I have a 4/4 in play. Please report other AI fixes you want done, as I think this is the best way to improve MTG Forge.
jpb
 
Posts: 132
Joined: 05 Sep 2008, 13:12
Has thanked: 0 time
Been thanked: 0 time

Return to Forge

Who is online

Users browsing this forum: No registered users and 157 guests


Who is online

In total there are 157 users online :: 0 registered, 0 hidden and 157 guests (based on users active over the past 10 minutes)
Most users ever online was 4143 on 23 Jan 2024, 08:21

Users browsing this forum: No registered users and 157 guests

Login Form