Advertisement
ColdIV

pathBuilder

Jun 5th, 2021 (edited)
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.14 KB | None | 0 0
  1. -- pastebin run FuQ3WvPs NJWn1GC5 pathBuilder
  2. -- config
  3. print ("Enter path width (must be odd): ")
  4. local pathWidth = tonumber(read())
  5. print ("\nEnter path length: ")
  6. local pathLength = tonumber(read())
  7. print ("\nPress Enter to start!")
  8. local start = read("")
  9. --local pathWidth = 3 -- has to be odd, but can be changed
  10. local placeTorchAt = 8
  11. --local pathLength = 32
  12.  
  13. -- DONT CHANGE (JUST YET)
  14. local ceiling = true -- unused right now
  15. local wallHeight = 2 -- not variable yet
  16. local errorLv = 0
  17. local torches = turtle.getItemCount(1)
  18. local fuel = turtle.getItemCount(2)
  19. local maxFuel = fuel
  20. local torchType = turtle.getItemDetail(1).name
  21. local fuelType = turtle.getItemDetail(2).name
  22. local buildingBlockType = turtle.getItemDetail(3).name
  23. local distance = 0
  24. local buildSlot = 3
  25.  
  26. function selectBuildingBlockSlot ()
  27.     buildingMaterial = 0
  28.     local maxMaterial = -1
  29.     local maxSlot = 3
  30.     for i = 3, 16, 1 do
  31.         local det = turtle.getItemDetail(i)
  32.         if det and det.name == buildingBlockType then
  33.             if det.count > maxMaterial then maxSlot = i end
  34.             buildingMaterial = buildingMaterial + det.count
  35.         end
  36.     end
  37.     buildSlot = maxSlot
  38.    
  39.     if buildingMaterial <= ((pathWidth * 2) + (wallHeight * 2)) then
  40.         print ("Not enough building material!\n")
  41.         errorLv = 1
  42.     end
  43.    
  44.     return buildSlot
  45. end
  46.  
  47. function placeBlock (direction)
  48.     if direction == "down" then
  49.         local _, data = turtle.inspectDown()
  50.         if data.name == buildingBlockType then return end
  51.         while turtle.detectDown() do turtle.digDown() end
  52.         checkSlot()
  53.         if not turtle.placeDown() then
  54.             turtle.select(selectBuildingBlockSlot())
  55.             turtle.placeDown()
  56.         end
  57.     elseif direction == "up" then
  58.         local _, data = turtle.inspectUp()
  59.         if data.name == buildingBlockType then return end
  60.         while turtle.detectUp() do turtle.digUp() end
  61.         checkSlot()
  62.         if not turtle.placeUp() then
  63.             turtle.select(selectBuildingBlockSlot())
  64.             turtle.placeUp()
  65.         end
  66.     else
  67.         local _, data = turtle.inspect()
  68.         if data.name == buildingBlockType then return end
  69.         while turtle.detect() do turtle.dig() end
  70.         checkSlot()
  71.         if not turtle.place() then
  72.             turtle.select(selectBuildingBlockSlot())
  73.             turtle.place()
  74.         end
  75.     end
  76. end
  77.  
  78. function checkInventory ()
  79.     print ("Checking inventory...\n")
  80.     torches = turtle.getItemCount(1)
  81.     fuel = turtle.getItemCount(2)
  82.    
  83.     selectBuildingBlockSlot()
  84.    
  85.     print ("Torches: " .. tostring(torches) .. "\nFuel: " .. tostring(fuel) .. "\nBuilding Material: " .. tostring(buildingMaterial) .. "\n")
  86.    
  87.     if pathWidth > 1 and torches <= 1 or turtle.getItemDetail(1).name ~= torchType then
  88.         print ("Not enough torches!\n")
  89.         errorLv = 1
  90.     end
  91.     if turtle.getFuelLevel() <= distance and turtle.getItemDetail(2).name ~= fuelType  then
  92.         print ("Not enough fuel!\n")
  93.         errorLv = 1
  94.     end
  95.    
  96.     print ("Error Level: " .. tostring(errorLv) .. "\n")
  97. end
  98.  
  99. function home ()
  100.     turtle.turnLeft()
  101.     turtle.turnLeft()
  102.    
  103.     for i = 1, distance, 1 do
  104.         turtle.forward()
  105.     end
  106. end
  107.  
  108. function checkSlot ()
  109.     if turtle.getItemCount(turtle.getSelectedSlot()) == 0 or turtle.getItemCount(turtle.getSelectedSlot()) > 0 and turtle.getItemDetail(turtle.getSelectedSlot()) ~= buildingBlockType then
  110.         turtle.select(selectBuildingBlockSlot())
  111.     end
  112. end
  113.  
  114. function run ()    
  115.     print ("Starting Program...\n")
  116.     while (true) do
  117.         checkInventory ()
  118.         if errorLv ~= 0 or distance >=  pathLength then
  119.             home()
  120.             return
  121.         end
  122.        
  123.         -- Refuel
  124.         if turtle.getFuelLevel() == 0 then
  125.             turtle.select(2)
  126.             turtle.refuel()
  127.         end
  128.        
  129.         -- Dig width
  130.         --print ("dig width\n")
  131.         turtle.select(selectBuildingBlockSlot())
  132.         while turtle.detectUp() do
  133.             turtle.digUp()
  134.         end
  135.        
  136.         placeBlock("down")
  137.            
  138.         -- right side
  139.         --print ("right side\n")
  140.         turtle.turnRight()
  141.         for i = 1, (pathWidth - 1) / 2, 1 do
  142.             while not turtle.forward() do
  143.                 turtle.dig()
  144.             end
  145.            
  146.             placeBlock("down")
  147.         end
  148.        
  149.         placeBlock()
  150.        
  151.         while not turtle.up() do
  152.             turtle.digUp()
  153.         end
  154.        
  155.         placeBlock("up")
  156.        
  157.         placeBlock()
  158.        
  159.         -- check if torch should be placed
  160.         if pathWidth > 1 and distance % placeTorchAt == 0 then
  161.             turtle.select(1)
  162.             turtle.placeDown()
  163.             turtle.select(selectBuildingBlockSlot())
  164.         end
  165.        
  166.         -- go back
  167.         --print ("go back\n")
  168.         turtle.turnLeft()
  169.         turtle.turnLeft()
  170.         for i = 1, (pathWidth - 1) / 2, 1 do
  171.             while not turtle.forward() do
  172.                 turtle.dig()
  173.             end
  174.            
  175.             placeBlock ("up")
  176.         end
  177.        
  178.         -- left side
  179.         --print ("left side\n")
  180.         for i = 1, (pathWidth - 1) / 2, 1 do
  181.             while not turtle.forward() do
  182.                 turtle.dig()
  183.             end
  184.            
  185.             placeBlock("up")
  186.         end
  187.        
  188.         placeBlock()
  189.        
  190.         while not turtle.down() do
  191.             turtle.digDown()
  192.         end
  193.        
  194.         placeBlock("down")
  195.        
  196.         placeBlock()
  197.        
  198.         -- go back
  199.         --print ("go back 2\n")
  200.         turtle.turnLeft()
  201.         turtle.turnLeft()
  202.         for i = 1, (pathWidth - 1) / 2, 1 do
  203.             while not turtle.forward() do
  204.                 turtle.dig()
  205.             end
  206.             if i < ((pathWidth - 1) / 2) then
  207.                 placeBlock("down")
  208.             end
  209.         end
  210.        
  211.         --print ("Turn left\n")
  212.         turtle.turnLeft()
  213.        
  214.         -- Move forward
  215.         --print ("move forward\n")
  216.         while not turtle.forward() do
  217.             turtle.dig()
  218.         end
  219.         distance = distance + 1
  220.         print ("Current distance: " .. tostring(distance) .. " / " .. tostring(pathLength) .. "\n")
  221.     end
  222. end
  223.  
  224. run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement