]> git.lizzy.rs Git - lua-star.git/blobdiff - src/lua-star.lua
Merge pull request #1 from NickFlexer/exclude_diagonal_moving
[lua-star.git] / src / lua-star.lua
index b8306d98763bf85fac575073982cfb4fcab36917..7c337b065528dcb32ee1e69d339bfbea7c72c68b 100644 (file)
@@ -91,7 +91,7 @@ local function listItem(list, item)
 end
 
 -- (Internal) Requests adjacent map values around the given node.
-local function getAdjacent(width, height, node, positionIsOpenFunc)
+local function getAdjacent(width, height, node, positionIsOpenFunc, includeDiagonals)
 
     local result = { }
 
@@ -100,13 +100,21 @@ local function getAdjacent(width, height, node, positionIsOpenFunc)
         { x = -1, y = 0 },  -- left
         { x = 0, y = 1 },   -- bottom
         { x = 1, y = 0 },   -- right
-        -- include diagonal movements
-        { x = -1, y = -1 },   -- top left
-        { x = 1, y = -1 },   -- top right
-        { x = -1, y = 1 },   -- bot left
-        { x = 1, y = 1 },   -- bot right
     }
 
+    if includeDiagonals then
+        local diagonalMovements = {
+            { x = -1, y = -1 },   -- top left
+            { x = 1, y = -1 },   -- top right
+            { x = -1, y = 1 },   -- bot left
+            { x = 1, y = 1 },   -- bot right
+        }
+
+        for _, value in ipairs(diagonalMovements) do
+            table.insert(positions, value)
+        end
+    end
+
     for _, point in ipairs(positions) do
         local px = clamp(node.x + point.x, 1, width)
         local py = clamp(node.y + point.y, 1, height)
@@ -121,7 +129,7 @@ local function getAdjacent(width, height, node, positionIsOpenFunc)
 end
 
 -- Returns the path from start to goal, or false if no path exists.
-function module:find(width, height, start, goal, positionIsOpenFunc, useCache)
+function module:find(width, height, start, goal, positionIsOpenFunc, useCache, excludeDiagonalMoving)
 
     if useCache then
         local cachedPath = getCached(start, goal)
@@ -153,7 +161,7 @@ function module:find(width, height, start, goal, positionIsOpenFunc, useCache)
 
         if not success then
 
-            local adjacentList = getAdjacent(width, height, current, positionIsOpenFunc)
+            local adjacentList = getAdjacent(width, height, current, positionIsOpenFunc, not excludeDiagonalMoving)
 
             for _, adjacent in ipairs(adjacentList) do