1package com.runehive.util.chance;
3import com.runehive.game.world.items.Item;
5import java.util.LinkedList;
35 private final List<WeightedObject<T>>
objects;
44 objects.sort((first, second) -> (
int) Math.signum(second.getWeight() - first.getWeight()));
49 this.objects =
new LinkedList<>();
54 public final void add(
double weight, T t) {
57 objects.sort((first, second) -> (
int) Math.signum(second.getWeight() - first.getWeight()));
67 double rnd = Math.random() *
sum;
71 hit += obj.getWeight();
78 throw new AssertionError(
"The random number [" + rnd +
"] is too large!");
83 if (boost <= 0 || boost > 1) {
84 throw new IllegalArgumentException(
"Boost is outside of the domain: (0, 1]");
87 double rnd = Math.random() *
sum;
91 hit += obj.getWeight() + boost;
93 if ((
int) (hit * (1 + boost)) >= (
int) rnd) {
98 throw new AssertionError(
"The random number [" + rnd +
"] is too large!");
105 array[count] = (
Item) obj.get();
The container class that represents an item that can be interacted with.
double sum
The sum of the weights.
final void add(double weight, T t)
Adds a new WeightedObject to the #object list.
Chance()
Creates a new instance of the class.
final void add(ChanceType type, T t)
Adds a new WeightedObject to the #object list.
WeightedObject< T > next(double boost)
Generates a WeightedObject.
T next()
Generates a WeightedObject.
Chance(List< WeightedObject< T > > objects)
Creates a new instance of the class.
final List< WeightedObject< T > > objects
The list of weighted object.
An item with a common chance.
ChanceType(double weight)
Represents a weighted object.
double getWeight()
Gets the object's weight.