Skip to content
Open
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
108 changes: 90 additions & 18 deletions src/combat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
removeProperty,
runCombat,
setAutoAttack,
setCcs,
Skill,
Stat,
urlEncode,
visitUrl,
writeCcs,
xpath,
} from "kolmafia";
import { getTodaysHolidayWanderers } from "./lib.js";
Expand Down Expand Up @@ -862,6 +864,89 @@
}
}

/**
* Create and set a CCS file from a given Macro
* @param macro Macro to set as CSS
*/
export function makeCcs<M extends StrictMacro>(macro: M) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export function makeCcs<M extends StrictMacro>(macro: M) {
export function makeCcs<M extends Macro>(macro: M) {

While the garbo version extends the hotter and cooler StrictMacro class, which we use exclusively, this should probably use the OG

writeCcs(`[default]\n"${macro.toString()}"`, "libram");
setCcs("libram");
}

/**

Check failure on line 876 in src/combat.ts

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @param "combatParams" declaration
* Run a combat initiated by a given action, handling multi-fights and choice adventures after combat.
* @param initiateCombatAction Callback that initiates the combat to be run.
* @returns Passed-through value from the callback.
*/
export function runCombatBy<T>(initiateCombatAction: () => T, ...combatParams: CombatParams) {

Check failure on line 881 in src/combat.ts

View workflow job for this annotation

GitHub Actions / testBuild

Cannot find name 'CombatParams'.
try {
const result = initiateCombatAction();
while (inMultiFight()) runCombat(...combatParams);
if (choiceFollowsFight()) visitUrl("choice.php");
return result;
} catch (e) {
throw `Combat exception! Last macro error: ${get(
"lastMacroError",
)}. Exception ${e}.`;
}
}

/**
* Attempt to perform a nonstandard combat-starting Action with a Macro
* @param autoMacro The macro to use as an autoattack.
* @param macro The macro to use if the autoattack fails or finishes.
* @param action The combat-starting action to attempt
* @returns The output of your specified action function (typically void)
*/
export function withMacro<T, M extends StrictMacro>(
autoMacro: M,
macro: M,
action: () => T,
): T;
/**
* Attempt to perform a nonstandard combat-starting Action with a Macro
* @param macro The Macro to attempt to use
* @param action The combat-starting action to attempt
* @param tryAuto Whether or not we should try to resolve the combat with an autoattack; autoattack macros can fail against special monsters, and thus we have to submit a macro via CCS regardless.
* @returns The output of your specified action function (typically void)
*/
export function withMacro<T, M extends StrictMacro>(
macro: M,
action: () => T,
tryAuto?: boolean,
): T;
// eslint-disable-next-line jsdoc/require-jsdoc
export function withMacro<T, M extends StrictMacro>(
macro: M,
macroOrAction: M | (() => T),
actionOrTryAuto: (() => T) | boolean = false,
): T {
if (getAutoAttack() !== 0) setAutoAttack(0);

// If we have two macros set, or if we have tryAuto, set an autoattack
if (macroOrAction instanceof Macro || actionOrTryAuto === true) {
macro.setAutoAttack();
}

// Make a CCS of the macro or, if we have two, the second macro.
makeCcs(macroOrAction instanceof Macro ? macroOrAction : macro);
Comment on lines +927 to +932
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The overloads bamboozled me a little bit but I trust TS that this is correct


// Determine the action from the overload
let action;
if (typeof macroOrAction === "function") {
action = macroOrAction;
} else if (typeof actionOrTryAuto === "function") {
action = actionOrTryAuto;
} else {
throw new Error("No action provided to withMacro");
}
try {
return runCombatBy(action);
} finally {
setAutoAttack(0);
}
}

/**
* Adventure in a location and handle all combats with a given macro.
* To use this function you will need to create a consult script that runs Macro.load().submit() and a CCS that calls that consult script.
Expand All @@ -872,15 +957,7 @@
* @param macro Macro to execute.
*/
export function adventureMacro(loc: Location, macro: Macro): void {
macro.save();
setAutoAttack(0);
try {
adv1(loc, 0, "");
while (inMultiFight()) runCombat();
if (choiceFollowsFight()) visitUrl("choice.php");
} finally {
Macro.clearSaved();
}
withMacro(macro, () => adv1(loc, 0, ""));
}

/**
Expand All @@ -898,16 +975,11 @@
autoMacro: Macro,
nextMacro: Macro | null = null,
): void {
nextMacro = nextMacro ?? Macro.abort();
autoMacro.setAutoAttack();
nextMacro.save();
try {
adv1(loc, 0, "");
while (inMultiFight()) runCombat();
if (choiceFollowsFight()) visitUrl("choice.php");
} finally {
Macro.clearSaved();
if (nextMacro) {
withMacro(autoMacro, nextMacro, () => adv1(loc, 0, ""));
return;
}
withMacro(autoMacro, () => adv1(loc, 0, ""));
}

export class StrictMacro extends Macro {
Expand Down
Loading