archlyn

testing

Dec 11th, 2022 (edited)
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.79 KB | Source Code | 0 0
  1. --[[===============CONFIGURATION VARIABLES=======]]--
  2. --define void trash can for mod
  3. trashcan = "cyclic:trash"
  4.  
  5. --define chest for storage
  6. chest = "minecraft:chest"
  7.  
  8. --Low fuel level
  9. lowFuel = 500
  10.  
  11. --Length of edge of square to dig (in blocks)
  12. edgeLength = 4
  13. --[[==============================================]]--
  14.  
  15. --[[===============MACHINE STATE==================]]--
  16. --count the number of times the turtle has turned right (used to help turtle face forward
  17. turnCount = 0
  18.  
  19. --count position in column
  20. columnPosition = 0
  21.  
  22. --number of columns the turtle has dug
  23. columnCount = 0
  24.  
  25. --count the the number layers down the turtle has dug
  26. depthCounter = 0
  27.  
  28. --has the turtle hit bedrock?
  29. foundBedrock = false
  30. --[[==============================================]]--
  31.  
  32. term.clear()
  33.  
  34. function printVariables()
  35.     print("Turn count: "..turnCount)
  36.     print("Column count: "..columnCount)
  37.     print("Column position: "..columnPosition)
  38.     print("Layer: "..depthCounter)
  39. end
  40.  
  41. function faceForward()
  42.     for i = 1, 4 - turnCount do
  43.         turtle.turnRight()
  44.     end
  45.     turnCount = 0
  46. end
  47.  
  48. function buildJunkTable() --add all items in turtle's inventory to junk table
  49.   junk_table = {}
  50.   local index = 1
  51.   local data = {}
  52.     for i = 1, 16 do
  53.       turtle.select(i)
  54.         if turtle.getItemCount(i) > 0 then
  55.           junk_table[index] = turtle.getItemDetail().name
  56.           index = index + 1
  57.         end
  58.     end
  59.   turtle.select(1)
  60.  
  61.   for i,v in ipairs(junk_table) do
  62.         print (v)
  63.     end
  64.    
  65. end
  66.  
  67. function confirmReadiness() --look for chest and trashcan
  68.     local chestFound = blockExsists(chest)
  69.     faceForward()
  70.     local trashcanFound = blockExsists(trashcan)
  71.     faceForward()
  72.  
  73.     if (chestFound and not trashcanFound) then
  74.       print ("Chest found but not trash can, place down trashcan from cyclic mod adjacent to the turtle!")
  75.       return false
  76.     else if (not chestFound and trashcanFound) then
  77.       print("No chest found but trashcan found! Place standard minecraft chest adjacent to turtle")
  78.       return false
  79.     else  if (not chestFound and not trashcanFound) then
  80.         print ("Place standard minecraft chest and cyclic mod trashcan adjacent to turtle")
  81.         return false
  82.     else
  83.         print("Chest AND Trashcan found! Ready!")
  84.         return true
  85.       end
  86.     end
  87.   end
  88. end
  89.  
  90. function blockExsists(blockType) --find chest for depositing inventory
  91.  
  92.     local success, data = turtle.inspect()
  93.    
  94.     for i = 1, 4 do
  95.         turnCount = i
  96.         success, data = turtle.inspect()
  97.         if (data.name == blockType and i == 0) then
  98.             print("Please do not place chest or trashcan directly in front of turtle!")
  99.             return false
  100.         elseif data.name == blockType then
  101.             return true
  102.         else
  103.             turtle.turnRight()
  104.         end
  105.     end
  106.     return false
  107. end
  108.  
  109. function deposit()
  110.     removeJunk()
  111.    
  112.     if blockExsists(chest) then
  113.         for i = 1, 16 do
  114.             turtle.select(i)
  115.             turtle.drop()
  116.         end
  117.        
  118.         turtle.select(1)
  119.     else
  120.         print("Chest missing!")
  121.     end
  122.     faceForward()
  123. end
  124.  
  125. function removeJunk()
  126.  
  127.     if blockExsists(trashcan) then
  128.     for i = 1, 16 do
  129.     turtle.select(i)
  130.         for index, value in pairs(junk_table) do                                --This whole loop
  131.             if turtle.getItemCount(i) > 0 then                                  --itterates through
  132.                 if value == turtle.getItemDetail(i).name then                   --the values in the junk table
  133.                     turtle.drop()                                               --compares them to the item names
  134.                 end                                                             --in the turtle's inventory and
  135.             else                                                                --deletes them if they match
  136.                 break
  137.             end                                                              
  138.         end                                                                  
  139.     end                                                                    
  140.     turtle.select(1)
  141.     else
  142.         print("Trashcan missing!")
  143.     end
  144.     faceForward()
  145. end
  146.  
  147. function digColumn()
  148.     print("Column position BEFORE digging: "..columnPosition)
  149.     for i = columnPosition, edgeLength - 1 do
  150.         columnPostion = i
  151.         if bedrockCheck() then
  152.             print ("Bedrock reached! Returning home!")
  153.             goHome()
  154.             return
  155.         end
  156.        
  157.         if not turtle.forward() then
  158.             turtle.dig()
  159.             turtle.forward()
  160.         end
  161.     end
  162.     --columnCount = columnCount + 1
  163.     print("Column position AFTER digging: "..columnPosition)
  164. end
  165.  
  166. function orient()
  167.     if columnCount % 2 == 0 then
  168.         turnCount = turnCount + 1
  169.         turtle.turnRight()
  170.         if turtle.detect() then
  171.             turtle.dig()
  172.             turtle.forward()
  173.             turnCount = turnCount + 1
  174.             turtle.turnRight()
  175.         else
  176.             turtle.forward()
  177.             turnCount = turnCount + 1
  178.             turtle.turnRight()
  179.         end
  180.     else
  181.         turnCount = turnCount - 1
  182.         turtle.turnLeft()
  183.         if turtle.detect() then
  184.             turtle.dig()
  185.             turtle.forward()
  186.             turnCount = turnCount - 1
  187.             turtle.turnLeft()
  188.         else
  189.             turtle.forward()
  190.             turnCount = turnCount - 1
  191.             turtle.turnLeft()
  192.         end
  193.     end
  194.     columnCount = columnCount + 1
  195. end
  196.  
  197. function isInvetoryFull()
  198.  
  199.   for i = 1,16 do
  200.     if turtle.getItemCount(i) == 0 then
  201.         return false
  202.         end
  203.     end
  204.   return true
  205.  
  206. end
  207.  
  208. function digLayer()
  209.     print("Column count BEFORE digging: "..columnCount)
  210.     for i = columnCount, (edgeLength - 2) do
  211.        
  212.         digColumn()
  213.         orient()   
  214.         print("Column count AFTER digging: "..columnCount)
  215.     end
  216.     digColumn()
  217.     --columnCount = columnCount + 1
  218.     print("Column count AFTER digging: "..columnCount)
  219.    
  220.     --print ("entering last column orientation")
  221.     --printVariables()
  222.    
  223.     if columnCount % 2 == 0 then
  224.         turtle.turnRight()
  225.         turnCount = turnCount + 1
  226.     else
  227.         turtle.turnLeft()
  228.         turtle.turnLeft()
  229.         turnCount = turnCount - 2
  230.         for i = 1, edgeLength do
  231.             turtle.forward()
  232.         end
  233.         turtle.forward()
  234.         turtle.turnRight() 
  235.     end
  236.     print ("entering movement phase")
  237.     --columnCount = columnCount + 1
  238.     for i = 1, edgeLength do
  239.         turtle.forward()
  240.     end
  241.    
  242.     turtle.turnRight()
  243.     turnCount = 0
  244.     columnCount = 0
  245.     columnPostion = 0
  246.     --print ("Depth count:" .. depthCounter)
  247.     --
  248. end
  249.  
  250. function goHome()
  251.  
  252.     local oldTurnCount = turnCount
  253.    
  254.     for i = 1,depthCounter do
  255.         turtle.up()
  256.     end
  257.    
  258.     faceForward()
  259.     turtle.turnLeft()
  260.     turnCount = turnCount + 3
  261.    
  262.     for i=1,columnCount do
  263.         turle.forward()
  264.     end
  265.    
  266.     turtle.turnLeft()
  267.     turnCount = turnCount - 1
  268.    
  269.     for i=1,columnPostion do
  270.         turtle.forward()
  271.     end
  272.     faceForward()
  273.     turnCount = oldTurnCount
  274. end
  275.    
  276. function goBack()
  277.     for i = 1, depthCounter do -- return to original layer
  278.         turtle.down()
  279.     end
  280.    
  281.     --turtle.turnRight()
  282.     --turnCount = turnCount + 1
  283.    
  284.     if columnCount % 2 ~= 0 then      --return to original position if turtle was on an
  285.         for i = 1, columnPosition - 1 do --odd column when it had to return home
  286.             turtle.forward()
  287.         end
  288.         turtle.turnRight()
  289.         turnCount = turnCount + 1
  290.         for i = 1, columnPosition do
  291.             turtle.forward()
  292.         end
  293.        
  294.         turtle.turnRight()
  295.         turnCount = turnCount + 1
  296.     else                                --return to original position if turtle was on an
  297.         turtle.turnRight()              --even column when it had to return home
  298.         turnCount = turnCount + 1
  299.        
  300.         for i = 1, columnCount - 1 do
  301.             turtle.forward()
  302.         end
  303.         turtle.turnLeft()
  304.         turnCount = turnCount - 1
  305.        
  306.     end
  307. end
  308.  
  309. function bedrockCheck()
  310.     local success, front = turtle.inspect()
  311.     local success, below = turtle.inspectDown()
  312.    
  313.     if (success and front.name == "minecraft:bedrock") then
  314.         foundBedrock = true
  315.         return true
  316.     elseif (success and below.name == "minecraft:bedrock") then
  317.         foundBedrock = true
  318.         return true
  319.     else
  320.         return false
  321.     end
  322. end
  323.  
  324.  
  325. --[[print("Current fuel level: "..turtle.getFuelLevel().."/"..turtle.getFuelLimit())
  326. if turtle.getFuelLevel() <= lowFuel then
  327.   print("Please add more fuel")
  328. end]]--
  329.  
  330.  
  331. if confirmReadiness() then
  332.     buildJunkTable()
  333.     deposit()
  334.     faceForward()
  335. else
  336.     return
  337. end
  338. --print("Variables BEFORE digging:")
  339. --printVariables()
  340. digLayer()
  341. turtle.digDown()
  342. depthCounter = depthCounter + 1
  343. turtle.down()
  344. digLayer()
  345. goHome()
  346. goBack()
  347.  
  348.  
  349. --print("Variables AFTER digging:")
  350. --printVariables()
  351.  
  352. --digLayer()
  353. --[[
  354. repeat
  355.     digLayer()
  356.     if not turtle.down() then
  357.         turtle.digDown()
  358.         turtle.down()
  359.     end
  360.     depthCounter = depthCounter + 1
  361. until isInvetoryFull() == true or depthCounter < 4
  362.  
  363.  
  364. goHome()
  365. deposit()
  366. goBack()
  367. --]]
Advertisement
Add Comment
Please, Sign In to add comment