diff options
author | wesley <wesley.werner@gmail.com> | 2021-07-26 07:18:50 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-26 07:18:50 +1000 |
commit | 1efbbbed996f60ae53934179325376bf11c3b31f (patch) | |
tree | 97739cfe58a9d970810bda387d56996c3e41e7f7 /src | |
parent | 281bc15b671406f785838b2b105d57079f31df38 (diff) | |
parent | fedc1dc38c4a6d3106de88acc1076e1fb2e53720 (diff) | |
download | lua-star-1efbbbed996f60ae53934179325376bf11c3b31f.tar.xz |
Merge pull request #1 from NickFlexer/exclude_diagonal_moving
added option to disable diagonal movement
Diffstat (limited to 'src')
-rw-r--r-- | src/lua-star.lua | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/src/lua-star.lua b/src/lua-star.lua index b8306d9..7c337b0 100644 --- a/src/lua-star.lua +++ b/src/lua-star.lua @@ -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 |