Author: oracle
Date: Wed Jul 9 22:29:50 2008
New Revision: 27880
URL: http://svn.gna.org/viewcvs/wesnoth?rev=27880&view=rev
Log:
AI now fetches scripts rather than asking ai_python::python_ai to
do it.
Modified:
trunk/src/ai.cpp
trunk/src/ai_python.cpp
trunk/src/ai_python.hpp
Modified: trunk/src/ai.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai.cpp?rev=27880&r1=27879&r2=27880&view=diff
==============================================================================
--- trunk/src/ai.cpp (original)
+++ trunk/src/ai.cpp Wed Jul 9 22:29:50 2008
@@ -46,6 +46,7 @@
#include "wml_exception.hpp"
#include <cassert>
+#include <fstream>
#define LOG_AI LOG_STREAM(info, ai)
#define WRN_AI LOG_STREAM(warn, ai)
@@ -187,6 +188,41 @@
}
};
+
+#ifdef HAVE_PYTHON
+// Finds all python AI scripts available in the current binary path.
+// They have to end with .py, and have #!WPY as first line.
+// If preferences allow for unsafe python AIs, then also look for
+// the #!UNSAFE_WPY tag.
+std::vector<std::string> get_available_py_scripts()
+{
+ int allow_unsafe = !preferences::run_safe_python() ;
+ std::vector<std::string> scripts;
+ const std::vector<std::string>& paths = get_binary_paths("data");
+ for(std::vector<std::string>::const_iterator i = paths.begin(); i !=
paths.end(); ++i) {
+ std::vector<std::string> files;
+ get_files_in_dir(*i + "ais", &files, NULL, ENTIRE_FILE_PATH);
+ for(std::vector<std::string>::const_iterator j = files.begin(); j !=
files.end(); ++j) {
+ // file ends with .py
+ if (j->substr(j->length() - 3) == ".py") {
+ std::string name(j->substr(j->rfind("/") + 1)); // extract name
+ // read first line
+ std::ifstream s(j->c_str()); std::string mark; s >> mark; s.close();
+ if (mark == "#!WPY" &&
+ std::find(scripts.begin(), scripts.end(), name) == scripts.end())
+ scripts.push_back(name);
+ else if (allow_unsafe && mark == "#!UNSAFE_WPY" &&
+ std::find(scripts.begin(), scripts.end(), name) ==
scripts.end())
+ scripts.push_back(name);
+ }
+ }
+ }
+ return scripts;
+}
+#endif
+
+
+
std::vector<std::string> get_available_ais()
{
std::vector<std::string> ais;
@@ -195,7 +231,7 @@
//ais.push_back("idle_ai");
ais.push_back("dfool_ai");
#ifdef HAVE_PYTHON
- std::vector<std::string> scripts = python_ai::get_available_scripts();
+ std::vector<std::string> scripts = get_available_py_scripts();
ais.insert(ais.end(), scripts.begin(), scripts.end());
#endif
return ais;
@@ -223,8 +259,8 @@
else if(name == "python_ai")
#ifdef HAVE_PYTHON
return new python_ai(info);
-// else if(name == "newpy_ai")
-// return new pythonai::PythonAI( info ) ;
+// else if(name == "newpy_ai")
+// return new pyai::PythonAI( info ) ;
#else
{
LOG_STREAM(err, ai) << "No Python AI support available in this
Wesnoth build!\n";
Modified: trunk/src/ai_python.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai_python.cpp?rev=27880&r1=27879&r2=27880&view=diff
==============================================================================
--- trunk/src/ai_python.cpp (original)
+++ trunk/src/ai_python.cpp Wed Jul 9 22:29:50 2008
@@ -2196,34 +2196,4 @@
Py_DECREF(globals);
}
-// Finds all python AI scripts available in the current binary path.
-// They have to end with .py, and have #!WPY as first line.
-// If preferences allow for unsafe python AIs, then also look for
-// the #!UNSAFE_WPY tag.
-std::vector<std::string> python_ai::get_available_scripts()
-{
- int allow_unsafe = !preferences::run_safe_python() ;
- std::vector<std::string> scripts;
- const std::vector<std::string>& paths = get_binary_paths("data");
- for(std::vector<std::string>::const_iterator i = paths.begin(); i !=
paths.end(); ++i) {
- std::vector<std::string> files;
- get_files_in_dir(*i + "ais", &files, NULL, ENTIRE_FILE_PATH);
- for(std::vector<std::string>::const_iterator j = files.begin();
j != files.end(); ++j) {
- // file ends with .py
- if (j->substr(j->length() - 3) == ".py") {
- std::string name(j->substr(j->rfind("/") + 1));
// extract name
- // read first line
- std::ifstream s(j->c_str()); std::string mark;
s >> mark; s.close();
- if (mark == "#!WPY" &&
- std::find(scripts.begin(),
scripts.end(), name) == scripts.end())
- scripts.push_back(name);
- else if (allow_unsafe && mark == "#!UNSAFE_WPY"
&&
- std::find(scripts.begin(),
scripts.end(), name) == scripts.end())
- scripts.push_back(name);
- }
- }
- }
- return scripts;
-}
-
#endif // HAVE_PYTHON
Modified: trunk/src/ai_python.hpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai_python.hpp?rev=27880&r1=27879&r2=27880&view=diff
==============================================================================
--- trunk/src/ai_python.hpp (original)
+++ trunk/src/ai_python.hpp Wed Jul 9 22:29:50 2008
@@ -80,7 +80,6 @@
static bool is_unit_valid(const unit* unit);
std::vector<team>& get_teams() { return get_info().teams; }
- static std::vector<std::string> get_available_scripts();
static void initialize_python();
static void invoke(std::string name);
_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits