Migrate ai_helper.LS_random_hex to location_set:random

This commit is contained in:
Celtic Minstrel 2017-05-10 17:39:41 -04:00
parent f6c04e7dd1
commit 0539a68eb4
3 changed files with 14 additions and 16 deletions

View file

@ -527,21 +527,6 @@ function ai_helper.get_LS_xy(index)
return xy[1], xy[2] return xy[1], xy[2]
end end
function ai_helper.LS_random_hex(map)
-- Select a random hex from the hexes in location set @map
-- This seems "inelegant", but I can't come up with another way without creating an extra array
-- Return -1, -1 if @map is empty
local r = math.random(map:size())
local i, xr, yr = 1, -1, -1
map:iter( function(x, y, v)
if (i == r) then xr, yr = x, y end
i = i + 1
end)
return xr, yr
end
--------- Location, position or hex related helper functions ---------- --------- Location, position or hex related helper functions ----------
function ai_helper.cartesian_coords(x, y) function ai_helper.cartesian_coords(x, y)

View file

@ -38,7 +38,7 @@ function ca_herding_sheep_move:execution(cfg)
-- Choose one of the possible locations at random (or the current location, if no move possible) -- Choose one of the possible locations at random (or the current location, if no move possible)
local x, y = sheep.x, sheep.y local x, y = sheep.x, sheep.y
if (reach_map:size() > 0) then if (reach_map:size() > 0) then
x, y = AH.LS_random_hex(reach_map) x, y = reach_map:random()
end end
-- If this move remains within herding area or dogs have no moves left, or sheep doesn't move -- If this move remains within herding area or dogs have no moves left, or sheep doesn't move

View file

@ -192,6 +192,19 @@ function methods:to_triples()
return res return res
end end
function methods:random()
-- Select a random hex from the hexes in the location set
-- This seems "inelegant", but I can't come up with another way without creating an extra array
-- Return -1, -1 if empty
local r = wesnoth.random(self:size())
local i, xr, yr = 1, -1, -1
map:iter( function(x, y, v)
if (i == r) then xr, yr = x, y end
i = i + 1
end)
return xr, yr
end
function location_set.create() function location_set.create()
if wesnoth.get_map_size then if wesnoth.get_map_size then
-- If called from the mapgen kernel, there's no map -- If called from the mapgen kernel, there's no map