[[lua AI fixes]]

(1) LuaAI ai.get_avoid() now provides the correct coordinates (not
offset by -1), this reduced some code duplication and code
inconsistency (2) Tweaked ai.attack() of LuaAI to consider weapons are
numbered from 1 and so forth, so that this function is more consistent
to the Lua indexing style
This commit is contained in:
Dmitry Kovalenko 2012-04-03 02:13:15 +00:00
parent ecf23e8055
commit bdc8138c3b
2 changed files with 41 additions and 29 deletions

View file

@ -161,6 +161,24 @@ Gs^Fp , Gs^Fp , Wwf , Wwf , Mm , Rd
{MODIFY_AI_DELETE_CANDIDATE_ACTION 2 ca_loop firstca}
[/event]
[event]
name=side 2 turn 1
[modify_side]
side=2
[ai]
[avoid]
x,y=1,11
[/avoid]
[leader_goal]
x,y=2,12
[/leader_goal]
[/ai]
[/modify_side]
#{MODIFY_UNIT side=1 moves 0}
[/event]
[side]
type=Dwarvish Steelclad
id=side_1_leader
@ -290,6 +308,7 @@ Gs^Fp , Gs^Fp , Wwf , Wwf , Mm , Rd
x,y=42,20
[/criteria]
[/goal]
[goal]
name=lua_goal
value=6
@ -311,7 +330,7 @@ Gs^Fp , Gs^Fp , Wwf , Wwf , Mm , Rd
code= <<
--! ==============================================================
ai = ...
-- local data = {} -- @note: this shouldn't be here
local my_ai = { }
@ -321,16 +340,13 @@ 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(self.data["pers"]) .. self.data["stringg"])
--local tg = ai.get_targets()
--for k,v in pairs(tg) do
-- if v.type==3 or v.type==4 then
-- wesnoth.message(tg[k].type .. " " .. tg[k].value .. " " .. tg[k].loc.x .. " " ..tg[k].loc.y)
-- end
--end
function my_ai:stage_hello()
wesnoth.message('hello from stage!')
local debug_utils = wesnoth.require "~add-ons/Wesnoth_Lua_Pack/debug_utils.lua"
local avoid = ai.get_avoid()
local leader_goal = ai.get_leader_goal()
debug_utils.dbms(avoid,false,"variable",false)
debug_utils.dbms(leader_goal,false,"variable",false)
end
function my_ai:candidate_action_evaluation_hello()
@ -348,14 +364,6 @@ end
function my_ai:candidate_action_evaluation_hello2()
wesnoth.message('hello from second candidate action evaluation!')
wesnoth.message("LuaDebug", "hello")
local map = ai.get_enemy_dstsrc()
wesnoth.message("LuaDebug", "hello " .. #map)
for dst,src in pairs(map) do
for k,v in pairs(src) do
wesnoth.message("LuaDebug", dst.x .. "," .. dst.y .. " -> " .. v.x .. "," .. v.y)
end
end
return 99
end
@ -375,7 +383,7 @@ function my_ai:do_moves()
--! full move. note that the my_leader still can be used altrough x and y are now different.
--ai.move_full(my_leader, 11, 23)
--! attack with auto weapon/aggression
ai.attack(2, 12, 3, 12)
ai.attack(2, 12, 3, 12, 2)
--! attack with weapon selected
ai.attack(3, 11, 3, 12, 1)
--! attack with different aggression

View file

@ -218,8 +218,8 @@ static int cfun_ai_execute_attack(lua_State *L)
aggression = lua_tonumber(L, index+1);
}
if (!lua_isnoneornil(L, index)) {
attacker_weapon = lua_tointeger(L, index);
if (!lua_isnoneornil(L, index) && attacker_weapon != -1) {
attacker_weapon = lua_tointeger(L, index) - 1; // Done for consistency of the Lua style
}
ai::attack_result_ptr attack_result = ai::actions::execute_attack_action(side,true,attacker,defender,attacker_weapon,aggression);
@ -351,15 +351,19 @@ static int cfun_ai_get_avoid(lua_State *L)
for (int i = 0; it != locs.end(); ++it, ++i)
{
lua_pushinteger(L, i + 1); // Index for the map location
lua_createtable(L, 2, 0); // Table for a single map location
push_map_location(L, *it);
// Deprecated
//lua_createtable(L, 2, 0); // Table for a single map location
lua_pushstring(L, "x");
lua_pushinteger(L, it->x);
lua_settable(L, -3);
//lua_pushstring(L, "x");
//lua_pushinteger(L, it->x + 1);
//lua_settable(L, -3);
lua_pushstring(L, "y");
lua_pushinteger(L, it->y);
lua_settable(L, -3);
//lua_pushstring(L, "y");
//lua_pushinteger(L, it->y + 1);
//lua_settable(L, -3);
lua_settable(L, -3);
}