Advertisement
MaximPixel

Heavy Pillars

Jun 6th, 2020
848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.18 KB | None | 0 0
  1. -- to install/reinstall:
  2. -- pastebin run YmWZcV0e WarrX8Nh pillars.lua
  3.  
  4. placedBlocks = 0
  5.  
  6. function fillArray(x, n)
  7.     arr = {}
  8.  
  9.     for i = 1, n do
  10.         arr[i] = x
  11.     end
  12.    
  13.     return arr
  14. end
  15.  
  16. function tryToExecute(actions, autoDig)
  17.     for i = 1, #actions do
  18.         print(i)
  19.         action = actions[i]
  20.        
  21.         if autoDig then
  22.             flag, e = nil, nil
  23.            
  24.             if action == turtle.forward then
  25.                 flag, e = turtle.dig()
  26.             elseif action == turtle.back then
  27.                 flag, e = tryToExecute({turtle.turnLeft, turtle.turnLeft, turtle.dig, turtle.turnLeft, turtle.turnLeft}, false)
  28.             elseif action == turtle.up then
  29.                 flag, e = turtle.digUp()
  30.             elseif action == turtle.down then
  31.                 flag, e = turtle.digDown()
  32.             end
  33.            
  34.             if not flag and not e == "Nothing to dig here" then
  35.                 return false, e
  36.             end
  37.         end
  38.        
  39.         flag, e = action()
  40.        
  41.         ignore = action == turtle.digDown or action == turtle.dig or action == turtle.digUp
  42.        
  43.         if not ignore then
  44.             if action == turtle.place or action == turtle.placeDown or action == turtle.placeUp then
  45.                 ignore = true
  46.             end
  47.         end
  48.        
  49.         if not ignore and not flag then
  50.             return false, e
  51.         end
  52.     end
  53.     return true
  54. end
  55.  
  56. function findBlock(name)
  57.     item = turtle.getItemDetail()
  58.     if item and item.name == name then
  59.         return true
  60.     end
  61.     for i = 1, 16 do
  62.         item = turtle.getItemDetail(i)
  63.         if item and item.name == name then
  64.             turtle.select(i)
  65.             return true
  66.         end
  67.     end
  68.     return false
  69. end
  70.  
  71.  
  72. local forwardActions = fillArray(turtle.forward, 20)
  73.  
  74. local blockName = "minecraft:cobblestone"
  75.  
  76. function placeBlock()
  77.     suc, par = turtle.inspect()
  78.    
  79.     if suc then
  80.         if par.name == blockName then
  81.             return true
  82.         else
  83.             s, e = turtle.dig()
  84.             if not s and not e == "Nothing to dig here" then
  85.                 return false, e
  86.             end
  87.         end
  88.     end
  89.    
  90.     if not findBlock(blockName) then
  91.         return false, "No blocks"
  92.     end
  93.    
  94.     if turtle.place() then
  95.         placedBlocks = placedBlocks + 1
  96.         return true
  97.     else
  98.         return false
  99.     end
  100. end
  101.  
  102. function placeBlockDown()
  103.     suc, par = turtle.inspectDown()
  104.    
  105.     if suc then
  106.         if par.name == blockName then
  107.             return true
  108.         else
  109.             s, e = turtle.digDown()
  110.             if not s then
  111.                 return false, e
  112.             end
  113.         end
  114.     end
  115.    
  116.     if not findBlock(blockName) then
  117.         return false, "No blocks"
  118.     end
  119.    
  120.     if turtle.placeDown() then
  121.         placedBlocks = placedBlocks + 1
  122.         return true
  123.     else
  124.         return false
  125.     end
  126. end
  127.  
  128. local pillarBase = {
  129.     turtle.down,
  130.     turtle.forward, placeBlock, turtle.turnLeft, placeBlock, turtle.turnLeft, turtle.forward, turtle.turnLeft,
  131.     turtle.forward, placeBlock, turtle.turnLeft, placeBlock, turtle.turnLeft, turtle.forward, turtle.turnLeft,
  132.     turtle.forward, placeBlock, turtle.turnLeft, placeBlock, turtle.turnLeft, turtle.forward, turtle.turnLeft,
  133.     turtle.forward, placeBlock, turtle.turnLeft, placeBlock, turtle.turnLeft, turtle.forward, turtle.turnLeft,
  134.    
  135.     placeBlock, turtle.turnLeft, placeBlock, turtle.turnLeft, placeBlock, turtle.turnLeft, placeBlock, turtle.turnLeft,
  136.    
  137.     turtle.up, placeBlockDown,
  138.    
  139.     turtle.forward, placeBlock, turtle.turnLeft, placeBlock, turtle.turnLeft, turtle.forward, turtle.turnLeft,
  140.     turtle.forward, placeBlock, turtle.turnLeft, placeBlock, turtle.turnLeft, turtle.forward, turtle.turnLeft,
  141.     turtle.forward, placeBlock, turtle.turnLeft, placeBlock, turtle.turnLeft, turtle.forward, turtle.turnLeft,
  142.     turtle.forward, placeBlock, turtle.turnLeft, placeBlock, turtle.turnLeft, turtle.forward, turtle.turnLeft,
  143.    
  144.     placeBlock, turtle.turnLeft, placeBlock, turtle.turnLeft, placeBlock, turtle.turnLeft, placeBlock, turtle.turnLeft,
  145.    
  146.     turtle.up, placeBlockDown
  147. }
  148.  
  149. local pillarPart = {
  150.     placeBlock, turtle.turnLeft, placeBlock, turtle.turnLeft, placeBlock, turtle.turnLeft, placeBlock, turtle.turnLeft,
  151.     turtle.up, placeBlockDown
  152. }
  153.  
  154. function goToNext()
  155.     return tryToExecute(forwardActions, false)
  156. end
  157.  
  158. pillarHeight = 0
  159.  
  160. function goDownAndCount()
  161.     run = true
  162.     pillarHeight = 0
  163.    
  164.     while run do
  165.         if turtle.down() then
  166.             pillarHeight = pillarHeight + 1
  167.         else
  168.             run = false
  169.         end
  170.     end
  171.    
  172.     print("Height: " .. pillarHeight)
  173.    
  174.     return true
  175. end
  176.  
  177. tryToExecute({goToNext, goDownAndCount}, false)
  178.  
  179. function pillarBaseFunc()
  180.     print("Pillar base")
  181.     return tryToExecute(pillarBase, true)
  182. end
  183.  
  184. function pillarPartsFunc()
  185.     print("Pillar parts")
  186.    
  187.     for i = 1, pillarHeight - 2 do
  188.         s, e = tryToExecute(pillarPart, true)
  189.        
  190.         if not s then
  191.             return s, e
  192.         end
  193.     end
  194.     return true
  195. end
  196.  
  197. if pillarHeight > 0 then
  198.     s, e = tryToExecute({pillarBaseFunc, pillarPartsFunc, turtle.up})
  199.    
  200.     if not s then
  201.         print(e)
  202.     end
  203. end
  204.  
  205. print("Placed blocks " .. placedBlocks)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement