Skip to content

Instantly share code, notes, and snippets.

@Ddemon26
Created November 9, 2024 07:31
Show Gist options
  • Save Ddemon26/3bec22a67bec99ca903f82ffab2c76c9 to your computer and use it in GitHub Desktop.
Save Ddemon26/3bec22a67bec99ca903f82ffab2c76c9 to your computer and use it in GitHub Desktop.
OSRS combat formula
using System;
using UnityEngine;
namespace TCS.CombatFormula {
public static class CombatFormula {
public static int CalculateEffectiveStrength(int combatLevels, float prayerMultiplier = 1, float strengthBoost = 1) {
/*
*********Step 1: Calculate the Effective Strength Level*********
Formula: Effective Strength = floor((Base Strength + Strength Boost) * Prayer Bonus) + 8
To calculate your effective strength level:
Add your Strength level and any Strength level boosts.
Multiply the sum by the prayer bonus.
Round down to the nearest integer.
Add 8 to the result.
*/
int baseStrength = combatLevels;
float effectiveStrength = ((baseStrength + strengthBoost) * prayerMultiplier) + 8;
Debug.Log($"Effective Strength: {effectiveStrength}");
return (int)effectiveStrength;
}
public static double CalculateMaximumHit(int effectiveStrength, int stats) {
/*
*********Step 2: Calculate the Maximum Hit*********
Formula: Maximum Hit = floor((Effective Strength * (Equipment Bonus + 64) + 320) / 640)
The maximum hit is the largest hit you can possibly do with the current setup.
To calculate your maximum hit:
Effective strength level
Multiply by (Equipment Strength bonus + 64)
Add 320
Divide by 640
Round down to nearest integer
The Equipment Strength bonus can be found in the Equipment Stats window under 'Other bonuses'.
*/
int equipmentBonus = stats;
// ReSharper disable once PossibleLossOfFraction
double maximumHit = Math.Floor((double)((effectiveStrength * (equipmentBonus + 64) + 320) / 640));
Debug.Log($"Maximum Hit: {maximumHit}");
return maximumHit;
}
public static int CalculateEffectiveAttack(int levels, float prayerMultiplier = 1, float attackBoost = 1) {
/*
*********Step 3: Calculate the Effective Attack Level*********
Formula: Effective Attack = floor((Base Attack + Attack Boost) * Prayer Bonus) + 8
Effective level is affected by your current attack level multiplied by prayers used and skill level boosts.
To calculate your effective attack level:
(Attack level + Attack level boost) * prayer bonus
Round down to nearest integer
+8
*/
int baseAttack = levels;
float effectiveAttack = ((baseAttack + attackBoost) * prayerMultiplier) + 8;
Debug.Log($"Effective Attack: {effectiveAttack}");
return (int)effectiveAttack;
}
public static int CalculateEnemyAttackRoll(int effectiveAttack, int attackBonus) {
/*
*********Step 4: Calculate the Attack Roll*********
Formula: Attack Roll = Effective Attack * (Equipment Attack Bonus + 64)
Effective attack level * (Equipment Attack bonus + 64)
Multiply by target-specific gear bonus
Round down to nearest integer
The Equipment Attack bonus is labelled Stab, Slash, Crush. Use the value of whichever attack type you are using.
*/
int equipmentAttackBonus = attackBonus;
var attackRoll = (int)Math.Floor((double)(effectiveAttack * (equipmentAttackBonus + 64)));
Debug.Log($"Attack Roll: {attackRoll}");
return attackRoll;
}
public static int CalculatePlayerAttackRoll(int effectiveAttack, int attackBonus) {
/*
*********Step 4: Calculate the Attack Roll*********
Formula: Attack Roll = Effective Attack * (Equipment Attack Bonus + 64)
Effective attack level * (Equipment Attack bonus + 64)
Multiply by target-specific gear bonus
Round down to nearest integer
The Equipment Attack bonus is labelled Stab, Slash, Crush. Use the value of whichever attack type you are using.
*/
int equipmentAttackBonus = attackBonus;
var attackRoll = (int)Math.Floor((double)(effectiveAttack * (equipmentAttackBonus + 64)));
Debug.Log($"Attack Roll: {attackRoll}");
return attackRoll;
}
public static int CalculatePlayerDefenceRoll(int combatLevels, int stats) {
int targetDefenceLevel = combatLevels;
int targetStyleDefenceBonus = stats;
// Effective Defence is calculated similarly to the aforementioned effective Attack level.
// Note that Void Knight set effects do not multiply effective Defence level.
// Target's style defence bonus refers to its stab, slash, or crush defensive stats, it depends on the attack type used.
// If the target is a player, use the formula:
// Def roll = Effective Defence * (Target Style Defence Bonus + 64)
// had to add *10 to match the formula
int defenceRoll = (int)Math.Floor((double)(targetDefenceLevel * (targetStyleDefenceBonus + 64))) * 10;
Debug.Log($"Player Defence Roll: {defenceRoll}");
return defenceRoll;
}
public static int CalculateEnemyDefenceRoll(int combatLevels, int stats) {
int targetDefenceLevel = combatLevels;
int targetStyleDefenceBonus = stats;
// Effective Defence is calculated similarly to the aforementioned effective Attack level.
// Note that Void Knight set effects do not multiply effective Defence level.
// Target's style defence bonus refers to its stab, slash, or crush defensive stats, it depends on the attack type used.
// For Monsters use the formula:
// Def roll = (Target Defence level + 9) * (Target Style Defence Bonus + 64)
var defenceRoll = (int)Math.Floor((double)((targetDefenceLevel + 9) * (targetStyleDefenceBonus + 64)));
Debug.Log($"Enemy Defence Roll: {defenceRoll}");
return defenceRoll;
}
public static float CalculateHitChance(int attackRoll, int defenceRoll) {
/*
If Attack Roll > Defence Roll:
Hit Chance = 1 − (Def Roll + 2) / (2 * (Attack Roll + 1))
Else:
Hit Chance = Attack Roll / (2 * (Def Roll + 1))
*/
float hitChance = attackRoll > defenceRoll
? 1 - (defenceRoll + 2) / (2f * (attackRoll + 1))
: attackRoll / (2 * (defenceRoll + 1f));
Debug.Log($"Hit Chance: {hitChance}");
return hitChance;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment