Fix and simplify Lua AI test scenario

This commit is contained in:
mattsc 2016-03-24 08:07:01 -07:00
parent ae8df114f6
commit 961d134ee2
3 changed files with 66 additions and 378 deletions

View File

@ -2,15 +2,20 @@
example_ca = {} example_ca = {}
function example_ca:eval(ai) function example_ca:evaluation(ai)
wesnoth.message("External eval says hi!") wesnoth.message("External CA evaluation says hi.")
return 10000 return 10000
end end
function example_ca:exec(ai) function example_ca:execution(ai)
wesnoth.message("External CA exec attacks!") wesnoth.message("External CA execution attacks.")
ai.attack(2, 12, 3, 12, 1, 1) -- showcasing the presence of the AI table
-- Note that there is no check whether these attacks are possible.
-- The CA will therefore be blacklisted the second time it gets called.
ai.attack(2, 12, 3, 12, 1, 1)
ai.attack(3, 13, 3, 12, 2, 1)
ai.attack(3, 11, 3, 12)
end end
return example_ca return example_ca

View File

@ -3,6 +3,10 @@
[test] [test]
name= _ "Test scenario for Lua AI" name= _ "Test scenario for Lua AI"
id=lua_ai
turns=90
{DEFAULT_SCHEDULE}
map_data=" map_data="
Hh , Hh , Gg , Wwf , Wwf , Gs^Fp , Mm , Hh , Gg , Gs^Fp , Gg , Hh , Gg , Mm , Hh , Mm , Wwf , Wwf , Hh , Gs^Fp , Hh , Mm , Mm Hh , Hh , Gg , Wwf , Wwf , Gs^Fp , Mm , Hh , Gg , Gs^Fp , Gg , Hh , Gg , Mm , Hh , Mm , Wwf , Wwf , Hh , Gs^Fp , Hh , Mm , Mm
Hh , Hh , Gg^Ve , Wwf , Wwf , Gs^Fp , Mm , Hh , Gg , Gs^Fp , Gg , Hh , Gg , Mm , Hh , Mm , Wwf , Wwf , Hh , Gs^Fp , Hh , Mm , Mm Hh , Hh , Gg^Ve , Wwf , Wwf , Gs^Fp , Mm , Hh , Gg , Gs^Fp , Gg , Hh , Gg , Mm , Hh , Mm , Wwf , Wwf , Hh , Gs^Fp , Hh , Mm , Mm
@ -32,84 +36,14 @@ Gs^Fp , Gs^Fp , Wwf , Wwf , Mm , Rd
Gs^Fp , Gs^Fp , Wwf , Wwf , Mm , Rd , Gs^Fp , Wwf , Wwf , Gg , Gg , Gg , Gg , Gg , Hh , Gg , Wwf , Wwf , Gg , Gg , Wwf , Mm , Mm Gs^Fp , Gs^Fp , Wwf , Wwf , Mm , Rd , Gs^Fp , Wwf , Wwf , Gg , Gg , Gg , Gg , Gg , Hh , Gg , Wwf , Wwf , Gg , Gg , Wwf , Mm , Mm
" "
turns=90
id=lua_ai
{DEFAULT_SCHEDULE}
[label]
x,y=16,5
text= _ "Patrol waypoint 1" # wmllint: no spellcheck
[/label]
[label]
x,y=16,15
text= _ "Patrol waypoint 2" # wmllint: no spellcheck
[/label]
[label]
x,y=3,14
text= _ "Formula priorities test"
[/label]
[label]
x,y=2,12
text= _ "first"
[/label]
[label]
x,y=3,11
text= _ "second"
[/label]
[label]
x,y=3,13
text= _ "third"
[/label]
[label]
x,y=8,5
text= _ "Location guarded (range = 3)" # wmllint: no spellcheck
[/label]
[event]
name=side 2 turn 1
first_time_only=yes
{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] [side]
type=Dwarvish Steelclad
id=side_1_leader
side=1 side=1
controller=human
id=side_1_leader
type=Dwarvish Steelclad
canrecruit=yes canrecruit=yes
recruit=Dwarvish Guardsman,Dwarvish Fighter,Dwarvish Thunderer,Thief,Poacher,Footpad recruit=Dwarvish Guardsman,Dwarvish Fighter,Dwarvish Thunderer,Thief,Poacher,Footpad
gold=100 gold=100
controller=human
[unit]
x,y=10,8
type="Elvish Archer"
hitpoints=1
generate_name=yes
[/unit]
[unit] [unit]
x,y=3,12 x,y=3,12
@ -136,14 +70,14 @@ Gs^Fp , Gs^Fp , Wwf , Wwf , Mm , Rd
[/side] [/side]
[side] [side]
#controller=human side=2
controller=ai
id=LuaAI
name=LuaAI name=LuaAI
type=Dark Sorcerer type=Dark Sorcerer
side=2
canrecruit=yes canrecruit=yes
recruit=Skeleton,Skeleton Archer,Walking Corpse,Ghost,Vampire Bat,Dark Adept,Ghoul recruit=Skeleton,Skeleton Archer,Walking Corpse,Ghost,Vampire Bat,Dark Adept,Ghoul
gold=100 gold=100
shroud=yes
[unit] [unit]
placement=recall placement=recall
@ -152,23 +86,6 @@ Gs^Fp , Gs^Fp , Wwf , Wwf , Mm , Rd
name= _ "Kiressh" name= _ "Kiressh"
[/unit] [/unit]
[unit]
x,y=8,5
type="Orcish Archer"
name= _ "Bilbo"
[/unit]
[unit]
x,y=3,8
type="Walking Corpse"
name= _ "Sirené"
[/unit]
[unit]
x,y=16,5
type="Wolf Rider"
name= _ "Rark"
[/unit]
[unit] [unit]
x,y=3,11 x,y=3,11
type="Goblin Spearman" type="Goblin Spearman"
@ -187,144 +104,59 @@ Gs^Fp , Gs^Fp , Wwf , Wwf , Mm , Rd
generate_name=yes generate_name=yes
[/unit] [/unit]
[unit]
x,y=7,20
type="Silver Mage"
generate_name=yes
[/unit]
[unit]
x,y=6,20
type="Ghost"
generate_name=yes
[/unit]
[unit]
x,y=15,22
type="Ghost"
generate_name=yes
[/unit]
[unit]
x,y=12,19
type="Ghost"
generate_name=yes
[/unit]
[unit]
x,y=10,6
type="Lich"
experience=149
generate_name=yes
[/unit]
[ai] [ai]
[goal]
name=protect_location
value=5
protect_radius=16
[criteria] #NOTE: this is a SLF, because we're protecting a location
x,y=42,20
[/criteria]
[/goal]
[goal]
name=lua_goal
value=6
engine=lua
code = <<
local t = {}
t[1] = {["value"]=2.3, ["type"]=3, ["loc"]={["x"]=5, ["y"]=6}}
t[2] = {["value"]=2.4, ["type"]=4, ["loc"]={["x"]=4, ["y"]=16}}
return t
>>
[/goal]
[aspect]
id=aggression
engine=lua
code = << return 0.23 >>
[/aspect]
[engine] [engine]
name="lua" name="lua"
code=<< code=<<
--! ============================================================== --! ==============================================================
local ai = ...
local debug_utils = wesnoth.require "~add-ons/Wesnoth_Lua_Pack/debug_utils.lua"
local my_ai = { } local my_ai = { }
-- data = { ["pers"] = 5, ["stringg"] = "stringg" }
local ai_stdlib = wesnoth.require('ai/lua/stdlib.lua');
ai_stdlib.init(ai, true)
function my_ai:stage_hello() function my_ai:stage_hello()
wesnoth.message('Lua AI stage says hello.')
--local att = ai.get_attacks() std_print('Lua AI stage says hello.')
--debug_utils.dbms(att,false,"variable",false)
--ai.debug.get_enemy_dst_src()
--debug_utils.dbms(ai,false,"variable",false)
local s = wesnoth.debug_ai(2)
--wesnoth.message("Hi", s)
--wesnoth.message(tostring(s["components"]["stage"]["ai_default_rca::candidate_action_evaluation_loop"]["candidate_action"]["second"]:exec(true)))
--debug_utils.dbms(s,false,"variable",false)
wesnoth.message("LuaAI agg", tostring(s.get_ai().get_aggression()))
end end
function my_ai:candidate_action_evaluation_hello() function my_ai:candidate_action_evaluation_hello()
wesnoth.message('hello from candidate action evaluation!') wesnoth.message('Hello from candidate action evaluation!')
std_print('Hello from candidate action evaluation!')
return 42, {test=123} return 42
end end
function my_ai:candidate_action_execution_hello(cfg) function my_ai:candidate_action_execution_hello(cfg)
wesnoth.message('hello from candidate action execution!') wesnoth.message('Hello from candidate action execution!')
--wesnoth.message('test value is ') -- .. cfg.test) -- ERROR: cfg.test is nil here std_print('Hello from candidate action execution!')
self:do_moves()
end end
function my_ai:candidate_action_evaluation_hello2() function my_ai:candidate_action_evaluation_hello2()
wesnoth.message('hello from second candidate action evaluation!') wesnoth.message('Hello from second candidate action evaluation!')
std_print('Hello from second candidate action evaluation!')
return 99 return 99
end end
function my_ai:candidate_action_execution_hello2() function my_ai:candidate_action_execution_hello2()
wesnoth.message('hello from second candidate action execution!') wesnoth.message('Hello from second candidate action execution!')
std_print('Hello from second candidate action execution!')
self:do_moves() self:do_moves()
end end
--wesnoth.modify_ai({side=2, action="delete", path="stage[ca_loop].candidate_action[firstca]"})
function my_ai:do_moves() function my_ai:do_moves()
wesnoth.message("ldbg", tostring(wesnoth.game_config.debug_lua)) -- Note that there is no check whether these actions are possible.
my_leader = wesnoth.get_units({side = 1, can_recruit=yes})[1].name -- The CA will therefore be blacklisted the second time it gets called.
ai.attack(2, 12, 3, 12, 1, 1)
ai.attack(3, 13, 3, 12, 2, 1)
ai.attack(3, 11, 3, 12)
--! full move. note that the my_leader still can be used altrough x and y are now different.
--ai.check_move(my_leader, 11, 23)
--! attack with auto weapon/aggression
ai.attack(2, 12, 3, 12, 4, 10)
--! attack with weapon selected
ai.check_attack(3, 11, 3, 12, 1)
--! attack with different aggression
ai.attack(3, 13, 3, 12, -1, 0.9)
if wesnoth.current.turn==1 then if wesnoth.current.turn==1 then
--! recruit on specific location
ai.recruit("Vampire Bat", 10, 22) ai.recruit("Vampire Bat", 10, 22)
--! recruit on any suitable location ai.recall("Kiressh", 11, 21)
ai.check_recruit("Skeleton")
--! recall on any suitable location.
ai.check_recall("Kiressh")
end end
end end
-- compulsory for the external CA's
function my_ai:get_ai()
return ai
end
return my_ai return my_ai
--! ============================================================== --! ==============================================================
>> >>
@ -334,22 +166,18 @@ return my_ai
id=ca_loop id=ca_loop
[candidate_action] [candidate_action]
engine=lua engine=lua
name=first name=first_CA
id=firstca id=first_CA
evaluation="return (...):candidate_action_evaluation_hello()" evaluation="return (...):candidate_action_evaluation_hello()"
execution="local ai, cfg = ...; ai:candidate_action_execution_hello(cfg)" execution="(...):candidate_action_execution_hello()"
[/candidate_action] [/candidate_action]
[candidate_action] [candidate_action]
engine=lua engine=lua
name=second id=second_CA
name=second_CA
evaluation="return (...):candidate_action_evaluation_hello2()" evaluation="return (...):candidate_action_evaluation_hello2()"
execution="(...):candidate_action_execution_hello2()" execution="(...):candidate_action_execution_hello2()"
[/candidate_action] [/candidate_action]
[candidate_action]
engine=lua
name=external
location="ai/lua/extCAexample.lua"
[/candidate_action]
[/stage] [/stage]
[stage] [stage]
name="another_stage" name="another_stage"
@ -359,5 +187,3 @@ return my_ai
[/ai] [/ai]
[/side] [/side]
[/test] [/test]
# vim: tabstop=4: shiftwidth=4: expandtab: softtabstop=4: autoindent:

View File

@ -1,8 +1,11 @@
#textdomain wesnoth-ai #textdomain wesnoth-ai
# @file data/scenario-luaai.cfg
[test] [test]
name= _ "Test scenario for Lua AI with no Lua engine defined" name= _ "Test scenario for Lua AI with no Lua engine defined"
id=lua_ai_no_engine
turns=90
{DEFAULT_SCHEDULE}
map_data=" map_data="
Hh , Hh , Gg , Wwf , Wwf , Gs^Fp , Mm , Hh , Gg , Gs^Fp , Gg , Hh , Gg , Mm , Hh , Mm , Wwf , Wwf , Hh , Gs^Fp , Hh , Mm , Mm Hh , Hh , Gg , Wwf , Wwf , Gs^Fp , Mm , Hh , Gg , Gs^Fp , Gg , Hh , Gg , Mm , Hh , Mm , Wwf , Wwf , Hh , Gs^Fp , Hh , Mm , Mm
Hh , Hh , Gg^Ve , Wwf , Wwf , Gs^Fp , Mm , Hh , Gg , Gs^Fp , Gg , Hh , Gg , Mm , Hh , Mm , Wwf , Wwf , Hh , Gs^Fp , Hh , Mm , Mm Hh , Hh , Gg^Ve , Wwf , Wwf , Gs^Fp , Mm , Hh , Gg , Gs^Fp , Gg , Hh , Gg , Mm , Hh , Mm , Wwf , Wwf , Hh , Gs^Fp , Hh , Mm , Mm
@ -32,84 +35,14 @@ Gs^Fp , Gs^Fp , Wwf , Wwf , Mm , Rd
Gs^Fp , Gs^Fp , Wwf , Wwf , Mm , Rd , Gs^Fp , Wwf , Wwf , Gg , Gg , Gg , Gg , Gg , Hh , Gg , Wwf , Wwf , Gg , Gg , Wwf , Mm , Mm Gs^Fp , Gs^Fp , Wwf , Wwf , Mm , Rd , Gs^Fp , Wwf , Wwf , Gg , Gg , Gg , Gg , Gg , Hh , Gg , Wwf , Wwf , Gg , Gg , Wwf , Mm , Mm
" "
turns=90
id=lua_ai_no_engine
{DEFAULT_SCHEDULE}
[label]
x,y=16,5
text= _ "Patrol waypoint 1" # wmllint: no spellcheck
[/label]
[label]
x,y=16,15
text= _ "Patrol waypoint 2" # wmllint: no spellcheck
[/label]
[label]
x,y=3,14
text= _ "Formula priorities test"
[/label]
[label]
x,y=2,12
text= _ "first"
[/label]
[label]
x,y=3,11
text= _ "second"
[/label]
[label]
x,y=3,13
text= _ "third"
[/label]
[label]
x,y=8,5
text= _ "Location guarded (range = 3)" # wmllint: no spellcheck
[/label]
[event]
name=side 2 turn 1
first_time_only=yes
{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] [side]
type=Dwarvish Steelclad
id=side_1_leader
side=1 side=1
controller=human
id=side_1_leader
type=Dwarvish Steelclad
canrecruit=yes canrecruit=yes
recruit=Dwarvish Guardsman,Dwarvish Fighter,Dwarvish Thunderer,Thief,Poacher,Footpad recruit=Dwarvish Guardsman,Dwarvish Fighter,Dwarvish Thunderer,Thief,Poacher,Footpad
gold=100 gold=100
controller=human
[unit]
x,y=10,8
type="Elvish Archer"
hitpoints=1
generate_name=yes
[/unit]
[unit] [unit]
x,y=3,12 x,y=3,12
@ -136,38 +69,14 @@ Gs^Fp , Gs^Fp , Wwf , Wwf , Mm , Rd
[/side] [/side]
[side] [side]
#controller=human side=2
controller=ai
id=LuaAI
name=LuaAI name=LuaAI
type=Dark Sorcerer type=Dark Sorcerer
side=2
canrecruit=yes canrecruit=yes
recruit=Skeleton,Skeleton Archer,Walking Corpse,Ghost,Vampire Bat,Dark Adept,Ghoul recruit=Skeleton,Skeleton Archer,Walking Corpse,Ghost,Vampire Bat,Dark Adept,Ghoul
gold=100 gold=100
shroud=yes
[unit]
placement=recall
type="Skeletal Dragon"
id="Kiressh"
name= _ "Kiressh"
[/unit]
[unit]
x,y=8,5
type="Orcish Archer"
name= _ "Bilbo"
[/unit]
[unit]
x,y=3,8
type="Walking Corpse"
name= _ "Sirené"
[/unit]
[unit]
x,y=16,5
type="Wolf Rider"
name= _ "Rark"
[/unit]
[unit] [unit]
x,y=3,11 x,y=3,11
@ -187,71 +96,19 @@ Gs^Fp , Gs^Fp , Wwf , Wwf , Mm , Rd
generate_name=yes generate_name=yes
[/unit] [/unit]
[unit]
x,y=7,20
type="Silver Mage"
generate_name=yes
[/unit]
[unit]
x,y=6,20
type="Ghost"
generate_name=yes
[/unit]
[unit]
x,y=15,22
type="Ghost"
generate_name=yes
[/unit]
[unit]
x,y=12,19
type="Ghost"
generate_name=yes
[/unit]
[unit]
x,y=10,6
type="Lich"
experience=149
generate_name=yes
[/unit]
[ai] [ai]
[goal]
name=protect_location
value=5
protect_radius=16
[criteria] #NOTE: this is a SLF, because we're protecting a location
x,y=42,20
[/criteria]
[/goal]
[goal]
name=lua_goal
value=6
engine=lua
code = <<
local t = {}
t[1] = {["value"]=2.3, ["type"]=3, ["loc"]={["x"]=5, ["y"]=6}}
t[2] = {["value"]=2.4, ["type"]=4, ["loc"]={["x"]=4, ["y"]=16}}
return t
>>
[/goal]
[aspect]
id=aggression
engine=lua
code = << return 0.23 >>
[/aspect]
[stage] [stage]
name=ai_default_rca::candidate_action_evaluation_loop name=ai_default_rca::candidate_action_evaluation_loop
id=ca_loop id=ca_loop
[candidate_action] [candidate_action]
engine=lua engine=lua
name=external name=external_CA
id=external_CA
location="ai/lua/extCAexample.lua" location="ai/lua/extCAexample.lua"
eval_parms=""
exec_parms=""
[/candidate_action] [/candidate_action]
[/stage] [/stage]
[/ai] [/ai]
[/side] [/side]
[/test] [/test]
# vim: tabstop=4: shiftwidth=4: expandtab: softtabstop=4: autoindent: