largeNumberGoeshere

dietree.lua

Apr 19th, 2021 (edited)
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.24 KB | None | 0 0
  1. count = 10        --max height turtle will go
  2. fuelBuffer =  50
  3. idleTime = 0.2
  4. maxSlotsFull = 16
  5. bonemealSlot = 12
  6.  
  7. print("Make sure to put sappings in slot one and logs in slot two and bonemal in slot 16")
  8.  
  9. -- make sure a slot is as full as possible
  10. function combine(slot)    
  11.     ---Make sure slot is not already full
  12.     turtle.select(slot)
  13.     if turtle.getItemCount() == 64 then return end
  14.    
  15.         if turtle.getItemCount() > 1 then
  16.              for i=1,16 do
  17.                     turtle.select(i)
  18.                     if turtle.getItemCount() >0 then
  19.                         if turtle.compareTo(i) then
  20.                                 turtle.transferTo(slot)
  21.                         end    
  22.                     end
  23.              end  
  24.         else
  25.             return "slot empty"
  26.         end
  27.    
  28.  
  29. end
  30.  
  31. -- see if there is tree left to dig up
  32. function isBlock()
  33.     local fwd = turtle.detect()
  34.     local up turtle.detectUp()
  35.     if fwd == true then return true end
  36.     if up == true then return true end
  37.     return false
  38. end
  39.  
  40.  
  41. function main()
  42.     --- do something repeatedly
  43.     function rpt(func,count)
  44.             for i=1, count do
  45.                     func()
  46.             end
  47.     end
  48.  
  49.     --- dig log then go up
  50.     for i=1, count do
  51.             turtle.dig()
  52.             turtle.suck()
  53.             turtle.digUp()
  54.             turtle.suck()
  55.             turtle.up()
  56.                 -- if no logs left, go back
  57.         if not isBlock() then
  58.                     rpt(
  59.                         function()
  60.                             turtle.down()
  61.                         end, i
  62.                     )
  63.                     return
  64.                 end
  65.                
  66.     end
  67.        
  68.         -- go back down if haven't already
  69.    rpt(
  70.        turtle.down,
  71.        count
  72.    )
  73. end
  74.  
  75.  
  76.  
  77.  
  78. isFullCounterMax = 10
  79. isFullCounter = 0   -- allows running only every n-th call
  80. function isFull(maxFull)
  81.     isFullCounter = isFullCounter + 1
  82.     if isFullCounter < isFullCounterMax then return end
  83.     isFullCounter = 0
  84.     local c1 = combine(1)
  85.  local c2 = combine(2)
  86.     if c1=="slot empty" or c2 =="slot empty" then
  87.         print("Please refill slots 1 and 2")
  88.         while combine(1) == "slot empty" do
  89.             sleep(2)
  90.             write(".")
  91.         end
  92.         while combine(2) == "slot empty" do
  93.             sleep(2)
  94.             write(".")
  95.         end
  96.        
  97.     end
  98.  
  99.  
  100.    
  101.     function returnIfEnough(slotsEmpty,slotsFull,maxEmpty,maxFull)
  102.         --print("slots empty / max empty: "..tostring(slotsEmpty).."/"..tostring(maxEmpty))
  103.        
  104.         if slotsEmpty>=maxEmpty then
  105.             return false
  106.        
  107.         elseif slotsFull>=maxFull then
  108.             return true
  109.        
  110.         else
  111.             return nil
  112.        
  113.         end
  114.     end
  115.    
  116.     --- --maxFull    = n
  117.     local slotsEmpty = 0
  118.     local slotsFull  = 0
  119.    
  120.     local maxEmpty = 17-maxFull
  121.     for slot=1,16 do
  122.         turtle.select(slot)
  123.         local qtyInSlot = turtle.getItemCount()
  124.         local isEmpty = qtyInSlot < 1
  125.        
  126.         if isEmpty then
  127.             slotsEmpty = slotsEmpty+1
  128.         else
  129.             slotsFull = slotsFull+1
  130.         end
  131.        
  132.         local enough = returnIfEnough(slotsEmpty,slotsFull,maxEmpty,maxFull)
  133.         if enough ~= nil then return enough end
  134.     end
  135.    
  136.    
  137.     local enough = returnIfEnough(slotsEmpty,slotsFull,maxEmpty,maxFull)
  138.     if enough ~= nil then return enough end
  139.    
  140.     error("unknown error @isFull")
  141.    
  142.    
  143. end
  144.  
  145. function isTree()
  146.    --is there a tree?
  147.    local i1,i2 = turtle.inspect()
  148.    local block = i2.name or "nothing"
  149.    local affix = string.sub(block,-3,-1)
  150.  
  151.    if turtle.detect() and affix == "log" then
  152.        main()
  153.        turtle.select(1)
  154.        turtle.place()
  155.      --organise slotss
  156.  
  157.    end
  158. end
  159.        
  160. while true do
  161.         -- if there's a tree, then dig it up
  162.         isTree()
  163.  
  164.     --idle some
  165.     sleep(idleTime )
  166.    
  167.     --refuel if neccesary and show fuel level
  168.         local fuelSlot = 2
  169.     local fuel = turtle.getFuelLevel()
  170.     while turtle.getFuelLevel() < fuelBuffer do
  171.         turtle.select(fuelSlot)
  172.         turtle.refuel(1)
  173.                 if turtle.getItemCount() <1 then
  174.                         print("Please put more fuel into slot "..tostring(fuelSlot))
  175.                         sleep(2)
  176.                         while turtle.getItemCount() <1 do
  177.                             sleep(2)
  178.                             write(" .")
  179.                         end
  180.                 end
  181.         end
  182.         print(turtle.getFuelLevel())
  183.  
  184.    --Place a new sappling  
  185.    turtle.select(1)
  186.    turtle.place()
  187.      
  188.      -- pickup left over items
  189.    turtle.suck()   
  190.    turtle.suckUp()
  191.  
  192.   --Allow for bonemeal being used
  193.   turtle.select(bonemealSlot)
  194.   turtle.place()
  195.    
  196.     --Stop if full
  197.     if isFull(maxSlotsFull) then
  198.         print("Overfilled, please empty turtle to continue")
  199.         while isFull(maxSlotsFull) do
  200.             sleep(2)
  201.         end
  202.         print("resuming")
  203.     end
  204.    
  205. end
Add Comment
Please, Sign In to add comment