Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display player's attack speed and allow overriding in manual mode #510

Merged
merged 4 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/app/components/player/Bonuses.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ const Bonuses: React.FC = observer(() => {

const player = toJS(store.player);
const monster = toJS(store.monster);
const computedStats = useMemo(() => calculateEquipmentBonusesFromGear(player, monster), [monster, player]);
const computedStats = useMemo(() => calculateEquipmentBonusesFromGear(player, monster), [player, monster]);

return (
<div className="px-4 my-4">
<div className="flex justify-between items-center gap-2">
<h4 className="font-serif font-bold">Bonuses</h4>
{manualMode && (
<button type="button" className="text-xs underline" onClick={() => store.recalculateEquipmentBonusesFromGear()}>
<button type="button" className="text-xs underline" onClick={() => store.updateEquipmentBonuses()}>
Calculate from equipment
</button>
)}
Expand Down
10 changes: 10 additions & 0 deletions src/app/components/player/bonuses/OtherBonuses.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import strength from '@/public/img/bonuses/strength.png';
import rangedStrength from '@/public/img/bonuses/ranged_strength.png';
import magicStrength from '@/public/img/bonuses/magic_strength.png';
import prayer from '@/public/img/tabs/prayer.png';
import attackSpeed from '@/public/img/bonuses/attack_speed.png';
import { EquipmentBonuses } from '@/lib/Equipment';

const OtherBonuses: React.FC<{ computedStats: EquipmentBonuses }> = observer(({ computedStats }) => {
Expand Down Expand Up @@ -49,6 +50,15 @@ const OtherBonuses: React.FC<{ computedStats: EquipmentBonuses }> = observer(({
className={`${(player.bonuses.prayer !== computedStats.bonuses.prayer) ? 'bg-yellow-200 dark:bg-yellow-500' : ''}`}
onChange={(v) => store.updatePlayer({ bonuses: { prayer: v } })}
/>
<AttributeInput
disabled={!prefs.manualMode}
name="Attack Speed"
image={attackSpeed}
value={player.attackSpeed}
className={`${(player.attackSpeed !== computedStats.attackSpeed) ? 'bg-yellow-200 dark:bg-yellow-500' : ''}`}
onChange={(v) => store.updatePlayer({ attackSpeed: v })}
min={1}
/>
</div>
</div>
);
Expand Down
63 changes: 61 additions & 2 deletions src/lib/Equipment.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { EquipmentPiece, Player, PlayerEquipment } from '@/types/Player';
import { Monster } from '@/types/Monster';
import { keys } from '@/utils';
import { BLOWPIPE_IDS, CAST_STANCES, TOMBS_OF_AMASCUT_MONSTER_IDS } from '@/lib/constants';
import {
BLOWPIPE_IDS,
CAST_STANCES,
DEFAULT_ATTACK_SPEED,
TOMBS_OF_AMASCUT_MONSTER_IDS,
} from '@/lib/constants';
import { sum } from 'd3-array';
import equipment from '../../cdn/json/equipment.json';
import generatedEquipmentAliases from './EquipmentAliases';

export type EquipmentBonuses = Pick<Player, 'bonuses' | 'offensive' | 'defensive'>;
export type EquipmentBonuses = Pick<Player, 'bonuses' | 'offensive' | 'defensive' | 'attackSpeed'>;

/**
* All available equipment that a player can equip.
Expand Down Expand Up @@ -224,6 +229,57 @@ export const getCanonicalEquipment = (inputEq: PlayerEquipment) => {
return canonicalized;
};

/**
* Calculates the player's attack speed using current stance and equipment.
*/
export const calculateAttackSpeed = (player: Player, monster: Monster): number => {
let attackSpeed = player.equipment.weapon?.speed || DEFAULT_ATTACK_SPEED;

if (player.style.type === 'ranged' && player.style.stance === 'Rapid') {
attackSpeed -= 1;
} else if (CAST_STANCES.includes(player.style.stance)) {
if (player.equipment.weapon?.name === 'Harmonised nightmare staff'
&& player.spell?.spellbook === 'standard'
&& player.style.stance !== 'Manual Cast') {
attackSpeed = 4;
} else {
attackSpeed = 5;
}
}

// Giant rat (Scurrius)
if (monster.id === 7223 && player.style.stance !== 'Manual Cast') {
if (['Bone mace', 'Bone shortbow', 'Bone staff'].includes(player.equipment.weapon?.name || '')) {
attackSpeed = 1;
}
}

let activeRelic: number;
if (player.style.type === 'ranged') {
activeRelic = player.leagues.five.ranged;
} else if (player.style.type === 'magic') {
activeRelic = player.leagues.five.magic;
} else {
activeRelic = player.leagues.five.melee;
}

if (activeRelic >= 5) {
if (attackSpeed >= 5) {
attackSpeed = Math.trunc(attackSpeed / 2);
} else {
attackSpeed = Math.ceil(attackSpeed / 2);
}
} else if (activeRelic >= 3) {
attackSpeed = Math.trunc(attackSpeed * 4 / 5);
}

if (player.style.type === 'magic' && activeRelic >= 2) {
attackSpeed += player.leagues.five.ticksDelayed;
}

return Math.max(attackSpeed, 1);
};

export const calculateEquipmentBonusesFromGear = (player: Player, monster: Monster): EquipmentBonuses => {
const totals: EquipmentBonuses = {
bonuses: {
Expand All @@ -246,6 +302,7 @@ export const calculateEquipmentBonusesFromGear = (player: Player, monster: Monst
ranged: 0,
magic: 0,
},
attackSpeed: DEFAULT_ATTACK_SPEED,
};

// canonicalize all items first, otherwise ammoApplicability etc calls may return incorrect results later
Expand Down Expand Up @@ -323,6 +380,8 @@ export const calculateEquipmentBonusesFromGear = (player: Player, monster: Monst
totals.bonuses.ranged_str += 1;
}

totals.attackSpeed = calculateAttackSpeed(player, monster);

return totals;
};

Expand Down
48 changes: 5 additions & 43 deletions src/lib/PlayerVsNPCCalc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import { isVampyre, MonsterAttribute } from '@/enums/MonsterAttribute';
import {
ALWAYS_MAX_HIT_MONSTERS,
BA_ATTACKER_MONSTERS,
CAST_STANCES,
DEFAULT_ATTACK_SPEED,
GLOWING_CRYSTAL_IDS,
GUARDIAN_IDS,
HUEYCOATL_PHASE_IDS,
Expand All @@ -47,7 +45,9 @@ import {
import { EquipmentCategory } from '@/enums/EquipmentCategory';
import { DetailKey } from '@/lib/CalcDetails';
import { Factor, MinMax } from '@/lib/Math';
import { AmmoApplicability, ammoApplicability, WEAPON_SPEC_COSTS } from '@/lib/Equipment';
import {
AmmoApplicability, ammoApplicability, calculateAttackSpeed, WEAPON_SPEC_COSTS,
} from '@/lib/Equipment';
import BaseCalc, { CalcOpts, InternalOpts } from '@/lib/BaseCalc';
import { scaleMonster, scaleMonsterHpOnly } from '@/lib/MonsterScaling';
import { CombatStyleType, getRangedDamageType } from '@/types/PlayerCombatStyle';
Expand Down Expand Up @@ -1864,46 +1864,8 @@ export default class PlayerVsNPCCalc extends BaseCalc {
* Returns the player's attack speed.
*/
public getAttackSpeed(): number {
let attackSpeed = this.player.equipment.weapon?.speed || DEFAULT_ATTACK_SPEED;

if (this.player.style.type === 'ranged' && this.player.style.stance === 'Rapid') {
attackSpeed -= 1;
} else if (CAST_STANCES.includes(this.player.style.stance)) {
if (this.player.equipment.weapon?.name === 'Harmonised nightmare staff'
&& this.player.spell?.spellbook === 'standard'
&& this.player.style.stance !== 'Manual Cast') {
attackSpeed = 4;
} else {
attackSpeed = 5;
}
}

// Giant rat (Scurrius)
if (this.monster.id === 7223 && this.player.style.stance !== 'Manual Cast') {
if (['Bone mace', 'Bone shortbow', 'Bone staff'].includes(this.player.equipment.weapon?.name || '')) {
attackSpeed = 1;
}
}

if (this.hasLeaguesMastery('melee', MeleeMastery.MELEE_5)
|| this.hasLeaguesMastery('ranged', RangedMastery.RANGED_5)
|| this.hasLeaguesMastery('magic', MagicMastery.MAGIC_5)) {
if (attackSpeed >= 5) {
attackSpeed = Math.trunc(attackSpeed / 2);
} else {
attackSpeed = Math.ceil(attackSpeed / 2);
}
} else if (this.hasLeaguesMastery('melee', MeleeMastery.MELEE_3)
|| this.hasLeaguesMastery('ranged', RangedMastery.RANGED_3)
|| this.hasLeaguesMastery('magic', MagicMastery.MAGIC_3)) {
attackSpeed = Math.trunc(attackSpeed * 4 / 5);
}

if (this.hasLeaguesMastery('magic', MagicMastery.MAGIC_2)) {
attackSpeed += this.player.leagues.five.ticksDelayed;
}

return attackSpeed;
return this.player.attackSpeed
?? calculateAttackSpeed(this.player, this.monster);
}

public getExpectedAttackSpeed() {
Expand Down
Binary file added src/public/img/bonuses/attack_speed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 9 additions & 12 deletions src/state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { getMonsters, INITIAL_MONSTER_INPUTS } from '@/lib/Monsters';
import { availableEquipment, calculateEquipmentBonusesFromGear } from '@/lib/Equipment';
import { CalcWorker } from '@/worker/CalcWorker';
import { spellByName } from '@/types/Spell';
import { NATURES_REPRISAL_MOCK_ID, NUMBER_OF_LOADOUTS } from '@/lib/constants';
import { DEFAULT_ATTACK_SPEED, NATURES_REPRISAL_MOCK_ID, NUMBER_OF_LOADOUTS } from '@/lib/constants';
import { defaultLeaguesState } from '@/lib/LeaguesV';
import { EquipmentCategory } from './enums/EquipmentCategory';
import {
Expand Down Expand Up @@ -76,6 +76,7 @@ export const generateEmptyPlayer = (name?: string): Player => ({
herblore: 0,
},
equipment: generateInitialEquipment(),
attackSpeed: DEFAULT_ATTACK_SPEED,
prayers: [],
bonuses: {
str: 0,
Expand Down Expand Up @@ -391,19 +392,17 @@ class GlobalState implements State {
this.calcWorker = worker;
}

recalculateEquipmentBonusesFromGear(loadoutIx?: number) {
updateEquipmentBonuses(loadoutIx?: number) {
loadoutIx = loadoutIx !== undefined ? loadoutIx : this.selectedLoadout;

const totals = calculateEquipmentBonusesFromGear(this.loadouts[loadoutIx], this.monster);
this.updatePlayer({
bonuses: totals.bonuses,
offensive: totals.offensive,
defensive: totals.defensive,
}, loadoutIx);
this.loadouts[loadoutIx] = merge(
this.loadouts[loadoutIx],
calculateEquipmentBonusesFromGear(this.loadouts[loadoutIx], this.monster),
);
}

recalculateEquipmentBonusesFromGearAll() {
this.loadouts.forEach((_, i) => this.recalculateEquipmentBonusesFromGear(i));
this.loadouts.forEach((_, i) => this.updateEquipmentBonuses(i));
}

updateUIState(ui: PartialDeep<UI>) {
Expand Down Expand Up @@ -674,9 +673,7 @@ class GlobalState implements State {

this.loadouts[loadoutIx] = merge(this.loadouts[loadoutIx], player);
if (!this.prefs.manualMode) {
if (eq || Object.hasOwn(player, 'spell') || Object.hasOwn(player, 'style')) {
this.recalculateEquipmentBonusesFromGear(loadoutIx);
}
this.updateEquipmentBonuses(loadoutIx);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/types/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export interface Player extends EquipmentStats {
*/
boosts: PlayerSkills;
equipment: PlayerEquipment;
attackSpeed: number;
prayers: Prayer[];
buffs: {
/**
Expand Down
Loading