Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- computercraft
- -- init
- turtle.refuel()
- rednet.open("left")
- -- utility functions
- function determineDirection()
- xc, yc, zc = gps.locate()
- turtle.dig()
- turtle.forward()
- xc2, yc2, zc2 = gps.locate()
- turtle.back()
- -- based on which coordinate changed we can calculate which direction the turtle is facing
- if xc - xc2 == -1 then
- return "east"
- end
- if xc - xc2 == 1 then
- return "west"
- end
- if zc - zc2 == -1 then
- return "south"
- end
- if zc - zc2 == 1 then
- return "north"
- end
- return "none"
- end
- function turtle.turnAround()
- turtle.turnLeft()
- turtle.turnLeft()
- end
- function sgn(num)
- if num > 0 then
- return 1
- end
- if num < 0 then
- return -1
- end
- return 0
- end
- function ensureFacingNorth()
- i = 0
- ::tryAgain::
- direction = determineDirection()
- -- make turtle always face north for movements
- if direction == "east" then
- turtle.turnLeft()
- elseif direction == "west" then
- turtle.turnRight()
- elseif direction == "south" then
- turtle.turnAround()
- elseif direction ~= "north" then
- if i ~= 4 then
- turtle.turnRight()
- i = i + 1
- goto tryAgain
- end
- end
- end
- function mineAllAroundEmergency()
- for i=1,4 do
- turtle.turnLeft()
- turtle.dig()
- end
- turtle.digUp()
- turtle.digDown()
- end
- function gotoPos(x, y, z, try)
- -- get turtle pos from gps
- xpos, ypos, zpos = gps.locate()
- ensureFacingNorth()
- turtle.turnRight()
- ::toPos::
- for i=1, math.abs(x-xpos) do
- if sgn(x-xpos) == 0 then
- break
- elseif sgn(x-xpos) == 1 then
- turtle.dig()
- turtle.forward()
- elseif sgn(x-xpos) == -1 then
- turtle.turnAround()
- turtle.dig()
- turtle.turnAround()
- turtle.back()
- end
- end
- turtle.turnLeft()
- if try >= 2 then
- mineAllAroundEmergency() --attempt to get unstuck
- end
- if try >= 2 and try <= 10 then
- turtle.up()
- end
- for i=1, math.abs(z-zpos) do
- if sgn(z-zpos) == 0 then
- break
- elseif sgn(z-zpos) == 1 then
- turtle.dig()
- turtle.forward()
- elseif sgn(z-zpos) == -1 then
- turtle.turnAround()
- turtle.dig()
- turtle.turnAround()
- turtle.back()
- end
- end
- for i=1, math.abs(y-ypos) do
- if sgn(y-ypos) == 0 then
- break
- elseif sgn(y-ypos) == 1 then
- turtle.digUp()
- turtle.up()
- elseif sgn(y-ypos) == -1 then
- turtle.digDown()
- turtle.down()
- end
- end
- -- get turtle pos from gps again
- xpos, ypos, zpos = gps.locate()
- xdiff = x - xpos
- ydiff = y - ypos
- zdiff = z - zpos
- -- if we aren't at target point then go back to trying to get there until we get there
- if not (xdiff == 0 and ydiff == 0 and zdiff == 0) then
- print("We are not at target point "..x.." "..y.." "..z..". Why?!?!")
- ensureFacingNorth()
- gotoPos(x, y, z, try + 1)
- end
- -- if we are at the target point then finish and end function
- end
- function splitBySpace(string)
- temp = {}
- i2 = 1
- for i in string.gmatch(string, "%S+") do
- temp[i2] = i
- i2 = i2 + 1
- end
- return temp
- end
- function faceDirectionAssumingNorth(directiona)
- if directiona == "east" then
- turtle.turnLeft()
- elseif directiona == "west" then
- turtle.turnRight()
- elseif directiona == "south" then
- turtle.turnAround()
- elseif directiona ~= "north" then
- error("Invalid direction")
- end
- end
- function depositAndRefuel(homex, homey, homez, minestartx, minestarty, minestartz, facing)
- curx, cury, curz = gps.locate()
- gotoPos(minestartx, homey, minestartz, 1)
- -- we are now on the surface (or should be)
- gotoPos(homex, homey, homez, 1)
- -- we are also guaranteed to be facing north after movement, so deposit in chest south of turtle
- turtle.turnAround()
- -- deposit everything in the chest
- for i=1, 16 do
- turtle.select(i)
- turtle.drop()
- end
- turtle.turnAround()
- -- we expect to have a fuel chest above the home point, so get a stack from the fuel chest
- turtle.suckUp()
- turtle.refuel()
- -- return to what we were doing
- gotoPos(minestartx, homey, minestartz, 1)
- -- travel down the mineshaft
- gotoPos(minestartx, minestarty, minestartz, 1)
- -- we are now back in the layer, go to where we were before this function was called
- gotoPos(curx, cury, curz, 1)
- -- face where we were before this function was called
- faceDirectionAssumingNorth(facing)
- end
- function goHome(homex, homey, homez, minestartx, minestarty, minestartz)
- gotoPos(minestartx, homey, minestartz, 1)
- gotoPos(homex, homey, homez, 1)
- turtle.turnAround()
- end
- function isInventoryFull()
- full = 0
- for i=1, 16 do
- turtle.select(i)
- if turtle.getItemCount() > 0 then
- full = full + 1
- end
- end
- turtle.select(1)
- return full == 16
- end
- -- main
- homex, homey, homez = gps.locate()
- while true do
- -- standby to recieve command
- sender, message = rednet.receive()
- if string.find(message, "GOTO") ~= nil then
- message = splitBySpace(message)
- rednet.send(sender, "ACK")
- gotoPos(tonumber(message[2]), tonumber(message[3]), tonumber(message[4]), 1)
- end
- if string.find(message, "MINE") ~= nil then
- message = splitBySpace(message)
- rednet.send(sender, "ACK") -- since i don't get the ACK from the turtle, the bug must be occuring before here
- -- message format: "MINE -4220 64 3433 -4110 50 3333"
- x1 = message[2]
- y1 = message[3]
- z1 = message[4]
- x2 = message[5]
- y2 = message[6]
- z2 = message[7]
- startx, starty, startz = gps.locate()
- -- depositAndRefuel(homex, homey, homez, startx, starty, startz, "north")
- -- everything in y layers
- for y=1, math.abs(y1-y2) do
- gotoPos(math.min(x1, x2), math.min(y1, y2) + y - 1, math.max(z1, z2), 1)
- switch = true
- facing = "north"
- -- everything in x layer
- for x=1, math.abs(x1-x2) + 1 do
- -- everything in z line
- for z=1, math.abs(z1-z2) do
- turtle.dig()
- turtle.forward()
- end
- if switch then
- turtle.turnRight()
- turtle.dig()
- turtle.forward()
- turtle.turnRight()
- facing = "south"
- else
- turtle.turnLeft()
- turtle.dig()
- turtle.forward()
- turtle.turnLeft()
- facing = "north"
- end
- if isInventoryFull() or turtle.getFuelLevel() <= 200 then
- depositAndRefuel(homex, homey, homez, math.min(x1, x2), math.min(y1, y2) + math.abs(y1-y2) - 1, math.max(z1, z2), facing)
- end
- switch = not switch
- end
- turtle.dig()
- end
- goHome(homex, homey, homez, math.min(x1, x2), math.min(y1, y2) + math.abs(y1-y2) - 1, math.max(z1, z2))
- turtle.turnAround()
- end
- end
Add Comment
Please, Sign In to add comment