From 2bfe5fcc6d44d8511a095290818dda4a12797487 Mon Sep 17 00:00:00 2001 From: Dominic Zimmer Date: Mon, 6 Jul 2020 19:09:36 +0200 Subject: [PATCH] intial commit --- mine/client.lua | 128 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 127 insertions(+), 1 deletion(-) diff --git a/mine/client.lua b/mine/client.lua index 8453cf2..9b93600 100644 --- a/mine/client.lua +++ b/mine/client.lua @@ -1,4 +1,5 @@ args = {...} +x, z, dir = 0, 0, 0 turtlestate = { name = os.getComputerLabel(), id = os.getComputerID(), @@ -442,4 +443,129 @@ function turtleAI() end end -turtleAI() +function updateState(update) + for k,v in pairs(update) do + turtlestate[k] = v + end + drawStatus() +end + +function enqueueForJob2() + updateState({ state = "Waiting for job..." }) + while true do + status, block = turtle.inspectDown() + if status and block["name"] == "computercraft:wired_modem_full" then + break + else + moveSafeForward() + end + end + turtle.turnLeft() + rednet.open("bottom") + local retrying = false + while true do + rednet.broadcast("gibjob", "jobs") + sender, message, proto = rednet.receive("newjob", 2) + if message then + job = tonumber(message) + return job + else + if not retrying then + retrying = true + --print("No job received. I will keep retrying.") + --turtlestate["state"] = "Waiting for job... (retrying)" + updateState({ state = "Waiting for job... (retrying)" }) + end + end + end +end + +function faceDir(dir) + if ((direction - dir) % 4 == 2) then + turtle.turnRight() + turtle.turnRight() + direction = (direction + 2) % 4 + elseif ((direction - dir) % 4 == 1) then + turtle.turnLeft() + direction = (direction -1) % 4 + elseif ((direction - dir) % 4 == 3) then + turtle.turnRight() + direction = (direction + 1) % 4 + end +end +-- directions: 0, 1, 2, 3 -> x, z, -x, -z +function goToCoordXZ(targetX, targetZ, extraaction) + if targetX > x then + faceDir(0) + while (targetX > x) do + moveSafe(turtle.forward, turtle.inspect, turtle.dig) + x += 1 + extraaction() + end + end + if targetX < x then + faceDir(2) + while (targetX < x) do + moveSafe(turtle.forward, turtle.inspect, turtle.dig) + x -= 1 + extraaction() + end + end + + if targetZ > z then + faceDir(1) + while (targetZ > z) do + moveSafe(turtle.forward, turtle.inspect, turtle.dig) + z += 1 + eztraaction() + end + end + if targetZ < z then + faceDir(3) + while (targetZ < z) do + moveSafe(turtle.forward, turtle.inspect, turtle.dig) + z -= 1 + eztraaction() + end + end +end + +function goToJob(job) + jobX, jobZ = iTo2Dcoords(job) -- this does all the magic + updateState({ state = "Going on the job...", morestate = " Jobsite is at ("..tostring(jobX)..", "..tostring(jobZ)..")" }) + -- move to hole + moveSafe(turtle.forward, turtle.inspect, noop) + moveSafe(turtle.forward, turtle.inspect, noop) + moveSafe(turtle.forward, turtle.inspect, noop) + moveSafe(turtle.forward, turtle.inspect, noop) + moveSafe(turtle.forward, turtle.inspect, noop) + moveSafe(turtle.forward, turtle.inspect, noop) + -- reset coords, descend + x, z = 0, 0 + moveSafe(turtle.down, turtle.inspectDown, noop) + moveSafe(turtle.down, turtle.inspectDown, noop) + -- go to job + function extraaction() + repeat sleep(0.6) until (not digSafe(turtle.inspectUp, turtle.digUp())) + end + goToCoord(jobX, jobZ, extraaction) +end + +function turtleAI2() + while true do + local job = enqueueForJob2() + goToJob(job) + -- returnToQueue() + break + end +end + +-- turtleAI() +faceDir(0) +faceDir(1) +faceDir(3) +faceDir(1) +faceDir(0) +faceDir(2) +faceDir(0) +goToCoordXZ(4, 2, noop)