Advertisement
Guest User

WE

a guest
Jul 1st, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.91 KB | None | 0 0
  1. function turn(left, mirrored)
  2.   if left ~= mirrored then
  3.     turtle.turnLeft()
  4.   else
  5.     turtle.turnRight()
  6.   end
  7. end
  8.  
  9. function digAll(depthleft)
  10.   if turtle.detect() then
  11.     turtle.dig()
  12.   end
  13.   if turtle.detectUp() and depthleft > 1 then
  14.     turtle.digUp()
  15.   end
  16.   if turtle.detectDown() and depthleft > 2 then
  17.     turtle.digDown()
  18.   end
  19. end
  20.  
  21. function nextSlotIfEmpty()
  22.   while turtle.getItemCount() == 0 do
  23.     turtle.select(turtle.getSelectedSlot() + 1)
  24.   end
  25. end
  26.  
  27. function place()
  28.   nextSlotIfEmpty()
  29.   turtle.place()
  30. end
  31. function placeUp()
  32.   nextSlotIfEmpty()
  33.   turtle.placeUp()
  34. end
  35. function placeDown()
  36.   nextSlotIfEmpty()
  37.   turtle.placeDown()
  38. end
  39.  
  40. function placeAll(height)
  41.   turtle.place()
  42.   if height > 1 then
  43.     turtle.placeDown()
  44.   end
  45.   if height > 2 then
  46.     turtle.placeUp()
  47.   end
  48. end
  49.  
  50. function move()
  51.   if turtle.detect() then
  52.     return false
  53.   end
  54.   while not turtle.forward() do
  55.     turtle.attack()
  56.   end
  57.   return true
  58. end
  59. function moveDown()
  60.   if turtle.detectDown() then
  61.     return false
  62.   end
  63.   while not turtle.down() do
  64.     turtle.attackDown()
  65.   end
  66.   return true
  67. end
  68. function moveUp()
  69.   if turtle.detectUp() then
  70.     return false
  71.   end
  72.   while not turtle.up() do
  73.     turtle.attackUp()
  74.   end
  75.   return true
  76. end
  77. function moveBack()
  78.   if not turtle.back() then
  79.     turtle.turnLeft()
  80.     turtle.turnLeft()
  81.     if turtle.detect() then
  82.       return false
  83.     end
  84.     while not turtle.forward() do
  85.       turtle.attack()
  86.     end
  87.     turtle.turnRight()
  88.     turtle.turnRight()
  89.     return true
  90.   end
  91. end
  92.  
  93. function Dmove()
  94.   if turtle.detect() then
  95.     if not turtle.dig() then
  96.       return false
  97.     end
  98.   end
  99.   while not turtle.forward() do
  100.     turtle.attack()
  101.   end
  102.   return true
  103. end
  104. function DmoveDown()
  105.   if turtle.detectDown() then
  106.     if not turtle.digDown() then
  107.       return false
  108.     end
  109.   end
  110.   while not turtle.down() do
  111.     turtle.attackDown()
  112.   end
  113.   return true
  114. end
  115. function DmoveUp()
  116.   if turtle.detectUp() then
  117.     if not turtle.digUp() then
  118.       return false
  119.     end
  120.   end
  121.   while not turtle.up() do
  122.     turtle.attackUp()
  123.   end
  124.   return true
  125. end
  126. function DmoveBack()
  127.   if not turtle.back() then
  128.     turtle.turnLeft()
  129.     turtle.turnLeft()
  130.     if turtle.detect() then
  131.       if not turtle.dig() then
  132.         return false
  133.       end
  134.     end
  135.     while not turtle.forward() do
  136.       turtle.attack()
  137.     end
  138.     turtle.turnRight()
  139.     turtle.turnRight()
  140.     return true
  141.   end
  142. end
  143.  
  144.  
  145. local cube = {
  146.   help = "cube <length> <width> <depth> [left]"
  147. }
  148.  
  149. function cube.fuel(length, width, depth)
  150.   length = tonumber(length)
  151.   width = tonumber(width)
  152.   depth = tonumber(depth)
  153.   return depth + math.ceil(depth / 3) * (length * width - 1)
  154. end
  155.  
  156. function cube.run(length, width, depth, mirrored)
  157.   length = tonumber(length)
  158.   width = tonumber(width)
  159.   depth = tonumber(depth)
  160.   if mirrored ~= true then mirrored = false end
  161.   local depthleft = depth
  162.   local direction = false
  163.  
  164.   while depthleft > 0 do
  165.     if turtle.detectDown() then
  166.       turtle.digDown()
  167.     end
  168.     moveDown()
  169.     if depthleft > 1 then
  170.       if turtle.detectDown() then
  171.         turtle.digDown()
  172.       end
  173.       moveDown()
  174.     end
  175.     if depthleft > 2 then
  176.       turtle.digDown()
  177.     end
  178.    
  179.    
  180.     local x = 1
  181.     local y = 1
  182.     while true do
  183.       if x >= length then --turn
  184.         if y >= width then --down
  185.           if turtle.detectUp() and depthleft > 1 then
  186.             turtle.digUp()
  187.           end
  188.           if turtle.detectDown() and depthleft > 2 then
  189.             turtle.digDown()
  190.           end
  191.           turtle.turnLeft()
  192.           turtle.turnLeft()
  193.           if depthleft > 2 then
  194.             turtle.down()
  195.           end
  196.           depthleft = depthleft - 3
  197.           break
  198.         end
  199.         turn(direction, mirrored)
  200.         digAll(depthleft)
  201.         move()
  202.         turn(direction, mirrored)
  203.         digAll(depthleft)
  204.         move()
  205.         y = y + 1
  206.         x = 1
  207.         direction = not direction
  208.       else
  209.         digAll(depthleft)
  210.         move()
  211.       end
  212.       x = x + 1
  213.     end
  214.   end
  215. end
  216.  
  217.  
  218. local wall = {
  219.   help = "wall <length> <width> <height> [left]"
  220. }
  221.  
  222. function wall.fuel(length, width, height)
  223.   length = tonumber(length)
  224.   width = tonumber(width)
  225.   height = tonumber(height)
  226.   return height + math.ceil(height / 3) * (2 * length + 2 * width - 1)
  227. end
  228.  
  229. function wall.run(length, width, height, mirrored)
  230.   length = tonumber(length)
  231.   width = tonumber(width)
  232.   height = tonumber(height)
  233.   if mirrored ~= true then mirrored = false end
  234.   Dmove()
  235.   turtle.turnLeft()
  236.   turtle.turnLeft()
  237.   while height > 0 do
  238.     if height > 1 then
  239.       DmoveUp()
  240.       placeDown()
  241.       if height > 2 then
  242.         placeUp()
  243.       end
  244.     end
  245.     local direction = 0
  246.     while direction < 4 do
  247.       local i = 1
  248.       while i < (direction % 2 == 0 and length or width) do
  249.         DmoveBack()
  250.         placeAll(height)
  251.         i = i + 1
  252.       end
  253.       turn(true, mirrored)
  254.       direction = direction + 1
  255.     end
  256.  
  257.   end
  258. end
  259.  
  260.  
  261. function getCommand(command)
  262.   if command == "help" then return help end
  263.   if command == "wall" then return wall end
  264.   if command == "cube" then return cube end
  265.   return nil
  266. end
  267.  
  268. local help = {
  269.   help = "help <command>"
  270. }
  271.  
  272. function help.run(command)
  273.   local result = getCommand(command)
  274.   if result == nil then
  275.     print("help: command not found")
  276.   else
  277.     print("usage: " .. result.help)
  278.   end
  279. end
  280.  
  281.  
  282. args = {...}
  283. local command = getCommand(table.remove(args, 1))
  284. if command == nil then
  285.   print("command not found")
  286.   return
  287. end
  288. if command.fuel ~= nil then
  289.   local required = command.fuel(unpack(args))
  290.   local now = turtle.getFuelLevel()
  291.   if required > now then
  292.     print("not enough fuel: " .. required .. " needed, has " .. now .. " (" .. (required - now) .. " short)")
  293.     return
  294.   end
  295. end
  296. command.run(unpack(args))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement