Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local supplies = false
- local data = {}
- gps.CHANNEL_GPS = 5033
- --modem things
- local modem = peripheral.wrap("left")
- modem.open(5038)
- local currentDirection = 1
- --gets and stores the direction of the turtle
- function direction(change, strict)
- if change == "left" then
- if currentDirection == 1 then
- currentDirection = 4
- else
- currentDirection = currentDirection - 1
- end
- return currentDirection
- elseif change == "right" then
- if currentDirection == 4 then
- currentDirection = 1
- else
- currentDirection = currentDirection + 1
- end
- return currentDirection
- else
- if strict then
- print("the function \"direction\" recieved and invalid direction while sctrict was set to true")
- else
- return currentDirection
- end
- end
- end
- --turns the robot
- function turn(idirection)
- if idirection == "left" then
- turtle.turnLeft()
- elseif idirection == "right" then
- turtle.turnRight()
- end
- return direction(idirection, true)
- end
- -- moves and mines at the same time to ensure there is no block where it is trying to move
- function moveMine(idirection)
- local waitingCount = 0
- if idirection == "up" then
- location = {gps.locate()}
- moving = true
- while moving do
- bool, block = turtle.inspectUp()
- if block.name ~= "computercraft:turtle_expanded" then
- turtle.digUp()
- else
- waitingCount = waitingCount + 1
- sleep(1)
- end
- turtle.up()
- local newLocation = {gps.locate()}
- if newLocation ~= location then
- moving = false
- end
- if waitingCount > 5 then
- turtle.dig()
- turtle.forward()
- return
- end
- end
- elseif idirection == "forward" then
- location = {gps.locate()}
- moving = true
- while moving do
- bool, block = turtle.inspect()
- if block.name ~= "computercraft:turtle_expanded" then
- turtle.dig()
- else
- waitingCount = waitingCount + 1
- sleep(1)
- end
- turtle.forward()
- local newLocation = {gps.locate()}
- if newLocation ~= location then
- moving = false
- end
- if waitingCount > 5 then
- turtle.digDown()
- turtle.down()
- turtle.digDown()
- turtle.down()
- sleep(3)
- return
- end
- end
- elseif idirection == "down" then
- location = {gps.locate()}
- moving = true
- while moving do
- bool, block = turtle.inspectDown()
- if block.name ~= "computercraft:turtle_expanded" then
- turtle.digDown()
- else
- waitingCount = waitingCount + 1
- end
- turtle.down()
- local newLocation = {gps.locate()}
- if newLocation ~= location then
- moving = false
- end
- if waitingCount > 5 then
- turtle.dig()
- turtle.forward()
- return
- end
- end
- else
- print("function \"moveMine\" recieved invalid direction")
- end
- end
- -- goes to a coordinate position
- function turtleGoto(x, y, z, reverse, gettingValues)
- if gettingValues then
- location = {gps.locate()}
- for part = 1, #location do
- location[part] = math.floor(location[part]+0.5)
- end
- distances = {x - location[1], y - location[2], z - location[3]}
- return distances
- end
- pathing = true
- if reverse then
- while pathing do
- location = {gps.locate()}
- for part = 1, #location do
- location[part] = math.floor(location[part]+0.5)
- end
- distances = {x - location[1], y - location[2], z - location[3]}
- print(textutils.serialize(distances))
- if distances[3] > 0 then
- while direction() ~= 3 do
- turn("left")
- end
- moveMine("forward")
- elseif distances[3] < 0 then
- while direction() ~= 1 do
- turn("left")
- end
- moveMine("forward")
- elseif distances[1] > 0 then
- while direction() ~= 2 do
- turn("right")
- end
- moveMine("forward")
- elseif distances[1] < 0 then
- while direction() ~= 4 do
- turn("left")
- end
- moveMine("forward")
- elseif distances[2] > 0 then
- moveMine("up")
- elseif distances[2] < 0 then
- moveMine("down")
- else
- print("at location")
- pathing = false
- return
- end
- end
- else
- while pathing do
- location = {gps.locate()}
- for part = 1, #location do
- location[part] = math.floor(location[part]+0.5)
- end
- distances = {x - location[1], y - location[2], z - location[3]}
- print(textutils.serialize(distances))
- if distances[2] > 0 then
- moveMine("up")
- elseif distances[2] < 0 then
- moveMine("down")
- elseif distances[1] > 0 then
- while direction() ~= 2 do
- turn("right")
- end
- moveMine("forward")
- elseif distances[1] < 0 then
- while direction() ~= 4 do
- turn("left")
- end
- moveMine("forward")
- elseif distances[3] > 0 then
- while direction() ~= 3 do
- turn("left")
- end
- moveMine("forward")
- elseif distances[3] < 0 then
- while direction() ~= 1 do
- turn("left")
- end
- moveMine("forward")
- else
- print("at location")
- pathing = false
- return
- end
- end
- end
- end
- -- mines the block in front if it and the blocks above and below that block
- function mineForward()
- turtle.dig()
- turtle.dig()
- turtle.dig()
- turtle.dig()
- turtle.dig()
- turtle.dig()
- turtle.dig()
- turtle.dig()
- turtle.dig()
- turtle.dig()
- turtle.dig()
- turtle.dig()
- turtle.dig()
- moveMine("forward")
- turtle.digDown()
- turtle.digUp()
- return
- end
- -- will drop all cobblestone in inventory
- function dropCobble()
- for s = 1,16 do
- turtle.select(s)
- local block = turtle.getItemDetail()
- if block then
- if block.name == "minecraft:cobblestone" then
- turtle.drop(64)
- elseif block.name == "minecraft:stone" then
- turtle.drop(64)
- elseif block.name == "minecraft:gravel" then
- turtle.drop(64)
- elseif block.name == "minecraft:dirt" then
- turtle.drop(64)
- elseif block.name == "chisel:basalt2" then
- turtle.drop(64)
- end
- end
- end
- return
- end
- -- moves using a sequece of commands in an array
- function path(givenPath, info)
- print(textutils.serialize(givenPath))
- for step = 1, #givenPath do
- location = {gps.locate()}
- if givenPath[step] == "forward" then
- moveMine("forward")
- elseif givenPath[step] == "backward" then
- moveMinw("backward")
- elseif givenPath[step] == "left" then
- turtle.turnLeft()
- elseif givenPath[step] == "right" then
- turtle.turnRight()
- elseif givenPath[step] == "up" then
- moveMine("up")
- elseif givenPath[step] == "down" then
- moveMine("down")
- elseif givenPath[step] == "fuel" then
- journey = turtleGoto(info.destination.x, info.destination.y, info.destination.z, false, true)
- turtle.suckUp(getCoalCount(journey[1], journey[2], journey[3]))
- turtle.refuel()
- elseif givenPath[step] == "supply" then
- turtle.suckUp(1)
- elseif givenPath[step] == "drop-off" then
- for s = 1,16 do
- turtle.select(s)
- turtle.dropUp(64)
- end
- elseif givenPath[step] == "north" then
- while direction() ~= 1 do
- turn("left")
- end
- elseif givenPath[step] == "east" then
- while direction() ~= 2 do
- turn("left")
- end
- elseif givenPath[step] == "south" then
- while direction() ~= 3 do
- turn("left")
- end
- elseif givenPath[step] == "west" then
- while direction() ~= 4 do
- turn("left")
- end
- elseif givenPath[step] == "goto" then
- step = step + 1
- turtleGoto(givenPath[step] + info.parentLocation[1], givenPath[step+1] + info.parentLocation[2], givenPath[step+2] + info.parentLocation[3])
- step = step + 2
- end
- end
- end
- -- will mine a 16x16x3 area
- function mineSubchunk(miningDestination, stationReturn)
- if miningDestination.direction == 1 then
- turtleGoto(miningDestination.x, miningDestination.y, miningDestination.z + 1)
- while direction() ~= 1 do
- turn("left")
- end
- elseif miningDestination.direction == 2 then
- turtleGoto(miningDestination.x - 1, miningDestination.y, miningDestination.z - 16)
- while direction() ~= 2 do
- turn("left")
- end
- elseif miningDestination.direction == 3 then
- turtleGoto(miningDestination.x + 16, miningDestination.y, miningDestination.z - 17)
- while direction() ~= 3 do
- turn("left")
- end
- elseif miningDestination.direction == 4 then
- turtleGoto(miningDestination.x + 17, miningDestination.y, miningDestination.z)
- while direction() ~= 4 do
- turn("left")
- end
- else
- print("invalid direction given to function \"minSubChunk\"")
- end
- mineForward()
- local direction = "right"
- for y= 1,16 do
- for x= 1,15 do
- mineForward()
- end
- dropCobble()
- if direction == "right" then
- turtle.turnRight()
- mineForward()
- turtle.turnRight()
- direction = "left"
- elseif direction == "left" then
- turtle.turnLeft()
- mineForward()
- turtle.turnLeft()
- direction = "right"
- end
- end
- turtleGoto(stationReturn.x, stationReturn.y, stationReturn.z, true)
- return true
- end
- -- calculates how much coal is needed for journey
- function getCoalCount(x, y, z)
- local distance = ((math.abs(x) + math.abs(y) + math.abs(z)) * 2) + 256
- print(distance)
- return distance / 80 + 3
- end
- function rollOut(startData)
- distances = turtleGoto(startData.destination.x, startData.destination.y, startData.destination.z, false, true)
- neededCoal = getCoalCount(distances[1], distances[2], distances[3])
- path(startData.exitPath, startData)
- print("done pathing")
- if mineSubchunk(startData.destination, startData.returnPoint) then
- rollIn(startData)
- end
- end
- --brings the bot back from outside the station
- function rollIn(startData)
- path(startData.enterPath, startData)
- modem.transmit(5037, 5038, "eatme")
- end
- --waits for connection with parent bot
- print("debug: made it to loop")
- transmitting = true
- while transmitting do
- print("debug: made it into loop")
- modem.transmit(5037, 5038, "created")
- response = {os.pullEvent("modem_message")}
- modem.transmit(response[3], response[4], "leaving")
- info = response[5]
- print(textutils.serialize(info.exitPath))
- rollOut(info)
- transmitting = false
- end
Add Comment
Please, Sign In to add comment