Advertisement
Lion4ever

Plot Border

Mar 12th, 2015
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.23 KB | None | 0 0
  1. if not turtle then
  2.     return
  3. end
  4.  
  5. local fd = turtle.forward
  6. local bk = turtle.back
  7. local up = turtle.up
  8. local dn = turtle.down
  9. local lt = turtle.turnLeft
  10. local rt = turtle.turnRight
  11. local dg = turtle.dig
  12. local pl = turtle.place
  13. local rf,el,er = turtle.refuel,turtle.equipLeft,turtle.equipRight
  14. local gfl = turtle.getFuelLevel
  15.  
  16. local cy = coroutine.yield
  17. local pc = pcall
  18. local smt = setmetatable
  19. local oc = os.clock
  20. local ost = os.startTimer
  21.  
  22. local lfl
  23. local ok,x,y,z= pcall(gps.locate)
  24.  
  25. if not y then--modem not found bug
  26.     for i=1,16 do
  27.         if turtle.getItemCount(i) == 0 then
  28.             turtle.select(i)
  29.             break
  30.         end
  31.     end
  32.     if turtle.getItemCount() == 0 then
  33.         turtle.equipLeft()
  34.         turtle.equipLeft()
  35.         turtle.equipRight()
  36.         turtle.equipRight()
  37.         ok,x,y,z= pcall(gps.locate)
  38.     end
  39. end
  40.  
  41. if not ok then
  42.     x = nil
  43. end
  44.  
  45. turtle.native = nil
  46. local function checkGps(ignore)
  47.     if not x or ignore then
  48.         _G.turtle = smt({refuel=rf,equipLeft=el,equipRight=er},{__index=function() return function() error("The turtle functions are blocked",2) end end})
  49.         if not x then print ("Gps Locating failed") end
  50.         error("Turtle functions blocked.",0)
  51.     end
  52. end
  53. checkGps()
  54.  
  55. local isAdmin = false
  56.  
  57. if x%39<4 or x%39>35 or z%39<4 or z%39>35 then
  58.     isAdmin = true
  59. end
  60.  
  61. if fs.exists("rom/adminturtles/"..os.getComputerID()) then
  62.     isAdmin = true
  63. end
  64.  
  65. if isAdmin then
  66. print("This is an admin turtle now")
  67. return
  68. end
  69.  
  70. local fx,fz =1,0
  71.  
  72. local function isInBounds(fm,ym)
  73.     return (x+fx*fm)%39>=4 and (x+fx*fm)%39<=35 and (z+fz*fm)%39>=4 and (z+fz*fm)%39<=35
  74. end
  75.  
  76. for i=1,4 do
  77.     if not isInBounds(1,0) then
  78.         printError("Turtle can't start on the border of a plot")
  79.         checkGps(true)
  80.     end
  81.     fx,fz=fz,-fx
  82. end
  83. fx,fz = nil,nil
  84.  
  85. local testOK = pcall(function()
  86. while not fx do
  87. if turtle.forward() then
  88. local nx,ny,nz = gps.locate()
  89. fx,fz = nx-x,nz-z
  90. repeat until turtle.back()
  91. elseif turtle.back() then
  92. local nx,ny,nz = gps.locate()
  93. fx,fz = x-nx,z-nz
  94. repeat until turtle.forward()
  95. end
  96. end
  97. end)
  98. if not testOK then
  99. checkGps(true) --block it
  100. end
  101.  
  102. lfl = gfl()
  103.  
  104.  
  105. local function checkIfMoved(move,fm,ym)
  106.     if isInBounds(fm,ym) then
  107.         local stime = oc()
  108.         local o,r,r2=pc(move)
  109.         if not o then
  110.             print("reached")
  111.             ost(2)
  112.             repeat
  113.                 cy("timer")
  114.             until oc()-stime >= 2
  115.         end
  116.         local o2,fl = pc(gfl)
  117.         if not o2 then
  118.             checkGps(true)
  119.         end
  120.         if fl < lfl then
  121.             x=x+fx*fm
  122.             y=y+ym
  123.             z=z+fz*fm
  124.             lfl = fl
  125.         end
  126.         if o then
  127.             return r,r2
  128.         else
  129.             error(r,0)
  130.         end
  131.     end
  132.     sleep(0.2)
  133.     return false,"Can not move out of the plot"
  134. end
  135.    
  136. function turtle.back()
  137.     return checkIfMoved(bk,-1,0)
  138. end
  139.  
  140. function turtle.forward()
  141.     return checkIfMoved(fd,1,0)
  142. end
  143.  
  144. function turtle.up()
  145.     return checkIfMoved(up,0,1)
  146. end
  147.  
  148. function turtle.down()
  149.     return checkIfMoved(dn,0,-1)
  150. end
  151.  
  152. function turtle.turnLeft()
  153. fx,fz=fz,-fx
  154. return lt()
  155. end
  156. function turtle.turnRight()
  157. fx,fz=-fz,fx
  158. return rt()
  159. end
  160.  
  161. function turtle.dig()
  162. if isInBounds(1,0) then
  163. return dg()
  164. end
  165. return false,"Can not dig outside of the plot"
  166. end
  167.  
  168. function turtle.place()
  169. if isInBounds(1,0) then
  170. return pl()
  171. end
  172. return false,"Can not place blocks outside of the plot"
  173. end
  174.  
  175. --Just because it is easier
  176. gps.locate = function() return x,y,z end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement