Trinsdar

turtle circle program

Sep 7th, 2020 (edited)
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.78 KB | None | 0 0
  1. print("please enter radius")
  2. radius = tonumber(read())
  3.  
  4. -- Record Keeping Variables: These are for recoding the blocks and fuel used
  5. local blocks = 0
  6. local fuel = 0
  7.  
  8. local facing = 0
  9.  
  10. function checkFuel()
  11. if (not(tonumber(turtle.getFuelLevel()) == nil)) then
  12. while turtle.getFuelLevel() < 50 do
  13. writeOut("Turtle almost out of fuel, pausing. Please drop fuel in inventory. And press enter.")
  14. io.read()
  15. turtle.refuel()
  16. end
  17. end
  18. end
  19.  
  20. function simulationCheck() -- checks state of the simulation
  21. if sim_mode then
  22. if compareProgress() then
  23. setSimFlags(false) -- If we're caught up, un-set flags
  24. else
  25. setSimFlags(true) -- If not caught up, just re-affirm that the flags are set
  26. end
  27. end
  28. end
  29.  
  30. function progressUpdate() -- This ONLY updates the local table variable. Writing is handled above. -- I want to change this to allow for any number of params
  31. progTable = {shape = choice, enderchest_refilling = tempProgTable.enderchest_refilling, param1 = tempProgTable.param1, param2 = tempProgTable.param2, param3 = tempProgTable.param3, param4 = tempProgTable.param4, x = positionX, y = positionY, z = positionZ, facing = facing, blocks = blocks}
  32. if not sim_mode then
  33. writeProgress()
  34. end
  35. end
  36.  
  37. function placeBlock()
  38. blocks = blocks + 1
  39. simulationCheck()
  40. if cost_only then
  41. return
  42. end
  43. if turtle.detectDown() and not turtle.compareDown() then
  44. turtle.digDown()
  45. end
  46. checkResources()
  47. turtle.placeDown()
  48. progressUpdate()
  49. end
  50.  
  51. function turnRightTrack()
  52. simulationCheck()
  53. facing = facing + 1
  54. if facing >= 4 then
  55. facing = 0
  56. end
  57. progressUpdate()
  58. if cost_only then
  59. return
  60. end
  61. turtle.turnRight()
  62. end
  63.  
  64. function turnLeftTrack()
  65. simulationCheck()
  66. facing = facing - 1
  67. if facing < 0 then
  68. facing = 3
  69. end
  70. progressUpdate()
  71. if cost_only then
  72. return
  73. end
  74. turtle.turnLeft()
  75. end
  76.  
  77. function turnAroundTrack()
  78. turnLeftTrack()
  79. turnLeftTrack()
  80. end
  81.  
  82. function safeForward()
  83. simulationCheck()
  84. if facing == 0 then
  85. positionY = positionY + 1
  86. elseif facing == 1 then
  87. positionX = positionX + 1
  88. elseif facing == 2 then
  89. positionY = positionY - 1
  90. elseif facing == 3 then
  91. positionX = positionX - 1
  92. end
  93. fuel = fuel + 1
  94. progressUpdate()
  95. if cost_only then
  96. return
  97. end
  98. checkFuel()
  99. local success = false
  100. local tries = 0
  101. while not success do
  102. success = turtle.forward()
  103. if not success then
  104. while (not success) and tries < 6 do
  105. tries = tries + 1
  106. turtle.dig()
  107. success = turtle.forward()
  108. sleep(0.3)
  109. end
  110. if not success then
  111. writeOut("Blocked attempting to move forward.")
  112. writeOut("Please clear and press enter to continue.")
  113. io.read()
  114. end
  115. end
  116. end
  117. end
  118.  
  119. function safeBack()
  120. simulationCheck()
  121. if facing == 0 then
  122. positionY = positionY - 1
  123. elseif facing == 1 then
  124. positionX = positionX - 1
  125. elseif facing == 2 then
  126. positionY = positionY + 1
  127. elseif facing == 3 then
  128. positionX = positionX + 1
  129. end
  130. fuel = fuel + 1
  131. progressUpdate()
  132. if cost_only then
  133. return
  134. end
  135. checkFuel()
  136. local success = false
  137. local tries = 0
  138. while not success do
  139. success = turtle.back()
  140. if not success then
  141. turnAroundTrack()
  142. while turtle.detect() and tries < 6 do
  143. tries = tries + 1
  144. if turtle.dig() then
  145. break
  146. end
  147. sleep(0.3)
  148. end
  149. turnAroundTrack()
  150. success = turtle.back()
  151. if not success then
  152. writeOut("Blocked attempting to move back.")
  153. writeOut("Please clear and press enter to continue.")
  154. io.read()
  155. end
  156. end
  157. end
  158. end
  159.  
  160. function safeUp()
  161. simulationCheck()
  162. positionZ = positionZ + 1
  163. fuel = fuel + 1
  164. progressUpdate()
  165. if cost_only then
  166. return
  167. end
  168. checkFuel()
  169. local success = false
  170. while not success do
  171. success = turtle.up()
  172. if not success then
  173. while turtle.detectUp() do
  174. if not turtle.digUp() then
  175. writeOut("Blocked attempting to move up.")
  176. writeOut("Please clear and press enter to continue.")
  177. io.read()
  178. end
  179. end
  180. end
  181. end
  182. end
  183.  
  184. function safeDown()
  185. simulationCheck()
  186. positionZ = positionZ - 1
  187. fuel = fuel + 1
  188. progressUpdate()
  189. if cost_only then
  190. return
  191. end
  192. checkFuel()
  193. local success = false
  194. while not success do
  195. success = turtle.down()
  196. if not success then
  197. while turtle.detectDown() do
  198. if not turtle.digDown() then
  199. writeOut("Blocked attempting to move down.")
  200. writeOut("Please clear and press enter to continue.")
  201. io.read()
  202. end
  203. end
  204. end
  205. end
  206. end
  207.  
  208. function moveY(targetY)
  209. if targetY == positionY then
  210. return
  211. end
  212. if (facing ~= 0 and facing ~= 2) then -- Check axis
  213. turnRightTrack()
  214. end
  215. while targetY > positionY do
  216. if facing == 0 then
  217. safeForward()
  218. else
  219. safeBack()
  220. end
  221. end
  222. while targetY < positionY do
  223. if facing == 2 then
  224. safeForward()
  225. else
  226. safeBack()
  227. end
  228. end
  229. end
  230.  
  231. function moveX(targetX)
  232. if targetX == positionX then
  233. return
  234. end
  235. if (facing ~= 1 and facing ~= 3) then -- Check axis
  236. turnRightTrack()
  237. end
  238. while targetX > positionX do
  239. if facing == 1 then
  240. safeForward()
  241. else
  242. safeBack()
  243. end
  244. end
  245. while targetX < positionX do
  246. if facing == 3 then
  247. safeForward()
  248. else
  249. safeBack()
  250. end
  251. end
  252. end
  253.  
  254. function moveZ(targetZ)
  255. if targetZ == positionZ then
  256. return
  257. end
  258. while targetZ < positionZ do
  259. safeDown()
  260. end
  261. while targetZ > positionZ do
  262. safeUp()
  263. end
  264. end
  265.  
  266. function navigateTo(targetX, targetY, targetZ, move_z_first)
  267. targetZ = targetZ or positionZ -- If targetZ isn't used in the function call, it defaults to its current z position, this should make it compatible with all previous implementations of navigateTo()
  268. move_z_first = move_z_first or false -- Defaults to moving z last, if true is passed as 4th argument, it moves vertically first
  269.  
  270. if move_z_first then
  271. moveZ(targetZ)
  272. end
  273.  
  274. if facing == 0 or facing == 2 then -- Y axis
  275. moveY(targetY)
  276. moveX(targetX)
  277. else
  278. moveX(targetX)
  279. moveY(targetY)
  280. end
  281.  
  282. if not move_z_first then
  283. moveZ(targetZ)
  284. end
  285. end
  286.  
  287. function blockInSphereIsFull(offset, x, y, z, radiusSq)
  288. x = x - offset
  289. y = y - offset
  290. z = z - offset
  291. x = x ^ 2
  292. y = y ^ 2
  293. z = z ^ 2
  294. return x + y + z <= radiusSq
  295. end
  296.  
  297.  
  298. function isSphereBorder(offset, x, y, z, radiusSq)
  299. spot = blockInSphereIsFull(offset, x, y, z, radiusSq)
  300. if spot then
  301. spot = not blockInSphereIsFull(offset, x, y - 1, z, radiusSq) or
  302. not blockInSphereIsFull(offset, x, y + 1, z, radiusSq) or
  303. not blockInSphereIsFull(offset, x - 1, y, z, radiusSq) or
  304. not blockInSphereIsFull(offset, x + 1, y, z, radiusSq) or
  305. not blockInSphereIsFull(offset, x, y, z - 1, radiusSq) or
  306. not blockInSphereIsFull(offset, x, y, z + 1, radiusSq)
  307. end
  308. return spot
  309. end
  310.  
  311. function circle(diameter)
  312. odd = not (math.fmod(diameter, 2) == 0)
  313. radius = diameter / 2
  314. if odd then
  315. width = (2 * math.ceil(radius)) + 1
  316. offset = math.floor(width/2)
  317. else
  318. width = (2 * math.ceil(radius)) + 2
  319. offset = math.floor(width/2) - 0.5
  320. end
  321. --diameter --radius * 2 + 1
  322. sqrt3 = 3 ^ 0.5
  323. boundaryRadius = radius + 1.0
  324. boundary2 = boundaryRadius ^ 2
  325. radius2 = radius ^ 2
  326. z = math.floor(radius)
  327. cz2 = (radius - z) ^ 2
  328. limitOffsetY = (boundary2 - cz2) ^ 0.5
  329. maxOffsetY = math.ceil(limitOffsetY)
  330. -- We do first the +x side, then the -x side to make movement efficient
  331. for side = 0,1 do
  332. -- On the right we go from small y to large y, on the left reversed
  333. -- This makes us travel clockwise (from below) around each layer
  334. if (side == 0) then
  335. yStart = math.floor(radius) - maxOffsetY
  336. yEnd = math.floor(radius) + maxOffsetY
  337. yStep = 1
  338. else
  339. yStart = math.floor(radius) + maxOffsetY
  340. yEnd = math.floor(radius) - maxOffsetY
  341. yStep = -1
  342. end
  343. for y = yStart,yEnd,yStep do
  344. cy2 = (radius - y) ^ 2
  345. remainder2 = (boundary2 - cz2 - cy2)
  346. if remainder2 >= 0 then
  347. -- This is the maximum difference in x from the centre we can be without definitely being outside the radius
  348. maxOffsetX = math.ceil((boundary2 - cz2 - cy2) ^ 0.5)
  349. -- Only do either the +x or -x side
  350. if (side == 0) then
  351. -- +x side
  352. xStart = math.floor(radius)
  353. xEnd = math.floor(radius) + maxOffsetX
  354. else
  355. -- -x side
  356. xStart = math.floor(radius) - maxOffsetX
  357. xEnd = math.floor(radius) - 1
  358. end
  359. -- Reverse direction we traverse xs when in -y side
  360. if y > math.floor(radius) then
  361. temp = xStart
  362. xStart = xEnd
  363. xEnd = temp
  364. xStep = -1
  365. else
  366. xStep = 1
  367. end
  368.  
  369. for x = xStart,xEnd,xStep do
  370. -- Only blocks within the radius but still within 1 3d-diagonal block of the edge are eligible
  371. if isSphereBorder(offset, x, y, z, radius2) then
  372. navigateTo(x, y)
  373. placeBlock()
  374. end
  375. end
  376. end
  377. end
  378. end
  379. end
  380.  
  381. checkFuel()
  382. circle(radius * 2)
Advertisement
Add Comment
Please, Sign In to add comment