|
|
|
@ -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 <forward> <left>")
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|