Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if not turtle then
- return
- end
- local fd = turtle.forward
- local bk = turtle.back
- local up = turtle.up
- local dn = turtle.down
- local lt = turtle.turnLeft
- local rt = turtle.turnRight
- local dg = turtle.dig
- local pl = turtle.place
- local rf,el,er = turtle.refuel,turtle.equipLeft,turtle.equipRight
- local gfl = turtle.getFuelLevel
- local cy = coroutine.yield
- local pc = pcall
- local smt = setmetatable
- local oc = os.clock
- local ost = os.startTimer
- local lfl
- local ok,x,y,z= pcall(gps.locate)
- if not y then--modem not found bug
- for i=1,16 do
- if turtle.getItemCount(i) == 0 then
- turtle.select(i)
- break
- end
- end
- if turtle.getItemCount() == 0 then
- turtle.equipLeft()
- turtle.equipLeft()
- turtle.equipRight()
- turtle.equipRight()
- ok,x,y,z= pcall(gps.locate)
- end
- end
- if not ok then
- x = nil
- end
- turtle.native = nil
- local function checkGps(ignore)
- if not x or ignore then
- _G.turtle = smt({refuel=rf,equipLeft=el,equipRight=er},{__index=function() return function() error("The turtle functions are blocked",2) end end})
- if not x then print ("Gps Locating failed") end
- error("Turtle functions blocked.",0)
- end
- end
- checkGps()
- local isAdmin = false
- if x%39<4 or x%39>35 or z%39<4 or z%39>35 then
- isAdmin = true
- end
- if fs.exists("rom/adminturtles/"..os.getComputerID()) then
- isAdmin = true
- end
- if isAdmin then
- print("This is an admin turtle now")
- return
- end
- local fx,fz =1,0
- local function isInBounds(fm,ym)
- return (x+fx*fm)%39>=4 and (x+fx*fm)%39<=35 and (z+fz*fm)%39>=4 and (z+fz*fm)%39<=35
- end
- for i=1,4 do
- if not isInBounds(1,0) then
- printError("Turtle can't start on the border of a plot")
- checkGps(true)
- end
- fx,fz=fz,-fx
- end
- fx,fz = nil,nil
- local testOK = pcall(function()
- while not fx do
- if turtle.forward() then
- local nx,ny,nz = gps.locate()
- fx,fz = nx-x,nz-z
- repeat until turtle.back()
- elseif turtle.back() then
- local nx,ny,nz = gps.locate()
- fx,fz = x-nx,z-nz
- repeat until turtle.forward()
- end
- end
- end)
- if not testOK then
- checkGps(true) --block it
- end
- lfl = gfl()
- local function checkIfMoved(move,fm,ym)
- if isInBounds(fm,ym) then
- local stime = oc()
- local o,r,r2=pc(move)
- if not o then
- print("reached")
- ost(2)
- repeat
- cy("timer")
- until oc()-stime >= 2
- end
- local o2,fl = pc(gfl)
- if not o2 then
- checkGps(true)
- end
- if fl < lfl then
- x=x+fx*fm
- y=y+ym
- z=z+fz*fm
- lfl = fl
- end
- if o then
- return r,r2
- else
- error(r,0)
- end
- end
- sleep(0.2)
- return false,"Can not move out of the plot"
- end
- function turtle.back()
- return checkIfMoved(bk,-1,0)
- end
- function turtle.forward()
- return checkIfMoved(fd,1,0)
- end
- function turtle.up()
- return checkIfMoved(up,0,1)
- end
- function turtle.down()
- return checkIfMoved(dn,0,-1)
- end
- function turtle.turnLeft()
- fx,fz=fz,-fx
- return lt()
- end
- function turtle.turnRight()
- fx,fz=-fz,fx
- return rt()
- end
- function turtle.dig()
- if isInBounds(1,0) then
- return dg()
- end
- return false,"Can not dig outside of the plot"
- end
- function turtle.place()
- if isInBounds(1,0) then
- return pl()
- end
- return false,"Can not place blocks outside of the plot"
- end
- --Just because it is easier
- gps.locate = function() return x,y,z end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement