RuneHive-Game
Loading...
Searching...
No Matches
com.runehive.util.chance.Chance< T > Class Template Reference

Handles a random chance. More...

Inheritance diagram for com.runehive.util.chance.Chance< T >:

Classes

enum  ChanceType

Public Member Functions

final void add (ChanceType type, T t)
 Adds a new WeightedObject to the #object list.
final void add (double weight, T t)
 Adds a new WeightedObject to the #object list.
 Chance ()
 Creates a new instance of the class.
 Chance (List< WeightedObject< T > > objects)
 Creates a new instance of the class.
next ()
 Generates a WeightedObject.
WeightedObject< T > next (double boost)
 Generates a WeightedObject.
Item[] toItemArray ()
String toString ()

Private Attributes

final List< WeightedObject< T > > objects
 The list of weighted object.
double sum
 The sum of the weights.

Detailed Description

Handles a random chance.

Parameters
<T>- The representation type.
Author
Michael | Chex

Definition at line 14 of file Chance.java.

Constructor & Destructor Documentation

◆ Chance() [1/2]

Creates a new instance of the class.

Definition at line 41 of file Chance.java.

41 {
42 this.objects = objects;
43 sum = objects.stream().mapToDouble(WeightedObject::getWeight).sum();
44 objects.sort((first, second) -> (int) Math.signum(second.getWeight() - first.getWeight()));
45 }

References com.runehive.util.chance.WeightedObject< T >.getWeight(), objects, and sum.

Here is the call graph for this function:

◆ Chance() [2/2]

Creates a new instance of the class.

Definition at line 48 of file Chance.java.

48 {
49 this.objects = new LinkedList<>();
50 sum = 0;
51 }

References sum.

Member Function Documentation

◆ add() [1/2]

final void com.runehive.util.chance.Chance< T >.add ( ChanceType type,
T t )

Adds a new WeightedObject to the #object list.

Definition at line 61 of file Chance.java.

61 {
62 add(type.getWeight(), t);
63 }

References add(), and com.runehive.util.chance.Chance< T >.ChanceType.getWeight().

Here is the call graph for this function:

◆ add() [2/2]

final void com.runehive.util.chance.Chance< T >.add ( double weight,
T t )

Adds a new WeightedObject to the #object list.

Definition at line 54 of file Chance.java.

54 {
55 objects.add(new WeightedChance<>(weight, t));
56 sum += weight;
57 objects.sort((first, second) -> (int) Math.signum(second.getWeight() - first.getWeight()));
58 }

References objects, and sum.

Referenced by add().

Here is the caller graph for this function:

◆ next() [1/2]

Generates a WeightedObject.

Definition at line 66 of file Chance.java.

66 {
67 double rnd = Math.random() * sum;
68 double hit = 0;
69
70 for (WeightedObject<T> obj : objects) {
71 hit += obj.getWeight();
72
73 if (hit >= rnd) {
74 return obj.get();
75 }
76 }
77
78 throw new AssertionError("The random number [" + rnd + "] is too large!");
79 }

References objects, and sum.

◆ next() [2/2]

WeightedObject< T > com.runehive.util.chance.Chance< T >.next ( double boost)

Generates a WeightedObject.

Definition at line 82 of file Chance.java.

82 {
83 if (boost <= 0 || boost > 1) {
84 throw new IllegalArgumentException("Boost is outside of the domain: (0, 1]");
85 }
86
87 double rnd = Math.random() * sum;
88 double hit = 0;
89
90 for (WeightedObject<T> obj : objects) {
91 hit += obj.getWeight() + boost;
92
93 if ((int) (hit * (1 + boost)) >= (int) rnd) {
94 return obj;
95 }
96 }
97
98 throw new AssertionError("The random number [" + rnd + "] is too large!");
99 }

References objects, and sum.

◆ toItemArray()

Item[] com.runehive.util.chance.Chance< T >.toItemArray ( )

Definition at line 101 of file Chance.java.

101 {
102 int count = 0;
103 Item[] array = new Item[objects.size()];
104 for (WeightedObject<T> obj : objects) {
105 array[count] = (Item) obj.get();
106 count++;
107 }
108 return array;
109 }

References objects.

◆ toString()

String com.runehive.util.chance.Chance< T >.toString ( )

Definition at line 112 of file Chance.java.

112 {
113 return objects.toString();
114 }

References objects.

Member Data Documentation

◆ objects

final List<WeightedObject<T> > com.runehive.util.chance.Chance< T >.objects
private

The list of weighted object.

Definition at line 35 of file Chance.java.

Referenced by add(), Chance(), next(), next(), toItemArray(), and toString().

◆ sum

double com.runehive.util.chance.Chance< T >.sum
private

The sum of the weights.

Definition at line 38 of file Chance.java.

Referenced by add(), Chance(), Chance(), next(), and next().


The documentation for this class was generated from the following file: