Guest User

G

a guest
Jan 12th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. function pathfind(map, x2, y2, x1, y1)
  2.  
  3. map_width = #map[1]
  4. map_height = #map
  5.  
  6. local function in_bounds(x, y) --check location is in bounds of map
  7. return x > 0 and
  8. y > 0 and
  9. x <= map_width and
  10. y <= map_height
  11. end
  12.  
  13. local translations = { --valid move directions, note we do not have diagonal translations
  14. {0, 1},
  15. {1, 0},
  16. {0, -1},
  17. {-1, 0}
  18. }
  19.  
  20. path_map = {} --generate a new map of 0s that has the same dimensions
  21. for y, row in ipairs (map) do
  22. local new_row = {}
  23. for x, val in ipairs(row) do
  24. table.insert(new_row, 0)
  25. end
  26. table.insert(path_map, new_row)
  27. end
  28.  
  29. local fill_positions = {{x1,y1,9}} -- startx, starty, currentvalue .. this is a list of positions to check next
  30.  
  31. local sucess = false
  32.  
  33. while #fill_positions > 0 do
  34.  
  35. local current_pos = table.remove(fill_positions,1) --lua "pop" the first element in a table
  36.  
  37. local xPos = current_pos[1]
  38. local yPos = current_pos[2]
  39. local count = current_pos[3]
  40.  
  41. if in_bounds(xPos, yPos) then
  42. if map[yPos][xPos] == 0 and path_map[yPos][xPos] == 0 then --both squares on maps are free
  43. path_map [yPos][xPos] = count + 1 --mark with count + 1
  44.  
  45.  
  46. if xPos == x2 and yPos == y2 then
  47. sucess = true
  48. break --if we reach destination we can cancel flood fill
  49. end
  50.  
  51. for _,translation in ipairs(translations) do --add all the surrounding squares to a check list
  52. -- print(inspect(translation))
  53. local new_fill_pos = {xPos + translation[1], yPos + translation[2], count +1}
  54. table.insert(fill_positions, new_fill_pos)
  55. end
  56. end
  57. end
  58. end
  59.  
  60. local results = {}
  61.  
  62. if sucess then
  63.  
  64. local current_pos = {x2,y2}
  65.  
  66. table.insert (results, current_pos)
  67.  
  68. while true do
  69. local current_number = path_map[current_pos[2]][current_pos[1]]
  70. if current_number == 10 then --we must have reached the destination (value 10)
  71. break --so break loop
  72. end
  73.  
  74. for _, translation in ipairs(translations) do --look for first tile surrounding with a number one lower
  75. local check_pos = {current_pos[1]+translation[1], current_pos[2] + translation[2]}
  76. if in_bounds(check_pos[1], check_pos[2]) then
  77. if path_map[check_pos[2]][check_pos[1]] == current_number - 1 then
  78.  
  79. table.insert(results, check_pos) --save the result
  80. current_number = current_number - 1 --lower the current number
  81. current_pos = check_pos
  82. break --don't check any more translations as this one was okay (loop will continue)
  83. end
  84. end
  85. end
  86. end
  87. return results
  88. end
  89. end
  90.  
  91.  
  92. local test_map = {
  93. {0, 0, 1, 0},
  94. {0, 0, 0, 0},
  95. {0, 1, 1, 0},
  96. {0, 0, 1, 0},
  97. }
  98.  
  99. print('PATHFIND TESTS')
  100. print('pathfind result:',inspect(pathfind(test_map, 1,1, 4,4)))
  101. print('pathfind result:',inspect(pathfind(test_map, 1,1, 3,4))) --blocked example
Advertisement
Add Comment
Please, Sign In to add comment