Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local ports = {
- ["phone"] = 8081,
- ["server"] = 8080,
- ["deployer"] = 5050,
- ["turtle"] = math.random(1000,9000) -- generates a random port for this turtle
- }
- local key = "" -- a key sent to us by the server which authenticates us
- local data = nil
- local modem = peripheral.wrap("left")
- modem.open(ports.turtle) -- opens a port for us
- local homeCoords = vector.new(gps.locate(5,false))
- -- modem.transmit(ports.server,ports.turtle,"MESSAGE") base transmit code
- -- startup sub --
- turtle.suckDown() -- gets lava sack
- -- startup sub finished --
- function split (inputstr, sep)
- if sep == nil then
- sep = "%s"
- end
- local t={}
- for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
- table.insert(t, str)
- end
- return t
- end
- function parseParams(data)
- coords = {}
- params = split(data, " ")
- coords[1] = vector.new(params[1], params[2], params[3])
- coords[2] = vector.new(params[4], params[5], params[6])
- return (coords)
- end
- function getItemIndex(itemName)
- for slot = 1, 16, 1 do
- local item = turtle.getItemDetail(slot)
- if(item ~= nil) then
- if(item["name"] == itemName) then
- return slot
- end
- end
- end
- end
- function checkFuel()
- if(turtle.getFuelLevel() < 100) then
- -- refuel code
- local sackIndex = getItemIndex("supplementaries:sack")
- turtle.dig() -- digs one forward to be sure we can place the sack
- turtle.place(sackIndex) -- places the sack
- turtle.suck() -- sucks forward to get lava bucket
- turtle.refuel(1) -- refuels
- local bucketIndex = getItemIndex("minecraft:bucket")
- turtle.drop(bucketIndex)
- turtle.dig() -- breaks the sack and puts it back in the turtles inventory
- print("Refueled.")
- end
- return true
- end
- function getOrientation()
- loc1 = vector.new(gps.locate(2, false))
- if not turtle.forward() then
- for j=1,6 do
- if not turtle.forward() then
- turtle.dig()
- else
- break
- end
- end
- end
- loc2 = vector.new(gps.locate(2, false))
- heading = loc2 - loc1
- turtle.back()
- -- turtle.down()
- return ((heading.x + math.abs(heading.x) * 2) + (heading.z + math.abs(heading.z) * 3))
- end
- function turnToFaceHeading(heading, destinationHeading)
- if(heading > destinationHeading) then
- for t = 1, math.abs(destinationHeading - heading), 1 do
- turtle.turnLeft()
- end
- elseif(heading < destinationHeading) then
- for t = 1, math.abs(destinationHeading - heading), 1 do
- turtle.turnRight()
- end
- end
- end
- function setHeadingZ(zDiff, heading)
- local destinationHeading = heading
- if(zDiff < 0) then
- destinationHeading = 2
- elseif(zDiff > 0) then
- destinationHeading = 4
- end
- turnToFaceHeading(heading, destinationHeading)
- return destinationHeading
- end
- function setHeadingX(xDiff, heading)
- local destinationHeading = heading
- if(xDiff < 0) then
- destinationHeading = 1
- elseif(xDiff > 0) then
- destinationHeading = 3
- end
- turnToFaceHeading(heading, destinationHeading)
- return destinationHeading
- end
- function digAndMove(n)
- for x = 1, n, 1 do
- while(turtle.detect()) do
- turtle.dig()
- end
- turtle.forward()
- checkFuel()
- end
- end
- function digAndMoveDown(n)
- for y = 1, n, 1 do
- while(turtle.detectDown()) do
- turtle.digDown()
- end
- turtle.down()
- checkFuel()
- end
- end
- function digAndMoveUp(n)
- for y = 1, n, 1 do
- while(turtle.detectUp()) do
- turtle.digUp()
- end
- turtle.up()
- checkFuel()
- end
- end
- function moveTo(coords, heading)
- local currX, currY, currZ = gps.locate()
- print("CURR: "..currX..","..currY..","..currZ)
- local xDiff, yDiff, zDiff = coords.x - currX, coords.y - currY, coords.z - currZ
- print(string.format("Distances from start: %d %d %d", xDiff, yDiff, zDiff))
- -- -x = 1
- -- -z = 2
- -- +x = 3
- -- +z = 4
- -- Move to X start
- heading = setHeadingX(xDiff, heading)
- digAndMove(math.abs(xDiff))
- -- Move to Z start
- heading = setHeadingZ(zDiff, heading)
- digAndMove(math.abs(zDiff))
- -- Move to Y start
- if(yDiff < 0) then
- digAndMoveDown(math.abs(yDiff))
- elseif(yDiff > 0) then
- digAndMoveUp(math.abs(yDiff))
- end
- return heading
- end
- modem.transmit(ports.server,ports.turtle,"CLIENT_READY")
- event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message") -- server gives us key
- key = msg
- print("Recieved auth key "..key)
- os.sleep(0.5)
- -- parse info
- event, side, senderChannel, replyChannel, mineinfo, distance = os.pullEvent("modem_message") -- server gives us info
- local args = split(mineinfo," ")
- local withStorage = args[#args]
- withStorage = withStorage == "1" and true or false
- data = parseParams(mineinfo)
- checkFuel()
- local startCoords = data[1]
- local quarrySize = data[2]
- local finalHeading = moveTo(startCoords, getOrientation())
- local EAST_HEADING = 3
- --Turn to face North
- turnToFaceHeading(finalHeading, EAST_HEADING)
- finalHeading = EAST_HEADING
- --Now in Starting Position--
- --------------------------------START MINING CODE-----------------------------------------
- ------------------------------------------------------------------------------------------
- local DROPPED_ITEMS = {
- "minecraft:stone",
- "minecraft:dirt",
- "minecraft:cobblestone",
- "minecraft:sand",
- "minecraft:gravel",
- "minecraft:redstone",
- "minecraft:flint",
- "railcraft:ore_metal",
- "extrautils2:ingredients",
- "minecraft:dye",
- "thaumcraft:nugget",
- "thaumcraft:crystal_essence",
- "thermalfoundation:material",
- "projectred-core:resource_item",
- "deepresonance:resonating_ore",
- "forestry:apatite"
- }
- function dropItems()
- print("Purging Inventory...")
- for slot = 1, 16, 1 do
- local item = turtle.getItemDetail(slot)
- if(item ~= nil) then
- for filterIndex = 1, #DROPPED_ITEMS, 1 do
- if(item["name"] ~= "minecraft:coal_block" and item["name"] ~= "minecraft:coal" and item["name"] ~= "supplementaries:sack") then
- print("Dropping - " .. item["name"])
- turtle.select(slot)
- turtle.dropDown()
- end
- end
- end
- end
- end
- function manageInventory()
- dropItems()
- end
- function detectAndDig()
- while(turtle.detect()) do
- turtle.dig()
- end
- end
- function forward()
- detectAndDig()
- turtle.forward()
- end
- function leftTurn()
- turtle.turnLeft()
- detectAndDig()
- turtle.forward()
- turtle.turnLeft()
- end
- function rightTurn()
- turtle.turnRight()
- detectAndDig()
- turtle.forward()
- turtle.turnRight()
- end
- function dropTier(heading)
- turtle.turnRight()
- turtle.turnRight()
- turtle.digDown()
- turtle.down()
- return flipDirection(heading)
- end
- function flipDirection(heading)
- return ((heading + 1) % 4) + 1
- end
- function turnAround(tier, heading)
- if(tier % 2 == 1) then
- if(heading == 2 or heading == 3) then
- rightTurn()
- elseif(heading == 1 or heading == 4) then
- leftTurn()
- end
- else
- if(heading == 2 or heading == 3) then
- leftTurn()
- elseif(heading == 1 or heading == 4) then
- rightTurn()
- end
- end
- return flipDirection(heading)
- end
- function startQuary(width, height, depth, heading)
- for tier = 1, height, 1 do
- for col = 1, width, 1 do
- for row = 1, depth - 1, 1 do
- -- if(not checkFuel()) then
- -- print("Turtle is out of fuel, Powering Down...")
- -- return
- -- end
- checkFuel()
- forward()
- end
- if(col ~= width) then
- heading = turnAround(tier, heading)
- end
- manageInventory()
- end
- if(tier ~= height) then
- heading = dropTier(heading)
- end
- end
- return heading
- end
- print(quarrySize)
- finishedHeading = startQuary(quarrySize.x, quarrySize.y, quarrySize.z, finalHeading)
- --------------------------------START RETURN TRIP CODE------------------------------------
- ------------------------------------------------------------------------------------------
- function returnTo(coords, heading)
- local currX, currY, currZ = gps.locate()
- local xDiff, yDiff, zDiff = coords.x - currX, coords.y - currY, coords.z - currZ
- print(string.format("Distances from end: %d %d %d", xDiff, yDiff, zDiff))
- -- Move to Y start
- if(yDiff < 0) then
- digAndMoveDown(math.abs(yDiff))
- elseif(yDiff > 0) then
- digAndMoveUp(math.abs(yDiff))
- end
- -- Move to X start
- heading = setHeadingX(xDiff, heading)
- digAndMove(math.abs(xDiff))
- -- Move to Z start
- heading = setHeadingZ(zDiff, heading)
- digAndMove(math.abs(zDiff))
- return heading
- end
- returnTo(homeCoords,finishedHeading)
- turtle.dropDown(getItemIndex("supplementaries:sack"))
- os.sleep(1.5)
- modem.transmit(ports.deployer,ports.turtle, "BREAK_TURTLE")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement