RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
Skill.java
1package com.osroyale.game.world.entity.skill;
2
3import com.osroyale.content.event.EventDispatcher;
4import com.osroyale.content.event.InteractionEvent;
5import com.osroyale.content.event.InteractionEvent.InteractionType;
6import com.osroyale.content.event.InteractionEventListener;
7import com.osroyale.content.event.impl.*;
8import com.osroyale.game.world.entity.mob.player.Player;
9import com.osroyale.util.Stopwatch;
10
11import java.util.function.Function;
12
54
55public class Skill implements InteractionEventListener {
56
58 public static final int ATTACK = 0;
59
61 public static final int DEFENCE = 1;
62
64 public static final int STRENGTH = 2;
65
67 public static final int HITPOINTS = 3;
68
70 public static final int RANGED = 4;
71
73 public static final int PRAYER = 5;
74
76 public static final int MAGIC = 6;
77
79 public static final int COOKING = 7;
80
82 public static final int WOODCUTTING = 8;
83
85 public static final int FLETCHING = 9;
86
88 public static final int FISHING = 10;
89
91 public static final int FIREMAKING = 11;
92
94 public static final int CRAFTING = 12;
95
97 public static final int SMITHING = 13;
98
100 public static final int MINING = 14;
101
103 public static final int HERBLORE = 15;
104
106 public static final int AGILITY = 16;
107
109 public static final int THIEVING = 17;
110
112 public static final int SLAYER = 18;
113
115 public static final int FARMING = 19;
116
118 public static final int RUNECRAFTING = 20;
119
121 public static final int CONSTRUCTION = 21;
122
124 public static final int HUNTER = 22;
125
127 public static final int SKILL_COUNT = 23;
128
130 public static final int TOTAL_SKILL_LEVEL = 99 * SKILL_COUNT;
131
133 private static final String[] SKILL_NAMES = new String[]{
134 /* 00 */ "Attack",
135 /* 01 */ "Defence",
136 /* 02 */ "Strength",
137 /* 03 */ "Hitpoints",
138 /* 04 */ "Ranged",
139 /* 05 */ "Prayer",
140 /* 06 */ "Magic",
141 /* 07 */ "Cooking",
142 /* 08 */ "Woodcutting",
143 /* 09 */ "Fletching",
144 /* 10 */ "Fishing",
145 /* 11 */ "Firemaking",
146 /* 12 */ "Crafting",
147 /* 13 */ "Smithing",
148 /* 14 */ "Mining",
149 /* 15 */ "Herblore",
150 /* 16 */ "Agility",
151 /* 17 */ "Thieving",
152 /* 18 */ "Slayer",
153 /* 19 */ "Farming",
154 /* 20 */ "Runecrafting",
155 /* 21 */ "Construction",
156 /* 22 */ "Hunter",
157 };
158
163 private static final int[] EXP_FOR_LEVEL = {
164 0, 83, 174, 276, 388, 512, 650, 801, 969, 1154, 1358, 1584, 1833, 2107,
165 2411, 2746, 3115, 3523, 3973, 4470, 5018, 5624, 6291, 7028, 7842, 8740,
166 9730, 10824, 12031, 13363, 14833, 16456, 18247, 20224, 22406, 24815, 27473,
167 30408, 33648, 37224, 41171, 45529, 50339, 55649, 61512, 67983, 75127, 83014,
168 91721, 101333, 111945, 123660, 136594, 150872, 166636, 184040, 203254, 224466,
169 247886, 273742, 302288, 333804, 368599, 407015, 449428, 496254, 547953, 605032,
170 668051, 737627, 814445, 899257, 992895, 1096278, 1210421, 1336443, 1475581,
171 1629200, 1798808, 1986068, 2192818, 2421087, 2673114, 2951373, 3258594, 3597792,
172 3972294, 4385776, 4842295, 5346332, 5902831, 6517253, 7195629, 7944614, 8771558,
173 9684577, 10692629, 11805606, 13034431
174 };
175
177 public static final int[][] INTERFACE_DATA = {
178 {4004, 4005},
179 {4008, 4009},
180 {4006, 4007},
181 {4016, 4017},
182 {4010, 4011},
183 {4012, 4013},
184 {4014, 4015},
185 {4034, 4035},
186 {4038, 4039},
187 {4026, 4027},
188 {4032, 4033},
189 {4036, 4037},
190 {4024, 4025},
191 {4030, 4031},
192 {4028, 4029},
193 {4020, 4021},
194 {4018, 4019},
195 {4022, 4023},
196 {12166, 12167},
197 {13926, 13927},
198 {4152, 4153},
199 {24134, 24135},
200 {-1, -1},
201 {-1, -1},
202 {-1, -1}};
203
204 public transient final Stopwatch stopwatch = Stopwatch.start();
205
207 private final int skill;
208
210 private int level;
211
213 private int maxLevel;
214
216 private double experience;
217
218 private boolean doingSkill = false;
219
221 public Skill(int skill, int level, double experience) {
222 this.skill = skill;
223 this.level = level;
224 this.maxLevel = getLevelForExperience(experience);
225 this.experience = experience;
226 }
227
233 public int getSkill() {
234 return skill;
235 }
236
242 public int getLevel() {
243 return level;
244 }
245
251 public int getMaxLevel() {
252 return maxLevel;
253 }
254
260 public double getExperience() {
261 return experience;
262 }
263
269 public int getRoundedExperience() {
270 return (int) experience;
271 }
272
278 public void setLevel(int level) {
279 this.level = level;
280 }
281
287 public void setMaxLevel(int maxLevel) {
288 this.maxLevel = maxLevel;
289 }
290
296 public void setExperience(double experience) {
297 this.experience = experience;
298 }
299
307 public boolean reqLevel(int level) {
308 return this.level >= level;
309 }
310
318 public void modifyLevel(Function<Integer, Integer> function) {
319 modifyLevel(function, 0, maxLevel);
320 }
321
332 public void modifyLevel(Function<Integer, Integer> function, int lowerBounds, int upperBounds) {
333 level = function.apply(level);
334
335 if (level < 0) {
336 level = 0;
337 } else {
338 if (level < lowerBounds) {
339 level = lowerBounds;
340 }
341
342 if (level > upperBounds) {
343 level = upperBounds;
344 }
345 }
346 }
347
353 public void addLevel(int amount) {
354 modifyLevel(level -> level + amount);
355 }
356
362 public void removeLevel(int amount) {
363 modifyLevel(level -> level - amount);
364 }
365
371 public void multiplyLevel(double amount) {
372 modifyLevel(level -> (int) (level * amount));
373 }
374
380 public void divideLevel(double amount) {
381 modifyLevel(level -> amount == 0 ? -level : (int) (level / amount));
382 }
383
391 public double modifyExperience(Function<Double, Double> function) {
392 return modifyExperience(function, 0, 200_000_000);
393 }
394
406 public double modifyExperience(Function<Double, Double> function, int lowerBounds, int upperBounds) {
407 experience = function.apply(experience);
408
409 if (experience < 0) {
410 experience = 0;
411 } else {
412 if (experience < lowerBounds) {
413 experience = lowerBounds;
414 }
415
416 if (experience > upperBounds) {
417 experience = upperBounds;
418 }
419 }
420
421 return experience;
422 }
423
429 public double addExperience(double amount) {
430 return modifyExperience(experience -> experience + amount);
431 }
432
438 public void removeExperience(double amount) {
439 modifyExperience(experience -> experience - amount);
440 }
441
447 public void multiplyExperience(double amount) {
448 modifyExperience(experience -> experience * amount);
449 }
450
456 public void divideExperience(double amount) {
457 modifyExperience(experience -> amount == 0 ? -experience : (experience / amount));
458 }
459
469 private static byte binarySearch(double experience, int min, int max) {
470 int mid = (min + max) / 2;
471 double value = EXP_FOR_LEVEL[mid];
472
473 if (value > experience) {
474 return binarySearch(experience, min, mid - 1);
475 } else if (value == (int) experience || EXP_FOR_LEVEL[mid + 1] > experience) {
476 return (byte) (mid + 1);
477 }
478
479 return binarySearch(experience, mid + 1, max);
480 }
481
483 public static final byte getLevelForExperience(double experience) {
484 if ((int) experience >= EXP_FOR_LEVEL[98]) {
485 return 99;
486 }
487 return binarySearch(experience, 0, 98);
488 }
489
491 public static final int getExperienceForLevel(int level) {
492 if (level >= 99) {
493 return EXP_FOR_LEVEL[98];
494 }
495 if (level < 1) {
496 return 0;
497 }
498 return EXP_FOR_LEVEL[level - 1];
499 }
500
502 public static String getName(int skill) {
503 return SKILL_NAMES[skill];
504 }
505
507 public static Function<Integer, Integer> add(int amount) {
508 return level -> level + amount;
509 }
510
512 public static Function<Integer, Integer> subtract(int amount) {
513 return level -> level - amount;
514 }
515
517 public static Function<Integer, Integer> multiply(double amount) {
518 return level -> (int) (level * amount);
519 }
520
522 public static Function<Integer, Integer> divide(double amount) {
523 return level -> amount == 0 ? 0 : (int) (level / amount);
524 }
525
526 public void setDoingSkill(boolean doingSkill) {
527 this.doingSkill = doingSkill;
528 }
529
530 public boolean isDoingSkill() {
531 return doingSkill;
532 }
533
534 protected double modifier() {
535 return 0;
536 }
537
538 protected boolean clickItem(Player player, ItemInteractionEvent event) {
539 return false;
540 }
541
542 protected boolean clickNpc(Player player, NpcInteractionEvent event) {
543 return false;
544 }
545
546 protected boolean clickObject(Player player, ObjectInteractionEvent event) {
547 return false;
548 }
549
550 protected boolean clickButton(Player player, ClickButtonInteractionEvent event) {
551 return false;
552 }
553
554 protected boolean useItem(Player player, ItemOnItemInteractionEvent event) {
555 return false;
556 }
557
558 protected boolean useItem(Player player, ItemOnObjectInteractionEvent event) {
559 return false;
560 }
561
562 protected boolean itemContainerAction(Player player, ItemContainerInteractionEvent event) {
563 return false;
564 }
565
566 @Override
567 public String toString() {
568 return String.format("Skill[name=%s, id=%s, level=%s, max=%s, experience=%s]", getName(skill), skill, level, maxLevel, experience);
569 }
570
571 @Override
572 public boolean onEvent(Player player, InteractionEvent interactionEvent) {
573 final EventDispatcher dispatcher = new EventDispatcher(interactionEvent);
574 dispatcher.dispatch(InteractionType.CLICK_BUTTON, e -> clickButton(player, (ClickButtonInteractionEvent) e));
575 dispatcher.dispatch(InteractionType.ITEM_ON_ITEM, e -> useItem(player, (ItemOnItemInteractionEvent) e));
576 dispatcher.dispatch(InteractionType.ITEM_ON_OBJECT, e -> useItem(player, (ItemOnObjectInteractionEvent) e));
577
578 dispatcher.dispatch(InteractionType.FIRST_ITEM_CLICK, e -> clickItem(player, (FirstItemClickInteractionEvent) e));
579 dispatcher.dispatch(InteractionType.SECOND_ITEM_CLICK, e -> clickItem(player, (SecondItemClickInteractionEvent) e));
580 dispatcher.dispatch(InteractionType.THIRD_ITEM_CLICK, e -> clickItem(player, (ThirdItemClickInteractionEvent) e));
581
582 dispatcher.dispatch(InteractionType.FIRST_CLICK_NPC, e -> clickNpc(player, (FirstNpcClick) e));
583 dispatcher.dispatch(InteractionType.SECOND_CLICK_NPC, e -> clickNpc(player, (SecondNpcClick) e));
584 dispatcher.dispatch(InteractionType.FIRST_CLICK_OBJECT, e -> clickObject(player, (FirstObjectClick) e));
585 dispatcher.dispatch(InteractionType.SECOND_CLICK_OBJECT, e -> clickObject(player, (SecondObjectClick) e));
586 dispatcher.dispatch(InteractionType.THIRD_CLICK_OBJECT, e -> clickObject(player, (ThirdObjectClick) e));
587 dispatcher.dispatch(InteractionType.ITEM_CONTAINER_INTERACTION_EVENT, e -> itemContainerAction(player, (ItemContainerInteractionEvent) e));
588 return interactionEvent.isHandled();
589 }
590}
static Function< Integer, Integer > subtract(int amount)
Definition Skill.java:512
static Function< Integer, Integer > divide(double amount)
Definition Skill.java:522
static String getName(int skill)
Definition Skill.java:502
Skill(int skill, int level, double experience)
Definition Skill.java:221
static final int getExperienceForLevel(int level)
Definition Skill.java:491
void modifyLevel(Function< Integer, Integer > function)
Definition Skill.java:318
static Function< Integer, Integer > add(int amount)
Definition Skill.java:507
void modifyLevel(Function< Integer, Integer > function, int lowerBounds, int upperBounds)
Definition Skill.java:332
double modifyExperience(Function< Double, Double > function)
Definition Skill.java:391
double modifyExperience(Function< Double, Double > function, int lowerBounds, int upperBounds)
Definition Skill.java:406
static Function< Integer, Integer > multiply(double amount)
Definition Skill.java:517
void setExperience(double experience)
Definition Skill.java:296
static final byte getLevelForExperience(double experience)
Definition Skill.java:483