From 82fb45a9edf3e994dc945533c8bcb744af9e399c Mon Sep 17 00:00:00 2001 From: TO Date: Mon, 26 Dec 2022 19:07:15 +0100 Subject: [PATCH] sand and sandStone update and "stable" --- turtleSand.lua | 221 ++++++++++++++++++++++++++++++++++++++++++++ turtleSandStone.lua | 58 +++++++----- 2 files changed, 256 insertions(+), 23 deletions(-) create mode 100644 turtleSand.lua diff --git a/turtleSand.lua b/turtleSand.lua new file mode 100644 index 0000000..8083035 --- /dev/null +++ b/turtleSand.lua @@ -0,0 +1,221 @@ +local xC, yC, zC, currentDir +local nbBlocs, nbFuel +local sizeLine, toDoLine +MAXBLOCS = 960 +MAXFUEL = 64 + +local direction = { + NORTH = function() -- X ++ + xC = xC + 1 + end, + SOUTH = function() -- X -- + xC = xC - 1 + end, + EAST = function() -- Y ++ + yC = yC + 1 + end, + WEST = function() -- Y -- + yC = yC - 1 + end, +} + +local function goUp() + assert(turtle.up()) + zC = zC + 1 +end + +local function goDown() + assert(turtle.down()) + zC = zC - 1 +end + +local function turnRight() + if (currentDir == direction.NORTH) then + currentDir = direction.EAST + elseif (currentDir == direction.EAST) then + currentDir = direction.SOUTH + elseif (currentDir == direction.SOUTH) then + currentDir = direction.WEST + else + currentDir = direction.NORTH + end + turtle.turnRight() +end + +local function turnLeft() + if (currentDir == direction.NORTH) then + currentDir = direction.WEST + elseif (currentDir == direction.WEST) then + currentDir = direction.SOUTH + elseif (currentDir == direction.SOUTH) then + currentDir = direction.EAST + else + currentDir = direction.NORTH + end + turtle.turnLeft() +end + +local function turnDir(dir) + while currentDir ~= dir do + turnRight() + end +end + +local function forward() + if not turtle.forward() then + goDown() + forward() + end + currentDir() +end + +local function gotoCoo(x, y, z, startZ) + if startZ then + while zC ~= z do + if zC < z then + goUp() + else + goDown() + end + end + end + while yC ~= y do + if yC < y then + turnDir(direction.EAST) + forward() + else + turnDir(direction.WEST) + forward() + end + end + while xC ~= x do + if xC < x then + turnDir(direction.NORTH) + forward() + else + turnDir(direction.SOUTH) + forward() + end + end + if not startZ then + while zC ~= z do + if zC < z then + goUp() + else + goDown() + end + end + end +end + +local function setSlotBloc() + local i = 1 + local is_fuel, isEmpty, reason = true, false, false + while is_fuel or isEmpty do + turtle.select(i) + is_fuel, reason = turtle.refuel(0) + isEmpty = turtle.getItemCount() == 0 + i = i + 1 + end +end + +local function setSlotFuel() + local i = 1 + local is_fuel, reason = false, false + while not is_fuel do + turtle.select(i) + is_fuel, reason = turtle.refuel(0) + i = i + 1 + end +end + +local function reFuel() + pcall(setSlotFuel) + while (nbFuel > 0) and (turtle.getFuelLevel() < turtle.getFuelLimit()) do + turtle.refuel(1) + nbFuel = nbFuel - 1 + end +end + +local function refill() + local saveX, saveY, saveZ, saveDir = xC, yC, zC, currentDir + gotoCoo(0, 0, 0, false) + turnDir(direction.NORTH) + reFuel() + turtle.suck(MAXFUEL - nbFuel) + nbFuel = MAXFUEL + reFuel() + turtle.suck(MAXFUEL - nbFuel) + nbFuel = MAXFUEL + turnRight() + forward() + turnLeft() + while nbBlocs < MAXBLOCS do + if nbBlocs + 64 <= MAXBLOCS then + turtle.suck(64) + nbBlocs = nbBlocs + 64 + else + turtle.suck(MAXBLOCS - nbBlocs) + nbBlocs = MAXBLOCS + end + end + gotoCoo(saveX, saveY, saveZ, true) + turnDir(saveDir) +end + +local function placeBlock() + if nbBlocs <= 0 then + refill() + end + if nbBlocs % 64 == 0 then + setSlotBloc() + end + if turtle.placeDown() then + nbBlocs = nbBlocs - 1 + end +end + +local function checkFuel() + if (turtle.getFuelLevel() <= turtle.getFuelLimit() / 2) then + if nbFuel > 0 then + reFuel() + checkFuel() + else + refill() + end + end +end + +local function fill() + local nbLine = 0 + while true do + while nbLine < 2 do + while toDoLine > 0 do + checkFuel() + while not turtle.detectDown() do + placeBlock() + sleep(1) + end + forward() + toDoLine = toDoLine - 1 + end + turnLeft() + toDoLine = sizeLine + nbLine = nbLine + 1 + end + sizeLine = sizeLine + 1 + nbLine = 0 + end +end + +local function main() + xC, yC, zC, currentDir = 0, 0, 0, direction.NORTH + nbFuel, nbBlocs = 0, 0 + refill() + goDown() + goDown() + sizeLine, toDoLine = 1, 1 + fill() +end + +main() diff --git a/turtleSandStone.lua b/turtleSandStone.lua index f6ac673..a0eab3f 100644 --- a/turtleSandStone.lua +++ b/turtleSandStone.lua @@ -63,13 +63,22 @@ end local function forward() if not turtle.forward() then - goDown() + goUp() forward() end currentDir() end -local function gotoCoo(x, y, z) +local function gotoCoo(x, y, z, startZ) + if startZ then + while zC ~= z do + if zC < z then + goUp() + else + goDown() + end + end + end while yC ~= y do if yC < y then turnDir(direction.EAST) @@ -88,11 +97,13 @@ local function gotoCoo(x, y, z) forward() end end - while zC ~= z do - if zC < z then - goUp() - else - goDown() + if not startZ then + while zC ~= z do + if zC < z then + goUp() + else + goDown() + end end end end @@ -118,21 +129,23 @@ local function setSlotFuel() end end +local function reFuel() + pcall(setSlotFuel) + while (nbFuel > 0) and (turtle.getFuelLevel() < turtle.getFuelLimit()) do + turtle.refuel(1) + nbFuel = nbFuel - 1 + end +end + local function refill() local saveX, saveY, saveZ, saveDir = xC, yC, zC, currentDir - gotoCoo(0, 0, 0) + gotoCoo(0, 0, 0, false) turnDir(direction.NORTH) - pcall(setSlotFuel) - if nbFuel < 8 then - turtle.refuel(nbFuel) - nbFuel = 0 - else - turtle.refuel(8) - nbFuel = nbFuel - 8 - end - turtle.suck(MAXFUEL) - turtle.refuel(8) - turtle.suck(8) + reFuel() + turtle.suck(MAXFUEL - nbFuel) + nbFuel = MAXFUEL + reFuel() + turtle.suck(MAXFUEL - nbFuel) nbFuel = MAXFUEL turnRight() forward() @@ -146,7 +159,7 @@ local function refill() nbBlocs = MAXBLOCS end end - gotoCoo(saveX, saveY, saveZ) + gotoCoo(saveX, saveY, saveZ, true) turnDir(saveDir) end @@ -163,10 +176,9 @@ local function placeBlock() end local function checkFuel() - if (turtle.getFuelLevel() <= 10) or (turtle.getFuelLevel() < (math.abs(xC) + math.abs(yC) + math.abs(zC) + 10)) then + if (turtle.getFuelLevel() <= turtle.getFuelLimit() / 2) then if nbFuel > 0 then - setSlotFuel() - turtle.refuel() + reFuel() checkFuel() else refill()