Advertisement
nickmcski

Untitled

Jul 26th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.25 KB | None | 0 0
  1. ----------------------------------------------------------
  2. -- Stripmining turtle --
  3. ----------------------------------------------------------
  4. -- Version 1.3.2 --
  5. ----------------------------------------------------------
  6. -- Makes the turtle mine straight down to the bedrock. --
  7. -- Usege: dig <max Y> <length> --
  8. -- <max Y> - maximum height for mining --
  9. -- <length> - number of blocks to mine forward --
  10. -- (return) - go back to the starting point. --
  11. ----------------------------------------------------------
  12. -- Based on a script pastebin.com/JbFMHaNg by Eonsv --
  13. -- Tested with CC 1.5 --
  14. -- Don't forget to use chunkloaders! --
  15. ----------------------------------------------------------
  16.  
  17. --Tests wether a file with the filename name exists.
  18. --Returns true if it does and false if not
  19. function fileExists(name)
  20. local f = io.open(name,'r')
  21. if (f ~= nil) then
  22. f:close()
  23. return true
  24. else
  25. return false
  26. end
  27. end
  28.  
  29. local arg = {...}
  30.  
  31. --Saves current position to file
  32. function save()
  33. local str = ''
  34. str = str..y..'\n'
  35. str = str..x..'\n'
  36. str = str..distance..'\n'
  37. local file = io.open('save', 'w')
  38. file:write(str)
  39. file:close()
  40. end
  41.  
  42. --Consumes a stack of fuel from the 2nd enderchest
  43. function refuel(fuel)
  44. if(9999 >= fuel) then
  45. return true
  46. end
  47. print('Refueling')
  48. turtle.select(2)
  49. while(not turtle.placeUp()) do
  50. turtle.digUp()
  51. turtle.attackUp()
  52. end
  53. turtle.select(16)
  54. while(not turtle.suckUp()) do
  55. sleep('No fuel found. Sleeping.', 10)
  56. end
  57. turtle.refuel(64)
  58. turtle.select(2)
  59. turtle.digUp()
  60. if(9999 <= fuel) then
  61. sleep('Refueling failed!', 10)
  62. dump()
  63. refuel(200)
  64. end
  65. end
  66.  
  67. --Dumps all items into the 1st enderchest
  68. function dump()
  69. turtle.select(1)
  70. while(not turtle.placeUp()) do
  71. turtle.digUp()
  72. turtle.attackUp()
  73. end
  74. for i = 3, 16 do
  75. if(turtle.getItemCount(i) > 0) then
  76. turtle.select(i)
  77. while(not turtle.dropUp()) do
  78. sleep('Dropoff chest is full. Sleeping.', 10)
  79. end
  80. end
  81. end
  82. turtle.select(1)
  83. turtle.digUp()
  84. print('Dropoff successful')
  85. end
  86.  
  87. function dropoff()
  88. local empty = 0
  89. --Calculates number of empty slots
  90. for i = 2, 16 do
  91. if(turtle.getItemCount(i) == 0) then
  92. empty = empty + 1
  93. end
  94. end
  95. --Dumps items if the inventory is full
  96. if(empty == 0) then
  97. dump()
  98. end
  99. end
  100.  
  101. --Clears the screen
  102. function clear()
  103. term.clear()
  104. term.setCursorPos(1, 1)
  105. end
  106.  
  107. --Sleeps for %time% seconds, while displaying %text% and counting down
  108. function sleep(text, time)
  109. for i = 0, time - 1 do
  110. clear()
  111. print(text)
  112. print('Countdown: '..time - i..'s.')
  113. os.sleep(1)
  114. end
  115. end
  116.  
  117. --Returns true if successful, and false if the block is bedrock
  118. local function bedrockTest()
  119. local test
  120. test = turtle.down()
  121. return test
  122. end
  123.  
  124. --Moves up until reaching initial height
  125. function moveUp()
  126. for i = 1, y do
  127. refuel(200)
  128. while(not turtle.up()) do
  129. turtle.attackUp()
  130. turtle.digUp()
  131. end
  132. end
  133. end
  134.  
  135. --Digging down until bedrock
  136. function digDown()
  137. local fail = 0
  138. while(true) do
  139. dropoff()
  140. refuel(500)
  141. turtle.digDown()
  142. if(not bedrockTest()) then
  143. while(turtle.detectDown() == false) do
  144. turtle.attackDown()
  145. turtle.down()
  146. end
  147. os.sleep(0.2)
  148. fail = fail + 1
  149. if(fail == 6) then
  150. break
  151. end
  152. end
  153. end
  154. end
  155.  
  156. --Mining loop
  157. function loop()
  158. while(x > 0) do
  159. clear()
  160. print('Blocks left to mine: '..x)
  161. digDown()
  162. print('Hit bedrock, moving up '..y..' blocks')
  163. moveUp()
  164. while(not turtle.forward()) do
  165. turtle.attack()
  166. turtle.dig()
  167. end
  168. dump()
  169. x = x - 1
  170. save()
  171. end
  172. end
  173.  
  174. --Init sequence
  175. function init()
  176. clear()
  177. y = tonumber(arg[1])
  178. y = y - 1
  179. x = tonumber(arg[2])
  180. if(arg[3] ~= nil) then
  181. distance = x
  182. print('Return mode on, distance: '..distance..' blocks')
  183. else distance = 0
  184. end
  185. while(turtle.getItemCount(1) ~= 1) do
  186. sleep('Wrong number of items in slot 1. Place one dropoff ender chest.', 5)
  187. end
  188. while(turtle.getItemCount(2) ~= 1) do
  189. sleep('Wrong number of items in slot 2. Place one fuel ender chest.', 5)
  190. end
  191. save()
  192. -- Creating startup file to continue mining after server restarts
  193. end
  194.  
  195.  
  196. function getMessage()
  197. senderId, message, distance = rednet.receive()
  198. if senderId == 78 then
  199. --term.write(message)
  200. getCommand(message)
  201. else
  202. term.write("Not from computer 78")
  203. end
  204. end
  205.  
  206. function getCommand(message)
  207. if message == "Mine" then
  208. term.write("Getting ready to mine")
  209. sleep("Hi", 5)
  210. digDown()
  211. print('Hit bedrock, moving up '..y..' blocks')
  212. moveUp()
  213. while(not turtle.forward()) do
  214. turtle.attack()
  215. turtle.dig()
  216. end
  217. dump()
  218. end
  219. end
  220.  
  221.  
  222. --------
  223. --MAIN--
  224. --------
  225. --if(not fileExists('startup')) then
  226. -- for i = 1, 2 do
  227. -- if(arg[i] == nil) then
  228. -- clear()
  229. -- print('Usage: dig <max Y> <length> return')
  230. -- print()
  231. -- print('<max Y> - maximum height for mining (e.g. your Y coordinate).')
  232. -- print('<length> - number of blocks to mine forward.')
  233. -- print('return - optional command to make the turtle go back to the starting point.')
  234. -- do return end
  235. -- end
  236. -- end
  237. -- init()
  238. --else
  239. -- clear()
  240. -- --Reading save file
  241. -- local file = io.open('save', 'r')
  242. -- y = tonumber(file:read('*l'))
  243. -- x = tonumber(file:read('*l'))
  244. -- distance = tonumber(file:read('*l'))
  245. -- file:close()
  246. -- --If rebooted while dumping items
  247. -- if(turtle.getItemCount(1) == 0) then
  248. -- turtle.select(1)
  249. -- turtle.digUp()
  250. -- dump()
  251. -- while(turtle.getItemCount(1) == 0) do
  252. -- sleep('Missing chest in slot 1.', 10)
  253. -- end
  254. -- end
  255. -- loop()
  256. --end
  257.  
  258.  
  259.  
  260.  
  261. --main--
  262. y = 51
  263. rednet.open("right")
  264. if rednet.isOpen("right") then
  265. print("RednetOpen")
  266. end
  267.  
  268. while true do
  269. getMessage()
  270.  
  271. end
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279. --Finishing
  280. print('Done!')
  281.  
  282. --Going to the starting point
  283. if distance > 0 then
  284. turtle.turnRight()
  285. turtle.turnRight()
  286. while(distance > 0) do
  287. clear()
  288. print('Going back '..distance..' blocks')
  289. while(not turtle.forward()) do
  290. turtle.attack()
  291. turtle.dig()
  292. end
  293. distance = distance - 1
  294. end
  295. print('Done!')
  296. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement