HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Percentage based drop prize on Mob entity kill

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
killmobdropbasedprizepercentageentity

Problem

I have created a simple percentage-based random prize drop for killing certain mobs.
A drop is basically what the player will get in return for killing a mob, as a reward.

So I have a large list of Mobs saved in an XML file which looks like this:












Basically each NPC has its own ID, so that way we can easily decide what drops does a certain NPC can drop.

So for NPC '1' we have 4 drops, the hardest to get drop is 0.001 (1/1000) and the easiest one is 0.158.

To make the drop system percentage based, I used SecureRandom and the nextFloat() method to generate the base drop rate for the NPC.

After I got the rate, I go through all of the drops that the NPC can drop, and find the drop with the closest drop chance rate to the generated random float rate.

I do this like that:

`/**
* Gets a random drop for NPC by random percentage
* @param npc The NPC id
* @return The drop details
*/
public Drop getRandomDrop(int npc) {
List drops = this.drops.get(npc);

/**
* The random drop rate
*/
float rate = random.nextFloat();

/**
* The closest distance to rate
*/
float bestDistance = Float.MAX_VALUE;

// Closest's drop instance
Drop closest = null;

for (Drop d : drops) {
float rt = d.getChance();
if (rt == rate) {
return d;
}

float distance = Math.abs(rt - rate);
if (distance

But I feel that I can make the drops system more accurate, as in this approach is not accurate enough. I need some reviews and suggestions on my approach.

Solution

/**
 * Gets a random drop for NPC by random percentage
 * @param npc       The NPC id
 * @return  The drop details
 */
public Drop getRandomDrop(int npc) {
    List drops = this.drops.get(npc);

    float roll = random.nextFloat();

    for ( Drop d : drops ) {
        float chance = d.getChance();
        if ( roll < chance ) {
            return d;
        }

        roll -= chance;
    }

    // if no drop was selected, return an empty drop
    return Drop::EMPTY_DROP;
}


This is how I would write it. Note that I changed the random variable to be called roll like a roll of a die. For each drop, it compares the roll to the chance of getting that drop. If the roll is less than the drop rate, then that drop is the loot. Otherwise, reduce the roll by the drop chance (which makes subsequent drops more likely).

This method assumes that the drop chances in the XML add up to 1 or that there is an implicit chance of an empty drop. If you want to always drop and have drop chances that don't add up to 1, you can normalize the rate.

float totalChance = 0.0;
for ( Drop d : drops ) {
    totalChance += d.getChance();
}


and then in the current loop

float chance = d.getChance() / totalChance;


You may still have to do some adjustment to get the rounding right in the case of a maximum random.nextFloat result.

You would set the EMPTY_DROP to be a static class constant representing no loot. Note that this would also be the drop if the NPC's section was missing from the XML file.

Code Snippets

/**
 * Gets a random drop for NPC by random percentage
 * @param npc       The NPC id
 * @return  The drop details
 */
public Drop getRandomDrop(int npc) {
    List<Drop> drops = this.drops.get(npc);

    float roll = random.nextFloat();

    for ( Drop d : drops ) {
        float chance = d.getChance();
        if ( roll < chance ) {
            return d;
        }

        roll -= chance;
    }

    // if no drop was selected, return an empty drop
    return Drop::EMPTY_DROP;
}
float totalChance = 0.0;
for ( Drop d : drops ) {
    totalChance += d.getChance();
}
float chance = d.getChance() / totalChance;

Context

StackExchange Code Review Q#69761, answer score: 3

Revisions (0)

No revisions yet.