LuaAI: (1) persistent storage mechanism now available and working properly (2) old/incomplete mechanism not removed, subject to future deprecation
This commit is contained in:
parent
fcf1c6dd4f
commit
754f2c2b3d
5 changed files with 45 additions and 11 deletions
|
@ -2,7 +2,7 @@
|
|||
|
||||
return {
|
||||
get_ai = function(ai)
|
||||
local my_ai = { }
|
||||
local my_ai = {}
|
||||
local ai_stdlib = wesnoth.require('ai/lua/stdlib.lua')
|
||||
ai_stdlib.init(ai, true)
|
||||
|
||||
|
|
|
@ -10,6 +10,11 @@ end
|
|||
|
||||
function example_ca:exec(ai)
|
||||
wesnoth.message("External CA exec attacks!")
|
||||
if (ai.store.n == nil) then
|
||||
ai.store.n = 0
|
||||
end
|
||||
ai.store.n = ai.store.n + 5
|
||||
wesnoth.message("xtcastore = ", tostring(ai.store.n))
|
||||
ai.attack(2, 12, 3, 12, 1, 1) -- showcasing the presence of the AI table
|
||||
end
|
||||
|
||||
|
|
|
@ -4,6 +4,9 @@ return {
|
|||
|
||||
init = function(ai, dbg)
|
||||
|
||||
-- Initialize persistent storage table
|
||||
ai.store = {}
|
||||
|
||||
-- Initialize the cache system for LuaAI context
|
||||
local cache = wesnoth.require("ai/lua/cache.lua")
|
||||
cache.init(ai)
|
||||
|
|
|
@ -240,8 +240,8 @@ engine_lua::engine_lua( readonly_context &context, const config &cfg )
|
|||
{
|
||||
name_ = "lua";
|
||||
config data(cfg.child_or_empty("data"));
|
||||
|
||||
if (lua_ai_context_) { // The context might be NULL if the config contains errors
|
||||
|
||||
if (lua_ai_context_ && !data.empty()) { // The context might be NULL if the config contains errors
|
||||
lua_ai_context_->set_persistent_data(data);
|
||||
}
|
||||
}
|
||||
|
@ -251,7 +251,7 @@ std::string engine_lua::get_engine_code(const config &cfg) const
|
|||
if (cfg.has_attribute("code")) {
|
||||
return cfg["code"].str();
|
||||
}
|
||||
// If there is no engine defined we create a dummy engine
|
||||
// If there is no engine defined we create a dummy engine
|
||||
std::string code = "local ai = ... local m_ai = wesnoth.require(\"ai/lua/dummy_engine_lua.lua\") return m_ai.get_ai(ai)";
|
||||
return code;
|
||||
}
|
||||
|
@ -368,7 +368,11 @@ config engine_lua::to_config() const
|
|||
config cfg = engine::to_config();
|
||||
|
||||
cfg["id"] = get_id();
|
||||
cfg["code"] = this->code_;
|
||||
|
||||
// Some AI's have no engine code
|
||||
if (!this->code_.empty()) {
|
||||
cfg["code"] = this->code_;
|
||||
}
|
||||
|
||||
if (lua_ai_context_) {
|
||||
config data = config();
|
||||
|
|
|
@ -72,11 +72,23 @@ void lua_ai_context::get_persistent_data(config &cfg) const
|
|||
lua_pushlightuserdata(L, static_cast<void *>(const_cast<char *>(&aisKey)));
|
||||
lua_rawget(L, LUA_REGISTRYINDEX);
|
||||
lua_rawgeti(L, -1, num_);
|
||||
|
||||
|
||||
int engine_index = lua_gettop(L);
|
||||
config data = config();
|
||||
lua_getfield(L, -1, "data");
|
||||
luaW_toconfig(L, -1, cfg);
|
||||
|
||||
luaW_toconfig(L, -1, data);
|
||||
lua_settop(L, engine_index);
|
||||
|
||||
config store = config();
|
||||
lua_getfield(L, -1, "get_ai");
|
||||
lua_pushvalue(L, -2);
|
||||
lua_call(L, 1, 1);
|
||||
lua_getfield(L, -1, "store");
|
||||
luaW_toconfig(L, -1, store);
|
||||
|
||||
lua_settop(L, top);
|
||||
cfg.add_child("lua_store") = store;
|
||||
cfg.add_child("lua_engine_data") = data;
|
||||
}
|
||||
|
||||
void lua_ai_context::set_persistent_data(const config &cfg)
|
||||
|
@ -86,10 +98,20 @@ void lua_ai_context::set_persistent_data(const config &cfg)
|
|||
lua_pushlightuserdata(L, static_cast<void *>(const_cast<char *>(&aisKey)));
|
||||
lua_rawget(L, LUA_REGISTRYINDEX);
|
||||
lua_rawgeti(L, -1, num_);
|
||||
|
||||
luaW_pushconfig(L, cfg);
|
||||
|
||||
int engine_index = lua_gettop(L);
|
||||
config data = cfg.child("lua_engine_data");
|
||||
luaW_pushconfig(L, data);
|
||||
lua_setfield(L, -2, "data");
|
||||
|
||||
lua_settop(L, engine_index);
|
||||
|
||||
config store = cfg.child("lua_store");
|
||||
lua_getfield(L, -1, "get_ai");
|
||||
lua_pushvalue(L, -2);
|
||||
lua_call(L, 1, 1);
|
||||
luaW_pushconfig(L, store);
|
||||
lua_setfield(L, -2, "store");
|
||||
|
||||
lua_settop(L, top);
|
||||
}
|
||||
static ai::engine_lua &get_engine(lua_State *L)
|
||||
|
|
Loading…
Add table
Reference in a new issue