You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
3.0 KiB

local robot = require("robot")
local component = require("component")
local sides = require("sides")
local event = require("event")
local term = require("term")
local nav = require("nav")
local geolyzer = component.geolyzer
local depth, width, crops = ...
if not depth or not width or not crops then
print("Usage: farm <forward> <right> <crops>")
return
end
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
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
else
plant()
end
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
end
term.clear()
robot.setLightColor(0x00ff00)
while running do
term.setCursor(pos_right, depth - pos_forward + 1)
check_crop()
move_depth()
handle_interrupt()
end