parent
796c982890
commit
68ef3b92b6
@ -1,47 +1,139 @@
|
||||
local robot = require("robot")
|
||||
local os = require("os")
|
||||
local robot = require("robot")
|
||||
local component = require("component")
|
||||
local event = require("event")
|
||||
local nav = require("nav")
|
||||
|
||||
|
||||
local length = ...
|
||||
local magnet = component.tractor_beam
|
||||
|
||||
if not length then
|
||||
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
|
||||
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 n = 0
|
||||
robot.swing()
|
||||
robot.forward()
|
||||
nav.forward()
|
||||
robot.swingDown()
|
||||
while robot.detectUp() do
|
||||
for _ = 1, 5 do
|
||||
robot.swingUp()
|
||||
robot.up()
|
||||
n = n + 1
|
||||
end
|
||||
for _ = 1, n do
|
||||
while not robot.down() do end
|
||||
nav.up()
|
||||
end
|
||||
nav.down(5)
|
||||
robot.placeDown()
|
||||
nav.back()
|
||||
end
|
||||
|
||||
while true do
|
||||
for _ = 1, length do
|
||||
if robot.detect() then
|
||||
mine_tree()
|
||||
local function move_depth()
|
||||
if dir_forward then
|
||||
if pos_forward == depth then
|
||||
dir_forward = false
|
||||
end
|
||||
else
|
||||
while not robot.forward() do end
|
||||
if pos_forward == 1 then
|
||||
dir_forward = true
|
||||
end
|
||||
robot.suckDown()
|
||||
end
|
||||
robot.turnAround()
|
||||
for _ = 1, length do
|
||||
if robot.detect() then
|
||||
mine_tree()
|
||||
|
||||
if dir_forward then
|
||||
robot.turnRight()
|
||||
nav.forward()
|
||||
robot.turnLeft()
|
||||
nav.forward(5)
|
||||
robot.turnLeft()
|
||||
nav.forward()
|
||||
robot.turnRight()
|
||||
pos_forward = pos_forward + 1
|
||||
else
|
||||
while not robot.forward() do end
|
||||
robot.turnRight()
|
||||
nav.forward()
|
||||
robot.turnRight()
|
||||
nav.forward(5)
|
||||
robot.turnRight()
|
||||
nav.forward()
|
||||
robot.turnRight()
|
||||
pos_forward = pos_forward - 1
|
||||
end
|
||||
robot.suckDown()
|
||||
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
|
||||
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
|
||||
robot.turnLeft()
|
||||
nav.forward()
|
||||
robot.turnRight()
|
||||
|
||||
running = false
|
||||
end
|
||||
end
|
||||
|
||||
robot.setLightColor(0x00ff00)
|
||||
|
||||
while running do
|
||||
if robot.detect() then
|
||||
mine_tree()
|
||||
end
|
||||
robot.turnAround()
|
||||
os.execute("sleep 5")
|
||||
move_width()
|
||||
collect_items()
|
||||
handle_interrupt()
|
||||
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…
Reference in new issue