Page 1 of 1

Beached As - fixed performance issues with Wonder etc...

PostPosted: 02 Jul 2010, 16:05
by mtgrares
How did you fix the problem? Feel free to copy and paste your code here. It seems like I coded Wonder eons ago. :D

Re: Beached As - fixed performance issues with Wonder etc...

PostPosted: 06 Jul 2010, 12:04
by Beached As
mtgrares wrote:How did you fix the problem? Feel free to copy and paste your code here. It seems like I coded Wonder eons ago. :D
Previously the code for the incarnations (e.g. Wonder) removed the keyword from all creatures effected by Wonder and then added the keyword back to them whenever the state effects are checked. The problem is however, that state effects are checked VERY often (you can confirm this by adding a message popup in any active state effect). The new code only adds and removes keywords from single creatures as they enter or leave the controller's play zone.

Old Code for Wonder:

Code: Select all

   public static Command Wonder                      = new Command() {
      private static final long serialVersionUID = 8346741995447241353L;

      CardList                  old              = new CardList();

      public void execute() {
         // reset creatures
         removeFlying(old);

         if(isInGrave(Constant.Player.Computer)) addFlying(Constant.Player.Computer);

         if(isInGrave(Constant.Player.Human)) addFlying(Constant.Player.Human);
      }// execute()

      void addFlying(String player) {
         PlayerZone play = AllZone.getZone(
               Constant.Zone.Play, player);
         CardList list = new CardList(play.getCards());
         list = list.getType("Creature");

         // add creatures to "old" or previous list of creatures
         old.addAll(list.toArray());

         addFlying(list);
      }

      boolean isInGrave(String player) {
         PlayerZone grave = AllZone.getZone(
               Constant.Zone.Graveyard, player);
         CardList list = new CardList(grave.getCards());

         PlayerZone play = AllZone.getZone(
               Constant.Zone.Play, player);
         CardList lands = new CardList(play.getCards());
         lands = lands.getType("Island");


         if(!list.containsName("Wonder") || lands.size() == 0) return false;
         else return true;
      }

      void removeFlying(CardList list) {
         for(int i = 0; i < list.size(); i++)
            list.get(i).removeExtrinsicKeyword("Flying");
      }

      void addFlying(CardList list) {
         for(int i = 0; i < list.size(); i++)
            list.get(i).addExtrinsicKeyword("Flying");
      }
   }; // Wonder

New Code for Wonder:

Code: Select all

   public static Command Wonder                      = new Command() {
      private static final long serialVersionUID = -846723300545847505L;

      CardList                  old              = new CardList();
      CardList                  next             = new CardList();

      public void execute() {
         if(Phase.GameBegins == 1) {
         // reset creatures
         removeFlying(old);

         if(isInGrave(Constant.Player.Computer)) addFlying(Constant.Player.Computer);

         if(isInGrave(Constant.Player.Human)) addFlying(Constant.Player.Human);
         }
      }// execute()

      void addFlying(String player) {
         next.clear();
         PlayerZone play = AllZone.getZone(Constant.Zone.Play, player);
         CardList playlist = new CardList(play.getCards());
         playlist = playlist.getType("Creature");
         for(int i = 0; i < playlist.size(); i++) {
            if(!old.contains(playlist.get(i))) next.add(playlist.get(i));
         }
         // add creatures to "old" or previous list of creatures
         
         
         addFlying(next);
      }

      boolean isInGrave(String player) {
         PlayerZone grave = AllZone.getZone(
               Constant.Zone.Graveyard, player);
         CardList list = new CardList(grave.getCards());

         PlayerZone play = AllZone.getZone(
               Constant.Zone.Play, player);
         CardList lands = new CardList(play.getCards());
         lands = lands.getType("Island");


         if(!list.containsName("Wonder") || lands.size() == 0) return false;
         else return true;
      }

      void removeFlying(CardList list) {
           CardList List_Copy = new CardList();
           List_Copy.add(list);
         for(int i = 0; i < List_Copy.size(); i++) {
            Card c = List_Copy.get(i);
            if(!isInGrave(c.getController()) && old.contains(c)) {
               List_Copy.get(i).removeExtrinsicKeyword("Flying");
               old.remove(c);
            }
      }
      }

      void addFlying(CardList list) {
         int Count = list.size();
         for(int i = 0; i < Count; i++) {
            list.get(i).addExtrinsicKeyword("Flying");
            old.add(list.get(i));
         }
      }
   }; // Wonder

This new method can also be applied to power/toughness bonuses and i've used this method to code Coat of Arms

Re: Beached As - fixed performance issues with Wonder etc...

PostPosted: 08 Jul 2010, 14:26
by mtgrares
Previously the code for the incarnations (e.g. Wonder) removed the keyword from all creatures effected by Wonder and then added the keyword back to them whenever the state effects are checked.
Yeah, that was my code. I didn't know how to do it any better. Thanks for the update, no one likes it when Forge slows to a crawl.

Re: Beached As - fixed performance issues with Wonder etc...

PostPosted: 08 Jul 2010, 14:42
by juzamjedi
This is pretty awesome - I liked the incarnations, but previously they made the game way too slow for my liking. I will have to try them again in draft.