Lion4ever

Turtle API

Sep 12th, 2013
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.06 KB | None | 0 0
  1. local old_fd = old_fd or turtle.forward
  2. local old_bk = old_bk or turtle.back
  3. local old_lt = old_lt or turtle.turnLeft
  4. local old_rt = old_rt or turtle.turnRight
  5. local old_up = old_up or turtle.up
  6. local old_dn = old_dn or turtle.down
  7. saveOnMove = false
  8. saveOnTurn = true
  9. local x,y,z,fx,fz
  10. local filePos = {}
  11. if fs.exists("turtle position") then
  12.     local f = fs.open("turtle position","r")
  13.     filePos = textutils.unserialize(f.readAll() or "")
  14.     f.close()
  15. end
  16. local gpsPos = {gps.locate(0)}
  17. if filePos.x and gpsPos[1] then
  18.     x,y,z,fx,fz = gpsPos[1],gpsPos[2],gpsPos[3],filePos.fx,filePos.fz
  19. elseif gpsPos[1] then
  20.     if not turtle.forward() then
  21.         print("Need to move forward to get Facing")
  22.         while not turtle.forward() do
  23.             turtle.refuel(1)
  24.             sleep(1)
  25.         end
  26.     end
  27.     local gpsPos2 = {gps.locate(0)}
  28.     repeat until turtle.back()
  29.     x,y,z,fx,fz = gpsPos[1],gpsPos[2],gpsPos[3],gpsPos2[1]-gpsPos[1],gpsPos2[3]-gpsPos[3]
  30. elseif filePos.x then
  31.     x,y,z,fx,fz = filePos.x,filePos.y,filePos.z,filePos.fx,filePos.fz
  32. else
  33.     local display = {"X","Y","Z","FX","FZ"}
  34.     local result = {}
  35.     local input
  36.     for i=1,5 do
  37.         repeat
  38.             write(display[i]..": ")
  39.             input = tonumber(read())
  40.         until input
  41.         table.insert(result,input)
  42.     end
  43.     x,y,z,fx,fz = unpack(result)
  44. end
  45. local function save()
  46.     local f = fs.open("turtle position","w")
  47.     f.write(textutils.serialize(getPos()))
  48.     f.close()
  49. end
  50. function turtle.forward()
  51.     if turtle.detect() then return false,"Movement obstructed" end
  52.     x = x + fx
  53.     z = z + fz
  54.     if saveOnMove then save() end
  55.     local r,m = old_fd()
  56.     if not r then
  57.         x = x - fx
  58.         z = z - fz
  59.         if saveOnMove then save() end
  60.     end
  61.     return r,m
  62. end
  63. function turtle.back()
  64.     x = x - fx
  65.     z = z - fz
  66.     if saveOnMove then save() end
  67.     local r,m = old_bk()
  68.     if not r then
  69.         x = x + fx
  70.         z = z + fz
  71.         if saveOnMove then save() end
  72.         sleep(1)
  73.     end
  74.     return r,m
  75. end
  76. function turtle.up()
  77.     if turtle.detectUp() then return false,"Movement obstructed" end
  78.     y = y + 1
  79.     if saveOnMove then save() end
  80.     local r,m = old_up()
  81.     if not r then
  82.         y = y - 1
  83.         if saveOnMove then save() end
  84.     end
  85.     return r,m
  86. end
  87. function turtle.down()
  88.     if turtle.detectDown() then return false,"Movement obstructed" end
  89.     y = y - 1
  90.     if saveOnMove then save() end
  91.     local r,m = old_dn()
  92.     if not r then
  93.         y = y + 1
  94.         if saveOnMove then save() end
  95.     end
  96.     return r,m
  97. end
  98. function turtle.turnLeft()
  99.     fx,fz = fz,-fx
  100.     if saveOnTurn then save() end
  101.     return old_lt()
  102. end
  103. function turtle.turnRight()
  104.     fx,fz = -fz,fx
  105.     if saveOnTurn then save() end
  106.     return old_rt()
  107. end
  108. function face(tfx,tfz)
  109.     if tfx==-fz and tfz==fx then
  110.         turtle.turnRight()
  111.     end
  112.     while tfx~=fx or tfz~=fz do
  113.         turtle.turnLeft()
  114.     end
  115.     return true
  116. end
  117. function goto(tx,ty,tz,tfx,tfz,startWithDir)
  118.     if type(tx)=="table" then
  119.         if tx.x then
  120.             startWithDir=ty
  121.             tx,ty,tz,tfx,tfz = tx.x,tx.y,tx.z,tx.fx,tx.fz
  122.         end
  123.         if #tx>=3 then
  124.             startWithDir=ty
  125.             tx,ty,tz,tfx,tfz = unpack(tx)
  126.         end
  127.     end
  128.     tx,ty,tz,tfx,tfz=tonumber(tx),tonumber(ty),tonumber(tz),tonumber(tfx),tonumber(tfz)
  129.     if not (tx and ty and tz) then
  130.         return false,"Invalid coordinates"
  131.     end
  132.     local dirs = {
  133.         function()
  134.             if x<tx then
  135.                 face(1,0)
  136.                 while x<tx and turtle.forward() do end
  137.             end
  138.             if x>tx then
  139.                 face(-1,0)
  140.                 while x>tx and turtle.forward() do end
  141.             end
  142.             return x==tx
  143.         end,
  144.         function()
  145.             while y<ty and turtle.up() do end
  146.             while y>ty and turtle.down() do end
  147.             return y==ty
  148.         end,
  149.         function()
  150.             if z<tz then
  151.                 face(0,1)
  152.                 while z<tz and turtle.forward() do end
  153.             end
  154.             if z>tz then
  155.                 face(0,-1)
  156.                 while z>tz and turtle.forward() do end
  157.             end
  158.             return z==tz
  159.         end
  160.     }
  161.     local tryDir = startWithDir or 1
  162.     local successCount=0
  163.     repeat
  164.         if dirs[(tryDir%3)+1]() then
  165.             successCount = successCount + 1
  166.         else
  167.             successCount = 0
  168.         end
  169.         tryDir = tryDir + 1
  170.     until successCount>=3 or tryDir>=30
  171.     if successCount>=3 and tfx and tfz then
  172.         face(tfx,tfz)
  173.     end
  174.     return successCount>=3
  175. end
  176. local Position = {__tostring=function() return "X:"..tostring(x).." Y:"..tostring(y).." Z:"..tostring(z).." Facing:"..getFacingName() end}
  177. Position.__index = Position
  178. function Position:goto()
  179.     return goto(self.x,self.y,self.z,self.fx,self.fz)
  180. end
  181. Position.__call = Position.goto
  182. function Position:moveBy()
  183.     return goto(x+self.x,y+self.y,z+self.z,fx,fz)
  184. end
  185. function Position:gotoPoint()
  186.     return goto(self.x,self.y,self.z)
  187. end
  188. function Position:face()
  189.     return face(self.fx,self.fz)
  190. end
  191. function Position:toVector()
  192.     return vector.new(self.x,self.y,self.z)
  193. end
  194. function newPos(ax,ay,az,afx,afz)
  195.     if type(ax)=="table" then
  196.         if ax.x then
  197.             ax,ay,az,afx,afz = ax.x,ax.y,ax.z,ax.fx,ax.fz
  198.         end
  199.         if #x>=3 then
  200.             ax,ay,az,afx,afz = unpack(ax)
  201.         end
  202.     end
  203.     local result = {x=ax,y=ay,z=az,fx=afx,fz=afz}
  204.     setmetatable(result,Position)
  205.     return result
  206. end
  207. function getPos()
  208.     return newPos(x,y,z,fx,fz)
  209. end
  210. function getFacing(afx,afz)
  211.     return (afx or fx)==0 and 1-(afz or fz) or 2+(afx or fx)
  212. end
  213. function getFacingName(afx,afz)
  214.     local names={"South","West","North","East"}
  215.     return names[getFacing(afx,afz)+1]
  216. end
Advertisement
Add Comment
Please, Sign In to add comment