ravneravn

enderMine m. pos

Aug 13th, 2013
115
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. area = 15
  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. --#### COMPARE UP ####
  76. function compareUp()
  77. turtle.select(1)
  78. turtle.compareUp()
  79. if turtle.compareUp() == true then
  80. notinuse = 1
  81. else
  82.  
  83. turtle.select(2)
  84. turtle.compareUp()
  85. if turtle.compareUp() == true then
  86. notinuse = 1
  87. else
  88.  
  89. turtle.select(3)
  90. turtle.compareUp()
  91. if turtle.compareUp() == true then
  92. notinuse = 1
  93. else
  94.  
  95.  
  96. turtle.digUp()
  97. position["valuable"] = position["valuable"] +1
  98. end
  99. end
  100. end
  101. end
  102.  
  103.  
  104. --#### COMPARE DOWN ####
  105. function compareDown()
  106. turtle.select(1)
  107. turtle.compareDown()
  108. if turtle.compareDown() == true then
  109. notinuse = 1
  110.  
  111. else
  112.  
  113. turtle.select(2)
  114. turtle.compareDown()
  115. if turtle.compareDown() == true then
  116. notinuse = 1
  117.  
  118. else
  119.  
  120. turtle.select(3)
  121. turtle.compareDown()
  122. if turtle.compareDown() == true then
  123. notinuse = 1
  124.  
  125. else
  126.  
  127. turtle.digDown()
  128. position["valuable"] = position["valuable"] +1
  129. end
  130. end
  131. end
  132. end
  133.  
  134.  
  135.  
  136.  
  137.  
  138. function dropOff()
  139. if position["enderchest"] == "yes" then
  140. stone = turtle.getItemCount(4)
  141. turtle.select(4)
  142. turtle.drop(stone-1)
  143. turtle.dig()
  144. turtle.select(16)
  145. turtle.place()
  146. for i = 5, 15 do
  147. turtle.select(i)
  148. turtle.drop()
  149. end
  150. turtle.select(16)
  151. turtle.dig()
  152.  
  153. elseif position["enderchest"] == "no" then
  154. if turtle.getItemCount(15) > 0 then
  155. stone = turtle.getItemCount(4)
  156. turtle.select(4)
  157. turtle.drop(stone-1)
  158. findPos()
  159. savePos()
  160. position["resumeY"] = tonumber(position["currY"])
  161. toSurface()
  162. turnRight()
  163. turnRight()
  164. for i = 5, 15 do
  165. turtle.select(i)
  166. turtle.drop()
  167. end
  168. turnRight()
  169. turnRight()
  170. while position["currY"] > position["resumeY"] do
  171. moveDown()
  172. printPos()
  173. savePos()
  174. end
  175. os.reboot()
  176. end
  177. end
  178. end
  179.  
  180.  
  181.  
  182.  
  183.  
  184. function mineRowXPlus()
  185. --for i = 1, area do
  186. while position["currX"] < area do
  187. moveForward()
  188. position["currX"] = position["currX"] +1
  189. savePos()
  190. printPos()
  191. compareUp()
  192. compareDown()
  193. end
  194. turnRight()
  195. dropOff()
  196. moveForward()
  197. compareUp()
  198. compareDown()
  199. position["currZ"] = position["currZ"] +1
  200. savePos()
  201. turnRight()
  202. end
  203.  
  204. function mineRowXMinus()
  205. --for i = 1, area do
  206. while position["currX"] > 1 do
  207. moveForward()
  208. position["currX"] = position["currX"] -1
  209. savePos()
  210. printPos()
  211. compareUp()
  212. compareDown()
  213. end
  214. turnLeft()
  215. dropOff()
  216. moveForward()
  217. compareUp()
  218. compareDown()
  219. position["currZ"] = position["currZ"] +1
  220. savePos()
  221. turnLeft()
  222. end
  223.  
  224. function mineLayer()
  225. --for i = 1, area/2 do
  226. while position["currZ"] < area do
  227. mineRowXPlus()
  228. mineRowXMinus()
  229. end
  230. findPos()
  231. end
  232.  
  233.  
  234. function newLayer()
  235. for i = 1, 3 do
  236. moveUp()
  237. printPos()
  238. savePos()
  239. end
  240. position["currLayer"] = position["currLayer"] + 1
  241. savePos()
  242. sendBroadcast("Layer #"..position["currLayer"]..", Y:"..position["currY"])
  243. --sendBroadcast("current valuables: "..position["valuable"])
  244. end
  245.  
  246.  
  247.  
  248. function toBedrock()
  249. mineStart = position["yStart"] - 6
  250. for i = 1, mineStart do
  251. turtle.digDown()
  252. turtle.down()
  253. position["currY"] = position["currY"] -1
  254. printPos()
  255. savePos()
  256. end
  257. position["currLayer"] = 1
  258. savePos()
  259. position["currY"] = 6
  260. savePos()
  261. sendBroadcast("Bottom reached")
  262. printPos()
  263. end
  264.  
  265.  
  266.  
  267. function toSurface()
  268. findPos()
  269. sendBroadcast("Going up")
  270. --for i = 1, position["yStart"] - position["currY"] do
  271. while tonumber(position["currY"]) < tonumber(position["yStart"]) do
  272. moveUp()
  273. savePos()
  274. end
  275. --if tonumber(position["currY"]) > tonumber(position["yStart"]) then
  276. while tonumber(position["currY"]) > tonumber(position["yStart"]) do
  277. turtle.down()
  278. position["currY"] = position["currY"] -1
  279. printPos()
  280. savePos()
  281. end
  282. --end
  283. end
  284.  
  285.  
  286. function sendBroadcast(message)
  287. rednet.open("right")
  288. rednet.broadcast("["..turtleId.."] "..message)
  289. end
  290.  
  291.  
  292.  
  293.  
  294. function findPos()
  295. while position["direction"] ~= 3 do
  296. turnRight()
  297. savePos()
  298. end
  299. while position["currX"] > 1 do
  300. moveForward()
  301. position["currX"] = position["currX"] -1
  302. savePos()
  303. end
  304. while position["direction"] ~= 4 do
  305. turnRight()
  306. savePos()
  307. end
  308. while position["currZ"] > 1 do
  309. moveForward()
  310. position["currZ"] = position["currZ"] -1
  311. savePos()
  312. end
  313. while position["direction"] ~= 1 do
  314. turnRight()
  315. savePos()
  316. end
  317. end
  318.  
  319.  
  320.  
  321. function resume()
  322. sendBroadcast("Resuming, Y:"..position["currY"])
  323. turtle.select(16)
  324. turtle.dig()
  325. layersToGo = math.floor((position["yStart"] - position["currY"])/4)
  326. for i = 1, layersToGo do
  327. mineLayer()
  328. newLayer()
  329. if position["currY"] > 50 then
  330. break
  331. end
  332. end
  333. position["status"] = "off"
  334. toSurface()
  335. end
  336.  
  337.  
  338.  
  339.  
  340.  
  341. -- ## WRITE FILE ##
  342. function savePos()
  343. handle = fs.open("enderMine/savedPosition", "w")
  344. handle.write(textutils.serialize(position))
  345. handle.close()
  346. end
  347.  
  348. function loadPos()
  349. handle = fs.open("enderMine/savedPosition", "r")
  350. readFile = handle.readLine()
  351. if readFile ~= nil then
  352. position = textutils.unserialize(readFile)
  353. end
  354. handle.close()
  355. end
  356.  
  357.  
  358. function checkFuel()
  359. layers = math.floor(position["yStart"] / 4)
  360. mass = area * area * layers
  361. fuel = turtle.getFuelLevel()
  362. if fuel < mass then
  363. term.clear()
  364. term.setCursorPos(1,1)
  365. print("Not enought fuel")
  366. print("Fuel needed: "..mass)
  367. print("Current fuel: "..fuel)
  368. print("Put fuel in slot 15")
  369. sleep(1)
  370. print("")
  371. print("Press a key to continue")
  372. event = os.pullEvent("key")
  373. turtle.select(15)
  374. turtle.refuel()
  375. end
  376. end
  377.  
  378.  
  379. --------------- LOOP
  380.  
  381. if args[2] == "on" then
  382. --if position["status"] == "on" then
  383. loadPos()
  384. findPos()
  385. resume()
  386. position["status"] = nil
  387. position["status"] = "off"
  388. os.reboot()
  389. end
  390.  
  391. print("checking fuel")
  392.  
  393. checkFuel()
  394.  
  395. print("fuel checked")
  396.  
  397. savePos()
  398. position["status"] = "on"
  399. layers = math.floor(position["yStart"] / 4)
  400. position["currY"] = position["yStart"]
  401. toBedrock()
  402. for i = 1, layers do
  403. mineLayer()
  404. newLayer()
  405. if position["currY"] > 50 then
  406. break
  407. end
  408. end
  409. toSurface()
  410. position["status"] = "off"
  411. sendBroadcast("Done")
  412. os.reboot()
RAW Paste Data