Advertisement
MrHG

Flatten

May 10th, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.15 KB | None | 0 0
  1. -- Flatten Turtle Program
  2. os.loadAPI("t")
  3. local args = {...}
  4. local versionNumber = "1.5"
  5. local programName = "StarDustFlattener "
  6. local digWidth = 0
  7. local digLength = 0
  8. local currentLine = 0
  9. local lastLine = false
  10.  
  11. term.clear()
  12. term.setCursorPos(1,1)
  13. print(programName..versionNumber.." Coded by MrHG")
  14. print("")
  15. print("Slot 1: Fuel")
  16. print("Slot 2: Materials")
  17.  
  18. if #args == 1 then
  19.  digLength = args[1]
  20.  digWidth = args[1]
  21. elseif #args == 2 then
  22.  digLength = args[1]
  23.  digWidth = args[2]
  24. else
  25.  print ("Usage: Flatten (Length) [Width]")
  26.  return
  27. end
  28.  
  29. function digUpUntilEmpty()
  30.  local emptyBlocks = 0
  31.  local blocksFromFloor = 0
  32.  while emptyBlocks < 2 do
  33.   t.up()
  34.   blocksFromFloor = blocksFromFloor + 1
  35.   if turtle.detectUp() then
  36.    emptyBlocks = 0
  37.   else
  38.    emptyBlocks = emptyBlocks + 1
  39.   end
  40.  end
  41.  t.down(blocksFromFloor)
  42. end
  43.  
  44. function placeBelow()
  45.  local blocksBelowFloor = 0
  46.  while not turtle.detectDown() do
  47.   t.down()
  48.   blocksBelowFloor = blocksBelowFloor + 1
  49.  end
  50.  t.up()
  51.  if turtle.getItemCount(2) < blocksBelowFloor+1 then
  52.   if t.restock(2,3,true) then
  53.   -- keep going
  54.   else
  55.     t.restock(2,3,false)
  56.   end
  57.  end
  58.  t.placeRow("down",2,"up",blocksBelowFloor-1)
  59.  t.placeDown(2)
  60. end
  61.  
  62. function digLine()
  63.  local blocksDug = 0
  64.  while blocksDug < digLength-1 do
  65.   t.forward()
  66.   blocksDug = blocksDug + 1
  67.   if turtle.detectUp() then
  68.    digUpUntilEmpty()
  69.   end
  70.   if not turtle.detectDown() then
  71.    placeBelow()
  72.   end
  73.  end
  74.  if lastLine then
  75.   return
  76.  end
  77.  if currentLine % 2 == 0 then
  78.   t.right()
  79.   t.forward()
  80.   t.right()
  81.  else
  82.   t.left()
  83.   t.forward()
  84.   t.left()
  85.  end
  86.   if turtle.detectUp() then
  87.    digUpUntilEmpty()
  88.   end
  89.   if not turtle.detectDown() then
  90.    placeBelow()
  91.   end
  92. end
  93.  
  94. -- Main Program loop
  95. fuelRequired = (digLength*digWidth)*3
  96. print("Fuel required: ~"..fuelRequired)
  97. t.refuel(fuelRequired,2)
  98. for i=1,digWidth do
  99.  digLine()
  100.  if i == digWidth - 1 then
  101.   lastLine = true
  102.  end
  103.  currentLine = currentLine + 1
  104. end
  105. if digWidth % 2 == 0 then
  106.  t.strafeRight(digWidth-1)
  107. else
  108.  t.turnAround()
  109.  t.forward(digLength-1)
  110.  t.strafeRight(digWidth-1)
  111. end
  112. t.turnAround()
  113. print("")
  114. print("Done!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement