Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function sweep(rows, columns, sweepFunc)
- assert(type(rows) == 'number' and rows >= 1)
- assert(type(columns) == 'number' and columns >= 1)
- -- "forward" is "north" and decreases z
- -- "back" is "south" and increases z
- -- "right" is "east" and increases x
- local x = 0
- local z = 0
- local turnRightNext = true
- -- call the sweep function at the starting point:
- if sweepFunc ~= nil then
- sweepFunc(x, z)
- end
- for curColumn = 1, columns do
- -- move up/down the row:
- for curRow = 2, rows do
- turtle.forward() -- TODO: handle if turtle can't move forward
- -- update the relative z coordinate:
- if turnRightNext then
- z = z - 1
- else
- z = z + 1
- end
- if sweepFunc ~= nil then
- sweepFunc(x, z) -- call the sweep function once per point in the rectangular area
- end
- end
- -- turn and move to the next column
- if curColumn ~= columns then
- if turnRightNext then
- turtle.turnRight()
- turtle.forward() -- TODO: handle if turtle can't move forward
- turtle.turnRight()
- else
- turtle.turnLeft()
- turtle.forward() -- TODO: handle if turtle can't move forward
- turtle.turnLeft()
- end
- x = x + 1
- if sweepFunc ~= nil then
- sweepFunc(x, z)
- end
- turnRightNext = not turnRightNext -- toggles turning direction
- end
- end
- -- move back to starting point
- local i
- if columns % 2 == 1 then -- when columns is odd, we end at the far end of the rows
- for i = 1, rows-1 do
- turtle.back() -- TODO: handle if turtle can't move back
- end
- turtle.turnLeft()
- else
- turtle.turnRight()
- end
- for i = 1, columns-1 do
- turtle.forward() -- TODO: handle if turtle can't move forward
- end
- turtle.turnRight() -- face the original direction
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement