Advertisement
natie3

3dPathfindingAlgoritm

Nov 3rd, 2014
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.89 KB | None | 0 0
  1. monitor = nil
  2. drieD = true
  3.  
  4. function displayArea()
  5.   maze.setBackgroundColor(colors.white)
  6.   maze.setTextColor(colors.black)
  7.   maze.clear()
  8.   maze.setCursorPos(1,1)
  9.   y = 1
  10.   for x = 319, 296, -1 do
  11.     for z = -258, -230 do
  12.       if not checkObstacle(x,65,z) then
  13.         maze.setBackgroundColor(colors.red)
  14.         maze.write("[]")
  15.       else
  16.         maze.setBackgroundColor(colors.white)
  17.         maze.write("  ")
  18.       end
  19.     end
  20.     y = y + 1
  21.     maze.setCursorPos(1,y)
  22.   end
  23. end
  24.  
  25. function addBlock(x,y,z,g,h,parent,path,facing)
  26.   object = { x = x, y = y, z = z, g = g, h = h, parent = parent,path = path, facing = facing, list = "open"}
  27.   table.insert(list,object)
  28. end
  29.  
  30. function save(x,y,z)
  31.   x = tonumber(x)
  32.   y = tonumber(y)
  33.   z = tonumber(z)
  34.   if obstacleList[x] == nil then
  35.     obstacleList[x] = {}
  36.   end
  37.   if obstacleList[x][y] == nil then
  38.     obstacleList[x][y] = {}
  39.   end
  40.   obstacleList[x][y][z] = "iets"
  41.   f = fs.open("obstacleList", "w")
  42.   f.write(textutils.serialize(obstacleList))
  43.   f.close()
  44.   print("added: "..x..","..y..","..z)
  45. end  
  46.  
  47. function calculateH(x,y,z,eindx,eindy,eindz)
  48.   return (10*(math.abs(eindx-x) + math.abs(eindy-y) + math.abs(eindz-z)))
  49. end
  50.  
  51. function getCoords(x,y,z,dir)
  52.   if dir == 0 then
  53.     return x, y, (z+1)
  54.   elseif dir == 1 then
  55.     return (x-1), y, z
  56.   elseif dir == 2 then
  57.     return x, y, (z-1)
  58.   elseif dir == 3 then
  59.     return (x+1), y, z
  60.   elseif dir == "u" then
  61.     return x, (y+1), z
  62.   else
  63.     return x, (y-1), z
  64.   end
  65. end
  66.  
  67. function getOpposide(dir)
  68.   if dir == "u" then
  69.     return "d"
  70.   elseif dir == "d" then
  71.     return "u"
  72.   else
  73.     return ((dir+2)%4)
  74.   end
  75. end
  76.  
  77. function checkObstacle(x,y,z)
  78.   free = true
  79.   if obstacleList[x] ~= nil then
  80.     if obstacleList[x][y] ~= nil then
  81.       if obstacleList[x][y][z] ~= nil then
  82.         free = false
  83.       end
  84.     end
  85.   end
  86.   return free
  87. end
  88.  
  89. function checkFree(x,y,z)
  90.   free = checkObstacle(x,y,z)
  91.   for k, v in pairs(list) do
  92.     if v.x == x and v.y == y and v.z == z then
  93.       free = false
  94.     end
  95.   end
  96.   return free
  97. end
  98.  
  99. function findRoute(beginx,beginy,beginz,eindx,eindy,eindz,facing)
  100.   beginx = tonumber(beginx)
  101.   beginy = tonumber(beginy)
  102.   beginz = tonumber(beginz)
  103.   eindx = tonumber(eindx)
  104.   eindy = tonumber(eindy)
  105.   eindz = tonumber(eindz)
  106.   facing = tonumber(facing)
  107.   list = {}
  108.   h = calculateH(beginx,beginy,beginz,eindx,eindy,eindz)
  109.   --print("beginh: "..h)
  110.   addBlock(beginx,beginy,beginz,0,h,nil,nil,facing)
  111.   total = 0
  112.   temp = 0
  113.   found = false
  114.   finalIndex = -1
  115.   while not found do
  116.     while not found and temp < 100 do
  117.       selectedIndex = -1
  118.       selectedF = 1000000
  119.       for k, v in pairs(list) do
  120.         if v.list == "open" then
  121.           f = tonumber(v.g) + tonumber(v.h)
  122.           if f < selectedF then
  123.             selectedIndex = k
  124.             selectedF = f
  125.             --print("f: "..f)
  126.           end
  127.         end
  128.       end
  129.       --print("lengte: "..#list)
  130.       --print("x: "..list[selectedIndex].x)
  131.       --print("y: "..list[selectedIndex].y)
  132.       --print("z: "..list[selectedIndex].z)
  133.       list[selectedIndex].list = "closed"
  134.       if list[selectedIndex].x == eindx and list[selectedIndex].y == eindy and list[selectedIndex].z == eindz then
  135.         found = true
  136.         finalIndex = selectedIndex
  137.       else
  138.         for i = 0, 3 do
  139.           dir = (tonumber(list[selectedIndex].facing) + i)%4
  140.           x, y, z = getCoords(list[selectedIndex].x,list[selectedIndex].y,list[selectedIndex].z,dir)
  141.           --print(x..","..y..","..z..","..dir)
  142.           if checkFree(x,y,z) then
  143.             g = list[selectedIndex].g + 10
  144.             if i == 1 or i == 3 then
  145.               g = g + 4
  146.             end
  147.             h = calculateH(x,y,z,eindx,eindy,eindz)        
  148.             addBlock(x,y,z,g,h,selectedIndex,dir,dir)
  149.           end
  150.         end
  151.         if drieD then
  152.           if checkFree(list[selectedIndex].x,(list[selectedIndex].y+1),list[selectedIndex].z) then
  153.             g = list[selectedIndex].g + 9
  154.             h = calculateH(list[selectedIndex].x,(list[selectedIndex].y+1),list[selectedIndex].z,eindx,eindy,eindz)
  155.             addBlock(list[selectedIndex].x,(list[selectedIndex].y+1),list[selectedIndex].z,g,h,selectedIndex,"u",list[selectedIndex].facing)
  156.           end
  157.           if checkFree(list[selectedIndex].x,(list[selectedIndex].y-1),list[selectedIndex].z) then
  158.             g = list[selectedIndex].g + 9
  159.             h = calculateH(list[selectedIndex].x,(list[selectedIndex].y-1),list[selectedIndex].z,eindx,eindy,eindz)
  160.             addBlock(list[selectedIndex].x,(list[selectedIndex].y-1),list[selectedIndex].z,g,h,selectedIndex,"d",list[selectedIndex].facing)
  161.           end  
  162.         end
  163.         --io.read()
  164.       end
  165.       temp = temp + 1
  166.     end
  167.     total = total + temp
  168.     temp = 0
  169.     term.clear()
  170.     term.setCursorPos(1,1)
  171.     print("total loops: "..total)
  172.     print("size list: "..#list)
  173.     os.sleep(0.1)
  174.   end
  175.   print("total loops: "..total)
  176.   print("size list: "..#list)
  177.   temp = {}
  178.   ready = false
  179.   selectedIndex = finalIndex
  180.   while not ready do
  181.     table.insert(temp,list[selectedIndex].path)
  182.     selectedIndex = list[selectedIndex].parent
  183.     --print("lengte: "..#list)
  184.     --print("index: "..selectedIndex)
  185.     if list[selectedIndex].x == beginx and list[selectedIndex].y == beginy and list[selectedIndex].z == beginz then
  186.       ready = true
  187.     end
  188.   end
  189.   route = {}
  190.   for i = #temp, 1, -1 do
  191.     table.insert(route,temp[i])
  192.   end
  193.   return route    
  194. end
  195.  
  196. -- initialize main
  197. modem = peripheral.wrap("back")
  198. modem.open(7000)
  199. obstacleList = {}
  200. if fs.exists("obstacleList") then
  201.   f = fs.open("obstacleList", "r")
  202.   obstacleList = textutils.unserialize(f.readAll())
  203.   f.close()
  204. end
  205. if monitor ~= nil then
  206.   m = peripheral.wrap(monitor)
  207.   m.setBackgroundColor(colors.black)
  208.   m.clear()
  209.   maze = window.create(m,1,1,61,26)
  210. end
  211.  
  212. while true do
  213.   event = { os.pullEvent("modem_message") }
  214.   data = textutils.unserialize(event[5])
  215.   if data.type == "routeRequest" then
  216.     request = textutils.unserialize(data.request)
  217.     route = findRoute(request.beginx,request.beginy,request.beginz,request.eindx,request.eindy,request.eindz,request.facing)
  218.     print("route found")
  219.     tabel = { type = "routeAnswer", id = data.id, route = route}
  220.     modem.transmit(7001,7000,textutils.serialize(tabel))
  221.   elseif data.type == "addObstacle" then
  222.     info = data.info
  223.     save(info.x,info.y,info.z)
  224.     if monitor ~= nil then
  225.       displayArea()
  226.     end
  227.   elseif data.type == "addObstacleList" then
  228.     info = data.info
  229.     x1 = tonumber(info.x)
  230.     y1 = tonumber(info.y)
  231.     z1 = tonumber(info.z)
  232.     x2 = tonumber(info.x2)
  233.     y2 = tonumber(info.y2)
  234.     z2 = tonumber(info.z2)
  235.     for x = x1, x2 do
  236.       for y = y1, y2 do
  237.         for z = z1, z2 do
  238.           save(x,y,z)
  239.         end
  240.       end
  241.     end
  242.     print("obstacle list added")
  243.     os.sleep(2)
  244.   end
  245. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement