abc123mewot

Smart Turtle API V2

May 30th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.95 KB | None | 0 0
  1. --SmartTurtleV2 By Jeremiah Lowe
  2. --(c) Jeremiah Lowe 2018
  3. --A simple turtle library for allowing
  4. --the easy creation of turtle scripts
  5.  
  6. local fuelSlot = 1
  7. local fuelBufferSize = 10
  8. local autoRefuel = false
  9. local direction = 0
  10. local invHomeName = nil
  11. local invFullCallback = nil
  12. local invTempHome = "_inv_tmp_hme"
  13. local homes = {}
  14.  
  15. --    Fwd (0)
  16. --Lft (3)  Rgt (1)
  17. --    Bwd (2)
  18.  
  19. local function normalize(val)
  20.   while val > 3 do val = val - 4 end
  21.   while val < 0 do val = val + 4 end
  22.   return val
  23. end
  24. local function _dirChg(by) direction = normalize(direction + by) end
  25. local function _movChg(by)
  26.   for k, h in pairs(homes) do
  27.     if direction == 0 then
  28.       homes[k].movementY = h.movementY + by
  29.     elseif direction == 1 then
  30.       homes[k].movementX = h.movementX + by
  31.     elseif direction == 2 then
  32.       homes[k].movementY = h.movementY - by
  33.     elseif direction == 3 then
  34.       homes[k].movementX = h.movementX - by
  35.     end
  36.   end
  37. end
  38. local function _dptChg(by)
  39.   for k, h in pairs(homes) do
  40.     homes[k].depthChange = h.depthChange + by
  41.   end
  42. end
  43. local function _autoRefuel(amt)
  44.   if not autoRefuel then return end
  45.   tamt = amt + fuelBufferSize
  46.   if tamt > turtle.getFuelLimit() then
  47.     tamt = turtle.getFuelLevel()
  48.   end
  49.   slot = turtle.getSelectedSlot()
  50.   turtle.select(fuelSlot)
  51.   if turtle.getFuelLevel() < tamt then print("Refuelling") end
  52.   while turtle.getFuelLevel() < tamt do
  53.     turtle.refuel(1)
  54.   end
  55.   turtle.select(slot)
  56. end
  57. local function _rotateHome(h)  
  58.   if not pointTo(h.direction) then return false end
  59.   return true
  60. end
  61. local function _fwdAbsVal(val)
  62.   amt = val
  63.   if val < 0 then amt = -val end
  64.   return forward(amt)
  65. end
  66. local function _moveHome(h)
  67.   --X axis
  68.   if h.movementX ~= 0 then
  69.     d = 3
  70.     if h.movementX < 0 then d = 1 end
  71.     if not pointTo(d) then return false end
  72.     if not _fwdAbsVal(h.movementX) then return false end
  73.   end
  74.   if h.movementY ~= 0 then
  75.     d = 2
  76.     if h.movementY < 0 then d = 0 end
  77.     if not pointTo(d) then return false end
  78.     if not _fwdAbsVal(h.movementY) then return false end  
  79.   end
  80.   return true  
  81. end
  82. local function _depthHome(h)
  83.   depthDir = 1
  84.   reposAmt = -h.depthChange
  85.   if h.depthChange > 0 then
  86.     depthDir = -1
  87.     reposAmt = h.depthChange
  88.   end
  89.   for i=1, reposAmt do
  90.     if depthDir > 0 then
  91.       if not turtle.up() then return false end
  92.     else
  93.       if not turtle.down() then return false end
  94.     end
  95.     _dptChg(depthDir)
  96.   end
  97.   return true
  98. end  
  99. local function _goHome(h)
  100.   if h == nil then error("Home object expected, got nil!") end
  101.   if not _depthHome(h) then return false end
  102.   if not _moveHome(h) then return false end
  103.   if not _rotateHome(h) then return false end
  104.   return true
  105. end
  106. function turnLeft(amt)
  107.   if amt == nil then amt = 1 end
  108.   for i=1,amt do
  109.     if turtle.turnLeft() then _dirChg(-1)
  110.     else return false end
  111.   end
  112.   return true
  113. end
  114. function turnRight(amt)
  115.   if amt == nil then amt = 1 end
  116.   for i=1,amt do
  117.     if turtle.turnRight() then _dirChg(1)
  118.     else return false end
  119.   end
  120.   return true
  121. end
  122. function forward(amt, digF)
  123.   if amt == nil then amt = 1 end
  124.   if digF == nil then digF = false end
  125.   _autoRefuel(amt)
  126.   for i=1,amt do
  127.     if turtle.forward() then
  128.       _movChg(1)
  129.     else
  130.       if not digF then
  131.         return false
  132.       elseif dig() and turtle.forward() then _movChg(1)
  133.       else return false end
  134.     end
  135.   end
  136.   return true
  137. end
  138. function back(amt) return backward(amt) end
  139. function backward(amt)
  140.   if amt == nil then amt = 1 end
  141.   _autoRefuel(amt)
  142.   for i=1,amt do
  143.     if turtle.back() then _movChg(-1)
  144.     else return false end
  145.   end
  146.   return true
  147. end
  148. function up(amt, dig)
  149.   if amt == nil then amt = 1 end
  150.   if dig == nil then dig = false end
  151.   _autoRefuel(amt)
  152.   for i=1,amt do
  153.     if turtle.up() then
  154.       _dptChg(1)
  155.     else
  156.       if not dig then
  157.         return false
  158.       elseif digUp() and turtle.up() then _dptChg(1)
  159.       else return false end
  160.     end
  161.   end
  162.   return true
  163. end
  164. function down(amt, dig)
  165.   if amt == nil then amt = 1 end
  166.   if dig == nil then dig = false end
  167.   _autoRefuel(amt)
  168.   for i=1,amt do
  169.     if turtle.down() then
  170.       _dptChg(-1)
  171.     else
  172.       if not dig then
  173.         return false
  174.       elseif digDown() and turtle.down() then _dptChg(-1)
  175.       else return false end
  176.     end
  177.   end
  178.   return true
  179. end
  180. local function _checkInv()
  181.   if invHomeName ~= nil and isInventoryFull() then
  182.     createHome(invTempHome)
  183.     home(invHomeName)
  184.     if invFullCallback ~= nil then
  185.       invFullCallback()
  186.     end
  187.     home(invTempHome)
  188.   end
  189. end
  190. function digUp()
  191.   _checkInv()
  192.   return turtle.digUp()
  193. end
  194. function digDown()
  195.   _checkInv()
  196.   return turtle.digDown()
  197. end
  198. function dig()
  199.   _checkInv()
  200.   return turtle.dig()
  201. end
  202. function getDepth() return depthChange end
  203. function getMovementX() return movementX end
  204. function getMovementY() return movementY end
  205. function getDirection() return direction end
  206. function pointTo(toDir)
  207.   left = true
  208.   d = direction - toDir
  209.   d2 = 4 - d
  210.   if d > d2 then left = false end
  211.   if left then
  212.     while direction ~= toDir do
  213.       if not turnLeft() then return false end
  214.     end
  215.   else
  216.     while direction ~= toDir do
  217.       if not turnRight() then return false end
  218.     end
  219.   end
  220.   return true
  221. end
  222. function goHome(homeName)
  223.   if homeName == nil then error("No home name provided!") end
  224.   h = homes[homeName]
  225.   if h == nil then error("No home with name " .. homeName .. " exists!") end
  226.   return _goHome(h)
  227. end
  228. function home(homeName) return goHome(homeName) end
  229. local function _homeObj()
  230.   local h = {}
  231.   h.movementX = 0
  232.   h.movementY = 0
  233.   h.depthChange = 0
  234.   h.direction = direction
  235.   return h
  236. end
  237. function createHome(homeName)
  238.   if homeName == nil then error("No home name provided!") end
  239.   homes[homeName] = _homeObj()
  240. end
  241. function getFreeSlots()
  242.   free = 0
  243.   for i=1,16 do
  244.     if turtle.getItemCount(i) == 0 then free = free + 1 end
  245.   end
  246.   return free
  247. end
  248. function turnAround(amt)
  249.   if amt == nil then amt = 1 end
  250.   turnRight(2 * amt)
  251. end
  252. function isInventoryFull()
  253.   for i=1,16 do
  254.     local a = turtle.getItemCount(i)
  255.     if a == 0 then return false end
  256.   end
  257.   return true
  258. end
  259. function setInventoryFullHome(homeName) invHomeName = homeName end
  260. function getInventoryFullHome() return invHomeName end
  261. function setInventoryFullCallback(cback) invFullCallback = cback end
  262. function getInventoryFullCallback() return invFullCallback end
  263. function enableAutoRefuel() autoRefuel = true end
  264. function disableAutoRefuel() autoRefuel = false end
  265. function setFuelSlot(slot)
  266.   if slot < 0 then return false end
  267.   if slot > 16 then return false end
  268.   fuelSlot = slot
  269.   return true
  270. end
  271. function getFuelSlot() return fuelSlot end
  272. function getFuelBufferSize() return fuelBufferSize end
  273. function setFuelBufferSize(size)
  274.   if size < 0 then return false end
  275.   if size > turtle.getFuelLimit() then return false end
  276.   fuelBufferSize = size
  277.   return true
  278. end
Add Comment
Please, Sign In to add comment