Help - Search - Members - Calendar
Full Version: Record macros
BatMUD > BatMUD forums > Batclient
Chronic
I created a small utility for myself to record macros for the client. It records your input and creates a batclient macro from the recording. I use it mainly to speed up creation of walking macros.
I figured that someone else might consider this helpful so decided to post the script here. There should be nothing complicated there, so feel free to edit it to your liking.

Basic usage:
Start recording /macrorecord <name>
Stop recording /macrosave
--
Chronic

CODE
///////////////////////////////////////////////////////////////////////////////
// Macro recording utility created by Chronic.
//
// It records your input until you stop recording and creates a macro with the
// given name. Multiple commands are grouped together (n;n;e;e;e -> 2 n;3 e).
//
// USAGE: Start recording: /macrorecord <name>
// Stop recording: /macrosave
// Cancel recording: /macrocancel
// Toggle pause on/off: /macropause
// Back up one step: /macroback
//
// Note: Prefix your commands with @ to escape recording or use /macropause.
//
// Note2: Backing up a step removes the last entry and tries to move in the
// opposite direction. If the direction is non-standard, it only removes the
// last entry from the list and does not try to move.
//
///////////////////////////////////////////////////////////////////////////////

import com.mythicscape.batclient.interfaces.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;

SCRIPT_NAME = "macrorecord";
SCRIPT_DESC = "Record macros";
SCRIPT_OFF = false;
SCRIPT_VERSION = "1.0";


///////////////////////////////////////////
// These can be changed to suitable ones
///////////////////////////////////////////
// Delimeter used in the macros
String delim = ";";

// Escape prefix to bypass recorder, this is stripped from the command that actually gets sent to the mud
String escape = "@";

// Macronames for controlling recording
String startmacro = "macrorecord";
String stopmacro = "macrosave";
String cancelmacro = "macrocancel";
String pausemacro = "macropause";
String backmacro = "macroback";

// Verbose level
// 0: No messages
// 1: All control messages
// 2: All control messages plus echo current recording after every command
int VERBOSE = 2;
///////////////////////////////////////////

private String macroname = "";
private String macro = "";
private LinkedList input = new LinkedList();
private boolean recording = false;

// Match a direction to it's abbreviated counterpart
private HashMap abbreviations = new HashMap();
abbreviations.put("north", "n");
abbreviations.put("northeast", "ne");
abbreviations.put("east", "e");
abbreviations.put("southeast", "se");
abbreviations.put("south", "s");
abbreviations.put("southwest", "sw");
abbreviations.put("west", "w");
abbreviations.put("northwest", "nw");
abbreviations.put("up", "u");
abbreviations.put("down", "d");

// Used by the back up macro to go in the opposite direction
private HashMap reverseDirections = new HashMap();
reverseDirections.put("n", "s");
reverseDirections.put("ne", "sw");
reverseDirections.put("e", "w");
reverseDirections.put("se", "nw");
reverseDirections.put("s", "n");
reverseDirections.put("sw", "ne");
reverseDirections.put("w", "e");
reverseDirections.put("nw", "se");
reverseDirections.put("u", "d");
reverseDirections.put("d", "u");

// These words are ignored by the recorder
private HashSet keywords = new HashSet();
keywords.add("/"+startmacro);
keywords.add("/"+stopmacro);
keywords.add("/"+cancelmacro);
keywords.add("/"+pausemacro);
keywords.add("/"+backmacro);
keywords.add("$"+SCRIPT_NAME+".start");
keywords.add("$"+SCRIPT_NAME+".stop");
keywords.add("$"+SCRIPT_NAME+".cancel");
keywords.add("$"+SCRIPT_NAME+".pause");
keywords.add("$"+SCRIPT_NAME+".back");

void bootup() {
macroManager.newMacro(startmacro, "$"+SCRIPT_NAME+".start %1");
macroManager.newMacro(stopmacro, "$"+SCRIPT_NAME+".stop");
macroManager.newMacro(cancelmacro, "$"+SCRIPT_NAME+".cancel");
macroManager.newMacro(pausemacro, "$"+SCRIPT_NAME+".pause");
macroManager.newMacro(backmacro, "$"+SCRIPT_NAME+".back");
}

