summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/dat/clua
diff options
context:
space:
mode:
authorJude Brown <bookofjude@users.sourceforge.net>2009-11-21 18:01:56 +1000
committerJude Brown <bookofjude@users.sourceforge.net>2009-11-21 18:03:43 +1000
commite3f6a41cd18e2f6ee3b65c5c1a3dc6fe0be475b5 (patch)
tree9c3af9cb83ecf8122c0debaac515a309d2af3f65 /crawl-ref/source/dat/clua
parentf2552fc0902e2ea33520135edac64829e587483d (diff)
downloadcrawl-ref-e3f6a41cd18e2f6ee3b65c5c1a3dc6fe0be475b5.tar.gz
crawl-ref-e3f6a41cd18e2f6ee3b65c5c1a3dc6fe0be475b5.zip
New Lua iterator: circle_iterator. Fix LOS/adjactent iterator.
LOS and adjacent iterators were only working if include_center was set to true; setting it to false (or nil) results in no points being returned.
Diffstat (limited to 'crawl-ref/source/dat/clua')
-rw-r--r--crawl-ref/source/dat/clua/iter.lua67
1 files changed, 61 insertions, 6 deletions
diff --git a/crawl-ref/source/dat/clua/iter.lua b/crawl-ref/source/dat/clua/iter.lua
index 9972a1208c..568fed991b 100644
--- a/crawl-ref/source/dat/clua/iter.lua
+++ b/crawl-ref/source/dat/clua/iter.lua
@@ -119,6 +119,11 @@ function iter.los_iterator (include_center, filter, center)
end
local top_corner = dgn.point(y_x - 8, y_y - 8)
local bottom_corner = dgn.point(y_x + 8, y_y + 8)
+
+ if include_center ~= true then
+ include_center = false
+ end
+
local function check_los (point)
local _x, _y = point:xy()
@@ -128,10 +133,8 @@ function iter.los_iterator (include_center, filter, center)
end
end
- if include_center == true and y_x == _x and y_y == y then
- return true
- else
- return false
+ if y_x == _x and y_y == y then
+ return include_center
end
if you.see_cell(_x, _y) then
@@ -157,6 +160,11 @@ function iter.adjacent_iterator (include_center, filter, center)
end
local top_corner = dgn.point(y_x - 1, y_y - 1)
local bottom_corner = dgn.point(y_x + 1, y_y + 1)
+
+ if include_center ~= true then
+ include_center = false
+ end
+
local function check_adj (point)
local _x, _y = point:xy()
@@ -166,11 +174,58 @@ function iter.adjacent_iterator (include_center, filter, center)
end
end
- if include_center ~= true and y_x == _x and y_y == y then
- return false
+ if y_x == _x and y_y == y then
+ return include_center
end
return true
end
return iter.rect_iterator(top_corner, bottom_corner, check_adj)
end
+
+-------------------------------------------------------------------------------
+-- circle_iterator
+-------------------------------------------------------------------------------
+
+function iter.circle_iterator (radius, include_center, filter, center)
+ if radius == nil then
+ error("circle_iterator needs a radius")
+ end
+
+ local y_x, y_y
+
+ if include_center ~= true then
+ include_center = false
+ end
+
+ if center == nil then
+ y_x, y_y = you.pos()
+ else
+ y_x, y_y = center:xy()
+ end
+
+ local top_corner = dgn.point(y_x - radius, y_y - radius)
+ local bottom_corner = dgn.point(y_x + radius, y_y + radius)
+ local function check_dist (point)
+ local _x, _y = point:xy()
+ local dist = dgn.distance(y_x, y_y, _x, _y)
+
+ if filter ~= nil then
+ if not filter(point) then
+ return false
+ end
+ end
+
+ if y_x == _x and y_y == y then
+ return include_center
+ end
+
+ if dist >= (radius * radius) + 1 then
+ return false
+ end
+
+ return true
+ end
+ return iter.rect_iterator(top_corner, bottom_corner, check_dist)
+end
+