Zone Guardian MAI: only use reachable zone hexes as goals for the unit

This is done so that the unit does not leave the zone in order to get
to a far away zone hex.  Exception: if none of the zone hexes are
reachable, then all of them are possible goals, so that the unit moves
toward the zone.
This commit is contained in:
mattsc 2013-06-27 12:04:36 -07:00
parent 59f90c5338
commit 79f2147034

View file

@ -5,6 +5,7 @@ return {
local H = wesnoth.require "lua/helper.lua"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local LS = wesnoth.require "lua/location_set.lua"
----- The coward guardian AI -----
function engine:mai_guardian_coward_eval(cfg)
@ -244,11 +245,23 @@ return {
-- Otherwise choose one randomly from those given in filter_location
else
local width, height = wesnoth.get_map_size()
local locs = wesnoth.get_locations {
local locs_map = LS.of_pairs(wesnoth.get_locations {
x = '1-' .. width,
y = '1-' .. height,
{ "and", cfg.filter_location }
}
})
-- Check out which of those hexes the unit can reach
local reach_map = LS.of_pairs(wesnoth.find_reach(unit))
reach_map:inter(locs_map)
-- If it can reach some hexes, use only reachable locations,
-- otherwise move toward any (random) one from the entire set
if (reach_map:size() > 0) then
locs_map = reach_map
end
local locs = locs_map:to_pairs()
local newind = math.random(#locs)
newpos = {locs[newind][1], locs[newind][2]}
end