Advertisement
Guest User

x

a guest
Apr 1st, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.02 KB | None | 0 0
  1. --automatic refuel function for computercraft
  2. --WIP
  3.  
  4.  
  5. --clear screen and math functions
  6. --for convenience
  7. function cls()
  8.  term.clear()
  9.  term.setCursorPos(1,1)
  10. end
  11. function ceil(num)
  12.  return math.ceil(num)
  13. end
  14.  function floor(num)
  15.   return math.floor(num)
  16.  end
  17.  
  18.  --checks if fueling is possible by
  19.  --comparing the needed amount of items
  20.  --with the amount of fuel in the turtle's
  21.  --inventory
  22. function canrefuel(items_needed)
  23.  local actual_items=0
  24.  for i=1,16 do
  25.   turtle.select(i)
  26.   if turtle.refuel(0) then
  27.    actual_items=actual_items+turtle.getItemCount(i)
  28.   end
  29.  end
  30.  if actual_items>=items_needed then
  31.   return true
  32.  else
  33.   return false
  34.  end
  35. end
  36.  
  37.  
  38.  --second refueling function
  39.  --because it would be too confusing
  40.  --to fit into the main function
  41. function refuel_sub(items_needed,moves_needed)
  42.  --for each spot in the inventory do
  43.  for i=1,16 do
  44.   turtle.select(i)
  45.    if turtle.getItemCount(i)>=items_needed then
  46.     --if there are more items than necessary then
  47.        --refuel and return
  48.     turtle.refuel(items_needed)
  49.    else
  50.     --else, subtract the amount of items needed
  51.        --by the amount of items used, refuel
  52.        -- and continue through
  53.        items_needed=items_needed-turtle.getItemCount(i)
  54.     turtle.refuel(items_needed)
  55.    end
  56.  end
  57.  if turtle.getFuelLevel()>=moves_needed then
  58.   print("Finished!")
  59.  else
  60.   print("Something went wrong, try again.")
  61.  end
  62. end
  63.  
  64. --checks if refueling is possible
  65. function refuel(moves_needed,items_needed)
  66.  if canrefuel(items_needed) then
  67.   cls()
  68.   print("Refueling...")
  69.   refuel_sub(items_needed,moves_needed)
  70.  else
  71.   print("Not enough coals, try again")
  72.  end  
  73. end  
  74.  
  75.  --Start of the program
  76. cls()
  77. print("Input amount of moves")
  78. local moves_needed = tonumber(io.read())
  79.  
  80.  --custom prompts depending on the
  81.  --amount of fuel needed
  82. if moves_needed<=turtle.getFuelLevel() then
  83.  print("You already have enough fuel")
  84. else
  85.  local items_needed=ceil((moves_needed-turtle.getFuelLevel())/80)
  86.  local stacks_needed=floor(items_needed/64)
  87.  local stacks_remainder=(items_needed%64)
  88.  if items_needed==1 then
  89.   print("Ok, put 1 unit of coal in the turtle's inventory")
  90.  elseif items_needed>1 and items_needed<=64 then
  91.   print("Ok, put ",items_needed," units of coals in the turtle's inventory")
  92.  elseif stacks_remainder==0 and stacks_needed==1 then
  93.   print("Ok, put 1 stack of coal in the turtle's inventory")
  94.  elseif  stacks_remainder==1 and stacks_needed==1 then
  95.   print("Ok, put 1 stack of coal and 1 unit of coal in the turtle's inventory")
  96.  elseif stacks_remainder>1 and stacks_needed>1 then
  97.   print("Ok, put ", stacks_needed , " stacks of coal and ", stacks_remainder, " units of coal in the turtle's inventory")
  98.  elseif stacks_remainder>1 and stacks_needed==1 then
  99.   print("Ok, put 1 stack of coal and ",stacks_remainder," units of coal in the turtle's inventory")
  100.  end
  101.  print("<press enter to continue>")
  102.  while true do
  103.   local event,key = os.pullEvent("key")
  104.   if key==28 then break end
  105.  end
  106.  refuel(moves_needed,items_needed)
  107. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement