Thread: Accurate NPC Drop Chance System

Page 1 of 2 12 LastLast
Results 1 to 10 of 11
  1. #1 Accurate NPC Drop Chance System 
    Registered Member
    Michael P's Avatar
    Join Date
    Dec 2013
    Posts
    1,244
    Thanks given
    488
    Thanks received
    309
    Discord
    View profile
    Rep Power
    644
    Not a lot of people seem to have very accurate NPC drop chance systems, so here you go. Enjoy.

    I do NOT give permission to re-release and/or distribute this in anyway.

    Code:
    package org.rs_reborn.game.model.npcs.drop;
    
    import java.util.Random;
    
    /**
     * Represents an npc drop chance.
     * 
     * @author Michael P
     *
     */
    public enum NpcDropChance {
    
    	ALWAYS(1),
    	COMMON("2-50"),
    	UNCOMMON("51-100"),
    	RARE("101-512"),
    	VERY_RARE("513-1000");
    	
    	/**
    	 * The rate of the chance.
    	 */
    	private final Object rate;
    	
    	/**
    	 * The {@link Random} used for generating a random number.
    	 */
    	private final Random random = new Random();
    	
    	/**
    	 * Constructs new a {@link NpcDropChance}.
    	 * 
    	 * @param rate
    	 *            The rate {@link Object}.
    	 */
    	private NpcDropChance(Object rate) {
    		this.rate = rate;
    	}
    	
    	/**
    	 * Checks to see if the chance is a success.
    	 * @return If the chance is a success {@code true};
    	 * if not {@code false}.
    	 */
    	public boolean isSuccess() {
    		if (rate instanceof String) {
    			String[] args = ((String) rate).split("-");
    			int n = getN(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
    			int success = Integer.parseInt(args[0]);
    			return n % success == 0;
    		}
    		return true; // rate is 1 (ALWAYS), therefore it's a success 100%.
    	}
    	
    	/**
    	 * Gets a random number within a certain range.
    	 * 
    	 * @param min
    	 *            The minimum number range.
    	 * @param max
    	 *            The maximum number range.
    	 * @return The random number.
    	 */
    	private int getN(int min, int max) {
    		final int n = Math.abs(max - min);
    		return Math.min(min, max) + (n == 0 ? 0 : random.nextInt(n));
    	}
    	
    }
    Here is a dump of the drop attempts using this system for those who want to see the accuracy of it.

    Code:
    Took 0 attempts for: COMMON
    Took 0 attempts for: COMMON
    Took 2 attempts for: COMMON
    Took 0 attempts for: COMMON
    Took 2 attempts for: COMMON
    <----    New Drop    ---->
     
    Took 162 attempts for: UNCOMMON
    Took 8 attempts for: UNCOMMON
    Took 112 attempts for: UNCOMMON
    Took 2 attempts for: UNCOMMON
    Took 39 attempts for: UNCOMMON
    <----    New Drop    ---->
     
    Took 15 attempts for: RARE
    Took 47 attempts for: RARE
    Took 123 attempts for: RARE
    Took 180 attempts for: RARE
    Took 70 attempts for: RARE
    <----    New Drop    ---->
     
    Took 335 attempts for: VERY_RARE
    Took 518 attempts for: VERY_RARE
    Took 1215 attempts for: VERY_RARE
    Took 88 attempts for: VERY_RARE
    Took 123 attempts for: VERY_RARE
    <----    New Drop    ---->
    Reply With Quote  
     

  2. Thankful user:


  3. #2  
    Donator

    Join Date
    Feb 2013
    Posts
    1,674
    Thanks given
    479
    Thanks received
    244
    Rep Power
    29
    Thanks Michael, won't use though.
    Reply With Quote  
     

  4. #3  
    Registered Member
    natsu's Avatar
    Join Date
    Apr 2007
    Age
    29
    Posts
    3,451
    Thanks given
    1,084
    Thanks received
    676
    Rep Power
    1096
    nice prob going to use do you mind showing the file build up ?

    [Only registered and activated users can see links. ]

    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  5. #4  
    Banned

    Join Date
    Jun 2013
    Posts
    939
    Thanks given
    462
    Thanks received
    171
    Rep Power
    0
    Nice one bro looks good might use
    Reply With Quote  
     

  6. #5  
    Registered Member
    Michael P's Avatar
    Join Date
    Dec 2013
    Posts
    1,244
    Thanks given
    488
    Thanks received
    309
    Discord
    View profile
    Rep Power
    644
    Quote Originally Posted by natsu View Post
    nice prob going to use do you mind showing the file build up ?
    What do you mean?

    Here is a dump of the drop attempts using this system for those who want to see the accuracy of it.

    Code:
    Took 0 attempts for: COMMON
    Took 0 attempts for: COMMON
    Took 2 attempts for: COMMON
    Took 0 attempts for: COMMON
    Took 2 attempts for: COMMON
    <----    New Drop    ---->
     
    Took 162 attempts for: UNCOMMON
    Took 8 attempts for: UNCOMMON
    Took 112 attempts for: UNCOMMON
    Took 2 attempts for: UNCOMMON
    Took 39 attempts for: UNCOMMON
    <----    New Drop    ---->
     
    Took 15 attempts for: RARE
    Took 47 attempts for: RARE
    Took 123 attempts for: RARE
    Took 180 attempts for: RARE
    Took 70 attempts for: RARE
    <----    New Drop    ---->
     
    Took 335 attempts for: VERY_RARE
    Took 518 attempts for: VERY_RARE
    Took 1215 attempts for: VERY_RARE
    Took 88 attempts for: VERY_RARE
    Took 123 attempts for: VERY_RARE
    <----    New Drop    ---->
    Reply With Quote  
     

  7. #6  
    touched like seafood
    Tyluur's Avatar
    Join Date
    Jun 2010
    Age
    23
    Posts
    4,838
    Thanks given
    1,676
    Thanks received
    1,567
    Discord
    View profile
    Rep Power
    1390
    Quote Originally Posted by Michael P View Post
    Not a lot of people seem to have very accurate NPC drop chance systems, so here you go. Enjoy.

    I do NOT give permission to re-release and/or distribute this in anyway.

    Code:
    package org.rs_reborn.game.model.npcs.drop;
    
    import java.util.Random;
    
    /**
     * Represents an npc drop chance.
     * 
     * @author Michael P
     *
     */
    public enum NpcDropChance {
    
    	ALWAYS(1),
    	COMMON("2-50"),
    	UNCOMMON("51-100"),
    	RARE("101-512"),
    	VERY_RARE("513-1000");
    	
    	/**
    	 * The rate of the chance.
    	 */
    	private final Object rate;
    	
    	/**
    	 * The {@link Random} used for generating a random number.
    	 */
    	private final Random random = new Random();
    	
    	/**
    	 * Constructs new a {@link NpcDropChance}.
    	 * 
    	 * @param rate
    	 *            The rate {@link Object}.
    	 */
    	private NpcDropChance(Object rate) {
    		this.rate = rate;
    	}
    	
    	/**
    	 * Checks to see if the chance is a success.
    	 * @return If the chance is a success {@code true};
    	 * if not {@code false}.
    	 */
    	public boolean isSuccess() {
    		if (rate instanceof String) {
    			String[] args = ((String) rate).split("-");
    			int n = getN(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
    			int success = Integer.parseInt(args[0]);
    			return n % success == 0;
    		}
    		return true; // rate is 1 (ALWAYS), therefore it's a success 100%.
    	}
    	
    	/**
    	 * Gets a random number within a certain range.
    	 * 
    	 * @param min
    	 *            The minimum number range.
    	 * @param max
    	 *            The maximum number range.
    	 * @return The random number.
    	 */
    	private int getN(int min, int max) {
    		final int n = Math.abs(max - min);
    		return Math.min(min, max) + (n == 0 ? 0 : random.nextInt(n));
    	}
    	
    }
    Here is a dump of the drop attempts using this system for those who want to see the accuracy of it.

    Code:
    Took 0 attempts for: COMMON
    Took 0 attempts for: COMMON
    Took 2 attempts for: COMMON
    Took 0 attempts for: COMMON
    Took 2 attempts for: COMMON
    <----    New Drop    ---->
     
    Took 162 attempts for: UNCOMMON
    Took 8 attempts for: UNCOMMON
    Took 112 attempts for: UNCOMMON
    Took 2 attempts for: UNCOMMON
    Took 39 attempts for: UNCOMMON
    <----    New Drop    ---->
     
    Took 15 attempts for: RARE
    Took 47 attempts for: RARE
    Took 123 attempts for: RARE
    Took 180 attempts for: RARE
    Took 70 attempts for: RARE
    <----    New Drop    ---->
     
    Took 335 attempts for: VERY_RARE
    Took 518 attempts for: VERY_RARE
    Took 1215 attempts for: VERY_RARE
    Took 88 attempts for: VERY_RARE
    Took 123 attempts for: VERY_RARE
    <----    New Drop    ---->
    Why have an Object field if you're only parsing a String object?

    Code:
    
    /**
    	 * Constructs new a {@link NpcDropChance}.
    	 * 
    	 * @param rate
    	 *            The rate {@link Object}.
    	 */
    	private NpcDropChance(String rate) {
    		this.rate = rate;
    	}
    Is what it should look like.
    [Only registered and activated users can see links. ] | [Only registered and activated users can see links. ] | [Only registered and activated users can see links. ] (official dog of rune-server)
    Quote Originally Posted by blakeman8192 View Post
    Keep trying. Quitting is the only true failure.
    Reply With Quote  
     

  8. Thankful user:


  9. #7  
    Registered Member
    Michael P's Avatar
    Join Date
    Dec 2013
    Posts
    1,244
    Thanks given
    488
    Thanks received
    309
    Discord
    View profile
    Rep Power
    644
    Quote Originally Posted by Tyluur View Post
    Why have an Object field if you're only parsing a String object?

    Code:
    
    /**
    	 * Constructs new a {@link NpcDropChance}.
    	 * 
    	 * @param rate
    	 *            The rate {@link Object}.
    	 */
    	private NpcDropChance(String rate) {
    		this.rate = rate;
    	}
    Is what it should look like.
    Yes you can do that, but you will have to modify the succesfull method a little.
    Doesn't really make much of a difference though.
    Reply With Quote  
     

  10. #8  
    Registered Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks given
    23
    Thanks received
    46
    Rep Power
    6
    If you want to use a String design for the rate at least parse it on initiation to local variables: int minRange, int maxRange
    Code:
    public final int minRange, maxRange;
    
    NpcDropChance(Object rate) {
        if(rate instanceof String) {
            String[] s = ((String) rate).split("-");
            minRange = Integer.valueOf(s[0]);
            maxRange = Integer.valueOf(s[1]);
            return;
        }
        minRange = maxRange = (Integer) rate;
    }
    In a heavy populated server my guess is that 'isSucess' will be called very often.
    This will cause for a lot of unneeded overhead each call by checking if the value is in fact a String object, splitting the value, and finally parsing the split results.
    Reply With Quote  
     

  11. Thankful user:


  12. #9  
    Banned Market Banned Market Banned


    Join Date
    Jan 2011
    Age
    23
    Posts
    3,115
    Thanks given
    1,198
    Thanks received
    1,479
    Rep Power
    0
    Quote Originally Posted by Archon View Post
    If you want to use a String design for the rate at least parse it on initiation to local variables: int minRange, int maxRange
    Code:
    public final int minRange, maxRange;
    
    NpcDropChance(Object rate) {
        if(rate instanceof String) {
            String[] s = ((String) rate).split("-");
            minRange = Integer.valueOf(s[0]);
            maxRange = Integer.valueOf(s[1]);
            return;
        }
        minRange = maxRange = (Integer) rate;
    }
    In a heavy populated server my guess is that 'isSucess' will be called very often.
    This will cause for a lot of unneeded overhead each call by checking if the value is in fact a String object, splitting the value, and finally parsing the split results.
    he could've litterally just used varargs and gotten the same effect without all of that unneeded casting and splitting
    Reply With Quote  
     

  13. #10  
    Registered Member
    Michael P's Avatar
    Join Date
    Dec 2013
    Posts
    1,244
    Thanks given
    488
    Thanks received
    309
    Discord
    View profile
    Rep Power
    644
    Quote Originally Posted by lare96 View Post
    he could've litterally just used varargs and gotten the same effect without all of that unneeded casting and splitting
    This is true; not sure why I didn't do that. I kinda just copied it straight from the wiki when I made it, so that's probably why.
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 15
    Last Post: 07-11-2014, 09:28 PM
  2. NPC loot system (npc drops) easier way!
    By wizzyt21 in forum Tutorials
    Replies: 36
    Last Post: 05-08-2010, 10:58 AM
  3. NPC drop Systems.
    By Bando in forum RS2 Server
    Replies: 12
    Last Post: 01-20-2010, 12:03 AM
  4. [Shard] Alternative NPC Drop System
    By Chachi in forum Snippets
    Replies: 6
    Last Post: 11-15-2009, 12:46 AM
  5. Alternate NPC Drop System
    By bloodargon in forum Tutorials
    Replies: 18
    Last Post: 07-15-2009, 07:45 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •