Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Modems:
- --91 To Server
- --92 To Client
- sleep(1)
- for i=1,16 do
- turtle.select(i)
- turtle.refuel()
- end
- print("Starting program")
- print("Fuel level:",turtle.getFuelLevel())
- rotation=0
- firstX,firstY,firstZ=gps.locate()
- print("Starting coordinates:", firstX, firstY, firstZ)
- while turtle.detect() do
- turtle.turnLeft()
- end
- turtle.forward()
- x,y,z=gps.locate()
- if x<firstX then
- rotation=2
- end
- if x>firstX then
- rotation=0
- end
- if z<firstZ then
- rotation=3
- end
- if z>firstZ then
- rotation=1
- end
- print("Current coordinates:", x, y, z, "Rotation:", rotation)
- modem=peripheral.find("modem")
- modem.open(92)
- modem.transmit(91,92,os.getComputerID())
- function splitString(str, sep)
- if type(str) ~= "string" then
- return {}
- end
- if sep == nil then
- sep = " "
- end
- local t = {}
- local start = 1
- while true do
- local sepStart, sepEnd = string.find(str, sep, start, true)
- if not sepStart then
- table.insert(t, string.sub(str, start))
- break
- end
- table.insert(t, string.sub(str, start, sepStart - 1))
- start = sepEnd + 1
- end
- return t
- end
- function turnLeft()
- turtle.turnLeft()
- rotation=rotation-1
- if rotation<0 then
- rotation=3
- end
- end
- function turnRight()
- turtle.turnRight()
- rotation=rotation+1
- if rotation>3 then
- rotation=0
- end
- end
- function gotoRotation(rot)
- while rotation~=rot do
- turnRight()
- end
- end
- function goForward()
- if turtle.forward()==false then
- gotoRotation(math.random(0,3))
- turtle.up()
- turtle.forward()
- turtle.up()
- turtle.forward()
- end
- end
- function goUp()
- if turtle.up()==false then
- gotoRotation(math.random(0,3))
- goForward()
- goForward()
- turtle.up()
- end
- end
- function goDown()
- if turtle.down()==false then
- gotoRotation(math.random(0,3))
- turtle.forward()
- turtle.forward()
- turtle.down()
- end
- end
- function forceForward()
- while turtle.forward()==false do
- sleep(0.01)
- end
- end
- function forceUp()
- while turtle.up()==false do
- sleep(0.01)
- end
- end
- function forceDown()
- while turtle.down()==false do
- sleep(0.01)
- end
- end
- function goToLoc(goX,goY,goZ,goDir,force)
- if force==0 then
- x, y, z = gps.locate()
- while x ~= goX or y ~= goY or z ~= goZ do
- if x < goX then
- gotoRotation(0)
- goForward()
- elseif x > goX then
- gotoRotation(2)
- goForward()
- elseif z < goZ then
- gotoRotation(1)
- goForward()
- elseif z > goZ then
- gotoRotation(3)
- goForward()
- elseif y < goY then
- goUp()
- elseif y > goY then
- goDown()
- end
- x, y, z = gps.locate()
- end
- if goDir then
- gotoRotation(goDir)
- end
- end
- if force==1 then
- x, y, z = gps.locate()
- while x ~= goX or y ~= goY or z ~= goZ do
- if x < goX then
- gotoRotation(0)
- forceForward()
- elseif x > goX then
- gotoRotation(2)
- forceForward()
- elseif z < goZ then
- gotoRotation(1)
- forceForward()
- elseif z > goZ then
- gotoRotation(3)
- forceForward()
- elseif y < goY then
- forceUp()
- elseif y > goY then
- forceDown()
- end
- x, y, z = gps.locate()
- end
- if goDir then
- gotoRotation(goDir)
- end
- end
- end
- while true do
- local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
- print(("Message received on side %s on channel %d (reply to %d) from %f blocks away with message %s"):format(
- side, channel, replyChannel, distance, tostring(message)
- ))
- if message=="reboot" then
- print("Rebooting...")
- os.reboot()
- end
- splitMessage=splitString(message, " ")
- if splitMessage[1]=="forceGo" then
- newx=tonumber(splitMessage[2])
- newy=tonumber(splitMessage[3])
- newz=tonumber(splitMessage[4])
- id=tonumber(splitMessage[5])
- force=tonumber(splitMessage[6])
- if os.getComputerID()==id then
- print("Forcing go to coordinates:", newx, newy, newz)
- x,y,z=gps.locate()
- yDiff=math.random(5,15)
- --goToLoc(x,y+yDiff,z,0)
- --goToLoc(newx,newy+yDiff,newz,0)
- goToLoc(newx,newy,newz,0,force)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment