shiryavsky

minig test

Nov 18th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.35 KB | None | 0 0
  1. --test miner
  2.  
  3. currentFacing = 0
  4. currentlySelectedSlot = 1
  5. fuelLevelToRefuelAt = 5
  6. dirtBlockCount = 0
  7.  
  8. tunnelLength = 64
  9. torchesEvery = 10
  10.  
  11. function main()
  12. getDirtBlock() -- init dirt blocks
  13. while getDirtBlock()==0 do
  14. awaitFix("No dirst blocks in 1sts slots")
  15. end
  16. local tunPos = 0
  17. local lastTorch = 0
  18. local fromSlot = dirtBlockCount + 1
  19. while tunPos < tunnelLength do
  20. print("tunPos: "..tunPos)
  21. --first fwd
  22. go('forward', true)
  23. --turn left and look
  24. turnLeft()
  25. if not checkEmpty('fwd') then
  26. if not checkDirt('fwd') then
  27. dig("fwd")
  28. putDirtBlock("fwd")
  29. end
  30. end
  31. go('up', true)
  32. if not checkEmpty('fwd') then
  33. if not checkDirt('fwd') then
  34. dig("fwd")
  35. putDirtBlock("fwd")
  36. end
  37. end
  38. go('up', true)
  39. if not checkEmpty('fwd') then
  40. if not checkDirt('fwd') then
  41. dig("fwd")
  42. putDirtBlock("fwd")
  43. end
  44. end
  45. if lastTorch >=torchesEvery then --torches
  46. turnLeft() --turn back
  47. placeTorch() --back
  48. turnRight()
  49. lastTorch = 0
  50. end
  51. if not checkEmpty('up') then --look up
  52. if not checkDirt('up') then
  53. while not checkEmpty('up') do
  54. dig("up")
  55. end
  56. putDirtBlock("up")
  57. end
  58. end
  59. turnRight()
  60. turnRight()
  61. if not checkEmpty('fwd') then
  62. if not checkDirt('fwd') then
  63. dig("fwd")
  64. putDirtBlock("fwd")
  65. end
  66. end
  67. go('down', true)
  68. if not checkEmpty('fwd') then
  69. if not checkDirt('fwd') then
  70. dig("fwd")
  71. putDirtBlock("fwd")
  72. end
  73. end
  74. go('down', true)
  75. if not checkEmpty('fwd') then
  76. if not checkDirt('fwd') then
  77. dig("fwd")
  78. putDirtBlock("fwd")
  79. end
  80. end
  81. turnLeft()
  82. if not checkEmpty('down') then
  83. if not checkDirt('down') then
  84. print("oh! dig down")
  85. dig("down")
  86. putDirtBlock("down")
  87. end
  88. else
  89. print("making bridge")
  90. putDirtBlock("down") -- for fucking peoples
  91. end
  92. --and repeat process
  93. tunPos = tunPos + 1
  94. lastTorch = lastTorch + 1
  95. end
  96. print("Ohh.. fin! I go back")
  97. turnLeft()
  98. turnLeft()
  99. while tunPos > 1 do
  100. go('forward', true)
  101. tunPos = tunPos - 1
  102. end
  103. putDirtBlock("up") --close tunnel
  104. go('forward', true)
  105. tunPos = tunPos - 1
  106. if checkEmpty("fwd")==false then --oh! chest!
  107. print("Unloading..")
  108. for i=fromSlot,14,1 do
  109. selectSlot(i)
  110. turtle.drop()
  111. end
  112. end
  113. end
  114.  
  115. function placeTorch()
  116. if turtle.getItemCount(15) <=1 then
  117. print("I have no torches...")
  118. return false
  119. else
  120. selectSlot(15)
  121. if not turtle.place() then
  122. print("Can't place torch!")
  123. end
  124. end
  125. end
  126.  
  127. function putDirtBlock(side)
  128. ensureFuel()
  129. local toSelect = 1
  130. toSelect = getDirtBlock()
  131. if toSelect==false then
  132. return false
  133. end
  134. selectSlot(toSelect)
  135. if side=="up" then
  136. return turtle.placeUp()
  137. elseif side=="down" then
  138. return turtle.placeDown()
  139. else
  140. return turtle.place()
  141. end
  142. return false
  143. end
  144.  
  145. function getDirtBlock()
  146. if dirtBlockCount==0 then
  147. selectSlot(1)
  148. while turtle.getItemCount(currentlySelectedSlot)~=0 do
  149. dirtBlockCount = dirtBlockCount + 1
  150. selectSlot(currentlySelectedSlot + 1)
  151. end
  152. end
  153. if dirtBlockCount==0 then
  154. return false
  155. end
  156. local fromSlot = dirtBlockCount + 1
  157. for i=fromSlot,16,1 do
  158. if turtle.getItemCount(i)>1 then
  159. selectSlot(i)
  160. for j=1,dirtBlockCount,1 do
  161. if turtle.compareTo(j) then
  162. return i
  163. end
  164. end
  165. end
  166. end
  167. return false
  168. end
  169.  
  170. function checkDirt(side)
  171. local result = false
  172. if dirtBlockCount==0 then
  173. getDirtBlock()
  174. end
  175. for j=1,dirtBlockCount,1 do
  176. selectSlot(j)
  177. if side=="up" then
  178. result = turtle.compareUp()
  179. elseif side=="down" then
  180. result = turtle.compareDown()
  181. else
  182. result = turtle.compare()
  183. end
  184. if result then
  185. return result
  186. end
  187. end
  188. return result
  189. end
  190.  
  191. function checkEmpty(side)
  192. local result = false
  193. if side=="up" then
  194. result = not turtle.detectUp()
  195. elseif side=="down" then
  196. result = not turtle.detectDown()
  197. else
  198. result = not turtle.detect()
  199. end
  200. return result
  201. end
  202.  
  203. function selectSlot(x)
  204. if x<1 then
  205. x = 1
  206. elseif x > 16 then
  207. x = 16
  208. end
  209. turtle.select(x)
  210. currentlySelectedSlot = x
  211. end
  212.  
  213.  
  214. function ensureFuel()
  215. -- Determine whether a refuel is required
  216. local fuelLevel = turtle.getFuelLevel()
  217. if (fuelLevel ~= "unlimited") then
  218. if (fuelLevel < fuelLevelToRefuelAt) then
  219. -- Need to refuel
  220. selectSlot(16)
  221. local fuelItems = turtle.getItemCount(16)
  222. -- Do we need to impact the emergency fuel to continue? (always
  223. -- keep one fuel item in slot 16)
  224. if (fuelItems == 0) then
  225. awaitFix("I want more fuel!")
  226. elseif (fuelItems == 1) then
  227. --load fuel from another slot
  228. local fueled = false
  229. local fromSlot = dirtBlockCount + 1
  230. for i=fromSlot,15,1 do
  231. selectSlot(i)
  232. if (turtle.compareTo(16)) then
  233. print("refueling from slot "+i)
  234. turtle.refuel(1)
  235. turtle.transferTo(16)
  236. fueled = true
  237. end
  238. end
  239. if not fueled then
  240. print("completely out of fuel!")
  241. end
  242. else
  243. print("refueling some")
  244. turtle.refuel(1)
  245. end
  246. end
  247. end
  248. end
  249.  
  250. function turnFacing(facing)
  251. while facing ~= currentFacing do
  252. turnRight()
  253. end
  254. end
  255.  
  256. function turnLeft()
  257. ensureFuel()
  258. turtle.turnLeft()
  259. currentFacing = currentFacing - 1
  260. if currentFacing < 0 then
  261. currentFacing = 3
  262. end
  263. end
  264.  
  265. function turnRight()
  266. ensureFuel()
  267. turtle.turnRight()
  268. currentFacing = currentFacing + 1
  269. if currentFacing >= 4 then
  270. currentFacing = 0
  271. end
  272. end
  273.  
  274. function dig(direction)
  275. local result = false
  276. local tries = 0
  277. selectSlot(dirtBlockCount + 1)
  278. while (not result) and (tries < 10) do
  279. ensureFuel()
  280. if direction=="up" then
  281. result = turtle.digUp()
  282. elseif direction=="down" then
  283. result = turtle.digDown()
  284. else
  285. result = turtle.dig()
  286. end
  287. tries = tries + 1
  288. end
  289. if not result then
  290. print ("can't dig in "..direction)
  291. end
  292. return result
  293. end
  294.  
  295. function go(direction, allowDig)
  296. if direction=="right" then
  297. turnRight()
  298. direction="forward"
  299. end
  300. if direction=="left" then
  301. turnLeft()
  302. direction="forward"
  303. end
  304. if direction=="forward" then
  305. while turtle.detect() do
  306. if allowDig then
  307. ensureFuel()
  308. turtle.dig()
  309. else
  310. awaitFix("can't forward")
  311. end
  312. end
  313.  
  314. while not turtle.forward() do
  315. ensureFuel()
  316. turtle.dig()
  317. end
  318. end
  319. if direction=="up" then
  320. while turtle.detectUp() do
  321. if allowDig then
  322. ensureFuel()
  323. turtle.digUp()
  324. else
  325. awaitFix("can't up")
  326. end
  327. end
  328. while not turtle.up() do
  329. ensureFuel()
  330. turtle.digUp()
  331. end
  332. end
  333. if direction=="down" then
  334. while turtle.detectDown() do
  335. if allowDig then
  336. ensureFuel()
  337. turtle.digDown()
  338. else
  339. awaitFix("can't down")
  340. end
  341. end
  342. while not turtle.down() do
  343. ensureFuel()
  344. turtle.digDown()
  345. end
  346. end
  347. end
  348.  
  349. function awaitFix(message)
  350. print("I have problem: " .. message)
  351. print("Please fix it and press Enter to continue")
  352. io.read()
  353. end
  354.  
  355. main()
Advertisement
Add Comment
Please, Sign In to add comment