Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- findWay = function(
- startx, starty,
- endx, endy,
- mazeMap,
- wallTile, spaceTile, ...)
- if type(wallTile) == "table" then
- isWall = function(n)
- for _, v in pairs(wallTile) do
- if v == n then
- return true
- end
- end
- return false
- end
- else
- isWall = function(n)
- return wallTile == n
- end
- end
- if type(spaceTile) == "table" then
- isSpace = function(n)
- for _, v in pairs(spaceTile) do
- if v == n then
- return true
- end
- end
- return false
- end
- else
- isSpace = function(n)
- return spaceTile == n
- end
- end
- do -- Fast error check
- local _, err = ...
- assert(type(mazeMap) == "table" or err, "Mazemap is "..type(mazeMap).." table expected!")
- assert(isSpace(mazeMap[startx][starty]) or err, "Start position is not clear!")
- assert(isSpace(mazeMap[endx][endy]) or err, "End position is not clear!")
- assert(wallTile ~= spaceTile or err, "Wall tile and Space tile are the same!")
- end
- do -- Create default Varibles
- yieldCount = 0
- doYield = 1000
- nWays = 1
- minDepth = 65535
- mazeMapDepth = { }
- allWays = { }
- exitWays = { }
- bigMap = true
- retSingleExit = true
- end
- do -- Check args
- local t = ...
- if type(t) == "table" then
- doYield = t.doYield or doYield
- retSingleExit = t.retSingleExit or retSingleExit
- minDepth = t.minDepth or minDepth
- -- bigMap = t.bigMap or bigMap
- end
- end
- do -- Create functions
- getSTIT = function(tbl) -- get smallest table in table
- local smlst = -1
- for i, v in pairs(tbl) do
- if smlst == -1 or #tbl[smlst] > #v then
- smlst = i
- end
- end
- return tbl[smlst]
- end
- dereference = function(tbl)
- y = 0
- local ret = { }
- for i, v in pairs(tbl or { }) do
- if type(v) == "table" then
- ret[i] = dereference(v)
- else
- ret[i] = v
- end
- end
- return ret
- end
- end
- for x=1, #mazeMap do
- mazeMapDepth[x] = { }
- for y=1, #mazeMap[x] do
- mazeMapDepth[x][y] = minDepth
- end
- end
- algorithm = function( -- Main body
- currx, curry,
- endx, endy,
- mazeMap,
- depth, nWay)
- yieldCount = yieldCount + 1
- if currx == endx and curry == endy then
- table.insert(exitWays, dereference(allWays[nWay]))
- return nil
- end
- -- if bigMap and #exitWays > 0 and #getSTIT(exitWays) > nWay then
- -- return nil
- -- end
- if yieldCount % doYield == 0 then
- os.sleep(0)
- end
- local firstWay = true
- local oldWay = dereference(allWays[nWay] or { })
- if type(mazeMap[currx + 1]) == "table" then
- if isSpace(mazeMap[currx + 1][curry]) and mazeMapDepth[currx + 1][curry] > depth then
- mazeMapDepth[currx + 1][curry] = depth
- if allWays[nWay] == nil then
- allWays[nWay] = { }
- end
- table.insert(allWays[nWay], { currx + 1, curry })
- algorithm(currx + 1, curry, endx, endy, mazeMap, depth + 1, nWay)
- firstWay = false
- end
- end
- if type(mazeMap[currx - 1]) == "table" then
- if isSpace(mazeMap[currx - 1][curry]) and mazeMapDepth[currx - 1][curry] > depth then
- mazeMapDepth[currx - 1][curry] = depth
- if firstWay then
- if allWays[nWay] == nil then
- allWays[nWay] = { }
- end
- table.insert(allWays[nWay], { currx - 1, curry })
- algorithm(currx - 1, curry, endx, endy, mazeMap, depth + 1, nWay)
- firstWay = false
- else
- nWays = nWays + 1
- allWays[nWays] = dereference(oldWay)
- table.insert(allWays[nWays], { currx - 1, curry })
- algorithm(currx - 1, curry, endx, endy, mazeMap, depth + 1, nWays)
- end
- end
- end
- if isSpace(mazeMap[currx][curry + 1]) and mazeMapDepth[currx][curry + 1] > depth then
- mazeMapDepth[currx][curry + 1] = depth
- if firstWay then
- if allWays[nWay] == nil then
- allWays[nWay] = { }
- end
- table.insert(allWays[nWay], { currx, curry + 1 })
- algorithm(currx, curry + 1, endx, endy, mazeMap, depth + 1, nWay)
- firstWay = false
- else
- nWays = nWays + 1
- allWays[nWays] = dereference(oldWay)
- table.insert(allWays[nWays], { currx, curry + 1 })
- algorithm(currx, curry + 1, endx, endy, mazeMap, depth + 1, nWays)
- end
- end
- if isSpace(mazeMap[currx][curry - 1]) and mazeMapDepth[currx][curry - 1] > depth then
- mazeMapDepth[currx][curry - 1] = depth
- if firstWay then
- if allWays[nWay] == nil then
- allWays[nWay] = { }
- end
- table.insert(allWays[nWay], { currx, curry - 1 })
- algorithm(currx, curry - 1, endx, endy, mazeMap, depth + 1, nWay)
- firstWay = false
- else
- nWays = nWays + 1
- allWays[nWays] = dereference(oldWay)
- table.insert(allWays[nWays], { currx, curry - 1 })
- algorithm(currx, curry - 1, endx, endy, mazeMap, depth + 1, nWays)
- end
- end
- end
- algorithm(startx, starty, endx, endy, mazeMap, 0, 1)
- local tReturn = dereference(getSTIT(exitWays))
- do -- Delete Varible
- isWall = nil
- isSpace = nil
- yieldCount = nil
- doYield = nil
- nWays = nil
- mazeMapDepth = nil
- minDepth = nil
- allWays = nil
- exitWays = nil
- retSingleExit = nil
- bigMap = nil
- getSTIT = nil
- dereference = nil
- algorithm = nil
- end
- return tReturn or false
- end
- turnImage = function(tbl)
- ima = { }
- max = 0
- for i, v in pairs(tbl) do
- if #v > max then
- max = #v
- end
- end
- for i=1, max do
- ima[i] = { }
- for _, v in pairs(tbl) do
- table.insert(ima[i], v[i])
- end
- end
- return ima
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement