Skip to content

Commit

Permalink
fix: check for time to Runes when computing Time to Power. (#752)
Browse files Browse the repository at this point in the history
* fix: 'extraPower' parameter of TimeToPower() not used correctly

Generalize the parameter to be table whose keys "extra_energy" and
"extra_focus" are checked for extra amounts of the power type when
calculating the number of seconds until that power threshold is
met.

This removes the need to infer the pooled resource/power type.

* refactor: extract getTimeToPowerAt() from getTimeToPowerStateAt()

getTimeToPowerat() is a state method that returns the number of
seconds until a power level is reached for the given power type.

* refactor: import 'huge' as INFINITY in conditions

Save needing to use a constant definition for INFINITY by directly
importing 'huge' as INFINITY, and use in place of 'huge' in
conditions.

* refactor: simplify TimeToPower() condition using getTimeToPowerAt()

Use getTimeToPowerAt() to determine the number of seconds until a
power level is reached instead of repeating the same code with
minor differences in the return type.

* refactor: extract TimeToPowerForSpell() into Spells module

Move Power:getTimetoPowerStateAt() to Spells:TimeToPowerForSpell()
and change all references from the old function to the new one.

Spells:TimeToPowerForSpell() also replaces Power:TimeToPower().

Remove unused Power functions TimeToPower() and hasPowerFor().

* refactor: make TimeToPowerForSpell() honor rune requirements

Runes are a type of power, so check that rune requirements are met
in Spells:TimeToPowerForSpell().

Remove runes check from BestAction module since it is now
encapsulated within Spells:TimeToPowerForSpell().
  • Loading branch information
johnnylam88 authored Dec 19, 2020
1 parent bc3a3e1 commit 3ddb48a
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 165 deletions.
6 changes: 4 additions & 2 deletions src/engine/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,8 @@ export interface AstActionNode
| "usable"
| "target"
| "offgcd"
| "extra_amount"
| "extra_energy"
| "extra_focus"
| "texture"
> {
name: ActionType;
Expand All @@ -877,7 +878,8 @@ const checkActionParameters: NamedParametersCheck<AstActionNode> = {
target: true,
usable: true,
offgcd: true,
extra_amount: true,
extra_energy: true,
extra_focus: true,
texture: true,
};

Expand Down
25 changes: 2 additions & 23 deletions src/engine/best-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ import {
setResultType,
} from "./ast";
import { OvaleCooldownClass } from "../states/Cooldown";
import { OvaleRunesClass } from "../states/Runes";
import { OvaleSpellsClass } from "../states/Spells";
import { isNumber, isString } from "../tools/tools";
import { OvaleClass } from "../Ovale";
import { AceModule } from "@wowts/tsaddon";
import { OvaleGUIDClass } from "./guid";
import { OvalePowerClass } from "../states/Power";
import { OvaleFutureClass } from "../states/Future";
import { OvaleSpellBookClass } from "../states/SpellBook";
import { Profiler, OvaleProfilerClass } from "./profiler";
Expand Down Expand Up @@ -58,13 +56,11 @@ export class OvaleBestActionClass {
private ovaleCooldown: OvaleCooldownClass,
Ovale: OvaleClass,
private OvaleGUID: OvaleGUIDClass,
private OvalePower: OvalePowerClass,
private OvaleFuture: OvaleFutureClass,
private OvaleSpellBook: OvaleSpellBookClass,
ovaleProfiler: OvaleProfilerClass,
ovaleDebug: OvaleDebugClass,
private variables: Variables,
private ovaleRunes: OvaleRunesClass,
private OvaleSpells: OvaleSpellsClass,
private runner: Runner
) {
Expand Down Expand Up @@ -292,37 +288,20 @@ export class OvaleBestActionClass {
result.actionTexture = `Interface\\Icons\\${si.texture}`;
}
if (result.actionCooldownStart && result.actionCooldownDuration) {
const extraPower =
<number>element.cachedParams.named.extra_amount || 0;
// let seconds = OvaleSpells.GetTimeToSpell(spellId, atTime, targetGUID, extraPower);
const timeToCd =
(result.actionCooldownDuration > 0 &&
result.actionCooldownStart +
result.actionCooldownDuration -
atTime) ||
0;
let timeToPower = this.OvalePower.TimeToPower(
const timeToPower = this.OvaleSpells.TimeToPowerForSpell(
spellId,
atTime,
targetGUID,
undefined,
extraPower
element.cachedParams.named
);
const runes = this.ovaleData.GetSpellInfoProperty(
spellId,
atTime,
"runes",
targetGUID
);
if (runes) {
const timeToRunes = this.ovaleRunes.GetRunesCooldown(
atTime,
<number>runes
);
if (timeToPower < timeToRunes) {
timeToPower = timeToRunes;
}
}
if (timeToPower > timeToCd) {
result.actionResourceExtend = timeToPower - timeToCd;
this.tracer.Log(
Expand Down
5 changes: 2 additions & 3 deletions src/ioc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,8 @@ export class IoC {
this.debug,
this.profiler,
this.data,
this.power
this.power,
this.runes,
);
this.bestAction = new OvaleBestActionClass(
this.equipment,
Expand All @@ -351,13 +352,11 @@ export class IoC {
this.cooldown,
this.ovale,
this.guid,
this.power,
this.future,
this.spellBook,
this.profiler,
this.debug,
this.variables,
this.runes,
this.spells,
runner
);
Expand Down
127 changes: 22 additions & 105 deletions src/states/Power.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,23 +465,6 @@ export class OvalePowerClass extends States<PowerState> implements StateModule {
}
};

TimeToPower(
spellId: number,
atTime: number,
targetGUID: string | undefined,
powerType: PowerType | undefined,
extraPower?: number
) {
return this.getTimeToPowerStateAt(
this.GetState(atTime),
spellId,
atTime,
targetGUID,
powerType,
extraPower
);
}

InitializeState() {
for (const [powerType] of kpairs(this.POWER_INFO)) {
this.next.power[powerType] = 0;
Expand Down Expand Up @@ -587,7 +570,7 @@ export class OvalePowerClass extends States<PowerState> implements StateModule {
spellId: number,
powerType: PowerType,
atTime: number,
targetGUID: string,
targetGUID: string | undefined,
maximumCost?: boolean
) {
return this.getPowerCostAt(
Expand Down Expand Up @@ -624,6 +607,7 @@ export class OvalePowerClass extends States<PowerState> implements StateModule {
}
return rate;
}

/**
* Power atTime for the given powerType.
* @param powerType
Expand All @@ -642,19 +626,27 @@ export class OvalePowerClass extends States<PowerState> implements StateModule {
return power;
}

hasPowerFor(
spellId: number,
/**
* Number of seconds until powrLevel is reached atTime for the powerType.
* @param powerLevel
* @param powerType
* @param atTime
*/
getTimeToPowerAt(
state: PowerState,
powerLevel: number,
powerType: PowerType,
atTime: number,
targetGUID?: string
): boolean {
const seconds = this.getTimeToPowerStateAt(
this.GetState(atTime),
spellId,
atTime,
targetGUID,
undefined
);
return (seconds === 0);
): number {
let seconds = INFINITY;
const power = this.getPowerAt(state, powerType, atTime)
if (power < powerLevel) {
const powerRate = this.getPowerRateAt(state, powerType, atTime);
if (powerRate > 0) {
seconds = (powerLevel - power) / powerRate;
}
}
return seconds;
}

/**
Expand Down Expand Up @@ -734,79 +726,4 @@ export class OvalePowerClass extends States<PowerState> implements StateModule {
this.profiler.StopProfiling("OvalePower_PowerCost");
return [spellCost, spellRefund];
}

/**
* How many seconds until there is enough power to use the ability.
* @param spellId
* @param atTime
* @param targetGUID
* @param powerType
* @param extraPower If true, will add this to the cost
*/
private getTimeToPowerStateAt(
state: PowerState,
spellId: number,
atTime: number,
targetGUID: string | undefined,
powerType: PowerType | undefined,
extraPower?: number
): number {
let timeToPower = 0;
const si = this.ovaleData.spellInfo[spellId];
if (si) {
for (const [, powerInfo] of kpairs(this.POWER_INFO)) {
const pType = powerInfo.type;
if (powerType === undefined || powerType == pType) {
let [cost] = this.getPowerCostAt(
state,
spellId,
pType,
atTime,
targetGUID
);
if (cost > 0) {
this.tracer.Log(" Spell ID '%d' has cost of %d %s",
spellId,
cost,
pType
);
if (powerType == pType && extraPower) {
this.tracer.Log(
" Including extra power %d for %s",
extraPower,
pType
);
cost = cost + extraPower;
}
const power = this.getPowerAt(state, pType, atTime);
if (power < cost) {
const powerRate =
this.getPowerRateAt(state, pType, atTime);
if (powerRate > 0) {
const seconds = (cost - power) / powerRate;
this.tracer.Log(
" Requires %f seconds to %d %s",
seconds,
cost,
pType
);
if (timeToPower < seconds) {
timeToPower = seconds;
}
} else {
timeToPower = INFINITY;
break;
}
}
}
}
}
}
this.tracer.Log(
"Spell ID '%d' requires %f seconds for power requirements.",
spellId,
timeToPower
);
return timeToPower;
}
}
Loading

0 comments on commit 3ddb48a

Please sign in to comment.