Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --new automatic roomer code
- --Clear screen and math functions for convenience
- function cls()
- term.clear()
- term.setCursorPos(1,1)
- end
- function floor(num)
- return math.floor(num)
- end
- function ceil(num)
- return math.ceil(num)
- end
- function even(num)
- if num%2==0 then return true else return false end
- end
- --checks if there are enough coals to refuel the desired amount
- function can_refuel(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
- --actual refueling code so it doesn't get confusing
- function smart_refuel_sub(items_needed)
- for i=1,16 do
- turtle.select(i)
- if items_needed<=64 then
- if turtle.getItemCount(i)>= items_needed then
- turtle.refuel(items_needed)
- return
- else
- items_needed=items_needed-turtle.getItemCount(i)
- turtle.refuel(items_needed)
- end
- else
- if (items_needed/64)>0 then
- items_needed=items_needed-turtle.getItemCount(i)
- turtle.refuel(64)
- else
- turtle.refuel(items_needed)
- return
- end
- end
- end
- end
- --actual refueling
- function smart_refuel(moves_needed,items_needed)
- if can_refuel(items_needed) then
- smart_refuel_sub(items_needed)
- --checks if the amount refueled is smaller than the amount desired
- if turtle.getFuelLevel()>=moves_needed then
- print("Fueling complete!")
- else
- print("An error has ocurred, don't remove the coals before fueling.")
- end
- else
- print("You don't have enough units of coal, try again.")
- end
- end
- --digs and waits for any falling blocks(sand or gravel)
- function wait_dig(dir)
- local wait_time = 1
- if dir=="forward" then
- while turtle.detect() do
- turtle.dig()
- os.sleep(wait_time)
- end
- elseif dir=="down" then
- while turtle.detectDown() do
- turtle.digDown()
- os.sleep(wait_time)
- end
- elseif dir=="up" then
- while turtle.detectUp() do
- turtle.digUp()
- os.sleep(wait_time)
- end
- end
- end
- function switch_direction()
- turtle.turnRight()
- turtle.turnRight()
- end
- function turn(turn_dir)
- if turn_dir=="right" then
- turtle.turnRight()
- wait_dig("forward")
- turtle.forward()
- turtle.turnRight()
- else
- turtle.turnLeft()
- wait_dig("forward")
- turtle.forward()
- turtle.turnLeft()
- end
- end
- function reset(x,y,z)
- if not even(y) then
- for i=1,y-1 do
- turtle.down()
- end
- switch_direction()
- for i=1,x-1 do
- turtle.forward()
- end
- turtle.turnRight()
- for i=0,z-1 do
- turtle.forward()
- end
- else
- turtle.turnRight()
- for i=0,z-1 do
- turtle.forward()
- end
- end
- end
- function room(x,y,z)
- print("Ok, the turtle will start mining now")
- --logs in the 'directions' that the turtle will follow
- local vertical_dir = "up"
- local turn_dir = 0
- local should_switch = false
- if even(y) then
- turn_dir="left"
- else
- turn_dir="right"
- should_switch=true
- end
- --get into position
- wait_dig("forward")
- turtle.forward()
- turtle.turnLeft()
- for depth=1,z do
- for rows=1,y do
- for collumns=1,x do
- if collumns~=x then
- wait_dig("forward")
- turtle.forward()
- elseif rows~=y then
- if vertical_dir=="up" then
- wait_dig("up")
- turtle.up()
- else
- wait_dig("down")
- turtle.down()
- end
- switch_direction()
- elseif depth~=z then
- if turn_dir=="right" then
- turn(turn_dir)
- if should_switch then
- turn_dir="left"
- end
- else
- turn(turn_dir)
- if should_switch then
- turn_dir="right"
- end
- end
- if vertical_dir=="up" and rows==y then
- vertical_dir="down"
- else
- vertical_dir="up"
- end
- end
- end
- end
- end
- reset(x,y,z)
- print("Mining finished! Returning to start...")
- end
- --program start and user input
- cls()
- print("Enter the following measures:")
- term.write("X:")
- local x = tonumber(io.read())
- term.write("Y:")
- local y = tonumber(io.read())
- term.write("Z:")
- local z = tonumber(io.read())
- --calculate the amount of fuel needed
- local moves_needed = 0
- moves_needed=(x*y*z)
- if even(y) then
- moves_needed=moves_needed+(z-1)
- else
- moves_needed=moves_needed+((z-1)+(x-1)+(y-1))
- end
- local items_needed = ceil((moves_needed-turtle.getFuelLevel())/80)
- local stacks_needed = floor(items_needed/64)
- local stacks_remainder = items_needed%64
- local inventory_needed = 0
- if items_needed>=64 and stacks_remainder>0 then
- inventory_needed=stacks_needed+1
- elseif items_needed>=64 and stacks_remainder==0 then
- inventory_needed=stacks_needed
- else
- inventory_needed=1
- end
- os.sleep(1)
- cls()
- if inventory_needed<=16 then
- if items_needed==1 then
- print("Please put 1 unit of coal in the turtle's inventory.")
- elseif items_needed>1 and items_needed<64 then
- print("Please put ",items_needed," units of coal in the turtle's inventory.")
- elseif stacks_needed==1 and stacks_remainder==0 then
- print("Please put 1 stack of coal in the turtle's inventory.")
- elseif stacks_needed==1 and stacks_remainder==1 then
- print("Please put 1 stack of coal and 1 unit of coal in the turtle's inventory.")
- elseif stacks_needed>1 and stacks_remainder>1 then
- print("please put ", stacks_needed , "stacks of coal and ", stacks_remainder , " units of coal in the turtle's inventory.")
- elseif items_needed==0 then
- print("You already have enough fuel,proceed")
- end
- print("<press enter to continue>")
- while true do
- local event,key = os.pullEvent("key")
- if key==28 then break end
- end
- if items_needed>0 then smart_refuel(moves_needed,items_needed) end
- if turtle.getFuelLevel()>=moves_needed then
- room(x,y,z)
- end
- else
- print("There is not enough inventory space for the fuel needed.")
- end
- print("Finished!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement