tompy90

Spiral.Final

May 6th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.40 KB | None | 0 0
  1. os.loadAPI("persistence")
  2. currentFace = persistence.pull("currentFace")
  3. currentRadius = persistence.pull("currentRadius")
  4. endRadius = persistence.pull("endRadius")
  5. currentX = persistence.pull("currentX")
  6. currentZ = persistence.pull("currentZ")
  7. digCount = persistence.pull("digCount")
  8.  
  9.  
  10. --functions
  11.  
  12. function get()
  13. term.clear()
  14. term.setCursorPos(1,1)
  15. print("currentFace: "..persistence.pull("currentFace"))
  16. print("currentRadius: "..persistence.pull("currentRadius"))
  17. print("endRadius: "..persistence.pull("endRadius"))
  18. print("currentX: "..persistence.pull("currentX"))
  19. print("currentZ: "..persistence.pull("currentZ"))
  20. print("digCount: "..persistence.pull("digCount"))
  21. print("fuelLevel: "..turtle.getFuelLevel())
  22.  
  23. end
  24.  
  25. function left()
  26. turtle.turnLeft()
  27. currentFace = persistence.pull("currentFace")
  28. if currentFace == 0 then
  29. currentFace = persistence.store("currentFace",3)
  30. return
  31. end
  32. if currentFace == 1 then
  33. currentFace = persistence.store("currentFace",0)
  34. return
  35. end
  36. if currentFace == 2 then
  37. currentFace = persistence.store("currentFace",1)
  38. return
  39. end
  40. if currentFace == 3 then
  41. currentFace = persistence.store("currentFace",2)
  42. return
  43. end
  44. end
  45.  
  46. function right()
  47. currentFace = persistence.pull("currentFace")
  48. turtle.turnRight()
  49. if currentFace == 0 then
  50. currentFace = persistence.store("currentFace",1)
  51. return
  52. end
  53. if currentFace == 1 then
  54. currentFace = persistence.store("currentFace",2)
  55. return
  56. end
  57. if currentFace == 2 then
  58. currentFace = persistence.store("currentFace",3)
  59. return
  60. end
  61. if currentFace == 3 then
  62. currentFace = persistence.store("currentFace",0)
  63. return
  64. end
  65. end
  66.  
  67. function face0()
  68. call()
  69. if currentFace == 1 then
  70. left()
  71. return
  72. end
  73. if currentFace == 2 then
  74. left()
  75. left()
  76. return
  77. end
  78. if currentFace == 3 then
  79. right()
  80. return
  81. end
  82. end
  83.  
  84. function face1()
  85. call()
  86. if currentFace == 2 then
  87. left()
  88. return
  89. end
  90. if currentFace == 3 then
  91. left()
  92. left()
  93. return
  94. end
  95. if currentFace == 0 then
  96. right()
  97. return
  98. end
  99. end
  100.  
  101. function face2()
  102. call()
  103. if currentFace == 3 then
  104. left()
  105. return
  106. end
  107. if currentFace == 0 then
  108. left()
  109. left()
  110. return
  111. end
  112. if currentFace == 1 then
  113. right()
  114. return
  115. end
  116. end
  117.  
  118. function face3()
  119. call()
  120. if currentFace == 0 then
  121. left()
  122. return
  123. end
  124. if currentFace == 1 then
  125. left()
  126. left()
  127. return
  128. end
  129. if currentFace == 2 then
  130. right()
  131. return
  132. end
  133. end
  134.  
  135. function forward()
  136. currentFace = persistence.pull("currentFace")
  137. get()
  138.  
  139. checkFuel()
  140. checkInv()
  141. while not turtle.forward() do
  142. checkInv()
  143. if turtle.dig() then
  144. digCount = persistence.pull("digCount")
  145. digCount = persistence.store("digCount",tonumber(digCount)+1)
  146. elseif not turtle.attack() then
  147. print("Something is blocking me")
  148. sleep(10)
  149. end
  150. end
  151. currentX = persistence.pull("currentX")
  152. currentZ = persistence.pull("currentZ")
  153. if currentFace == 0 then
  154. currentZ = persistence.store("currentZ",currentZ+1)
  155. end
  156. if currentFace == 1 then
  157. currentX = persistence.store("currentX",currentX-1)
  158. end
  159. if currentFace == 2 then
  160. currentZ = persistence.store("currentZ",currentZ-1)
  161. end
  162. if currentFace == 3 then
  163. currentX = persistence.store("currentX",currentX+1)
  164. end
  165. get()
  166. end
  167.  
  168. function dig()
  169. digCount = persistence.pull("digCount")
  170. checkInv()
  171. if turtle.digDown() then
  172. digCount = persistence.store("digCount", digCount+1)
  173. end
  174. while turtle.detectUp() do
  175. digCount = persistence.pull("digCount")
  176. checkInv()
  177. if turtle.digUp() then
  178. digCount = persistence.store("digCount",digCount+1)
  179. sleep(0.8)
  180. else
  181. print("I can't dig up")
  182. sleep(10)
  183. end
  184. end
  185. end
  186.  
  187. function checkFuel()
  188. if turtle.getFuelLevel() < 10 then
  189. refuel()
  190. end
  191. end
  192.  
  193. function checkInv()
  194. if turtle.getItemCount(15) < 1 then --checks to see if refuel chest is present
  195. refuel()
  196. end
  197. if turtle.getItemCount(16) < 1 then --checks to see if unload chest is present
  198. unload()
  199. end
  200. for i=13,14 do
  201. if turtle.getItemCount(i) > 0 then -- checks slots 13 and 14 for items
  202. unload()
  203. end
  204. end
  205. end
  206.  
  207. function unload()
  208. if turtle.getItemCount(16) > 0 then --if the unload chest is present
  209. if turtle.detectDown() then
  210. digCount = persistence.pull("digCount")
  211. while not turtle.digDown() and turtle.detectDown() do
  212. term.clear()
  213. term.setCursorPos(1,1)
  214. print("I can't dig down")
  215. sleep(10)
  216. end
  217. digCount = persistence.store("digCount",digCount+1)
  218. end
  219. end
  220. if turtle.getItemCount(16) < 1 then --if unload chest is missing
  221. if not turtle.detectDown() then --the chest isn't in inv or below
  222. print("I'VE LOST MY CHEST!!!")
  223. while turtle.getItemCount(16) < 1 do
  224. term.clear()
  225. term.setCursorPos(1,1)
  226. print("please put an unload chest in slot(16)...")
  227. sleep(10)
  228. end
  229. end
  230. if turtle.detectDown() then --the chest is below the turtle
  231. turtle.select(16)
  232. turtle.digDown()
  233. turtle.select(1)
  234. end
  235. end
  236. turtle.select(16)
  237. turtle.placeDown()
  238. for i=1,14 do
  239. while turtle.getItemCount(i) > 0 do
  240. turtle.select(i)
  241. if not turtle.dropDown() then
  242. sleep(10) -- waiting for space in chest
  243. end
  244. end
  245. end
  246. turtle.select(16)
  247. turtle.digDown()
  248. turtle.select(1)
  249. end
  250.  
  251. function refuel()
  252. unload()
  253. if turtle.getItemCount(15) > 0 then --Refuel chest is present
  254. if turtle.detectUp() then
  255. digCount = persistence.pull("digCount")
  256. while not turtle.digUp() and turtle.detectUp() do
  257. term.clear()
  258. term.setCursorPos(1,1)
  259. print("I can't dig up for some reason")
  260. sleep(10)
  261. end
  262. digCount = persistence.store("digCount",digCount+1)
  263. end
  264. end
  265. if turtle.getItemCount(15) < 1 then --if refuel chest isn't in Inv
  266. if not turtle.detectUp() then
  267. term.clear()
  268. term.setCursorPos(1,1)
  269. print("I've lost my refuel chest")
  270. while turtle.getItemCount(15) < 1 do
  271. term.clear()
  272. term.setCursorPos(1,1)
  273. print("please put refuel chest in slot(15)")
  274. sleep(10)
  275. end
  276. end
  277. if turtle.detectUp() then
  278. turtle.select(15)
  279. turtle.digUp()
  280. end
  281. end
  282. turtle.select(15)
  283. turtle.placeUp()
  284. while turtle.getFuelLevel() < 500 do
  285. turtle.select(1)
  286. if not turtle.suckUp() then
  287. term.clear()
  288. term.setCursorPos(1,1)
  289. print("Waiting for more fuel in refuel chest")
  290. sleep(10)
  291. end
  292. if turtle.getItemCount(1) > 0 then
  293. turtle.refuel(turtle.getItemCount(1))
  294. end
  295. end
  296. turtle.select(15)
  297. turtle.digUp()
  298. turtle.select(1)
  299. end
  300.  
  301. function call()
  302. currentX = persistence.pull("currentX")
  303. currentZ = persistence.pull("currentZ")
  304. currenFace = persistence.pull("currentFace")
  305. digCount = persistence.pull("digCount")
  306. currentRadius = persistence.pull("currentRadius")
  307. endRadius = persistence.pull("endRadius")
  308. end
  309.  
  310. function spiral()
  311. call()
  312. if currentX==0 and currentZ==0 then
  313. if currentFace ~= 0 then
  314. face0()
  315. end
  316. print("at start")
  317. dig()
  318. forward()
  319. left()
  320. dig()
  321. currentRadius = persistence.store("currentRadius",currentRadius+1)
  322. call()
  323. get()
  324. return
  325. elseif math.abs(currentX) == math.abs(currentZ) then
  326. if currentZ > 0 and currentX < 0 then
  327. if currentFace==0 then
  328. dig()
  329. forward()
  330. face3()
  331. dig()
  332. forward()
  333. currentRadius = persistence.store("currentRadius",currentRadius+1)
  334. call()
  335. get()
  336. return
  337. else
  338. face3()
  339. dig()
  340. forward()
  341. return
  342. end
  343. elseif currentX>0 and currentZ>0 then
  344. face2()
  345. dig()
  346. forward()
  347. return
  348. elseif currentX>0 and currentZ<0 then
  349. face1()
  350. dig()
  351. forward()
  352. return
  353. elseif currentX<0 and currentZ<0 then
  354. face0()
  355. dig()
  356. forward()
  357. return
  358. end
  359. else
  360. dig()
  361. forward()
  362. return
  363. end
  364. end
  365.  
  366. function spiralMine()
  367. call()
  368. while tonumber(currentRadius) < tonumber(endRadius) do
  369. spiral()
  370. end
  371. end
  372.  
  373. spiralMine()
Advertisement
Add Comment
Please, Sign In to add comment