From 244ad22ce29038720cd685a85cbc45bb94e655d8 Mon Sep 17 00:00:00 2001 From: mattsc Date: Fri, 28 Mar 2014 07:00:59 -0700 Subject: [PATCH] MAIs: minor code simplification in AIs that take both [filter] and id= These are the AIs that used to use Behavior Candidate Actions (BCA). --- data/ai/micro_ais/cas/ca_coward.lua | 34 +++++++------------ data/ai/micro_ais/cas/ca_hunter.lua | 33 +++++++----------- data/ai/micro_ais/cas/ca_patrol.lua | 33 +++++++----------- data/ai/micro_ais/cas/ca_return_guardian.lua | 34 +++++++------------ .../micro_ais/cas/ca_stationed_guardian.lua | 33 +++++++----------- data/ai/micro_ais/cas/ca_zone_guardian.lua | 33 +++++++----------- 6 files changed, 72 insertions(+), 128 deletions(-) diff --git a/data/ai/micro_ais/cas/ca_coward.lua b/data/ai/micro_ais/cas/ca_coward.lua index 08dd8b277a02..ac2a12b325ff 100644 --- a/data/ai/micro_ais/cas/ca_coward.lua +++ b/data/ai/micro_ais/cas/ca_coward.lua @@ -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) diff --git a/data/ai/micro_ais/cas/ca_hunter.lua b/data/ai/micro_ais/cas/ca_hunter.lua index b919e688ac8f..af8f0f9271de 100644 --- a/data/ai/micro_ais/cas/ca_hunter.lua +++ b/data/ai/micro_ais/cas/ca_hunter.lua @@ -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 diff --git a/data/ai/micro_ais/cas/ca_patrol.lua b/data/ai/micro_ais/cas/ca_patrol.lua index 4ba4b9814a84..b05d33c21096 100644 --- a/data/ai/micro_ais/cas/ca_patrol.lua +++ b/data/ai/micro_ais/cas/ca_patrol.lua @@ -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, ",") diff --git a/data/ai/micro_ais/cas/ca_return_guardian.lua b/data/ai/micro_ais/cas/ca_return_guardian.lua index 609ea36eeb54..831910201d24 100644 --- a/data/ai/micro_ais/cas/ca_return_guardian.lua +++ b/data/ai/micro_ais/cas/ca_return_guardian.lua @@ -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 diff --git a/data/ai/micro_ais/cas/ca_stationed_guardian.lua b/data/ai/micro_ais/cas/ca_stationed_guardian.lua index 0cb37463eff9..fbb9dce329a7 100644 --- a/data/ai/micro_ais/cas/ca_stationed_guardian.lua +++ b/data/ai/micro_ais/cas/ca_stationed_guardian.lua @@ -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 { diff --git a/data/ai/micro_ais/cas/ca_zone_guardian.lua b/data/ai/micro_ais/cas/ca_zone_guardian.lua index 8b6c2bbd1b0f..358e0600fe2f 100644 --- a/data/ai/micro_ais/cas/ca_zone_guardian.lua +++ b/data/ai/micro_ais/cas/ca_zone_guardian.lua @@ -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