Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --rednet.open("right")
- local tArgs = {...}
- local sendChannel = 12456
- if #tArgs>0 then
- if tonumber(tArgs[1]) then
- sendChannel = tonumber(tArgs[1])
- end
- end
- local blacklist = {
- "minecraft:cobblestone"
- }
- local blacklistLookup = {}
- for k,v in pairs(blacklist) do
- blacklistLookup[v] = true
- end
- shell.run("refuel all")
- local facings = {}
- facings.north = 0
- facings.east = 1
- facings.south = 2
- facings.west = 3
- for k,v in pairs(facings) do
- facings[v] = k
- end
- local x,y,z = gps.locate()
- local assigned = nil
- local facing = 0
- local modem = peripheral.find("modem")
- modem.open(os.getComputerID())
- local up
- local down
- local dig
- local tryup
- local trydown
- local digDown
- local digUp
- local function receive(timeout)
- local id = os.startTimer(timeout and timeout or 10)
- while true do
- local event = {os.pullEvent()}
- if event[1] == "modem_message" and event[6] < 200 then
- return event
- elseif event[1] == "timer" then
- return nil
- end
- end
- end
- local function send(message)
- modem.transmit(sendChannel,os.getComputerID(),message)
- end
- local function left()
- turtle.turnLeft()
- facing = facing+3
- facing=facing%4
- end
- local function right()
- turtle.turnRight()
- facing = facing+1
- facing = facing%4
- end
- up = function()
- while (os.clock()*100)%10~=0 do
- os.queueEvent("hi")
- coroutine.yield()
- end
- while not turtle.up() do
- digUp()
- while (os.clock()*100)%10~=0 do
- os.queueEvent("hi")
- coroutine.yield()
- end
- end
- y=y+1
- end
- tryup = function()
- while (os.clock()*100)%10~=0 do
- os.queueEvent("hi")
- coroutine.yield()
- end
- for i =1 ,3 do
- if turtle.up() then
- y=y+1
- break
- end
- digUp()
- while (os.clock()*100)%10~=0 do
- os.queueEvent("hi")
- coroutine.yield()
- end
- end
- end
- down = function()
- while (os.clock()*100)%10~=0 do
- os.queueEvent("hi")
- coroutine.yield()
- end
- while not turtle.down() do
- digDown()
- while (os.clock()*100)%10~=0 do
- os.queueEvent("hi")
- coroutine.yield()
- end
- end
- y=y-1
- end
- trydown = function()
- while (os.clock()*100)%10~=0 do
- os.queueEvent("hi")
- coroutine.yield()
- end
- for i = 1 , 3 do
- if turtle.down() then
- y=y-1
- break
- end
- digDown()
- while (os.clock()*100)%10~=0 do
- os.queueEvent("hi")
- coroutine.yield()
- end
- end
- end
- dig = function()
- while (os.clock()*100)%10 == 0 do
- os.queueEvent("hi")
- coroutine.yield()
- end
- local ok,data = turtle.inspect()
- if ok then
- if data.name:lower():match("computercraft") then
- if data.state and data.state.facing then
- if facings[(facing+2)%4] == data.state.facing then
- if math.random(1,2) == 2 then
- right()
- else
- left()
- end
- end
- end
- return false
- else
- return turtle.dig()
- end
- end
- end
- digDown = function()
- while (os.clock()*100)%10 == 0 do
- os.queueEvent("hi")
- coroutine.yield()
- end
- local ok,data = turtle.inspectDown()
- if ok then
- if data.name:lower():match("computercraft") then
- return false
- else
- return turtle.digDown()
- end
- end
- end
- digUp = function()
- while (os.clock()*100)%10 == 0 do
- os.queueEvent("hi")
- coroutine.yield()
- end
- local ok,data = turtle.inspectUp()
- if ok then
- if data.name:lower():match("computercraft") then
- return false
- else
- return turtle.digUp()
- end
- end
- end
- while not turtle.forward() do
- if math.random(1,2) == 2 then
- up()
- else
- down()
- end
- dig()
- x,y,z = gps.locate()
- end
- do
- local x2,y2,z2 = gps.locate()
- if z2<z then
- facing = 0
- elseif z2>z then
- facing = 2
- elseif x<x2 then
- facing = 1
- else
- facing = 3
- end
- end
- x,y,z = gps.locate()
- print(x,y,z,facing)
- local function forward()
- while (os.clock()*100)%10~=0 do
- os.queueEvent("hi")
- coroutine.yield()
- end
- while not turtle.forward() do
- dig()
- while (os.clock()*100)%10~=0 do
- os.queueEvent("hi")
- coroutine.yield()
- end
- end
- if facing == 0 then
- z=z-1
- elseif facing == 2 then
- z=z+1
- elseif facing == 1 then
- x=x+1
- else
- x=x-1
- end
- end
- local function tryforward()
- while (os.clock()*100)%10~=0 do
- os.queueEvent("hi")
- coroutine.yield()
- end
- for i = 1, 3 do
- if turtle.forward() then
- if facing == 0 then
- z=z-1
- elseif facing == 2 then
- z=z+1
- elseif facing == 1 then
- x=x+1
- else
- x=x-1
- end
- break
- end
- dig()
- while (os.clock()*100)%10~=0 do
- os.queueEvent("hi")
- coroutine.yield()
- end
- end
- end
- local function goto(coords)
- local x3,y3,z3 = coords:match("(%-?%d+),(%-?%d+),(%-?%d+)")
- x3 = tonumber(x3)
- y3 = tonumber(y3)
- z3 = tonumber(z3)
- local px,py,pz
- while y~=y3 or x3~=x or z3~=z do
- px = x
- py = y
- pz = z
- for i = 1, 20 do
- if y~=y3 then
- if y3>y then
- tryup()
- else
- trydown()
- end
- end
- end
- if x3~=x then
- for i = 1, 10 do
- if x3>x then
- while facing~=1 do
- left()
- end
- tryforward()
- elseif x3<x then
- while facing~=3 do
- right()
- end
- tryforward()
- end
- end
- end
- if z3~=z then
- for i = 1, 10 do
- if z3>z then
- while facing~= 2 do
- right()
- end
- tryforward()
- elseif z3<z then
- while facing~=0 do
- right()
- end
- tryforward()
- end
- end
- end
- if x == px and y == py and z==pz then
- print("gaveup"..coords)
- send("gaveup"..coords)
- assigned = nil
- break
- end
- end
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- local item = turtle.getItemDetail(i)
- if item and blacklistLookup[item.name] then
- turtle.select(i)
- turtle.drop()
- end
- end
- end
- turtle.select(1)
- end
- while true do
- if not assigned then
- send("request"..x..","..y..","..z)
- local message = receive(1)
- local counter = 0
- while not message or type(message[5]) ~="string" or message[5]:match("request") or message[5]:match("gaveup") do
- message = receive(.05)
- counter = counter+1
- if counter == 30 then
- send("request"..x..","..y..","..z)
- counter = 0
- end
- end
- if message then
- print(message[5])
- end
- if type(message[5]) == "string" then
- if not message[5]:match("request") and message[5]:match("a%-?%d+,%-?%d+,%-?%d+") then
- assigned = message[5]
- end
- end
- print(assigned)
- else
- print("test1")
- goto(assigned)
- print("test2")
- assigned = nil
- end
- sleep(1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement