Code to implement bag protection

Developers
Benjamin B
2005-05-09
2013-04-22
  • Benjamin B
    Benjamin B
    2005-05-09

    Hie for fun, I have trie to implement a way to applay the bag protection during a damage.

    Ask me what you thinks about this code..?

    Sorry but my english is not good!

    List of changes:

    <b><u>Damage.java:</u></b>
    <b>modify damageInventory</b>
        // inflict damage on thing and all sub-items/inventory
        public static void damageInventory(Thing t, int dam, String damtype, int chance) {

            Thing[] inv=t.inv();
          if (t.isHero()){
             inv = new Thing[inv.length];
             /*for(int k=0;k<inv.length;k++){
                inv[k]=t.inv()[k];
             }*/
             System.arraycopy(t.inv(),0, inv,0,inv.length);
             bagProtection(t,inv, dam, damtype,chance);
          }

    <b>add new method bagProtection</b>
       /**
        * Applay the protection of all wields bags for a damage.
        *
        * <b>Always use a copy of the inventory array because
        * protected equipment as set to null in the array.</b>
        **/
       private static void bagProtection(Thing hero,  Thing[] equip, int dam, String damtype, int chance) {
          for(int i =0;i<RPG.WT_BAGS.length;i++){
             Thing bag =hero.getWielded(RPG.WT_BAGS[i]);
             if(bag!=null){
                Damage.inflict(bag, dam, damtype);
                if(!bag.isDead()){
                   for(int j=0;j<equip.length;j++){
                      if (equip[j]!=null){
                         //set to null the protected items and the bag because damage have already been apply
                             if ( ( equip[j].getFlag((String)bag.get("Holds")) && ! hero.isWielded(equip[j]))
                             || equip[j]==bag){
                            equip[j]=null;
                         }
                      }
                   }
                }
             }
          }
       }
    <hr>
    <b><u>Thing.java:</u></b>
    <b>add this method</b>
       /**
        * checks if a thing is wielded
        * @return the fact
        */
       public boolean isWielded(Thing item) {
          boolean result=false;
          for (int i = 0; i < invcount; i++) {
             Thing t = inv[i];
             if (t.getFlag("IsItem")&&(t.y > 0) && t==item ) {
                return true;
             }
          }
          return result;
       }

    <hr>
    <b><u>RPG.java:</u></b>
    <b>add this line after the wield constant</b>
       public static final int[] WT_BAGS = {WT_BOOKBAG,WT_FOODSACK, WT_HOLDING,WT_INGREDIENTPOUCH,WT_JEWELRYCASE, WT_KEYRING, WT_POTIONCASE, WT_QUIVER, WT_RUNEBAG, WT_SCROLLCASE, WT_WANDCASE    };

    <hr>
    <b><u>Equipment.java:</u></b>
    <b>I have detect an error, I suggest italic correction</b>
            t = Lib.extend("scroll case","base container");
            t.set("Image",142);
            t.set("WieldType", RPG.WT_SCROLLCASE);
            t.set("Holds","IsReadable<i>IsScroll</i>");
            t.set("LevelMin",3);
            Lib.add(t);
    <b>I have detect an error, I suggest italic correction</b>
            t = Lib.extend("book bag","base container");
            t.set("Image",142);
            t.set("WieldType", RPG.WT_BOOKBAG);
            t.set("Holds","IsReadable<i>IsBook</i>");
            t.set("LevelMin",3);
            Lib.add(t);

     
    • Benjamin B
      Benjamin B
      2005-05-09

      Oups....

      I have the code with the good indentation.. if you want it

       
    • Mike Anderson
      Mike Anderson
      2005-05-09

      Looks pretty good to me. Will certainly make bags more valuable.

      I've added you as a developer so you can commit to CVS.... welcome to the team!

       
      • Buddha Buddy
        Buddha Buddy
        2005-05-09

        You say "more valuable", but I don't see that they are at _all_ valuable currently. Do containers as of 0.333 have any use at all?

         
    • Benjamin B
      Benjamin B
      2005-05-10

      Tk's Mikera,

      Just for be sure, I have to extract HEAD and after I commit my change in head... Or I must work in a branch?

      I will trie to commit the code tonight.

      tk' s again

       
      • Mike Anderson
        Mike Anderson
        2005-05-12

        Yes, it's fine to work in HEAD.

        Only thing I ask is that you also write a JUnit test case to cover your changes - that way we can make sure that there aren't any nasty interactions

         
    • Benjamin B
      Benjamin B
      2005-05-13

      I will try to imlpement the test case this week-end

       
    • Buddha Buddy
      Buddha Buddy
      2005-05-18

      I have a suggestion and a question.

      In your "for" loops, it is better to do easy checks first, then expensive checks. The line:
      if (t.getFlag("IsItem")&&(t.y > 0) && t==item )
      ... is more efficient written as:
      if (t==item &&(t.y > 0)  )

      However, why go through the inventory at all? You already have the item in question. The entire isWielded() method can be replaced with:
      return t!=null && t.place==Game.hero() && t.y>0 && t.getFlag("IsItem");

      That would make the method very efficient, and the line:
      if ( ( equip[j].getFlag((String)bag.get("Holds")) && ! hero.isWielded(equip[j]))
      ... could be changed to this:
      if ( ! hero.isWielded(equip[j]) && equip[j].getFlag((String)bag.get("Holds"))

       
    • Benjamin B
      Benjamin B
      2005-05-24

      OK Buddha Buddy

      Your suggestions look greats. I will modify the test order to be more efficient. I made the code incremently and don't optimize it.

      For you question on the For, I will look tonight in the code because I can't now.

      Tk's

       
    • Benjamin B
      Benjamin B
      2005-05-25

      I go through all the items array for set to null, all the items tthat's protected by the bag.

       
    • Benjamin B
      Benjamin B
      2005-05-25

      Buddha Buddy Look the last version(1.55 )in the CVS head of the Damage class