Any C++ <-> Python programmers interested in contribut
Moderator: Forum Moderators
You can already do this. Just make a program which implements the Wesnoth network protocol and implements an AI.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).
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
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.
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.
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.
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.
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 .
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 .
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).freecraft wrote:Greetings!
Now THAT is something completely different !
Is it included in 1.0.2 ?
You indeed only need to change that path (it appears 2 times, at most 1) to point to your script file.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 .
Note that the game loads the script each turn, so you can change it often to test things.
Hee hee. I made a random movement AI. It moves. Randomly.
Calculation of the average distance moved as a function of turns is left as an excercise for the reader.
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)
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)
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)
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)
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)
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)
Add HP properties in ai_python
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
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
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
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 355 times