Advertisement
theguywhojoojes

Room creation with auto refueling(BETA)

Apr 1st, 2020
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.75 KB | None | 0 0
  1. --new automatic roomer code
  2.  
  3. --Clear screen and math functions for convenience
  4. function cls()
  5.  term.clear()
  6.  term.setCursorPos(1,1)
  7. end
  8. function floor(num)
  9.  return math.floor(num)
  10. end
  11. function ceil(num)
  12.  return math.ceil(num)
  13. end
  14. function even(num)
  15.  if num%2==0 then return true else return false end
  16. end
  17.  
  18.  
  19. --checks if there are enough coals to refuel the desired amount
  20. function can_refuel(items_needed)
  21.  local actual_items = 0
  22.  for i=1,16 do
  23.   turtle.select(i)
  24.   if turtle.refuel(0) then
  25.    actual_items=actual_items+turtle.getItemCount(i)
  26.   end
  27.  end
  28.  if actual_items>= items_needed then
  29.    return true
  30.   else
  31.    return false
  32.  end
  33. end
  34.  
  35. --actual refueling code so it doesn't get confusing
  36. function smart_refuel_sub(items_needed)
  37.  for i=1,16 do
  38.   turtle.select(i)
  39.   if items_needed<=64 then
  40.    if turtle.getItemCount(i)>= items_needed then
  41.     turtle.refuel(items_needed)
  42.     return
  43.    else
  44.    items_needed=items_needed-turtle.getItemCount(i)
  45.    turtle.refuel(items_needed)
  46.    end
  47.   else
  48.    if (items_needed/64)>0 then
  49.     items_needed=items_needed-turtle.getItemCount(i)
  50.     turtle.refuel(64)
  51.    else
  52.     turtle.refuel(items_needed)
  53.     return
  54.    end
  55.   end
  56.  end
  57. end
  58.  
  59. --actual refueling
  60. function smart_refuel(moves_needed,items_needed)
  61.  if can_refuel(items_needed) then
  62.   smart_refuel_sub(items_needed)
  63.    --checks if the amount refueled is smaller than the amount desired
  64.  if turtle.getFuelLevel()>=moves_needed then
  65.   print("Fueling complete!")
  66.  else
  67.   print("An error has ocurred, don't remove the coals before fueling.")
  68.  end
  69.  else
  70.   print("You don't have enough units of coal, try again.")
  71.  end
  72. end
  73.  
  74. --digs and waits for any falling blocks(sand or gravel)
  75. function wait_dig(dir)
  76.  local wait_time = 1
  77.  if dir=="forward" then
  78.   while turtle.detect() do
  79.    turtle.dig()
  80.    os.sleep(wait_time)
  81.   end
  82.  elseif dir=="down" then
  83.   while turtle.detectDown() do
  84.    turtle.digDown()
  85.    os.sleep(wait_time)
  86.   end
  87.  elseif dir=="up" then
  88.   while turtle.detectUp() do
  89.    turtle.digUp()
  90.    os.sleep(wait_time)
  91.   end
  92.  end
  93.  end
  94.  
  95.  function switch_direction()
  96.   turtle.turnRight()
  97.   turtle.turnRight()
  98.  end
  99.  
  100.  function turn(turn_dir)
  101.   if turn_dir=="right" then
  102.    turtle.turnRight()
  103.    wait_dig("forward")
  104.    turtle.forward()
  105.    turtle.turnRight()
  106.   else
  107.    turtle.turnLeft()
  108.    wait_dig("forward")
  109.    turtle.forward()
  110.    turtle.turnLeft()
  111.   end
  112.  end
  113.  
  114.  function reset(x,y,z)
  115.   if not even(y) then
  116.    for i=1,y-1 do
  117.     turtle.down()
  118.    end
  119.    switch_direction()
  120.    for i=1,x-1 do
  121.     turtle.forward()
  122.    end
  123.    turtle.turnRight()
  124.    for i=0,z-1 do
  125.     turtle.forward()
  126.    end
  127.   else
  128.    turtle.turnRight()
  129.    for i=0,z-1 do
  130.     turtle.forward()
  131.    end
  132.   end
  133.  end
  134.  
  135. function room(x,y,z)
  136.  print("Ok, the turtle will start mining now")
  137.  --logs in the 'directions' that the turtle will follow
  138.  local vertical_dir = "up"
  139.  local turn_dir = 0
  140.  local should_switch = false
  141.  if even(y) then
  142.   turn_dir="left"
  143.  else
  144.   turn_dir="right"
  145.   should_switch=true
  146.  end
  147.  --get into position
  148.  wait_dig("forward")
  149.  turtle.forward()
  150.  turtle.turnLeft()
  151.   for depth=1,z do
  152.    for rows=1,y do
  153.     for collumns=1,x do
  154.      if collumns~=x then
  155.       wait_dig("forward")
  156.       turtle.forward()
  157.      elseif rows~=y then
  158.       if vertical_dir=="up" then
  159.        wait_dig("up")
  160.        turtle.up()
  161.       else
  162.        wait_dig("down")
  163.        turtle.down()
  164.       end
  165.       switch_direction()
  166.      elseif depth~=z then
  167.       if turn_dir=="right" then
  168.        turn(turn_dir)
  169.        if should_switch then
  170.         turn_dir="left"
  171.        end
  172.       else
  173.        turn(turn_dir)
  174.        if should_switch then
  175.         turn_dir="right"
  176.        end
  177.       end
  178.       if vertical_dir=="up" and rows==y  then
  179.        vertical_dir="down"
  180.       else
  181.        vertical_dir="up"
  182.       end
  183.      end
  184.     end
  185.    end
  186.   end
  187.  reset(x,y,z)
  188.  print("Mining finished! Returning to start...")
  189. end
  190.  
  191. --program start and user input
  192. cls()
  193. print("Enter the following measures:")
  194. term.write("X:")
  195. local x = tonumber(io.read())
  196. term.write("Y:")
  197. local y = tonumber(io.read())
  198. term.write("Z:")
  199. local z = tonumber(io.read())
  200.  
  201. --calculate the amount of fuel needed
  202. local moves_needed = 0
  203. moves_needed=(x*y*z)
  204. if even(y) then
  205.  moves_needed=moves_needed+(z-1)
  206. else
  207.  moves_needed=moves_needed+((z-1)+(x-1)+(y-1))
  208. end
  209. local items_needed = ceil((moves_needed-turtle.getFuelLevel())/80)
  210. local stacks_needed = floor(items_needed/64)
  211. local stacks_remainder = items_needed%64
  212. local inventory_needed = 0
  213. if items_needed>=64 and stacks_remainder>0 then
  214.  inventory_needed=stacks_needed+1
  215. elseif items_needed>=64 and stacks_remainder==0 then
  216.  inventory_needed=stacks_needed
  217. else
  218.  inventory_needed=1
  219. end
  220. os.sleep(1)
  221.  
  222.  
  223. cls()
  224. if inventory_needed<=16 then
  225.  if items_needed==1 then
  226.   print("Please put 1 unit of coal in the turtle's inventory.")
  227.  elseif items_needed>1 and items_needed<64 then
  228.   print("Please put ",items_needed," units of coal in the turtle's inventory.")
  229.  elseif stacks_needed==1 and stacks_remainder==0 then
  230.   print("Please put 1 stack of coal in the turtle's inventory.")
  231.  elseif stacks_needed==1 and stacks_remainder==1 then
  232.   print("Please put 1 stack of coal and 1 unit of coal in the turtle's inventory.")
  233.  elseif stacks_needed>1 and stacks_remainder>1 then
  234.   print("please put ", stacks_needed , "stacks of coal and ", stacks_remainder , " units of coal in the turtle's inventory.")
  235.  elseif items_needed==0 then
  236.   print("You already have enough fuel,proceed")
  237.  end
  238.  print("<press enter to continue>")
  239.  while true do
  240.   local event,key = os.pullEvent("key")
  241.   if key==28 then break end
  242.  end
  243.  if items_needed>0 then smart_refuel(moves_needed,items_needed) end
  244.  if turtle.getFuelLevel()>=moves_needed then
  245.   room(x,y,z)
  246.  end
  247. else
  248.  print("There is not enough inventory space for the fuel needed.")
  249. end
  250. print("Finished!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement