Skip to content

Commit

Permalink
MAIs: minor code simplification in AIs that take both [filter] and id=
Browse files Browse the repository at this point in the history
These are the AIs that used to use Behavior Candidate Actions (BCA).
  • Loading branch information
mattsc committed Mar 28, 2014
1 parent c72c8e7 commit 244ad22
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 128 deletions.
34 changes: 12 additions & 22 deletions data/ai/micro_ais/cas/ca_coward.lua
Expand Up @@ -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)

Expand Down
33 changes: 12 additions & 21 deletions data/ai/micro_ais/cas/ca_hunter.lua
Expand Up @@ -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
Expand All @@ -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
Expand Down
33 changes: 12 additions & 21 deletions data/ai/micro_ais/cas/ca_patrol.lua
Expand Up @@ -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, ",")
Expand Down
34 changes: 12 additions & 22 deletions data/ai/micro_ais/cas/ca_return_guardian.lua
Expand Up @@ -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
Expand All @@ -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
Expand Down
33 changes: 12 additions & 21 deletions data/ai/micro_ais/cas/ca_stationed_guardian.lua
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
33 changes: 12 additions & 21 deletions data/ai/micro_ais/cas/ca_zone_guardian.lua
Expand Up @@ -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
Expand Down

0 comments on commit 244ad22

Please sign in to comment.