Not a member of Pastebin yet?
                        Sign Up,
                        it unlocks many cool features!                    
                - -- Read map
 - -- Save tiles in a Cartesian plane, such that the top left corner is (0, 0).
 - -- Locate starting position via the 'K'
 - -- Walls are #
 - -- Whitespace is open.
 - -- GOAL:
 - -- Figure out if all spaces are accessible.
 - -- Pick a target, then iterate through all possible paths until
 - -- we find one that works, then set that tile as checked.
 - -- Have a base map table
 - -- Have a checked map table
 - local map = {}
 - local spacesChecked = {} -- Is 2D table in which spacesChecked[y][x] is a whitespace tile that has or has not been checked.
 - local function loadMap(path)
 - local fileHandle = fs.open(path, 'r')
 - if not fileHandle then
 - error("Could not open map at " .. path)
 - end
 - local height, width = fileHandle.readLine():match("(%d+)%s(%d+)")
 - print("Map Dimensions: [w = " .. width .. ", h = " .. height .. "]")
 - local spacesChecked = {}
 - local map = {}
 - local line = ""
 - for y = 1, height do
 - map[y] = {}
 - line = fileHandle.readLine()
 - for x = 1, width do
 - local tile = line:sub(x, x)
 - map[y][x] = tile
 - if tile == " " then
 - if not spacesChecked[y] then
 - spacesChecked[y] = {}
 - end
 - spacesChecked[y][x] = { checked = false, accessible = false }
 - elseif tile == "K" then
 - map.start = { x = x, y = y }
 - end
 - end
 - end
 - return map, spacesChecked
 - end
 - local function printMap(map, spacesChecked)
 - for y, line in ipairs(map) do
 - for x, char in ipairs(line) do
 - if spacesChecked[y] and spacesChecked[y][x] then
 - write(".")
 - else
 - write(char)
 - end
 - end
 - print("")
 - end
 - end
 - local function distanceTo(fromX, fromY, x, y)
 - return math.sqrt((x - fromX)^2 + (y - fromY)^2)
 - end
 - local function getSurroundingFreeSpace(map, x, y, destX, destY)
 - local freeSpace = {}
 - for _y = y - 1, y + 1 do
 - for _x = x - 1, x + 1 do
 - if not (_x == x and _y == y) and map[_y][_x] == " " then
 - table.insert(freeSpace, { x = _x, y = _y, d = distanceTo(_x, _y, destX, destY) })
 - end
 - end
 - end
 - return freeSpace
 - end
 - local function getTrueSize(_table)
 - local size = 0
 - for _, __ in pairs(_table) do
 - size = size + 1
 - end
 - return size
 - end
 - local function isSpaceAccessible(map, fromX, fromY, spaceX, spaceY)
 - if fromX == spaceX and fromY == spaceY then
 - return true
 - end
 - -- Gather surrounding free-space.
 - local freeSpace = getSurroundingFreeSpace(map, fromX, fromY, spaceX, spaceY)
 - -- Select the space which will get us closest to the space.
 - if getTrueSize(freeSpace) < 1 then
 - error("Trapped at " .. fromX .. ", " .. fromY)
 - end
 - local closestSpace = freeSpace[1]
 - for _, space in ipairs(freeSpace) do
 - if space.d < closestSpace.d then
 - closestSpace = space
 - end
 - end
 - print("Destination: (" .. spaceX .. ", " .. spaceY .. ")\n (" .. fromX .. ", " .. fromY .. "; " .. distanceTo(fromX, fromY, spaceX, spaceY) .. ") -> (" .. closestSpace.x .. ", " .. closestSpace.y .. "; " .. closestSpace.d .. ")")
 - -- Shouldn't this prevent getting stuck?
 - if distanceTo(fromX, fromY, spaceX, spaceY) <= closestSpace.d then
 - return false
 - end
 - -- Shit's gonna get stuck going back and forth between two spaces.
 - return isSpaceAccessible(map, closestSpace.x, closestSpace.y, spaceX, spaceY)
 - end
 - map, spacesChecked = loadMap("/map")
 - local function checkSpaces(map, spacesChecked)
 - for y, set in pairs(spacesChecked) do
 - for x, space in pairs(set) do
 - space.checked = true
 - space.accessible = isSpaceAccessible(map, map.start.x, map.start.y, x, y)
 - end
 - end
 - end
 - printMap(map, spacesChecked)
 - checkSpaces(map, spacesChecked)
 - for y, set in pairs(spacesChecked) do
 - for x, space in pairs(set) do
 - print("[" .. x .. ", " .. y .. "] = Checked: " .. tostring(space.checked) .. "; Accessible: " .. tostring(space.accessible))
 - sleep(0.05)
 - end
 - end
 
Advertisement
 
                    Add Comment                
                
                        Please, Sign In to add comment