Macros, in my Consult script? It's more likely than you think.

StDoodle

Minion
(NB: I felt like telling a story. Please keep this in mind. Also, I greatly appreciate every bit of work that has gone into KoLmafia; please don't take any of this as whining or bitching. Just lousy storytelling. :p)

Once upon a time, shortly after combat macros were introduced into KoL, jasonharper did some very snazzy implementation of their use by KoLmafia. We rejoiced at the savings in real-life time & server hits, but it didn't take long for us to ask "so when can we call or generate a macro from a consult script?" We were told that for various reasons, this wouldn't be a simple task, so it would have to go on the back burner. Alas, both jasonharper & Veracity have found their KoL(mafia) time at a greater premium than before, and so-called "employScripts" have been indefinitely shelved.

But fear not! For I have dragged from the darkness a suitable method of "faking" such an employ script.

The basic concept is fairly simple; in a consult script, you have access to basically all of the same functions as you normally would in an ash script. In fact, it isn't at all unheard of to use visit_url() to scrape page text before figuring out what your next action should be. So why not make use of CDM's "secret" ability for scripters to include a macro in their URL?

Basically, you want to use:
Code:
visit_url("fight.php?action=macro&macrotext=" + macro())
where macro() is a function that builds your macro & returns it as a string. It's actually simpler than one might think; KoLmafia automatically does the necessary URL encoding needed, so one doesn't even need to replace spaces with "+" or any of that junk. Just type your macro with a semicolon between what would normally be the different lines in your macro (remembering of course that predicate lines need to be kept together), and voila! it works.

Ok, perhaps it isn't THAT easy. The ability to use "&&" and "||" in your macro is quite useful. However, I couldn't for the life of me figure out what format the ampersand needed to be in inside of the script such that it would make it to KoL as "&&". In a fit of despair & frustration, I tried something that I THOUGHT was completely wacky; "ANDAND" as plain text. Low and behold, it worked! I suspect "OROR" would work as well, though I have yet to test it.

One more little caveat to keep in mind; KoLmafia's CCS doesn't exactly play nicely with a consult script that generates a macro and uses visit_url() to run it. If you want, for example, to run your consult script, come back to your CCS, perhaps do other actions, etc., you'll find that this won't work; KoLmafia is pretty much "lost" once you've done a macro from inside an ash script. To deal with this, it's best to handle the entire combat from within the single consult.

It IS possible to run several macros; I've tested a consult script that generates one macro, submits it, then generates another macro and submits it as well, and it worked perfectly. Just realize that if, for any reason, combat has not finished when your script ends, KoLmafia will almost certainly sit out the rest of your combat, waiting for manual input.

Ok, now that I have that out there, please let me know what I've missed, what else we need to keep in mind, etc. I'd love to get this information up on the wiki!

Happy scripting!

Edit: After additional testing, "OROR" does indeed work. Also, this is a KoL-itself thing; you can write your in-game macros with "ANDAND" and "OROR" and they'll work just fine.
 
Last edited:

Bale

Minion
Nice. This doesn't quite do everything that an employ script was expected to do since it requires you to generate the base macro from scratch. However, it does everything that it needs to do. Perhaps I ought to give it a try.

The ANDAND and OROR thing is interesting. I wonder if the need for that is a KoL bug or a KoLmafia bug. Veracity?

One more little caveat to keep in mind; KoLmafia's CCS doesn't exactly play nicely with a consult script that generates a macro and uses visit_url() to run it. If you want, for example, to run your consult script, come back to your CCS, perhaps do other actions, etc., you'll find that this won't work; KoLmafia is pretty much "lost" once you've done a macro from inside an ash script. To deal with this, it's best to handle the entire combat from within the single consult.

That sounds like a mafia bug. You've found a good workaround by simply continuing the next round of combat within the same consult script so it isn't a serious bug.
 

StDoodle

Minion
The ANDAND and OROR thing is interesting. I wonder if the need for that is a KoL bug or a KoLmafia bug. Veracity?

Ah, you missed my edit; it's a KoL-itself thing; try it in a vanilla KoL macro and see! (Even tested 100% w/o mafia, and they still work.)

That sounds like a mafia bug. You've found a good workaround by simply continuing the next round of combat within the same consult script so it isn't a serious bug.

Well, if by "bug" you mean KoLmafia fails to handle something it has been explicitly stated that it doesn't handle, yeah. ;) FWIW, I've tested to make sure, and having a KoL macro that only handles a round or two used first thing in combat still allows you to hit the "script" button and get expected results (including mafia tracking the current round properly, and skipping those sections in the CCS), but it doesn't work when you've used my above methods to submit your macro.

As far as changes I would want to request, the only one that I really care about is handling of CCS after generated macros are done, when combat isn't finished. Even if it required an extra server hit or something to grab accurate round information, this would be wonderful. I'm fine, also, requiring the scripter to perhaps be responsible for "saving" any previous page loads and their information if it would need to be used.

As for the rest -- the automatic macro starting points -- these can be scripted, making use of various preferences (autoOlfact, restore settings, etc.) as needed. So I wouldn't want developers to use their time on these unless they somehow found they had absolutely nothing else they'd rather work on. (I mean, if they want to, I certainly wouldn't complain, but when it's work that most of us could do instead, there's really no need.)

At this point, I'm calling on all intrepid scripters to poke holes in the above methodology, as well as perhaps develop a small library of helpful functions (such as creation of subs based on preferences like autoOlfact and autoSteal), and maybe even a library of action-to-macro-text functions.

I'm on day 4 of a 5 or 6 day run (*cough* lazy me *cough*), and plan to devote a good chunk of turns to more extensive testing once I'm post-prism. After such time, I want to start going through this information and updating the wiki's CCS information with whatever has been learned.
 
Top