Advertisement
NickM13

[ComputerCraft] Refloor

Jun 9th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.12 KB | None | 0 0
  1. --Programmed by Nick Mead(NickM13)
  2.  
  3. --Refloor, arguments when calling program should be "Flooring <Forward> <Right>"
  4.  
  5. args = {...}
  6. m_pos = {}
  7. m_dim = {}
  8. m_floorMat = ""
  9. m_dirSwitch = false
  10. m_blocksMined = 0
  11. m_fuelUsed = 0
  12.  
  13. m_pos.x = 0
  14. m_pos.z = 0
  15. m_rot = 0
  16.  
  17. local function clear()
  18.     term.clear()
  19.     term.setCursorPos(1, 1)
  20. end
  21.  
  22. local function showStats()
  23.     clear()
  24.     print("Excavation statistics screen")
  25.     print("*Hold CTRL+T to cancel excavation*\n")
  26.     print("Floor material: "..m_floorMat)
  27.     print("Blocks Mined: "..m_blocksMined)
  28.     print("Distance Moved: "..m_fuelUsed)
  29.     print("Fuel Level: "..turtle.getFuelLevel().."\n")
  30.     print("Position")
  31.     print("X: "..m_pos.x.."/"..m_dim.x)
  32.     print("Z: "..m_pos.z.."/"..m_dim.z)
  33.     print("")
  34.     io.write("Rotation: "..m_rot.." (")
  35.     if m_rot == 0 then
  36.         io.write("Forward")
  37.     elseif m_rot == 1 then
  38.         io.write("Right")
  39.     elseif m_rot == 2 then
  40.         io.write("Backward")
  41.     elseif m_rot == 3 then
  42.         io.write("Left")
  43.     end
  44.     print(")")
  45. end
  46.  
  47. local function getMaterial()
  48.     for i = 1, 16 do
  49.         if turtle.getItemCount(i) > 0 and turtle.getItemDetail(i).name == m_floorMat then
  50.             turtle.select(i)
  51.             return true
  52.         end
  53.     end
  54.     print("Out of <"..m_floorMat..">!  Waiting for more material.")
  55.     while true do
  56.         for i = 1, 16 do
  57.             if turtle.getItemCount(i) > 0 and turtle.getItemDetail(i).name == m_floorMat then
  58.                 turtle.select(i)
  59.                 return true
  60.             end
  61.         end
  62.         sleep(1)
  63.     end
  64. end
  65.  
  66. local function dig(direction)
  67.     dug = false
  68.     if direction == "forward" then
  69.         if turtle.dig() then
  70.             m_blocksMined = m_blocksMined + 1
  71.             dug = true
  72.         end
  73.     elseif direction == "up" then
  74.         if turtle.digUp() then
  75.             m_blocksMined = m_blocksMined + 1
  76.             dug = true
  77.         end
  78.     elseif direction == "down" then
  79.         if turtle.digDown() then
  80.             m_blocksMined = m_blocksMined + 1
  81.             dug = true
  82.         end
  83.     end
  84.     showStats()
  85.     return dug
  86. end
  87. local function move(direction, distance)
  88.     m_fuelUsed = m_fuelUsed + distance
  89.    
  90.     warned = false
  91.     if direction == "forward" then
  92.         for i = 1, distance do
  93.             while not turtle.forward() do
  94.                 dig("forward")
  95.                 sleep(1)
  96.                 if not warned then
  97.                     print("Forward movement blocked!")
  98.                     warned = true
  99.                 end
  100.             end
  101.             dig("down")
  102.             getMaterial()
  103.             turtle.placeDown()
  104.             if m_rot == 0 then
  105.                 m_pos.x = m_pos.x + 1
  106.             elseif m_rot == 1 then
  107.                 m_pos.z = m_pos.z + 1
  108.             elseif m_rot == 2 then
  109.                 m_pos.x = m_pos.x - 1
  110.             elseif m_rot == 3 then
  111.                 m_pos.z = m_pos.z - 1
  112.             end
  113.             showStats()
  114.         end
  115.     elseif direction == "back" then
  116.         for i = 1, distance do
  117.             while not turtle.back() do
  118.                 sleep(1)
  119.                 if not warned then
  120.                     print("Backward movement blocked!")
  121.                     warned = true
  122.                 end
  123.             end
  124.             if m_rot == 0 then
  125.                 m_pos.x = m_pos.x - 1
  126.             elseif m_rot == 1 then
  127.                 m_pos.z = m_pos.z - 1
  128.             elseif m_rot == 2 then
  129.                 m_pos.x = m_pos.x + 1
  130.             elseif m_rot == 3 then
  131.                 m_pos.z = m_pos.z + 1
  132.             end
  133.             showStats()
  134.         end
  135.     elseif direction == "up" then
  136.         for i = 1, distance do
  137.             while dig("up") do sleep(0.1) end
  138.             while not turtle.up() do
  139.                 dig("up")
  140.                 sleep(1)
  141.                 if not warned then
  142.                     print("Up movement blocked!")
  143.                     warned = true
  144.                 end
  145.             end
  146.             m_pos.y = m_pos.y - 1
  147.             showStats()
  148.         end
  149.     elseif direction == "down" then
  150.         for i = 1, distance do
  151.             dig("down")
  152.             while not turtle.down() do
  153.                 dig("down")
  154.                 sleep(1)
  155.                 if not warned then
  156.                     print("Down movement blocked!")
  157.                     warned = true
  158.                 end
  159.             end
  160.             m_pos.y = m_pos.y + 1
  161.             showStats()
  162.         end
  163.     end
  164. end
  165. local function turn(direction)
  166.     if direction == "right" then
  167.         turtle.turnRight()
  168.         m_rot = m_rot + 1
  169.     elseif direction == "left" then
  170.         turtle.turnLeft()
  171.         m_rot = m_rot - 1
  172.     elseif direction == "around" then
  173.         turtle.turnRight()
  174.         turtle.turnRight()
  175.         m_rot = m_rot + 2
  176.     end
  177.     while m_rot > 3 do
  178.         m_rot = m_rot - 4
  179.     end
  180.     while m_rot < 0 do
  181.         m_rot = m_rot + 4
  182.     end
  183.     showStats()
  184. end
  185.  
  186. if #args >= 2 then
  187.     m_dim.x = tonumber(args[1])
  188.     m_dim.z = tonumber(args[2])
  189.    
  190.     turtle.select(1)
  191.     print("Place desired floor material in first slot and press any key.")
  192.     sleep(0.5)
  193.     os.pullEvent("key")
  194.     m_floorMat = turtle.getItemDetail(1).name
  195.     print("Material: "..m_floorMat)
  196.     print("Checking fuel...")
  197.     if turtle.getFuelLevel() < m_dim.x * m_dim.z then
  198.         print("Not enough fuel.  Insert coal.")
  199.         while turtle.getFuelLevel() < m_dim.x * m_dim.z do
  200.             for i = 1, 16 do
  201.                 if turtle.getItemCount(i) > 0 and turtle.getItemDetail(i).name == "minecraft:coal" then
  202.                     turtle.select(i)
  203.                     turtle.refuel(1)
  204.                     break
  205.                 end
  206.             end
  207.             sleep(0.25)
  208.         end
  209.         turtle.select(1)
  210.     else
  211.         print("Enough fuel.  Beginning reflooring process.")
  212.     end
  213.    
  214.     dig("down")
  215.     getMaterial()
  216.     turtle.placeDown()
  217.     for r = 1, m_dim.x do
  218.         move("forward", m_dim.z - 1)
  219.         if r < m_dim.x then
  220.             if m_dirSwitch == true then turn("left")
  221.             else turn("right") end
  222.             move("forward", 1)
  223.             if m_dirSwitch == true then turn("left")
  224.             else turn("right") end
  225.         else
  226.             if m_dirSwitch == true then turn("right")
  227.             else turn("left") end
  228.         end
  229.         m_dirSwitch = not m_dirSwitch
  230.     end
  231. else
  232.     error("Incorrect usage of program: Floor <Forward> <Right>")
  233. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement