Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- digBox 2.0
- -- JerikTelorian
- -- Jerikorp Industrial & Technology Solutions
- -- Copyright 2015
- -- VARIABLES ----------------------------
- -- None
- -- FUNCTIONS ----------------------------
- -- Digs Upwards
- function DigAscend(height)
- count = 1
- while (count < height) do
- turtle.digUp()
- result = turtle.up()
- if result then
- count = count + 1
- end
- end
- end
- -- Digs a line straight ahead
- function DigStraightAhead(distance)
- count = 1
- while count <= distance do
- turtle.dig()
- result = turtle.forward()
- print("----Block Count: ", count)
- if result then -- Only increment if the move was a success
- count = count + 1
- end
- end
- end
- function RefuelToLevel(targetFuelLevel)
- for i=1, 16 do
- while true do
- res = turtle.Refuel()
- if not res or turtle.getFuelLevel() > targetFuelLevel then
- break
- end
- end
- if turtle.getFuelLevel() > targetFuelLevel then
- return true
- end
- end
- return false
- end
- -- Starting at the lowest point, ascends and digs a vertical slice of
- -- the specified height and depth. Plane is Saggital with respect to the
- -- orientation of the turtle.
- function DigSaggitalPlane(height, depth)
- DigAscend(height)
- count = 1
- while (count <= height) do
- DigStraightAhead(depth)
- -- Does an about face and goes down one level.
- -- No need to check since this should always work.
- if (count < height) then
- turtle.turnLeft()
- turtle.turnLeft()
- turtle.digDown()
- turtle.down()
- end
- count = count + 1
- end
- end
- -- DigCoronalPlane digs a coronal plane with respect to the orientation of the turtle at start
- function DigCoronalPlane(width, depth, isOnLeftSide, alreadyInPlane)
- rowCount = 1
- nextTurn = 0
- if isOnLeftSide then
- nextTurn = 1 -- Positive indicates a right turn
- else
- nextTurn = -1 -- Negative indicates a left turn
- end
- -- If you aren't already in the plane, go forward one block to enter
- if not alreadyInPlane then
- DigStraightAhead(1)
- end
- while (rowCount <= width) do
- print("--Row Count: ", rowCount)
- DigStraightAhead(depth-1)
- -- We only need to execute the turn if you're not on the last set
- if rowCount < width then
- -- Code for turning right
- if nextTurn > 0 then
- turtle.turnRight()
- while true do -- Ensure that the turtle moves
- turtle.dig()
- res = turtle.forward()
- if res then
- break
- end
- end
- turtle.turnRight()
- -- Code for turning left
- else
- turtle.turnLeft()
- while true do -- Ensure that the turtle moves
- turtle.dig()
- res = turtle.forward()
- if res then
- break
- end
- end
- turtle.turnLeft()
- end
- -- Invert the turn so the next one goes the other way
- end
- nextTurn = nextTurn * -1
- rowCount = rowCount + 1
- end
- end
- function DigBox(w, h, d)
- -- User is instructed to place the bot at the bottom-left corner of the box.
- isOnLeftSide = true
- -- First go to the top corner.
- DigAscend(h)
- for i=h,1, -1 do
- print("Level Count: ", i)
- if i==h then -- On the first one, you need to enter the plane
- DigCoronalPlane(w, d, isOnLeftSide, false)
- else -- on subsequent, you're already in the plane
- DigCoronalPlane(w, d, isOnLeftSide, true)
- end
- -- 180deg turn
- turtle.turnLeft()
- turtle.turnLeft()
- -- Descend
- if i > 1 then
- turtle.digDown()
- turtle.down()
- end
- -- For even widths, we alternate the first turns
- if w%2 == 0 then
- isOnLeftSide = not isOnLeftSide
- end
- end
- end
- -- Estimates fuel needs. Returns true if the job can be completed within the
- -- maximum fuel level for the turtle, false if not.
- function VerifyFuel(dims)
- totalCost = dims["w"] * dims["d"] * dims["h"]
- print("Fuel cost estimated at: ", totalCost)
- print("CurrentFuel: ", turtle.getFuelLevel())
- if totalCost > turtle.getFuelLimit() then
- print("Warning! Fuel Cost Higher than maximum fuel level!")
- print("Maximum fuel level is: ", turtle.getFuelLimit())
- return false
- elseif totalCost > turtle.getFuelLevel() then
- print("Attempt to refuel? y/n")
- if io.read() == "y" then
- return RefuelToLevel(totalCost)
- else
- return false
- end
- else
- return true
- end
- end
- function RefuelToLevel(level)
- for i=1,16 do
- turtle.select(i)
- while turtle.getFuelLevel() <= level do
- success = turtle.refuel(1)
- if not success then
- break
- end
- end
- if turtle.getFuelLevel() > level then
- print("Success. Fuel level: ", turtle.getFuelLevel())
- return true
- end
- end
- print("Not enough fuel. Current: ", turtle.getFuelLevel())
- return false
- end
- function GetInput()
- result = {}
- io.write("How wide should the box be?")
- w = math.floor(tonumber(io.read()))
- io.write("How deep should the box be?")
- d = math.floor(tonumber(io.read()))
- io.write("How high should the box be?")
- h = math.floor(tonumber(io.read()))
- result["w"] = w
- result["d"] = d
- result["h"] = h
- return result
- end
- function ObtainAndVerifyInput()
- gotInput,dims = pcall(GetInput)
- if not gotInput then
- return false
- end
- print("**Input obtained.")
- if (dims["w"] < 1) or (dims["d"] < 1) or (dims["h"] < 1) then
- print("Error: All dimensions must be greater than or equal to 1")
- return false
- end
- print("**Input formatted correctly.")
- if not VerifyFuel(dims) then
- return false
- end
- print("**Fuel levels nominal.")
- return true, dims
- end
- function Main()
- term.clear()
- term.setCursorPos(1,1)
- goodToGo, dims = ObtainAndVerifyInput()
- print("Dig parameters:")
- print("Width: ", dims["w"])
- print("Depth: ", dims["d"])
- print("Height: ", dims["h"])
- print("Execute Dig? y/n")
- ans = io.read()
- if goodToGo and ans == "y" then
- term.clear()
- term.setCursorPos(1,1)
- print("Beginning dig")
- DigBox(dims["w"], dims["h"], dims["d"])
- print("Dig finished")
- else
- print("Dig cancelled.")
- end
- print("Goodbye")
- end
- function DispLogo()
- print("___________________________ ")
- sleep(0.05)
- print("++++++++++..++...+++....+++++++ ")
- sleep(0.05)
- print("+++++++++...++..+++...+++++++++++ ")
- sleep(0.05)
- print("+++++++++..++...+...+++++++++++++++ ")
- sleep(0.05)
- print("++++++++...++..+..,++++++++++++++++++ ")
- sleep(0.05)
- print("++++++++..++......+++++++++++++++++++++")
- sleep(0.05)
- print("+++++++..:+=..+,..+++++++++++++++++++ ")
- sleep(0.05)
- print("+...++...++..~++..~+++++++++++++++ ")
- sleep(0.05)
- print("+.......++...+++...+++++++++++++ ")
- sleep(0.05)
- print("++~..,++++..++++...+++++++++++ ")
- sleep(0.05)
- print("--------------------------- ")
- sleep(0.05)
- print("Jerikorp: Today's Tech,Tomorrow's World")
- sleep(2)
- end
- term.clear()
- term.setCursorPos(1,1)
- DispLogo()
- print("DigBox 2.0 -- A Jerikorp® Product\n")
- print("Copyright Jerikorp Industrial \n\tand Technological Solutions 2015\n")
- io.write("Instructions: Place the Turtle at the BOTTOM-LEFT corner of the box you want to dig.\n")
- io.write("All dimensions must be an integer greater than 0.\n\n")
- print("Press Enter to Continue")
- io.read()
- -- Executes main program.
- Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement