Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print("Starting up client")
- local redSide = "left"
- local redProtocol = "mine"
- local gui = false
- local gx, gy, gz = gps.locate(10)
- gz = gz - 1
- local direction = "north"
- --Returns true value if all requitements are met
- function verify()
- --verifing coordinates are not empty
- if gps.locate() == nil then
- return false
- end
- return true
- end
- --Massive functions for turning the turtle using Compass Measurements
- function lookNorth()
- if direction ~= "north" then
- if direction == "east" then
- turtle.turnLeft()
- elseif direction == "south" then
- turtle.turnLeft()
- turtle.turnLeft()
- elseif direction == "west" then
- turtle.turnRight()
- end
- end
- direction = "north"
- end
- function lookSouth()
- if direction ~= "south" then
- if direction == "west" then
- turtle.turnLeft()
- elseif direction == "north" then
- turtle.turnLeft()
- turtle.turnLeft()
- elseif direction == "east" then
- turtle.turnRight()
- end
- end
- direction = "south"
- end
- function lookEast()
- if direction ~= "east" then
- if direction == "south" then
- turtle.turnLeft()
- elseif direction == "west" then
- turtle.turnLeft()
- turtle.turnLeft()
- elseif direction == "north" then
- turtle.turnRight()
- end
- end
- direction = "east"
- end
- function lookWest()
- if direction ~= "west" then
- if direction == "north" then
- turtle.turnLeft()
- elseif direction == "east" then
- turtle.turnLeft()
- turtle.turnLeft()
- elseif direction == "south" then
- turtle.turnRight()
- end
- end
- direction = "west"
- end
- --Compass Functions ends
- function sortInventory()
- local slots = {}
- for i = 1, 16, 1 do
- if turtle.getItemDetail(i) ~= nil then
- slots[i] = turtle.getItemDetail(i).name
- end
- end
- for c = 1, 16, 1 do
- if slots[c] ~= nil then
- for s = 1, 16, 1 do
- if slots[s] ~= nil then
- if slots[c] == slots[s] and turtle.getItemCount(c) ~= 64 and turtle.getItemCount(s) ~= 64 then
- turtle.select(s)
- turtle.transferTo(c)
- if turtle.getItemCount(s) == 0 then
- slots[s] = nil
- end
- end
- end
- end
- end
- end
- for c = 1, 16, 1 do
- if slots[c] == nil then
- for s = 1, 16, 1 do
- if s > c and slots[s] ~= nil then
- turtle.select(s)
- turtle.transferTo(c)
- slots[c] = slots[s]
- slots[s] = nil
- break
- end
- end
- end
- end
- end
- --Checks to see if there is any space left in the turtle and returns true if space left are false if no space is left
- function checkSpace()
- --True is empty and false is full
- local slots = {}
- for i = 1, 16, 1 do
- if turtle.getItemDetail(i) ~= nil then
- if turtle.getItemDetail(i).name == "minecraft:cobblestone" or turtle.getItemDetail() == "minecraft:dirt" then
- turtle.select(i)
- turtle.drop()
- slots[i] = true
- else
- slots[i] = false
- end
- else
- slots[i] = true
- end
- end
- for _, slot in pairs(slots) do
- if slot == true then
- return true
- end
- end
- return false
- end
- function goForward(steps)
- for i = 1, steps, 1 do
- while turtle.detect() == true do
- turtle.dig()
- end
- turtle.forward()
- end
- end
- function goDown(steps)
- for i = 1, steps, 1 do
- if turtle.detectDown() == true then
- turtle.digDown()
- end
- turtle.down()
- end
- end
- function goUp(steps)
- for i = 1, steps, 1 do
- if turtle.detectUp() == true then
- turtle.digUp()
- end
- turtle.up()
- end
- end
- function getCoalLevel(distance)
- local distanceNeeded = (distance - turtle.getFuelLevel())
- if distanceNeeded > turtle.getFuelLevel() then
- print(string.format("I have %f of fuel I'm requesting %f more!", turtle.getFuelLevel(), distanceNeeded))
- local refuled = false
- while refuled do
- for i = 1, 16, 1 do
- local coalNeeded = math.ceil(distanceNeeded / (80 * turtle.getItemCount(i)))
- if turtle.getItemDetail(i) ~= nil then
- if turtle.getItemDetail(i).name == "minecraft:coal" then
- if turtle.getItemCount(i) >= coalNeeded then
- turtle.select(i)
- turtle.refuel(coalNeeded)
- refuled = true
- else
- print("There is not enough coal to continue please place the coal into the slot")
- end
- end
- end
- end
- print("Waiting 5 seconds")
- sleep(5)
- end
- end
- end
- --Directs the turtle to it's corresponding given coordinates
- function gotoCoord(x, y, z)
- --Calculating Distance
- local distance = math.abs(x - gx) + math.abs(y - gy) + math.abs(z - gz)
- getCoalLevel(distance)
- print("Going To (" .. x .. ", " .. y .. ", " .. z .. ")")
- print("I am at (" .. gx .. ", " .. gy .. ", " .. gz .. ")")
- --Turtle is always placed north
- --North is negative z
- --South is positive z
- --West is negative x
- --East is postive x
- if gz > z then
- lookNorth()
- elseif gz < z then
- lookSouth()
- end
- goForward(math.abs(z - gz))
- if gx > x then
- lookWest()
- else
- lookEast()
- end
- goForward(math.abs(x - gx))
- if gy > y then
- goDown(math.abs(y - gy))
- else
- goUp(math.abs(y - gy))
- end
- lookNorth()
- if checkSpace() == false then
- sortInventory()
- if checkSpace() == false then
- print("Need to Surface!")
- end
- end
- end
- function getConnection()
- rednet.open("left")
- if rednet.isOpen() then
- while true do
- rednet.broadcast(string.format("%f,%f,%f", gx, gy, gz), "mine")
- local id, message = rednet.receive("mine")
- local command, index = {}, 0
- for i in string.gmatch(message, "([^" .. "," .. "]+)") do
- command[index] = i
- index = index + 1
- end
- --command, x, y, z
- print(tonumber(command[1]), tonumber(command[2]), tonumber(command[3]))
- gotoCoord(tonumber(command[1]), tonumber(command[2]), tonumber(command[3]))
- gx, gy, gz = gps.locate(100)
- end
- end
- end
- getConnection()
Add Comment
Please, Sign In to add comment