Thread: Not dropping charms

Page 1 of 2 12 LastLast
Results 1 to 10 of 12
  1. #1 Not dropping charms 
    Scrub Lord
    _Will's Avatar
    Join Date
    Aug 2012
    Posts
    537
    Thanks given
    60
    Thanks received
    61
    Rep Power
    34
    So I'm making a charms dropping system and I have everything done but I cant figure out how calculate the percentages with not dropping a charm certain times. My class has the percentages pulled from a notepad and they all equal 100. Here's the method where it calculates it:

    Code:
    public static int getCharmType(int[] chances) {
    		int noSpawnRate = chances[0]; //73%
    		int goldRate = chances[1]; //12%
    		int greenRate = chances[2]; //6%
    		int crimRate = chances[3]; //4%
    		int blueRate = chances[4]; //5%
    
    		ArrayList<Integer> possibleCharms = new ArrayList<Integer>();
    		int rand = Utils.random(100);
    		if (rand < noSpawnRate) {
    			return -1;
    		}
    		if (rand < (blueRate) && blueRate != 0)
    			possibleCharms.add(3);
    		if (rand < (crimRate) && crimRate != 0)
    			possibleCharms.add(2);
    		if (rand < (greenRate) && greenRate != 0)
    			possibleCharms.add(1);
    		if (rand < (goldRate) && goldRate != 0)
    			possibleCharms.add(0);
    		if (possibleCharms.isEmpty())
    			return -1;
    		else {
    			Collections.shuffle(possibleCharms);
    			return possibleCharms.get(Utils.random(possibleCharms.size()));
    		}
    	}

    Ex:
    Attached image
    Reply With Quote  
     

  2. #2  
    Registered Member
    Flash's Avatar
    Join Date
    Sep 2015
    Posts
    145
    Thanks given
    19
    Thanks received
    36
    Rep Power
    82
    What's wrong with this?

    Code:
    int[] chances = {73, 12, 6, 4, 5}; //no charm, gold, green, crimson, blue
    
    public static int getCharmType(int[] chances) {
    	int rand = (int) (Math.random() * 100); //random number from 0 to 99
    	for(int c = 0; c < chances.length; c++) {
    		if(rand < chances[c])
    			return c;
    }
    That'll return
    if it's 0-72, return 0 (no charm)
    if it's 73-84, return 1 (gold charm)
    if it's 86-90, return 2 (green charm)
    if it's 91-94, return 3 (crimson charm)
    if it's 95-99, return 4 (blue charm)
    Reply With Quote  
     

  3. #3  
    Scrub Lord
    _Will's Avatar
    Join Date
    Aug 2012
    Posts
    537
    Thanks given
    60
    Thanks received
    61
    Rep Power
    34
    Quote Originally Posted by Flash View Post
    What's wrong with this?

    Code:
    int[] chances = {73, 12, 6, 4, 5}; //no charm, gold, green, crimson, blue
    
    public static int getCharmType(int[] chances) {
    	int rand = (int) (Math.random() * 100); //random number from 0 to 99
    	for(int c = 0; c < chances.length; c++) {
    		if(rand < chances[c])
    			return c;
    }
    That'll return
    if it's 0-72, return 0 (no charm)
    if it's 73-84, return 1 (gold charm)
    if it's 86-90, return 2 (green charm)
    if it's 91-94, return 3 (crimson charm)
    if it's 95-99, return 4 (blue charm)
    Is that accurate though? Like I'm trying to have closest to runescape as I can get it.

    EDIT: For some reason it keeps returning 2 and it drops crimson charms
    Reply With Quote  
     

  4. #4  
    Respected Member


    Kris's Avatar
    Join Date
    Jun 2016
    Age
    26
    Posts
    3,638
    Thanks given
    820
    Thanks received
    2,642
    Rep Power
    5000
    Quote Originally Posted by Flash View Post
    What's wrong with this?

    Code:
    int[] chances = {73, 12, 6, 4, 5}; //no charm, gold, green, crimson, blue
    
    public static int getCharmType(int[] chances) {
    	int rand = (int) (Math.random() * 100); //random number from 0 to 99
    	for(int c = 0; c < chances.length; c++) {
    		if(rand < chances[c])
    			return c;
    }
    That'll return
    if it's 0-72, return 0 (no charm)
    if it's 73-84, return 1 (gold charm)
    if it's 86-90, return 2 (green charm)
    if it's 91-94, return 3 (crimson charm)
    if it's 95-99, return 4 (blue charm)
    I believe you've made a mistake in your formula, verify the logic in it. (In case you're wondering, the mistake lies in the fact that you're not adding the rates up)

    @Will:
    Basically what you need to do is generate a random number between 0 and 100.
    You have either a list, an array or whatever.. of all the rates (You really only need the four, "no charms" is unnecessary).
    From here on, you basically do something similar to:
    Code:
    	public static final int getIndex(final int[] charmsArrayOrList) {
    		final int roll = (int) (Math.random() * 100) + 1;//returns a value from 1 to 100.
    		int chance = 0, index = 0;
    		for (int c : charmsArrayOrList) {
    			if ((chance += c) >= roll)
    				return index;
    			index++;
    		}
    		return -1;//Should never happen unless the value of the array is below 100.
    	}
    If you try the code I provided, you'll get some results like:
    System.out.println(getIndex(new int[] { 73, 12, 6, 4, 5 }));
    ->
    Say roll in the method returns 86, the value returned will be 2 AKA number 6 (73 + 12 -> 85; 85 < 86 therefore continue. 85 + 6 -> 91. 91 > 86 therefore return the index AKA 2.
    Attached image
    Reply With Quote  
     

  5. Thankful users:


  6. #5  
    Registered Member
    hc747's Avatar
    Join Date
    Dec 2013
    Age
    26
    Posts
    1,474
    Thanks given
    3,312
    Thanks received
    691
    Rep Power
    1098
    Quote Originally Posted by Kris View Post
    I believe you've made a mistake in your formula, verify the logic in it. (In case you're wondering, the mistake lies in the fact that you're not adding the rates up)

    @Will:
    Basically what you need to do is generate a random number between 0 and 100.
    You have either a list, an array or whatever.. of all the rates (You really only need the four, "no charms" is unnecessary).
    From here on, you basically do something similar to:
    Code:
        public static final int getIndex(final int[] charmsArrayOrList) {
            final int roll = (int) (Math.random() * 100) + 1;//returns a value from 1 to 100.
            int chance = 0, index = 0;
            for (int c : charmsArrayOrList) {
                if ((chance += c) >= roll)
                    return index;
                index++;
            }
            return -1;//Should never happen unless the value of the array is below 100.
        }
    If you try the code I provided, you'll get some results like:
    System.out.println(getIndex(new int[] { 73, 12, 6, 4, 5 }));
    ->
    Say roll in the method returns 86, the value returned will be 2 AKA number 6 (73 + 12 -> 85; 85 < 86 therefore continue. 85 + 6 -> 91. 91 > 86 therefore return the index AKA 2.
    Lol, the issue is actually to do with the ordering of the array - reverse it and you have your solutions.
    i.e, 6 < 12 ? return 12;
    Reply With Quote  
     

  7. #6  
    Respected Member


    Kris's Avatar
    Join Date
    Jun 2016
    Age
    26
    Posts
    3,638
    Thanks given
    820
    Thanks received
    2,642
    Rep Power
    5000
    Quote Originally Posted by hc747 View Post
    Lol, the issue is actually to do with the ordering of the array - reverse it and you have your solutions.
    i.e, 6 < 12 ? return 12;
    The values do not always go up or down in a fixed length, they can be completely random. E.g. an array of { 15, 72, 8, 4, 1 } is perfectly valid. Each number in there responds to respective charm - it's fixed by index.
    Attached image
    Reply With Quote  
     

  8. #7  
    Registered Member
    hc747's Avatar
    Join Date
    Dec 2013
    Age
    26
    Posts
    1,474
    Thanks given
    3,312
    Thanks received
    691
    Rep Power
    1098
    Quote Originally Posted by Kris View Post
    The values do not always go up or down in a fixed length, they can be completely random. E.g. an array of { 15, 72, 8, 4, 1 } is perfectly valid. Each number in there responds to respective charm - it's fixed by index.
    No, it isn't, lol.

    Say you have two arrays, a {73, 12, 6, 4, 5} and b {4, 5, 6, 12, 73}, and you roll a 4:

    4 < a[0] = 73 (incorrect)
    whereas
    4 < b[1] = 5 (correct)

    Thus, the ordering matters.
    Reply With Quote  
     

  9. #8  
    Respected Member


    Kris's Avatar
    Join Date
    Jun 2016
    Age
    26
    Posts
    3,638
    Thanks given
    820
    Thanks received
    2,642
    Rep Power
    5000
    Quote Originally Posted by hc747 View Post
    No, it isn't, lol.

    Say you have two arrays, a {73, 12, 6, 4, 5} and b {4, 5, 6, 12, 73}, and you roll a 4:

    4 < a[0] = 73 (incorrect)
    whereas
    4 < b[1] = 5 (correct)

    Thus, the ordering matters.
    Alright. Look at your methods here though.
    Let's focus on example b here.
    What is the chance of index 2 getting selected? By your method there, it's only 1% as only value 5 will ever return that. The numbers there are percentages of the given index to be returned in the method.
    I'm not sure we're on the same page here. The method needs to return the index based on the percentages in the array. If we ran 100k simulations on what you provided would show the percentages being completely different than that in the array.
    Attached image
    Reply With Quote  
     

  10. #9  
    Registered Member
    hc747's Avatar
    Join Date
    Dec 2013
    Age
    26
    Posts
    1,474
    Thanks given
    3,312
    Thanks received
    691
    Rep Power
    1098
    Quote Originally Posted by Kris View Post
    Alright. Look at your methods here though.
    Let's focus on example b here.
    What is the chance of index 2 getting selected? By your method there, it's only 1% as only value 5 will ever return that. The numbers there are percentages of the given index to be returned in the method.
    I'm not sure we're on the same page here. The method needs to return the index based on the percentages in the array. If we ran 100k simulations on what you provided would show the percentages being completely different than that in the array.
    You're right - we're not. Anything less than the number 5, thus 1 .. 4 will provide the intended charm at the specified rate.

    Edit: I see your point.
    Reply With Quote  
     

  11. #10  
    Scrub Lord
    _Will's Avatar
    Join Date
    Aug 2012
    Posts
    537
    Thanks given
    60
    Thanks received
    61
    Rep Power
    34
    Quote Originally Posted by kris View Post
    i believe you've made a mistake in your formula, verify the logic in it. (in case you're wondering, the mistake lies in the fact that you're not adding the rates up)

    @will:
    Basically what you need to do is generate a random number between 0 and 100.
    You have either a list, an array or whatever.. Of all the rates (you really only need the four, "no charms" is unnecessary).
    From here on, you basically do something similar to:
    Code:
    	public static final int getindex(final int[] charmsarrayorlist) {
    		final int roll = (int) (math.random() * 100) + 1;//returns a value from 1 to 100.
    		Int chance = 0, index = 0;
    		for (int c : Charmsarrayorlist) {
    			if ((chance += c) >= roll)
    				return index;
    			index++;
    		}
    		return -1;//should never happen unless the value of the array is below 100.
    	}
    if you try the code i provided, you'll get some results like:
    System.out.println(getindex(new int[] { 73, 12, 6, 4, 5 }));
    ->
    say roll in the method returns 86, the value returned will be 2 aka number 6 (73 + 12 -> 85; 85 < 86 therefore continue. 85 + 6 -> 91. 91 > 86 therefore return the index aka 2.
    This works but for some reason it drops gold charms if I change the no drop to 100%

    this is how I pull from a .txt
    Code:
    	public static Drop getCharmDrop(String npcName) {
    		int[] chances = charmDrops.get(npcName.toLowerCase().replace(" ", "_"));
    		if (chances == null)
    			return null;
    		int charmIndex = getIndex(chances);
    		int amount = getCharmAmount(npcName.toLowerCase());
    		if (charmIndex == -1)
    			return null;
    		Drop charm = new Drop(charmIds[charmIndex], amount, amount);
    		return charm;
    	}
    	
    	public static void loadCharmDrops() {
    		try {
    			charmDrops = new HashMap<String, int[]>();
    			Path path = Paths.get(PACKED_PATH);
    			try (Scanner scanner = new Scanner(path, ENCODING.name())) {
    				String npcName = null;
    				int[] charmPerc = new int[4];
    				int noDropPerc;
    
    				while (scanner.hasNextLine()) {
    					String line = scanner.nextLine();
    					//lineNumber++;
    
    					if (line.startsWith("//") || line.isEmpty())
    						continue;
    
    					String[] subs = line.split(":");
    					String[] info = subs[1].split("-");
    
    					npcName = subs[0];
    					noDropPerc = Integer.parseInt(info[0]);
    					charmPerc[0] = Integer.parseInt(info[1]);
    					charmPerc[1] = Integer.parseInt(info[2]);
    					charmPerc[2] = Integer.parseInt(info[3]);
    					charmPerc[3] = Integer.parseInt(info[4]);
    
    					charmDrops.put(npcName, new int[] { noDropPerc, charmPerc[0], charmPerc[1], charmPerc[2], charmPerc[3] });
    				}
    			}
    		} catch (Throwable e) {
    			Logger.handle(e);
    		}
    	}
    and what my txt file looks like
    Code:
    cow:100-0-0-0-0:
    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. [HELP]NPC are not dropping
    By Trivzor in forum Help
    Replies: 3
    Last Post: 05-26-2011, 07:41 PM
  2. npcs not dropping
    By OodlesOfNoodles in forum Help
    Replies: 4
    Last Post: 05-26-2011, 01:02 AM
  3. HyBriD PvP 602 New Npcs Are Not Dropping
    By Trivzor in forum Help
    Replies: 14
    Last Post: 05-10-2011, 07:02 AM
  4. Replies: 11
    Last Post: 12-22-2010, 04:02 AM
  5. [PI]NPC's Not Dropping?
    By James_Akins in forum Help
    Replies: 1
    Last Post: 10-04-2010, 05:34 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •