Improved the performance of drop table rolling

Chromatic Dragon eggs now only drop at a 1/1000 rate if the player has 99 summoning, impossible to obtain otherwise
This commit is contained in:
Ceikry
2023-07-31 03:03:03 +00:00
committed by Ryan
parent bf26faf2a2
commit 6147293431
8 changed files with 125 additions and 32 deletions
@@ -10,12 +10,12 @@ class NPCDropTable : WeightBasedTable() {
return charmDrops.add(element)
}
override fun roll(receiver: Entity?): ArrayList<Item> {
override fun roll(receiver: Entity?, times: Int): ArrayList<Item> {
val items = ArrayList<Item>()
// Charms table is always rolled, and should contain explicit "Nothing"
// entries at the data level to account for the chance to not drop a charm.
items.addAll(charmDrops.roll())
items.addAll(super.roll(receiver))
items.addAll(charmDrops.roll(receiver, times))
items.addAll(super.roll(receiver, times))
return items
}
@@ -38,29 +38,26 @@ open class WeightBasedTable : ArrayList<WeightedItem>() {
}
}
fun roll(receiver: Entity? = null, rollCount: Int) : ArrayList<Item> {
val items = ArrayList<Item>()
for (i in 0 until rollCount)
items.addAll(roll(receiver))
return items
open fun roll(receiver: Entity? = null) : ArrayList<Item> {
return roll(receiver, 1)
}
open fun roll(receiver: Entity? = null): ArrayList<Item>{
val items = ArrayList<WeightedItem>()
items.addAll(guaranteedItems)
open fun roll(receiver: Entity? = null, times: Int = 1): ArrayList<Item>{
val items = ArrayList<WeightedItem>((guaranteedItems.size + 1) * times)
if (size == 1) {
items.add(get(0))
}
else if (isNotEmpty()) {
var rngWeight = RandomFunction.randomDouble(totalWeight)
for (item in this) {
rngWeight -= item.weight
if (rngWeight <= 0) {
items.add(item)
break
for (i in 0 until times) {
items.addAll(guaranteedItems)
if (size == 1) {
items.add(get(0))
} else if (isNotEmpty()) {
var rngWeight = RandomFunction.randomDouble(totalWeight)
for (item in this) {
rngWeight -= item.weight
if (rngWeight <= 0) {
items.add(item)
break
}
}
}
}
@@ -31,7 +31,9 @@ import kotlin.math.floor
* @author Emperor
* @author Ceikry - Kotlin refactoring, general cleanup
*/
abstract class CombatSwingHandler(var type: CombatStyle?, vararg val flags: SwingHandlerFlag) {
abstract class CombatSwingHandler(var type: CombatStyle?) {
var flags: Array<out SwingHandlerFlag> = emptyArray()
constructor(type: CombatStyle?, vararg flags: SwingHandlerFlag) : this(type) { this.flags = flags }
/**
* The mapping of the special attack handlers.
*/
@@ -74,11 +74,17 @@ public final class NPCDropTables {
*/
public void drop(NPC npc, Entity looter) {
Player p = looter instanceof Player ? (Player) looter : null;
ArrayList<Item> drops = table.roll(looter);
ArrayList<Item> drops = table.roll(looter, 1);
npc.behavior.onDropTableRolled(npc, looter, drops);
drops.forEach(item -> createDrop(item,p,npc,npc.getDropLocation()));
}
public List<Item> roll (NPC npc, Entity looter, int times) {
ArrayList<Item> drops = table.roll(looter, times);
npc.behavior.onDropTableRolled(npc, looter, drops);
return drops;
}
/**
* Creates a dropped item.
* @param item The item to drop.
@@ -167,13 +167,8 @@ class DevelopmentCommandSet : CommandSet(Privilege.ADMIN) {
val amount = args[2].toInt()
container.clear()
for(i in 0..amount)
{
val drops = NPCDefinition.forId(npcId).dropTables.table.roll(player)
for(drop in drops) container.add(drop)
}
val drops = NPCDefinition.forId(npcId).dropTables.table.roll(player, amount)
for(drop in drops) container.add(drop, false)
container.open(player)
}
@@ -50,6 +50,7 @@ public class RandomFunction {
* @return true if you hit the roll, false otherwise
*/
public static boolean roll(int chance){
if (chance <= 1) return true;
return random(chance + 1) == chance / 2;
}