Guest User

CC Task Thing

a guest
Sep 7th, 2015
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.33 KB | None | 0 0
  1. -- Read map
  2. -- Save tiles in a Cartesian plane, such that the top left corner is (0, 0).
  3. -- Locate starting position via the 'K'
  4. -- Walls are #
  5. -- Whitespace is open.
  6. -- GOAL:
  7. --      Figure out if all spaces are accessible.
  8. --      Pick a target, then iterate through all possible paths until
  9. --      we find one that works, then set that tile as checked.
  10.  
  11. -- Have a base map table
  12. -- Have a checked map table
  13.  
  14. local map           = {}
  15. local spacesChecked = {} -- Is 2D table in which spacesChecked[y][x] is a whitespace tile that has or has not been checked.
  16.  
  17. local function loadMap(path)
  18.     local fileHandle = fs.open(path, 'r')
  19.    
  20.     if not fileHandle then
  21.         error("Could not open map at " .. path)
  22.     end
  23.    
  24.     local height, width = fileHandle.readLine():match("(%d+)%s(%d+)")
  25.     print("Map Dimensions: [w = " .. width .. ", h = " .. height .. "]")
  26.    
  27.     local spacesChecked = {}
  28.     local map           = {}
  29.     local line          = ""
  30.    
  31.     for y = 1, height do
  32.         map[y] = {}
  33.         line = fileHandle.readLine()
  34.        
  35.         for x = 1, width do
  36.             local tile = line:sub(x, x)
  37.            
  38.             map[y][x] = tile
  39.             if tile == " " then
  40.                 if not spacesChecked[y] then
  41.                     spacesChecked[y] = {}
  42.                 end
  43.                
  44.                 spacesChecked[y][x] = { checked = false, accessible = false }
  45.             elseif tile == "K" then
  46.                 map.start = { x = x, y = y }
  47.             end
  48.         end
  49.     end
  50.    
  51.     return map, spacesChecked
  52. end
  53.  
  54. local function printMap(map, spacesChecked)
  55.     for y, line in ipairs(map) do
  56.         for x, char in ipairs(line) do
  57.             if spacesChecked[y] and spacesChecked[y][x] then
  58.                 write(".")
  59.             else
  60.                 write(char)
  61.             end
  62.         end
  63.         print("")
  64.     end
  65. end
  66.  
  67. local function distanceTo(fromX, fromY, x, y)
  68.     return math.sqrt((x - fromX)^2 + (y - fromY)^2)
  69. end
  70.  
  71. local function getSurroundingFreeSpace(map, x, y, destX, destY)
  72.     local freeSpace = {}
  73.    
  74.     for _y = y - 1, y + 1 do
  75.         for _x = x - 1, x + 1 do
  76.             if not (_x == x and _y == y) and map[_y][_x] == " " then
  77.                 table.insert(freeSpace, { x = _x, y = _y, d = distanceTo(_x, _y, destX, destY) })
  78.             end
  79.         end
  80.     end
  81.    
  82.     return freeSpace
  83. end
  84.  
  85. local function getTrueSize(_table)
  86.     local size = 0
  87.    
  88.     for _, __ in pairs(_table) do
  89.         size = size + 1
  90.     end
  91.    
  92.     return size
  93. end
  94.  
  95. local function isSpaceAccessible(map, fromX, fromY, spaceX, spaceY)
  96.     if fromX == spaceX and fromY == spaceY then
  97.         return true
  98.     end
  99.  
  100.     -- Gather surrounding free-space.
  101.     local freeSpace = getSurroundingFreeSpace(map, fromX, fromY, spaceX, spaceY)
  102.    
  103.     -- Select the space which will get us closest to the space.
  104.     if getTrueSize(freeSpace) < 1 then
  105.         error("Trapped at " .. fromX .. ", " .. fromY)
  106.     end
  107.    
  108.     local closestSpace = freeSpace[1]
  109.     for _, space in ipairs(freeSpace) do
  110.         if space.d < closestSpace.d then
  111.             closestSpace = space
  112.         end
  113.     end
  114.    
  115.     print("Destination: (" .. spaceX .. ", " .. spaceY .. ")\n     (" .. fromX .. ", " .. fromY .. "; " .. distanceTo(fromX, fromY, spaceX, spaceY) .. ") -> (" .. closestSpace.x .. ", " .. closestSpace.y .. "; " .. closestSpace.d .. ")")
  116.    
  117.     -- Shouldn't this prevent getting stuck?
  118.     if distanceTo(fromX, fromY, spaceX, spaceY) <= closestSpace.d then
  119.         return false
  120.     end
  121.    
  122.     -- Shit's gonna get stuck going back and forth between two spaces.
  123.     return isSpaceAccessible(map, closestSpace.x, closestSpace.y, spaceX, spaceY)
  124. end
  125.  
  126. map, spacesChecked = loadMap("/map")
  127.  
  128. local function checkSpaces(map, spacesChecked)
  129.     for y, set in pairs(spacesChecked) do
  130.         for x, space in pairs(set) do
  131.             space.checked = true
  132.             space.accessible = isSpaceAccessible(map, map.start.x, map.start.y, x, y)
  133.         end
  134.     end
  135. end
  136.  
  137. printMap(map, spacesChecked)
  138. checkSpaces(map, spacesChecked)
  139. for y, set in pairs(spacesChecked) do
  140.     for x, space in pairs(set) do
  141.         print("[" .. x .. ", " .. y .. "] = Checked: " .. tostring(space.checked) .. "; Accessible: " .. tostring(space.accessible))
  142.         sleep(0.05)
  143.     end
  144. end
Advertisement
Add Comment
Please, Sign In to add comment