Advertisement
RockRevenchy

Gapbuild

Apr 15th, 2021
1,051
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.10 KB | None | 0 0
  1. --Gapbuilder
  2. --Made by RockRevenchy
  3. --ver 1.0
  4.  
  5. --also with plenty of comments if you want to trace what I did here!
  6.  
  7. --=========
  8. --variables
  9. --=========
  10. curslot = 1
  11. fuelused = 0
  12. x = 0
  13. z = 0
  14. currentl= 0
  15. step = 0
  16. onlymine = 0
  17. progress = 1
  18. futurew = 0
  19. oneline = false
  20.  
  21. --=========
  22. --function
  23. --=========
  24.  
  25. --plain and simple, move 1 block ahead and check if we got fuel, attack in front of us in case someone is at the wrong place and make sure we can go forward by mining whatever's in front of us
  26. function moveforward()
  27.     while not turtle.forward() do
  28.         checkfuel()
  29.         turtle.dig()
  30.         turtle.attack()
  31.     end
  32. end
  33.  
  34. --make sure we still have fuel, if not then go to the first slot and refuel, if however this didnt succeed, wait until the user places fuel in the first slot
  35. function checkfuel()
  36.     if turtle.getFuelLevel() < 1 then
  37.         turtle.select(1)
  38.         if not turtle.refuel(1) then
  39.             print("No fuel in slot 1, awaiting fuel")
  40.             while not turtle.refuel(1) do
  41.             end
  42.             print("Successfully refueled, continuing")
  43.         end
  44.         fuelused = fuelused + 1
  45.         turtle.select(curslot)
  46.     end
  47. end
  48.  
  49. --if the turtle is not on "mine mode only" then select the next slot, if we're at the last slot, stop here and wait until the user puts more blocks inside, until then just scan the inventory and keep pestering the user every 5 seconds :D
  50. function selnextslot()
  51.     if onlymine == 0 then
  52.         curslot = curslot + 1
  53.         if curslot == 16 then
  54.             print("Out of blocks")
  55.             print("Please insert more blocks")
  56.             select(1)
  57.             curslot = 1
  58.             while turtle.getItemCount(curslot) < 0 do
  59.                 turtle.getItemCount(curslot)
  60.                 if curslot == 16 then
  61.                     turtle.select(curslot)
  62.                     turtle.getItemCount(curslot)
  63.                     curslot = 1
  64.                 else
  65.                     turtle.select(curslot)
  66.                     turtle.getItemCount(curslot)
  67.                     sleep (5)
  68.                     curslot = curslot + 1  
  69.                 end
  70.             end
  71.         end
  72.         turtle.select(curslot)
  73.     end
  74. end
  75.  
  76. --make sure we're not currently out of blocks, if so, go to the next slot
  77. function checkblockcount()
  78.     if turtle.getItemCount(curslot) == 0 then
  79.         selnextslot()
  80.     end
  81. end
  82.  
  83. --checks if we've got inventory, if we do, stop the select to the first item detected, if we dont, ask user if we should only mine then
  84. function searchinv()
  85.     turtle.select(1)
  86.     curslot = 1
  87.     for z = 1, 16 do
  88.         if curslot == 16 then
  89.             turtle.select(1)
  90.             curslot = 1
  91.             shell.run("clear")
  92.             print("This turtle is empty, do you want to only mine holes?")
  93.             print(" ")
  94.             write("Press any key to continue or ctrl+t to stop")
  95.             print(" ")
  96.             os.pullEvent("key")
  97.             print(" ")
  98.             print("OK!")
  99.             onlymine = 1
  100.             break
  101.         end
  102.         turtle.select(curslot)
  103.         if turtle.getItemCount(curslot) > 1 then
  104.             break
  105.         end
  106.         curslot = curslot + 1
  107.     end
  108. end
  109.  
  110. --checks if the user requested up or down placing and if the turtle is set to "mine mode only" then replace (or mine) whatever block it should
  111. function place()
  112.     sleep(0.5)
  113.     if up == true then
  114.         turtle.digUp()
  115.         sleep(0.5)
  116.         if onlymine == 0 then
  117.             while turtle.placeUp() == false do
  118.                     checkblockcount()
  119.             end
  120.         end
  121.     else
  122.         turtle.digDown()
  123.         sleep(0.5)
  124.         if onlymine == 0 then
  125.             while turtle.placeDown() == false do
  126.                 checkblockcount()
  127.                
  128.             end
  129.         end
  130.     end
  131. end
  132.  
  133. --go to the next line
  134. function turn()
  135.     for x = 1, step+1 do
  136.         moveforward()
  137.     end
  138.     turtle.turnLeft()
  139.     place()
  140. end
  141.  
  142. --=========
  143. --code
  144. --=========
  145.  
  146. --checks if arguments were put in the commands, if not it shows how to use the command
  147. local args = {...}
  148. if #args ~= 4 then
  149.     print("Usage: [length] [width] [gap] [u|d]")
  150.     os.pullEvent("key")
  151.     print(" ")  
  152.  
  153.     print("[length] [width]")
  154.     print(" ")
  155.     print("From bottom left corner to top right corner, including the turtle's current position.")
  156.     os.pullEvent("key")  
  157.     print(" ")
  158.  
  159.     print("[gap]")
  160.     print(" ")
  161.     print("How many empty blocks between the blocks placed down?")
  162.     os.pullEvent("key")  
  163.     print(" ")
  164.  
  165.     print("[u|d]")
  166.     print(" ")
  167.     print("(Up or Down)")
  168.     print("Should the blocks be placed below or above the turtle?")
  169.     return
  170. end  
  171.  
  172. --pulling arguments from the command thrown and keeping them as variables
  173. length = tonumber(args[1])
  174. width = tonumber(args[2])
  175. step = tonumber(args[3])
  176.  
  177. --check if the turtle should place upward
  178. if args[4] == "u" then
  179.     up = true
  180. elseif args[4] == "up" then
  181.     up = true
  182. end
  183.  
  184. --gives visual feedback to make sure the user is fine with the program's intentions
  185. shell.run("clear")
  186. print("Current length: "..length)
  187. print("Current width: "..width)
  188. print("Current distance between blocks: "..step)
  189. if up == true then
  190.     print("place blocks up (above turtle)")
  191. else
  192.     print("place blocks down (below turtle)")
  193. end
  194.  
  195. print(" ")
  196.  
  197. --take one block away to include the turtle's position from the start of the dimensions requested
  198. width = width - 1
  199. length = length - 1
  200.  
  201. --if the user requested either one line of blocks or a gap greater than the width, make it so it runs only for one width line
  202. if width < step then
  203.     width = step
  204.     futurew = step+1
  205.     oneline = true
  206. end
  207.  
  208. --if the user puts an illegal value, stop there rather than let it go awire
  209. if step > length then
  210.     print("Incorrect value, your gap is greater than your length")
  211.     error()
  212. end
  213.  
  214. --request user's agreement to actually start
  215. print("Is this ok?")
  216. write("Press any key to continue or ctrl+t to stop")
  217. os.pullEvent("key")  
  218. print(" ")
  219. print("Starting...")
  220.  
  221. sleep(1.5)
  222. --search inventory for blocks, if the function doesnt find any, the program will ask the user if the turtle should only mine then (see functions above)
  223. searchinv()
  224.  
  225. --place the first block before starting (else it would not as it should place it after actually moving)
  226. place()
  227.  
  228. --start the work sequence here
  229.  
  230. --run this sequence for as long as the width number
  231. for xx = 1, width do
  232.  
  233. --shows progress on screen (also adapts to one line requests)
  234.     if oneline == true then
  235.         print("Current width: "..progress.."/1")
  236.     else
  237.         print("Current width: "..progress.."/"..width+1)
  238.     end
  239.  
  240. --repeat this process for as many times as the length requested also localy calculate how many blocks we moved without placing a block
  241. --once we moved as many blocks as the gap requested + 1 block for the turtle's position, reset the counter and place one block
  242.     for x = 1, length do
  243.         currentl = currentl + 1
  244.         moveforward()
  245.         if currentl == step+1 then
  246.             place()
  247.             currentl = 0
  248.         end
  249.     end  
  250.  
  251. --we reached the end of the line, go back to the start of the line
  252.     turtle.turnRight()
  253.     turtle.turnRight()
  254.     for y = 1, length do
  255.         moveforward()
  256.     end
  257.     turtle.turnLeft()
  258.  
  259. --check if the next gap goes beyond the max width, if it does then stop right there rather than break out of bounds
  260.     futurew = progress + step
  261.  
  262.     --print("DEBUG: "..width.." | "..progress.." | "..futurew)
  263.  
  264.     if progress > width then
  265.         break
  266.     elseif futurew > width then
  267.         print("Stopping here to avoid going out of the requested width.")
  268.         print(" ")
  269.         break
  270.     end
  271.  
  272. --calculate where we're going before actually going there
  273.     currentl = 0
  274.     progress = progress + step
  275.     progress = progress + 1
  276.  
  277. --proceed to the next line then go back to the start of the sequence to redo everything until we're done
  278.     turn()
  279. end
  280.  
  281. --make the turtle face its magnificient work in admiration then take a coffee break until the user comes back
  282. turtle.turnLeft()
  283. print("All done!")
  284.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement