AI:JAI

From Spring
Revision as of 21:36, 15 May 2008 by Hoijui (talk | contribs) (added main part of JAI wiki)
Jump to navigationJump to search
JAI
Developer
hoijui
Version
alpha 3 (spring SVN)
Mod Support
Supports All Mods
Status
Under Active Development
License
-
Interface
{{{7}}}
Wrapper
{{{8}}}
<- Back to List of AIs

Forum threads

Description

JAI is a Java wrapper for the GlobalAI interface of Spring. It allows AIs to be written in Java.

At the time of writing (May 2008), JAI only works with SVN builds of Spring, due to a necessary change in the way Spring handles AI loading. The current JAI package has been developed using build 5868 and has also been successfully tested against build 5869. You can obtain one of these builds from the buildbot.

Downloads

  • JAI-bin.zip - for testing JAI with the default Java AI implementation
  • JAI-src.zip - for implementing your own Java AI and for testing JAI deeper

How to use/test it

First of all, extract the contents of JAI-bin.zip to your /spring/AI/Bot-libs directory. Note that, unlike normal AI DLLs, the JAI.dll does NOT belong directly in this directory, but has to stay in the /JAI subdirectory.

Software you need to have installed to be able to run JAI:

Make sure your Env-Vars are set like this:

On Windows:
SET JAVA_HOME=C:/Program Files/Java/jre1.6.0_05
SET PATH=%JAVA_HOME%/bin;%JAVA_HOME%/bin/client;%PATH%

On Linux:
export JAVA_HOME=/usr/jre1.6.0_05
export PATH=$JAVA_HOME/bin:$JAVA_HOME/bin/client:$PATH

If your lobby of choice does not support adding Java AIs as bots yet (until May 2008 only supported by SpringLobby), you will have to manually edit the startup script for spring. A simple way to do this is the following:

1. Start TASClient
2. Host a new game
3. Set everything to your liking
4. Add one or more Bots using a normal AI DLL such as AAI.dll or KAIK.dll
5. Start the game and abort it using SHIFT+ESCAPE once it's loaded
6. Open script.txt (in the spring root directory)
7. Search for aidll=<whatever DLL you choose>
8. Replace it with aidll=AI/Bot-libs/JAI-impl-hoijui.jar
9. Open a command line window, navigate to your spring directory
10. Start Spring using "spring.exe script.txt" on the command line

See Bug Report below, if the game crashes. If it doesn't, and you're using the JAI-impl-hoijui.jar, you can chat numbers from 1 to 7 to test the AI. This is how JAI-impl-hoijui.jar should react to chat numbers:

1 List friendly units
2 Print start position
3 Listing selected units
4 Listing UnitDefs
5 Attack: sends all units to positions of currently visible ememies
6 Toggle global rally point: move units to random position after creation (default: false)
7 Attack cheat: sends all units to positions of enemies

Some of these commands may require you to switch your team to the AI's, and build some units first. Do "/cheat" and then "/team 1" to achieve this.

Bug Report

If the game crashes, check the various log files:

  • JAI_CppLog.txt
  • SWIG_CppLog.txt
  • infolog.txt
  • hotspot.log
  • hserror_pid*.log
  • fromJAI-impl-hoijuilog.txt
  • AI/Bot-libs/JAI/log/*.txt
  • AI/hoijuiAI/log/*.txt

Most likely, you will not have all of them. They will be reduced in number in future versions, sorry ;-) Please report any errors to hoijui in the forum or on the main lobby, thanks. Do not commit the error as a bug on mantis please.

Build from Source

(Hint: the whole procedure is currently a bit simpler on windows) There are two makefiles available:

  • windows (tested on XP)
  • linux (tested on Upuntu 7.10).

they both compile with mingw and for windows (so they can be used to create for the official release build of spring).

First off, you need the current spring svn version and the build environment set up for it, see Compiling Spring. Build spring (make sure that JAI is not yet in AI/Global, as building spring would fail then).

Software you need to have installed to be able to compile JAI:

Extract the JAI-src.zip to: spring_current_svn/AI/Global in the Makefile(s), you have to adjust the vars: - JAVA_HOME - SPRING_CURRENT_WINDOWS (only change it if you use the unix makefile) on unix, you can set SPRING_CURRENT_WINDOWS to a mount of an smb shared spring on a windows PC, which needs to contains the same build as the one on unix. if you dont do this, you have to manually copy the results to your test machine on windows. As the makefile only compiles for windows, you can not test JAI on linux yet, except you adjust the makefile yourself and get it to work.

Compile with either of the two:

makefile -f Makefile_unix jall

makefile -f Makefile_win32 jall

You should now have JAI ready to run, please see above.

Creating your own implementation

To create your own implementation, it's best to use hoijui's test implementaion at JAI-src.zip/src/java-testimpl as a start. It is a minimal implementation, which consists of one implementation for each:

  • info.osrt.jai.IJGlobalAIFactory
  • info.osrt.jai.JGlobalAIInterface

To compile, you also need JAI.jar (JAI-bin.zip/JAI) in your classpath.

You can override methods from the superclass of TstJGlobalAI.java to implement AI behavior. Once you're done modifying, just export your classes to a jar, and use it as your Java AI as described above in above. Make sure to set your new jar in step 8.

If you need further Java libraries, put the jars into AI/Bot-libs/JAI/lib, and native libraries into AI/Bot-libs/JAI/lib aswell.

Debugging

It is possible to debugg your Java AI while it runs in spring. To enable debugging, you need to save the following into file AI/Bot-libs/JAI/config.xml:

<?xml version="1.0" encoding="UTF-8" ?>

<JAI-config>

   <jvm>
       <option>-Xms4M</option>
       <option>-Xmx64M</option>
       <option>-Xss512K</option>
       <option>-Xoss400K</option>
       <option>-verbose:jni</option>
       <option>-Xcheck:jni</option>
       <option>-XX:+UnlockDiagnosticVMOptions</option>
       <option>-XX:+LogVMOutput</option>
       <option>-Xdebug</option>
       <option>-agentlib:jdwp=transport=dt_socket,server=y,address=7777</option>
       <option>-Djava.compiler=NONE</option>
       
       <option>-Djava.util.logging.config.file=./AI/Bot-libs/JAI/logging.properties</option>
   </jvm>

</JAI-config>

Run spring with a JAI implementation. Now you can connect with a java debugger to the machine running spring on port 7777.

How does it work

JAI utilizes the jvm.dll of the JRE/JDK through JNI, to create a virtual machine for the Java AI to run in. It uses the same C/C++ interface as the native AIs, and just translates calls from one langauge to the other, bidirectional. Most of the sourcecode is generated with SWIG. This has the advantage that it supports a lot of other languages. One side is always C/C++, but the other could be Java, Lua, C#, Phyton, Ruby, ...

See SWIG Home for further details. Supporting an other language is not as easy as just switching one variable from "Java" to "Ruby" eg, but if you are desperate, you might want to use JAI as a base to support your favourite language.

Used Libraries

  • JVM: ...cause we are doing Java
  • JarClassLoader: loading classes from jars at runntime (Java)
  • Apache Commons: logging and parsing XML (Java)
  • log4j: logging (Java)
  • TinyXML: parsing XML (C/C++)
  • CMetalMap: Krogothes Metal Spot finder (C/C++)

TODO

  • Cleanup the Logging (mainly: fewer log-files).
  • Integrate JAI into spring, so it compiles during the main build process.

<- Back to AI page