Write your Group AI in Python!

Write your Group AI in Python!

Here is where ideas can be collected for the skirmish AI in development

Moderators: hoijui, Moderators

Post Reply
Jack
Posts: 32
Joined: 15 Jul 2006, 00:35

Write your Group AI in Python!

Post by Jack »

I've written a Python wrapper for the Spring group AI interface. You can use this to write group AIs in Python, which might be easier than writing them in C++. The webpage has source code, a Windows DLL, and some example AIs (mostly ported from the C++ group AIs).

http://www.jwhitham.org.uk/pyai/

This is the first version, so there are bound to be rough edges. However I have been playing using it, and it seems stable. If you have any comments, please post them below. Thanks!
hollowsoul
Posts: 665
Joined: 06 Jun 2006, 19:49

Post by hollowsoul »

:shock:
Woah nice :)
Gonna give this a shot over the weekend 8)
User avatar
jcnossen
Former Engine Dev
Posts: 2440
Joined: 05 Jun 2005, 19:13

Post by jcnossen »

Nice work, only the spring restriction of 1 AI per DLL is now a drawback... Ideally someone would write a patch to have more than 1 AI per DLL ;)

This also allows others to easily make wrappers of other scripting languages, because SWIG supports quite a lot of them (C#, perl, ruby, tcl...)
Jack
Posts: 32
Joined: 15 Jul 2006, 00:35

Post by Jack »

jcnossen wrote:Nice work, only the spring restriction of 1 AI per DLL is now a drawback... Ideally someone would write a patch to have more than 1 AI per DLL ;)
Yes - PyAI would work far better if it was possible to do this. Would the devs accept a patch from myself?
jcnossen wrote:This also allows others to easily make wrappers of other scripting languages, because SWIG supports quite a lot of them (C#, perl, ruby, tcl...)
True: SWIG is a really useful tool. All it takes is a bit of glue code, and possibly some extra wrappers for some functions to make them behave in the way that the users of the scripting language expect. I started this process for Python, but there are still some things that could be improved.
User avatar
Michilus_nimbus
Posts: 634
Joined: 19 Nov 2004, 20:38

Post by Michilus_nimbus »

Hot diggity do!
I've always wanted to do this, but I just lacked the knowledge and patience, and overall competence to do it!
You, sir, are a god.
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

Can this be used for lua? It'd be a godsend if you set it up for that, as it would take out a huge chunk of XE10 work and allow me to change a heck of a lot very quickly and allow a huge feature explosion
Jack
Posts: 32
Joined: 15 Jul 2006, 00:35

Post by Jack »

AF wrote:Can this be used for lua? It'd be a godsend if you set it up for that, as it would take out a huge chunk of XE10 work and allow me to change a heck of a lot very quickly and allow a huge feature explosion
That's possible, but I don't know much about Lua myself. SWIG is able to generate Lua - have a look at http://www.swig.org/ to see what it can do. Basically it produces proxy classes to represent C++ classes in the language of your choice, which is ideal for auto-generating an interface between your favourite scripting language and C++. The PyAI source might be useful if you are doing this, in particular 'spring.i' which contains instructions for SWIG, and 'prepare.py' which calls SWIG and processes the results.
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

I looked at that a month or two ago and was baffled and confused at the time, it's too much at once to take in when I have as much going on as I do.
Tobi
Spring Developer
Posts: 4598
Joined: 01 Jun 2005, 11:36

Post by Tobi »

Jack wrote:
jcnossen wrote:Nice work, only the spring restriction of 1 AI per DLL is now a drawback... Ideally someone would write a patch to have more than 1 AI per DLL ;)
Yes - PyAI would work far better if it was possible to do this. Would the devs accept a patch from myself?
Provided the patch is of sufficient quality, I'm pretty sure we do.
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

I took a look at the source and found it to be a seething mass of confusion, I cant even find the exported functions the engine calls to get the name of the groupAI and the copy of IGroupAI.

btw I take it this requires the users's installed a python interpreter on their system?
hollowsoul
Posts: 665
Joined: 06 Jun 2006, 19:49

Post by hollowsoul »

Use pyexe & turn the python script into a windows exe.
U can also specfic which .dll to include along with the exe
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

? huh
hollowsoul
Posts: 665
Joined: 06 Jun 2006, 19:49

Post by hollowsoul »

User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

I dont get the point, that compiles into ,exe's, what we have here is a library/ aka .so/.dll in c/c++ with an attatched scripting language, only I dont see how the language is getting attatched or what its even getting attatched to, just that he's used SWIG. I see no linkage between IGroupAI::InitAI() or CommandFinished() calls from the engine and the python equivilants, I cant even find the functions that initialize the whole thing and pass it back to the engine.
Sheekel
Posts: 1391
Joined: 19 Apr 2005, 19:23

Post by Sheekel »

Would this be possible with Java?

I'm taking and advanced Java course right now...but i dont know, maybe people look down on it as a internet language for applets only?
User avatar
krogothe
AI Developer
Posts: 1050
Joined: 14 Nov 2005, 17:07

Post by krogothe »

Sheekel wrote:Would this be possible with Java?

I'm taking and advanced Java course right now...but i dont know, maybe people look down on it as a internet language for applets only?
Far from it, java is becoming something awesome! I want to learn it as my second language :P
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

Look at the JNI documentation.

You could use SWIG too.

Write an itnerface class in java but instead fo defining the functions just write

public static native return_type function_name(params);

Then once the class is done, pass it through the javah and it'll generate a c/c++ header.

For the most part this is easy as the data types jint, jchar, jfloat, jdouble etc are all exactly the same as the standard c/c++ data types, the only conversions and issues come with JString and structures/objects.

Another issue is where they use stuff like void* to pass over information, and I havent figured out what the java equivilant is yet and howto translate between them two.
Jack
Posts: 32
Joined: 15 Jul 2006, 00:35

Post by Jack »

hollowsoul wrote:http://www.py2exe.org/
Unfortunately this won't work. I thought about doing this, and investigated it, but Py2exe isn't able to embed the Python interpreter in a DLL: it can only generate EXE files. So you do have to install Python - sorry!
Jack
Posts: 32
Joined: 15 Jul 2006, 00:35

Post by Jack »

AF wrote:I took a look at the source and found it to be a seething mass of confusion, I cant even find the exported functions the engine calls to get the name of the groupAI and the copy of IGroupAI.
That's in gluecpp, which gets #included by the SWIG code. The reason for doing this, rather than compiling gluecpp separately, is that I needed access to certain SWIG functions in order to get the C++ pointer for a Python object. The reason for not calling gluecpp something more sensible, like 'glue.cpp', is that scons will then attempt to build it separately (any suggestions of better ways to organise this are welcome). Hope this helps.
Post Reply

Return to “AI”