added a wesnoth.modify_ai Lua function,

...continued work on the patrolling system, made more mess in lua_ai
arena
This commit is contained in:
Dmitry Kovalenko 2011-06-20 17:05:59 +00:00
parent 910ca163b7
commit 437b619397
2 changed files with 84 additions and 115 deletions

View file

@ -85,13 +85,66 @@ Gs^Fp , Gs^Fp , Wwf , Wwf , Mm , Rd
-- Define your global constants here.
-- ...
ai = {}
H.set_wml_var_metatable(_G)
-- Define your global functions here.
-- ...
>>
[/lua]
-- Patrol function, to be moved out in a separate file
function patrol_gen(n, wp) -- n is the name of the unit, like Kiressh
-- wp - a table of waypoint tables of form {x,y}
--wesnoth.message("Creation of the patrol func") -- debug info
local unit = wesnoth.get_units({name=n})[1]
local x, y = unit.x, unit.y
local wpn = 1 --WayPoint Number - we have to remember which waypoint we are heading to
if (x == wp[1].x and y == wp[1].y) then
wpn = wpn + 1
--w1, w2 = w2, w2 -- if we are standing on the first waypoint, swap them
end
--local waypoints = {w1, w2} -- this form might be just received from the args
local wpcount = # wp
-- wesnoth.message ("Eof creation")-- debug info
return
function()
--wesnoth.message(tostring(waypoints[1].x).." YES")
-- wesnoth.message("Entering patrol execution code") -- debug info
x, y = unit.x, unit.y
if (x == wp[wpn].x and y == wp[wpn].y) then
wpn = wpn % wpcount + 1 -- advance by one waypoint(this construct loops in range [1, wpcount])
-- wpn = 3 - wpn -- hardcoded for two waypoints -- deprecated
end
--wesnoth.message("Going to waypoint number " .. wpn .. " [" .. wp[wpn].x .. ";" .. wp[wpn].y .. "]")
ai.move_full(unit, wp[wpn].x, wp[wpn].y)
end
end
function patrol_eval()
return 1000000
end
patrol_rark = patrol_gen("Rark", {{x=14, y=7}, {x=15, y=7}, {x=15, y=8}, {x=14, y=8}}) -- need to find a solution for this
-- End of patrol function // patrol_gen(ai, "Rark", {{x=14, y=7}, {x=15, y=7}, {x=15, y=8}, {x=14, y=8}})
>>
[/lua]
[/event]
[event]
name=side 2 turn 2
first_time_only=yes
{MODIFY_AI_ADD_CANDIDATE_ACTION 2 ca_loop (
[candidate_action]
engine=lua
name=patrol
evaluation="return patrol_eval()"
execution="patrol_rark()"
[/candidate_action]
)}
[/event]
[side]
@ -213,81 +266,20 @@ Gs^Fp , Gs^Fp , Wwf , Wwf , Mm , Rd
generate_name=yes
[/unit]
[ai]
[aspect]
id=number_of_possible_recruits_to_force_recruit
engine=lua
value=0.34
[/aspect]
[aspect]
id=passive_leader
engine=lua
value=true
[/aspect]
[aspect]
id=passive_leader_shares_keep
engine=lua
code=return true
[/aspect]
[aspect]
id=scout_village_targeting
engine=lua
value=0.36
[/aspect]
[aspect]
id=simple_targeting
engine=lua
value=true
[/aspect]
[aspect]
id=recruitment_ignore_bad_combat
engine=lua
value=false
[/aspect]
[aspect]
id=village_value
engine=lua
value=1.2
[/aspect]
[aspect]
id=support_villages
engine=lua
value=true
[/aspect]
[aspect]
id=villages_per_scout
engine=lua
value=2
[/aspect]
[aspect]
id=grouping
engine=lua
value="'defensive'"
[/aspect]
[aspect]
id=recruitment_pattern
engine=lua
value=<< {'fighter','scout'} >>
[/aspect]
[aspect]
id=leader_goal
engine=lua
value=<< {x=14, y=10} >>
[/aspect]
[aspect]
id=avoid
engine=lua
value=<< { x = '2-4', y = '23-25'} >>
[/aspect]
[ai]
version=10710
[engine]
name="lua"
code= <<
--! ==============================================================
local ai = ...
ai = ...
for k,v in pairs(ai) do
wesnoth.message("*" .. k .. " " ..tostring(v))
end
local my_ai = { }
local ai_stdlib = wesnoth.require('ai/lua/stdlib.lua');
ai_stdlib.init(ai)
@ -316,43 +308,9 @@ function my_ai:candidate_action_execution_hello2()
self:do_moves()
end
function my_ai:patrol_eval()
wesnoth.message('Considering to patrol, checking, whether everything is ok')
return 10000
end
function my_ai:patrol_gen(ai, n, wp) -- n is the name of the unit, like Kiressh
-- wp - a table of waypoint tables of form {x,y}
--wesnoth.message("Creation of the patrol func") -- debug info
local unit = wesnoth.get_units({name=n})[1]
local x, y = unit.x, unit.y
local wpn = 1 --WayPoint Number - we have to remember which waypoint we are heading to
if (x == wp[1].x and y == wp[1].y) then
wpn = wpn + 1
--w1, w2 = w2, w2 -- if we are standing on the first waypoint, swap them
end
--local waypoints = {w1, w2} -- this form might be just received from the args
local wpcount = # wp
-- wesnoth.message ("Eof creation")-- debug info
return
function()
--wesnoth.message(tostring(waypoints[1].x).." YES")
-- wesnoth.message("Entering patrol execution code") -- debug info
x, y = unit.x, unit.y
if (x == wp[wpn].x and y == wp[wpn].y) then
wpn = wpn % wpcount + 1 -- advance by one waypoint(this construct loops in range [1, wpcount])
-- wpn = 3 - wpn -- hardcoded for two waypoints -- deprecated
end
--wesnoth.message("Going to waypoint number " .. wpn .. " [" .. wp[wpn].x .. ";" .. wp[wpn].y .. "]")
ai.move_full(unit, wp[wpn].x, wp[wpn].y)
end
end
my_ai.patrol_exec = my_ai:patrol_gen(ai, "Rark", {{x=14, y=7}, {x=15, y=7}, {x=15, y=8}, {x=14, y=8}})
--my_ai.patrol_exec = my_ai:patrol_gen(ai, "Rark", {{x=14, y=7}, {x=15, y=7}, {x=15, y=8}, {x=14, y=8}})
function my_ai:do_moves()
@ -378,7 +336,7 @@ function my_ai:do_moves()
-- wesnoth.message("->" .. l.x .. " " .. l.y)
-- end
--end
wesnoth.modify_ai({side=2, action="delete", path="stage[ca_loop].candidate_action[firstca]"})
my_leader = wesnoth.get_units({side = 1, can_recruit=yes})[1].name
--wesnoth.wml_actions.label({ text = "suitable_keep", x = x, y = y })
@ -409,15 +367,11 @@ return my_ai
[/engine]
[stage]
name=testing_ai_default::candidate_action_evaluation_loop
[candidate_action]
engine=lua
name=patrol
evaluation="return (...):patrol_eval()"
execution="(...):patrol_exec()"
[/candidate_action]
id=ca_loop
[candidate_action]
engine=lua
name=first
id=firstca
evaluation="return (...):candidate_action_evaluation_hello()"
execution="local ai, cfg = ...; ai:candidate_action_execution_hello(cfg)"
[/candidate_action]

View file

@ -36,6 +36,7 @@
#include "scripting/lua_api.hpp"
#include "actions.hpp"
#include "ai/manager.hpp"
#include "attack_prediction.hpp"
#include "filesystem.hpp"
#include "foreach.hpp"
@ -3217,6 +3218,19 @@ static int impl_theme_items_set(lua_State *L)
return 0;
}
/**
* Lua frontend to the modify_ai functionality
*/
static int intf_modify_ai(lua_State *L)
{
config cfg;
luaW_toconfig(L, -1, cfg);
ERR_LUA << cfg.debug() << std::endl;
int side = cfg["side"];
ai::manager::modify_active_ai_for_side(side, cfg);
return 0;
}
LuaKernel::LuaKernel(const config &cfg)
: mState(luaL_newstate()), level_(cfg)
{
@ -3280,6 +3294,7 @@ LuaKernel::LuaKernel(const config &cfg)
{ "match_side", &intf_match_side },
{ "match_unit", &intf_match_unit },
{ "message", &intf_message },
{ "modify_ai", &intf_modify_ai },
{ "play_sound", &intf_play_sound },
{ "put_recall_unit", &intf_put_recall_unit },
{ "put_unit", &intf_put_unit },