Relay Override Scripting

From Kolmafia
Revision as of 09:04, 17 July 2010 by imported>Bale
Jump to navigation Jump to search

A relay script is a script that creates or modifies a web page in the relay browser. It is only in the relay browser that the effects of these scripts can be seen. There are two different kinds of relay override scripts. The first is simply known as an Relay Script. The second is a User Interface script.

Relay Script

Basic relay scripts are used to change the appearance of a KoL page. Note that relay scripts are disabled by default. In order to use relay override scripts, you must enable the relevant KoLmafia preference checked as shown:

Override browser preference.jpg

Then you may download relay scripts from the KoLmafia Forums to your /relay directory.

Writing a Relay Script

For a simple Relay Script, there are several basic rules:

  1. The name of the script must be the same as the page it is overriding, except with an ash extension instead of a php extension. For users who have enabled relay override scripts, mafia will automatically call xyz.ash every time the relay browser attempts to visit xyz.php.
  2. Unless the script will display entirely new content, it should first load the unmodified page from KoL using the command visit_url() with no parameters.
  3. Then the contents of the page can be modified with replace_string() or other string manipulation functions.
  4. Finally, the modified page is written with the write() command.

Example of Relay Override

Here is an example of a relay script. Assuming it is named shore.ash, it will modify shore.php to reveal items that are needed for the level 6 tower monster.

// This script will only work if it is named shore.ash
void main() {
   buffer results;
   // Get the basic shore page
   append(results, visit_url());
   
   // This will modify the Dude Ranch text if a stick of dynamite is needed
   void dynamite() {
      if(available_amount($item[stick of dynamite]) < 1)
         replace_string(results, "Distant Lands Dude Ranch Adventure",
          "<font color=\"green\"><b>Distant Lands Dude Ranch Adventure"
          +" (stick of dynamite needed)</b></font>");
   }

   // This will modify the Paradise Island text if a tropical orchid is needed
   void orchid() {
      if(available_amount($item[tropical orchid]) < 1)
         replace_string(results, "Tropical Paradise Island Getaway",
          "<font color=\"green\"><b>Tropical Paradise Island Getaway"
          +" (tropical orchid needed)</b></font>");
   }

   // This will modify the Ski Resort text if a barbed-wire fence is needed
   void fence() {
      if(available_amount($item[barbed-wire fence]) < 1)
         replace_string(results, "Large Donkey Mountain Ski Resort",
          "<font color=\"green\"><b>Large Donkey Mountain Ski Resort"
          +" (barbed-wire fence needed)</b></font>");
   }
   
   // Only do this if the character has no access to the mall
   if(!can_interact()) {   
      // If the character has a level 7 telescope, only check for the necessary item
      if(get_property("telescopeUpgrades") == "7")
         switch(get_property("telescope7")) {
         case "see a wooden beam":
            dynamite(); break;
         case "see a formidable stinger":
            orchid(); break;
         case "see a pair of horns":
            fence(); break;
         }
      // Check for all three items
      else {
         dynamite();
         orchid();
         fence();
      }
   }
   
   // Write the modified page to the web browser
   write(results);
}


User Interface Script

There are special kinds of relay scripts; scripts with user interfaces. As with a regular relay override script, they go in the "relay" directory. However, they don't override other pages; their entire content is created from scratch. These scripts can be accessed under the relay drop-down in the bottom-right of the top pane. Their names must start with "relay_" in order for them to show up there.

This can provide a pretty interface and automate tasks, but relay scripts aren't really suitable for long, ongoing processes such as autoadventuring, since nothing appears in the browser until the script exits. Also, writing them requires a rather different mindset than the sequential execution of a normal script: if the relay script presents options for the user, they are actually read by an entirely separate invocation of the script than the one that displayed the options. The areas in which relay scripts will be the most useful are user-friendly configuration of other scripts, and presentation of options for the user to carry out in the relay browser themselves since the script can simply link to any in-game activity.

Writing an Interface Script

For writing interface scripts, several features were added to KoLmafia.

  • ASH predefined string constant __FILE__, which is the filename of the current script. This allows relay scripts to link or submit a form to themselves, even if the user has renamed them.
  • ASH function form_fields(), which returns a string[string] map with all the name/value pairs from the relay request being handled.
  • ASH string functions entity_encode() and entity_decode(), for converting text that might contain special characters such as ampersands and angle brackets so that it can safely be displayed in HTML, and retrieving such text from form fields.

Additionally, there are some excellent tools for writing form elements in interface scripts on the KoLmafia Forum. That script is a toolbox of essential functions for any advanced interface script so make good use of it.

Example of User Interface

Here is an example of a User Interface script. This must be saved in /relay with a name starting with relay, such as relay_breakables.ash to appear in the relay drop-down in KoL's top menu. This was originally presented by Jason Harper here and makes an excellent example.

// The following constant can be edited to add more breakable items. 
// However, please note that adding items here does no good at all
// unless KoLmafia has been updated to recognize the specific breakage
// messages of those items.
boolean[item] breakable = $items[
   antique helmet, antique spear, antique shield, antique greaves,
   cyber-mattock, cheap studded belt,
   sugar chapeau, sugar shank, sugar shield, sugar shillelagh,
   sugar shirt, sugar shotgun, sugar shorts];

void write_option(string label, string value, string current) {
   write("<option value='");
   write(value);
   write("'");
   if (value == current)
      write(" selected");
   write(">");
   write(entity_encode(label));
   writeln("</option>");
}

void write_select(string label, string pref, boolean addDefault, string[string] fields) {
   if (fields contains pref)
      set_property(pref, fields[pref]);
   
   string current = get_property(pref);
   write("<tr><td align=right>");
   write(label);
   write("</td><td><select name='");
   write(pref);
   writeln("'>");
   
   if (addDefault)
      write_option("Use default", "", current);
   write_option("Abort on breakage", "1", current);
   write_option("Equip previous item", "2", current);
   write_option("Re-equip from inventory (if available), or abort", "3", current);
   write_option("Re-equip from inventory, or equip previous item", "4", current);
   write_option("Acquire new item & re-equip", "5", current);
   writeln("</select></td></tr>");
}
   
void main() {
   write("<html><body><form method=POST action='");
   write(__FILE__);
   writeln("'>");
   write("Breakable equipment fine tuning v1.0, by jasonharper@pobox.com");
   writeln(" (in-game: <a href='sendmessage.php?toid=363053'>Seventh</a>)");
   write("<p>");
   write("This script lets you override the default behavior when equipment breaks in combat,");
   write(" on an item-by-item basis.  The default setting (found in the Item section of Choice Advs)");
   writeln(" is duplicated here for your convenience.");
   writeln("<table border=0>");
   string[string] fields = form_fields();
   write_select("Default", "breakableHandling", false, fields);
   foreach itm in breakable
      write_select(to_string(itm), "breakableHandling" + to_int(itm), true, fields);
   writeln("</table>");
   writeln("<input type=submit value='Save changes'>");
   writeln("</form></body></html>");
}