Lua AI: make persistent data variable work with external CAs

It was previously being set correctly by the engine, but was not
accessible from the Lua AI.
This commit is contained in:
mattsc 2013-10-23 18:39:26 -07:00
parent e9fd0d51d8
commit d5b994c0bb
2 changed files with 21 additions and 15 deletions

View file

@ -1,16 +1,22 @@
--! #textdomain wesnoth
-- This is the engine used by the Lua AI when no engine is
-- defined specifically in the [side] tag
return {
get_ai = function(ai)
local my_ai = { }
local ai_stdlib = wesnoth.require('ai/lua/stdlib.lua')
ai_stdlib.init(ai, true)
get_ai = function(ai)
local my_ai = {}
-- compulsory for the external CA's
function my_ai:get_ai()
return ai
end
return my_ai
end
}
local ai_stdlib = wesnoth.require('ai/lua/stdlib.lua')
ai_stdlib.init(ai, true)
-- Make the ai table available to the eval/exec functions
function my_ai:get_ai()
return ai
end
-- Make the persistent data table available to the eval/exec functions
my_ai.data = {}
return my_ai
end
}

View file

@ -152,8 +152,8 @@ private:
void generate_code(std::string& eval, std::string& exec) {
std::string code = "wesnoth.require(\"" + location_ + "\")";
eval = "return " + code + ":eval((...):get_ai())";
exec = code + ":exec((...):get_ai())";
eval = "return " + code + ":evaluation((...):get_ai(), (...))";
exec = code + ":execution((...):get_ai(), (...))";
}
};
@ -252,7 +252,7 @@ std::string engine_lua::get_engine_code(const config &cfg) const
return cfg["code"].str();
}
// 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)";
std::string code = "local ai = ... return wesnoth.require(\"ai/lua/dummy_engine_lua.lua\").get_ai(ai)";
return code;
}