ravneravn

enderTest

Nov 10th, 2013
53
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. area = 16
  2. turtleId = os.getComputerLabel()
  3.  
  4. args = {...}
  5. position = {}
  6. position["yStart"] = tonumber(args[1])
  7. position["currY"] = tonumber(args[1])
  8. position["currX"] = 1
  9. position["currZ"] = 1
  10. position["direction"] = 1
  11. position["currLayer"] = "na"
  12. position["valuable"] = 0
  13.  
  14. if args[3] == "manual" then
  15. position["enderchest"] = "no"
  16. else
  17. position["enderchest"] = "yes"
  18. end
  19.  
  20. function printPos()
  21. term.clear()
  22. term.setCursorPos(1,1)
  23. print("------------")
  24. print("x"..position["currX"])
  25. print("z"..position["currZ"])
  26. print("y"..position["currY"])
  27. print("direction "..position["direction"])
  28. print("layer "..position["currLayer"])
  29. print("------------")
  30. end
  31.  
  32. function moveForward()
  33. while not turtle.forward() do
  34. turtle.dig()
  35. end
  36. printPos()
  37. end
  38.  
  39. function moveUp()
  40. while not turtle.up() do
  41. turtle.digUp()
  42. end
  43. position["currY"] = position["currY"]+1
  44. printPos()
  45. end
  46.  
  47. function moveDown()
  48. while not turtle.down() do
  49. turtle.digDown()
  50. end
  51. position["currY"] = position["currY"]-1
  52. printPos()
  53. end
  54.  
  55. function turnRight()
  56. turtle.turnRight()
  57. position["direction"] = position["direction"] +1
  58. if position["direction"] > 4 then position["direction"] = 1
  59. end
  60. savePos()
  61. printPos()
  62. end
  63.  
  64. function turnLeft()
  65. turtle.turnLeft()
  66. position["direction"] = position["direction"] -1
  67. if position["direction"] < 1 then position["direction"] = 4
  68. end
  69. savePos()
  70. printPos()
  71. end
  72.  
  73.  
  74.  
  75. function compareUp()
  76. for i = 1, 3 do
  77. turtle.select(i)
  78. turtle.compareUp()
  79. if turtle.compareUp() == true then
  80. match = 1
  81. else
  82. turtle.digUp()
  83. position["valuable"] = position["valuable"] +1
  84. end
  85. end
  86. end
  87.  
  88.  
  89.  
  90. function compareDown()
  91. for i = 1, 3 do
  92. turtle.select(i)
  93. turtle.compareDown()
  94. if turtle.compareDown() == true then
  95. match = 1
  96. else
  97. turtle.digDown()
  98. position["valuable"] = position["valuable"] +1
  99. end
  100. end
  101. end
  102.  
  103.  
  104.  
  105.  
  106. function dropOff()
  107. if position["enderchest"] == "yes" then
  108. stone = turtle.getItemCount(4)
  109. turtle.select(4)
  110. turtle.drop(stone-1)
  111. turtle.dig()
  112. turtle.select(16)
  113. turtle.place()
  114. for i = 5, 15 do
  115. turtle.select(i)
  116. turtle.drop()
  117. end
  118. turtle.select(16)
  119. turtle.dig()
  120.  
  121. elseif position["enderchest"] == "no" then
  122. if turtle.getItemCount(15) > 0 then
  123. stone = turtle.getItemCount(4)
  124. turtle.select(4)
  125. turtle.drop(stone-1)
  126. findPos()
  127. savePos()
  128. position["resumeY"] = tonumber(position["currY"])
  129. toSurface()
  130. turnRight()
  131. turnRight()
  132. for i = 5, 15 do
  133. turtle.select(i)
  134. turtle.drop()
  135. end
  136. turnRight()
  137. turnRight()
  138. while position["currY"] > position["resumeY"] do
  139. moveDown()
  140. printPos()
  141. savePos()
  142. end
  143. os.reboot()
  144. end
  145. end
  146. end
  147.  
  148.  
  149.  
  150.  
  151.  
  152. function mineRowXPlus()
  153. --for i = 1, area do
  154. while position["currX"] < area do
  155. moveForward()
  156. position["currX"] = position["currX"] +1
  157. savePos()
  158. printPos()
  159. compareUp()
  160. compareDown()
  161. end
  162. turnRight()
  163. dropOff()
  164. moveForward()
  165. compareUp()
  166. compareDown()
  167. position["currZ"] = position["currZ"] +1
  168. savePos()
  169. turnRight()
  170. end
  171.  
  172. function mineRowXMinus()
  173. --for i = 1, area do
  174. while position["currX"] > 1 do
  175. moveForward()
  176. position["currX"] = position["currX"] -1
  177. savePos()
  178. printPos()
  179. compareUp()
  180. compareDown()
  181. end
  182. turnLeft()
  183. dropOff()
  184. moveForward()
  185. compareUp()
  186. compareDown()
  187. position["currZ"] = position["currZ"] +1
  188. savePos()
  189. turnLeft()
  190. end
  191.  
  192. function mineLayer()
  193. --for i = 1, area/2 do
  194. while position["currZ"] < area do
  195. mineRowXPlus()
  196. mineRowXMinus()
  197. end
  198. findPos()
  199. end
  200.  
  201.  
  202. function newLayer()
  203. for i = 1, 3 do
  204. moveUp()
  205. printPos()
  206. savePos()
  207. end
  208. position["currLayer"] = position["currLayer"] + 1
  209. savePos()
  210. sendBroadcast("Starting layer number: "..position["currLayer"]..", Y:"..position["currY"])
  211. sendBroadcast("current valuables: "..position["valuable"])
  212. end
  213.  
  214.  
  215.  
  216. function toBedrock()
  217. mineStart = position["yStart"] - 6
  218. for i = 1, mineStart do
  219. turtle.digDown()
  220. turtle.down()
  221. position["currY"] = position["currY"] -1
  222. printPos()
  223. savePos()
  224. end
  225. position["currLayer"] = 1
  226. savePos()
  227. position["currY"] = 6
  228. savePos()
  229. sendBroadcast("Bottom reached")
  230. printPos()
  231. end
  232.  
  233.  
  234.  
  235. function toSurface()
  236. findPos()
  237. sendBroadcast("Going up")
  238. --for i = 1, position["yStart"] - position["currY"] do
  239. while tonumber(position["currY"]) < tonumber(position["yStart"]) do
  240. moveUp()
  241. savePos()
  242. end
  243. --if tonumber(position["currY"]) > tonumber(position["yStart"]) then
  244. while tonumber(position["currY"]) > tonumber(position["yStart"]) do
  245. turtle.down()
  246. position["currY"] = position["currY"] -1
  247. printPos()
  248. savePos()
  249. end
  250. --end
  251. end
  252.  
  253.  
  254. function sendBroadcast(message)
  255. rednet.open("right")
  256. rednet.broadcast("["..turtleId.."] "..message)
  257. end
  258.  
  259.  
  260.  
  261.  
  262. function findPos()
  263. while position["direction"] ~= 3 do
  264. turnRight()
  265. savePos()
  266. end
  267. while position["currX"] > 1 do
  268. moveForward()
  269. position["currX"] = position["currX"] -1
  270. savePos()
  271. end
  272. while position["direction"] ~= 4 do
  273. turnRight()
  274. savePos()
  275. end
  276. while position["currZ"] > 1 do
  277. moveForward()
  278. position["currZ"] = position["currZ"] -1
  279. savePos()
  280. end
  281. while position["direction"] ~= 1 do
  282. turnRight()
  283. savePos()
  284. end
  285. end
  286.  
  287.  
  288.  
  289. function resume()
  290. sendBroadcast("Resuming, Y:"..position["currY"])
  291. turtle.select(16)
  292. turtle.dig()
  293. layersToGo = math.floor((position["yStart"] - position["currY"])/4)
  294. for i = 1, layersToGo do
  295. mineLayer()
  296. newLayer()
  297. if position["currY"] > 50 then
  298. break
  299. end
  300. end
  301. position["status"] = "off"
  302. toSurface()
  303. end
  304.  
  305.  
  306.  
  307.  
  308.  
  309. -- ## WRITE FILE ##
  310. function savePos()
  311. handle = fs.open("enderMine/savedPosition", "w")
  312. handle.write(textutils.serialize(position))
  313. handle.close()
  314. end
  315.  
  316. function loadPos()
  317. handle = fs.open("enderMine/savedPosition", "r")
  318. readFile = handle.readLine()
  319. if readFile ~= nil then
  320. position = textutils.unserialize(readFile)
  321. end
  322. handle.close()
  323. end
  324.  
  325.  
  326.  
  327.  
  328. --------------- LOOP
  329.  
  330. if args[2] == "on" then
  331. --if position["status"] == "on" then
  332. loadPos()
  333. findPos()
  334. resume()
  335. position["status"] = nil
  336. position["status"] = "off"
  337. os.reboot()
  338. end
  339.  
  340. savePos()
  341. position["status"] = "on"
  342. layers = math.floor(position["yStart"] / 4)
  343. position["currY"] = position["yStart"]
  344. toBedrock()
  345. for i = 1, layers do
  346. mineLayer()
  347. newLayer()
  348. if position["currY"] > 50 then
  349. break
  350. end
  351. end
  352. position["status"] = "off"
  353. toSurface()
RAW Paste Data