Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local pickSlot = 13
- local modemSlot = 14
- local chestSlot = 16
- local pickName = "minecraft:diamond_pickaxe"
- local modemName = "computercraft:wireless_modem_advanced"
- local minFuel = turtle.getFuelLimit() * 0.25
- local maxFuel = turtle.getFuelLimit() * 0.75
- local validFuels = {
- ["minecraft:coal_block"] = true,
- ["minecraft:coal"] = true,
- ["minecraft:charcoal"] = true,
- ["minecraft:blaze_rod"] = true
- }
- sleep(1)--might need to increase to prevent server crash
- if fs.exists("DiscordHook.lua") == false then
- shell.execute("wget", "https://raw.githubusercontent.com/Wendelstein7/DiscordHook-CC/master/DiscordHook.lua", "DiscordHook.lua")
- end
- local DiscordHook = require("./DiscordHook")
- local hookSuccess, hook = DiscordHook.createWebhook("https://discord.com/api/webhooks/890291858431959120/1tn7J_aOFVJfxIqSwds0r2GvfgPyrzlnq6XE8TDPL10AfO0bx9GFaeGuRw6Q_fSpOGOG")
- if not hookSuccess then
- error("Webhook connection failed! Reason: " .. hook)
- end
- function Hold(s)
- if turtle.getItemCount(s) == 1 then
- turtle.select(s)
- turtle.equipLeft()
- local i = turtle.getItemDetail()
- if i ~= nil then
- if i.name == pickName then
- turtle.transferTo(pickSlot)
- elseif i.name == geoName then
- turtle.transferTo(geoSlot)
- elseif i.name == modemName then
- turtle.transferTo(modemSlot)
- else
- turtle.transferTo(1)
- end
- end
- turtle.select(1)
- end
- end
- local facing = "unknown"
- local pos = "unknown"
- function Forward()
- while turtle.forward() == false do
- turtle.dig()
- end
- if facing == "south" then
- pos["z"] = pos["z"] + 1
- elseif facing == "north" then
- pos["z"] = pos["z"] - 1
- elseif facing == "east" then
- pos["x"] = pos["x"] + 1
- elseif facing == "west" then
- pos["x"] = pos["x"] - 1
- end
- print("Pos: " .. tostring(pos["x"]) .. ", " .. tostring(pos["y"]) .. ", " .. tostring(pos["z"]))
- end
- function Up()
- while turtle.up() == false do
- turtle.digUp()
- end
- pos["y"] = pos["y"] + 1
- print("Pos: " .. tostring(pos["x"]) .. ", " .. tostring(pos["y"]) .. ", " .. tostring(pos["z"]))
- end
- function Down()
- while turtle.down() == false do
- turtle.digDown()
- end
- pos["y"] = pos["y"] - 1
- print("Pos: " .. tostring(pos["x"]) .. ", " .. tostring(pos["y"]) .. ", " .. tostring(pos["z"]))
- end
- function Dig()
- while turtle.detect() do
- turtle.dig()
- end
- end
- function DigUp()
- while turtle.detectUp() do
- turtle.digUp()
- end
- end
- function DigDown()
- while turtle.detectDown() do
- turtle.digDown()
- end
- end
- function GetIndex(v, t)
- for tk, tv in pairs(t) do
- if tv == v then
- return tk
- end
- end
- end
- local dirs = {
- [1] = "north",
- [2] = "east",
- [3] = "south",
- [4] = "west"
- }
- function Left()
- turtle.turnLeft()
- local cd = GetIndex(facing, dirs)
- nd = cd - 1
- if nd == 0 then
- nd = 4
- end
- print("Facing " .. dirs[nd])
- facing = dirs[nd]
- end
- function Right()
- turtle.turnRight()
- local cd = GetIndex(facing, dirs)
- nd = cd + 1
- if nd == 5 then
- nd = 1
- end
- print("Facing " .. dirs[nd])
- facing = dirs[nd]
- end
- function Face(nd)
- local cd = GetIndex(facing, dirs)
- local td = GetIndex(nd, dirs)
- print("Currently facing: " .. facing .. " " .. tostring(cd))
- print("Target facing: " .. nd .. " " .. tostring(td))
- facing = nd
- if cd == td then
- print("Already facing " .. nd)
- return
- end
- if cd == 1 and td == 4 then
- turtle.turnLeft()
- print("Facing " .. nd)
- return
- end
- if cd == 4 and td == 1 then
- turtle.turnRight()
- print("Facing " .. nd)
- return
- end
- if cd < td then
- for i = cd, td - 1 do
- turtle.turnRight()
- end
- end
- if cd > td then
- for i = cd, td + 1, -1 do
- turtle.turnLeft()
- end
- end
- print("Facing " .. nd)
- end
- function Inv()
- Hold(pickSlot)
- DigUp()
- turtle.select(16)
- while turtle.placeUp() == false do
- DigUp()
- turtle.attackUp()
- end
- local c = peripheral.wrap("top")
- if turtle.getFuelLevel() < minFuel then
- local fuelAttempts = 0
- turtle.select(1)
- while turtle.getFuelLevel() < maxFuel do
- local cl = c.list()
- print(cl[1].name)
- if cl[1] ~= nil and validFuels[cl[1].name] then
- turtle.suckUp(cl[1].count - 1)
- turtle.refuel(64)
- else
- print("Waiting for fuel")
- end
- sleep(1)
- fuelAttempts = fuelAttempts + 1
- if fuelAttempts > 10 and turtle.getFuelLevel() > minFuel then
- print("Will try get more fuel later")
- break
- end
- end
- end
- turtle.select(16)
- Hold(pickSlot)
- turtle.digUp()
- turtle.select(1)
- end
- function GetDir()
- Hold(modemSlot)
- local sx, sy, sz = gps.locate()
- while sx == nil do
- print("Coudn't get GPS")
- sleep(3)
- sx, sy, sz = gps.locate()
- end
- Hold(pickSlot)
- Forward()
- Hold(modemSlot)
- local nx, ny, nz = gps.locate()
- while nx == nil do
- print("Coudn't get GPS")
- sleep(3)
- nx, ny, nz = gps.locate()
- end
- turtle.turnLeft()
- turtle.turnLeft()
- Hold(pickSlot)
- Forward()
- turtle.turnLeft()
- turtle.turnLeft()
- if nz > sz then
- facing = "south"
- elseif nz < sz then
- facing = "north"
- elseif nx > sx then
- facing = "east"
- elseif nx < sx then
- facing = "west"
- end
- print("Facing: " .. facing)
- end
- function GetPos()
- Hold(modemSlot)
- local cx, cy, cz = gps.locate()
- pos = {}
- pos["x"] = cx
- pos["y"] = cy
- pos["z"] = cz
- print("Pos: " .. tostring(cx) .. ", " .. tostring(cy) .. ", " .. tostring(cz))
- end
- ---
- if turtle.getItemCount(16) == 0 then
- Hold(pickSlot)
- turtle.select(16)
- turtle.digUp()
- end
- Inv()
- GetPos()
- GetDir()
- function GotoRel(rx, ry, rz)
- print("Going to rel: " .. tostring(rx) .. ", " .. tostring(ry) .. ", " .. tostring(rz))
- Hold(pickSlot)
- if ry >= 0 then
- for i = 1, ry do
- Up()
- end
- else
- for i = -1, ry, -1 do
- Down()
- end
- end
- if rx > 0 then
- dir = Face("east")
- for i = 1, rx do
- Forward()
- end
- elseif rx < 0 then
- dir = Face("west")
- for i = -1, rx, -1 do
- Forward()
- end
- end
- if rz > 0 then
- dir = Face("south")
- for i = 1, rz do
- Forward()
- end
- elseif rz < 0 then
- dir = Face("north")
- for i = -1, rz, -1 do
- Forward()
- end
- end
- end
- function Goto(x, y, z)
- print("Going to: " .. tostring(x) .. ", " .. tostring(y) .. ", " .. tostring(z))
- GotoRel(x - pos["x"], y - pos["y"], z - pos["z"])
- end
- function CheckSelf()
- Hold(pickSlot)
- if turtle.getItemCount(6) > 0 or turtle.getFuelLevel() < minFuel then
- Inv()
- end
- turtle.select(1)
- end
- while true do
- Hold(modemSlot)
- rednet.open("left")
- print("Waiting for command")
- hook.send("Waiting for command")
- local id, msg = rednet.receive()
- print("Received command: " .. tostring(msg["x"]) .. " " .. tostring(msg["y"]) .. " " .. tostring(msg["z"]))
- hook.send("Received command: " .. tostring(msg["x"]) .. " " .. tostring(msg["y"]) .. " " .. tostring(msg["z"]))
- Goto(msg["x"], msg["y"], msg["z"])
- turtle.select(1)
- turtle.placeDown()
- print("Finished, waiting for next command")
- hook.send("Finished, waiting for next command")
- end
Add Comment
Please, Sign In to add comment