Advertisement
Chronno

Computercraft Wall

Dec 15th, 2015
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.63 KB | None | 0 0
  1. -- usage: "Wall <X> <Y> <Direction>"
  2. -- <X> = number of blocks over
  3. -- <Y> = number of block up
  4. -- <Direction> = direction the wall will be built
  5. --      accepts "Left" or "Right"
  6. -- The turtle will look at slot 1 to find what block it has to work with.  It will look to see if there are any successive blocks
  7. -- with the same block type.  All 16 slots can be used as long as the block type is the same.  If a different type of block is in
  8. -- slot 4, for example, only slots 1, 2, and 3 will be used.
  9.  
  10. -- See this program in action at https://youtu.be/fLfqDiI0tsY
  11.  
  12. local variables = {...}
  13.  
  14. x = tonumber(variables[1])
  15. y = tonumber(variables[2])
  16.  
  17. success = null
  18. data = {}
  19. data.name = null
  20.  
  21. block = {}
  22. block2 = {}
  23. slots = 0
  24.  
  25. locationX = 0
  26. locationY = 0
  27.  
  28. direction = "left"
  29. directionY = "up"
  30.  
  31. function printUsage()
  32.     print("Wall program usage:\nWall <X> <Y> <Direction>\n\t<X> = number of blocks over\n\t<Y> = number of blocks up\n\t<Direction> = direction the wall will be build\n\t\taccepts 'Left' or 'Right'")
  33. end
  34.  
  35. function start()
  36.     if tonumber(variables[1]) == null then
  37.         printUsage()
  38.         return false
  39.     end
  40.     if tonumber(variables[2]) == null then
  41.         printUsage()
  42.         return false
  43.     end
  44.     if variables[3] ~= null then
  45.         if string.lower(variables[3]) == "left" then
  46.             print("direction set Left")
  47.             direction = "left"
  48.         elseif string.lower(variables[3]) == "right" then
  49.             print("direction set Right")
  50.             direction = "right"
  51.         else
  52.             printUsage()
  53.             return false
  54.         end
  55.     end
  56.     turtle.select(1)
  57.     if turtle.getItemDetail() == null then
  58.         print ("place some blocks in the turtle")
  59.         return false
  60.     end
  61.     slots = 1
  62.     block = turtle.getItemDetail()
  63.     for i=2, 16 do
  64.         turtle.select(i)
  65.         if turtle.getItemDetail() == null then
  66.             break
  67.         end
  68.         block2 = turtle.getItemDetail()
  69.         if block.name == block2.name then
  70.             slots = i
  71.         else
  72.             break
  73.         end
  74.     end
  75.     turtle.select(1)
  76.     print("Turtle will build a wall " .. x .. " by " .. y .. " blocks")
  77. end
  78.  
  79. function tryForward()
  80.     while turtle.forward() ~= true do
  81.         success, data = turtle.inspect()
  82.         if data.name == "minecraft:bedrock" then
  83.             return false
  84.         elseif success then
  85.             turtle.dig()
  86.         elseif turtle.attack() then
  87.             turtle.suck()
  88.         else
  89.             sleep(0.5)
  90.         end
  91.     end
  92.     return true
  93. end
  94.  
  95. function tryDown()
  96.     while turtle.down() ~= true do
  97.         success, data = turtle.inspectDown()
  98.         if data.name == "minecraft:bedrock" then
  99.             return false
  100.         elseif success then
  101.             turtle.digDown()
  102.         elseif turtle.attackDown() then
  103.             turtle.suckDown()
  104.         else
  105.             sleep(0.5)
  106.         end
  107.     end
  108.     locationY = locationY - 1
  109.     return true
  110. end
  111.  
  112. function tryUp()
  113.     while turtle.up() ~= true do
  114.         success, data = turtle.inspectUp()
  115.         if data.name == "minecraft:bedrock" then
  116.             return false
  117.         elseif success then
  118.             turtle.digUp()
  119.         elseif turtle.attackUp() then
  120.             turtle.suckUp()
  121.         else
  122.             sleep(0.5)
  123.         end
  124.     end
  125.     locationY = locationY + 1
  126.     return true
  127. end
  128.  
  129. function slideLeft()
  130.     turtle.turnLeft()
  131.     tryForward()
  132.     turtle.turnRight()
  133.     tryPlace()
  134. end
  135.  
  136. function slideRight()
  137.     turtle.turnRight()
  138.     tryForward()
  139.     turtle.turnLeft()
  140.     tryPlace()
  141. end
  142.  
  143. function digUp()
  144.     while turtle.detectUp() == true do
  145.         turtle.digUp()
  146.         sleep(0.5)
  147.     end
  148. end
  149.  
  150. function dig()
  151.     while turtle.detect == true do
  152.         turtle.dig()
  153.         sleep(0.5)
  154.     end
  155. end
  156.  
  157. function tryPlace()
  158.     invCheck()
  159.     success, data = turtle.inspect()
  160.     if data.name ~= block.name then
  161.         while turtle.dig() == true do
  162.             sleep(0.5)
  163.         end
  164.         turtle.place()
  165.     end
  166. end
  167.  
  168. function invCheck()
  169.     if turtle.getItemDetail() ~= null then
  170.         block2 = turtle.getItemDetail()
  171.     else
  172.         block2.name = "empty"
  173.     end
  174.     if block.name ~= block2.name then
  175.         if turtle.getSelectedSlot() < slots then
  176.             turtle.select(turtle.getSelectedSlot() + 1)
  177.         else
  178.             local locationX2 = locationX
  179.             local locationY2 = locationY
  180.             locationX = locationX + 1
  181.             goHome()
  182.             print("Out of items.  Please replace and press Enter")
  183.             turtle.select(1)
  184.             if turtle.getItemDetail() ~= null then
  185.                 block2 = turtle.getItemDetail()
  186.             else
  187.                 block2.name = "blank"
  188.             end
  189.             while block2.name ~= block.name do
  190.                 block2 = turtle.getItemDetail()
  191.                 local event, key = os.pullEvent("key")
  192.                 if key == 28 then
  193.                     break
  194.                 end
  195.             end
  196.             goBack(locationX2,locationY2)
  197.             locationX = locationX - 1
  198.         end
  199.     end
  200. end
  201.  
  202. function nextRow()
  203.     if locationX < x then
  204.         if direction == "left" then
  205.             fuelCheck()
  206.             slideLeft()
  207.         elseif direction == "right" then
  208.             fuelCheck()
  209.             slideRight()
  210.         end
  211.         locationX = locationX + 1
  212.     end
  213. end
  214.  
  215. function buildRow()
  216.     if directionY=="up" then
  217.         while locationY<y-1 do
  218.             fuelCheck()
  219.             tryUp()
  220.             tryPlace()
  221.         end
  222.         directionY = "down"
  223.     elseif directionY=="down" then
  224.         while locationY>0 do
  225.             fuelCheck()
  226.             tryDown()
  227.             tryPlace()
  228.         end
  229.         directionY = "up"
  230.     end
  231. end
  232.  
  233. function goHome()
  234.     if direction == "left" then
  235.         turtle.turnRight()
  236.     elseif direction == "right" then
  237.         turtle.turnLeft()
  238.     end
  239.     for i=1,locationX-1 do
  240.         tryForward()
  241.     end
  242.     if direction == "right" then
  243.         turtle.turnRight()
  244.     elseif direction == "left" then
  245.         turtle.turnLeft()
  246.     end
  247.     for i=1,locationY do
  248.         tryDown()
  249.     end
  250. end
  251.  
  252. function goBack(X2,Y2)
  253.     if direction == "left" then
  254.         turtle.turnLeft()
  255.     elseif direction == "right" then
  256.         turtle.turnRight()
  257.     end
  258.     for i=1,X2 do
  259.         tryForward()
  260.     end
  261.     if direction == "left" then
  262.         turtle.turnRight()
  263.     elseif direction == "right" then
  264.         turtle.turnLeft()
  265.     end
  266.     for i=1,Y2 do
  267.         tryUp()
  268.     end
  269. end
  270.  
  271. function refuel()
  272.     print("Put fuel in any free slot and press any key")
  273.     local suc = false
  274.     while true do
  275.         local event, key = os.pullEvent("key")
  276.         if key ~= null then
  277.             break
  278.         end
  279.     end
  280.     local curSlot = turtle.getSelectedSlot()
  281.     for i=1,16 do
  282.         turtle.select(i)
  283.         if turtle.refuel() == true then
  284.             suc = true
  285.         end
  286.     end
  287.     turtle.select(curSlot)
  288.     if suc == true then
  289.         return true
  290.     else
  291.         return false
  292.     end
  293. end
  294.  
  295. function fuelCheck()
  296.     if turtle.getFuelLevel() <= locationX + locationY +2 then
  297.         local locationX2 = locationX
  298.         local locationY2 = locationY
  299.         locationX = locationX + 1
  300.         goHome()
  301.         while refuel() == false do
  302.         end
  303.         goBack(locationX2,locationY2)
  304.         locationX = locationX - 1
  305.     end
  306.     return true
  307. end
  308.  
  309. --Program Start
  310. if start() == false then
  311.     return
  312. end
  313. while locationX < x do
  314.     tryPlace()
  315.     buildRow()
  316.     if locationX < x-1 then
  317.         nextRow()
  318.     else
  319.         locationX = locationX + 1
  320.     end
  321. end
  322. goHome()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement