From 2af22cdd0769dea03435236d668dca691c29ddeb Mon Sep 17 00:00:00 2001 From: Dominic Zimmer Date: Sat, 4 Jul 2020 13:53:15 +0200 Subject: [PATCH] add canemine script --- dominic/canemine.lua | 126 +++++++++++++++++++++++++++++++++++++++++++ dominic/pairing.lua | 72 ++++++++++++++++++++++--- 2 files changed, 192 insertions(+), 6 deletions(-) create mode 100644 dominic/canemine.lua diff --git a/dominic/canemine.lua b/dominic/canemine.lua new file mode 100644 index 0000000..8db2ccd --- /dev/null +++ b/dominic/canemine.lua @@ -0,0 +1,126 @@ +args = {...} + +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 digTo(x, z) + print("Digging to rel. coord ("..tostring(x)..", "..tostring(z)..")") + while z > 0 do + turtle.dig() + turtle.forward() + turtle.digUp() + z = z - 1 + end + turtle.turnRight() + while x > 0 do + turtle.dig() + turtle.forward() + turtle.digUp() + x = x - 1 + 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 + while true do + -- termination condition: + -- * if limit != -1: limit > 0 + -- * otherwise : bedrock beneath + if limit < 0 then + local nextblock = inspectAction() + if nextblock 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() + -- TODO + -- 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["name"] == "minecraft:cobblestone" then + break + end + end +end + +function digShaft() + print("Starting to dig shaft") + depth = digDeep(turtle.down, turtle.digDown, turtle.inspectDown, 3) + print("Dug the first shaft (depth = "..tostring(depth)..")") + turtle.turnLeft() + digDeep(turtle.up, noop, noop, depth - 1) + turtle.turnLeft() + digDeep(turtle.down, noop, noop, depth - 1) + turtle.turnLeft() + digDeep(turtle.up, noop, noop, depth - 1) + turtle.turnLeft() + selectCobble() + turtle.placeDown() + turtle.up() +end + +spotNum = tonumber(args[1]) +if spotNum then + x, z = ithSpot(spotNum) + digTo(x,z) + digShaft() +end diff --git a/dominic/pairing.lua b/dominic/pairing.lua index 84d0446..8db2ccd 100644 --- a/dominic/pairing.lua +++ b/dominic/pairing.lua @@ -1,5 +1,7 @@ args = {...} +function noop() end + function pair(x, y) return math.floor((x + y) * (x + y + 1)/2 + y) end @@ -38,6 +40,7 @@ function ithSpot(i) end function digTo(x, z) + print("Digging to rel. coord ("..tostring(x)..", "..tostring(z)..")") while z > 0 do turtle.dig() turtle.forward() @@ -53,14 +56,71 @@ function digTo(x, z) end end -function digSpot(i) - x, z = ithSpot(i) - digTo(x, z) - turtle.digDown() - turtle.down() +-- 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 + while true do + -- termination condition: + -- * if limit != -1: limit > 0 + -- * otherwise : bedrock beneath + if limit < 0 then + local nextblock = inspectAction() + if nextblock 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() + -- TODO + -- 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["name"] == "minecraft:cobblestone" then + break + end + end +end + +function digShaft() + print("Starting to dig shaft") + depth = digDeep(turtle.down, turtle.digDown, turtle.inspectDown, 3) + print("Dug the first shaft (depth = "..tostring(depth)..")") + turtle.turnLeft() + digDeep(turtle.up, noop, noop, depth - 1) + turtle.turnLeft() + digDeep(turtle.down, noop, noop, depth - 1) + turtle.turnLeft() + digDeep(turtle.up, noop, noop, depth - 1) + turtle.turnLeft() + selectCobble() + turtle.placeDown() + turtle.up() end spotNum = tonumber(args[1]) if spotNum then - digSpot(spotNum) + x, z = ithSpot(spotNum) + digTo(x,z) + digShaft() end