Any C++ <-> Python programmers interested in contribut

Discussion of all aspects of the game engine, including development of new and existing features.

Moderator: Forum Moderators

Dave
Founding Developer
Posts: 7071
Joined: August 17th, 2003, 5:07 am
Location: Seattle
Contact:

Post by Dave »

ryn wrote:Why Python or Lua? Why not a generic I/O interface?

ie, the game can run and open a pipe to any executable and then communicate with it, allowing AIs in any language - even an interpreted language can easily be used (to clarify the use of 'executable') with a batch file or shell script. We'd just need to specify a protocol and create a class to encapsulate the actions of such generic AIs (pretending that it, in fact, is executing moves while actually running the external AI).
You can already do this. Just make a program which implements the Wesnoth network protocol and implements an AI.

Programming with Python embedded is alot easier though, because it makes it easier for us to expose C++ 'helper functions' to the AI.

David
“At Gambling, the deadly sin is to mistake bad play for bad luck.” -- Ian Fleming
freecraft
Posts: 94
Joined: April 28th, 2005, 12:49 am
Location: Serbia
Contact:

Post by freecraft »

And generic I/O would be unsafe. You never know what lies behind some executable file ... :)

I support this initiative and offer my, err, limited help. Although what we need here is a leader who will start things. Anyway, I know nothing about AI ... But maybe I could make some algorithms like pathfinding and much more.
Ryo
Inactive Developer
Posts: 18
Joined: August 23rd, 2005, 12:50 pm

Post by Ryo »

Alpha (or maybe beta) Python API is already in SVN, in the wesnoth-python branch.

Reference is at http://www.wesnoth.org/wiki/ReferencePythonAPI

It needs some testing and feedback (on exported classes, method name, ...), though, so Python programmers feel free to try it.
freecraft
Posts: 94
Joined: April 28th, 2005, 12:49 am
Location: Serbia
Contact:

Post by freecraft »

Greetings!

Now THAT is something completely different !
Is it included in 1.0.2 ?
yobbo
Art Contributor
Posts: 151
Joined: September 16th, 2005, 6:31 am
Location: New Zealand

Post by yobbo »

Omygawd, I can make a Wesnoth AI in Python!? How am I ever gonna have a real life :(.
yobbo
Art Contributor
Posts: 151
Joined: September 16th, 2005, 6:31 am
Location: New Zealand

Post by yobbo »

How can I test a Python AI? i.e. what do I need to name the script to have it recognized as the play_turn handler. I had a quick look in ai_python.cpp and it seems the file name is hardcoded as "c:\w.py"... it's been years since I've had a "c:" :?.

I'd just change it but I'm not confident enough in my C++ knowledge to be sure that this is what I need to change :).
Ryo
Inactive Developer
Posts: 18
Joined: August 23rd, 2005, 12:50 pm

Post by Ryo »

freecraft wrote:Greetings!

Now THAT is something completely different !
Is it included in 1.0.2 ?
No, the development has been done in the wesnoth-python branch. Though it'd be a matter of just copying the 2 files I added to the trunk (and change the AI instanciation function to recognize eg ai_python).
Ryo
Inactive Developer
Posts: 18
Joined: August 23rd, 2005, 12:50 pm

Post by Ryo »

yobbo wrote:How can I test a Python AI? i.e. what do I need to name the script to have it recognized as the play_turn handler. I had a quick look in ai_python.cpp and it seems the file name is hardcoded as "c:\w.py"... it's been years since I've had a "c:" :?.

I'd just change it but I'm not confident enough in my C++ knowledge to be sure that this is what I need to change :).
You indeed only need to change that path (it appears 2 times, at most 1) to point to your script file.

Note that the game loads the script each turn, so you can change it often to test things.
yobbo
Art Contributor
Posts: 151
Joined: September 16th, 2005, 6:31 am
Location: New Zealand

Post by yobbo »

Hee hee. I made a random movement AI. It moves. Randomly.

Code: Select all

import wesnoth
import random

src_dst = wesnoth.get_src_dst()
unit_dict = wesnoth.get_units()
for location in unit_dict.keys():
    if src_dst.has_key(location):
        moves = src_dst[location]
        move = random.choice(moves)
        wesnoth.move_unit(location, move)
Calculation of the average distance moved as a function of turns is left as an excercise for the reader.
Ryo
Inactive Developer
Posts: 18
Joined: August 23rd, 2005, 12:50 pm

Post by Ryo »

Warning: you should definitely restart the loop after move_unit, and get the lastest get_src_dst :)
Case in mind: you try to move a unit where another already moved.
yobbo
Art Contributor
Posts: 151
Joined: September 16th, 2005, 6:31 am
Location: New Zealand

Post by yobbo »

Hmm, you are quite right. random_walk_ai version 1.1:

Code: Select all

import wesnoth
import random

unit_dict = wesnoth.get_units()
for location in unit_dict.keys():
    src_dst = wesnoth.get_src_dst()
    if src_dst.has_key(location):
        moves = src_dst[location]
        move = random.choice(moves)
        wesnoth.move_unit(location, move)
:)
Ryo
Inactive Developer
Posts: 18
Joined: August 23rd, 2005, 12:50 pm

Post by Ryo »

I put a dump test script on the wiki, at http://www.wesnoth.org/wiki/PythonTestScript

It shows some basics of the API & such.

(note: it can crash the game, as it doesn't recompute possible moves/destinations after moving a unit. it is after all to test :p)
Ryo
Inactive Developer
Posts: 18
Joined: August 23rd, 2005, 12:50 pm

Post by Ryo »

Python API is ready for test.

Check http://www.wesnoth.org/wiki/ReferencePythonAPI for info on how to build it & such

(note: makefiles aren't yet updated, so you'll need to tweak build process under Linux)
yobbo
Art Contributor
Posts: 151
Joined: September 16th, 2005, 6:31 am
Location: New Zealand

Post by yobbo »

I suspect "unittype.nightvision" should in stead be "unittype.nightstalk" :).
viorc
Posts: 130
Joined: February 22nd, 2006, 3:03 am

Add HP properties in ai_python

Post by viorc »

Hi to all,
here is a small patch adding the health points (HP) in the ai_python.cpp file. This way you can access to two new properties that are :
* unit.hitpoints - remaining HP
* unit.max_hitpoints - HP of the unit at creation
I find it to be highly needed properties in order to try to keep the python AI alive a little longer by sending on due time damaged units to the closest village rather than letting them die on the battlefield :?
Having that feature included allowed me to get my first python AI grown level2 unit 8)
You can apply this change on the code with patch command or manually copy paste the code in ai_python.cpp at the line referenced in the here-under add_HP.diff file.

NB: patch command should be something like (on GNU/linux):
- go to wesnoth containing directory
- make a copy of the wesnoth directory tree into new patches directory (cp -R ... patches/)
- patch -d patches -p1 < add_HP.diff
Attachments
add_HP.diff.gz
patch generated on version 1.1
(2.32 KiB) Downloaded 303 times
Post Reply