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.

96 lines
2.0 KiB

local robot = require('robot')
local nav = require('nav')
local io = require('io')
local filename = ...
if not filename then
print('Usage: build_circle <filename>')
print('The robot starts in the center')
return 1
end
local file = io.open(filename, 'r')
if not file then
print('File not found')
return 2
end
local radius = tonumber(file:read("*l"))
if not radius then
print('Requires radius on the first line')
return 3
end
local rings = {}
-- parse file:
for line in file:lines() do
local ring = {}
for c in string.gmatch(line, ".") do
local n = tonumber(c)
if c == 'X' then
ring[#ring + 1] = c
elseif n then
ring[#ring + 1] = n
else
print('Invalid sequence: ' .. c)
return 4
end
end
rings[#rings + 1] = ring
end
file:close()
local inverted = false
robot.select(1)
-- build:
nav.forward(radius)
robot.turnRight()
for _, ring in ipairs(rings) do
for _ = 1, 4 do
inverted = false
for i, n in ipairs(ring) do
if n == 'X' then
robot.turnRight()
inverted = true
else
-- place sequence
for _ = 1, n do
robot.placeDown()
if robot.count() == 0 then
robot.select(robot.select() + 1) -- select the next item slot
end
nav.forward()
end
if i ~= #ring then
-- step level
if not inverted then
robot.turnRight()
nav.forward()
robot.turnLeft()
else
robot.turnLeft()
nav.forward()
robot.turnRight()
end
end
end
end
end
robot.turnLeft()
nav.forward()
robot.turnRight()
end
robot.turnRight()
nav.forward(radius + #rings)
robot.turnAround()