Using xpai Python Interface
First, start a server on your machine. See the xpserver tutorial for help
You must have installed Python and the
Xpilot-AI C Library.
Make sure that libxpilot_ai.so is installed in your library path somewhere. Then download
Extract the contents of this file.
tar xvzf pyxpai.tar.gz
To build python xpai, type the following in the directory containing the extracted files:
python setup.py build
Now locate the file pytest.py in the same directory. This is the
source code for a simple bot that spins in place. Since Python is an
interpreted language instead of a compiled language like Java, all you
have to do to run this bot is type:
cp build/lib*/xpai.so .
This should load up the Xpilot client. Now click the "Local" button
and try to connect to the local Xpilot server that you started. If you
didn't start a local Xpilot server, you can try connecting to a server
on the internet. After the bot joins the game it should be just
sitting there, spinning in place.
Now let's look at the code for pytest.py:
The python interface, xpai, is imported by the import xpai line. Xpai
has all the functions that can be found in the reference. Xpai also
defines an abstract function named AImain, which must be defined by
the user. In python, we must tell xpai which function is the AImain
function. We do this with the xpai.set_AImain(AImain) line. AImain is
a callback function which is called once for every frame of gameplay
in Xpilot. In this AImain the bot is instructed to spin 10 degrees,
which it does for every frame of gameplay; more complicated bots, such
as Morthon, do more complicated behaviors and call other functions to
help calculate what the bot should do.
xpai.setargs("-name jim") line simply sets the bot's
name to jim, but
xpai.setargs(string) can be used to pass
any command line arguments to Xpilot upon startup, where string is a
string containing the arguments desired. xpai.launch() starts Xpilot
with the arguments given. For a more complicated bot that also uses
command-line-arguments, try downloading morthon.py and putting it in
your bot folder. Run with:
Here is a list of valuable xpilot client options which you can use in
the command args, the string input to the "xpilot" function. For a
complete list, look at a copy of the Xpilot Manpage. You can use any
of those options with Xpilot-AI.
Join server on certain port. Also looks for servers on that port if you do a "local" search.
Name of the player.
Team to try to join once connected to a server.
X windows display number on which to run Xpilot. You can run a video framebuffer with the command Xvfb and then connect to that screen with this option (takes less CPU). For example, run the command Xvfb :1 -screen 0 1024x768x8 -auth noauth, which will start a screen on :1. Then use the option "-display :1" to run the xpilot client on it.