RuneHive-Game
Loading...
Searching...
No Matches
RangeAccuracy.java
Go to the documentation of this file.
1package com.runehive.game.world.entity.combat.accuracy;
2
3import com.runehive.game.world.entity.combat.CombatType;
4import com.runehive.game.world.entity.combat.FormulaUtils;
5import com.runehive.game.world.entity.mob.Mob;
6import com.runehive.game.world.entity.mob.npc.Npc;
7import com.runehive.game.world.entity.mob.player.Player;
8import com.runehive.game.world.entity.mob.prayer.Prayer;
9import com.runehive.game.world.entity.skill.Skill;
10import com.runehive.game.world.items.containers.equipment.Equipment;
11
12import java.security.SecureRandom;
13
14/**
15 * @Author Origin
16 * 2/22/2023
17 */
18public class RangeAccuracy {
19
20 public static final SecureRandom srand = new SecureRandom();
21
22 public static boolean successful(Mob attacker, Mob defender) {
23 double attackRoll = getAttackRoll(attacker);
24 double defenceRoll = getDefenceRoll(defender);
25
26 double chance;
27 if (attackRoll > defenceRoll)
28 chance = 1D - (defenceRoll + 2D) / (2D * (attackRoll + 1D));
29 else
30 chance = attackRoll / (2D * (defenceRoll + 1D));
31
32 double roll = srand.nextDouble();
33
34
35 return chance > roll;
36 }
37
38 public static double getPrayerAttackBonus(Mob attacker) {
39 double prayerBonus = 1;
40 if (attacker.getStrategy().getCombatType().equals(CombatType.RANGED)) {
41 if (attacker.prayer.isActive(Prayer.SHARP_EYE))
42 prayerBonus *= 1.05D; // 5% range level boost
43 else if (attacker.prayer.isActive(Prayer.HAWK_EYE))
44 prayerBonus *= 1.10D; // 10% range level boost
45 else if (attacker.prayer.isActive(Prayer.EAGLE_EYE))
46 prayerBonus *= 1.15D; // 15% range level boost
47 else if (attacker.prayer.isActive(Prayer.RIGOUR))
48 prayerBonus *= 1.20D; // 20% range level boost
49 }
50 return prayerBonus;
51 }
52
53 public static double getPrayerDefenseBonus(Mob defender) {
54 double prayerBonus = 1;
55 if (defender.prayer.isActive(Prayer.RIGOUR)) {
56 prayerBonus *= 1.25D;
57 }
58 return prayerBonus;
59 }
60
61 public static double getEffectiveLevel(Mob attacker) {
62 double effectiveLevel = getRangeLevel(attacker);
63 if (attacker instanceof Player) {
64 effectiveLevel *= getPrayerAttackBonus(attacker);
65 effectiveLevel = Math.floor(effectiveLevel);
66
67 switch (attacker.getCombat().getFightType().getStyle()) {
68 case ACCURATE -> effectiveLevel += 3;
69 case CONTROLLED -> effectiveLevel += 1;
70 }
71 }
72 effectiveLevel += 8;
73
74 if (attacker instanceof Player player) {
75 if (FormulaUtils.voidRanger(player)) {
76 effectiveLevel *= 1.10;
77 }
78 }
79
80 return Math.floor(effectiveLevel);
81 }
82
83 public static int getRangeLevel(Mob attacker) {
84 int rangeLevel = 1;
85 if (attacker instanceof Npc npc) {
86 int[] skills = npc.definition.getSkills();
87 if (skills != null) {
88 rangeLevel = skills[Skill.RANGED];
89 }
90 } else {
91 rangeLevel = attacker.skills.getLevel(Skill.RANGED);
92 }
93 return rangeLevel;
94 }
95
96 public static int getRangeBonus(Mob attacker) {
97 return attacker.getBonus(Equipment.RANGED_OFFENSE);
98 }
99
100 public static int getDefenceBonus(Mob defender) {
101 return defender.getBonus(Equipment.RANGED_DEFENSE);
102 }
103
104 public static double getAttackRoll(Mob attacker) {
105 double effectiveLevel = getEffectiveLevel(attacker);
106 double bonus = getRangeBonus(attacker);
107
108 double roll = effectiveLevel * (bonus + 64);
109
110 if (attacker instanceof Player player) {
111 final Equipment equipment = player.equipment;
112 if (equipment.contains(25865)
113 || equipment.contains(25867)
114 || equipment.contains(25884)
115 || equipment.contains(25886)
116 || equipment.contains(25888)
117 || equipment.contains(25890)
118 || equipment.contains(25892)
119 || equipment.contains(25894)
120 || equipment.contains(25896)) {
121 if (equipment.contains(23971)) {
122 roll *= 1.05;
123 }
124 if (equipment.contains(23975)) {
125 roll *= 1.15;
126 }
127 if (equipment.contains(23975)) {
128 roll *= 1.10;
129 }
130 }
131 }
132
133 return Math.floor(roll);
134 }
135
136 public static double getDefenceRoll(Mob defender) {
137 double defenceLevel = defender.skills.getLevel(Skill.DEFENCE);
138 double defenceBonus = getDefenceBonus(defender);
139
140 if (defender instanceof Player) {
141 defenceLevel *= getPrayerDefenseBonus(defender);
142
143 switch (defender.getCombat().getFightType().getStyle()) {
144 case DEFENSIVE -> defenceLevel += 3;
145 case CONTROLLED -> defenceLevel += 1;
146 }
147 } else {
148 defenceLevel += 9;
149 }
150
151 return defenceLevel * (defenceBonus + 64);
152 }
153
154}
static boolean successful(Mob attacker, Mob defender)
Handles the mob class.
Definition Mob.java:66
abstract< T extends Mob > CombatStrategy<? super T > getStrategy()
The combat strategy of the mob.
abstract Combat<? extends Mob > getCombat()
The combat of the mob.
Represents a non-player character in the in-game world.
Definition Npc.java:29
This class represents a character controlled by a player.
Definition Player.java:125
boolean isActive(Prayer... prayers)
Checks if all given prayers are active.
Represents a trainable and usable skill.
Definition Skill.java:18
static final int RANGED
The ranged skill id.
Definition Skill.java:33
static final int DEFENCE
The defence skill id.
Definition Skill.java:24
int getLevel(int id)
Gets the level of a skill.
The container that manages the equipment for a player.
boolean contains(int[] bowsWithNoArrowsRequired)