RuneHive-Game
Loading...
Searching...
No Matches
NpcDropTable.java
Go to the documentation of this file.
1package com.runehive.game.world.entity.mob.npc.drop;
2
3import com.runehive.game.world.entity.mob.player.Player;
4import com.runehive.game.world.entity.mob.player.PlayerRight;
5import com.runehive.game.world.items.ItemDefinition;
6import com.runehive.util.RandomUtils;
7import com.runehive.util.parser.impl.NpcDropParser;
8
9import java.util.LinkedList;
10import java.util.List;
11
12/**
13 * The class which represents a npc drop table.
14 *
15 * @author Michael | Chex
16 */
17public final class NpcDropTable {
18
19 /** The npc ids that share this drop table. */
20 public final int[] npcIds;
21
22 /** Determines if this table has access to the rare drop table. */
23 private final boolean rareDropTable;
24
25 /** The cached array of {@link NpcDrop}s. */
26 public final NpcDrop[] drops;
27
28 public final NpcDrop[][] table = new NpcDrop[5][];
29
30 private int maxRoll = 20_000;
31
32 // common = 1/2
33 // uncommon = 1 / 4
34 // rare = 1/75
35 // very rare = 1/100
36 private static final int[] ROLL_DATA = {
37 /* very rare roll */
38 200,
39
40 /* rare roll */
41 350,
42
43 /* uncommon roll */
44 7000,
45
46 /* common roll */
47 12500,
48
49 /* empty drop roll */
50 200
51 };
52
53 private int[] rollData;
54
55 /** Constructs a new {@link NpcDropTable}. */
56 public NpcDropTable(int[] npcIds, boolean rareDropTable, NpcDrop[] npcDrops, NpcDrop[] always, NpcDrop[] common, NpcDrop[] uncommon, NpcDrop[] rare, NpcDrop[] veryRare) {
57 this.npcIds = npcIds;
58 this.rareDropTable = rareDropTable;
59 this.drops = npcDrops;
61 table[0] = veryRare;
62 table[1] = rare;
63 table[2] = uncommon;
64 table[3] = common;
65 table[4] = always;
66 }
67
68 public List<NpcDrop> generate(Player player, boolean simulated) {
69 LinkedList<NpcDrop> items = new LinkedList<>();
70 int roll = RandomUtils.inclusive(maxRoll);
71 if (player.equipment.hasRow()) {
72 int old = roll;
74
75 if (roll < rollData[0] && old >= rollData[0]) {
76 if (!simulated) {
77 player.message("Very Rare drop table accessed by ring");
78 player.playerAssistant.useROW();
79 }
80 } else if (roll < rollData[1] && old >= rollData[1]) {
81 if (!simulated) {
82 player.message("Rare drop table accessed by ring");
83 player.playerAssistant.useROW();
84 }
85 } else {
86 /* undo the ring effect since both tables weren't accessed */
87 roll = old;
88 }
89 }
90
91 for (int chance = 0, index = 0; index < rollData.length - 1; index++) {
92 chance += rollData[index];
93
94 if (rollData[index] == 0 || roll >= chance) {
95 continue;
96 }
97
98 if (table[index].length > 0) {
99 items.addFirst(RandomUtils.random(table[index]));
100 break;
101 }
102 }
103
104 for (NpcDrop drop : table[4]) {
105 items.addFirst(drop);
106 }
107
108 return items;
109 }
110
111 public void setRollData(int[] rollData) {
112 this.rollData = rollData;
113 maxRoll = 0;
114 for (int roll : rollData) {
115 maxRoll += roll;
116 }
117 }
118
119 public static void main(String[] args) {
120 int npcId = 7241;
121
122 /*
123 *
124 * DONT USE RATES LESS THAN 100!!!
125 *
126 * The ring of wealth works best with values larger than 100
127 * since 3% of 100 is 3, anything lower than 100 will break ROW
128 *
129 */
130
131 int[] customRolls = {
132 /* very rare roll */
133 200,
134
135 /* rare roll */
136 350,
137
138 /* uncommon roll */
139 7000,
140
141 /* common roll */
142 12500,
143
144 /* empty drop roll */
145 200
146 };
147
149 new NpcDropParser().run();
150
152
153 System.out.println();
154 System.out.println("Table lengths (vr, r, uc, c): ");
155 for (int index = 0; index < table.table.length - 1; index++) {
156 System.out.print(table.table[index].length + " ");
157 }
158 System.out.println("\n");
159 System.out.println("--------------------------------------------------");
160 System.out.println("Current rolls: \t \t \t \t Custom rolls:");
161 printRates(table.table, table.rollData, customRolls);
162 System.out.println();
163 }
164
165 private static void printRates(NpcDrop[][] tables, int[] rollData, int[] rollData2) {
166 long maxRoll = 0, maxRoll2 = 0;
167
168 for (int index = 0; index < rollData.length; index++) {
169 maxRoll += rollData[index];
170 maxRoll2 += rollData2[index];
171 }
172
173 System.out.println("--------------------------------------------------");
174 System.out.println("Very Rare ---- " + ((long) rollData[0] * 100_00 / maxRoll) / 100.0 + "% \t\t Very Rare ---- " + ((long) rollData2[0] * 100_00 / maxRoll2) / 100.0 + "%");
175 System.out.println("Rare --------- " + ((long) rollData[1] * 100_00 / maxRoll) / 100.0 + "% \t\t Rare --------- " + ((long) rollData2[1] * 100_00 / maxRoll2) / 100.0 + "%");
176 System.out.println("Uncommon ----- " + ((long) rollData[2] * 100_00 / maxRoll) / 100.0 + "% \t\t Uncommon ----- " + ((long) rollData2[2] * 100_00 / maxRoll2) / 100.0 + "%");
177 System.out.println("Common ------- " + ((long) rollData[3] * 100_00 / maxRoll) / 100.0 + "% \t\t Common ------- " + ((long) rollData2[3] * 100_00 / maxRoll2) / 100.0 + "%");
178 System.out.println("Empty Drop --- " + ((long) rollData[4] * 100_00 / maxRoll) / 100.0 + "% \t\t Empty Drop --- " + ((long) rollData2[4] * 100_00 / maxRoll2) / 100.0 + "%");
179 System.out.println("--------------------------------------------------");
180 System.out.println("Very Rare ---- " + ((long) rollData[0] * 100_00 / maxRoll) / tables[0].length / 100.0 + "% \t\t Very Rare ---- " + ((long) rollData2[0] * 100_00 / maxRoll2) / tables[0].length / tables[0].length / 100.0 + "%");
181 System.out.println("Rare --------- " + ((long) rollData[1] * 100_00 / maxRoll) / tables[1].length / 100.0 + "% \t\t Rare --------- " + ((long) rollData2[1] * 100_00 / maxRoll2) / tables[0].length / tables[1].length / 100.0 + "%");
182 System.out.println("Uncommon ----- " + ((long) rollData[2] * 100_00 / maxRoll) / tables[2].length / 100.0 + "% \t\t Uncommon ----- " + ((long) rollData2[2] * 100_00 / maxRoll2) / tables[0].length / tables[2].length / 100.0 + "%");
183 System.out.println("Common ------- " + ((long) rollData[3] * 100_00 / maxRoll) / tables[3].length / 100.0 + "% \t\t Common ------- " + ((long) rollData2[3] * 100_00 / maxRoll2) / tables[0].length / tables[3].length / 100.0 + "%");
184 System.out.println("--------------------------------------------------");
185 }
186
187}
The manager class which holds the static entries of drop tables and has a method to execute a drop ta...
static final Map< Integer, NpcDropTable > NPC_DROPS
The collection of npc ids by their representative drop tables.
final NpcDrop[] drops
The cached array of NpcDrops.
final boolean rareDropTable
Determines if this table has access to the rare drop table.
static void printRates(NpcDrop[][] tables, int[] rollData, int[] rollData2)
final int[] npcIds
The npc ids that share this drop table.
List< NpcDrop > generate(Player player, boolean simulated)
NpcDropTable(int[] npcIds, boolean rareDropTable, NpcDrop[] npcDrops, NpcDrop[] always, NpcDrop[] common, NpcDrop[] uncommon, NpcDrop[] rare, NpcDrop[] veryRare)
Constructs a new NpcDropTable.
This class represents a character controlled by a player.
Definition Player.java:125
Represents all of an in-game Item's attributes.
A static-util class that provides additional functionality for generating pseudo-random numbers.
static int inclusive(int min, int max)
Returns a pseudo-random int value between inclusive min and inclusive max.
static< T > T random(T[] array)
Pseudo-randomly retrieves a element from array.
static int dropRateIncrease(Player player, int roll)