ravneravn

Mine med antal miner

Jun 22nd, 2014
217
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. savePos()
  37. printPos()
  38. end
  39.  
  40. function moveUp()
  41. while not turtle.up() do
  42. turtle.digUp()
  43. end
  44. position["currY"] = position["currY"]+1
  45. savePos()
  46. printPos()
  47. end
  48.  
  49. function moveDown()
  50. while not turtle.down() do
  51. turtle.digDown()
  52. end
  53. position["currY"] = position["currY"]-1
  54. savePos()
  55. printPos()
  56. end
  57.  
  58. function turnRight()
  59. turtle.turnRight()
  60. position["direction"] = position["direction"] +1
  61. savePos()
  62. if position["direction"] > 4 then position["direction"] = 1
  63. savePos()
  64. end
  65. printPos()
  66. end
  67.  
  68. function turnLeft()
  69. turtle.turnLeft()
  70. position["direction"] = position["direction"] -1
  71. savePos()
  72. if position["direction"] < 1 then position["direction"] = 4
  73. savePos()
  74. end
  75. printPos()
  76. end
  77.  
  78.  
  79.  
  80. --#### COMPARE UP ####
  81. function compareUp()
  82. turtle.select(1)
  83. turtle.compareUp()
  84. if turtle.compareUp() == true then
  85. notinuse = 1
  86. else
  87.  
  88. turtle.select(2)
  89. turtle.compareUp()
  90. if turtle.compareUp() == true then
  91. notinuse = 1
  92. else
  93.  
  94. turtle.select(3)
  95. turtle.compareUp()
  96. if turtle.compareUp() == true then
  97. notinuse = 1
  98. else
  99.  
  100.  
  101. turtle.digUp()
  102. position["valuable"] = position["valuable"] +1
  103. end
  104. end
  105. end
  106. end
  107.  
  108.  
  109. --#### COMPARE DOWN ####
  110. function compareDown()
  111. turtle.select(1)
  112. turtle.compareDown()
  113. if turtle.compareDown() == true then
  114. notinuse = 1
  115.  
  116. else
  117.  
  118. turtle.select(2)
  119. turtle.compareDown()
  120. if turtle.compareDown() == true then
  121. notinuse = 1
  122.  
  123. else
  124.  
  125. turtle.select(3)
  126. turtle.compareDown()
  127. if turtle.compareDown() == true then
  128. notinuse = 1
  129.  
  130. else
  131.  
  132. turtle.digDown()
  133. position["valuable"] = position["valuable"] +1
  134. end
  135. end
  136. end
  137. end
  138.  
  139.  
  140. function layerResumeManual()
  141. loadPosManual()
  142. turnRight()
  143. while position["currZ"] < positionResumeManual["currZ"] do
  144. moveForward()
  145. position["currZ"] = position["currZ"] +1
  146. savePos()
  147. end
  148. turnLeft()
  149. while position["currX"] < positionResumeManual["currX"] do
  150. moveForward()
  151. position["currX"] = position["currX"] +1
  152. savePos()
  153. end
  154. while position["direction"] ~= positionResumeManual["direction"] do
  155. turnRight()
  156. savePos()
  157. end
  158. end
  159.  
  160.  
  161.  
  162. function dropOff()
  163. if position["enderchest"] == "yes" then
  164. stone = turtle.getItemCount(4)
  165. turtle.select(4)
  166. turtle.drop(stone-1)
  167. turtle.dig()
  168. turtle.select(16)
  169. turtle.place()
  170. for i = 5, 15 do
  171. turtle.select(i)
  172. turtle.drop()
  173. end
  174. turtle.select(16)
  175. turtle.dig()
  176.  
  177. elseif position["enderchest"] == "no" then
  178. stone = turtle.getItemCount(4)
  179. turtle.select(4)
  180. turtle.drop(stone-1)
  181. if turtle.getItemCount(16) > 0 then
  182. savePosManual()
  183. findPos()
  184. savePos()
  185. position["resumeY"] = tonumber(position["currY"])
  186. toSurface()
  187. turnRight()
  188. turnRight()
  189. for i = 5, 16 do
  190. turtle.select(i)
  191. turtle.drop()
  192. end
  193. turtle.select(5)
  194. turnRight()
  195. turnRight()
  196. while position["currY"] > position["resumeY"] do
  197. moveDown()
  198. printPos()
  199. savePos()
  200. end
  201. --os.reboot()
  202. layerResumeManual()
  203. end
  204. end
  205. end
  206.  
  207.  
  208.  
  209.  
  210.  
  211. function mineRowXPlus()
  212. --for i = 1, area do
  213. while position["currX"] < area do
  214. moveForward()
  215. position["currX"] = position["currX"] +1
  216. savePos()
  217. printPos()
  218. compareUp()
  219. compareDown()
  220. end
  221. turnRight()
  222. dropOff()
  223. moveForward()
  224. compareUp()
  225. compareDown()
  226. position["currZ"] = position["currZ"] +1
  227. savePos()
  228. turnRight()
  229. end
  230.  
  231. function mineRowXMinus()
  232. --for i = 1, area do
  233. while position["currX"] > 1 do
  234. moveForward()
  235. position["currX"] = position["currX"] -1
  236. savePos()
  237. printPos()
  238. compareUp()
  239. compareDown()
  240. end
  241. turnLeft()
  242. dropOff()
  243. moveForward()
  244. compareUp()
  245. compareDown()
  246. position["currZ"] = position["currZ"] +1
  247. savePos()
  248. turnLeft()
  249. end
  250.  
  251. function mineLayer()
  252. --for i = 1, area/2 do
  253. while position["currZ"] < area do
  254. mineRowXPlus()
  255. mineRowXMinus()
  256. end
  257. findPos()
  258. end
  259.  
  260.  
  261. function newLayer()
  262. for i = 1, 3 do
  263. moveUp()
  264. printPos()
  265. savePos()
  266. end
  267. position["currLayer"] = position["currLayer"] + 1
  268. savePos()
  269. sendBroadcast("Y Level:"..position["currY"])
  270. --sendBroadcast("current valuables: "..position["valuable"])
  271. end
  272.  
  273.  
  274.  
  275. function toBedrock()
  276. mineStart = position["yStart"] - 6
  277. for i = 1, mineStart do
  278. turtle.digDown()
  279. turtle.down()
  280. position["currY"] = position["currY"] -1
  281. printPos()
  282. savePos()
  283. end
  284. position["currLayer"] = 1
  285. savePos()
  286. position["currY"] = 6
  287. savePos()
  288. sendBroadcast("Bottom reached")
  289. printPos()
  290. end
  291.  
  292.  
  293.  
  294. function toSurface()
  295. findPos()
  296. --sendBroadcast("Going up")
  297. --for i = 1, position["yStart"] - position["currY"] do
  298. while tonumber(position["currY"]) < tonumber(position["yStart"]) do
  299. moveUp()
  300. savePos()
  301. end
  302. --if tonumber(position["currY"]) > tonumber(position["yStart"]) then
  303. while tonumber(position["currY"]) > tonumber(position["yStart"]) do
  304. turtle.down()
  305. position["currY"] = position["currY"] -1
  306. printPos()
  307. savePos()
  308. end
  309. --end
  310. end
  311.  
  312.  
  313. function sendBroadcast(message)
  314. if peripheral.isPresent("right") == true then
  315. rednet.open("right")
  316. rednet.broadcast("["..turtleId.."] "..message)
  317. end
  318. end
  319.  
  320.  
  321.  
  322.  
  323. function findPos()
  324. while position["direction"] ~= 3 do
  325. turnRight()
  326. savePos()
  327. end
  328. while position["currX"] > 1 do
  329. moveForward()
  330. position["currX"] = position["currX"] -1
  331. savePos()
  332. end
  333. while position["direction"] ~= 4 do
  334. turnRight()
  335. savePos()
  336. end
  337. while position["currZ"] > 1 do
  338. moveForward()
  339. position["currZ"] = position["currZ"] -1
  340. savePos()
  341. end
  342. while position["direction"] ~= 1 do
  343. turnRight()
  344. savePos()
  345. end
  346. end
  347.  
  348.  
  349.  
  350. function resume()
  351. --sendBroadcast("Resuming, Y:"..position["currY"])
  352. turtle.select(16)
  353. turtle.dig()
  354. layersToGo = math.floor((position["yStart"] - position["currY"])/4)
  355. for i = 1, layersToGo do
  356. mineLayer()
  357. newLayer()
  358. if position["currY"] > 50 then
  359. break
  360. end
  361. end
  362. position["status"] = "off"
  363. toSurface()
  364. end
  365.  
  366.  
  367.  
  368.  
  369.  
  370. -- ## WRITE FILE ##
  371. function savePos()
  372. handle = fs.open("enderMine/savedPosition", "w")
  373. handle.write(textutils.serialize(position))
  374. handle.close()
  375. end
  376.  
  377. function savePosManual()
  378. handle = fs.open("enderMine/savedPositionResume", "w")
  379. handle.write(textutils.serialize(position))
  380. handle.close()
  381. end
  382.  
  383. function loadPos()
  384. handle = fs.open("enderMine/savedPosition", "r")
  385. readFile = handle.readLine()
  386. if readFile ~= nil then
  387. position = textutils.unserialize(readFile)
  388. end
  389. handle.close()
  390. end
  391.  
  392. function loadPosManual()
  393. handle = fs.open("enderMine/savedPositionResume", "r")
  394. readFile = handle.readLine()
  395. if readFile ~= nil then
  396. positionResumeManual = textutils.unserialize(readFile)
  397. end
  398. handle.close()
  399. end
  400.  
  401.  
  402. function checkFuel()
  403. layers = math.floor(position["yStart"] / 4)
  404. mass = area * area * layers
  405. fuel = turtle.getFuelLevel()
  406. if fuel < mass then
  407. term.clear()
  408. term.setCursorPos(1,1)
  409. sendBroadcast("No fuel")
  410. print("Not enought fuel")
  411. print("Fuel needed: "..mass)
  412. print("Current fuel: "..fuel)
  413. print("Put fuel in slot 15")
  414. sleep(1)
  415. print("")
  416. print("Press a key to continue")
  417. event = os.pullEvent("key")
  418. turtle.select(15)
  419. turtle.refuel()
  420. end
  421. end
  422.  
  423. function setUp()
  424. turtle.select(15)
  425. turtle.drop(64)
  426. turtle.digUp()
  427. for i = 1, 16 do
  428. moveForward()
  429. end
  430. turtle.dipUp()
  431. turtle.placeUp()
  432. end
  433.  
  434.  
  435. --------------- LOOP
  436.  
  437. if args[4] == "on" then
  438. --if position["status"] == "on" then
  439. loadPos()
  440. findPos()
  441. resume()
  442. position["status"] = nil
  443. position["status"] = "off"
  444. os.reboot()
  445. end
  446.  
  447. for i = 1, args[2] do
  448. print("checking fuel")
  449.  
  450. checkFuel()
  451.  
  452. print("fuel checked")
  453.  
  454. savePos()
  455. position["status"] = "on"
  456. layers = math.floor(position["yStart"] / 4)
  457. position["currY"] = position["yStart"]
  458. toBedrock()
  459. for i = 1, layers do
  460. mineLayer()
  461. newLayer()
  462. if position["currY"] > 50 then
  463. break
  464. end
  465. end
  466. toSurface()
  467. position["status"] = "off"
  468. setUp()
  469. end
  470. sendBroadcast("Done")
  471. os.reboot()
RAW Paste Data