Added Lua support for multiple aspects.

More info on: http://wiki.wesnoth.org/LuaAI
This commit is contained in:
Dmitry Kovalenko 2011-06-02 12:24:27 +00:00
parent 79a08559bf
commit 330b3886d8
3 changed files with 86 additions and 12 deletions

View file

@ -194,6 +194,31 @@ Gs^Fp , Gs^Fp , Wwf , Wwf , Mm , Rd
[/unit]
[ai]
[aspect]
id=aggression
engine=lua
value="0.34"
[/aspect]
[aspect]
id=attack_depth
engine=lua
value="2"
[/aspect]
[aspect]
id=caution
engine=lua
value="0.35"
[/aspect]
[aspect]
id=leader_aggression
engine=lua
value="0.36"
[/aspect]
[aspect]
id=leader_value
engine=lua
value="0.37"
[/aspect]
version=10710
[engine]
name="lua"
@ -216,7 +241,7 @@ end
function my_ai:candidate_action_execution_hello(cfg)
wesnoth.message('hello from candidate action execution!')
wesnoth.message('test value is ' .. cfg.test)
wesnoth.message('test value is ') -- .. cfg.test) -- ERROR: cfg.test is nil here
self:do_moves()
end
@ -232,7 +257,12 @@ end
function my_ai:do_moves()
wesnoth.message('Aggression: ' .. ai.get_aggression())
wesnoth.message('Aggression: ' .. ai.get_aggression())
wesnoth.message('Att. depth: ' .. ai.get_attack_depth())
wesnoth.message('Caution: ' .. ai.get_caution())
wesnoth.message('Leader aggression: ' .. ai.get_leader_aggression())
wesnoth.message('Leader value: ' .. ai.get_leader_value())
my_leader = wesnoth.get_units({canrecruit = true, side = ai.side})[1]
x,y = ai.suitable_keep(my_leader)
wesnoth.wml_actions.label({ text = "suitable_keep", x = x, y = y })

View file

@ -255,6 +255,34 @@ static int cfun_ai_get_aggression(lua_State *L)
return 1;
}
static int cfun_ai_get_attack_depth(lua_State *L)
{
int attack_depth = get_readonly_context(L).get_attack_depth();
lua_pushnumber(L, attack_depth);
return 1;
}
static int cfun_ai_get_caution(lua_State *L)
{
double caution = get_readonly_context(L).get_caution();
lua_pushnumber(L, caution);
return 1;
}
static int cfun_ai_get_leader_aggression(lua_State *L)
{
double leader_aggression = get_readonly_context(L).get_leader_aggression();
lua_pushnumber(L, leader_aggression);
return 1;
}
static int cfun_ai_get_leader_value(lua_State *L)
{
double leader_value = get_readonly_context(L).get_leader_value();
lua_pushnumber(L, leader_value);
return 1;
}
lua_ai_context* lua_ai_context::create(lua_State *L, char const *code, ai::engine_lua *engine)
{
int res_ai = luaL_loadstring(L, code);//stack size is now 1 [ -1: ai_context]
@ -273,16 +301,20 @@ lua_ai_context* lua_ai_context::create(lua_State *L, char const *code, ai::engin
lua_setfield(L, -2, "side");//stack size is 2 [- 1: new table; -2 ai as string]
static luaL_reg const callbacks[] = {
{ "attack", &cfun_ai_execute_attack },
{ "get_aggression", &cfun_ai_get_aggression },
{ "move", &cfun_ai_execute_move_partial },
{ "move_full", &cfun_ai_execute_move_full },
{ "recall", &cfun_ai_execute_recall },
{ "recruit", &cfun_ai_execute_recruit },
{ "stopunit_all", &cfun_ai_execute_stopunit_all },
{ "stopunit_attacks", &cfun_ai_execute_stopunit_attacks },
{ "stopunit_moves", &cfun_ai_execute_stopunit_moves },
{ "suitable_keep", &cfun_ai_get_suitable_keep },
{ "attack", &cfun_ai_execute_attack },
{ "get_aggression", &cfun_ai_get_aggression },
{ "get_attack_depth", &cfun_ai_get_attack_depth }, // { "get_", &cfun_ai_get_}, little template # TODELETE
{ "get_caution", &cfun_ai_get_caution },
{ "get_leader_aggression", &cfun_ai_get_leader_aggression },
{ "get_leader_value", &cfun_ai_get_leader_value },
{ "move", &cfun_ai_execute_move_partial },
{ "move_full", &cfun_ai_execute_move_full },
{ "recall", &cfun_ai_execute_recall },
{ "recruit", &cfun_ai_execute_recruit },
{ "stopunit_all", &cfun_ai_execute_stopunit_all },
{ "stopunit_attacks", &cfun_ai_execute_stopunit_attacks },
{ "stopunit_moves", &cfun_ai_execute_stopunit_moves },
{ "suitable_keep", &cfun_ai_get_suitable_keep },
{ NULL, NULL }
};

View file

@ -362,6 +362,18 @@ static register_aspect_factory< standard_aspect<int> >
static register_lua_aspect_factory< lua_aspect<double> >
aggression__lua_aspect_factory("aggression*lua_aspect");
static register_lua_aspect_factory< lua_aspect<int> >
attack_depth__lua_aspect_factory("attack_depth*lua_aspect");
static register_lua_aspect_factory< lua_aspect<double> >
caution__lua_aspect_factory("caution*lua_aspect");
static register_lua_aspect_factory< lua_aspect<double> >
leader_aggression__lua_aspect_factory("leader_aggression*lua_aspect");
static register_lua_aspect_factory< lua_aspect<double> >
leader_value__lua_aspect_factory("leader_value*lua_aspect");
void registry::init()
{
}