Advertisement
natie3

Move2.0

Nov 28th, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. drieD = false
  2.  
  3. function getRoute(x1,y1,z1,x2,y2,z2,dir,world,ox,oy,oz)
  4.   post = "x1="..x1.."&y1="..y1.."&z1="..z1.."&x2="..x2.."&y2="..y2.."&z2="..z2.."&dir="..dir.."&world="..world
  5.   if(ox ~= nil and oy ~= nil and oz ~= nil) then
  6.     post = post.."&ox="..ox.."&oy="..oy.."&oz="..oz
  7.   end
  8.   if not drieD then
  9.     post = post.."&drieD=false"
  10.   end
  11.   succeed = false
  12.   route = ""
  13.   http.request("http://80.61.60.110/Pathfinding/getRoute.do",post)
  14.   while not succeed do  
  15.     event = { os.pullEvent() }
  16.     if event[1] == "http_failure" then
  17.       --http.request("http://80.61.60.110/Pathfinding/getRoute.do",post)
  18.       return "e_The http request failed"
  19.     elseif event[1] == "http_success" then
  20.       response = event[3]
  21.       route = response.readAll()
  22.       response.close()
  23.       succeed = true
  24.     end
  25.   end
  26.   return route
  27. end
  28.  
  29. function save()
  30.   f = fs.open("pos","w")
  31.   f.writeLine(x)
  32.   f.writeLine(y)
  33.   f.writeLine(z)
  34.   f.writeLine(facing)
  35.   f.close()
  36. end
  37.  
  38. function move(dir)
  39.   obstacle = false
  40.   if dir == "u" then
  41.     while not turtle.up() and not obstacle do
  42.       if turtle.detectUp() then
  43.         obstacle = true
  44.       else
  45.         os.sleep(0.5)
  46.       end
  47.     end
  48.     if not obstacle then
  49.       y = y + 1
  50.       save()
  51.     end
  52.     return not obstacle
  53.   elseif dir == "d" then
  54.     while not turtle.down() and not obstacle do
  55.       if turtle.detectDown() then
  56.         obstacle = true
  57.       else
  58.         os.sleep(0.5)
  59.       end
  60.     end
  61.     if not obstacle then
  62.       y = y - 1
  63.       save()
  64.     end
  65.     return not obstacle  
  66.   else
  67.     dir = tonumber(dir)
  68.     if facing - dir == -3 or facing - dir == 1 then
  69.       turtle.turnLeft()
  70.     elseif facing - dir == -2 or facing - dir == 2 then
  71.       back = true
  72.       if not turtle.back() then
  73.         turtle.turnLeft()
  74.         turtle.turnLeft()
  75.         back = false
  76.       end
  77.     elseif facing - dir == -1 or facing - dir == 3 then
  78.       turtle.turnRight()
  79.     end
  80.     if not back then
  81.       facing = dir
  82.       save()
  83.       while not turtle.forward() and not obstacle do
  84.         if turtle.detect() then
  85.           obstacle = true
  86.         else
  87.           os.sleep(0.5)
  88.         end
  89.       end
  90.     else
  91.       back = false
  92.     end
  93.     if not obstacle then
  94.       x, y, z = getCoords(x,y,z,tostring(dir))
  95.       save()
  96.     end
  97.     return not obstacle
  98.   end
  99. end  
  100.  
  101. function getCoords(x,y,z,dir)
  102.   if dir == "0" then
  103.     return x, y, (z+1)
  104.   elseif dir == "1" then
  105.     return (x-1), y, z
  106.   elseif dir == "2" then
  107.     return x, y, (z-1)
  108.   elseif dir == "3" then
  109.     return (x+1), y, z
  110.   elseif dir == "u" then
  111.     return x, (y+1), z
  112.   else
  113.     return x, (y-1), z
  114.   end
  115. end
  116.  
  117. -- initialize main
  118. modem = peripheral.wrap("right")
  119. f = fs.open("pos","r")
  120. x = tonumber(f.readLine())
  121. y = tonumber(f.readLine())
  122. z = tonumber(f.readLine())
  123. facing = tonumber(f.readLine())
  124. back = false
  125. pos = false
  126. eindx = 331
  127. eindy = 63
  128. eindz = 107
  129. id = 1234
  130. modem.open(7001)
  131. os.loadAPI("RouteApi")
  132.  
  133. while true do
  134.   event = { os.pullEvent("modem_message") }
  135.   data = textutils.unserialize(event[5])
  136.   if data.type == "moveAction" then
  137.     print("Move command received")
  138.     position = data.position
  139.     eindx = position.x
  140.     eindy = position.y
  141.     eindz = position.z
  142.     pos = false
  143.     obstacle = false
  144.     ox = 0
  145.     oy = 0
  146.     oz = 0
  147.     while not pos do
  148.       if obstacle then
  149.         route = getRoute(x,y,z,eindx,eindy,eindz,facing,"temp",ox,oy,oz)
  150.         obstacle = false
  151.       else
  152.         route = getRoute(x,y,z,eindx,eindy,eindz,facing,"temp")
  153.       end
  154.       print("Route received")
  155.       if route:sub(1,1) == "e" then
  156.         print(route:sub(3,#route))
  157.         pos = true
  158.       else
  159.         i = 0
  160.         for i = 1, #route do
  161.           write(route:sub(i,i))
  162.         end
  163.         while i < #route and not obstacle do
  164.           i = i + 1
  165.           v = route:sub(i,i)
  166.           if not move(v) then
  167.             ox, oy, oz= getCoords(x,y,z,tostring(v))
  168.             obstacle = true
  169.             print("obstacle")
  170.           end
  171.         end
  172.         if not obstacle then
  173.           pos = true
  174.         end
  175.       end
  176.     end
  177.   end
  178. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement