Using xpai Java Interface

You must have installed Java and the Xpilot-AI C Library. Make sure that libxpilot_ai.so is installed in your library path somewhere. Then download xpai.jar and place it in the folder where you would like to run your Java bots.

Now download the file
Spinner.java and place it in your xpai folder. This is the source code for a simple bot that spins in place. To compile the file, enter the command:
javac Spinner.java -classpath xpai.jar
Now you should have a file named Spinner.class in your folder. To run it, type:
java -classpath .:xpai.jar Spinner
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 Spinner.java:
import org.xpilotai.xpai;
public class Spinner extends xpai {

  public void AImain() {
    AIself_turn(15);
    return;
  }

  public static void main(String[] args) {
    Spinner spinner = new Spinner();
    spinner.AI_setmaxturn(15);
    spinner.xpilot("-name spinner");
  }
}
The java interface, xpai, is imported by the import org.xpilotai.xpai; line. To create a bot, one must create a class that extends xpai. Xpai has all the functions that can be found in the reference. The xpai class also defines an abstract function named AImain, which must be defined by the user. 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 15 degrees, which it does for every frame of gameplay; more complicated bots, such as Morty, do more complicated behaviors and call other functions to help calculate what the bot should do.

Within the main function, the a Spinner class named spinner is created, which must be done because Java won't let you call the non-static functions in xpai from within main, which is static. Once we have created spinner, we set the maximum turn rate to 15 (rather unnecessarily, in this case), and then we launch the game with the xpilot command. You will notice that the functions called within AImain are directly called, but the ones from main must be called with a variable name specification (spinner).

For a more complicated bot that also uses command-line-arguments, try downloading Morty.java and putting it in your bot folder. Compile with:
javac Morty.java -classpath xpai.jar
Run without command-line arguments:
java -classpath .:xpai.jar Morty
Run with command-line arguments:
java -classpath .:xpai.jar Morty "-name Morty -join localhost"
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.
-port integer
Join server on certain port. Also looks for servers on that port if you do a "local" search.
-name string
Name of the player.
-team integer
Team to try to join once connected to a server.
-display :integer
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.