Advertisement
disasm

chop.lua

Aug 14th, 2022 (edited)
898
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.85 KB | None | 0 0
  1.  
  2. local xPos,zPos = 0,-1
  3. local xDir,zDir = 0,1
  4. local depth = 0
  5. local trees = 0  -- trees or saplings detected
  6. local choppedTrees = 0
  7.  
  8. local tArgs = { ... }
  9. if #tArgs < 2 then
  10.     print( "Usage: chop <front_size> <right_size>" )
  11.     print( "Pattern: ")
  12.     print( "###" )
  13.     print( "###" )
  14.     print( "###" )
  15.     print( "^    <-turtle, chest" )
  16.     return
  17. end
  18. zSize = tonumber( tArgs[1] ) -- front_size
  19. xSize = tonumber( tArgs[2] ) -- right_size
  20. cycleDelay = 10
  21. if #tArgs > 2 then
  22.     cycleDelay = tonumber( tArgs[3] )
  23. end
  24.  
  25.  
  26.  
  27.  
  28. local function checkFuel(needed)
  29.     local fuelLevel = turtle.getFuelLevel()
  30.     if fuelLevel == "unlimited" then
  31.         return true
  32.     end
  33.     return fuelLevel >= needed
  34. end
  35.  
  36. -- position: in front of a tree
  37. local function chopForward()
  38.     turtle.dig()
  39.     while not turtle.forward() do
  40.         turtle.attack()
  41.         sleep(1)
  42.     end
  43.  
  44.     local height = 0
  45.     while true do
  46.         local r, item = turtle.inspectUp()
  47.         if r then
  48.             if item.name == "minecraft:log" then
  49.                 turtle.digUp()
  50.                 while not turtle.up() do
  51.                     turtle.digUp()
  52.                     sleep(1)
  53.                 end
  54.                 height = height + 1
  55.             else
  56.                 break
  57.             end
  58.         else
  59.             break
  60.         end
  61.     end
  62.     while height > 0 do
  63.         while not turtle.down() do
  64.             turtle.digDown()
  65.             sleep(1)
  66.         end
  67.         height = height - 1
  68.     end
  69.  
  70.     while not turtle.digDown() do
  71.         sleep(1)
  72.     end
  73.  
  74.     choppedTrees = choppedTrees + 1
  75.  
  76.     for slot=1,16 do
  77.         local t = turtle.getItemDetail(slot)
  78.         if t then
  79.             if t.name == "minecraft:sapling" then
  80.                 turtle.select(slot)
  81.                 while not turtle.placeDown() do
  82.                     sleep(1)
  83.                 end
  84.                 turtle.select(1)
  85.                 return
  86.             end
  87.         end
  88.     end
  89. end
  90.  
  91. local function countSaplings()
  92.     local count = 0
  93.     for slot=1,16 do
  94.         local t = turtle.getItemDetail(slot)
  95.         if t then
  96.             if t.name == "minecraft:sapling" then
  97.                 count = count + t.count
  98.             end
  99.         end
  100.     end
  101.     return count
  102. end
  103.  
  104. -- position: above the chest
  105. local function unload()
  106.     local r, block = turtle.inspectDown()
  107.     if not r then
  108.         return false
  109.     end
  110.     if block.name ~= "minecraft:chest" then
  111.         return false
  112.     end
  113.  
  114.     for slot=1,16 do
  115.         local t = turtle.getItemDetail(slot)
  116.         if t then
  117.             if t.name ~= "minecraft:sapling" then
  118.                 turtle.select(slot)
  119.                 if not turtle.dropDown() then
  120.                     -- no space
  121.                     return false
  122.                 end
  123.             end
  124.         end
  125.     end
  126.  
  127.     -- optimize saplings
  128.     for slot=2,16 do
  129.         for i=1,slot-1 do
  130.             if turtle.getItemCount(slot) == 0 then
  131.                 break
  132.             end
  133.             turtle.select(slot)
  134.             turtle.transferTo(i)
  135.         end
  136.     end
  137.  
  138.     turtle.select(1)
  139.     return true
  140. end
  141.  
  142. -- position: above the chest
  143. local function unloadSaplings()
  144.     local toUnload = 0
  145.     if trees > 0 then
  146.         -- unload saplings
  147.         local saplings = countSaplings()
  148.         if saplings > trees then
  149.             toUnload = saplings - trees
  150.         end
  151.     end
  152.  
  153.     if toUnload == 0 then
  154.         return true
  155.     end
  156.  
  157.     for slot=1,16 do
  158.         local t = turtle.getItemDetail(slot)
  159.         if t and toUnload > 0 then
  160.             if t.name == "minecraft:sapling" then
  161.                 local slotUnload = t.count
  162.                 if toUnload < t.count then
  163.                     slotUnload = toUnload
  164.                 end
  165.                 turtle.select(slot)
  166.                 if not turtle.dropDown(slotUnload) then
  167.                     -- no space
  168.                     return false
  169.                 end
  170.                 toUnload = toUnload - slotUnload
  171.             end
  172.         end
  173.     end
  174.  
  175.     turtle.select(1)
  176.     return true
  177. end
  178.  
  179. local function forceForward()
  180.     while true do
  181.         if turtle.forward() then
  182.             xPos = xPos + xDir
  183.             zPos = zPos + zDir
  184.             if not turtle.inspectDown() then
  185.                 -- suck only if there is no chest below
  186.                 turtle.suckDown()
  187.             end
  188.             return
  189.         end
  190.  
  191.         local r, block = turtle.inspect()
  192.         if r then
  193.             if block.name == "minecraft:log" then
  194.                 chopForward()
  195.                 xPos = xPos + xDir
  196.                 zPos = zPos + zDir
  197.                 return
  198.             end
  199.         else
  200.             turtle.attack()
  201.             sleep(0.5)
  202.         end
  203.     end
  204. end
  205.  
  206. local function turnLeft()
  207.     turtle.turnLeft()
  208.     xDir, zDir = -zDir, xDir
  209. end
  210.  
  211. local function turnRight()
  212.     turtle.turnRight()
  213.     xDir, zDir = zDir, -xDir
  214. end
  215.  
  216. local function gotoDir(xd, zd)
  217.     while (xDir ~= xd) or (zDir ~= zd) do
  218.         turnLeft()
  219.     end
  220. end
  221.  
  222. local function goto(x, z, xd0, zd0)
  223.     if (xPos ~= x) or (zPos ~= y) then
  224.         local xd = 0
  225.         if (xPos-x) < 0 then
  226.             xd = 1
  227.         else
  228.             xd = -1
  229.         end
  230.  
  231.         local zd = 0
  232.         if (zPos-z) < 0 then
  233.             zd = 1
  234.         else
  235.             zd = -1
  236.         end
  237.  
  238.         gotoDir(xd, 0)
  239.         while xPos ~= x do
  240.             forceForward()
  241.         end
  242.  
  243.         gotoDir(0, zd)
  244.         while zPos ~= z do
  245.             forceForward()
  246.         end
  247.     end
  248.  
  249.     gotoDir(xd0, zd0)
  250. end
  251.  
  252. local function goNext()
  253.     local z = zPos + zDir
  254.     if (z>=0) and (z<zSize) then
  255.         forceForward()
  256.         return true
  257.     end
  258.     -- can't move forward, try to shift
  259.  
  260.     if (xPos+1) >= xSize then
  261.         -- can't shift
  262.         return false
  263.     end
  264.     local zd = zDir
  265.     if zd==1 then
  266.         turnRight()
  267.     else
  268.         turnLeft()
  269.     end
  270.     forceForward()
  271.     if zd==1 then
  272.         turnRight()
  273.     else
  274.         turnLeft()
  275.     end
  276.     return true
  277. end
  278.  
  279. -- position: above the chest
  280. local function chopCycle()
  281.     if not checkFuel((zSize + 1) * (xSize + 1) * 2) then
  282.         print("Not enough fuel")
  283.         return false
  284.     end
  285.  
  286.     if not unload() then
  287.         print("Unload failed")
  288.         return false
  289.     end
  290.  
  291.     -- reset tree counter
  292.     trees = 0
  293.  
  294.     while goNext() do
  295.         turtle.suckDown()
  296.  
  297.         local r, block = turtle.inspectDown()
  298.         if r then
  299.             if block.name == "minecraft:sapling" then
  300.                 trees = trees + 1
  301.             end
  302.         end
  303.     end
  304.     -- save a copy to ignore trees on a way back
  305.     local trees2 = trees
  306.  
  307.     goto(0, -1, 0, 1)
  308.  
  309.     trees = trees2
  310.     print("Trees: "..trees)
  311.  
  312.     return true
  313. end
  314.  
  315.  
  316. while true do
  317.     -- initial position: above the chest
  318.  
  319.     local saplingsBefore = countSaplings()
  320.     local fuelBefore = turtle.getFuelLevel()
  321.  
  322.     choppedTrees = 0
  323.     if not chopCycle() then
  324.         return
  325.     end
  326.     local saplingsAfter = countSaplings()
  327.     local fuelAfter = turtle.getFuelLevel()
  328.  
  329.     local deltaSaplings = saplingsAfter - saplingsBefore
  330.     local saplingsBudget = saplingsAfter - trees
  331.     local deltaFuel = 0
  332.     if fuelAfter ~= "unlimited" then
  333.         deltaFuel = fuelAfter - fuelBefore
  334.     end
  335.  
  336.     print("chopped: "..choppedTrees..", saplings: "..deltaSaplings..", fuel: "..deltaFuel)
  337.  
  338.     if not unload() then
  339.         print("Unload failed")
  340.         return
  341.     end
  342.     if not unloadSaplings() then
  343.         print("Unload failed")
  344.         return
  345.     end
  346.  
  347.     if choppedTrees == 0 or saplingsBudget >= 0 then
  348.         sleep(cycleDelay)
  349.     end
  350. end
  351.  
  352. -- turtle.inspectDown()
  353. -- true, {name="minecraft:sapling", metadata=2}
  354. -- true, {name="minecraft:log", metadata=2}
  355.  
  356. -- turtle.getItemDetail(slot)
  357. -- {count=1, name="minecraft:sapling", damage=2}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement