Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --the codes for the ports
- local SERVER_PORT = 568
- local CLIENT_PORT = 342
- local INVENTORY_SLOT = 1
- local FUEL_SLOT = 2
- --The number of slots in the turtle
- local SLOT_COUNT = 16
- --To help with turning while mining
- local RIGHT = 0
- local LEFT = 1
- --we always turn right first while digging
- local turnDirection = 0
- --split a string message into a table of strings
- --inputstr is the sting to be separated
- --sep is the string that will be found between sections
- 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
- --the data is the
- --put the string data into the coords array
- 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 checkFuel()
- while(turtle.getFuelLevel() < 100) do
- --selectchest
- turtle.select(FUEL_SLOT)
- --make space for chest
- turtle.digUp()
- --place chest
- turtle.placeUp()
- --get lava bucket
- turtle.suckUp()
- --refuel
- turtle.refuel()
- --put bucket back
- turtle.dropUp()
- --get chest back
- turtle.digUp()
- end
- end
- function checkInventory()
- --we deposit our inventory if we have extra chests or if the inventory is almost full
- if ((turtle.getItemCount(SLOT_COUNT - 1) > 0) or (turtle.getItemCount(FUEL_SLOT) > 1) or (turtle.getItemCount(INVENTORY_SLOT) > 1)) then
- --select the chest
- turtle.select(INVENTORY_SLOT)
- --make space for the chest
- turtle.digUp()
- --place chest
- turtle.placeUp()
- --place everything except chests in the chest
- for i = 1, SLOT_COUNT, 1 do
- --we want to deposit everything except the inventory and fuel chest
- --We know that one inventory chest is above the turtle so we can deposit any extras
- --We need to make sure we keep at least on fuel chest
- if i == FUEL_SLOT then
- --drop evertyihing in the slot except one
- turtle.dropUp(turtle.getItemCount(FUEL_SLOT) - 1)
- else
- --for every slot except FUEL_SLOT we want to deposit everything
- turtle.dropUp()
- end
- end
- --get the chest back
- turtle.digUp()
- end
- end
- -------------------- Movement Functions -------------------------
- --turn the turtle around
- function turnAround(heading)
- turtle.turnRight()
- turtle.turnRight()
- return ((heading + 1) % 4) + 1
- end
- --Dig the forward block and move forward
- function forward()
- checkFuel()
- while(turtle.detect()) do
- turtle.dig()
- end
- turtle.forward()
- checkInventory()
- end
- --Dig the forward block, move forward,
- --dig up if digSize is 2 or more
- --dig down if digSize is 3 or more
- function dig(digSize)
- forward()
- if digSize >= 2 then
- while(turtle.detectUp()) do
- turtle.digUp()
- end
- end
- if digSize >= 3 then
- while(turtle.detectDown()) do
- turtle.digDown()
- end
- end
- checkInventory()
- end
- -- do a digging u turn to the left
- function turnLeft(digSize, heading)
- turtle.turnLeft()
- dig(digSize)
- turtle.turnLeft()
- return ((heading + 1) % 4) + 1
- end
- --do a digging u turn to the right
- function turnRight(digSize, heading)
- turtle.turnRight()
- dig(digSize)
- turtle.turnRight()
- return ((heading + 1) % 4) + 1
- end
- --turn the correct direction while digging
- function digTurn(digSize, heading)
- if turnDirection == RIGHT then
- heading = turnRight(digSize, heading)
- else
- heading = turnLeft(digSize, heading)
- end
- turnDirection = (turnDirection + 1) % 2
- return heading
- end
- --turn to a heading to go a specific direction
- 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
- --find the orientation of the turtle
- --then move into position to start dig
- --returns the Heading
- function getOrientation()
- loc1 = vector.new(gps.locate(2, false))
- forward()
- loc2 = vector.new(gps.locate(2, false))
- heading = loc2 - loc1
- turtle.down()
- turtle.down()
- return (heading.x + math.abs(heading.x) * 2) + (heading.z + math.abs(heading.z) * 3)
- end
- --turn to the heading needed to move in z direction
- 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
- --turn to the heading needed to move in x direction
- 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
- --move dig and move the specified amount
- function digAndMove(n, digSize)
- for x = 1, n, 1 do
- dig(digSize)
- end
- end
- function move(n)
- for x = 1, n, 1 do
- forward()
- end
- end
- --move turle up
- function MoveDown(n)
- for y = 1, n, 1 do
- while(turtle.detectDown()) do
- turtle.digDown()
- end
- turtle.down()
- checkFuel()
- end
- end
- --move turtledown
- function MoveUp(n)
- for y = 1, n, 1 do
- while(turtle.detectUp()) do
- turtle.digUp()
- end
- turtle.up()
- checkFuel()
- end
- end
- --move to a specific coordinate
- function moveTo(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 start: %d %d %d", xDiff, yDiff, zDiff))
- -- -x = 1
- -- -z = 2
- -- +x = 3
- -- +z = 4
- -- Move to X start
- heading = setHeadingX(xDiff, heading)
- move(math.abs(xDiff))
- -- Move to Z start
- heading = setHeadingZ(zDiff, heading)
- move(math.abs(zDiff))
- -- Move to Y start
- if yDiff < 0 then
- MoveDown(math.abs(yDiff))
- elseif yDiff > 0 then
- MoveUp(math.abs(yDiff))
- end
- return heading
- end
- --------------------------------START BEGIN TRIP CODE-------------------------
- --connect to the modem on the turtle
- local modem = peripheral.wrap("left")
- modem.open(CLIENT_PORT)
- --tell the server we are placed
- modem.transmit(SERVER_PORT, CLIENT_PORT, "CLIENT_DEPLOYED")
- --we should receive the fuel and deposit chest right now
- --get mining coords from server
- event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
- data = parseParams(msg)
- --get Fuel
- checkFuel()
- --Go to starting Postion
- local startCoords = data[1]
- local currentHeading = moveTo(startCoords, getOrientation())
- --Turn to face North
- local NORTH_HEADING = 2
- turnToFaceHeading(currentHeading, NORTH_HEADING)
- currentHeading = NORTH_HEADING
- --Now in Starting Position--
- --------------------------------START MINING CODE------------------------------
- --function for mining a quarry
- function startQuary(width, height, length, heading)
- --define the dig size
- local digSize = 3
- --repeat for each tier in the dig
- for tier = 1, height, 3 do
- --define the dig size
- digSize = 3
- if((height - (tier - 1)) < digSize) then
- digSize = (height - (tier - 1))
- end
- --we need to move differently for the first tier
- if tier == 1 then
- --clear blocks in starting path
- if digSize == 3 then
- moveDown(2)
- moveUp(1)
- elseif digSize == 2 then
- moveDown(1)
- end
- else
- --move to next tier
- if digSize == 3 then
- moveDown(4)
- moveUp(1)
- elseif digSize == 2 then
- moveDown(3)
- else
- moveDown(2)
- end
- end
- --for each row j
- for j = 1, width, 1 do
- --dig the length of the dig not including the block we are already in
- digAndMove( (length-1) , digSize)
- --we don't want to turn if it is the last row
- if(j ~= width) then
- heading = digTurn(digSize, heading)
- end
- end
- --turn around
- heading = turnAround(heading)
- end
- return heading
- end
- --get the size of the quarry from the server message
- local quary = data[2]
- --start dig
- currentHeading = startQuary(quary.x, quary.y, quary.z, currentHeading)
- --------------------------------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
- MoveDown(math.abs(yDiff))
- elseif yDiff > 0 then
- MoveUp(math.abs(yDiff))
- end
- -- Move to X start
- heading = setHeadingX(xDiff, heading)
- move(math.abs(xDiff))
- -- Move to Z start
- heading = setHeadingZ(zDiff, heading)
- move(math.abs(zDiff))
- return heading
- end
- endCoords = data[3]
- returnTo(endCoords, currentHeading)
- local timoutWait = 60
- for i = 1, timoutWait, 1 do
- os.sleep(1)
- print(string.format( "Waiting for brothers %d/%d", i, timoutWait))
- end
- modem.transmit(SERVER_PORT, CLIENT_PORT, "end")
Add Comment
Please, Sign In to add comment