L2JLisvus

Would you like to react to this message? Create an account in a few clicks or log in to continue.

3 posters

    Treasure Chests AI -> Java [COMMITED]

    Karakan
    Karakan


    Posts : 756
    Join date : 2013-10-04

    Treasure Chests AI -> Java [COMMITED] Empty Treasure Chests AI -> Java [COMMITED]

    Post  Karakan 18th July 2019, 12:58

    Adapted Chests AI to java.
    No problems found during my tests.

    Feel free to make suggestions on how to improve the code.



    scripts.cfg


    Code:

    Index: scripts.cfg
    ===================================================================
    --- scripts.cfg (revision 627)
    +++ scripts.cfg (working copy)
    @@ -8,7 +8,6 @@
     # AI Section
     
     ai/group_template/polymorphing_onAttack.py
    -ai/group_template/chests.py
     ai/group_template/devils_isle.py
     ai/group_template/feedable_beasts.py
     ai/group_template/FairyTrees.java
    @@ -18,6 +17,7 @@
     ai/group_template/PlainsOfDion.java
     ai/group_template/SearchingMaster.java
     ai/group_template/TowerOfInsolence.java
    +ai/group_template/TreasureChests.java
     ai/group_template/ValleyOfSaints.java




    ../group_template/TreasureChests.java

    Code:

    /*
     * 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 ai.group_template;

    import net.sf.l2j.gameserver.ai.CtrlIntention;
    import net.sf.l2j.gameserver.model.L2Object;
    import net.sf.l2j.gameserver.model.L2Skill;
    import net.sf.l2j.gameserver.model.L2Character;
    import net.sf.l2j.gameserver.model.actor.instance.L2NpcInstance;
    import net.sf.l2j.gameserver.model.actor.instance.L2ChestInstance;
    import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
    import net.sf.l2j.gameserver.model.quest.Quest;
    import net.sf.l2j.gameserver.network.serverpackets.SocialAction;
    import net.sf.l2j.gameserver.util.Util;
    import net.sf.l2j.util.Rnd;

    /**
     * Treasure Chest AI
     * By Karakan for L2JLisvus
     */
    public class TreasureChests extends Quest
    {
      //Deluxe Key skill
      private static final int SKILL_DELUXE_KEY = 2229;
     
      //Base chance for BOX to be opened
      private static final int BASE_CHANCE = 100;
     
      // Percent to decrease base chance when grade of DELUXE key not match
      private static final int LEVEL_DECREASE = 40;
     
      // Chance for a chest to actually be a BOX (as opposed to being a mimic).
      private static final int IS_BOX = 40;
     
      private static final int[] NPC_IDS =
      { 13100,13101,13102,13103,13104,13105,13106,13107,13108,13109,13110,13111,13112,13113,13114,13115,13116,13117,13118,13119,13120,13121,1801,1802,1803,1804,1805,1806,1807,1808,1809,1810,
          1671,1694,1717,1740,1763,1786,13213,13215,13217,13219,13221,13223,1811,1812,1813,1814,1815,1816,1817,1818,1819,1820,1821,1822
      };

      public TreasureChests(int questId, String name, String descr)
      {
          super(questId, name, descr);
          this.registerMobs(NPC_IDS);
      }
     
      @Override
      public String onSkillSee(L2NpcInstance npc, L2PcInstance caster, L2Skill skill, L2Object[] targets, boolean isPet)
      {
          if (npc instanceof L2ChestInstance)
          {
            // this behavior only runs when the target of skill is the passed npc (chest)
            // i.e. when the player is attempting to open the chest using a skill       
            if (!Util.contains(targets,npc))
            {
                return super.onSkillSee(npc, caster, skill, targets, isPet);
            }

            final L2ChestInstance chest = ((L2ChestInstance) npc);
            final int npcId = chest.getNpcId();
            final int skillId = skill.getId();
            final int skillLevel = skill.getLevel();
           
            if (!Util.contains(NPC_IDS, npcId))
            {
                return super.onSkillSee(npc, caster, skill, targets, isPet);
            }
            // if this has already been interacted, no further ai decisions are needed
            // if it's the first interaction, check if this is a box or mimic
            if (!chest.isInteracted())
            {
                chest.setInteracted();
                if (Rnd.get(100) < IS_BOX)
                {
                  // if it's a box, either it will be successfully opened by a proper key, or instantly disappear
                  if (skillId == SKILL_DELUXE_KEY)
                  {
                      // check the chance to open the box
                      int keyLevelNeeded = chest.getLevel() / 10;
                      keyLevelNeeded -= skillLevel;
                      if (keyLevelNeeded < 0)
                        keyLevelNeeded *= -1;
                      final int chance = BASE_CHANCE - keyLevelNeeded * LEVEL_DECREASE;

                      // success, pretend-death with rewards
                      if (Rnd.get(100) < chance)
                      {
                        caster.broadcastPacket(new SocialAction(caster.getObjectId(), 3));
                        chest.setMustRewardExpSp(false);
                        chest.setSpecialDrop();
                        chest.reduceCurrentHp(99999999, caster, false, false);
                        return null;
                      }
                  }
                  // used a skill other than chest-key, or used a chest-key but failed to open: disappear with no rewards
                  caster.broadcastPacket(new SocialAction(caster.getObjectId(), 13));
                  npc.onDecay();
                }
                else
                {
                  final L2Character originalCaster = isPet ? caster.getPet() : caster;
                  chest.setRunning();
                  chest.addDamageHate(originalCaster, 0, 999);
                  chest.getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, originalCaster);
                }
            }
          }

          return super.onSkillSee(npc, caster, skill, targets, isPet);
      }
     
      @Override
      public String onAttack(L2NpcInstance npc, L2PcInstance attacker, int damage, boolean isPet)
      {
          if (npc instanceof L2ChestInstance)
          {
            final L2ChestInstance chest = ((L2ChestInstance) npc);
            final int npcId = chest.getNpcId();
            // check if the chest and skills used are valid for this script.  Exit if invalid.
            if (!Util.contains(NPC_IDS, npcId))
            {
                return super.onAttack(npc, attacker, damage, isPet);
            }
           
            // if this was a mimic, set the target, start the skills and become aggro
            if (!chest.isInteracted())
            {
                chest.setInteracted();
                if (Rnd.get(100) < IS_BOX)
                {
                  chest.getSpawn().decreaseCount(chest);
                  chest.deleteMe();
                }
                else
                {
                  // if this weren't a box, upon interaction start the mimic behaviors...
                  // todo: perhaps a self-buff (skill id 4245) with random chance goes here?
                  final L2Character originalAttacker = isPet ? attacker.getPet() : attacker;
                  chest.setRunning();
                  chest.addDamageHate(originalAttacker, 0, (damage * 100) / (chest.getLevel() + 7));
                  chest.getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, originalAttacker);
                }
            }
          }

          return super.onAttack(npc, attacker, damage, isPet);
      }
     
      public static void main(String[] args)
      {
          // now call the constructor (starts up the ai)
          new TreasureChests(-1, "treasurechests", "ai");
      }
    }


    Last edited by Karakan on 28th July 2019, 13:02; edited 1 time in total
    improvise
    improvise


    Posts : 144
    Join date : 2017-07-21

    Treasure Chests AI -> Java [COMMITED] Empty Re: Treasure Chests AI -> Java [COMMITED]

    Post  improvise 19th July 2019, 15:34

    Error on loading script on 627 clear build.
    Treasure Chests AI -> Java [COMMITED] Chests10
    Karakan
    Karakan


    Posts : 756
    Join date : 2013-10-04

    Treasure Chests AI -> Java [COMMITED] Empty Re: Treasure Chests AI -> Java [COMMITED]

    Post  Karakan 19th July 2019, 16:53

    Ups, my bad...forgot to post the Util.java diff.


    Here you go.  Cool



    ...\Lisvus_GameServer\java\net\sf\l2j\gameserver\util\Util.java


    Code:

        public static boolean contains(int[] array, int object)
       {
          for (int i : array)
          {
             if (i == object)
             {
                return true;
             }
          }
          return false;
       }

    +  /**
    +    * Checks if given array of objects contains given object.
    +    *
    +    * @param <T>
    +    * @param array - the array to look into
    +    * @param object - The object to search for.
    +   * @return
    +   */
    +   public static <T> boolean contains(T[] array, T object)
    +   {
    +      for (T element : array)
    +      {
    +         if (element == object)
    +         {
    +            return true;
    +         }
    +      }
    +      return false;
    +   }
    }
    improvise
    improvise


    Posts : 144
    Join date : 2017-07-21

    Treasure Chests AI -> Java [COMMITED] Empty Re: Treasure Chests AI -> Java [COMMITED]

    Post  improvise 19th July 2019, 17:25

    It works! Nice work. Social action when chest opening fails made me happy.

    PS: I would like to bring up the following question for discussion.
    According to my experience, on PTS server chance of the successfully opening lower level Treasure Chest with higher level key doesn't decrease.
    Karakan
    Karakan


    Posts : 756
    Join date : 2013-10-04

    Treasure Chests AI -> Java [COMMITED] Empty Re: Treasure Chests AI -> Java [COMMITED]

    Post  Karakan 19th July 2019, 17:39

    improvise wrote:It works! Nice work. Social action when chest opening fails made me happy.

    PS: I would like to bring up the following question for discussion.
    According to my experience, on PTS server chance of the successfully opening lower level Treasure Chest with higher level key doesn't decrease.


    Glad you like it Very Happy

    About higher level keys opening lower level chests...

    I think that might be working fine for Chests with 9 or lower lvl difference.
    After that the "deep blue mob" drop penalty applies.

    So it kinda would'nt make sense after a certain level ?

    Anyways...I'll check that and adjust the code accordingly.

    Thanks for your feedback. Cool
    improvise
    improvise


    Posts : 144
    Join date : 2017-07-21

    Treasure Chests AI -> Java [COMMITED] Empty Re: Treasure Chests AI -> Java [COMMITED]

    Post  improvise 19th July 2019, 18:14

    I mean that chance that box will be opened (ofc, if "chest" is "box") decreases when player using treasure key level higher than it needed.
    For example, i will never open level 24 treasure box using 7 level treasure key.
    In that way opening 45 lvl treasure box with 5 level key is not 100%.
    According to the Treasure Key description text there are no guarantees that you will open lower level box with higher level key, but as i remember it works at this way anywhere.
    Karakan
    Karakan


    Posts : 756
    Join date : 2013-10-04

    Treasure Chests AI -> Java [COMMITED] Empty Re: Treasure Chests AI -> Java [COMMITED]

    Post  Karakan 19th July 2019, 18:34

    Yeah, i got you.
    Thats what im talking about too.

    Time to farm Deluxe Chest Keys and test. Cool
    xlinkinx
    xlinkinx


    Posts : 244
    Join date : 2012-12-11
    Age : 32
    Location : Russian Federation

    Treasure Chests AI -> Java [COMMITED] Empty Re: Treasure Chests AI -> Java [COMMITED]

    Post  xlinkinx 24th July 2019, 07:27

    strange, what else is no added to the SVN (just in case I will give a complete copy of python.

    Chest.java
    Spoiler:
    Karakan
    Karakan


    Posts : 756
    Join date : 2013-10-04

    Treasure Chests AI -> Java [COMMITED] Empty Re: Treasure Chests AI -> Java [COMMITED]

    Post  Karakan 24th July 2019, 15:36

    strange, what else is no added to the SVN

    There was always a working version in the svn.
    What are you talking about ?


    (just in case I will give a complete copy of python.

    Our goal is to get rid of python scripts...... In case you didnt know. jocolor
    xlinkinx
    xlinkinx


    Posts : 244
    Join date : 2012-12-11
    Age : 32
    Location : Russian Federation

    Treasure Chests AI -> Java [COMMITED] Empty Re: Treasure Chests AI -> Java [COMMITED]

    Post  xlinkinx 24th July 2019, 15:57

    Karakan wrote:
    strange, what else is no added to the SVN

    There was always a working version in the svn.
    What are you talking about ?


    (just in case I will give a complete copy of python.

    Our goal is to get rid of python scripts...... In case you didnt know. jocolor

    omg, I will say differently, it is a pity that they did not add the Java version on the SVN.

    what I gave was just a copy of the python version, but in Java version, the backup version is nothing more.

    only that was meant.
    Karakan
    Karakan


    Posts : 756
    Join date : 2013-10-04

    Treasure Chests AI -> Java [COMMITED] Empty Re: Treasure Chests AI -> Java [COMMITED]

    Post  Karakan 24th July 2019, 16:04


    Im sure DnR will add the above version when he has time to check/adjust the code.

    Patience young Padawan.

    Sponsored content


    Treasure Chests AI -> Java [COMMITED] Empty Re: Treasure Chests AI -> Java [COMMITED]

    Post  Sponsored content


      Current date/time is 19th May 2024, 07:56