From dc11f25fab837dc877073abd57c016dbc8a6dd3a Mon Sep 17 00:00:00 2001 From: D4VID Date: Sun, 17 Nov 2024 19:25:52 +0100 Subject: [PATCH] Real farming now --- farm.lua | 155 ++++++++++++++++++++++++++++++++++++++++++++++--------- nav.lua | 10 ++++ 2 files changed, 142 insertions(+), 23 deletions(-) create mode 100644 nav.lua diff --git a/farm.lua b/farm.lua index a665154..bfaea64 100644 --- a/farm.lua +++ b/farm.lua @@ -1,35 +1,144 @@ local robot = require("robot") -local depth,width = ... +local component = require("component") +local sides = require("sides") +local event = require("event") +local term = require("term") +local nav = require("nav") -if not depth or not width then - print("Usage: farm ") +local geolyzer = component.geolyzer + +local depth, width, crops = ... + +if not depth or not width or not crops then + print("Usage: farm ") return end -print("Farming in a " .. depth .. "x" .. width .. " area") +depth = tonumber(depth) +width = tonumber(width) +crops = tonumber(crops) + +print("Farming " .. crops .. " crops in a " .. depth .. "x" .. width .. " area") + + +local dir_forward = true +local dir_right = true + +local pos_forward = 1 +local pos_right = 1 + +local running = true + +local function growth_to_char(growth) + if growth <= 0.25 then + return '.' + elseif growth <= 0.50 then + return '+' + elseif growth <= 0.75 then + return 'S' + else + return '#' + end +end + +local function move_width() + if dir_right then + if pos_right == width then + dir_right = false + end + else + if pos_right == 1 then + dir_right = true + end + end + + if dir_right then + robot.turnRight() + nav.forward() + robot.turnLeft() + pos_right = pos_right + 1 + else + robot.turnLeft() + nav.forward() + robot.turnRight() + pos_right = pos_right - 1 + end +end + +local function move_depth() + if dir_forward then + if pos_forward == depth then + dir_forward = false + move_width() + else + nav.forward() + pos_forward = pos_forward + 1 + end + else + if pos_forward == 1 then + dir_forward = true + move_width() + else + nav.back() + pos_forward = pos_forward - 1 + end + end +end -for _ = 1,width do - -- break - for _ = 1,depth do - if robot.detectDown() then +local function plant() + robot.select(pos_right % crops + 1) + robot.placeDown() + term.write(growth_to_char(0)) + term.setCursorBlink(true) +end + +local function check_crop() + if robot.detectDown() then + local crop = geolyzer.analyze(sides.down) + if not crop.growth then + return -- ignore this block + end + if crop.growth >= 1.0 then robot.swingDown() + plant() + else + term.write(growth_to_char(crop.growth)) + term.setCursorBlink(true) end - while not robot.forward() do end + else + plant() end - robot.turnAround() - -- go back and place - for _ = 1,depth do - while not robot.forward() do end - robot.placeDown() +end + +local function handle_interrupt() + if event.pull(0.1, "interrupted") then + term.clear() + print("Interrupted. Returning to original location...") + robot.setLightColor(0xff0000) + + -- return to original location + while pos_forward > 1 do + nav.back() + pos_forward = pos_forward - 1 + end + + robot.turnLeft() + while pos_right > 1 do + nav.forward() + pos_right = pos_right - 1 + end + robot.turnRight() + + running = false end - -- advance to next row - robot.turnRight() - while not robot.forward() do end - robot.turnRight() end --- go back to starting position -robot.turnRight() -for _ = 1,width do - while not robot.forward() do end + +term.clear() +robot.setLightColor(0x00ff00) + +while running do + term.setCursor(pos_right, depth - pos_forward + 1) + check_crop() + move_depth() + handle_interrupt() end -robot.turnLeft() diff --git a/nav.lua b/nav.lua new file mode 100644 index 0000000..e9be344 --- /dev/null +++ b/nav.lua @@ -0,0 +1,10 @@ +local robot = require("robot") + +return { + forward = function() + while not robot.forward() do end + end, + back = function() + while not robot.back() do end + end, +}