From 96dc469e4bdf33f80435b9bb9211c070e4d919b2 Mon Sep 17 00:00:00 2001 From: Dominic Zimmer Date: Mon, 6 Jul 2020 00:51:43 +0200 Subject: [PATCH 01/17] Prettyprint <3 --- dominic/canemine.lua | 58 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/dominic/canemine.lua b/dominic/canemine.lua index 01e8738..847e0d8 100644 --- a/dominic/canemine.lua +++ b/dominic/canemine.lua @@ -1,4 +1,11 @@ args = {...} +status = { + name = os.getComputerLabel(), + id = os.getComputerID(), + fuel = "", + state = "init", + job = nil +} function noop() end @@ -143,7 +150,7 @@ function digShaft() end function digTo(x, z) - print("Digging to rel. coord ("..tostring(x)..", "..tostring(z)..")") + --print("Digging to rel. coord ("..tostring(x)..", "..tostring(z)..")") while z > 0 do moveSafe(turtle.forward, turtle.inspect, turtle.dig) z = z - 1 @@ -304,7 +311,7 @@ function goToJob() end function waitForJob() - print("Waiting for job...") + --print("Waiting for job...") while true do status, block = turtle.inspectDown() if status and block["name"] == "computercraft:wired_modem_full" then @@ -320,12 +327,12 @@ function waitForJob() sender, message, proto = rednet.receive("newjob", 2) if message then job = tonumber(message) - print("Received job "..tostring(job)) + --print("Received job "..tostring(job)) return job else if not retrying then retrying = true - print("No job received. I will keep retrying.") + --print("No job received. I will keep retrying.") end end end @@ -340,22 +347,61 @@ function emptyToBarrel() end end +function drawStatus() + print("Turtle ("..status["id"]..")") + print() + print(" \""..status["name"].."\"") + print() + print() + if status["job"] then + print("Current job: "..status["job"]) + else + print("Current job: ") + end + print() + print("Last job fuel consumption: "..status["fuel"]) + print() + print("Status:") + print(" "..status["state"]) + print() + print() +end + while true do + status["state"] = "Waiting for job..." + drawStatus() thejob = waitForJob() + status["job"] = thejob + status["state"] = "Refueling..." + drawStatus() doRefuel() fuel = turtle.getFuelLevel() + status["state"] = "Headed to the jobsite..." + drawStatus() goToJob() x, z = ithSpot(thejob) digTo(x,z) + status["state"] = "Digging the Shaft..." + drawStatus() digShaft() + status["state"] = "Returning to the station..." + drawStatus() goToSpawn(x, z) path = math.floor(math.random() + 0.5) + status["state"] = "Unloading Inventory..." + drawStatus() goToBarrel(path) emptyToBarrel() leaveBarrel(path) fuelconsumed = fuel - turtle.getFuelLevel() - print("I consumed "..tostring(fuelconsumed).." fuel") - print("Reporting Duty") + --print("I consumed "..tostring(fuelconsumed).." fuel") + --print("Reporting Duty") + status["fuel"] = fuelconsumed + status["state"] = "Reporting duty..." + drawStatus() reportDuty(thejob, fuelconsumed) + status["job"] = nil + status["state"] = "Going back in line..." + drawStatus() enqueueTurtle() end From f45eefd932ba9049e2bc56467bf7fd58d3095656 Mon Sep 17 00:00:00 2001 From: Dominic Zimmer Date: Mon, 6 Jul 2020 00:55:51 +0200 Subject: [PATCH 02/17] Fix? --- dominic/canemine.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/dominic/canemine.lua b/dominic/canemine.lua index 847e0d8..03fe1b9 100644 --- a/dominic/canemine.lua +++ b/dominic/canemine.lua @@ -354,12 +354,16 @@ function drawStatus() print() print() if status["job"] then - print("Current job: "..status["job"]) + print("Current job: "..tostring(status["job"])) else print("Current job: ") end print() - print("Last job fuel consumption: "..status["fuel"]) + if status["job"] then + print("Last job fuel consumption: "..tostring(status["fuel"])) + else + print("Last job fuel consumption: ") + end print() print("Status:") print(" "..status["state"]) From 268c8679e11b31180768a8adba3fcde31713c154 Mon Sep 17 00:00:00 2001 From: Dominic Zimmer Date: Mon, 6 Jul 2020 00:57:07 +0200 Subject: [PATCH 03/17] Fix? --- dominic/canemine.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dominic/canemine.lua b/dominic/canemine.lua index 03fe1b9..7de921b 100644 --- a/dominic/canemine.lua +++ b/dominic/canemine.lua @@ -375,7 +375,7 @@ while true do status["state"] = "Waiting for job..." drawStatus() thejob = waitForJob() - status["job"] = thejob + --status["job"] = thejob status["state"] = "Refueling..." drawStatus() doRefuel() From 766f72171d7194fe2d94fd2a6de09ed8d5a1a743 Mon Sep 17 00:00:00 2001 From: Dominic Zimmer Date: Mon, 6 Jul 2020 00:58:42 +0200 Subject: [PATCH 04/17] Fix? --- dominic/canemine.lua | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/dominic/canemine.lua b/dominic/canemine.lua index 7de921b..f26b5b5 100644 --- a/dominic/canemine.lua +++ b/dominic/canemine.lua @@ -1,5 +1,5 @@ args = {...} -status = { +turtlestatus = { name = os.getComputerLabel(), id = os.getComputerID(), fuel = "", @@ -348,51 +348,51 @@ function emptyToBarrel() end function drawStatus() - print("Turtle ("..status["id"]..")") + print("Turtle ("..turtlestatus["id"]..")") print() - print(" \""..status["name"].."\"") + print(" \""..turtlestatus["name"].."\"") print() print() - if status["job"] then - print("Current job: "..tostring(status["job"])) + if turtlestatus["job"] then + print("Current job: "..tostring(turtlestatus["job"])) else print("Current job: ") end print() - if status["job"] then - print("Last job fuel consumption: "..tostring(status["fuel"])) + if turtlestatus["job"] then + print("Last job fuel consumption: "..tostring(turtlestatus["fuel"])) else print("Last job fuel consumption: ") end print() print("Status:") - print(" "..status["state"]) + print(" "..turtlestatus["state"]) print() print() end while true do - status["state"] = "Waiting for job..." + turtlestatus["state"] = "Waiting for job..." drawStatus() thejob = waitForJob() - --status["job"] = thejob - status["state"] = "Refueling..." + turtlestatus["job"] = thejob + turtlestatus["state"] = "Refueling..." drawStatus() doRefuel() fuel = turtle.getFuelLevel() - status["state"] = "Headed to the jobsite..." + turtlestatus["state"] = "Headed to the jobsite..." drawStatus() goToJob() x, z = ithSpot(thejob) digTo(x,z) - status["state"] = "Digging the Shaft..." + turtlestatus["state"] = "Digging the Shaft..." drawStatus() digShaft() - status["state"] = "Returning to the station..." + turtlestatus["state"] = "Returning to the station..." drawStatus() goToSpawn(x, z) path = math.floor(math.random() + 0.5) - status["state"] = "Unloading Inventory..." + turtlestatus["state"] = "Unloading Inventory..." drawStatus() goToBarrel(path) emptyToBarrel() @@ -400,12 +400,12 @@ while true do fuelconsumed = fuel - turtle.getFuelLevel() --print("I consumed "..tostring(fuelconsumed).." fuel") --print("Reporting Duty") - status["fuel"] = fuelconsumed - status["state"] = "Reporting duty..." + turtlestatus["fuel"] = fuelconsumed + turtlestatus["state"] = "Reporting duty..." drawStatus() reportDuty(thejob, fuelconsumed) - status["job"] = nil - status["state"] = "Going back in line..." + turtlestatus["job"] = nil + turtlestatus["state"] = "Going back in line..." drawStatus() enqueueTurtle() end From 2975af1945b2ba66f61eaa7ae2a17561f002b3d1 Mon Sep 17 00:00:00 2001 From: Dominic Zimmer Date: Mon, 6 Jul 2020 01:00:21 +0200 Subject: [PATCH 05/17] Nazipropaganda --- dominic/canemine.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dominic/canemine.lua b/dominic/canemine.lua index f26b5b5..b804762 100644 --- a/dominic/canemine.lua +++ b/dominic/canemine.lua @@ -348,7 +348,7 @@ function emptyToBarrel() end function drawStatus() - print("Turtle ("..turtlestatus["id"]..")") + print("Turtle ("..tostring(turtlestatus["id"])..")") print() print(" \""..turtlestatus["name"].."\"") print() @@ -368,7 +368,6 @@ function drawStatus() print("Status:") print(" "..turtlestatus["state"]) print() - print() end while true do From b54d35309e931a6501d52f251c4b2bc9030ef54b Mon Sep 17 00:00:00 2001 From: Dominic Zimmer Date: Mon, 6 Jul 2020 01:05:49 +0200 Subject: [PATCH 06/17] Aaaand deploy! --- dominic/canemine.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dominic/canemine.lua b/dominic/canemine.lua index b804762..96da3f8 100644 --- a/dominic/canemine.lua +++ b/dominic/canemine.lua @@ -333,6 +333,7 @@ function waitForJob() if not retrying then retrying = true --print("No job received. I will keep retrying.") + turtlestatus["state"] = "Waiting for job... (retrying)" end end end @@ -360,9 +361,9 @@ function drawStatus() end print() if turtlestatus["job"] then - print("Last job fuel consumption: "..tostring(turtlestatus["fuel"])) + print("Recent fuel usage: "..tostring(turtlestatus["fuel"])) else - print("Last job fuel consumption: ") + print("Recent fuel usage: ") end print() print("Status:") From e26f22514e632f22d4bb80c345428187f00c6714 Mon Sep 17 00:00:00 2001 From: Dominic Zimmer Date: Mon, 6 Jul 2020 14:46:46 +0200 Subject: [PATCH 07/17] Migrate turtle client and server to /mine --- mine/client.lua | 445 ++++++++++++++++++++++++++++++++++++++++++++++++ mine/server.lua | 46 +++++ 2 files changed, 491 insertions(+) create mode 100644 mine/client.lua create mode 100644 mine/server.lua diff --git a/mine/client.lua b/mine/client.lua new file mode 100644 index 0000000..8453cf2 --- /dev/null +++ b/mine/client.lua @@ -0,0 +1,445 @@ +args = {...} +turtlestate = { + name = os.getComputerLabel(), + id = os.getComputerID(), + fuel = "", + state = "init", + job = nil +} + +function noop() end + +function pair(x, y) + return math.floor((x + y) * (x + y + 1)/2 + y) +end + +function unpairX(z) + local j = math.floor(math.sqrt(0.25 + 2*z) - 0.5) + return j - (z - j*(j+1)/2) +end + + +function unpairZ(z) + local j = math.floor(math.sqrt(0.25 + 2*z) - 0.5) + return z - j * (j+1)/2; +end + +lookup = {4, 1, 3, 0, 2} +local function isDiggingSpot(x, y) + local val = lookup[(y % 5) + 1] + return (x % 5) == val +end + +offset = 10 +function ithSpot(i) + j = offset + while true do + x, z = unpairX(j), unpairZ(j) + if isDiggingSpot(x, z) then + if i <= 1 then + return x, z + else + i = i - 1 + end + end + j = j + 1 + end +end + +function adjustToQuadrant(i, x, z) + if i % 4 == 0 then + return x, z + elseif i % 4 == 1 then + return -z-1, x + elseif i % 4 == 2 then + return z, -x-1 + elseif i % 4 == 3 then + return -z-1, -x-1 + end +end + +function iTo2Dcoords(j) + i = j // 4 + quadrantI = j % 4 -- 0-3 quadrant + counter = 0 -- number of digging spots found + j = 0 -- enumerate 2D plane + while true do + x, z = unpairX(j), unpairZ(j) -- coords in first quadrant + x, z = adjustToQuadrant(quadrantI, x, z) -- map coords correctly into other quadrants + if isDiggingSpot(x, z) then + if counter == i then + return x, z + end + counter = counter + 1 + end + j = j + 1 + end +end + +-- move safely into the direction, not hurting other turtles, but freeing the path if needed. +function moveSafe(moveAction, inspectAction, digAction) + while true do + local status, block = inspectAction() + if status then + if block["name"]:lower():find("turtle") then + -- turtle in front of me. lets wait for it to move + sleep(0.6) + else + digAction() + end + else + -- nothing in the way, try to move + if moveAction() then + break + end + end + end +end + +-- If the block in front of the turtle is desired, take it. +function grabOres() + local status, block = turtle.inspect() + if status and block["name"]:lower():find("ore") then + turtle.dig() + end +end + +-- dig all the way down to bedrock, collect ores +-- Parameters: +-- * moveAction: the **vertical** move action, eg turtle.down +-- * digAction: the **vertical** dig action, eg turtle.digDown +-- * inpectAction: the **vertical** inspect action, eg turtle.inspectDown +-- * limit: if >= 0 * the number of blocks to dig +-- * dig until bedrock is hit +-- +-- +-- Returns: the depth dug +function digDeep(moveAction, digAction, inspectAction, limit) + local depth = 0 + grabOres() + while true do + -- termination condition: + -- * if limit != -1: limit > 0 + -- * otherwise : bedrock beneath + if limit < 0 then + local status, nextblock = inspectAction() + if status and nextblock["name"] == "minecraft:bedrock" then + return depth + end + else + if limit == 0 then + return depth + end + end + -- Limit not reached: keep digging + depth = depth + 1 + limit = limit - 1 + digAction() + moveAction() + grabOres() -- inspect block in front of turtle, take if neccessary + end +end + +function selectCobble() + for i = 1,16 do + turtle.select(i) + item = turtle.getItemDetail() + if item and item["name"] == "minecraft:cobblestone" then + break + end + end +end + +function selectFuel() + for i = 1,16 do + turtle.select(i) + item = turtle.getItemDetail() + if item and (item["name"] == "minecraft:coal" or item["name"] == "minecraft:charcoal") then + break + end + end +end + +function digShaft() + turtle.digDown() + selectCobble() + turtle.placeUp() + turtle.down() + depth = digDeep(turtle.down, turtle.digDown, turtle.inspectDown, -1) + turtle.turnLeft() + digDeep(turtle.up, noop, noop, depth) + turtle.turnLeft() + digDeep(turtle.down, noop, noop, depth) + turtle.turnLeft() + digDeep(turtle.up, noop, noop, depth) + selectCobble() + turtle.placeDown() + turtle.up() + turtle.digUp() + turtle.up() +end + +function digTo(x, z) + --print("Digging to rel. coord ("..tostring(x)..", "..tostring(z)..")") + while z > 0 do + moveSafe(turtle.forward, turtle.inspect, turtle.dig) + z = z - 1 + repeat sleep(0.6) + until (not turtle.digUp()) + end + turtle.turnRight() + while x > 0 do + moveSafe(turtle.forward, turtle.inspect, turtle.dig) + x = x - 1 + repeat sleep(0.6) + until (not turtle.digUp()) + end +end + +function goToSpawn(x, z) + moveSafe(turtle.up, turtle.inspectUp, turtle.digUp) + while z > 0 do + moveSafe(turtle.forward, turtle.inspect, turtle.dig) + z = z - 1 + end + turtle.turnRight() + while x > 0 do + moveSafe(turtle.forward, turtle.inspect, turtle.dig) + x = x - 1 + end +end + +function moveSafeForward() + moveSafe(turtle.forward, turtle.inspect, noop) +end + +function goToBarrel(path) + if path == 0 then + turtle.turnLeft() + moveSafe(turtle.forward, turtle.inspect, noop) + turtle.turnRight() + 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) + turtle.turnRight() + moveSafe(turtle.forward, turtle.inspect, noop) + moveSafe(turtle.forward, turtle.inspect, noop) + moveSafe(turtle.down, turtle.inspectDown, noop) + else + moveSafe(turtle.forward, turtle.inspect, noop) + turtle.turnRight() + moveSafe(turtle.forward, turtle.inspect, noop) + turtle.turnLeft() + moveSafe(turtle.forward, turtle.inspect, noop) + moveSafe(turtle.forward, turtle.inspect, noop) + moveSafe(turtle.forward, turtle.inspect, noop) + moveSafe(turtle.down, turtle.inspectDown, noop) + turtle.turnRight() + end +end + +function leaveBarrel(path) + if path == 0 then + moveSafe(turtle.forward, turtle.inspect, noop) + moveSafe(turtle.forward, turtle.inspect, noop) + moveSafe(turtle.forward, turtle.inspect, noop) + turtle.turnRight() + moveSafe(turtle.forward, turtle.inspect, noop) + moveSafe(turtle.forward, turtle.inspect, noop) + turtle.turnRight() + else + moveSafe(turtle.forward, turtle.inspect, noop) + moveSafe(turtle.forward, turtle.inspect, noop) + moveSafe(turtle.forward, turtle.inspect, noop) + turtle.turnRight() + moveSafe(turtle.forward, turtle.inspect, noop) + turtle.turnRight() + end +end + +function reportDuty(id, fuelconsumed) + moveSafe(turtle.down, turtle.inspectDown, noop) + moveSafe(turtle.down, turtle.inspectDown, noop) + moveSafe(turtle.down, turtle.inspectDown, noop) + moveSafeForward() + moveSafeForward() + moveSafeForward() + moveSafeForward() + moveSafeForward() + moveSafeForward() + moveSafeForward() + moveSafeForward() + rednet.open("bottom") + --print("Proto: Sending completed Job id") + rednet.broadcast(tostring(id), "jobs") + sleep(0.6) + --print("Proto: Sending fuel status") + rednet.broadcast(tostring(fuelconsumed), "fuel") + while true do + --print("Proto: Awaiting thanks") + sender, message, proto = rednet.receive("jobcomplete") + if message == "thanks" then + break + else + sleep(1) + end + end + --print("Proto: Received thanks") +end + +function enqueueTurtle() + moveSafe(turtle.up, turtle.inspectUp, noop) + moveSafe(turtle.up, turtle.inspectUp, noop) + moveSafe(turtle.up, turtle.inspectUp, noop) + turtle.turnLeft() + moveSafeForward() + moveSafeForward() + moveSafeForward() + turtle.turnRight() + while true do + local status, block = turtle.inspectDown() + if not status then + if turtle.down() then + break -- we moved in line + end + else + if turtle.forward() then + -- pass, thats ok + else + local status, block = turtle.inspect() + if status and not block["name"]:lower():find("turtle") then + turtle.dig() + turtle.forward() + turtle.digUp() + turtle.digDown() + end + end + end + end + -- we moved into line + turtle.turnLeft() + turtle.turnLeft() +end + +function doRefuel() + moveSafeForward() + turtle.turnRight() + while turtle.getFuelLevel() < 1500 do + turtle.suck(4) + selectFuel() + turtle.refuel() + end + turtle.turnLeft() +end + +function goToJob() + moveSafeForward() + moveSafeForward() + moveSafeForward() +end + +function waitForJob() + --print("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 + 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) + --print("Received job "..tostring(job)) + return job + else + if not retrying then + retrying = true + --print("No job received. I will keep retrying.") + turtlestate["state"] = "Waiting for job... (retrying)" + end + end + end +end + +function emptyToBarrel() + sleep(0.4) + for i = 1,16 do + turtle.select(i) + sleep(0.1) + turtle.dropDown() + end +end + +function drawStatus() + print("Turtle ("..tostring(turtlestate["id"])..")") + print() + print(" \""..turtlestate["name"].."\"") + print() + print() + if turtlestate["job"] then + print("Current job: "..tostring(turtlestate["job"])) + else + print("Current job: ") + end + print() + if turtlestate["job"] then + print("Recent fuel usage: "..tostring(turtlestate["fuel"])) + else + print("Recent fuel usage: ") + end + print() + print("Status:") + print(" "..turtlestate["state"]) + print() +end + +function turtleAI() + while true do + turtlestate["state"] = "Waiting for job..." + drawStatus() + thejob = waitForJob() + turtlestate["job"] = thejob + turtlestate["state"] = "Refueling..." + drawStatus() + doRefuel() + fuel = turtle.getFuelLevel() + turtlestate["state"] = "Headed to the jobsite..." + drawStatus() + goToJob() + x, z = ithSpot(thejob) + digTo(x,z) + turtlestate["state"] = "Digging the Shaft..." + drawStatus() + digShaft() + turtlestate["state"] = "Returning to the station..." + drawStatus() + goToSpawn(x, z) + path = math.floor(math.random() + 0.5) + turtlestate["state"] = "Unloading Inventory..." + drawStatus() + goToBarrel(path) + emptyToBarrel() + leaveBarrel(path) + fuelconsumed = fuel - turtle.getFuelLevel() + --print("I consumed "..tostring(fuelconsumed).." fuel") + --print("Reporting Duty") + turtlestate["fuel"] = fuelconsumed + turtlestate["state"] = "Reporting duty..." + drawStatus() + reportDuty(thejob, fuelconsumed) + turtlestate["job"] = nil + turtlestate["state"] = "Going back in line..." + drawStatus() + enqueueTurtle() + end +end + +turtleAI() diff --git a/mine/server.lua b/mine/server.lua new file mode 100644 index 0000000..76ed962 --- /dev/null +++ b/mine/server.lua @@ -0,0 +1,46 @@ +rednet.open("back") + +curjob = 69 +while true do + print("From what index do you want to resume jobs?") + x = read() + if tonumber(x) then + curjob = tonumber(x) + break + end + print("That's not a number.") +end + +function getNextJob() + curjob = curjob + 1 + return curjob - 1 +end + +while true do + sender, message, proto = rednet.receive("jobs") + if not message then + sleep(0.6) + else + --print("> raw: "..message) + if message == "gibjob" then + if rs.getInput("right") then + job = getNextJob() + rednet.broadcast(tostring(job), "newjob") + print("Found job request. Offering "..tostring(job)) + end + else + -- numberic job is complete + completejob = tonumber(message) + fuelused = "??" + fuelsender, fuelmessage, fuelproto = rednet.receive("fuel") + if fuelmessage then + fuelused = fuelmessage + end + print("Job "..tostring(completejob).." was completed, "..fuelused.." fuel was used") + rednet.broadcast("thanks", "jobcomplete") + log = io.open("jobs.log","a") + log:write(tostring(completejob),"\n") + log:close() + end + end +end From 2bfe5fcc6d44d8511a095290818dda4a12797487 Mon Sep 17 00:00:00 2001 From: Dominic Zimmer Date: Mon, 6 Jul 2020 19:09:36 +0200 Subject: [PATCH 08/17] 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) From bd3b15a1e86084d63b7eb5a80b95c326e039bbe6 Mon Sep 17 00:00:00 2001 From: Dominic Zimmer Date: Mon, 6 Jul 2020 19:11:17 +0200 Subject: [PATCH 09/17] Syntax --- mine/client.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mine/client.lua b/mine/client.lua index 9b93600..1a443b5 100644 --- a/mine/client.lua +++ b/mine/client.lua @@ -60,7 +60,7 @@ function adjustToQuadrant(i, x, z) end function iTo2Dcoords(j) - i = j // 4 + i = math.floor(j / 4) quadrantI = j % 4 -- 0-3 quadrant counter = 0 -- number of digging spots found j = 0 -- enumerate 2D plane From 75cd0336b8560bca16cedf0b0b99e0f599f97ab7 Mon Sep 17 00:00:00 2001 From: Dominic Zimmer Date: Mon, 6 Jul 2020 19:12:01 +0200 Subject: [PATCH 10/17] Syntax --- mine/client.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mine/client.lua b/mine/client.lua index 1a443b5..4cdd637 100644 --- a/mine/client.lua +++ b/mine/client.lua @@ -499,7 +499,7 @@ function goToCoordXZ(targetX, targetZ, extraaction) faceDir(0) while (targetX > x) do moveSafe(turtle.forward, turtle.inspect, turtle.dig) - x += 1 + x = x + 1 extraaction() end end @@ -507,7 +507,7 @@ function goToCoordXZ(targetX, targetZ, extraaction) faceDir(2) while (targetX < x) do moveSafe(turtle.forward, turtle.inspect, turtle.dig) - x -= 1 + x = x - 1 extraaction() end end @@ -516,7 +516,7 @@ function goToCoordXZ(targetX, targetZ, extraaction) faceDir(1) while (targetZ > z) do moveSafe(turtle.forward, turtle.inspect, turtle.dig) - z += 1 + z = z + 1 eztraaction() end end @@ -524,7 +524,7 @@ function goToCoordXZ(targetX, targetZ, extraaction) faceDir(3) while (targetZ < z) do moveSafe(turtle.forward, turtle.inspect, turtle.dig) - z -= 1 + z = z - 1 eztraaction() end end From 99a2cdb6e12668fc21e8b6c6f515e08e43856d86 Mon Sep 17 00:00:00 2001 From: Dominic Zimmer Date: Mon, 6 Jul 2020 19:12:31 +0200 Subject: [PATCH 11/17] Syntax --- mine/client.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mine/client.lua b/mine/client.lua index 4cdd637..851a122 100644 --- a/mine/client.lua +++ b/mine/client.lua @@ -1,5 +1,5 @@ args = {...} -x, z, dir = 0, 0, 0 +x, z, direction = 0, 0, 0 turtlestate = { name = os.getComputerLabel(), id = os.getComputerID(), From 86aa002444a628912a09cbc4ac33689a787e6461 Mon Sep 17 00:00:00 2001 From: Dominic Zimmer Date: Mon, 6 Jul 2020 19:15:21 +0200 Subject: [PATCH 12/17] Syntax --- mine/client.lua | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/mine/client.lua b/mine/client.lua index 851a122..639b881 100644 --- a/mine/client.lua +++ b/mine/client.lua @@ -511,13 +511,12 @@ function goToCoordXZ(targetX, targetZ, extraaction) extraaction() end end - if targetZ > z then faceDir(1) while (targetZ > z) do moveSafe(turtle.forward, turtle.inspect, turtle.dig) z = z + 1 - eztraaction() + extraaction() end end if targetZ < z then @@ -525,7 +524,7 @@ function goToCoordXZ(targetX, targetZ, extraaction) while (targetZ < z) do moveSafe(turtle.forward, turtle.inspect, turtle.dig) z = z - 1 - eztraaction() + extraaction() end end end @@ -561,11 +560,7 @@ function turtleAI2() end -- turtleAI() -faceDir(0) -faceDir(1) faceDir(3) -faceDir(1) -faceDir(0) -faceDir(2) -faceDir(0) +goToCoordXZ(4, 2, noop) +goToCoordXZ(0, 0, noop) goToCoordXZ(4, 2, noop) From eb535fa029fb1016c668539d49fe98d37791b464 Mon Sep 17 00:00:00 2001 From: Dominic Zimmer Date: Mon, 6 Jul 2020 19:18:11 +0200 Subject: [PATCH 13/17] Syntax --- mine/client.lua | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/mine/client.lua b/mine/client.lua index 639b881..6323bce 100644 --- a/mine/client.lua +++ b/mine/client.lua @@ -77,6 +77,22 @@ function iTo2Dcoords(j) end end +function digSafe(inspectAction, digAction) + while true do + local status, block = inspectAction() + if status then + if block["name"]:lower():find("turtle") then + -- turtle in front of me. lets wait for it to move + return true + else + digAction() + end + else + return true + end + end +end + -- move safely into the direction, not hurting other turtles, but freeing the path if needed. function moveSafe(moveAction, inspectAction, digAction) while true do @@ -529,6 +545,9 @@ function goToCoordXZ(targetX, targetZ, extraaction) end end +function digSafe(inspectAction, digAction) +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)..")" }) @@ -559,8 +578,11 @@ function turtleAI2() end end +function extraaction() + repeat sleep(0.6) until (not digSafe(turtle.inspectUp, turtle.digUp())) +end -- turtleAI() faceDir(3) -goToCoordXZ(4, 2, noop) -goToCoordXZ(0, 0, noop) -goToCoordXZ(4, 2, noop) +goToCoordXZ(4, 2, extraaction) +goToCoordXZ(0, 0, extraaction) +goToCoordXZ(4, 2, extraaction) From 4269615573169f94e797640ae52bfd5818ed1815 Mon Sep 17 00:00:00 2001 From: Dominic Zimmer Date: Mon, 6 Jul 2020 19:21:33 +0200 Subject: [PATCH 14/17] Syntax --- mine/client.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mine/client.lua b/mine/client.lua index 6323bce..9bb4f46 100644 --- a/mine/client.lua +++ b/mine/client.lua @@ -82,12 +82,15 @@ function digSafe(inspectAction, digAction) local status, block = inspectAction() if status then if block["name"]:lower():find("turtle") then - -- turtle in front of me. lets wait for it to move + -- turtle in front of me. its free space return true else digAction() + -- gravel may take time to fall. + sleep(0.6) end else + -- No block in front of me. good return true end end From 5c9db8868f249e39bd727370e4416cecd5baa841 Mon Sep 17 00:00:00 2001 From: Dominic Zimmer Date: Mon, 6 Jul 2020 19:22:56 +0200 Subject: [PATCH 15/17] Syntax --- mine/client.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mine/client.lua b/mine/client.lua index 9bb4f46..9b46aee 100644 --- a/mine/client.lua +++ b/mine/client.lua @@ -87,7 +87,7 @@ function digSafe(inspectAction, digAction) else digAction() -- gravel may take time to fall. - sleep(0.6) + sleep(1) end else -- No block in front of me. good From 00f45a8a6a37b99e66ae8f43e053380f4dad647f Mon Sep 17 00:00:00 2001 From: Dominic Zimmer Date: Mon, 6 Jul 2020 19:23:44 +0200 Subject: [PATCH 16/17] Syntax --- mine/client.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mine/client.lua b/mine/client.lua index 9b46aee..fc22a1d 100644 --- a/mine/client.lua +++ b/mine/client.lua @@ -87,7 +87,7 @@ function digSafe(inspectAction, digAction) else digAction() -- gravel may take time to fall. - sleep(1) + sleep(2) end else -- No block in front of me. good From c2a8dfedaf8c367f4f008527a814ddf8851d841d Mon Sep 17 00:00:00 2001 From: Dominic Zimmer Date: Mon, 6 Jul 2020 19:24:55 +0200 Subject: [PATCH 17/17] Syntax --- mine/client.lua | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/mine/client.lua b/mine/client.lua index fc22a1d..83afcfc 100644 --- a/mine/client.lua +++ b/mine/client.lua @@ -87,7 +87,7 @@ function digSafe(inspectAction, digAction) else digAction() -- gravel may take time to fall. - sleep(2) + sleep(1) end else -- No block in front of me. good @@ -548,9 +548,6 @@ function goToCoordXZ(targetX, targetZ, extraaction) end end -function digSafe(inspectAction, digAction) -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)..")" })