LuaAI: first draft of the external candidate action system,
...with on-the-fly file reloading, if you use the --debug-lua launch argument
This commit is contained in:
parent
743cb21c1c
commit
a72febe0e4
3 changed files with 61 additions and 12 deletions
|
@ -250,7 +250,7 @@ Gs^Fp , Gs^Fp , Wwf , Wwf , Mm , Rd
|
|||
code= <<
|
||||
--! ==============================================================
|
||||
local ai = ...
|
||||
|
||||
local debug_utils = wesnoth.require "~add-ons/Wesnoth_Lua_Pack/debug_utils.lua"
|
||||
|
||||
local my_ai = { }
|
||||
|
||||
|
@ -261,10 +261,10 @@ ai_stdlib.init(ai, true)
|
|||
|
||||
|
||||
function my_ai:stage_hello()
|
||||
local debug_utils = wesnoth.require "~add-ons/Wesnoth_Lua_Pack/debug_utils.lua"
|
||||
--ai.debug.get_dst_src()
|
||||
|
||||
ai.get_dst_src()
|
||||
--ai.debug.get_enemy_dst_src()
|
||||
debug_utils.dbms(ai,false,"variable",false)
|
||||
--debug_utils.dbms(ai,false,"variable",false)
|
||||
|
||||
|
||||
end
|
||||
|
@ -318,6 +318,12 @@ function my_ai:do_moves()
|
|||
end
|
||||
end
|
||||
|
||||
-- compulsory for the external CA's
|
||||
function my_ai:ai()
|
||||
return ai
|
||||
end
|
||||
|
||||
|
||||
return my_ai
|
||||
--! ==============================================================
|
||||
>>
|
||||
|
@ -338,6 +344,11 @@ return my_ai
|
|||
evaluation="return (...):candidate_action_evaluation_hello2()"
|
||||
execution="(...):candidate_action_execution_hello2()"
|
||||
[/candidate_action]
|
||||
[candidate_action]
|
||||
engine=lua
|
||||
name=external
|
||||
location="ai/lua/extCAexample.lua"
|
||||
[/candidate_action]
|
||||
[/stage]
|
||||
[stage]
|
||||
engine="lua"
|
||||
|
|
|
@ -109,7 +109,7 @@ public:
|
|||
execution_action_handler_ = boost::shared_ptr<lua_ai_action_handler>(resources::lua_kernel->create_lua_ai_action_handler(execution_.c_str(),lua_ai_ctx));
|
||||
}
|
||||
|
||||
virtual ~lua_candidate_action_wrapper() {}
|
||||
virtual ~lua_candidate_action_wrapper() {}
|
||||
|
||||
virtual config to_config() const
|
||||
{
|
||||
|
@ -124,6 +124,38 @@ private:
|
|||
std::string execution_;
|
||||
};
|
||||
|
||||
class lua_candidate_action_wrapper_external : public lua_candidate_action_wrapper_base {
|
||||
public:
|
||||
lua_candidate_action_wrapper_external(rca_context& context, const config& cfg, lua_ai_context &lua_ai_ctx)
|
||||
: lua_candidate_action_wrapper_base(context,cfg), location_(cfg["location"])
|
||||
{
|
||||
std::string eval_code;
|
||||
std::string exec_code;
|
||||
generate_code(eval_code, exec_code);
|
||||
|
||||
evaluation_action_handler_ = boost::shared_ptr<lua_ai_action_handler>(resources::lua_kernel->create_lua_ai_action_handler(eval_code.c_str(),lua_ai_ctx));
|
||||
execution_action_handler_ = boost::shared_ptr<lua_ai_action_handler>(resources::lua_kernel->create_lua_ai_action_handler(exec_code.c_str(),lua_ai_ctx));
|
||||
}
|
||||
|
||||
virtual ~lua_candidate_action_wrapper_external() {}
|
||||
|
||||
virtual config to_config() const
|
||||
{
|
||||
config cfg = lua_candidate_action_wrapper_base::to_config();
|
||||
cfg["location"] = location_;
|
||||
return cfg;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string location_;
|
||||
|
||||
void generate_code(std::string& eval, std::string& exec) {
|
||||
std::string code = "wesnoth.require(\"" + location_ + "\")";
|
||||
eval = "return " + code + ".eval((...):ai())";
|
||||
exec = code + ".exec((...):ai())";
|
||||
}
|
||||
};
|
||||
|
||||
class lua_sticky_candidate_action_wrapper : public lua_candidate_action_wrapper {
|
||||
public:
|
||||
lua_sticky_candidate_action_wrapper( rca_context &context, const config &cfg, lua_ai_context &lua_ai_ctx)
|
||||
|
@ -229,7 +261,11 @@ void engine_lua::do_parse_candidate_action_from_config( rca_context &context, co
|
|||
candidate_action_ptr ca_ptr;
|
||||
if (!cfg["sticky"].to_bool())
|
||||
{
|
||||
ca_ptr = candidate_action_ptr(new lua_candidate_action_wrapper(context,cfg,*lua_ai_context_));
|
||||
if (cfg.has_attribute("location")) {
|
||||
ca_ptr = candidate_action_ptr(new lua_candidate_action_wrapper_external(context,cfg,*lua_ai_context_));
|
||||
} else {
|
||||
ca_ptr = candidate_action_ptr(new lua_candidate_action_wrapper(context,cfg,*lua_ai_context_));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -1459,13 +1459,15 @@ static int intf_require(lua_State *L)
|
|||
char const *m = luaL_checkstring(L, 1);
|
||||
|
||||
// Check if there is already an entry.
|
||||
luaW_getglobal(L, "wesnoth", NULL);
|
||||
lua_pushstring(L, "package");
|
||||
lua_rawget(L, -2);
|
||||
lua_pushvalue(L, 1);
|
||||
lua_rawget(L, -2);
|
||||
if (!lua_isnil(L, -1)) return 1;
|
||||
|
||||
luaW_getglobal(L, "wesnoth", NULL); // [1:fn 2:wesnoth]
|
||||
lua_pushstring(L, "package"); // [1:fn 2:wesnoth 3:"package"]
|
||||
lua_rawget(L, -2); // [1:fn 2:wesnoth 3:package]
|
||||
lua_pushvalue(L, 1); // [1:fn 2:wesnoth 3:package 4:fn]
|
||||
lua_rawget(L, -2); // [1:fn 2:wesnoth 3:package 4:nil/file]
|
||||
if (!lua_isnil(L, -1) && !game_config::debug_lua) return 1; // Am I wrong, or this return leaves 4 values on the stack? (neph)
|
||||
lua_pop(L, 1);
|
||||
|
||||
|
||||
std::string p = get_wml_location(m);
|
||||
if (p.empty())
|
||||
|
|
Loading…
Add table
Reference in a new issue