Advertisement
OfficialStamper

botLiving.lua

Jan 30th, 2023
733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.50 KB | Gaming | 0 0
  1. local growingSpeed = 31 -- 31 when using , else 61
  2. local growingCount = 0
  3.  
  4. local function getEmpty()
  5.     -- find the first empty slot and return the slot number
  6.     local rc = false
  7.     local slot = 0
  8.     while not rc and slot <= 16 do
  9.         slot = slot + 1
  10.         if turtle.getItemCount(slot) == 0 then
  11.             rc = slot
  12.         end
  13.     end
  14.     return rc
  15. end
  16.  
  17. local function place(pSlot)
  18.     local s = turtle.getSelectedSlot()
  19.     turtle.select(pSlot)
  20.     turtle.place()
  21.     turtle.select(s)
  22.     growingCount = growingCount + 1
  23. end
  24.  
  25. local function digDrop()
  26.     local s = turtle.getSelectedSlot()
  27.     local u = getEmpty()
  28.     turtle.select(u)
  29.     turtle.dig()
  30.     turtle.dropDown()
  31.     turtle.select(s)
  32.     growingCount = growingCount - 1
  33. end
  34.  
  35. local grow = {
  36.     ['grow'] = true,
  37.     ['grown'] = false,
  38.     ['action'] = place
  39. }
  40.  
  41. local grown = {
  42.     ['grow'] = false,
  43.     ['grown'] = true,
  44.     ['action'] = digDrop
  45. }
  46.  
  47. local items = {
  48.     ['minecraft:log']       = grow,
  49.     ['minecraft:logs']      = grow,
  50.     ['minecraft:oak_log']   = grow,
  51.     ['minecraft:stone']     = grow,
  52.     ['botania:livingwood']  = grown,
  53.     ['botania:livingrock']  = grown
  54. }
  55.  
  56. local function tidyUp()
  57.     for i = 1, 16 do
  58.         if turtle.getItemCount(i) > 0 then
  59.             local iData = turtle.getItemDetail(i)
  60.             if type(items[iData.name]) == 'table' then
  61.                 if not items[iData.name].grow then
  62.                     turtle.dropDown()
  63.                 end
  64.             else
  65.                 turtle.dropDown()
  66.             end
  67.         end
  68.     end
  69. end
  70.  
  71. local function getItem()
  72.     for i = 1, 16 do
  73.         if turtle.getItemCount(i) > 0 then
  74.             local iData = turtle.getItemDetail(i)
  75.             if type(items[iData.name]) == 'table' then
  76.                 if items[iData.name].grow then
  77.                     items[iData.name].action(i)
  78.                     return true
  79.                 else
  80.                     turtle.dropDown()
  81.                 end
  82.             else
  83.                 turtle.dropDown()
  84.             end
  85.         end
  86.     end
  87.     return false
  88. end
  89.  
  90. --START MAIN
  91.  
  92. tidyUp()
  93.  
  94. while true do
  95.     for m = 1, 4 do
  96.         turtle.turnRight()
  97.         local rc, tData = turtle.inspect()
  98.         if tData.name ~= nil then
  99.             --There is a block
  100.             if type(items[tData.name]) == 'table' then
  101.                 if items[tData.name].grown then
  102.                     items[tData.name].action()
  103.                     turtle.suckUp(1)
  104.                     getItem()
  105.                 elseif items[tData.name].grow then
  106.                     -- do nothing, wait
  107.                 else
  108.                     --Unknown block, mine it
  109.                     digDrop()
  110.                 end
  111.             else
  112.                 --Unknown block, mine it
  113.                 digDrop()
  114.             end
  115.  
  116.         else
  117.             --no block, Look for an eligible item in inventory
  118.             if not getItem() then
  119.                 tidyUp()
  120.                 turtle.suckUp(1)
  121.                 getItem()
  122.             end
  123.         end
  124.     end
  125.     if growingCount == 4 then
  126.         os.sleep(growingSpeed)
  127.     else
  128.         os.sleep(2)
  129.         growingCount = 0
  130.     end
  131. end
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement