MAIs: minor code simplification in AIs that take both [filter] and id=

These are the AIs that used to use Behavior Candidate Actions (BCA).
This commit is contained in:
mattsc 2014-03-28 07:00:59 -07:00
parent c72c8e7ff2
commit 244ad22ce2
6 changed files with 72 additions and 128 deletions

View file

@ -4,35 +4,25 @@ local AH = wesnoth.require "ai/lua/ai_helper.lua"
local ca_coward = {}
function ca_coward:evaluation(ai, cfg)
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 filter = cfg.filter or { id = cfg.id }
local unit = wesnoth.get_units({
side = wesnoth.current.side,
{ "and", filter },
formula = '$this_unit.moves > 0' }
)[1]
-- Check if unit exists as sticky BCAs are not always removed successfully
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
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 filter = cfg.filter or { id = cfg.id }
local unit = wesnoth.get_units({
side = wesnoth.current.side,
{ "and", filter },
formula = '$this_unit.moves > 0' }
)[1]
local reach = wesnoth.find_reach(unit)

View file

@ -39,16 +39,12 @@ local function hunter_attack_weakest_adj_enemy(ai, unit)
end
function ca_hunter:evaluation(ai, cfg)
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 filter = cfg.filter or { id = cfg.id }
local unit = wesnoth.get_units({
side = wesnoth.current.side,
{ "and", filter },
formula = '$this_unit.moves > 0' }
)[1]
if unit then return cfg.ca_score end
return 0
@ -60,17 +56,12 @@ function ca_hunter:execution(ai, cfg)
-- hunting_ground, then retreats to
-- position given by 'home_x,home_y' for 'rest_turns' turns, or until fully healed
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('Hunter: ', unit.id)
local filter = cfg.filter or { id = cfg.id }
local unit = wesnoth.get_units({
side = wesnoth.current.side,
{ "and", filter },
formula = '$this_unit.moves > 0' }
)[1]
-- If hunting_status is not set for the unit -> default behavior -> hunting
if (not unit.variables.hunting_status) then

View file

@ -4,33 +4,24 @@ local AH = wesnoth.require "ai/lua/ai_helper.lua"
local ca_patrol = {}
function ca_patrol:evaluation(ai, cfg)
local patrol
if cfg.filter then
patrol = wesnoth.get_units({
side = wesnoth.current.side,
{ "and", cfg.filter },
formula = '$this_unit.moves > 0' }
)[1]
else
patrol = wesnoth.get_units({ id = cfg.id, formula = '$this_unit.moves > 0' })[1]
end
local filter = cfg.filter or { id = cfg.id }
local patrol = wesnoth.get_units({
side = wesnoth.current.side,
{ "and", filter },
formula = '$this_unit.moves > 0' }
)[1]
-- Check if unit exists as sticky BCAs are not always removed successfully
if patrol then return cfg.ca_score end
return 0
end
function ca_patrol:execution(ai, cfg, self)
local patrol
if cfg.filter then
patrol = wesnoth.get_units({
side = wesnoth.current.side,
{ "and", cfg.filter },
formula = '$this_unit.moves > 0' }
)[1]
else
patrol = wesnoth.get_units({ id = cfg.id, formula = '$this_unit.moves > 0' })[1]
end
local filter = cfg.filter or { id = cfg.id }
local patrol = wesnoth.get_units({
side = wesnoth.current.side,
{ "and", filter },
formula = '$this_unit.moves > 0' }
)[1]
cfg.waypoint_x = AH.split(cfg.waypoint_x, ",")
cfg.waypoint_y = AH.split(cfg.waypoint_y, ",")

View file

@ -3,18 +3,13 @@ local AH = wesnoth.require "ai/lua/ai_helper.lua"
local ca_return_guardian = {}
function ca_return_guardian:evaluation(ai, cfg)
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 filter = cfg.filter or { id = cfg.id }
local unit = wesnoth.get_units({
side = wesnoth.current.side,
{ "and", filter },
formula = '$this_unit.moves > 0' }
)[1]
-- Check if unit exists as sticky BCAs are not always removed successfully
if unit then
if ((unit.x ~= cfg.return_x) or (unit.y ~= cfg.return_y)) then
return cfg.ca_score
@ -26,17 +21,12 @@ function ca_return_guardian:evaluation(ai, cfg)
end
function ca_return_guardian:execution(ai, cfg)
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)
local filter = cfg.filter or { id = cfg.id }
local unit = wesnoth.get_units({
side = wesnoth.current.side,
{ "and", filter },
formula = '$this_unit.moves > 0' }
)[1]
-- In case the return hex is occupied:
local x, y = cfg.return_x, cfg.return_y

View file

@ -4,18 +4,13 @@ local AH = wesnoth.require "ai/lua/ai_helper.lua"
local ca_stationed_guardian = {}
function ca_stationed_guardian:evaluation(ai, cfg)
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 filter = cfg.filter or { id = cfg.id }
local unit = wesnoth.get_units({
side = wesnoth.current.side,
{ "and", filter },
formula = '$this_unit.moves > 0' }
)[1]
-- Check if unit exists as sticky BCAs are not always removed successfully
if unit then return cfg.ca_score end
return 0
end
@ -24,16 +19,12 @@ 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
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 filter = cfg.filter or { id = cfg.id }
local unit = wesnoth.get_units({
side = wesnoth.current.side,
{ "and", filter },
formula = '$this_unit.moves > 0' }
)[1]
-- find if there are enemies within 'distance'
local enemies = wesnoth.get_units {

View file

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