Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --// Client
- local SERVER_PORT = 3230
- local CLIENT_PORT = 2020
- local modem = peripheral.wrap("left")
- modem.open(CLIENT_PORT)
- function MODEM_SEND(port,message)
- modem.transmit(port,CLIENT_PORT,message)
- end
- function getItemIndex(itemName)
- for slot = 1, SLOT_COUNT, 1 do
- local item = turtle.getItemDetail(slot)
- if(item ~= nil) then
- if(item["name"] == itemName) then
- return slot
- end
- end
- end
- end
- 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)
- parsed = {}
- params = split(data, " ")
- parsed[1] = vector.new(params[1], params[2], params[3])
- parsed[2] = vector.new(params[4], params[5], params[6])
- parsed[3] = tonumber(params[7])
- return (parsed)
- end
- function locateVector()
- local v = vector.new(gps.locate())
- v.x = v.x - 1
- v.y = v.y
- return v
- end
- function recalculateVector(vec)
- return vector.new(vec.x - 1, vec.y, vec.z)
- end
- function refuel()
- if turtle.getFuelLevel == 0 then
- turtle.select(1)
- turtle.placeUp()
- turtle.suckUp(1)
- turtle.select(getItemIndex("minecraft:lava_bucket"))
- turtle.refuel()
- turtle.dropUp()
- turtle.digUp()
- print("Refuel complete")
- end
- 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.down()
- 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 mineLayer()
- turtle.dig()
- turtle.forward()
- turtle.digUp()
- refuel()
- turtle.turnRight()
- turtle.dig()
- turtle.forward()
- turtle.digUp()
- turtle.back()
- turtle.turnLeft()
- end
- function digAndMove(n)
- for x = 1, n, 1 do
- while(turtle.detect()) do
- turtle.dig()
- end
- turtle.forward()
- refuel()
- end
- end
- function digAndMoveDown(n)
- for y = 1, n, 1 do
- print(y)
- while(turtle.detectDown()) do
- turtle.digDown()
- end
- turtle.down()
- refuel()
- end
- end
- function moveTo(coords, heading)
- MODEM_SEND(SERVER_PORT,"BotMoving")
- 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)
- 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_SEND(SERVER_PORT,"BotOnline")
- print("Bot Online")
- event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
- print("Got message")
- local parsedData = parseParams(msg)
- local startPos = parsedData[1]
- local toPos = parsedData[2]
- local tunnelLen = parsedData[3]
- --local heading = calculateHeading()
- print(string.format("To position: %d %d %d",toPos.x, toPos.y, toPos.z))
- local finalHeading = moveTo(toPos, getOrientation())
- local SOUTH_HEADING = 4
- --Turn to face South
- turnToFaceHeading(finalHeading, SOUTH_HEADING)
- finalHeading = SOUTH_HEADING
- --Now in Starting Position--
- for i = 1,tunnelLen+0,1 do
- print("Mining layer")
- mineLayer()
- end
- print("Mine Finished.")
Advertisement
Add Comment
Please, Sign In to add comment