Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function updateDir()
- local a = fs.open("direction", "w")
- a.writeLine(dir)
- a.close()
- end
- function sendUpdate()
- upX, upY, upZ = gps.locate()
- rednet.send(2, "updatePos")
- rednet.send(2, upX)
- rednet.send(2, upY)
- rednet.send(2, upZ)
- end
- function getUpdate()
- x, y, z = gps.locate()
- local a = fs.open("direction", "r")
- dir = a.readLine()
- print(dir)
- a.close()
- end
- function turnTo(newDir)
- while dir ~= newDir do
- turn("right")
- end
- end
- function turn(relDir)
- if relDir == "right" then
- turtle.turnRight()
- if dir == "south" then
- dir = "west"
- elseif dir == "east" then
- dir = "south"
- elseif dir == "north" then
- dir = "east"
- elseif dir == "west" then
- dir = "north"
- end
- elseif relDir == "left" then
- turtle.turnLeft()
- if dir == "south" then
- dir = "east"
- elseif dir == "east" then
- dir = "north"
- elseif dir == "north" then
- dir = "west"
- elseif dir == "west" then
- dir = "south"
- end
- end
- updateDir()
- end
- function goTo(newX, newY, newZ)
- print(x)
- print(newX)
- newX = tonumber(newX)
- newY = tonumber(newY)
- newZ = tonumber(newZ)
- if x>newX then
- turnTo("west")
- while x>newX do
- turtle.forward()
- x = x - 1
- end
- elseif x<newX then
- turnTo("east")
- while x<newX do
- turtle.forward()
- x = x + 1
- end
- end
- if z>newZ then
- turnTo("north")
- while z>newZ do
- turtle.forward()
- z = z - 1
- end
- elseif z<newZ then
- turnTo("south")
- while z<newZ do
- turtle.forward()
- z = z + 1
- end
- end
- if y>newY then
- while y>newY do
- turtle.down()
- y = y - 1
- end
- elseif y<newY then
- while y<newY do
- turtle.up()
- y = y + 1
- end
- end
- end
- rednet.open("left")
- getUpdate()
- local cmd_table =
- {
- ["digDown"] = function()
- turtle.digDown()
- end,
- ["digUp"] = function()
- turtle.digUp()
- end,
- ["dig"] = function()
- turtle.dig()
- end,
- }
- while true do
- id, cmd = rednet.receive()
- print(cmd)
- if cmd == "goTo" then
- id, xyz = rednet.receive()
- print(xyz)
- locX = tonumber(xyz[1])
- locY = tonumber(xyz[2])
- locZ = tonumber(xyz[3])
- print(locX, locY, locZ)
- goTo(locX, locY, locZ)
- sendUpdate()
- else
- cmd_table[cmd]()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement