drpepper240

turtle shearer v3

Jun 29th, 2022 (edited)
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.63 KB | None | 0 0
  1. print("Shearer v3 by DrPepper\n")
  2.  
  3. -- delay in seconds between runs
  4. local delay = 180
  5.  
  6. -- max movement allowed
  7. local maxMov = 16
  8.  
  9. local function itemBelow()
  10.     local ok, item = turtle.inspectDown()
  11.     if ok then
  12.         if item["name"] == "minecraft:chest" or item["name"] == "ironchest:iron_chest" then
  13.             return "chest"
  14.         else
  15.             return "other"
  16.         end
  17.     else
  18.         return "none"
  19.     end
  20. end
  21.  
  22.  
  23. local function tryFindChest(maxDistance)
  24.     local dx = 0
  25.     for i=1,maxDistance do
  26.         if itemBelow() == "chest" then
  27.             return true
  28.         end
  29.         if turtle.forward() then
  30.             dx = dx + 1
  31.         else
  32.             break
  33.         end
  34.     end
  35.     -- no luck, return back
  36.     turtle.turnRight()
  37.     turtle.turnRight()
  38.     for i=1,dx do
  39.         while not turtle.forward() do
  40.             sleep(0.5)
  41.         end
  42.     end
  43.     return false
  44. end
  45.  
  46.  
  47. local function tryFindChest2(maxDistance)
  48.     local dx = 0
  49.     local fd = true
  50.     while dx <= maxDistance do
  51.         local below  = itemBelow()
  52.         if below == "chest" then
  53.             return true
  54.         end
  55.        
  56.         local movRes = false
  57.         if fd then
  58.             movRes = turtle.forward()
  59.         else
  60.             movRes = turtle.back()
  61.         end
  62.        
  63.         if movRes == true then
  64.             dx = dx + 1
  65.         elseif fd == true then
  66.             fd = false
  67.             dx = 0
  68.         else
  69.             return false
  70.         end
  71.        
  72.         if dx == maxDistance and fd == true then
  73.             dx = 0
  74.             fd = false
  75.         end
  76.        
  77.     end
  78. end
  79.  
  80.  
  81. local function unload()
  82.     local total = 0
  83.     for i=1,16 do
  84.         if turtle.getItemCount(i) > 0 then
  85.             total = total + turtle.getItemCount(i)
  86.             turtle.select(i)
  87.             if not turtle.dropDown() then
  88.                 print("No space in the chest")
  89.                 --fs.delete("startup")
  90.                 return false
  91.             end
  92.             sleep(0.5)
  93.         end
  94.     end
  95.     print("Time: "..os.time().." items: "..total.." fuel: "..turtle.getFuelLevel())
  96.     return true
  97. end
  98.  
  99. -- enable autorun
  100. local s = "shell.run(\"" ..arg[0].. "\")"
  101. local f = fs.open("/startup", "w")
  102. f.write(s)
  103. f.close()
  104.  
  105. -- find chest
  106. if itemBelow() ~= "chest" then
  107.     print("Chest not found")
  108.     if not tryFindChest2(maxMov) then
  109.         return
  110.     end
  111. end
  112.  
  113. if not unload() then
  114.     return
  115. end
  116.  
  117. while true do
  118.     -- check fuel
  119.     if turtle.getFuelLevel() < 100 then
  120.         print("Low fuel")
  121.         return
  122.     end
  123.  
  124.     -- sanity check
  125.     if itemBelow() ~= "chest" then
  126.         print("Chest not found")
  127.         return
  128.     end
  129.  
  130.     -- harvest
  131.     local dx = 0
  132.     while true do
  133.         if turtle.forward() then
  134.             dx = dx + 1
  135.         else
  136.             break
  137.         end
  138.        
  139.         local item = itemBelow()
  140.         if item == "none" then
  141.             repeat
  142.                 local sheared = turtle.attackDown()
  143.             until not sheared
  144.         elseif item ~= "none" then
  145.             break
  146.         end
  147.     end
  148.  
  149.     -- return back
  150.     for i=1,dx do
  151.         while not turtle.back() do
  152.             sleep(0.5)
  153.         end
  154.     end
  155.    
  156.     -- unload
  157.     if not unload() then
  158.         return
  159.     end
  160.  
  161.     sleep(delay)
  162. end
Add Comment
Please, Sign In to add comment