RuneHive-Game
Loading...
Searching...
No Matches
MagicAccuracy.java
Go to the documentation of this file.
1package com.runehive.game.world.entity.combat.accuracy;
2
3import com.runehive.game.world.entity.combat.FormulaUtils;
4import com.runehive.game.world.entity.mob.Mob;
5import com.runehive.game.world.entity.mob.npc.Npc;
6import com.runehive.game.world.entity.mob.player.Player;
7import com.runehive.game.world.entity.mob.prayer.Prayer;
8import com.runehive.game.world.entity.skill.Skill;
9import com.runehive.game.world.items.containers.equipment.Equipment;
10
11import java.security.SecureRandom;
12
13/**
14 * @Author Origin
15 * 2/22/2023
16 */
17public class MagicAccuracy {
18
19 public static final SecureRandom srand = new SecureRandom();
20
21 public static boolean successful(Mob attacker, Mob defender) {
22 double attackRoll = getAttackRoll(attacker);
23 if (attacker instanceof Player player) {
24 if (FormulaUtils.voidMagic(player)) {
25 attackRoll *= 1.45;
26 }
27 }
28
29 double defenceRoll = getDefenceRoll(defender);
30
31 double chance;
32 if (attackRoll > defenceRoll)
33 chance = 1D - (defenceRoll + 2D) / (2D * (attackRoll + 1D));
34 else
35 chance = attackRoll / (2D * (defenceRoll + 1D));
36
37 double roll = srand.nextDouble();
38 return chance > roll;
39 }
40
41 public static int getAttackBonus(Mob attacker) {
42 return attacker.getBonus(Equipment.MAGIC_OFFENSE);
43 }
44
45 public static int getDefenceBonus(Mob defender) {
46 return defender.getBonus(Equipment.MAGIC_DEFENSE);
47 }
48
49 public static double getDefenceRoll(Mob defender) {
50 double magicLevel = getMagicLevel(defender);
51 double defenceBonus = getDefenceBonus(defender);
52
53 double effectiveLevel;
54 if (defender instanceof Player) {
55 magicLevel *= getPrayerBonusDefender(defender);
56
57 double defenceLevel = defender.skills.getLevel(Skill.DEFENCE);
58 switch (defender.getCombat().getFightType().getStyle()) {
59 case DEFENSIVE -> defenceLevel += 3;
60 case CONTROLLED -> defenceLevel += 1;
61 }
62
63 effectiveLevel = magicLevel * 0.7 + defenceLevel * 0.3 + 8;
64 } else {
65 effectiveLevel = magicLevel + 9;
66 }
67
68 return effectiveLevel * (defenceBonus + 64);
69 }
70
71 public static int getMagicLevel(Mob mob) {
72 int magicLevel = 1;
73 if (mob instanceof Npc npc) {
74 int[] skills = npc.definition.getSkills();
75 if (skills != null) {
76 magicLevel = skills[Skill.MAGIC];
77 }
78 } else {
79 magicLevel = mob.skills.getLevel(Skill.MAGIC);
80 }
81 return magicLevel;
82 }
83
84 public static double getPrayerBonus(Mob attacker) {
85 double prayerBonus = 1;
86 if (attacker.prayer.isActive(Prayer.MYSTIC_WILL))
87 prayerBonus *= 1.05D; // 5% magic level boost
88 else if (attacker.prayer.isActive(Prayer.MYSTIC_LORE))
89 prayerBonus *= 1.10D; // 10% magic level boost
90 else if (attacker.prayer.isActive(Prayer.MYSTIC_MIGHT))
91 prayerBonus *= 1.15D; // 15% magic level boost
92 else if (attacker.prayer.isActive(Prayer.AUGURY))
93 prayerBonus *= 1.25D; // 25% magic level boost
94 return prayerBonus;
95 }
96
97 public static double getPrayerBonusDefender(Mob defender) {
98 double prayerBonus = 1;
99 if (defender.prayer.isActive(Prayer.AUGURY))
100 prayerBonus *= 1.25D;
101 return prayerBonus;
102 }
103
104 public static double getAttackRoll(Mob attacker) {
105 double magicLevel = getMagicLevel(attacker);
106 double attackBonus = getAttackBonus(attacker);
107
108 double effectiveLevel;
109 if (attacker instanceof Player) {
110 effectiveLevel = magicLevel * getPrayerBonus(attacker) + 8;
111 } else {
112 effectiveLevel = magicLevel + 9;
113 }
114
115 return effectiveLevel * (attackBonus + 64);
116 }
117
118}
static boolean successful(Mob attacker, Mob defender)
Handles the mob class.
Definition Mob.java:66
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 DEFENCE
The defence skill id.
Definition Skill.java:24
static final int MAGIC
The magic skill id.
Definition Skill.java:39
int getLevel(int id)
Gets the level of a skill.
The container that manages the equipment for a player.