Advertisement
Guest User

pace2.lua

a guest
May 25th, 2021
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.70 KB | None | 0 0
  1. -- It mines trees! - V2--
  2.  
  3. -- doesn't go out of the outOfBoundsMarker
  4. -- reverses at the stop marker
  5. -- errors at a halt maker
  6.  
  7. -- ground markers
  8. local outOfBoundsMarker = "minecraft:grass_block"
  9. local outOfBoundsMarker2 = "minecraft:dirt"
  10. local pauseMarker = "atmospheric:arid_sandstone"
  11. local stopMakrer  = "minecraft:cobblestone"
  12. local haltMarker = "minecraft:glass"
  13. local inventoryMarker = "environmental:willow_planks"
  14. local inventory = "environmental:willow_chest"
  15.  
  16. --side markers
  17. local woodLog = "minecraft:birch_log"
  18. local sapling ="minecraft:birch_sapling"
  19. local ignore = "minecraft:snow"
  20. local realSnow  ="snowrealmagic:snow"
  21.  
  22. --local
  23. local fuelSlot = 2
  24.  
  25. local emptyTurtleSlots = {      3,4,5,6,7,8,9,10,11,12,13,14        }
  26. local keepFullSlots = {1,2}
  27.  
  28. function combine(slot)    
  29.                 ---Make sure slot is not already full
  30.                 turtle.select(slot)
  31.                 if turtle.getItemCount() == 64 then return end
  32.                
  33.                 if turtle.getItemCount() > 0 then
  34.                      for i=1,16 do
  35.                             turtle.select(i)
  36.                             if turtle.getItemCount() >0 then
  37.                                 if turtle.compareTo(i) then
  38.                                         turtle.transferTo(slot)
  39.                                 end    
  40.                             end
  41.                      end  
  42.                 else
  43.                     return "slot empty"
  44.                 end
  45. end
  46.  
  47. function pickupItems()
  48.             turtle.suck()
  49.             turtle.suckDown()
  50.             turtle.suckUp()
  51. end
  52.  
  53. function t_detect(f)
  54.             detectDown = detectBlockNameDown()
  55.            
  56. end
  57.  
  58. function t_detectFwd(f)
  59.             if turtle.detect() == true then
  60.                     local a,b = turtle.inspect()
  61.                     if b ~= false then
  62.                             detectFwd = b.name
  63.                     else
  64.                             detectFwd = "none"
  65.                     end
  66.             else
  67.                     detectFwd = false
  68.             end
  69.             --  f()
  70. send
  71.  
  72. function testBounds()
  73.             local isBlock = turtle.detect == true
  74.             haltIfNeccesary(detectDown)
  75.             keepInBounds(detectDown,f)
  76.             testInfFront(isBlock)
  77. end
  78.  
  79. function testInfFront(isBlock)
  80.                     local blockInfort = turtle.inspect()
  81.                     if isBlock == true then    
  82.                             if blockInfort == "minecraft:snow" then
  83.                                     turtle.turnRight2()
  84.                             elseif blockInfort == woodLog then
  85.                             elseif blockInfort == sapling then
  86.                             elseif blockInfort == ignore then
  87.                             elseif blockInfort == realSnow then turtle.dig()
  88.                             else
  89.                                     error("obstructed - there is something strange in the way!")
  90.                             end
  91.                     end
  92. end
  93.  
  94.  
  95.  
  96. function isBlockDown(block,blockDetected)
  97.         if type(blockDetected) ~= "string" then
  98.             print(string.sub(debug.traceback(),1,200))
  99.             error("invalid detection")
  100.         end
  101.  
  102.         if block == blockDetected then
  103.             return true
  104.         else
  105.             return false
  106.         end
  107. end
  108.  
  109. function detectBlockNameDown()
  110.         local a,b = turtle.inspectDown()
  111.         return b.name or "none"
  112.  
  113. end
  114.  
  115. function haltIfNeccesary(detectDown)
  116.         local halt = isBlockDown(haltMarker,detectDown)
  117.         if halt == true then
  118.                 error("Halted")
  119.         end
  120. end
  121.  
  122.  
  123. function returnFromOutOfBounds(f)
  124.     turtle.back2()
  125.     turtle.turnRight2()
  126. end
  127.  
  128. function keepInBounds(detectDown,f)
  129.     if isBlockDown(outOfBoundsMarker,detectDown) == true then
  130.         returnFromOutOfBounds(f)
  131.     end
  132.    
  133.     -- out of bounds marker 2
  134.         if isBlockDown(outOfBoundsMarker2,detectDown,f) == true then
  135.         returnFromOutOfBounds(f)
  136.     end
  137. end
  138.  
  139. do
  140.     local f = turtle.forward
  141.     function turtle.forward2()--detectDown)
  142.             f()
  143.             t_detect(f)
  144.             testBounds()
  145.             pickupItems()
  146.             return true
  147.     end
  148. end
  149.  
  150. do
  151.         local b = turtle.back
  152.         function turtle.back2()
  153.             b()
  154.             t_detect(b)
  155.             testBounds()
  156.             pickupItems()
  157.         end
  158. end
  159.  
  160. do
  161.         local l = turtle.turnLeft
  162.         function turtle.turnLeft2()
  163.                 --print("l1")
  164.                 l()
  165.                 t_detectFwd(l)
  166.                 pickupItems()
  167.                 --print("l2")
  168.         end
  169. end
  170.  
  171. do
  172.         local r=  turtle.turnRight
  173.         function turtle.turnRight2()
  174.                 --print("r1")
  175.                 r()
  176.                 t_detectFwd(r)
  177.                 pickupItems()
  178.                 --print("r2")
  179.         end
  180. end
  181.  
  182. do
  183.         local u = turtle.up
  184.         function turtle.up2()
  185.                 local try = u()
  186.                 print("going up")      
  187.                 while try == false do
  188.                             print("failed going up retying")
  189.                             turtle.digUp()
  190.                             try = u()
  191.                 end
  192.                 pickupItems()
  193.         end
  194. end
  195.  
  196. do
  197.         local d = turtle.down
  198.         function turtle.down2()
  199.                 local try = d()
  200.                 print("going down")    
  201.                 while try == false do
  202.                             print("failed going down retying")
  203.                             turtle.digDown()
  204.                             try = d()
  205.                 end
  206.             pickupItems()
  207.         end
  208. end
  209.  
  210. function sort()
  211.         for i,v in pairs(keepFullSlots) do
  212.                 combine(v)
  213.         end
  214. end
  215.  
  216. function    insertToInventory()
  217.         sort()
  218.         --local emptyTurtleSlots
  219.         for i,v in pairs(emptyTurtleSlots) do
  220.                 turtle.select(v)
  221.                 turtle.drop()
  222.         end
  223. end
  224.  
  225.  
  226. function FindInventory()
  227.         local i=1
  228.         while detectFwd ~= inventory do
  229.             turtle.turnLeft2()
  230.             i = i + 1
  231.             if i == 10 then
  232.                     error("unable to find inventory")
  233.             end
  234.         end
  235.         insertToInventory()
  236.         for i2=1, i-1 do
  237.             turtle.turnRight2()
  238.         end
  239.        
  240. end
  241.  
  242. function checkForChestBlock()
  243.         if detectDown == inventoryMarker then
  244.                     FindInventory()
  245.         end
  246.    
  247. end
  248.  
  249.  
  250. function digTree()
  251.    
  252.     local maxZ = 10
  253.    
  254.     function isBlock(block)
  255.             if isBlock ~= false then
  256.                     --local a,b = turtle.inspect()
  257.                     local blockName = detectFwd
  258.                     if blockName == block then
  259.                         return true
  260.                     else
  261.                         return false
  262.                     end
  263.             else
  264.                     return false
  265.             end
  266.     end
  267.    
  268.     function searchSlotsFor(blockId)
  269.         for slot = 1,16 do
  270.             turtle.select(slot)
  271.             --local i = turtle.
  272.             local i = turtle.getItemDetail() or "nothing"
  273.             if i ~= "nothing" then
  274.                 if i.name == blockId then
  275.                     return slot
  276.                 else
  277.                 end
  278.             end
  279.         end
  280.         return "nothing found"
  281.     end
  282.    
  283.     function findBlock(blockId)
  284.         local i = turtle.getItemDetail() or "nothing"
  285.         if i ~= "nothing" and i.name == blockId then
  286.             return true
  287.         else
  288.             slot = searchSlotsFor(blockId)
  289.             if slot ~= "nothing found" then
  290.                 return true
  291.             else
  292.                 print(tostring(blockId) .. " not found")
  293.                
  294.                 local i = 0
  295.                 local ingnoreTime = 5
  296.                 while searchSlotsFor(blockId) == "nothing found" do
  297.                         i = i + 1
  298.                         sleep(1)
  299.                         write(".")
  300.                         if type(ingnoreTime) == "number" then
  301.                             if i == ingnoreTime then
  302.                                 print("ignoring")
  303.                                 return "none"
  304.                             end
  305.                         end
  306.                        
  307.  
  308.                 end
  309.             end
  310.            
  311.         end
  312.     end
  313.    
  314.     function ifLogDigTree()
  315.         local isLog = isBlock(woodLog) == true
  316.         if isLog then  
  317.             for i=1,maxZ do
  318.                 turtle.dig()
  319.                 turtle.digUp()
  320.                 turtle.up2()
  321.             end
  322.            
  323.             for i=1,maxZ do
  324.                 turtle.down2()
  325.             end
  326.            
  327.             if findBlock(sapling) == true then
  328.                 turtle.place()
  329.             end
  330.         end
  331.        
  332.         if not isLog then
  333.  
  334.  
  335.             local isRealSnow = isBlock(realSnow) == true
  336.             if isRealSnow then
  337.                     turtle.dig()
  338.             end
  339.            
  340.             local isSnow = isBlock(ignore) == true
  341.             if isSnow then
  342.                     turtle.dig()
  343.  
  344.             --local isSapling = isBlock(sapling)
  345.             else
  346.                     turtle.place()
  347.             end
  348.            
  349.         end
  350.    
  351.     end
  352.    
  353.     ifLogDigTree()
  354.    
  355.  
  356. end
  357.  
  358.  
  359. detectDown = detectBlockNameDown()   --- updates in turtle.forward2()
  360. function paceBlock(pauseAt,stopAt,direction)
  361.    
  362.     print(detectDown)
  363.     turtle.forward2()--detectDown)
  364.     if direction == "right" then
  365.         checkForChestBlock()
  366.     end
  367.    
  368.     local pause = isBlockDown(pauseAt,detectDown)
  369.     local stop = isBlockDown(stopAt,detectDown)
  370.    
  371.     if pause == true then
  372.         --pauseTurn()
  373.         turtle.turnRight2()
  374.         digTree()
  375.         turtle.turnLeft2()
  376.         turtle.turnLeft2()
  377.         digTree()
  378.         turtle.turnRight2()
  379.         --resumeTurn()
  380.     elseif stop == true then
  381.         return "stop"
  382.     end
  383.    
  384.     --
  385. end
  386.  
  387.  
  388. function paceBlockRepeatedly(pauseAt,stopAt,direction)
  389.     while true do
  390.         topUpFuel()
  391.         if paceBlock(pauseAt,stopAt,direction) == "stop" then return "stop" end
  392.     end
  393. end
  394.  
  395. function paceRight(pauseAt,stopAt)
  396.     --turtle.turnRight()
  397.     paceBlockRepeatedly(pauseAt,stopAt,"right")
  398.     turtle.turnRight2()
  399.     turtle.turnRight2()
  400.     --turtle.turnRight()
  401.  
  402. end
  403.  
  404.  
  405. function paceLeft(pauseAt,stopAt)
  406.     -- turtle.turnLeft()
  407.     paceBlockRepeatedly(pauseAt,stopAt,"left")
  408.     turtle.turnLeft2()
  409.     turtle.turnLeft2()
  410.     -- turtle.turnLeft()
  411.     --paceRight()
  412. end
  413.  
  414. function pace(pauseAt,stopAt,direction)
  415.     if direction == "right" then
  416.         paceRight(pauseAt,stopAt)
  417.     elseif direction == "left" then
  418.         paceLeft(pauseAt,stopAt)
  419.     end
  420. end
  421.  
  422.  
  423. function topUpFuel()
  424.     local fuelBuffer = 1000
  425.  
  426.    
  427.     local fuelLevel = turtle.getFuelLevel()
  428.     if fuelLevel < fuelBuffer then
  429.         local prevSlot = turtle.getSelectedSlot()
  430.         print("attempting to refuel")
  431.        
  432.         local i = 1
  433.         while turtle.getFuelLevel() < fuelBuffer do
  434.            
  435.             turtle.select(fuelSlot)
  436.             turtle.refuel(1)
  437.             i = i+1
  438.  
  439.            
  440.         end
  441.         turtle.select(prevSlot)
  442.     else
  443.        
  444.     end
  445.  
  446. end
  447.  
  448. function turnToCorretSide()
  449.         turtle.turnRight2()
  450.         while detectFwd ~= false do
  451.                 turtle.turnLeft2()
  452.                 print(detectFwd)
  453.         end
  454.        
  455.         --turtle.turnLeft2()
  456. end
  457.  
  458.  
  459. ------ main
  460. turtle.select(1)
  461. topUpFuel()
  462. turnToCorretSide()
  463. while true do
  464.     topUpFuel()
  465.     paceLeft(pauseMarker,stopMakrer)
  466.     topUpFuel()
  467.     paceRight(pauseMarker,stopMakrer)
  468.  
  469. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement