Atrinik Client  4.0
Scripting

Introduction

Since a program can't possibly have every single feature users want, scripting is used, which provides additional features.

How it works

The script is a program which is external to the client. It can be written in a wide range of languages, including C, Java, Perl, Python, Bash, PHP, anything you can think about. How can this be possible? Let's take a look at what happens when you type "echo hello world" on a shell prompt. Sure it writes on your terminal "hello world". And if you are in a graphical shell prompt? It appears in the graphical console! Some process changed your request of writing to screen to a complex process of getting system's font and rendering the line in the specified window at the specified position. All this is because when you ask to "write hello world to screen" you really ask to "write hello world to the standard output device". This standard output device is called stdout. There is also the stdin, which most of the time is your keyboard and stderr, the standard error device being most of the time the same as stdout.

Now what Atrinik client uses is exactly the same trick as the graphical console. When the client runs a script, it changes the script's stdin and replaces the keyboard input with it's own orders (using a pipe). And it changes the stdout so instead of writing to screen, the script sends data to the client (using another pipe). And this is how any language can be used. Because every language can write to the screen and read from the keyboard!

Examples

Consider the following example:

#include <stdio.h>
int main()
{
printf("issue command /say Hello World.\n");
fflush(stdout);
return 0;
}

The above example would, when compiled and executed by the client, use the /say command to output the message "Hello World." to everyone in hearing range.

Another example is this:

#include <stdio.h>
static char *directions[] = {
"s", "w",
"n", "e"
};
#define NUM_DIRECTIONS (int) (sizeof(directions) / sizeof(char *))
#define NUM_REPEATS 4
int main()
{
int i, ii;
for (i = 0; i <= NUM_REPEATS; i++)
{
for (ii = 0; ii < NUM_DIRECTIONS; ii++)
{
printf("issue command /%s\n", directions[ii]);
fflush(stdout);
}
}
return 0;
}

The above example would make your character go south, west, north and east, repeating the process 4 times.

Flushing stdout

The stdout has something called a buffer. When you write to output device, it's not immediately sent to it. For performance reasons, successive prints to stdout are grouped. Most of the time, \n is enough to force sending of data, but we ensure all the data is sent to client by flushing the stdout (force empty buffer). In the future, when you think client didn't get a command but the script did send it, ensure you flushed stdout.

Commands

This section documents all the possible commands a script can use to make the client do something.

Events

When something happens, events can be triggered. For example, when server sends stats update, the stats event is triggered. This can be useful for scripts watching food and eat something when it reaches critical point, or if hp goes way too down and there is grace to be spared cast a healing spell or find a potion of healing in inventory and apply it.