apencer1w

Turtle API

Apr 8th, 2021
473
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local forwardVector = {
  2.     [0] = vec.new( 0, 0, -1),
  3.     [1] = vec.new( 1, 0,  0),
  4.     [2] = vec.new( 0, 0,  1),
  5.     [3] = vec.new(-1, 0,  0)
  6. }
  7.  
  8. local upVector = vec.new(0, 1, 0)
  9.  
  10. local stringToDir = {
  11.     ["north"] = 0,
  12.     ["west "] = 1,
  13.     ["south"] = 2,
  14.     ["east "] = 3
  15. }
  16.  
  17. local dirToString = {
  18.     [0] = "north",
  19.     [1] = "west",
  20.     [2] = "south",
  21.     [3] = "east",
  22. }
  23.  
  24. local pos = vec.new()
  25. local dir = 0
  26.  
  27. local refuelLimit = 500
  28.  
  29. local nop = function() end
  30.  
  31. --MAYBE CHANGE
  32. local function slotHasFuel(n)
  33.     local s = turtle.getItemDetail(n)
  34.     if s ~= nil then
  35.         return  s.name:find("coal"     ) ~= nil or
  36.                 s.name:find("wood"     ) ~= nil or
  37.                 s.name:find("log"      ) ~= nil or
  38.                 s.name:find("blaze_rod") ~= nil or
  39.                 s.name:find("lava"     ) ~= nil or
  40.                 s.name == "minecraft:stick"
  41.     else
  42.         return false
  43.     end
  44. end
  45.  
  46. function refuel(force)
  47.     if force == nil then force = false end
  48.  
  49.     local s = false
  50.     if turtle.getFuelLevel() < refuelLimit or force then
  51.         local oldSlot = turtle.getSelectedSlot()
  52.    
  53.         for i = 1, 16 do
  54.             if slotHasFuel(i) then
  55.                 turtle.select(i)
  56.                 turtle.refuel(64)
  57.                 s = true
  58.             end
  59.         end
  60.        
  61.         turtle.select(oldSlot)
  62.     end
  63.    
  64.     --CHANGE THIS
  65.     -- if s == false then
  66.         -- os.queueEvent("noFuel")
  67.     -- end
  68.    
  69.     return s
  70. end
  71.  
  72. function setPos(x, y, z)
  73.     pos:change(x, y, z)
  74. end
  75.  
  76. function setPosGPS()
  77.     local s = vec.fromGPS()
  78.     if s == nil then return false end
  79.     setPos(s)
  80.     return true
  81. end
  82.  
  83. function getPos()
  84.     return pos
  85. end
  86.  
  87. function setDir(d)
  88.     dir = d % 4
  89. end
  90.  
  91. function getDir()
  92.     return dir
  93. end
  94.  
  95. function setRefuelLimit(n)
  96.     refuelLimit = n
  97. end
  98.  
  99. function getRefuelLimit()
  100.     return refuelLimit
  101. end
  102.  
  103. local dirMap = {
  104.     [0] =  0,
  105.     [1] =  1,
  106.     [2] =  2,
  107.     [3] = -1
  108. }
  109.  
  110. function face(d)
  111.     if type(d) == "string" then
  112.         d = stringToDir[d:lower()]
  113.         assert(d ~= nil, "Unexpected direction. Use either 'North', 'West', 'South', or 'East.'")
  114.     end
  115.    
  116.     d = d % 4
  117.    
  118.     rt(dirMap[(d - dir) % 4])
  119. end
  120.  
  121.  
  122. function rt(n, func)
  123.     if not n then n = 1 end
  124.     if not func then func = nop end
  125.    
  126.     if n < 0 then return lt(-n, func) end
  127.    
  128.     local i = 0
  129.     while i < n or n == 0 do
  130.         if func(i) then break end
  131.        
  132.         turtle.turnRight()
  133.         dir = (dir + 1) % 4
  134.        
  135.         i = i + 1
  136.     end
  137.    
  138.     return i
  139. end
  140.  
  141. function lt(n, func)
  142.     if not n then n = 1 end
  143.     if not func then func = nop end
  144.    
  145.     if n < 0 then return rt(-n, func) end
  146.    
  147.     local i = 0
  148.     while i < n or n == 0 do
  149.         if func(i) then break end
  150.        
  151.         turtle.turnLeft()
  152.         dir = (dir - 1) % 4
  153.        
  154.         i = i + 1
  155.     end
  156.    
  157.     return i
  158. end
  159.  
  160. function fd(n, func, canBreak)
  161.     if not n then n = 1 end
  162.     if not func then func = nop end
  163.    
  164.     if n < 0 then return bk(-n, func) end
  165.    
  166.     if canBreak == nil then canBreak = true end
  167.    
  168.     local i = 0
  169.     while i < n or n == 0 do
  170.         if func(i) then break end
  171.    
  172.         refuel()
  173.        
  174.         if not turtle.forward() then
  175.             if canBreak then
  176.                 repeat until not turtle.dig()
  177.                 if not turtle.forward() then break end
  178.             else
  179.                 break
  180.             end
  181.         end
  182.        
  183.         pos = pos + forwardVector[dir]
  184.         i = i + 1
  185.     end
  186.    
  187.     return i
  188. end
  189.  
  190. function bk(n, func)
  191.     if not n then n = 1 end
  192.     if not func then func = nop end
  193.    
  194.     if n < 0 then return fd(-n, func) end
  195.    
  196.     local i = 0
  197.     while i < n or n == 0 do
  198.         if func(i) then break end
  199.        
  200.         refuel()
  201.    
  202.         if not turtle.back() then break end
  203.        
  204.         pos = pos - forwardVector[dir]
  205.         i = i + 1
  206.     end
  207.    
  208.     return i
  209. end
  210.  
  211. function up(n, func, canBreak)
  212.     if not n then n = 1 end
  213.     if not func then func = nop end
  214.    
  215.     if n < 0 then return dn(-n, func) end
  216.    
  217.     if canBreak == nil then canBreak = true end
  218.    
  219.     local i = 0
  220.     while i < n or n == 0 do
  221.         if func(i) then break end
  222.        
  223.         refuel()
  224.        
  225.         if not turtle.up() then
  226.             if canBreak then
  227.                 repeat until not turtle.digUp()
  228.                 if not turtle.up() then break end
  229.             else
  230.                 break
  231.             end
  232.         end
  233.        
  234.         pos = pos + upVector
  235.         i = i + 1
  236.     end
  237.    
  238.     return i
  239. end
  240.  
  241. function dn(n, func, canBreak)
  242.     if not n then n = 1 end
  243.     if not func then func = nop end
  244.    
  245.     if n < 0 then return up(-n, func) end
  246.    
  247.     if canBreak == nil then canBreak = true end
  248.    
  249.     local i = 0
  250.     while i < n or n == 0 do
  251.         if func(i) then break end
  252.        
  253.         refuel()
  254.        
  255.         if not turtle.down() then
  256.             if canBreak then
  257.                 repeat until not turtle.digDown()
  258.                 if not turtle.down() then break end
  259.             else
  260.                 break
  261.             end
  262.         end
  263.        
  264.         pos = pos - upVector
  265.         i = i + 1
  266.     end
  267.    
  268.     return i
  269. end
  270.  
  271. function insFd()
  272.     return {turtle.inspect()}
  273. end
  274.  
  275. function insUp()
  276.     return {turtle.inspectUp()}
  277. end
  278.  
  279. function insDn()
  280.     return {turtle.inspectDown()}
  281. end
  282.  
  283. atkFd = turtle.attack
  284. atkUp = turtle.attackUp
  285. atkDn = turtle.attackDown
  286.  
  287. digFd = turtle.dig
  288. digUp = turtle.digUp
  289. digDn = turtle.digDown
  290.  
  291. plcFd = turtle.place
  292. plcUp = turtle.placeUp
  293. plcDn = turtle.placeDown
  294.  
  295. dctFd = turtle.detect
  296. dctUp = turtle.detectUp
  297. dctDn = turtle.detectDown
  298.  
  299. cmpFd = turtle.compare
  300. cmpUp = turtle.compareUp
  301. cmpDn = turtle.compareDown
  302.  
  303. sucFd = turtle.suck
  304. sucUp = turtle.suckUp
  305. sucDn = turtle.suckDown
  306.  
  307. drpFd = turtle.drop
  308. drpUp = turtle.dropUp
  309. drpDn = turtle.dropDown
  310.  
  311. getFuelLevel = turtle.getFuelLevel
  312.  
  313. select = turtle.select
  314. getSelectedSlot = turtle.getSelectedSlot
  315. getItemDetail = turtle.getItemDetail
  316. transferTo = turtle.transferTo
  317.  
  318. equipLeft = turtle.equipLeft
  319. equipRight = turtle.equipRight
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×