Migrate ai_helper.LS_random_hex to location_set:random
This commit is contained in:
parent
f6c04e7dd1
commit
0539a68eb4
3 changed files with 14 additions and 16 deletions
|
@ -527,21 +527,6 @@ function ai_helper.get_LS_xy(index)
|
|||
return xy[1], xy[2]
|
||||
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 ----------
|
||||
|
||||
function ai_helper.cartesian_coords(x, y)
|
||||
|
|
|
@ -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)
|
||||
local x, y = sheep.x, sheep.y
|
||||
if (reach_map:size() > 0) then
|
||||
x, y = AH.LS_random_hex(reach_map)
|
||||
x, y = reach_map:random()
|
||||
end
|
||||
|
||||
-- If this move remains within herding area or dogs have no moves left, or sheep doesn't move
|
||||
|
|
|
@ -192,6 +192,19 @@ function methods:to_triples()
|
|||
return res
|
||||
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()
|
||||
if wesnoth.get_map_size then
|
||||
-- If called from the mapgen kernel, there's no map
|
||||
|
|
Loading…
Add table
Reference in a new issue