void run() {
}

public String processCommand() {
if (recording) {
if (command.startsWith(escape)) {
return command.substring(escape.length());
} else if (!keywords.contains(command)) {
command = abbreviate(command);
input.add(command);
if (VERBOSE >= 2) {clientGUI.printText("general", "/macro "+macroname+" = "+compactInput()+"\n", "555555");}
}
}
return command;
}

public void start() {
if (argument == null || argument.equals("") || argument.equals("%1")) {
help();
} else {
if (VERBOSE >= 1) {clientGUI.printText("general", "Recording started.\n");}
macro = "";
macroname = argument;
input.clear();
recording = true;
}
}

public void stop() {
if (macroname != null && !macroname.equals("")) {
recording = false;
if (input.isEmpty()) {
if (VERBOSE >= 1) {clientGUI.printText("general", "Recording stopped.\n");}
} else {
macro = compactInput();
clientGUI.doCommand("/macro "+macroname+" = "+macro);
//macroManager.newMacro() BUGS in saving so use /macro command
//macroManager.newMacro(macroname, macro);
//if (VERBOSE >= 0) {clientGUI.printText("general", "Created new macro.\n");}
}
macro = "";
macroname = "";
input.clear();
}
}

public void back() {
if (macroname != null && !macroname.equals("")) {
recording = false;
if (input.isEmpty()) {
if (VERBOSE >= 1) {clientGUI.printText("general", "Recording is empty.\n");}
} else {
String command = input.removeLast();
if (VERBOSE >= 1) {clientGUI.printText("general", "Removed previous command.\n");}
if (VERBOSE >= 2) {clientGUI.printText("general", "/macro "+macroname+" = "+compactInput()+"\n", "555555");}
String reverse = reverseDirection(command);
if (reverse != null) {
net.send(reverse);
}
}
recording = true;
}
}

public void pause() {
if (macroname != null && !macroname.equals("")) {
if (recording) {
recording = false;
if (VERBOSE >= 1) {clientGUI.printText("general", "Recording paused.\n");}
} else {
recording = true;
if (VERBOSE >= 1) {clientGUI.printText("general", "Recording restarted.\n");}
}
}
}

public void cancel() {
if (macroname != null && !macroname.equals("")) {
recording = false;
macro = "";
macroname = "";
input.clear();
if (VERBOSE >= 1) {clientGUI.printText("general", "Recording canceled.\n");}
}
}

private String abbreviate(String direction) {
if (abbreviations.containsKey(direction)) {
return abbreviations.get(direction);
}
return direction;
}

private String reverseDirection(String direction) {
if (reverseDirections.containsKey(direction)) {
String reverse = reverseDirections.get(direction);
return reverseDirections.get(direction);
}
return null;
}

private String compactInput() {
String compact = "";
int counter = 1;
String previousCommand = "";
for (String command : input) {
if (previousCommand.equals("")) {
previousCommand = command;
} else if (command.equals(previousCommand) && counter < 20) {
++counter;
} else {
compact += (counter == 1) ? "" : counter+" ";
compact += previousCommand+delim;
previousCommand = command;
counter = 1;
}
}
compact += (counter == 1) ? "" : counter+" ";
compact += previousCommand;
return compact;
}

public void help() {
clientGUI.printText("general", "Usage: Start recording: /"+startmacro+" <macro_name>\n");
clientGUI.printText("general", " Stop recording: /"+stopmacro+"\n");
clientGUI.printText("general", " Cancel recording: /"+cancelmacro+"\n");
clientGUI.printText("general", " Toggle pause on/off: /"+pausemacro+"\n");
clientGUI.printText("general", " Back up one step: /"+backmacro+"\n");
clientGUI.printText("general", "\n");
clientGUI.printText("general", "Note: Prefix your commands with @ to escape recording or use /macropause.\n");
clientGUI.printText("general", "Note2: Backing up a step removes the last entry from the list and tries to move in the opposite direction. If the direction is non-standard, it only removes the entry but does not try to move.\n");
}
Elurin
Thanks for this, very useful.
Calmar
Thanks, I made similar but it grapped output so I had to put echo on. This is much smarter :)
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2024 Invision Power Services, Inc.