Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modem
- for k,v in pairs(peripheral.getNames()) do
- if peripheral.getType(v)=="modem" and peripheral.call(v,"isWireless") then
- modem=peripheral.wrap(v)
- end
- end
- assert(modem,"No wireless modem found")
- local up=function()
- local tries=0
- while (not e or tries>10) do
- local e=turtle.up()
- if not e then turtle.digUp() turtle.attackUp() tries=tries+1 end
- end
- if tries==10 then error("Something in way... Cannot continue.",3) end
- return true
- end
- local fd=function()
- local tries=0
- while (not e or tries>10) do
- local e=turtle.forward()
- if not e then turtle.dig() turtle.attack() tries=tries+1 end
- end
- if tries==10 then error("Something in way... Cannot continue.",3) end
- return true
- end
- local down=function()
- local tries=0
- while (not e or tries>10) do
- local e=turtle.down()
- if not e then turtle.digDown() turtle.attackDown() tries=tries+1 end
- end
- if tries==10 then error("Something in way... Cannot continue.",3) end
- return true
- end
- function goTo(channel, msg)
- if not turtle then error("Can't go with a computer. Shtewpid",2) end
- local isGPS
- if channel==true and msg==nil then
- channel=65534
- isGPS=true
- end
- modem.closeAll()
- modem.open(channel)
- local distanceT={}
- local function checkIfBeacon(ch,msg) --Checks if there is a beacon with that channel and message
- local e,s,c,r,m,d
- if not modem.isOpen(ch) then modem.open(ch) end
- modem.transmit(ch,ch,"PING")
- local id=os.startTimer(2)
- repeat
- e,s,c,r,m,d=os.pullEventRaw()
- until (e=="modem_message" and c==ch and (not msg or msg==m)) or (e=="timer" and s==id) --If it matches or times out then end the loop.
- if e=="timer" then return false end
- return true
- end
- if not checkIfBeacon(channel,msg) then
- return false, "Invalid beacon."
- end
- turtle.up() --start moving out of the way
- turtle.turnRight()--^
- turtle.turnRight()--^
- turtle.forward() --^
- local function getData() --Get data
- modem.transmit(channel,channel,"PING")
- local eventData
- local tID=os.startTimer(5)
- repeat
- eventData={os.pullEventRaw()}
- if eventData[1]=="modem_message" and (eventData[5]==msg or msg==nil) then return eventData end
- --print("test")
- until eventData[1]=="terminate" or (eventData[1]=="timer" and eventData[2]==tID)
- return (eventData[1]=="terminate" and error("Terminated",0)) or error("Lost signal.",2)--
- end
- local function getCloser(func)
- while true do
- func()
- local nDat=getData()
- if nDat[6]>distanceT[#distanceT] or nDat[6]==1 then break --If it got farther or its next to it then exit the loop.
- else
- table.insert(distanceT,nDat[6]) --Insert to the distance table.
- end
- end
- end
- local eventData=getData() --Get the data
- if isGPS then msg=eventData[5] end --if its a gps then the message is that message
- table.insert(distanceT,eventData[6])
- turtle.up()
- local nData=getData()
- table.insert(distanceT,nData[6])
- if distanceT[#distanceT-1]>distanceT[#distanceT] then
- --This means its higher
- getCloser(up)
- turtle.digDown()
- turtle.down()
- else
- --Lower
- getCloser(down)
- turtle.digUp()
- turtle.up()
- end
- local eventData=getData()
- table.insert(distanceT,eventData[6])
- turtle.forward()
- local nData=getData()
- table.insert(distanceT,nData[6])
- if distanceT[#distanceT-1]>distanceT[#distanceT] then
- --Keep going forward
- getCloser(fd)
- turtle.back()
- else
- --Moonwalk
- turtle.turnRight()
- turtle.turnRight()
- getCloser(fd)
- turtle.back()
- --turtle.turnRight()
- --turtle.turnRight()
- end
- --Turn right and do (almost) the same thing
- turtle.turnRight()
- local eventData=getData()
- table.insert(distanceT,eventData[6])
- turtle.forward()
- local nData=getData()
- table.insert(distanceT,nData[6])
- if distanceT[#distanceT-1]>distanceT[#distanceT] then
- --Keep going forward
- getCloser(function() repeat turtle.dig() turtle.attack() until (turtle.forward()) end)
- else
- --Moonwalk
- turtle.turnRight()
- turtle.turnRight()
- getCloser(function() repeat turtle.dig() turtle.attack() until (turtle.forward()) end)
- end
- return peripheral.getType("front")=="computer" or peripheral.getType("front")=="turtle" --Return true if its an actual host.
- end
- function host(channel, msg) --Host a beacon
- modem.closeAll()
- modem.open(channel)
- repeat
- local eventData={os.pullEventRaw("modem_message")}
- if eventData[1]=="modem_message" and eventData[3]==channel and eventData[5]=="PING" then --If its pinged send the message
- modem.transmit(channel,channel,msg)
- end
- until eventData[1]=="terminate"
- return error("Terminated",0)
- end
Advertisement
Add Comment
Please, Sign In to add comment