KaoSDlanor

2D pathfinder

Feb 6th, 2013
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.22 KB | None | 0 0
  1. local tMap=setmetatable({
  2. "01000000000000000101",
  3. "01000000000000000101",
  4. "01111100000000000101",
  5. "00000111111111000101",
  6. "11110110000001000101",
  7. "00000110111101000101",
  8. "01111110001101000101",
  9. "01000011101101000101",
  10. "01000000101101000101",
  11. "01111111101101000101",
  12. "00000000001101000101",
  13. "11111111111101111101",
  14. "00000000000100001101",
  15. "00000000000101111101",
  16. "00000000000101111101",
  17. "00000000000100000001",
  18. "00000000000111111111"
  19. },{__call=function(self,x,y)
  20.     if self[y] and x<=#self[y] then
  21.         return string.sub(self[y],x,x)
  22.     end
  23. end})
  24.  
  25. local function fPath(map,targx,targy)
  26.  
  27.     --[VARS]
  28.     local path={x=1,y=1,""}
  29.     local target={x=targx or 1,y=targy or 1}
  30.     local displacement=setmetatable({},{__index=function(self,index) if path[index] and target[index] then return path[index]-target[index] end end,__newindex=function() return nil end})
  31.     local checked=setmetatable({{x=1,y=1}},{__call=function(self,x,y) for k,v in pairs(self) do if v.x==x and v.y==y then return true end end return false end})
  32.     local directions_mt={__index=function(self,index) if index=="x" or index=="y" then return 0 end end}
  33.     local directions=setmetatable({{x=1,"r"},{x=-1,"l"},{y=1,"d"},{y=-1,"u"}},{__call=function(self,direction)
  34.         if direction then
  35.             for k,v in pairs(self) do
  36.                 if v[1]==direction then
  37.                     return v
  38.                 end
  39.             end
  40.         else
  41.             local x
  42.             local y
  43.             for k,v in pairs(self) do
  44.                 if (displacement.x>0 and v.x<0) or (displacement.x<0 and v.x>0) then
  45.                     x=v
  46.                 elseif (displacement.y>0 and v.y<0) or (displacement.y<0 and v.y>0) then
  47.                     y=v
  48.                 end
  49.             end
  50.             return x,y
  51.         end
  52.     end})
  53.     for k,v in pairs(directions) do setmetatable(v,directions_mt) end
  54.  
  55.     --[FUNCTIONS]
  56.     local render=function()
  57.         term.clear()
  58.         for k,v in pairs(map) do
  59.             term.setCursorPos(1,k)
  60.             print(({string.gsub(string.gsub(v,"0","."),"1","#")})[1])
  61.         end
  62.         print(path.x..", "..path.y)
  63.         term.setCursorPos(path.x,path.y)
  64.         write("*")
  65.     end
  66.  
  67.     --[EXECUTION]
  68.     while true do
  69.         local x,y=directions()
  70.         if x and map(path.x+x.x,path.y)=="0" and not checked(path.x+x.x,path.y) then
  71.             path.x=path.x+x.x
  72.             path[1]=path[1]..x[1]
  73.             table.insert(checked,{x=path.x,y=path.y})
  74.         elseif y and map(path.x,path.y+y.y)=="0" and not checked(path.x,path.y+y.y) then
  75.             path.y=path.y+y.y
  76.             path[1]=path[1]..y[1]
  77.             table.insert(checked,{x=path.x,y=path.y})
  78.         else
  79.             local moved=false
  80.             for k,v in pairs(directions) do
  81.                 if v~=x and v~=y and map(path.x+v.x,path.y+v.y)=="0" and not checked(path.x+v.x,path.y+v.y) then
  82.                     moved=true
  83.                     path.x=path.x+v.x
  84.                     path.y=path.y+v.y
  85.                     path[1]=path[1]..v[1]
  86.                     table.insert(checked,{x=path.x,y=path.y})
  87.                     break
  88.                 end
  89.             end
  90.             if not moved then
  91.                 if path.x==1 and path.y==1 then
  92.                     term.clear()
  93.                     term.setCursorPos(1,1)
  94.                     print("no way to get to target...")
  95.                     break
  96.                 end
  97.                 path.x=path.x-directions(string.sub(path[1],-1)).x
  98.                 path.y=path.y-directions(string.sub(path[1],-1)).y
  99.                 path[1]=string.sub(path[1],1,-2)
  100.             end
  101.         end
  102.         term.setCursorPos(1,12)
  103.         if path.x==target.x and path.y==target.y then
  104.             term.clear()
  105.             term.setCursorPos(1,1)
  106.             print("target found, path:")
  107.             print(path[1])
  108.             break
  109.         end
  110.         sleep(0.1)
  111.         render()
  112.     end
  113. end
  114.  
  115. fPath(tMap,19,1)
Advertisement
Add Comment
Please, Sign In to add comment