Advertisement
Guest User

stripmine

a guest
Jun 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.07 KB | None | 0 0
  1. local dim = { ... }
  2. if #dim ~= 3 then
  3.   print("Usage: stripmine <width> <height> <depth>")
  4.   return
  5. end
  6.  
  7. width = tonumber(dim[1])
  8. height = tonumber(dim[2])
  9. depth = tonumber(dim[3])
  10.  
  11. if width < 1 or height < 1 or depth < 1 then
  12.   print("Please ensure all parameters are positive")
  13.   return
  14. end
  15.  
  16. if height * depth > 750 then
  17.   print("Please select smaller bounds")
  18.   return
  19. end
  20.  
  21. function moveTo(x, y, z)
  22.   while loc[3] < z do
  23.     if direct ~= 1 then
  24.       redirect(1) end
  25.     if turtle.forward() then
  26.       loc[3] = loc[3] + 1
  27.     else turtle.dig() end    
  28.   end
  29.   while loc[3] > z do
  30.     if direct ~= 3 then
  31.       redirect(3) end
  32.     if turtle.forward() then
  33.       loc[3] = loc[3] - 1
  34.     else turtle.dig() end
  35.   end
  36.   while loc[1] < x do
  37.     if direct ~= 2 then
  38.       redirect(2) end
  39.     if turtle.forward() then
  40.       loc[1] = loc[1] + 1
  41.     else turtle.dig() end
  42.   end
  43.   while loc[1] > x do
  44.     if direct ~= 4 then
  45.       redirect(4) end
  46.     if turtle.forward() then
  47.       loc[1] = loc[1] - 1
  48.     else turtle.dig() end
  49.   end
  50.   while loc[2] < y do
  51.     success = turtle.up()
  52.     if success then
  53.       loc[2] = loc[2] + 1
  54.     else turtle.digUp() end
  55.   end
  56.   while loc[2] > y do
  57.     if turtle.down() then
  58.       loc[2] = loc[2] - 1
  59.     else turtle.digDown() end
  60.   end
  61. end
  62.  
  63. function redirect(newD)
  64.   if newD > 4 then newD = newD - 4 end
  65.   while direct ~= newD do
  66.     if newD - direct == 1 or (direct == 4 and newD == 1) then
  67.       turtle.turnRight()
  68.       direct = direct + 1
  69.     else
  70.       turtle.turnLeft()
  71.       direct = direct - 1
  72.     end
  73.     if direct > 4 then
  74.       direct = direct - 4 end
  75.     if direct == 0 then
  76.       direct = 4 end
  77.   end
  78. end
  79.  
  80. function isFull()
  81.   for i = 1, 16 do
  82.     if turtle.getItemCount(i) == 0 then
  83.     return false end
  84.   end
  85.   return true
  86. end
  87.  
  88. function dump()
  89.   local current = {}
  90.   current[1] = loc[1]
  91.   current[2] = loc[2]
  92.   current[3] = loc[3]
  93.   moveTo(1, 1, 1)
  94.   redirect(3)
  95.   foundFuel = false
  96.   for i = 1,16 do
  97.     turtle.select(i)
  98.     if not turtle.refuel(0) then
  99.       turtle.drop()
  100.     else
  101.       if foundFuel then
  102.         turtle.drop()
  103.       else foundFuel = true end
  104.     end
  105.     if i == 16 and isFull() then
  106.       i = 1 end
  107.   end
  108.   moveTo(current[1], current[2], current[3])
  109. end
  110.  
  111. function digStrip(forward)
  112.   if forward then
  113.     while loc[3] <= depth do
  114.       if loc[3] % 10 == 0 then
  115.       print(string.format("Current depth: %d", loc[3])) end
  116.      
  117.       moveTo(loc[1], loc[2], loc[3] + 1)
  118.       if isFull() then dump() end
  119.       if loc[2] == 1 then
  120.         moveTo(loc[1], height, loc[3])
  121.       else
  122.         moveTo(loc[1], 1, loc[3])
  123.       end
  124.     end
  125.   else
  126.     while loc[3] > 1 do
  127.       if loc[3] % 10 == 0 then
  128.       print(string.format("Current depth: %d", loc[3])) end
  129.      
  130.       moveTo(loc[1], loc[2], loc[3] - 1)
  131.       if isFull() then dump() end
  132.       if loc[2] == 1 then
  133.         moveTo(loc[1], height, loc[3])
  134.       else
  135.         moveTo(loc[1], 1, loc[3])
  136.       end
  137.     end
  138.   end
  139. end
  140.  
  141. --Main:
  142. loc = {}
  143. loc[1] = 1
  144. loc[2] = 1
  145. loc[3] = 1
  146. direct = 1
  147. if width % 3 ~= 1 then
  148.   while width % 3 ~= 1 do
  149.     width = width - 1
  150.   end
  151.   print(string.format("Width truncated to %d", width))
  152. end
  153. forward = true
  154. while true do
  155.   if turtle.getFuelLevel() < 1000 then
  156.     refueled = false
  157.     for i = 1, 16 do
  158.       turtle.select(i)
  159.       if turtle.refuel(16) then
  160.         select(1)
  161.         refueled = true
  162.         break end
  163.     end
  164.     if not refueled then
  165.       moveTo(1, 1, 1)
  166.       redirect(1)
  167.       print("No additional fuel stored")
  168.       print("Opperation cancelled")
  169.       print(string.format("Type \"stripmine %d %d %d\" to resume", width, height, depth))
  170.       return
  171.     end
  172.   else print(string.format("Current fuel: %d", turtle.getFuelLevel()))
  173.   end
  174.   digStrip(forward)
  175.   forward = not forward
  176.   if (loc[1] == width) then
  177.     moveTo(1, 1, 1)
  178.     dump()
  179.     redirect(1)
  180.     return
  181.   end
  182.   for i = 1,3 do
  183.     if loc[2] == 1 then
  184.       moveTo(loc[1] + 1, height, loc[3])
  185.     else
  186.       moveTo(loc[1] + 1, 1, loc[3])
  187.     end
  188.   end
  189. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement