Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --automatic refuel function for computercraft
- --WIP
- --clear screen and math functions
- --for convenience
- function cls()
- term.clear()
- term.setCursorPos(1,1)
- end
- function ceil(num)
- return math.ceil(num)
- end
- function floor(num)
- return math.floor(num)
- end
- --checks if fueling is possible by
- --comparing the needed amount of items
- --with the amount of fuel in the turtle's
- --inventory
- function canrefuel(items_needed)
- local actual_items=0
- for i=1,16 do
- turtle.select(i)
- if turtle.refuel(0) then
- actual_items=actual_items+turtle.getItemCount(i)
- end
- end
- if actual_items>=items_needed then
- return true
- else
- return false
- end
- end
- --second refueling function
- --because it would be too confusing
- --to fit into the main function
- function refuel_sub(items_needed,moves_needed)
- --for each spot in the inventory do
- for i=1,16 do
- turtle.select(i)
- if turtle.getItemCount(i)>=items_needed then
- --if there are more items than necessary then
- --refuel and return
- turtle.refuel(items_needed)
- else
- --else, subtract the amount of items needed
- --by the amount of items used, refuel
- -- and continue through
- items_needed=items_needed-turtle.getItemCount(i)
- turtle.refuel(items_needed)
- end
- end
- if turtle.getFuelLevel()>=moves_needed then
- print("Finished!")
- else
- print("Something went wrong, try again.")
- end
- end
- --checks if refueling is possible
- function refuel(moves_needed,items_needed)
- if canrefuel(items_needed) then
- cls()
- print("Refueling...")
- refuel_sub(items_needed,moves_needed)
- else
- print("Not enough coals, try again")
- end
- end
- --Start of the program
- cls()
- print("Input amount of moves")
- local moves_needed = tonumber(io.read())
- --custom prompts depending on the
- --amount of fuel needed
- if moves_needed<=turtle.getFuelLevel() then
- print("You already have enough fuel")
- else
- local items_needed=ceil((moves_needed-turtle.getFuelLevel())/80)
- local stacks_needed=floor(items_needed/64)
- local stacks_remainder=(items_needed%64)
- if items_needed==1 then
- print("Ok, put 1 unit of coal in the turtle's inventory")
- elseif items_needed>1 and items_needed<=64 then
- print("Ok, put ",items_needed," units of coals in the turtle's inventory")
- elseif stacks_remainder==0 and stacks_needed==1 then
- print("Ok, put 1 stack of coal in the turtle's inventory")
- elseif stacks_remainder==1 and stacks_needed==1 then
- print("Ok, put 1 stack of coal and 1 unit of coal in the turtle's inventory")
- elseif stacks_remainder>1 and stacks_needed>1 then
- print("Ok, put ", stacks_needed , " stacks of coal and ", stacks_remainder, " units of coal in the turtle's inventory")
- elseif stacks_remainder>1 and stacks_needed==1 then
- print("Ok, put 1 stack of coal and ",stacks_remainder," units of coal in the turtle's inventory")
- end
- print("<press enter to continue>")
- while true do
- local event,key = os.pullEvent("key")
- if key==28 then break end
- end
- refuel(moves_needed,items_needed)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement