It is possible now to store data in the Lua AI engine...
...and the data persists between save/load routines // Needs slight refactoring, use at your own risk
This commit is contained in:
parent
3e5bdcf448
commit
a3acad0d70
4 changed files with 50 additions and 2 deletions
|
@ -103,7 +103,12 @@ Gs^Fp , Gs^Fp , Wwf , Wwf , Mm , Rd
|
|||
patrol_bilbo = patrol_gen("Bilbo", {{x=2, y=3}, {x=3, y=2}}).exec
|
||||
patrol_sally = patrol_gen("Sally", {{x=5, y=7}, {x=7, y=5}}).exec
|
||||
patrol_eval_rark = ptrl.eval
|
||||
-- End of patrol function // patrol_gen(ai, "Rark", {{x=14, y=7}, {x=15, y=7}, {x=15, y=8}, {x=14, y=8}})
|
||||
-- End of patrol function // patrol_gen(ai, "Rark", {{x=14, y=7}, {x=15, y=7}, {x=15, y=8}, {x=14, y=8}})
|
||||
|
||||
function persistance_check()
|
||||
ai.data["pers"] = 182 -- we just set a variable to some value
|
||||
ai.data["stringg"] = " = it works"
|
||||
end
|
||||
>>
|
||||
[/lua]
|
||||
[/event]
|
||||
|
@ -156,7 +161,15 @@ Gs^Fp , Gs^Fp , Wwf , Wwf , Mm , Rd
|
|||
[event]
|
||||
name=side 2 turn 1
|
||||
first_time_only=yes
|
||||
{MODIFY_AI_DELETE_CANDIDATE_ACTION 2 ca_loop firstca}
|
||||
{MODIFY_AI_DELETE_CANDIDATE_ACTION 2 ca_loop firstca}
|
||||
[/event]
|
||||
|
||||
[event]
|
||||
name=side 2 turn 2
|
||||
first_time_only=yes
|
||||
[lua]
|
||||
code = << persistance_check() >>
|
||||
[/lua]
|
||||
[/event]
|
||||
|
||||
[side]
|
||||
|
@ -290,6 +303,7 @@ Gs^Fp , Gs^Fp , Wwf , Wwf , Mm , Rd
|
|||
code= <<
|
||||
--! ==============================================================
|
||||
ai = ...
|
||||
ai.data = {} -- @note: this shouldn't be here
|
||||
|
||||
local my_ai = { }
|
||||
|
||||
|
@ -298,8 +312,10 @@ local my_ai = { }
|
|||
local ai_stdlib = wesnoth.require('ai/lua/stdlib.lua');
|
||||
ai_stdlib.init(ai)
|
||||
|
||||
|
||||
function my_ai:stage_hello()
|
||||
wesnoth.message('hello from stage!')
|
||||
wesnoth.message('PERSISTANCE: ' .. tostring(ai.data["pers"]) .. ai.data["stringg"])
|
||||
end
|
||||
|
||||
function my_ai:candidate_action_evaluation_hello()
|
||||
|
|
|
@ -187,6 +187,9 @@ engine_lua::engine_lua( readonly_context &context, const config &cfg )
|
|||
cfg["code"].str().c_str(), this))
|
||||
{
|
||||
name_ = "lua";
|
||||
|
||||
config data(cfg.child_or_empty("data"));
|
||||
lua_ai_context_->set_persistent_data(data);
|
||||
}
|
||||
|
||||
|
||||
|
@ -272,8 +275,14 @@ void engine_lua::set_ai_context(ai_context * /*context*/)
|
|||
config engine_lua::to_config() const
|
||||
{
|
||||
config cfg = engine::to_config();
|
||||
|
||||
cfg["id"] = get_id();
|
||||
cfg["code"] = this->code_;
|
||||
|
||||
config data = config();
|
||||
lua_ai_context_->get_persistent_data(data);
|
||||
cfg.add_child("data") = data;
|
||||
|
||||
return cfg;
|
||||
}
|
||||
|
||||
|
|
|
@ -65,6 +65,27 @@ void lua_ai_context::init(lua_State *L)
|
|||
lua_rawset(L, LUA_REGISTRYINDEX);
|
||||
}
|
||||
|
||||
void lua_ai_context::get_persistent_data(config &cfg) const
|
||||
{
|
||||
int top = lua_gettop(L);
|
||||
|
||||
lua_getglobal(L, "ai");
|
||||
lua_getfield(L, -1, "data");
|
||||
luaW_toconfig(L, -1, cfg);
|
||||
|
||||
lua_settop(L, top);
|
||||
}
|
||||
|
||||
void lua_ai_context::set_persistent_data(const config &cfg)
|
||||
{
|
||||
int top = lua_gettop(L);
|
||||
|
||||
lua_getglobal(L, "ai");
|
||||
luaW_pushconfig(L, cfg);
|
||||
lua_setfield(L, -2, "data");
|
||||
|
||||
lua_settop(L, top);
|
||||
}
|
||||
static ai::engine_lua &get_engine(lua_State *L)
|
||||
{
|
||||
return *((ai::engine_lua*)lua_touserdata(L, lua_upvalueindex(1)));
|
||||
|
|
|
@ -52,6 +52,8 @@ public:
|
|||
{
|
||||
}
|
||||
void load();
|
||||
void get_persistent_data(config &) const;
|
||||
void set_persistent_data(const config &);
|
||||
static void init(lua_State *L);
|
||||
friend class ::LuaKernel;
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue