Proper lumberjack

Requires tractor beam upgrade
master
D4VID 10 months ago
parent 796c982890
commit 68ef3b92b6

@ -1,47 +1,139 @@
local robot = require("robot") local os = require("os")
local os = require("os") local robot = require("robot")
local component = require("component")
local event = require("event")
local nav = require("nav")
local length = ...
if not length then local magnet = component.tractor_beam
print('Usage: lumberjack <length>')
local depth, width = ...
if not depth or not width then
print('Usage: lumberjack <depth> <width>')
print('Operates in a depth x width grid of trees with 4 blocks between them')
print('Depth is forward, width is right')
print('Place stone as the 8th block above ground where a tree would grow')
print('Starting location of the robot is directly facing the first tree')
return return
end end
depth = tonumber(depth)
width = tonumber(width)
local dir_forward = true
local dir_right = true
local pos_forward = 1
local pos_right = 1
local running = true
local function mine_tree() local function mine_tree()
local n = 0
robot.swing() robot.swing()
robot.forward() nav.forward()
robot.swingDown() robot.swingDown()
while robot.detectUp() do for _ = 1, 5 do
robot.swingUp() robot.swingUp()
robot.up() nav.up()
n = n + 1
end
for _ = 1, n do
while not robot.down() do end
end end
nav.down(5)
robot.placeDown() robot.placeDown()
nav.back()
end end
while true do local function move_depth()
for _ = 1, length do if dir_forward then
if robot.detect() then if pos_forward == depth then
mine_tree() dir_forward = false
else end
while not robot.forward() do end else
if pos_forward == 1 then
dir_forward = true
end end
robot.suckDown()
end end
robot.turnAround()
for _ = 1, length do if dir_forward then
if robot.detect() then robot.turnRight()
mine_tree() nav.forward()
robot.turnLeft()
nav.forward(5)
robot.turnLeft()
nav.forward()
robot.turnRight()
pos_forward = pos_forward + 1
else
robot.turnRight()
nav.forward()
robot.turnRight()
nav.forward(5)
robot.turnRight()
nav.forward()
robot.turnRight()
pos_forward = pos_forward - 1
end
end
local function collect_items()
while magnet.suck() do end
end
local function move_width()
if dir_right then
if pos_right == width then
dir_right = false
move_depth()
else
robot.turnRight()
nav.forward(5)
robot.turnLeft()
pos_right = pos_right + 1
end
else
if pos_right == 1 then
dir_right = true
move_depth()
else else
while not robot.forward() do end robot.turnLeft()
nav.forward(5)
robot.turnRight()
pos_right = pos_right - 1
end
end
end
local function handle_interrupt()
if event.pull(0.5, "interrupted") then
print("Interrupted. Returning to original location...")
robot.setLightColor(0xff0000)
-- return to original location
robot.turnLeft()
while pos_right > 1 do
nav.forward(5)
pos_right = pos_right - 1
end
nav.back()
robot.turnRight()
while pos_forward > 1 do
nav.back(5)
pos_forward = pos_forward - 1
end end
robot.suckDown() robot.turnLeft()
nav.forward()
robot.turnRight()
running = false
end
end
robot.setLightColor(0x00ff00)
while running do
if robot.detect() then
mine_tree()
end end
robot.turnAround() move_width()
os.execute("sleep 5") collect_items()
handle_interrupt()
end end

@ -0,0 +1,38 @@
local robot = require("robot")
return {
forward = function(n)
if not n then n = 1 end
for _ = 1, n do
while not robot.forward() do
local result, desc = robot.detect()
if result and desc == "solid" then
robot.swing()
end
end
end
end,
back = function(n)
if not n then n = 1 end
for _ = 1, n do
while not robot.back() do end
end
end,
up = function(n)
if not n then n = 1 end
for _ = 1, n do
while not robot.up() do
local result, desc = robot.detectUp()
if result and desc == "solid" then
robot.swingUp()
end
end
end
end,
down = function(n)
if not n then n = 1 end
for _ = 1, n do
while not robot.down() do end
end
end,
}
Loading…
Cancel
Save