Advertisement
theguywhojoojes

Auto refuel for turtles

Apr 1st, 2020
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.39 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 items_needed<=64 then
  46.     if turtle.getItemCount(i)>=items_needed then
  47.      --if there are more items than necessary then
  48.      --refuel and return
  49.      turtle.refuel(items_needed)
  50.     else
  51.      --else, subtract the amount of items needed
  52.      --by the amount of items used, refuel
  53.      -- and continue through
  54.      items_needed=items_needed-turtle.getItemCount(i)
  55.      turtle.refuel(items_needed)
  56.     end
  57.    else
  58.     if floor(items_needed/64)>0 then
  59.      items_needed=items_needed-64
  60.      turtle.refuel(64)
  61.     else
  62.      turtle.refuel(items_needed%64)
  63.     end
  64.    end
  65.  end
  66.  if turtle.getFuelLevel()>=moves_needed then
  67.   print("Finished!")
  68.  else
  69.   print("Something went wrong, try again.")
  70.  end
  71. end
  72.  
  73. --checks if refueling is possible
  74. function refuel(moves_needed,items_needed)
  75.  if canrefuel(items_needed) then
  76.   cls()
  77.   print("Refueling...")
  78.   refuel_sub(items_needed,moves_needed)
  79.  else
  80.   print("Not enough coals, try again")
  81.  end  
  82. end  
  83.  
  84.  --Start of the program
  85. cls()
  86. print("Input amount of moves")
  87. local moves_needed = tonumber(io.read())
  88.  
  89.  --custom prompts depending on the
  90.  --amount of fuel needed
  91. if moves_needed<=turtle.getFuelLevel() then
  92.  print("You already have enough fuel")
  93. else
  94.  local items_needed=ceil((moves_needed-turtle.getFuelLevel())/80)
  95.  local stacks_needed=floor(items_needed/64)
  96.  local stacks_remainder=(items_needed%64)
  97.  local inventory_space=stacks_needed+(stacks_remainder/stacks_remainder)
  98.  if inventory_space<=16 then
  99.   if items_needed==1 then
  100.    print("Ok, put 1 unit of coal in the turtle's inventory")
  101.   elseif items_needed>1 and items_needed<=64 then
  102.    print("Ok, put ",items_needed," units of coals in the turtle's inventory")
  103.   elseif stacks_remainder==0 and stacks_needed==1 then
  104.    print("Ok, put 1 stack of coal in the turtle's inventory")
  105.   elseif  stacks_remainder==1 and stacks_needed==1 then
  106.    print("Ok, put 1 stack of coal and 1 unit of coal in the turtle's inventory")
  107.   elseif stacks_remainder>1 and stacks_needed>1 then
  108.    print("Ok, put ", stacks_needed , " stacks of coal and ", stacks_remainder, " units of coal in the turtle's inventory")
  109.   elseif stacks_remainder>1 and stacks_needed==1 then
  110.    print("Ok, put 1 stack of coal and ",stacks_remainder," units of coal in the turtle's inventory")
  111.   end
  112.  else
  113.    print("There is not enough inventory space, try again")
  114.  end
  115.  print("<press enter to continue>")
  116.  while true do
  117.   local event,key = os.pullEvent("key")
  118.   if key==28 then break end
  119.  end
  120.  refuel(moves_needed,items_needed)
  121. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement