Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
- while true do
- event = { os.pullEvent("modem_message") }
- data = textutils.unserialize(event[5])
- if data.type == "moveAction" then
- position = data.position
- eindx = position.x
- eindy = position.y
- eindz = position.z
- pos = false
- while not pos do
- request = textutils.serialize({ beginx = x, eindx = eindx, beginy = y, eindy = eindy, beginz = z, eindz = eindz, facing = facing })
- tabel = { type = "routeRequest", id = id, request = request }
- table.insert(tabel,request)
- modem.transmit(7000,7001,textutils.serialize(tabel))
- event = { os.pullEvent("modem_message") }
- print("received")
- data = textutils.unserialize(event[5])
- route = data.route
- print(#route)
- obstacle = false
- i = 0
- for k, v in pairs(route) do
- write(v)
- end
- --io.read()
- while i < #route and not obstacle do
- i = i + 1
- v = route[i]
- if not move(v) then
- tempx, tempy, tempz = getCoords(x,y,z,tostring(v))
- info = { x = tempx, y = tempy, z = tempz }
- tabel = { type = "addObstacle", id = id, info = info }
- modem.transmit(7000,7001,textutils.serialize(tabel))
- obstacle = true
- print("obstacle")
- end
- end
- if not obstacle then
- pos = true
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement