Advertisement
asweigart

hare (twitch tv)

Oct 21st, 2016
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1.  
  2. function sweep(rows, columns, sweepFunc)
  3. assert(type(rows) == 'number' and rows >= 1)
  4. assert(type(columns) == 'number' and columns >= 1)
  5.  
  6. -- "forward" is "north" and decreases z
  7. -- "back" is "south" and increases z
  8. -- "right" is "east" and increases x
  9. local x = 0
  10. local z = 0
  11.  
  12. local turnRightNext = true
  13.  
  14. -- call the sweep function at the starting point:
  15. if sweepFunc ~= nil then
  16. sweepFunc(x, z)
  17. end
  18.  
  19. for curColumn = 1, columns do
  20. -- move up/down the row:
  21. for curRow = 2, rows do
  22. turtle.forward() -- TODO: handle if turtle can't move forward
  23.  
  24. -- update the relative z coordinate:
  25. if turnRightNext then
  26. z = z - 1
  27. else
  28. z = z + 1
  29. end
  30.  
  31. if sweepFunc ~= nil then
  32. sweepFunc(x, z) -- call the sweep function once per point in the rectangular area
  33. end
  34. end
  35.  
  36. -- turn and move to the next column
  37. if curColumn ~= columns then
  38. if turnRightNext then
  39. turtle.turnRight()
  40. turtle.forward() -- TODO: handle if turtle can't move forward
  41. turtle.turnRight()
  42. else
  43. turtle.turnLeft()
  44. turtle.forward() -- TODO: handle if turtle can't move forward
  45. turtle.turnLeft()
  46. end
  47. x = x + 1
  48. if sweepFunc ~= nil then
  49. sweepFunc(x, z)
  50. end
  51. turnRightNext = not turnRightNext -- toggles turning direction
  52. end
  53. end
  54.  
  55. -- move back to starting point
  56. local i
  57. if columns % 2 == 1 then -- when columns is odd, we end at the far end of the rows
  58. for i = 1, rows-1 do
  59. turtle.back() -- TODO: handle if turtle can't move back
  60. end
  61. turtle.turnLeft()
  62. else
  63. turtle.turnRight()
  64. end
  65.  
  66. for i = 1, columns-1 do
  67. turtle.forward() -- TODO: handle if turtle can't move forward
  68. end
  69.  
  70. turtle.turnRight() -- face the original direction
  71. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement