Improve backwards compatibility for Lua AI

This changes the following:
- Fixes the experimental AI, without changing any of its code except for that in the [engine] tag
- Returns a dummy self from the dummy Lua engine, so that external CAs are more easily switched to using [params]
- Changes the order in which parameters are passed to AI component code. The order is now:
        state/self,    params,    data
This commit is contained in:
Celtic Minstrel 2016-03-01 13:20:45 -05:00 committed by mattsc
parent e2cbe50781
commit 1d7c1c74f6
4 changed files with 15 additions and 10 deletions

View file

@ -7,4 +7,6 @@
local ai_stdlib = wesnoth.require('ai/lua/stdlib.lua')
ai_stdlib.init(ai)
-- No special state is returned by the default engine
-- This is only returned for minor backwards compatibility
local p, d = ...
return {data = d}

View file

@ -548,8 +548,10 @@
[engine]
name="lua"
code= <<
local ai = ...
return wesnoth.require("ai/lua/generic_rush_engine.lua").init(ai)
local _,data = ...
local exp_ai = wesnoth.require("ai/lua/generic_rush_engine.lua").init(ai)
exp_ai.data = data
return exp_ai
>>
[/engine]
[stage]

View file

@ -161,14 +161,14 @@ private:
bool use_parms_;
void generate_code(std::string& eval, std::string& exec) {
std::string preamble = "local params, data, state = ...\n";
std::string preamble = "local self, params, data = ...\n";
std::string load = "wesnoth.require(\"" + location_ + "\")";
if (use_parms_) {
eval = preamble + "return " + load + ":evaluation(ai, {" + eval_parms_ + "}, {data = data})";
exec = preamble + load + ":execution(ai, {" + exec_parms_ + "}, {data = data})";
} else {
eval = preamble + "return " + load + ".evaluation(params, data, state)";
exec = preamble + load + ".execution(params, data, state)";
eval = preamble + "return " + load + ".evaluation(self, params, data)";
exec = preamble + load + ".execution(self, params, data)";
}
}
};

View file

@ -1050,7 +1050,7 @@ void lua_ai_context::update_state()
}
// Store the state for use by components
lua_setfield(L, -2, "state"); // [-1: AI state]
lua_setfield(L, -2, "self"); // [-1: AI state]
// And return with empty stack.
lua_pop(L, 1);
@ -1127,9 +1127,10 @@ void lua_ai_action_handler::handle(const config &cfg, bool read_only, lua_object
lua_remove(L, -2); // [-1: AI action -2: AI state]
// Load the arguments
luaW_pushconfig(L, cfg); // [-1: parameters -2: AI action -3: AI state]
lua_getfield(L, -3, "data"); // [-1: data -2: parameters -3: action -4: state]
lua_getfield(L, -4, "state");
int iState = lua_absindex(L, -2);
lua_getfield(L, iState, "self");
luaW_pushconfig(L, cfg);
lua_getfield(L, iState, "data");
// Call the function
luaW_pcall(L, 3, l_obj ? 1 : 0, true);