From 15ae824816ae27c0671728ea5c006bb6b6e97c4e Mon Sep 17 00:00:00 2001 From: Laurie Fisher Date: Fri, 27 Jun 2025 14:42:52 +0100 Subject: [PATCH] test --- libs/behavior.lua | 28 +++++++++++++ libs/logger.lua | 29 ++++++++++++++ libs/turtleutils.lua | 93 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+) create mode 100644 libs/behavior.lua create mode 100644 libs/logger.lua create mode 100644 libs/turtleutils.lua diff --git a/libs/behavior.lua b/libs/behavior.lua new file mode 100644 index 0000000..e4b2c68 --- /dev/null +++ b/libs/behavior.lua @@ -0,0 +1,28 @@ +local M = {} + +local logger = require("logger") +local currentStatus = "idle" + +function M.getStatus() + return currentStatus +end + +function M.run() + while true do + currentStatus = "moving backward" + logger.log("Started moving backward") + for i = 1, 10 do + turtle.back() + sleep(0.5) + end + + currentStatus = "moving forward" + logger.log("Started moving forward") + for i = 1, 10 do + turtle.forward() + sleep(0.5) + end + end +end + +return M \ No newline at end of file diff --git a/libs/logger.lua b/libs/logger.lua new file mode 100644 index 0000000..a232273 --- /dev/null +++ b/libs/logger.lua @@ -0,0 +1,29 @@ +local M = {} + +local logLines = {} +local maxLines = 5 + +function M.log(msg) + local time = textutils.formatTime(os.time(), true) + local entry = string.format("[%s] %s", time, msg) + + table.insert(logLines, entry) + if #logLines > maxLines then + table.remove(logLines, 1) + end + + M.draw() +end + +function M.draw() + local w, h = term.getSize() + for i = 1, maxLines do + term.setCursorPos(1, h - maxLines + i) + term.clearLine() + if logLines[i] then + term.write(logLines[i]) + end + end +end + +return M \ No newline at end of file diff --git a/libs/turtleutils.lua b/libs/turtleutils.lua new file mode 100644 index 0000000..b10a08e --- /dev/null +++ b/libs/turtleutils.lua @@ -0,0 +1,93 @@ +local M = {} + +-- Helper: retry turtle action up to N times +local function try(actionFn, times) + times = times or 5 + for i = 1, times do + if actionFn() then return true end + sleep(0.5) + end + return false +end + +-- Basic movement +function M.forward() + return try(turtle.forward) +end + +function M.back() + return try(turtle.back) +end + +function M.up() + return try(turtle.up) +end + +function M.down() + return try(turtle.down) +end + +-- Digging with movement +function M.digForward() + while turtle.detect() do + turtle.dig() + sleep(0.3) + end +end + +function M.digUp() + while turtle.detectUp() do + turtle.digUp() + sleep(0.3) + end +end + +function M.digDown() + while turtle.detectDown() do + turtle.digDown() + sleep(0.3) + end +end + +-- Move forward, clearing blocks if necessary +function M.moveUntilClearForward() + while not turtle.forward() do + if turtle.detect() then + turtle.dig() + else + sleep(0.5) + end + end +end + +-- Inspection +function M.inspectForward() + local success, data = turtle.inspect() + if success then return data else return nil end +end + +function M.inspectUp() + local success, data = turtle.inspectUp() + if success then return data else return nil end +end + +function M.inspectDown() + local success, data = turtle.inspectDown() + if success then return data else return nil end +end + +-- Turning +function M.turnLeft() + turtle.turnLeft() +end + +function M.turnRight() + turtle.turnRight() +end + +function M.turnAround() + turtle.turnLeft() + turtle.turnLeft() +end + +return M