Advertisement
LegoStax

Wall-building turtle

Aug 13th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.14 KB | None | 0 0
  1. --[[
  2.     1.Place down, place up, move back, place front
  3.     2.repeat step 1 until width is reached
  4.     3.if height is reached stop.
  5.     4.move up, place down, move up, place down, move up, turnRight twice
  6.     5.Goto step 1
  7.  
  8.     NOTE: Slots 1-15 are blocks, slot 16 is fuel
  9.     TODO: react to insufficient fuel level and block count
  10. ]]
  11. local tArgs = {...}
  12. local buildWidth, buildHeight = nil, nil
  13. local currentHeight = 2
  14. local blockType = nil
  15. if #tArgs < 2 then
  16.     print(fs.getName(shell.getRunningProgram()).." <width> <height>")
  17.     return
  18. else
  19.     buildWidth = tonumber(tArgs[1])
  20.     buildHeight = tonumber(tArgs[2])
  21. end
  22.  
  23. -- speed up calling
  24. local goUp = turtle.up
  25. local goDn = turtle.down
  26. local goLt = turtle.turnLeft
  27. local goRt = turtle.turnRight
  28. local goFd = turtle.forward
  29. local goBk = turtle.back
  30. local plUp = turtle.placeUp
  31. local pl = turtle.place
  32. local plDn = turtle.placeDown
  33. local atUp = turtle.attackUp
  34. local at = turtle.attack
  35. local atDn = turtle.attackDown
  36. local digUp = turtle.digUp
  37. local dig = turtle.dig
  38. local digDn = turtle.digDown
  39.  
  40. local function go(d,s)
  41.     s = s or 1
  42.     if d == "up" then
  43.         for i = 1,s do
  44.             if not goUp() then
  45.                 digUp()
  46.                 atUp()
  47.                 goUp()
  48.             end
  49.         end
  50.     elseif d == "dn" then
  51.         for i = 1,s do
  52.             if not goDn() then
  53.                 digDn()
  54.                 atDn()
  55.                 goDn()
  56.             end
  57.         end
  58.     elseif d == "lt" then
  59.         for i = 1,s do
  60.             goLt()
  61.         end
  62.     elseif d == "rt" then
  63.         for i = 1,s do
  64.             goRt()
  65.         end
  66.     elseif d == "fd" then
  67.         for i = 1,s do
  68.             if not goFd() then
  69.                 dig()
  70.                 at()
  71.                 goFd()
  72.             end
  73.         end
  74.     elseif d == "bk" then
  75.         for i = 1,s do
  76.             if not goBk() then
  77.                 go("rt", 2)
  78.                 dig()
  79.                 at()
  80.                 go("rt", 2)
  81.                 goBk()
  82.             end
  83.         end
  84.     end
  85. end
  86.  
  87. local function getBlockName(data)
  88.     return string.sub(data.name, 11)
  89. end
  90.  
  91. local function placeBlock(side)
  92.     for i = 1,15 do
  93.         if turtle.getItemCount(i) > 0 then
  94.             if turtle.getSelectedSlot() ~= i then
  95.                 turtle.select(i)
  96.             end
  97.             break
  98.         end
  99.     end
  100.     if side == "up" then
  101.         if not plUp() then
  102.             digUp()
  103.             atUp()
  104.             plUp()
  105.         end
  106.     elseif side == "dn" then
  107.         if not plDn() then
  108.             digDn()
  109.             atDn()
  110.             plDn()
  111.         end
  112.     else
  113.         if not pl() then
  114.             dig()
  115.             at()
  116.             pl()
  117.         end
  118.     end
  119. end
  120. -- 1 lava bucket = 1000 fuel
  121. local function init()
  122.     local canRun = true
  123.     local rows = math.floor(buildHeight/3)
  124.     local extramoves = (buildHeight*buildWidth)-(rows*3*buildWidth)
  125.     if extramoves > 0 then
  126.         extramoves = extramoves-2
  127.     end
  128.     print("excess moves: "..extramoves)
  129.     print("Rows: "..rows)
  130.     requiredFuel = ((rows*buildWidth)+rows*2)+buildHeight+extramoves
  131.     startFuel = turtle.getFuelLevel()
  132.     print("Current fuel: "..startFuel)
  133.     print("Required fuel: "..requiredFuel)
  134.     if requiredFuel > startFuel then
  135.         turtle.select(16)
  136.         while true do
  137.             term.clear()
  138.             print("Current fuel: "..turtle.getFuelLevel())
  139.             print("Required fuel: "..requiredFuel)
  140.             print("Slots 1-15: blocks")
  141.             print("Slot 16: fuel")
  142.             print("[r] = Refuel current slot")
  143.             print("[q] = Quit")
  144.             print("[f] = Force run")
  145.             local input = read()
  146.             if input == "r" or input == "R" then
  147.                 turtle.refuel(turtle.getItemCount())
  148.                 if turtle.getFuelLevel() >= requiredFuel then
  149.                     break
  150.                 end
  151.             elseif input == "q" or input == "Q" then
  152.                 canRun = false
  153.                 break
  154.             elseif input == "f" or input == "F" then break
  155.             else
  156.                 print("Invalid input")
  157.                 sleep(1)
  158.             end
  159.         end
  160.         turtle.select(1)
  161.     end
  162.  
  163.     requiredBlocks = buildHeight*buildWidth
  164.     print("Required blocks: "..requiredBlocks)
  165.     blockType = getBlockName(turtle.getItemDetail())
  166.     write("Run? [y/n] ")
  167.     while true do
  168.         local input = read()
  169.         if input == "y" or input == "Y" then
  170.             break
  171.         elseif input == "n" or input == "N" then
  172.             canRun = false
  173.             break
  174.         else
  175.             print("Invalid Input")
  176.         end
  177.     end
  178.     return canRun
  179. end
  180.  
  181. local function singleBlockPlace()
  182.     for h = currentHeight,buildHeight do
  183.         go("rt",2)
  184.         for i = 1,buildWidth-1 do
  185.             go("bk")
  186.             placeBlock()
  187.         end
  188.         if currentHeight == buildHeight then
  189.             go("rt")
  190.             go("bk")
  191.             placeBlock()
  192.             return true
  193.         else
  194.             go("up")
  195.             placeBlock("dn")
  196.             currentHeight = currentHeight+1
  197.         end
  198.     end
  199.     return false
  200. end
  201. -- buildHeight = 11, currentHeight = 9,
  202. local function mainLoop()
  203.     --term.clear()
  204.     --term.setCursorPos(1,1)
  205.     for j = 1,math.floor(buildHeight/3) do
  206.         --print("currentHeight: "..currentHeight)
  207.         for i = 1,buildWidth-1 do
  208.             placeBlock("up")
  209.             placeBlock("dn")
  210.             go("bk")
  211.             placeBlock()
  212.         end
  213.         placeBlock("dn")
  214.         go("up")
  215.         placeBlock("dn")
  216.         go("up")
  217.         placeBlock("dn")
  218.         currentHeight = currentHeight+2
  219.  
  220.         --check fuel level and block count
  221.  
  222.         if currentHeight >= buildHeight-2 then
  223.             if not singleBlockPlace() then go("rt") go("bk") end
  224.             go("dn", currentHeight-1)
  225.             break
  226.         end
  227.         go("up")
  228.         go("rt", 2)
  229.         currentHeight = currentHeight+1
  230.     end
  231.     print("currentHeight: "..currentHeight)
  232. end
  233.  
  234. if init() then mainLoop()
  235.     print("Used fuel: "..tostring(startFuel-turtle.getFuelLevel()))
  236. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement