Advertisement
fatboychummy

semistationaryTreeFarm.lua

Sep 8th, 2019
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.10 KB | None | 0 0
  1. local sapling = "minecraft:sapling"
  2. local wood = "minecraft:log"
  3. local sleepTime = 35
  4. local beneath = {"minecraft:stone", 1} -- blockname, damage
  5.  
  6.  
  7. local function isHome()
  8.   local isBlock, block = turtle.inspectDown()
  9.   if isBlock then
  10.     if block.name == beneath[1] and block.metadata == beneath[2] then
  11.       return true
  12.     end
  13.   end
  14.  
  15.   return false
  16. end
  17.  
  18. local function moveSaplings()
  19.   local slot1 = turtle.getItemDetail(1)
  20.   if slot1 and slot1.name ~= sapling then
  21.     turtle.select(1)
  22.     for i = 2, 16 do
  23.       if turtle.transferTo(i) then
  24.         break
  25.       end
  26.     end
  27.   end
  28.  
  29.   for i = 2, 16 do
  30.     local sloti = turtle.getItemDetail(i)
  31.     if sloti and sloti.name == sapling then
  32.       turtle.transferTo(1)
  33.     end
  34.   end
  35. end
  36.  
  37. local function emptyInv()
  38.   turtle.turnRight()
  39.   for i = 2, 16 do
  40.     turtle.select(i)
  41.     turtle.drop()
  42.   end
  43.   turtle.turnLeft()
  44. end
  45.  
  46. local function digTree()
  47.   local up = 0
  48.   while turtle.detectUp() do
  49.     for i = 1, 4 do
  50.       turtle.dig()
  51.       turtle.turnRight()
  52.     end
  53.     turtle.digUp()
  54.     turtle.up()
  55.     up = up + 1
  56.   end
  57.  
  58.   for i = 1, up do
  59.     turtle.down()
  60.   end
  61.   turtle.back()
  62. end
  63.  
  64. local function refuel()
  65.   local function dropInGoodOrder()
  66.     for i = 1, 16 do
  67.       local item = turtle.getItemDetail(i)
  68.       if item and item.name == wood then
  69.         turtle.select(i)
  70.         turtle.drop()
  71.       end
  72.     end
  73.     for i = 1, 16 do
  74.       turtle.select(i)
  75.       turtle.drop()
  76.     end
  77.   end
  78.  
  79.  
  80.   local function pullWood()
  81.     print("Pulling wood")
  82.     for i = 1, 16 do
  83.       turtle.select(i)
  84.       turtle.suck()
  85.       local item = turtle.getItemDetail(i)
  86.       if item and item.name ~= wood then
  87.         turtle.drop()
  88.         break
  89.       elseif not item then
  90.         break
  91.       end
  92.     end
  93.   end
  94.  
  95.   local function grab()
  96.     turtle.select(1)
  97.     for i = 1, 16 do
  98.       turtle.suck()
  99.     end
  100.   end
  101.  
  102.   local function plankRefuel()
  103.     pullWood()
  104.  
  105.     local h = 0
  106.     print("Checking # stacks of logs")
  107.     for i = 1, 16 do
  108.       if turtle.getItemCount(i) > 0 then
  109.         h = h + 1
  110.       end
  111.     end
  112.     if h == 0 then
  113.       print("No items to refuel with.")
  114.       grab()
  115.       return
  116.     end
  117.  
  118.     print("dropping extra wood")
  119.     for i = 2, 16 do
  120.       turtle.select(i)
  121.       turtle.drop()
  122.     end
  123.  
  124.     print("Crafting.")
  125.     turtle.craft()
  126.  
  127.     print("refuelling using planks recieved.")
  128.     for i = 1, 16 do
  129.       turtle.select(i)
  130.       turtle.refuel()
  131.     end
  132.  
  133.     if h > 1 then return plankRefuel() end
  134.     grab()
  135.   end
  136.  
  137.   local max = turtle.getFuelLimit()
  138.   local cur = turtle.getFuelLevel()
  139.   print("fuel:", cur, '/', max)
  140.  
  141.   if cur < max * 0.05 then
  142.     print("Doing a refuelly boi")
  143.     turtle.turnLeft()
  144.     dropInGoodOrder()
  145.     -- drop all items in inventory into chest, wood first.
  146.     plankRefuel()
  147.     turtle.turnRight()
  148.   end
  149.  
  150. end
  151.  
  152. local function checkLog()
  153.   local isBlock, block = turtle.inspect()
  154.   if isBlock then
  155.     if block.name == wood then
  156.       return true, true -- there is a log, place sapling after op.
  157.     end
  158.  
  159.     if block.name ~= sapling then
  160.       return true, true -- there isn't a log, but dig and replace what is there.
  161.     end
  162.     return false, false -- there is no log, but likely sapling, do not place sapling.
  163.   end
  164.  
  165.   return false, true -- there is no block at all, place sapling.
  166. end
  167.  
  168. ----------------------------------------------------------
  169. local function main()
  170.   while true do
  171.     print("==================================")
  172.     assert(isHome(), "Please return me to homebase.")
  173.     print("At home. Run.")
  174.  
  175.     local doDig, doSapling = checkLog()
  176.     if doDig then
  177.       print("Digging.")
  178.       turtle.dig()
  179.       turtle.forward()
  180.       digTree()
  181.     end
  182.     moveSaplings()
  183.     if doSapling then
  184.       print("Placing a sapling.")
  185.       turtle.select(1)
  186.       assert(turtle.place())
  187.     end
  188.  
  189.     print("Do Refuel.")
  190.     refuel()
  191.     moveSaplings()
  192.  
  193.     print("Empty inventory")
  194.     emptyInv()
  195.  
  196.     print("Waiting " .. tostring(sleepTime) .. " seconds.")
  197.     os.sleep(sleepTime)
  198.   end
  199. end
  200.  
  201. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement