Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local position = {}
- table.insert(position, 1)
- table.insert(position, 1)
- table.insert(position, 1)
- table.insert(position, 1)
- --1 is up, 2 is right, 3 is down and 4 is left
- local function clear()
- term.clear()
- term.setCursorPos(1, 1)
- end
- local function detect()
- if turtle.detect() then
- local out = 1
- else
- local out = 0
- end
- return out
- end
- local function goForward()
- if turtle.forward() then
- rednet.send(idToAccept, "<success:forward>")
- if position[4] == 1 then
- local x, y, z, orientation = position[1], position[2], position[3], position[4]
- local y = y-1
- local block = detect()
- position = {
- x,
- y,
- z,
- orientation,
- block
- }
- elseif position[4] == 2 then
- local x, y, z, orientation = position[1], position[2], position[3], position[4]
- local x = x+1
- local block = detect()
- position = {
- x,
- y,
- z,
- orientation,
- block
- }
- elseif position[4] == 3 then
- local x, y, z, orientation = position[1], position[2], position[3], position[4]
- local y = y+1
- local block = detect()
- position = {
- x,
- y,
- z,
- orientation,
- block
- }
- elseif position[4] == 4 then
- local x, y, z, orientation = position[1], position[2], position[3], position[4]
- local x = x-1
- local block = detect()
- position = {
- x,
- y,
- z,
- orientation,
- block
- }
- end
- local positionS = textutils.serialize(position)
- rednet.send(idToAccept, positionS)
- return "true"
- else
- turtle.attack()
- goForward()
- end
- end
- local function goBack()
- if turtle.back() then
- rednet.send(idToAccept, "<success:back>")
- if position[4] == 1 then
- local x, y, z, orientation = position[1], position[2], position[3], position[4]
- local y = y+1
- local block = detect()
- position = {
- x,
- y,
- z,
- orientation,
- block
- }
- elseif position[4] == 2 then
- local x, y, z, orientation = position[1], position[2], position[3], position[4]
- local x = x-1
- local block = detect()
- position = {
- x,
- y,
- z,
- orientation,
- block
- }
- elseif position[4] == 3 then
- local x, y, z, orientation = position[1], position[2], position[3], position[4]
- local y = y-1
- local block = detect()
- position = {
- x,
- y,
- z,
- orientation,
- block
- }
- elseif position[4] == 4 then
- local x, y, z, orientation = position[1], position[2], position[3], position[4]
- local x = x+1
- local block = detect()
- position = {
- x,
- y,
- z,
- orientation,
- block
- }
- end
- local positionS = textutils.serialize(position)
- rednet.send(idToAccept, positionS)
- return "true"
- else
- goBack()
- end
- end
- local function goUp()
- if turtle.up() then
- rednet.send(idToAccept, "<success:up>")
- local x, y, z, orientation = position[1], position[2], position[3], position[4]
- local z = z+1
- local block = detect()
- position = {
- x,
- y,
- z,
- orientation,
- block
- }
- local positionS = textutils.serialize(position)
- rednet.send(idToAccept, positionS)
- return "true"
- else
- turtle.attackUp()
- goUp()
- end
- end
- local function goDown()
- if turtle.down() then
- rednet.send(idToAccept, "<success:down>")
- local x, y, z, orientation = position[1], position[2], position[3], position[4]
- local z = z-1
- local block = detect()
- position = {
- x,
- y,
- z,
- orientation,
- block
- }
- local positionS = textutils.serialize(position)
- rednet.send(idToAccept, positionS)
- return "true"
- else
- turtle.attackDown()
- goDown()
- end
- end
- local function selectInv()
- local idSender, msg = rednet.receive()
- if idSender == idToAccept then
- local slot = tonumber(msg)
- turtle.select(slot)
- rednet.send(idToAccept, "<success:select>")
- else
- rednet.send(tonumber(idSender), "<print$error:you are not the autorised sender$>")
- return
- end
- end
- local function turnRight()
- turtle.turnRight()
- local x, y, z, orientation = position[1], position[2], position[3], position[4]
- if orientation+1 < 5
- orientation = orientation+1
- else
- orientation = 1
- end
- local block = detect()
- position = {
- x,
- y,
- z,
- orientation,
- block
- }
- local positionS = textutils.serialize(position)
- rednet.send(idToAccept, positionS)
- end
- local function turnLeft()
- turtle.turnLeft()
- local x, y, z, orientation = position[1], position[2], position[3], position[4]
- if orientation-1 > 0
- orientation = orientation-1
- else
- orientation = 4
- end
- local block = detect()
- position = {
- x,
- y,
- z,
- orientation,
- block
- }
- local positionS = textutils.serialize(position)
- rednet.send(idToAccept, positionS)
- end
- print("Listening for a ping request...")
- local id, ping_message = rednet.receive()
- if ping_message == "#/ping#" then
- idToAccept = tonumber(id)
- rednet.send(idToAccept, "<ping>")
- clear()
- end
- while true do
- local idSender, msg = rednet.receive()
- if idToAccept == tonumber(idSender) then
- if msg == "#/dig#" then
- turtle.dig()
- elseif msg == "#/forward#" then
- print("Going forward ...")
- goForward()
- elseif msg == "#/back#" then
- print("Going back ...")
- goBack()
- elseif msg == "#/down#" then
- goDown()
- elseif msg == "#/up#" then
- goUp()
- elseif msg == "#/right#" then
- turnRight()
- elseif msg == "#/left#" then
- turnLeft()
- elseif msg == "#/select#" then
- selectInv()
- else
- print("Unknow command")
- end
- else
- print("Wrong sender")
- print(idToAccept)
- print(idSender)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment