-
Notifications
You must be signed in to change notification settings - Fork 25
Expose a withMacro function that lets you carry out some arbitrary action with the given macro #834
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
Open
gausie
wants to merge
7
commits into
main
Choose a base branch
from
with-macro
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c7ee059
Expose a withMacro function that lets you carry out some arbitrary ac…
gausie 18236a8
Merge branch 'main' into with-macro
gausie dfcbe53
Refactor to take inspiration from garbo's withMacro function
gausie 62faa6e
Merge branch 'with-macro' of github.com:loathers/libram into with-macro
gausie dcb8a2e
Merge branch 'main' into with-macro
spaghetti-squash 1d9735b
Update src/combat.ts
gausie 423050e
Merge branch 'main' into with-macro
spaghetti-squash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,10 +13,12 @@ | |
| removeProperty, | ||
| runCombat, | ||
| setAutoAttack, | ||
| setCcs, | ||
| Skill, | ||
| Stat, | ||
| urlEncode, | ||
| visitUrl, | ||
| writeCcs, | ||
| xpath, | ||
| } from "kolmafia"; | ||
| import { getTodaysHolidayWanderers } from "./lib.js"; | ||
|
|
@@ -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) { | ||
| writeCcs(`[default]\n"${macro.toString()}"`, "libram"); | ||
| setCcs("libram"); | ||
| } | ||
|
|
||
| /** | ||
|
Check failure on line 876 in src/combat.ts
|
||
| * 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) { | ||
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
|
@@ -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, "")); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -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 { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While the garbo version extends the hotter and cooler
StrictMacroclass, which we use exclusively, this should probably use the OG