This commit is contained in:
Laurie Fisher 2025-06-27 14:42:52 +01:00
parent 5238e31018
commit 15ae824816
Signed by: PinkEyedOrphan
GPG Key ID: 7F827B68147AEE76
3 changed files with 150 additions and 0 deletions

28
libs/behavior.lua Normal file
View File

@ -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

29
libs/logger.lua Normal file
View File

@ -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

93
libs/turtleutils.lua Normal file
View File

@ -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