Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- drieD = false
- function getRoute(x1,y1,z1,x2,y2,z2,dir,world,ox,oy,oz)
- post = "x1="..x1.."&y1="..y1.."&z1="..z1.."&x2="..x2.."&y2="..y2.."&z2="..z2.."&dir="..dir.."&world="..world
- if(ox ~= nil and oy ~= nil and oz ~= nil) then
- post = post.."&ox="..ox.."&oy="..oy.."&oz="..oz
- end
- if not drieD then
- post = post.."&drieD=false"
- end
- succeed = false
- route = ""
- http.request("http://80.61.60.110/Pathfinding/getRoute.do",post)
- while not succeed do
- event = { os.pullEvent() }
- if event[1] == "http_failure" then
- --http.request("http://80.61.60.110/Pathfinding/getRoute.do",post)
- return "e_The http request failed"
- elseif event[1] == "http_success" then
- response = event[3]
- route = response.readAll()
- response.close()
- succeed = true
- end
- end
- return route
- end
- function save()
- f = fs.open("pos","w")
- f.writeLine(x)
- f.writeLine(y)
- f.writeLine(z)
- f.writeLine(facing)
- f.close()
- end
- function move(dir)
- obstacle = false
- if dir == "u" then
- while not turtle.up() and not obstacle do
- if turtle.detectUp() then
- obstacle = true
- else
- os.sleep(0.5)
- end
- end
- if not obstacle then
- y = y + 1
- save()
- end
- return not obstacle
- elseif dir == "d" then
- while not turtle.down() and not obstacle do
- if turtle.detectDown() then
- obstacle = true
- else
- os.sleep(0.5)
- end
- end
- if not obstacle then
- y = y - 1
- save()
- end
- return not obstacle
- else
- dir = tonumber(dir)
- if facing - dir == -3 or facing - dir == 1 then
- turtle.turnLeft()
- elseif facing - dir == -2 or facing - dir == 2 then
- back = true
- if not turtle.back() then
- turtle.turnLeft()
- turtle.turnLeft()
- back = false
- end
- elseif facing - dir == -1 or facing - dir == 3 then
- turtle.turnRight()
- end
- if not back then
- facing = dir
- save()
- while not turtle.forward() and not obstacle do
- if turtle.detect() then
- obstacle = true
- else
- os.sleep(0.5)
- end
- end
- else
- back = false
- end
- if not obstacle then
- x, y, z = getCoords(x,y,z,tostring(dir))
- save()
- end
- return not obstacle
- end
- end
- function getCoords(x,y,z,dir)
- if dir == "0" then
- return x, y, (z+1)
- elseif dir == "1" then
- return (x-1), y, z
- elseif dir == "2" then
- return x, y, (z-1)
- elseif dir == "3" then
- return (x+1), y, z
- elseif dir == "u" then
- return x, (y+1), z
- else
- return x, (y-1), z
- end
- end
- -- initialize main
- modem = peripheral.wrap("right")
- f = fs.open("pos","r")
- x = tonumber(f.readLine())
- y = tonumber(f.readLine())
- z = tonumber(f.readLine())
- facing = tonumber(f.readLine())
- back = false
- pos = false
- eindx = 331
- eindy = 63
- eindz = 107
- id = 1234
- modem.open(7001)
- os.loadAPI("RouteApi")
- while true do
- event = { os.pullEvent("modem_message") }
- data = textutils.unserialize(event[5])
- if data.type == "moveAction" then
- print("Move command received")
- position = data.position
- eindx = position.x
- eindy = position.y
- eindz = position.z
- pos = false
- obstacle = false
- ox = 0
- oy = 0
- oz = 0
- while not pos do
- if obstacle then
- route = getRoute(x,y,z,eindx,eindy,eindz,facing,"temp",ox,oy,oz)
- obstacle = false
- else
- route = getRoute(x,y,z,eindx,eindy,eindz,facing,"temp")
- end
- print("Route received")
- if route:sub(1,1) == "e" then
- print(route:sub(3,#route))
- pos = true
- else
- i = 0
- for i = 1, #route do
- write(route:sub(i,i))
- end
- while i < #route and not obstacle do
- i = i + 1
- v = route:sub(i,i)
- if not move(v) then
- ox, oy, oz= getCoords(x,y,z,tostring(v))
- obstacle = true
- print("obstacle")
- end
- end
- if not obstacle then
- pos = true
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement