Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. function place()
  2. while turtle.getItemCount() == 0 do
  3. turtle.select(turtle.getSelectedSlot() + 1)
  4. end
  5.  
  6. turtle.place()
  7. end
  8.  
  9. function createCircleGrid(radius)
  10. -- create the pixel array
  11. width = radius*2 + 1
  12. height = radius*2 + 1
  13. grid = {}
  14.  
  15. for i = 1, width do
  16. grid[i] = {}
  17.  
  18. for j = 1, height do
  19. grid[i][j] = 0
  20. end
  21. end
  22.  
  23. -- draw circle into grid
  24. x0 = radius
  25. y0 = radius
  26.  
  27. x = radius
  28. y = 0
  29. err = 1-x
  30.  
  31. while x >= y do
  32. grid[x+x0+1][y+y0+1] = 1
  33. grid[y+x0+1][x+y0+1] = 1
  34. grid[-x+x0+1][y+y0+1] = 1
  35. grid[-y+x0+1][x+y0+1] = 1
  36. grid[-x+x0+1][-y+y0+1] = 1
  37. grid[-y+x0+1][-x+y0+1] = 1
  38. grid[x+x0+1][-y+y0+1] = 1
  39. grid[y+x0+1][-x+y0+1] = 1
  40. y = y + 1
  41.  
  42. if err < 0 then
  43. err = err + 2*y + 1
  44. else
  45. x = x - 1
  46. err = err + 2*(y-x+1)
  47. end
  48. end
  49.  
  50. return grid
  51. end
  52.  
  53. function buildCircle(radius)
  54. grid = createCircleGrid(radius)
  55.  
  56. width = radius*2 + 1
  57. height = radius*2 + 1
  58.  
  59. -- build turtle steps (1-indexed x, y from here on)
  60. x = radius+1
  61. y = 1
  62. quarter = 1 -- top quarter, going around clockwise
  63.  
  64. forward = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}
  65. left = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}
  66. right = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}}
  67. change = {{1, 1}, {-1, 1}, {-1, -1}, {1, -1}}
  68.  
  69. function move(d)
  70. x = x + d[1]
  71. y = y + d[2]
  72. end
  73.  
  74. while 1 do
  75. if x == radius and y == 1 then
  76. -- finish it off
  77. turtle.turnLeft()
  78. turtle.back()
  79. place()
  80.  
  81. -- sit on top of the starting point
  82. turtle.up()
  83. turtle.forward()
  84. turtle.turnRight()
  85. turtle.back()
  86. turtle.back()
  87. break
  88. end
  89.  
  90. place()
  91.  
  92. -- initial move
  93. move(forward[quarter])
  94.  
  95. if grid[x][y] == 0 then
  96. if (quarter == 1 and (y > 1 and grid[x][y-1] ~= 0)) or
  97. (quarter == 2 and (x < width and grid[x+1][y] ~= 0)) or
  98. (quarter == 3 and (y < height and grid[x][y+1] ~= 0)) or
  99. (quarter == 4 and (x > 1 and grid[x-1][y] ~= 0)) then
  100.  
  101. turtle.turnLeft()
  102. turtle.back()
  103. turtle.turnRight()
  104.  
  105. move(left[quarter])
  106.  
  107. elseif (quarter == 1 and grid[x][y+1] ~= 0) or
  108. (quarter == 2 and grid[x-1][y] ~= 0) or
  109. (quarter == 3 and grid[x][y-1] ~= 0) or
  110. (quarter == 4 and grid[x+1][y] ~= 0) then
  111.  
  112. turtle.turnRight()
  113. turtle.back()
  114. turtle.turnLeft()
  115.  
  116. move(right[quarter])
  117. end
  118. end
  119.  
  120. turtle.back()
  121.  
  122. if (quarter == 1 and width - x + 1 <= y) or
  123. (quarter == 2 and x <= y) or
  124. (quarter == 3 and x <= height - y) or
  125. (quarter == 4 and x >= y) then
  126.  
  127. place()
  128. turtle.turnRight()
  129. turtle.back()
  130. turtle.back()
  131.  
  132. move(change[quarter])
  133. quarter = (quarter % 4) + 1
  134. end
  135. end
  136. end
  137.  
  138. function buildCylinder(radius, height)
  139. for i = 1, height do
  140. buildCircle(radius)
  141. end
  142. end
  143.  
  144. function buildDome(radius)
  145. -- create grid for identifying radius of each dome layer
  146. grid = createCircleGrid(radius)
  147.  
  148. prev_r = 0
  149.  
  150. -- work our way up the dome
  151. for i = 1, radius do
  152. -- find radius at this point
  153. y = radius - i + 1
  154.  
  155. x = 1
  156. while grid[x][y] == 0 do
  157. x = x + 1
  158. end
  159.  
  160. r = radius - x + 1
  161.  
  162. if prev_r > 0 then
  163. diff = prev_r - r
  164.  
  165. if diff > 0 then
  166. turtle.turnRight()
  167.  
  168. for j = 1, diff do
  169. turtle.back()
  170. end
  171.  
  172. turtle.turnLeft()
  173. end
  174. end
  175.  
  176. prev_r = r
  177.  
  178. -- build circle of this radius
  179. buildCircle(r)
  180. end
  181. end
  182.  
  183. buildDome(7)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement