All Guardian MAI [micro_ai] tags: add optional [filter] tag

This commit is contained in:
mattsc 2013-11-01 17:05:55 -07:00
parent 0670630ff9
commit 08a722e248
5 changed files with 105 additions and 17 deletions

View file

@ -4,17 +4,36 @@ local AH = wesnoth.require "ai/lua/ai_helper.lua"
local ca_coward = {}
function ca_coward:evaluation(ai, cfg)
local unit = wesnoth.get_units{ id = cfg.id }[1]
local unit
if cfg.filter then
unit = wesnoth.get_units({
side = wesnoth.current.side,
{ "and", cfg.filter },
formula = '$this_unit.moves > 0' }
)[1]
else
unit = wesnoth.get_units({ id = cfg.id, formula = '$this_unit.moves > 0' })[1]
end
-- Check if unit exists as sticky BCAs are not always removed successfully
if unit and (unit.moves > 0) then return cfg.ca_score end
if unit then return cfg.ca_score end
return 0
end
-- cfg parameters: id, distance, seek_x, seek_y, avoid_x, avoid_y
function ca_coward:execution(ai, cfg)
--print("Coward exec " .. cfg.id)
local unit = wesnoth.get_units{ id = cfg.id }[1]
local unit
if cfg.filter then
unit = wesnoth.get_units({
side = wesnoth.current.side,
{ "and", cfg.filter },
formula = '$this_unit.moves > 0' }
)[1]
else
unit = wesnoth.get_units({ id = cfg.id, formula = '$this_unit.moves > 0' })[1]
end
local reach = wesnoth.find_reach(unit)
-- enemy units within reach
local enemies = wesnoth.get_units {

View file

@ -3,7 +3,16 @@ local AH = wesnoth.require "ai/lua/ai_helper.lua"
local ca_return_guardian = {}
function ca_return_guardian:evaluation(ai, cfg)
local unit = wesnoth.get_units { id = cfg.id }[1]
local unit
if cfg.filter then
unit = wesnoth.get_units({
side = wesnoth.current.side,
{ "and", cfg.filter },
formula = '$this_unit.moves > 0' }
)[1]
else
unit = wesnoth.get_units({ id = cfg.id, formula = '$this_unit.moves > 0' })[1]
end
-- Check if unit exists as sticky BCAs are not always removed successfully
if unit then
@ -17,7 +26,16 @@ function ca_return_guardian:evaluation(ai, cfg)
end
function ca_return_guardian:execution(ai, cfg)
local unit = wesnoth.get_units { id = cfg.id }[1]
local unit
if cfg.filter then
unit = wesnoth.get_units({
side = wesnoth.current.side,
{ "and", cfg.filter },
formula = '$this_unit.moves > 0' }
)[1]
else
unit = wesnoth.get_units({ id = cfg.id, formula = '$this_unit.moves > 0' })[1]
end
--print("Exec guardian move",unit.id)
-- In case the return hex is occupied:

View file

@ -4,10 +4,19 @@ local AH = wesnoth.require "ai/lua/ai_helper.lua"
local ca_stationed_guardian = {}
function ca_stationed_guardian:evaluation(ai, cfg)
local unit = wesnoth.get_units { id = cfg.id }[1]
local unit
if cfg.filter then
unit = wesnoth.get_units({
side = wesnoth.current.side,
{ "and", cfg.filter },
formula = '$this_unit.moves > 0' }
)[1]
else
unit = wesnoth.get_units({ id = cfg.id, formula = '$this_unit.moves > 0' })[1]
end
-- Check if unit exists as sticky BCAs are not always removed successfully
if unit and (unit.moves > 0) then return cfg.ca_score end
if unit then return cfg.ca_score end
return 0
end
@ -15,7 +24,16 @@ function ca_stationed_guardian:execution(ai, cfg)
-- (s_x,s_y): coordinates where unit is stationed; tries to move here if there is nobody to attack
-- (g_x,g_y): location that the unit guards
local unit = wesnoth.get_units { id = cfg.id }[1]
local unit
if cfg.filter then
unit = wesnoth.get_units({
side = wesnoth.current.side,
{ "and", cfg.filter },
formula = '$this_unit.moves > 0' }
)[1]
else
unit = wesnoth.get_units({ id = cfg.id, formula = '$this_unit.moves > 0' })[1]
end
-- find if there are enemies within 'distance'
local enemies = wesnoth.get_units {

View file

@ -5,15 +5,34 @@ local LS = wesnoth.require "lua/location_set.lua"
local ca_zone_guardian = {}
function ca_zone_guardian:evaluation(ai, cfg)
local unit = wesnoth.get_units { id = cfg.id }[1]
local unit
if cfg.filter then
unit = wesnoth.get_units({
side = wesnoth.current.side,
{ "and", cfg.filter },
formula = '$this_unit.moves > 0' }
)[1]
else
unit = wesnoth.get_units({ id = cfg.id, formula = '$this_unit.moves > 0' })[1]
end
-- Check if unit exists as sticky BCAs are not always removed successfully
if unit and (unit.moves > 0) then return cfg.ca_score end
if unit then return cfg.ca_score end
return 0
end
function ca_zone_guardian:execution(ai, cfg)
local unit = wesnoth.get_units { id = cfg.id }[1]
local unit
if cfg.filter then
unit = wesnoth.get_units({
side = wesnoth.current.side,
{ "and", cfg.filter },
formula = '$this_unit.moves > 0' }
)[1]
else
unit = wesnoth.get_units({ id = cfg.id, formula = '$this_unit.moves > 0' })[1]
end
local reach = wesnoth.find_reach(unit)
local zone_enemy = cfg.filter_location_enemy or cfg.filter_location
-- enemy units within reach

View file

@ -307,21 +307,35 @@ function wesnoth.wml_actions.micro_ai(cfg)
--------- Micro AI Guardian -----------------------------------
elseif (cfg.ai_type == 'stationed_guardian') then
required_keys = { "id", "distance", "station_x", "station_y", "guard_x", "guard_y" }
if (not cfg.id) and (not H.get_child(cfg, "filter")) then
H.wml_error("Stationed Guardian [micro_ai] tag requires either id= key or [filter] tag")
end
required_keys = { "distance", "station_x", "station_y", "guard_x", "guard_y" }
optional_keys = { "id", "filter" }
CA_parms = { { ca_id = 'mai_stationed_guardian', location = 'ai/micro_ais/cas/ca_stationed_guardian.lua', score = cfg.ca_score or 300000 } }
elseif (cfg.ai_type == 'zone_guardian') then
required_keys = { "id", "filter_location" }
optional_keys = { "filter_location_enemy", "station_x", "station_y" }
if (not cfg.id) and (not H.get_child(cfg, "filter")) then
H.wml_error("Zone Guardian [micro_ai] tag requires either id= key or [filter] tag")
end
required_keys = { "filter_location" }
optional_keys = { "id", "filter", "filter_location_enemy", "station_x", "station_y" }
CA_parms = { { ca_id = 'mai_zone_guardian', location = 'ai/micro_ais/cas/ca_zone_guardian.lua', score = cfg.ca_score or 300000 } }
elseif (cfg.ai_type == 'return_guardian') then
required_keys = { "id", "return_x", "return_y" }
if (not cfg.id) and (not H.get_child(cfg, "filter")) then
H.wml_error("Return Guardian [micro_ai] tag requires either id= key or [filter] tag")
end
required_keys = { "return_x", "return_y" }
optional_keys = { "id", "filter" }
CA_parms = { { ca_id = 'mai_return_guardian', location = 'ai/micro_ais/cas/ca_return_guardian.lua', score = cfg.ca_score or 100010 } }
elseif (cfg.ai_type == 'coward') then
required_keys = { "id", "distance" }
optional_keys = { "seek_x", "seek_y","avoid_x","avoid_y" }
if (not cfg.id) and (not H.get_child(cfg, "filter")) then
H.wml_error("Coward [micro_ai] tag requires either id= key or [filter] tag")
end
required_keys = { "distance" }
optional_keys = { "id", "filter", "seek_x", "seek_y","avoid_x","avoid_y" }
CA_parms = { { ca_id = 'mai_coward', location = 'ai/micro_ais/cas/ca_coward.lua', score = cfg.ca_score or 300000 } }
--------- Micro AI Animals ------------------------------------