[Crossfire-wiki] [Crossfire DokuWiki] page changed: client_side_scripting:client_scripting_interface-basic_howto

no-reply_wiki at metalforge.org no-reply_wiki at metalforge.org
Thu Dec 13 08:05:05 CST 2012


A page in your DokuWiki was added or changed. Here are the details:

Date        : 2012/12/13 08:05
User        : katia
Edit Summary: Added Issue command info and some sorting & grammar fixes

@@ -1,32 +1,43 @@
- ==== Client Scripting Interface - Basic howto ====
+ ====== Client Scripting Interface - Basic howto ======
  
- === Purpose of this manual ===
+ ===== Purpose of this manual =====
  This Howto covers the scripting interface present in gtk and x11 client under linux and Windows (see notes at end).
  
- === What is the Client Scripting Interface? ===
+ ===== What is the Client Scripting Interface? =====
  Basically, the Client Scripting Interface is a way to have an external program (the script) interact client-side with your in-game behaviour. The script can have a copy of messages sent from the server to your client (there is a wide variety of such messages), can have a copy of messages sent from your client to the server (there are also lots of them), may request information (on a item, a map square ...) and finally can issue commands to the server. Lets call those actions, respectively, server-client spying, client-server spying, examining and interacting.
  
- === How do I run my script ===
- Most of the time it is basically straight forward.  The command     script <pathname>    would be entered to start the script.  How ever that only works if you write your script in a shell script or C/++.  If you write you script in Java it get a bit more tricky but no worries its not that hard.  
+ ===== How do I run my script =====
  
+ ==== C/++, Perl, Python, Bash, and others ====
+ 
+ Remember to ** chmod +x ** the script so that its executable.
+ In the client type   **   script <pathname>    **
+ 
+ The script should start.
+ 
+ 
+ see also: [[#Windows-specific notes]]
+ 
+ ==== Java ====
+ If you write you script in Java it gets a bit more tricky but no worries its not that hard.  
  We are going to create a shell script that will run our scripts in Java for us.
  
       #!/bin/bash
       cd /dir/to/java.class
       java $*
  
- Remember to chmod +x the script so that its executable.
+ Remember to chmod +x this script so that its executable.
  Now to run your script this is all you have to do is  "script /path/to/bashScript blah"   blah being your .class file - .class and /path/to/bashScript being the executable bash script.
  
- === Pipes, stdout, stdin ===
+ ===== Pipes, stdout, stdin =====
  (If this section causes you to consider puking by the second line, jump to the next section.)
  
  The script is a program which is external to the client. It can be written in a wide range of languages. This can be C, Java, Perl, Python, Bash script, 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 //get some system font// and //render the line in the specified window at the specified position//. All this because when you ask to "write hello world to screen" you ask, really, 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.\\  
  \\ 
  Ok, and now? Now what we use is exactly the same trick as the graphical console. when the client runs a script, it changes the script's stdin and replace 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!
  
- === First Script ===
+ ===== First Script =====
  
  Learn to say hello
  
  Here we go for the first script. We will do quite simple things. We will ask our character to say "hello world" around. The script will be written in C because we simply need to choose a language. The script is quite simple:
@@ -61,9 +72,9 @@
    }
  
  Do you get the idea? every printf you make is a command to the client scripting interface. So now let's look at this command. It begins with issue followed by 2 integers. The command __issue__ is part of the interacting of the Client Scripting Interface. It allows a script to send any command the player could send. There are lots of them i won't explain here. Just issue the command 'help commands' to get the list. What are those 2 integers? The first one is repeat. It typically is used for dropping items, but can be used in other cases. The second integer, 1 or 0, is must send. If it is //one//, the command must be sent, if it is //zero//, command may be lost in the client-server process. Most user will set this to 1.
  
- === Second Script ===
+ ===== Second Script =====
  
  Spy Kids
  
  What to do next? As you can see, our script doesn't wait very long after issuing commands. And it doesn't get information from the client at all. In fact it just //hopes// it is really speaking to the client. We are going to write a simple script which will __issue__ a command to the client and then gets the result. We are going to spy!\\ 
@@ -101,9 +112,9 @@
  If you type the command __scripts__ in your client you will see our script is still running.\\ 
  \\ 
  Let's look more closely at the code. We define a character buffer and a length. We will use the buffer to read what the client sends to the script. Then our script sends to the client the command __monitor__ (don't forget the \n). This command asks the client to give the script a copy of all commands sent from the client to the server. Now each time a command is sent from client to server, the script will get a "monitor <command>" string.\\ 
  \\ 
- == A strange C command ==
+ ==== A strange C command ====
    fflush(stdout)
  
  The stdout has something called a buffer. When you write to output device, it's not immediatly sent to it. For performance reasons, successive print to stdout are grouped. Most of the time, \n is enough to force sending of data, but we ensure all data are 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.\\ 
  \\ 
@@ -153,12 +164,12 @@
    watch stats food 395
  
  Wow! This mean we know when food is low and we can ask our script to invoke restoration (remember the __issue__ command?) when our character's food gets below 50! No starvation anymore.
  
- === What's next? ===
+ ===== What's next? =====
  There are two things you can still do with Script. The first is to request a bit of informations. The client then tells the script what it wants to know. The second thing is triggering an action of the script from client interface. The command scripttell allows player to say something to a script. The script will get the exact command typed by player. See below for command list.
  
- == Commands to client ==
+ ==== Commands to client ====
  Here is a list of command the script can send to client.
  
    * //watch <command type>// - watch the given command from server-client protocol. <command type> specifies the commands we want to watch. Set to empty to get all commands.
    * //unwatch <command type>// - unwatch the given command from server-client protocol. <command type> specifies the commands we want to watch. Set to empty to get all commands.
@@ -181,19 +192,19 @@
  | items cont | Return a list of items in the open container, one per line |
  | map pos | Return the players x,y within the current map |
  | map near | Return the 3x3 grid of the map centered on the player |
  | map all | Return all the known map information |
- | map <x> <y> | Return the information about square x,y in the current map |
+ | map <x> <y> | Return the information about square x,y in the current map (relative to player position)|
  </box>
  
-   * //issue <repeat> <must_send> <command>// - send <command> to server on behalf of client. <repeat> is the number of times to execute command <must_send> tells whether or not the command must sent at all cost (1 or 0)
+   * //issue <repeat> <must_send> <command>// - send <command> to server on behalf of client. <repeat> is the number of times to execute command <must_send> tells whether or not the command must sent at all cost (1 or 0). <repeat> and <must_send> are optional parameters. See [[#The Issue Command]] for more details.
    * //issue mark <tag>// - special case of issue command. only gets the command 'mark' and a object tag
    * //issue lock <new state> <tag>// - special case of issue command. Only gets the command 'lock' with 2 parameters
    * //draw <color> <text>// - draw the following text on client interface with given color. Usefull for debugging and may help you to forget about using the stderr
    * //monitor// - start monitoring commands send from client to server. Doesn't take any parameter.
    * //unmonitor// - stop monitoring commands send from client to server. Doesn't take any parameter.
  
- == Information from client ==
+ ==== Information from client ====
  Here is an incomplete list of information strings send by client to script. Those informations are sent only because the client asked them, except for scripttell.
  
    * //scripttell <yourname> <additional data>// - user send special command to this script specifically
    * //monitor <repeat> <must_send> <command>//
@@ -206,12 +217,56 @@
    * //request map end// - Marks the end of a complete map transfer from client to script. Helpful.
  
    * FIXME ** NOTE more information strings to be added here, incomplete list **
  
- === Windows-specific notes ===
+ 
+ ==== The issue command ====
+ This command has it's particularities. I'll try to detail all I've encountered so far.
+ 
+ Usage:
+   issue [<repeat> [<must_send>]] <command>
+ 
+   * //<repeat>// - Times the command should be issued. It's an optional numeric parameter. (Doesn't always work as expected)
+   * //<must_send>// - May be 1 or 0. If set to 1 the command will always be sent to the server. Otherwise it //may// be discarded under certain circumstances. 
+ 
+ The next examples should clarify the use of numeric parameters.
+ 
+ **Some examples that work:**
+   issue 0 0 apply
+   issue 0 1 apply
+   issue 1 0 apply
+   issue 1 1 apply
+   issue 1 1 stay f
+   issue 0 0 stay f
+   issue 1 1 south
+   issue 0 0 south
+   issue 0 0 examine bag
+   issue 1 0 get nugget (works as 'get 1 nugget')
+   issue 2 0 get nugget (works as 'get 2 nugget')
+   issue 1 1 get nugget (works as 'get 1 nugget')
+   issue 2 1 get nugget (works as 'get 2 nugget')
+   issue 0 0 get nugget (works as 'get nugget')
+   issue lookat 2 3 (works as left clicking on the spot 2 tiles to your right and 3 tiles down, coords are relative to player position)
+ 
+ Some examples that **do NOT** work:
+   <del>issue apply</del>
+   <del>issue 0 apply</del> 
+   <del>issue 1 apply</del>
+   <del>issue 2 0 apply</del> (works only once, not twice as expected)
+   <del>issue 2 1 apply</del> (works only once, not twice as expected)
+   <del>issue get 4 nugget</del> 
+   <del>issue 4 1 south</del> (works only once, not 4 times as expected)
+   <del>issue 4 0 south</del> (works only once, not 4 times as expected)
+   <del>issue 1 1 lookat 2 3</del>
+   <del>issue 0 1 lookat 2 3</del>
+   <del>issue 0 0 lookat 2 3</del>
+ 
+ FIXME add a complete set of commands that work with <repeat> and another of those that work without it.
+ 
+ ===== Windows-specific notes =====
  Scripting works thanks to a patch from archaios. Known issues are:
  
      * If you want to run a Perl script for instance, you need to issue 'perl <script_name.pl>, even if .pl is correctly seen as perl script.
      * If script doesn't output anything, try turning off buffering (in perl $| = 1) or flush your pipe, or add a sleep at end of program. It seems Windows closes pipes at script termination before client gets time to grab output. 
  
  
  


IP-Address  : 186.52.88.112
Old Revision: http://wiki.metalforge.net/doku.php/client_side_scripting:client_scripting_interface-basic_howto?rev=1199147820
New Revision: http://wiki.metalforge.net/doku.php/client_side_scripting:client_scripting_interface-basic_howto

-- 
This mail was generated by DokuWiki at
http://wiki.metalforge.net/



More information about the crossfire-wiki mailing list