Options for Python API.

Script is now specified as a tag in the [ai] bloc. API is built only
if HAVE_PYTHON is defined (fallback on ai2 if Python not built).
This commit is contained in:
Nicolas Weeger 2005-12-17 13:59:04 +00:00
parent b350367222
commit ec55160ad9
5 changed files with 66 additions and 18 deletions

42
.gitignore vendored Normal file
View file

@ -0,0 +1,42 @@
*.swp
.DS_Store
wesnoth
wesnothd
make_translation
merge_translations
Makefile
autom4te.cache
stamp-h1
.libs
config.h
config.log
config.status
configure.lineno
Makefile.in
aclocal.m4
config.h.in
configure
translations
6not
Debug
wesnoth.exe
fribidi.dll
jpeg.dll
libgettextlib.dll
libgettextpo.dll
libgettextsrc.dll
libiconv2.dll
libintl3.dll
libpng13.dll
SDL.dll
SDL_image.dll
SDL_mixer.dll
SDL_net.dll
freetype6.dll
wesnoth.exp
wesnoth.ilk
wesnoth.lib
wesnoth.ncb
wesnoth.opt
wesnoth.plg
zlib1.dll

View file

@ -228,6 +228,8 @@ SVN trunk (1.1.x):
* --with-preferences-dir configure option for coexistence of multiple versions
* now defaults to --with-fribidi if fribidi found during configure
* various bug fixes and code cleanups
* experimental Python API for AI. See http://www.wesnoth.org/wiki/ReferencePythonAPI
for more information.
Version 1.0rc1:
* language and i18n:

View file

@ -17,6 +17,9 @@
#include "ai.hpp"
#include "ai2.hpp"
#include "ai_dfool.hpp"
#ifdef HAVE_PYTHON
#include "ai_python.hpp"
#endif
//#include "advanced_ai.hpp"
#include "dialogs.hpp"
#include "game_config.hpp"
@ -200,6 +203,12 @@ ai_interface* create_ai(const std::string& name, ai_interface::info& info)
// return new advanced_ai(info);
else if(name == "ai2")
return new ai2(info);
else if(name == "python_ai")
#ifdef HAVE_PYTHON
return new python_ai(info);
#else
return new ai2(info);
#endif
else if(name != "")
LOG_STREAM(err, ai) << "AI not found: '" << name << "'\n";

View file

@ -16,15 +16,15 @@
* display, which I don't think has to be exposed to Python. This is used by C++ AIs mostly for
debugging purposes. We might want to allow Python scripts to write messages to the display for
debugging purposes also, but they would only need very limited access.
p gamemap, defined in map.hpp, which contains the definition of the map. Definitely needs to be
* gamemap, defined in map.hpp, which contains the definition of the map. Definitely needs to be
exposed to Python.
- game_data, defined in unit_types.hpp, which contains definitions of the available unit types,
* game_data, defined in unit_types.hpp, which contains definitions of the available unit types,
attacks, races, and so forth. Needs to be exposed to Python.
* unit_map, a map<gamemap::location,unit>. gamemap::location is defined in map.hpp and unit is
defined in unit.hpp. This contains all the units currently in the game. Will need to be exposed.
* vector<team>, a listing of the teams (i.e. sides) in the game. Defined in team.hpp. Will
need to be exposed.
- gamestatus, the current turn and time of day, etc. Defined in gamestatus.hpp. Will need to
* gamestatus, the current turn and time of day, etc. Defined in gamestatus.hpp. Will need to
be exposed.
- turn_info, does not need to be exposed.
@ -32,6 +32,8 @@ Additionally, useful utility functions such as those found in pathutils.hpp shou
*/
#ifdef HAVE_PYTHON
#include "global.hpp"
#include "ai.hpp"
@ -1054,20 +1056,10 @@ python_ai::~python_ai()
void python_ai::play_turn()
{
//PyRun_SimpleString("import Wesnoth\nWesnoth.LogMessage('test')");
/* int result = PyRun_SimpleString("import Wesnoth\n"
"units = Wesnoth.GetUnits()\n"
"Wesnoth.LogMessage('%s units'%len(units))\n"
"for i in [1..len(units)]:\n"
"\tu = units.get(i)\n");/*
"\tWesnoth.LogMessage('%s'%(u.Name))\n");
/* "for u in units:\n"
"\tWesnoth.LogMessage('%s'%(u.Name))\n");
"Wesnoth.LogMessage('test1')\n");
PyObject* error = PyErr_Occurred();*/
char* script = "c:\\w.py";
PyObject* file = PyFile_FromString(script,"rt");
//FILE* f = fopen("c:\\w.py","rt");
PyRun_SimpleFile(PyFile_AsFile(file),script);
std::string script = current_team().ai_parameters()["python_script"];
PyObject* file = PyFile_FromString((char*)script.c_str(),"rt");
PyRun_SimpleFile(PyFile_AsFile(file),(char*)script.c_str());
Py_DECREF(file);
}
#endif // HAVE_PYTHON

View file

@ -1,6 +1,8 @@
#ifndef AI_PTYHON_HPP_INCLUDED
#define AI_PTYHON_HPP_INCLUDED
#ifdef HAVE_PYTHON
#include "ai_interface.hpp"
#include <Python.h>
@ -60,5 +62,6 @@ protected:
std::map<location,paths> enemy_possible_moves_;
};
#endif // HAVE_PYTHON
#endif