Skip to content

Commit

Permalink
fix: properly check for known spells in InUsableSpell() (#811)
Browse files Browse the repository at this point in the history
* fix: properly check for known spells in InUsableSpell()

Revert 86439eb and fix the
underlying problem in a different way. That commit used the WoW
IsUsableSpell() API function to determine whether a spell is known,
but that returns false if the spell is genuinely not usable due to
either cooldown or power requirements. Fix this in a better way by
teaching SpellBook.ts:IsKnownSpell() to check for a spell being
known to the player even if it's not in the spellbook by using the
WoW IsSpellKnown() API function.

While here, adjust IsUsableSpell() so that there is only a single
exit point from the function to make the profiler stats work
correctly.
  • Loading branch information
johnnylam88 authored Jan 20, 2021
1 parent 36bb313 commit 305c5bf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
18 changes: 17 additions & 1 deletion src/states/SpellBook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
HasPetSpells,
IsHarmfulSpell,
IsHelpfulSpell,
IsSpellKnown,
BOOKTYPE_PET,
BOOKTYPE_SPELL,
MAX_TALENT_TIERS,
Expand Down Expand Up @@ -391,7 +392,22 @@ export class OvaleSpellBookClass {
return (spellId && this.isHelpful[spellId] && true) || false;
}
IsKnownSpell(spellId: number): boolean {
return (spellId && this.spell[spellId] && true) || false;
/**
* A spell is known if it's in the spellbook, or is a temporary spell
* or action granted by an encounter that may not be in the spellbook.
*/

let isKnown = this.spell[spellId] !== undefined;
if (!isKnown) {
isKnown = IsSpellKnown(spellId) || IsSpellKnown(spellId, true);
if (isKnown) {
this.tracer.Log(
"Spell ID '%s' is not in the spellbook, but is still known.",
spellId
);
}
}
return (isKnown && true) || false;
}
IsKnownTalent(talentId: number): boolean {
return (talentId && this.talentPoints[talentId] && true) || false;
Expand Down
42 changes: 17 additions & 25 deletions src/states/Spells.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { huge as INFINITY } from "@wowts/math";
import { kpairs } from "@wowts/lua";
import { kpairs, tonumber } from "@wowts/lua";
import aceEvent, { AceEvent } from "@wowts/ace_event-3.0";
import {
GetSpellCount,
Expand Down Expand Up @@ -151,57 +151,49 @@ export class OvaleSpellsClass implements StateModule {
targetGUID: string | undefined
): [boolean, boolean] {
this.profiler.StartProfiling("OvaleSpellBook_state_IsUsableSpell");
let [isUsable, noMana] = [true, false]
let isKnown = this.OvaleSpellBook.IsKnownSpell(spellId);
if (!isKnown) {
// check the WOW API if the spell is usable, if it is than we assume that it is a known spell
[isUsable, noMana] = IsUsableSpell(spellId);
isKnown = isUsable;
this.tracer.Log("Checked WoW API for spell %s. Returns [%s, %s]", spellId, isUsable, noMana);
}
let [isUsable, noMana] = [false, false];
const isKnown = this.OvaleSpellBook.IsKnownSpell(spellId);
const si = this.ovaleData.spellInfo[spellId];
if (si && isKnown) {
this.tracer.Log(
"Found spell info about %s (isKnown = %s)",
spellId,
isKnown
);

if (!isKnown) {
this.tracer.Log("Spell ID '%s' is not known.", spellId);
[isUsable, noMana] = [false, false];
} else if (si !== undefined) {
const unusable = this.ovaleData.GetSpellInfoProperty(
spellId,
atTime,
"unusable",
targetGUID
);
if (unusable !== undefined && unusable > 0) {
if (unusable !== undefined && tonumber(unusable) > 0) {
this.tracer.Log(
"Spell ID '%s' is flagged as unusable.",
spellId
);
isUsable = false;
}
if (isUsable) {
[isUsable, noMana] = [false, false];
} else {
const seconds = this.TimeToPowerForSpell(
spellId,
atTime,
targetGUID,
undefined
);
noMana = (seconds > 0);
if (noMana) {
isUsable = false;
if (seconds > 0) {
this.tracer.Log(
"Spell ID '%s' does not have enough power.",
spellId
);
[isUsable, noMana] = [false, true];
} else {
this.tracer.Log(
"Spell ID '%s' passed power requirements.",
"Spell ID '%s' meets power requirements.",
spellId
);
[isUsable, noMana] = [true, false];
}
}
}
} else {
[isUsable, noMana] = IsUsableSpell(spellId);
}
this.profiler.StopProfiling("OvaleSpellBook_state_IsUsableSpell");
return [isUsable, noMana];
}
Expand Down

0 comments on commit 305c5bf

Please sign in to comment.