Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("turtleShell")
- -- bottomDweller
- -- In this program, the turtle will mine from level 5 to level 15.
- -- He will hollow out the entirety of level 5, 8, 11, and 14.
- -- During this process, he will be detecting above and below him,
- -- ignoring the designated blocks in slots 2-4. The turtle should be
- -- stocked with fuel in slot 1, but if there is no fuel, he will
- -- look for an alternative source in all slots.
- -- Eventually, the turtle will have the ability to broadcast his
- -- coordinates to the server.
- -- Information specific to rednet
- job = "Bottom Dwelling"
- broadcast = false
- -- End rednet
- task = "" -- Will also be broadcast on rednet
- xHome = 0
- zHome = 0
- yHome = 0
- facingHome = 1
- facings = {"west", "north", "east", "south"}
- yActual = 0
- yDestination = 5
- radius = 0
- xMinimum = 0
- xMaximum = 0
- zMinimum = 0
- zMaximum = 0
- yMaximum = 15
- -- Where the turtle is at, relative to home.
- xCurrent = 0
- zCurrent = 0
- yCurrent = 0
- facingCurrent = 1
- -- If the turtle needs to unload, store coordinates here.
- xTemp = 0
- yTemp = 0
- zTemp = 0
- facingTemp = 0
- currentQuadrant = 0
- currentLevel = 5
- ignoreInventory = {}
- success = false
- incomplete = true
- continue = true
- function getParameters()
- confirm = false
- while not(confirm) do
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter Y coordinate of turtle:")
- yActual = read()
- -- print("Enter current facing (1=W/2=N/3=E/4=S):")
- -- facingCurrent = read()
- print("Enter radius:")
- radius = read()
- term.clear()
- print("Current Y = " .. yActual)
- -- print("Facing " .. facingCurrent)
- print("Mine a " .. radius .. " block radius.")
- print("Confirm (Y/N)?")
- response = read()
- if response == "Y" or response == "y" then
- -- facingHome = facingCurrent
- confirm = true
- end
- end
- end
- function initialize()
- task = "Initializing"
- -- begin rednet
- if broadcast then
- end
- -- end rednet
- xMinimum = xHome - radius -- west edge
- xMaximum = xHome + radius -- east edge
- zMinimum = zHome - radius -- south edge
- zMaximum = zHome + radius -- north edge
- for i = 1,16 do
- table.insert(ignoreInventory,i)
- end
- end
- function checkBlockAboveAndBelow()
- digUp = true
- digDown = true
- for i = 1,#ignoreInventory do
- turtle.select(ignoreInventory[i])
- if turtle.compareUp() then
- digUp = false
- end
- if turtle.compareDown() then
- digDown = false
- end
- end
- if digUp then
- if turtleShell.betterDigUp() then
- end
- end
- if digDown then
- if turtleShell.betterDigDown() then
- end
- end
- end
- function checkBlockForward()
- digForward = true
- for i = 1,#ignoreInventory do
- turtle.select(ignoreInventory[i])
- if turtle.compare() then
- digForward = false
- end
- end
- if digForward then
- if turtleShell.betterDig() then
- end
- end
- end
- -- The amount of fuel needed to return home.
- function checkDistance()
- return xCurrent + zCurrent + yCurrent
- end
- function trackForward(checkBlocks)
- -- If we are on the perimeter of the square, we also want to
- -- check the walls. Check will tell us "left", "forward",
- -- "right", "forwardleft", "forwardright", or "nothing".
- -- All settings except "nothing" will check up and down .
- checkBlocks = checkBlocks or "nothing"
- -- check fuel
- if not(turtleShell.fuelUp(checkDistance())) then
- returnHome(false)
- end
- if turtleShell.betterDig() then
- end
- if turtle.forward() then
- if facingCurrent == 1 then
- xCurrent = xCurrent - 1
- elseif facingCurrent == 2 then
- zCurrent = zCurrent + 1
- elseif facingCurrent == 3 then
- xCurrent = xCurrent + 1
- elseif facingCurrent == 4 then
- yCurrent = yCurrent - 1
- end
- else
- returnHome(false)
- end
- -- Check adjacent blocks
- if not(checkBlocks == "nothing") then
- checkBlockAboveAndBelow()
- if checkBlocks == "forward" then
- checkBlockForward()
- elseif checkBlocks == "right" then
- trackRight()
- checkBlockForward()
- trackLeft()
- elseif checkBlocks == "left" then
- trackLeft()
- checkBlockForward()
- trackRight()
- elseif checkBlocks == "forwardright" then -- corners
- checkBlockForward()
- trackRight()
- checkBlockForward()
- trackLeft()
- elseif checkBlocks == "forwardleft" then -- corners
- checkBlockForward()
- trackLeft()
- checkBlockForward()
- trackRight()
- end
- end
- -- Rather than checking inventory every time the turtle successfully
- -- digs, check it once at the end. The turtle will be sucking up
- -- everything when it returns anyhow, should anything be left behind.
- if turtleShell.checkInventory() then
- returnHome(false)
- end
- turtle.suck()
- turtle.suckUp()
- turtle.suckDown()
- end
- function trackDown(digShaft)
- -- The only time that we have to dig is during the initial descent,
- -- so we grant the ability to bypass the digging.
- digShaft = digShaft or false
- -- check fuel
- if not(turtleShell.fuelUp(checkDistance())) then
- returnHome(false)
- end
- if digShaft then
- if turtleShell.betterDigDown() then
- if (turtle.down()) then
- yCurrent = yCurrent - 1
- end
- else
- continue = false
- returnHome()
- end
- if turtleShell.checkInventory() then
- returnHome(false)
- end
- else
- if (turtle.down()) then
- yCurrent = yCurrent - 1
- end
- end
- turtle.suck()
- turtle.suckUp()
- turtle.suckDown()
- end
- -- The only time that we will ever be moving up is when going up
- -- the mine shaft. We never have to check for fuel during this
- -- process because we will always be closer to home, so the check
- -- has already been made.
- function trackUp()
- if (turtle.up()) then
- yCurrent = yCurrent + 1
- end
- end
- function trackRight()
- turtle.turnRight()
- facingCurrent = facingCurrent + 1
- if facingCurrent == 5 then
- facingCurrent = 1
- end
- end
- function trackLeft()
- turtle.turnLeft()
- facingCurrent = facingCurrent - 1
- if facingCurrent == 0 then
- facingCurrent = 4
- end
- end
- -- face(dDirection,cDirection)
- -- sDirection - destination direction
- -- cDirection - current direction (if not provided, uses currentDirection)
- function face(dDirection,cDirection)
- cDirection = cDirection or facingCurrent
- if cDirection - dDirection == 0 then
- elseif cDirection - dDirection == 1 or cDirection - dDirection == -3 then
- trackLeft()
- elseif cDirection - dDirection == -1 or cDirection - dDirection == 3 then
- trackRight()
- elseif cDirection - dDirection == 2 or cDirection - dDirection == -2 then
- trackLeft()
- trackLeft()
- else
- -- Unknown direction.
- end
- end
- function faceWest(cDirection)
- face(1,cDirection)
- end
- function faceNorth(cDirection)
- face(2,cDirection)
- end
- function faceEast(cDirection)
- face(3,cDirection)
- end
- function faceSouth(cDirection)
- face(4,cDirection)
- end
- -- if in quadrant 1, go south then east
- -- if in quadrant 2, go west then south
- -- if in quadrant 3, go north then west
- -- if in quadrant 4, go east then north
- function returnHome(quadrant,returnToSurface)
- -- If the turtle needs to return to the surface, flag as true
- quadrant = quadrant or currentQuadrant
- returnToSurface = returnToSurface or false
- if returnToSurface then
- task = "Returning to mine origin"
- else
- task = "Returning to level origin"
- end
- -- begin rednet
- if broadcast then
- end
- -- end rednet
- -- Remember where we were at
- xTemp = xCurrent
- zTemp = zCurrent
- yTemp = yCurrent
- facingTemp = facingCurrent
- pDirection = quadrant - 1
- if pDirection == 0 then
- pDirection = 4
- end
- sDirection = quadrant - 2
- if sDirection == 0 then
- sDirection = 4
- elseif sDirection == -1 then
- sDirection = 3
- end
- for i = 1,2 do
- if i == 1 then
- direction = pDirection
- else
- direction = sDirection
- end
- face(direction)
- if direction == 1 or direction == 3 then
- while xCurrent ~= xHome do
- trackForward("nothing")
- end
- end
- if direction == 2 or direction == 4 then
- while zCurrent ~= zHome do
- trackForward("nothing")
- end
- end
- end
- if returnToSurface then
- while yCurrent ~= yHome do
- trackUp()
- end
- end
- end
- -- if in quadrant 1, go west then north
- -- if in quadrant 2, go north then east
- -- if in quadrant 3, go east then south
- -- if in quadrant 4, go south then west
- function returnToMining(quadrant)
- quadrant = quadrant or currentQuadrant
- task = "Returning to mining"
- -- begin rednet
- if broadcast then
- end
- -- end rednet
- pDirection = quadrant
- sDirection = quadrant + 1
- if sDirection == 5 then
- sDirection = 1
- end
- while yCurrent ~= yTemp do
- trackDown()
- end
- for i = 1,2 do
- if i == 1 then
- direction = pDirection
- else
- direction = sDirection
- end
- face(direction)
- if direction == 1 or direction == 3 then
- while xCurrent ~= xTemp do
- trackForward("nothing")
- end
- end
- if direction == 2 or direction == 4 then
- while zCurrent ~= zTemp do
- trackForward("nothing")
- end
- end
- end
- face(facingTemp)
- end
- function unloadInventory()
- task = "Unloading inventory"
- -- begin rednet
- if broadcast then
- end
- -- end rednet
- face(facingHome)
- for i = 1,16 do
- if not(ignoreInventory[i]) then
- turtle.select(i)
- turtle.drop()
- end
- end
- end
- -- quadrants
- -- 1 2
- -- 4 3
- -- quadrant 1 will dig z = 0 between 1 and 4
- -- quadrant 2 will dig x = 0 between 2 and 1
- -- quadrant 3 will dig z = 0 between 3 and 2
- -- quadrant 4 will dig x = 0 between 4 and 3
- -- digQuadrant
- -- Digging -- Direction
- -- I********# -- IBBBBBBBBB
- -- *XXXXXXXX# -- BH<<<<<<<#
- -- *XXXXXXXX# -- B>>>>>>>^#
- -- *XXXXXXXX# -- B^<<<<<<<#
- -- *XXXXXXXX# -- B>>>>>>>^#
- -- *XXXXXXXX# -- B^<<<<<<<#
- -- *XXXXXXXX# -- B>>>>>>>^#
- -- *XXXXXXXX# -- B^<<<<<<<#
- -- *XXXXXXXX# -- B>>>>>>>^#
- -- *XXXXXXXX0 -- B^<<<<<<<<
- --
- -- 0 = home x,z (mine shaft)
- -- X = dig and check up and down
- -- * = do not dig, but check
- -- # = will be dug by subsequent quadrant run
- -- B = border
- -- < = go in primary direction
- -- > = go opposite of primary direction
- -- ^ = go in secondary direction
- -- H = go home
- -- I = inaccessible
- --
- --
- -- This will be run 4 times for each level.
- -- Picture the above graphic with a push pin
- -- placed directly in the home (0). Then
- -- rotate that 90 degrees for each run.
- function digQuadrant(quadrant,length)
- length = length or radius
- task = "Mining quadrant " .. quadrant
- -- begin rednet
- if broadcast then
- end
- -- end rednet
- pDirection = quadrant -- primary direction - <
- sDirection = quadrant + 1 -- secondary direction - ^
- if sDirection == 5 then
- sDirection = 1
- end
- tDirection = quadrant + 2
- if tDirection == 5 then -- tertiary direction - >
- tDirection = 1
- elseif tDirection == 6 then
- tDirection = 2
- end
- evenDig = true
- pEnd = false
- sEnd = false
- trackForward("normal")
- for s = 1,length do -- secondary
- if s == length then
- sEnd = true
- end
- for p = 1,length - 1 do -- primary
- if p == length - 1 then
- pEnd = true
- end
- if pEnd and sEnd and evenDig then
- trackForward("forwardright")
- elseif pEnd and sEnd and not(evenDig) then
- trackForward("left")
- elseif pEnd and not(sEnd) and evenDig then
- trackForward("forward")
- elseif pEnd and not(sEnd) and not(evenDig) then
- trackForward("normal")
- elseif not(pEnd) and sEnd and evenDig then
- trackForward("right")
- elseif not(pEnd) and sEnd and not(evenDig) then
- trackForward("left")
- elseif not(pEnd) and not(sEnd) and evenDig then
- trackForward("normal")
- elseif not(pEnd) and not(sEnd) and not(evenDig) then
- trackForward("normal")
- end
- end
- if not(sEnd) and evenDig then
- trackRight()
- trackForward("left")
- trackRight()
- evenDig = false
- elseif not(sEnd) and not(evenDig) then
- trackLeft()
- trackForward("normal")
- trackLeft()
- evenDig = true
- end
- pEnd = false
- end
- returnHome()
- end
- function digLevel()
- for i = 1,4 do
- currentQuadrant = i
- digQuadrant(i)
- end
- end
- function digMineShaft()
- task = "Digging mine shaft"
- -- begin rednet
- if broadcast then
- end
- -- end rednet
- while yActual + yCurrent ~= yDestination and continue do
- trackDown(true)
- end
- end
- getParameters()
- initialize()
- digMineShaft()
- while currentLevel < yMaximum do
- digLevel()
- trackUp()
- trackUp()
- trackUp()
- currentLevel = currentLevel + 3
- end
- returnHome()
- unloadInventory()
Advertisement
Add Comment
Please, Sign In to add comment