exposed suitable_keep(unit) to Lua AI library.

Added test of AI.suitable_keep to lua ai test scenario.
This commit is contained in:
Jody Northup 2011-04-16 00:05:20 +00:00
parent c7058c03b2
commit 9e720501c6
2 changed files with 28 additions and 0 deletions

View file

@ -234,6 +234,8 @@ end
function my_ai:do_moves()
wesnoth.message('Aggression: ' .. ai.get_aggression())
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 })
--! move (partial move)
ai.move(my_leader,13, 22)
--! full move. note that the my_leader still can be used altrough x and y are now different.

View file

@ -105,6 +105,31 @@ static bool to_map_location(lua_State *L, int &index, map_location &res)
return true;
}
static int cfun_ai_get_suitable_keep(lua_State *L)
{
int index = 1;
if (false) {
error_call_destructors:
return luaL_typerror(L, index, "location (unit/integers)");
}
ai::readonly_context &context = get_readonly_context(L);
unit const *leader;
if (lua_isuserdata(L, index))
{
leader = luaW_tounit(L, index);
if (!leader) goto error_call_destructors;
}
else goto error_call_destructors;
const map_location loc = leader->get_location();
const pathfind::paths leader_paths(*resources::game_map, *resources::units, loc,
*resources::teams, false, true, context.current_team());
const map_location &res = context.suitable_keep(loc,leader_paths);
lua_pushnumber(L, res.x+1);
lua_pushnumber(L, res.y+1);
return 2;
}
static int ai_execute_move(lua_State *L, bool remove_movement)
{
int index = 1;
@ -254,6 +279,7 @@ lua_ai_context* lua_ai_context::create(lua_State *L, char const *code, ai::engin
{ "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 }
};