Advertisement
Guest User

Quarry version 2.3.3

a guest
Jan 13th, 2013
2,284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.86 KB | None | 0 0
  1. --[[TO DO:
  2. Awesome code that will automatically dig 3 for every layer until last, when specs matter
  3. Make auto-empty smarter: e.g. it will empty every few layers instead of every layer.
  4.  
  5. Version 2.3.3
  6. Recent changes: added an argument for which side the chest is on.
  7.     REDNET SUPPORT: Add the argument "true" after chest to send to
  8.     rednet!!!!!!!!!!!
  9. Directions: If you want, place a chest right behind where you place the turtle.
  10. It will place things here when its done
  11.  
  12. Also, if auto-empty is on, it will always eject the things it has
  13. into the place where a chest is supposed to be,
  14. it just dosen't check for a chest
  15. ]]
  16.  
  17.  
  18. --Check Fuel Disabling for those on softcore
  19. doCheckFuel = true
  20. --For those that find this annoying
  21. forceAutoEmpty = true
  22. --If set to false, will ignore all safety restraints
  23. careAboutResources = true
  24.  
  25. doDebug = false
  26.  
  27. --This is how many (theoretical) slots it will fill with cobble. e.g. 13 allows 13 slots for cobble (or other stack) and 2 slots for other things.
  28. --Maximum here is 16
  29. maxslots = 13
  30. --If your job is bigger than it can carry (as defined by max slots) it will default to auto-empty
  31. --Note: For counting to work properly, cobblestone must be the first block it mines, or put a cobble in slot 1
  32.  
  33. --Defining things
  34. tArgs = {...}
  35. if tArgs[1] == nil then
  36. dropSide = "front"
  37. else
  38. dropSide = tArgs[1]
  39. end
  40. if tArgs[2] == "true" then
  41. rednetEnabled = true
  42. else
  43. rednetEnabled = false
  44. end
  45.  
  46. if careAboutResources == false then
  47. forceAutoEmpty = false
  48. end
  49.  
  50. function biometrics()
  51. if pass == true then
  52. rednet.open("right")
  53. local toSend = {length = x, height = y, width = z,
  54.     percent = percent, blocksMined = mined,
  55.     fuel = checkFuel(), blocksMoved = moved,
  56.     blocksLeft = (volume-moved), ID = os.getComputerID(),
  57.     finishedLayers = doneHeight, finishedWidth = doneWidth,
  58.     volume = volume}
  59. rednet.send(host, textutils.serialize(toSend))
  60. hostCheck, confirm, distX = 0,0,0
  61. hostCheck, confirm, distX = rednet.receive(1)
  62. if hostCheck == host and confirm == "Quarry Confirm" then
  63. displayRedCheck = true --This section displays to the screen that
  64. else                   --the host computer is still receiving.
  65. displayRedCheck = false
  66. end
  67. rednet.close("right")
  68. end
  69. end
  70.  
  71. function screen(xPos,yPos)
  72. term.clear()
  73. term.setCursorPos(xPos,yPos)
  74. end
  75. function mine() -- Basic Move Forward
  76. while turtle.forward() == false do
  77. if turtle.dig() == false then
  78. turtle.attack()
  79. mined = mined - 1
  80. end
  81. mined = mined + 1
  82. end
  83. if Cdouble == 1 then
  84. if turtle.digDown() == true then
  85. mined = mined + 1
  86. end
  87. end
  88. if Cdouble == 0 then
  89. moved = moved + 1
  90. else
  91. moved = moved + 2
  92. end
  93. display()
  94. end
  95. function mineRow() -- Move a row then turn
  96. if cRows ~= 0 then -- cRows is "Rows Completed"
  97. for length=1, x do --It does this check because on the first row of a layer,
  98. mine()             --   it already occupies the first position
  99. end
  100. else
  101. for length=1, x-1 do
  102. mine()
  103. end
  104. end
  105. cRows = cRows + 1
  106. biometrics()
  107. end
  108. function display() --Runs in Mine(), display information to the screen in a certain place
  109. percent = math.ceil((moved/volume)*100)
  110. term.setCursorPos(1,2)
  111. term.clearLine()
  112. print(mined)
  113. term.setCursorPos(1,4)
  114. term.clearLine()
  115. print(percent.."%")
  116. if rednetEnabled then
  117. if displayRedCheck then
  118. display1 = "Good"
  119. else
  120. display1 = "Lost"
  121. end
  122. term.setCursorPos(1,6)
  123. print("Connection: "..display1)
  124. end
  125. end
  126.  
  127. totals = {cobble = 0, sand = 0, other = 0} -- Total for display (cannot go inside function)
  128. function count() --Done after every row of auto-empty and at end
  129. slot = {}        --Detection of blocks code is 1: Cobble 2: Other Raw 3:Other
  130. for i=1, 16 do   --[1] is type, [2] is number
  131. slot[i] = {}
  132. slot[i][2] = turtle.getItemCount(i)
  133. end
  134. doCheckSand = false
  135. if area > 100 then
  136. doCheckSand = true
  137. end
  138.  
  139. slot[1][1] = 1   -- = Assumes Cobble/Main
  140. for i=2, 16 do   --Cobble Check
  141. turtle.select(i)
  142. if turtle.compareTo(1) == true then
  143. slot[i][1] = 1
  144. end
  145. end
  146. for i=2, 16 do   --Sand/Raw Check
  147. tempItemCount = turtle.getItemCount(i)
  148. if slot[i][1] ~= 1 then
  149. if tempItemCount > 32 and doCheckSand then
  150. slot[i][1] = 2
  151. elseif tempItemCount > 0 and slot[i][1] then
  152. slot[i][1] = 3
  153. end
  154. end
  155. end
  156. for i=1, 16 do
  157. if slot[i][1] == 1 then
  158. totals.cobble = slot[i][2] + totals.cobble
  159. elseif slot[i][1] == 2 then
  160. totals.sand = slot[i][2] + totals.sand
  161. else
  162. totals.other = slot[i][2] + totals.other
  163. end
  164. end
  165.  
  166. if doDebug == true then
  167. for i=1, 16 do
  168. print(slot[i][2])
  169. end
  170. end
  171. end
  172.  
  173. function endDrop(final)
  174. if final ~= false then
  175. final = true
  176. end
  177. if dropSide == "left" then --Turning
  178. turtle.turnRight()
  179. elseif dropSide == "right" then
  180. turtle.turnLeft()
  181. end
  182. turtle.select(1)
  183. if dropSide == "top" then -- Drops to top
  184. if final == false then
  185. avar = turtle.getItemCount(1) --Dosen't drop 1 cobblestone for future counting purposes if autofill
  186. turtle.dropUp(avar-1)
  187. for i=2, 16 do
  188. turtle.select(i)
  189. turtle.dropUp()
  190. end
  191. elseif turtle.detectUp() then
  192. for i=1, 16 do
  193. turtle.select(i)
  194. turtle.dropUp()
  195. end
  196. end
  197. elseif dropSide == "bottom" then --Drops to bottom
  198. if final == false then
  199. avar = turtle.getItemCount(1)
  200. turtle.dropDown(avar-1)
  201. for i=2, 16 do
  202. turtle.select(i)
  203. turtle.dropDown()
  204. end
  205. elseif turtle.detectDown() then
  206. for i=1, 16 do
  207. turtle.select(i)
  208. turtle.dropDown()
  209. end
  210. end
  211. else --Drops to front
  212. if final == false then
  213. avar = turtle.getItemCount(1)
  214. turtle.drop(avar-1)
  215. for i=2, 16 do
  216. turtle.select(i)
  217. turtle.drop()
  218. end
  219. elseif turtle.detect() then
  220. for i=1, 16 do
  221. turtle.select(i)
  222. turtle.drop()
  223. end
  224. end
  225.  
  226. end
  227. turtle.select(1)
  228.  
  229. if dropSide == "right" then --Turning, inverted
  230. turtle.turnRight()
  231. elseif dropSide == "left" then
  232. turtle.turnLeft()
  233. end
  234.  
  235. end
  236. function checkFuel()
  237. fuelLevel = turtle.getFuelLevel()
  238. return fuelLevel
  239. end
  240.  
  241.  
  242.  -----------------------------------------------------------------
  243. --Input Phase
  244.  
  245. screen(1,1)
  246. print("----- Welcome to Quarry! -----")
  247. print("")
  248.  
  249. --Dimensions of Hole
  250. print("What dimensions?")
  251. print("")
  252. term.write("Length: ")
  253. x = io.read()
  254.  
  255. term.write("Width: ")
  256. z = io.read()
  257.  
  258. term.write("Height: ")
  259. y = io.read()
  260.  
  261. if not tonumber(x) or x == 0 then
  262. x = 3
  263. end
  264. if not tonumber(y) or y == 0 then
  265. y = 2
  266. end
  267. if not tonumber(z) or z == 0 then
  268. z = 3
  269. end
  270.  
  271. x = math.abs(tonumber(x))
  272. y = math.abs(tonumber(y))
  273. z = math.abs(tonumber(z))
  274. if doDebug == true then
  275. print(x," ",z," ",y)
  276. end
  277.  
  278. Cdouble = 0
  279. --Check for if it can do doubles
  280. if math.ceil(y/2) == y/2 then
  281. Cdouble = 1
  282. end
  283.  
  284.  
  285.  
  286. volume = x*y*z
  287. area = x*z
  288.  
  289. if maxslots > 16 then
  290. maxslots = 16
  291. end
  292. maxsize = maxslots*64
  293. if Cdouble == 1 then
  294. maxsize = maxsize/2
  295. end
  296.  
  297. empty = 0
  298.  
  299.  
  300. --Checks if bigger than inventory: 832 blocks theoretical Assuming at least 2 are non-cobble
  301.  
  302. if forceAutoEmpty == true then
  303. if volume > maxsize then
  304. print("Job very big")
  305. print("Auto-emptying on")
  306. empty = 1
  307. sleep(2)
  308. end
  309. end
  310.  
  311. if empty ~= 1 and careAboutResources == true then
  312. print("Turn on auto-emptying? (Y/N)")
  313. ans = string.sub(string.lower(io.read()),1,1)
  314. if ans == "y" or ans == "1" then
  315. empty = 1
  316. elseif ans == "s" then
  317. print("Stop? (type \"yes\")")
  318. if io.read() == "yes" then
  319. os.shutdown()
  320. end
  321. end
  322. end
  323.  
  324. if area > maxsize and careAboutResources then
  325. print("Area too Large, please restart program")
  326. sleep(5)
  327. os.reboot()
  328. end
  329.  
  330. --Getting Fuel
  331. if doCheckFuel == true then --Calculating Needed Fuel
  332. subHeight = 0
  333. if check == 1 then
  334. for i=0, y, 2 do --Usage is y = -2i+y, but I don't know how to do that
  335. subHeight = (2*y-i) + subHeight
  336. end
  337. else
  338. subHeight = y*2
  339. end
  340. if Cdouble == 1 then
  341. neededFuel = math.ceil((volume + x*y*z)/2 + subHeight)
  342. else
  343. neededFuel = math.ceil(volume + x*y*z + subHeight)
  344. end
  345. if checkFuel() < neededFuel then
  346. term.clear()
  347. term.setCursorPos(1,1)
  348.  
  349. print("More Fuel Needed")
  350. print("Current Fuel: ",checkFuel()," Needed: ",neededFuel)
  351. print("Place in Bottom Right, press any key")
  352. os.pullEvent("char")
  353. print("I'll take a bit more, so we don't have to do this as often")
  354. turtle.select(16)
  355. while checkFuel() < neededFuel+100 do
  356. if turtle.refuel(1) == false then
  357. term.clearLine()
  358. print("Still too little fuel")
  359. term.clearLine()
  360. print("Press a key to resume fueling")
  361. os.pullEvent("char")
  362. end
  363. xx,xy = term.getCursorPos()
  364. print(fuelLevel.." Fuel")
  365. term.setCursorPos(xx,xy)
  366. end
  367. print(fuelLevel.." Units of Fuel")
  368. sleep(3)
  369. turtle.select(1)
  370. end
  371.  
  372. end
  373.  
  374. --Initial Rednet Handshake
  375. if rednetEnabled then
  376. screen(1,1)
  377. print("----- Welcome to Quarry! -----")
  378. print("")
  379. rednet.open("right")
  380. pass = false
  381. print("Host Computer ID: ")
  382. host = tonumber(io.read())
  383. initRedCheck = 0
  384. repeat
  385. initRedCheck = initRedCheck + 1
  386. print("Sending: "..initRedCheck)
  387. rednet.send(host, "Initial Confirm")
  388. hostCheck, confirmCheck, distX = rednet.receive(3)
  389. if hostCheck == host and confirmCheck == "Host Confirm" then
  390. pass = true
  391. print("Established Connection")
  392. print("with Computer "..hostCheck.."!")
  393. sleep(1)
  394. displayRedCheck = true
  395. end
  396. if initRedCheck == 10 then
  397. error("Connection Failed to establish, try again...",0)
  398. end
  399. until pass == true
  400. rednet.close("right")
  401. end
  402.  
  403. --Check Double changing the Depth y (times it goes down)
  404. if Cdouble == 1 then
  405. Dy = y/2
  406. else
  407. Dy = y
  408. end
  409.  
  410. --Height Calculation
  411. if Cdouble == 0 then
  412. b = 1
  413. else
  414. b = 2
  415. end
  416.  
  417.  
  418. --Mining Phase
  419.  
  420. turn = 1 --Turn right is 0, turn left is 1 opposite to start
  421. mined = 0 -- Total Blocks Mined
  422. moved = 0 -- Total Spaces Moved
  423.  
  424. turtle.select(1)
  425. screen(1,1)
  426. print("Blocks Mined")
  427. print("")
  428. print("Percent Complete")
  429. mine()
  430.  
  431. doneHeight = 0
  432. doneWidth = 0 --Initialize for first biometrics
  433. biometrics()
  434. for depth=1, Dy do
  435. doneHeight = doneHeight+b
  436. doneWidth = 0
  437. for width = 1, z do
  438. doneWidth = doneWidth + 1
  439. cRows = 0 -- Rows completed (z)
  440. mineRow()
  441. if turn == 1 then
  442. turn = 0
  443. else
  444. turn = 1
  445. end
  446. if width ~= z then --Check if last row of set, will not turn if last
  447. if turn == 0 then
  448. turtle.turnRight()
  449. mine()
  450. turtle.turnRight()
  451. else
  452. turtle.turnLeft()
  453. mine()
  454. turtle.turnLeft()
  455. end
  456. end
  457. end
  458. if turn == 0 then --This If statement gets it back to the start of this layer
  459. turtle.turnRight()
  460. if Cdouble == 1 then
  461. if turtle.digDown() == true then
  462. mined = mined + 1
  463. end
  464. end
  465. turtle.turnRight()
  466. for i=1, x-1 do
  467. while not turtle.forward() do
  468. end
  469. end
  470. turtle.turnRight()
  471. for i=1, z-1 do
  472. while not turtle.forward() do
  473. end
  474. end
  475. else
  476. turtle.turnRight()
  477. for i=1, z-1 do
  478. while not turtle.forward() do
  479. end
  480. end
  481. end
  482. if empty == 1 and depth ~= Dy then --AutoEmptying at the end of every layer
  483. if Cdouble == 1 then
  484. AEup = depth*2
  485. else
  486. AEup = depth
  487. end
  488. for i=1, AEup-b do
  489. while not turtle.up() do
  490. end
  491. end
  492. turtle.turnLeft()
  493. while not turtle.forward() do
  494. end
  495. count()   --Counting
  496. endDrop(false) -- Drop into specified chest
  497.  
  498. turtle.turnLeft()
  499. turtle.turnLeft()
  500. while not turtle.forward() do
  501. end
  502. for i=1, AEup-b do
  503. turtle.down()
  504. end
  505. turtle.turnLeft()
  506. end
  507. if depth ~= Dy then --This checks if it is the last layer of the run
  508. for a=1, b do
  509. while not turtle.down() do
  510. turtle.digDown()
  511. mined = mined+1
  512. end
  513. moved = moved + 1
  514. end
  515. turtle.turnRight()
  516. if Cdouble == 1 then
  517. if turtle.digDown() == true then
  518. mined = mined + 1
  519. end
  520. end
  521. turn = 1
  522. end
  523. end
  524.  
  525. for i=1, y-b do --This gets the turtle back to the top layer
  526. while not turtle.up() do
  527. end
  528. end
  529. turtle.turnLeft()
  530. while not turtle.forward() do
  531. end
  532.  
  533. --Counts all blocks at the end also
  534. count()
  535.  
  536. --Output to a chest or sit there
  537. endDrop()
  538. turtle.turnRight()
  539. turtle.turnRight()
  540. turtle.select(1)
  541. --Display
  542. screen(1,1)
  543. print("Total Blocks Mined: "..mined)
  544. print("Current Fuel Level: "..turtle.getFuelLevel())
  545. print("Cobble: "..totals.cobble)
  546. print("Other Raw: "..totals.sand)
  547. print("Other: "..totals.other)
  548. if rednetEnabled then
  549. print("")
  550. print("Sent Stop Message")
  551. rednet.open("right")
  552. rednet.send(host, "Stop")
  553. finalTable = {cobble = totals.cobble, sand = totals.sand,
  554.     other = totals.other, fuel = checkFuel(), mined = mined}
  555. rednet.send(host, textutils.serialize(finalTable))
  556.  
  557. rednet.close("right")
  558. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement