Merge pull request #115 from tacigar/tacigar/search_surrounding

Update search_surrounding algorithm
Close #113
This commit is contained in:
tacigar 2016-12-24 10:24:05 +09:00 committed by GitHub
commit 5f069b6d84
1 changed files with 39 additions and 6 deletions

View File

@ -6,12 +6,45 @@
maidroid_core._aux = {}
function maidroid_core._aux.search_surrounding(pos, pred, searching_range)
for x = -searching_range.x, searching_range.x do
for y = -searching_range.y, searching_range.y do
for z = -searching_range.z, searching_range.z do
local p = vector.add(pos, {x = x, y = y, z = z})
if pred(p) then
return p
pos = vector.round(pos)
local max_xz = math.max(searching_range.x, searching_range.z)
for j = -searching_range.y, searching_range.y do
local p = vector.add({x = 0, y = j, z = 0}, pos)
if pred(p) then
return p
end
end
for i = 0, max_xz do
for j = -searching_range.y, searching_range.y do
for k = -i, i do
if searching_range.x >= k and searching_range.z >= i then
local p = vector.add({x = k, y = j, z = i}, pos)
if pred(p) then
return p
end
p = vector.add({x = k, y = j, z = -i}, pos)
if pred(p) then
return p
end
end
if searching_range.z >= i and searching_range.z >= k then
if i ~= k then
local p = vector.add({x = i, y = j, z = k}, pos)
if pred(p) then
return p
end
end
if -i ~= k then
local p = vector.add({x = -i, y = j, z = k}, pos)
if pred(p) then
return p
end
end
end
end
end