yeah568

Untitled

Oct 29th, 2013
91
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. args = {...}
  2. -- Make sure turtle is positioned correctly
  3. term.clear()
  4. term.setCursorPos(1,1)
  5. print("I am set up to dig a rectangle downward in a forward-left direction. There should be a refuel chest to my right and a dropoff chest behind me. Is this how I am set up?")
  6. print("\(y/n\)")
  7. while true do
  8. local event, character = os.pullEvent()
  9. if event == "char" and character == "y" then
  10. print("Initializing...")
  11. sleep(1)
  12. break
  13. elseif event == "char" and character == "n" then
  14. print("Please set up correctly.")
  15. error()
  16. end
  17. end
  18.  
  19. local function forward() --Forward movement
  20. --Move forward
  21. local i = 0 --Iterator for bedrock/strong player detection
  22. while not turtle.forward() do
  23. if not turtle.dig() then --Detect blocks
  24. i = i + 1
  25. turtle.attack() --Detect entities
  26. if i == 30 then
  27. return false --If movement fails
  28. end
  29. end
  30. end
  31. --Clear above and below
  32. while turtle.detectUp() do
  33. turtle.digUp()
  34. end
  35. while turtle.detectDown() do
  36. turtle.digDown()
  37. end
  38. --Position tracking
  39. if currentpos.f == 0 then
  40. currentpos.z = currentpos.z + 1
  41. elseif currentpos.f == 1 then
  42. currentpos.x = currentpos.x - 1
  43. elseif currentpos.f == 2 then
  44. currentpos.z = currentpos.z - 1
  45. elseif currentpos.f == 3 then
  46. currentpos.x = currentpos.x + 1
  47. else
  48. running = false
  49. error("Something went wrong with the direction :P/>/>/>")
  50. end
  51. return true
  52. end
  53. local function turnRight() --Right turn with position tracking
  54. turtle.turnRight()
  55. if currentpos.f < 3 then
  56. currentpos.f = currentpos.f + 1
  57. else
  58. currentpos.f = 0
  59. end
  60. end
  61. local function turnLeft() --Left turn with position tracking
  62. turtle.turnLeft()
  63. if currentpos.f > 0 then
  64. currentpos.f = currentpos.f - 1
  65. else
  66. currentpos.f = 3
  67. end
  68. end
  69. local function down() --Downward movement
  70. --Move down
  71. local i = 0 --Iterator for bedrock detection
  72. while not turtle.down() do
  73. if not turtle.digDown() then --Detect blocks
  74. i = i + 1
  75. turtle.attackDown() --Detect entities
  76. if i == 25 then
  77. return false --If movement fails
  78. end
  79. end
  80. end
  81. --Position tracking
  82. currentpos.y = currentpos.y - 1
  83. return true
  84. end
  85. local function mineDown() --Moves one layer downward
  86. if currentpos.y == edge.y + 2 then --If close to bottom (2 blocks away)
  87. if not down() then --If downward movement fails, return to start
  88. shell.run("goto","special",0,0,0,0,currentpos.x,currentpos.y,currentpos.z,currentpos.f)
  89. running = false
  90. error("I think I tried to dig bedrock.")
  91. end
  92. elseif currentpos.y == edge.y + 3 then --If close to bottom (3 blocks away)
  93. for i=1,2 do
  94. if not down() then --If downward movement fails, return to start
  95. shell.run("goto","special",0,0,0,0,currentpos.x,currentpos.y,currentpos.z,currentpos.f)
  96. running = false
  97. error("I think I tried to dig bedrock.")
  98. end
  99. end
  100. else --If far enough from bottom
  101. for i=1,3 do
  102. if not down() then --If downward movement fails, return to start
  103. shell.run("goto","special",0,0,0,0,currentpos.x,currentpos.y,currentpos.z,currentpos.f)
  104. running = false
  105. error("I think I tried to dig bedrock.")
  106. end
  107. end
  108. end
  109. end
  110. local function getFuelLevel() --Check if fuel level is unlimited
  111. local fuelLevel = turtle.getFuelLevel()
  112. if type(fuelLevel) == "string" then
  113. fuelLevel = 9001e9001
  114. end
  115. return fuelLevel
  116. end
  117. local function findDistance(x,y,z,newx,newy,newz) --Find how many blocks to travel to get somewhere (non-diagonally)
  118. local distance = 0
  119. local xDist = 0
  120. local yDist = 0
  121. local zDist = 0
  122. if x > newx then
  123. xDist = x - newx
  124. elseif x < newx then
  125. xDist = newx - x
  126. end
  127. if y > newy then
  128. yDist = y - newy
  129. elseif y < newy then
  130. yDist = newy - y
  131. end
  132. if z > newz then
  133. zDist = z - newz
  134. elseif z < newz then
  135. zDist = newz - z
  136. end
  137. distance = xDist + yDist + zDist
  138. return distance
  139. end
  140. local function saveLoc()
  141. --Write variables to savefile
  142. local fPos = fs.open("GPSExcavateCurrentpos","w")
  143. fPos.writeLine(currentpos.x)
  144. fPos.writeLine(currentpos.y)
  145. fPos.writeLine(currentpos.z)
  146. fPos.writeLine(currentpos.f)
  147. fPos.writeLine(edge.x)
  148. fPos.writeLine(edge.y)
  149. fPos.writeLine(edge.z)
  150. fPos.writeLine(backwards)
  151. fPos.writeLine(totalMined)
  152. fPos.writeLine(lastSlot)
  153. fPos.close()
  154. end
  155. local function detectUnwanted()
  156. local unwantedSlots = 0
  157. for i=1, lastSlot do
  158. turtle.select(i)
  159. if turtle.compareTo(13) or turtle.compareTo(14) or turtle.compareTo(15) or turtle.compareTo(16) then
  160. unwantedSlots = unwantedSlots + 1
  161. end
  162. end
  163. turtle.select(1)
  164. return unwantedSlots
  165. end
  166. local function dropUnwanted()
  167. local freeSlots = 0
  168. turtle.turnLeft()
  169. turtle.turnLeft()
  170. for i=1, lastSlot do
  171. turtle.select(i)
  172. if turtle.compareTo(13) or turtle.compareTo(14) or turtle.compareTo(15) or turtle.compareTo(16) then
  173. turtle.drop()
  174. end
  175. end
  176. turtle.turnLeft()
  177. turtle.turnLeft()
  178. turtle.select(1)
  179. end
  180. local function dropAll() --Drop mined resources, display amounts
  181. local mined = 0
  182. turtle.turnRight()
  183. turtle.turnRight()
  184. for i=1,lastSlot do
  185. turtle.select(i)
  186. mined = mined + turtle.getItemCount(i)
  187. turtle.drop()
  188. end
  189. --This will send to rednet soon
  190. totalMined = totalMined + mined
  191. print("Minerals mined this run: "..mined)
  192. print("Total mined: "..totalMined)
  193. turtle.select(1)
  194. turtle.turnRight()
  195. turtle.turnRight()
  196. end
  197. local function refuel() --Refuel if needed
  198. turtle.turnRight()
  199. turtle.select(1)
  200. while getFuelLevel() < findDistance(currentpos.x,currentpos.y,currentpos.z,0,0,0) + 400 do --Enough to make it back to where he was digging and dig a bit
  201. turtle.suck()
  202. if turtle.getItemCount(1) == 0 then --If no fuel is in the box
  203. print("Please put fuel in my top-left slot, then press space.")
  204. while true do
  205. local event, character = os.pullEvent()
  206. if event == "key" and character == 57 then
  207. print("Refueling...")
  208. sleep(1)
  209. break
  210. end
  211. end
  212. end
  213. if not turtle.refuel() then --If item isn't fuel
  214. print("This is not fuel! Please remove it, then press space.")
  215. while true do
  216. local event, character = os.pullEvent()
  217. if event == "key" and character == 57 then
  218. print("Refueling...")
  219. sleep(1)
  220. break
  221. end
  222. end
  223. end
  224. end
  225. turtle.turnLeft()
  226. end
  227. local function dropRefuel()
  228. print("Dropping & refueling")
  229. shell.run("goto","special",0,0,0,0,currentpos.x,currentpos.y,currentpos.z,currentpos.f) --Return to start
  230. dropAll()
  231. refuel()
  232. shell.run("goto","special",currentpos.x,currentpos.y,currentpos.z,currentpos.f,0,0,0,0) --Return to where he left off
  233. end
  234. local function excavate() --The actual excavation process
  235. while running do --Make sure stop signal hasn't been sent
  236. turtle.select(1)
  237. if currentpos.x == 0 and currentpos.y == 0 and currentpos.z == 0 then --To start off a layer down
  238. down()
  239. turtle.digDown()
  240. end
  241. if ( currentpos.x == edge.x and currentpos.y == edge.y + 1 and currentpos.z == edge.z or currentpos.x == edge.x and currentpos.y == edge.y + 1 and currentpos.z == 0 ) and not backwards or ( currentpos.x == 0 and currentpos.y == edge.y + 1 and currentpos.z == 0 or currentpos.x == 0 and currentpos.y == edge.y + 1 and currentpos.z == edge.z ) and backwards then --Very long check to see if at the end of process
  242. if lastSlot ~= 16 and detectUnwanted() then
  243. dropUnwanted()
  244. end
  245. shell.run("goto","special",0,0,0,0,currentpos.x,currentpos.y,currentpos.z,currentpos.f) --Return to start
  246. dropAll()
  247. print("Done digging a hole! Whew, that was hard work.")
  248. done = true --Record that turtle is finished digging
  249. running = false --Stop other "stopping" loop
  250. break
  251. end
  252. if turtle.getItemCount(lastSlot) > 0 then --Check if inventory is full or fuel is low
  253. if lastSlot ~= 16 then
  254. dropUnwanted()
  255. if detectUnwanted() < 3 then
  256. dropRefuel()
  257. elseif turtle.getItemCount(lastSlot) > 0 then
  258. turtle.select(lastSlot)
  259. while turtle.getItemCount(lastSlot) > 0 do
  260. for i=1, lastSlot do
  261. turtle.transferTo(i)
  262. end
  263. end
  264. end
  265. else
  266. dropRefuel()
  267. end
  268. end
  269. if getFuelLevel() < (findDistance(currentpos.x,currentpos.y,currentpos.z,0,0,0) + 16) then
  270. if lastSlot ~= 16 then
  271. dropUnwanted()
  272. end
  273. dropRefuel()
  274. end
  275. if ( currentpos.x == edge.x and currentpos.z == edge.z or currentpos.x == edge.x and currentpos.z == 0 ) and not backwards then --If at the end of a layer
  276. mineDown()
  277. turnRight()
  278. turnRight()
  279. backwards = true --Switching directions
  280. turtle.digDown()
  281. elseif ( currentpos.x == 0 and currentpos.z == 0 or currentpos.x == 0 and currentpos.z == edge.z ) and backwards then --If at the end of a layer
  282. mineDown()
  283. turnLeft()
  284. turnLeft()
  285. backwards = false --Switching directions
  286. turtle.digDown()
  287. elseif currentpos.z == edge.z then --If at edge, turn around and do next line
  288. if backwards then
  289. turnRight()
  290. forward()
  291. turnRight()
  292. else
  293. turnLeft()
  294. forward()
  295. turnLeft()
  296. end
  297. elseif currentpos.z == 0 and currentpos.x ~= 0 then --If at edge, turn around and do next line
  298. if backwards then
  299. turnLeft()
  300. forward()
  301. turnLeft()
  302. else
  303. turnRight()
  304. forward()
  305. turnRight()
  306. end
  307. end
  308. if not forward() then --If movement fails, return to start
  309. shell.run("goto","special",0,0,0,0,currentpos.x,currentpos.y,currentpos.z,currentpos.f)
  310. running = false
  311. error("I think I tried to dig bedrock.")
  312. end
  313. saveLoc()
  314. end
  315. end
  316. local function stop() --Ability to stop turtle mid-excavation. This will wait until current action is done, then exit the excavate function
  317. while running do
  318. local event, data, message = os.pullEvent()
  319. if event == "char" and data == "p" then --If direct keypress
  320. print("Stopping...")
  321. running = false
  322. break
  323. end
  324. end
  325. end
  326. local function restart() --To restart from previous digging
  327. print("Restarting from saved position...")
  328. if not fs.exists("GPSExcavateCurrentpos") then -- Check for save file
  329. error("Could not find saved position!")
  330. end
  331. --Read save file, change variables
  332. local fPos = fs.open("GPSExcavateCurrentpos","r")
  333. currentpos.x = tonumber(fPos.readLine())
  334. currentpos.y = tonumber(fPos.readLine())
  335. currentpos.z = tonumber(fPos.readLine())
  336. currentpos.f = tonumber(fPos.readLine())
  337. edge.x = tonumber(fPos.readLine())
  338. edge.y = tonumber(fPos.readLine())
  339. edge.z = tonumber(fPos.readLine())
  340. local backwardsString = fPos.readLine()
  341. if backwardsString == "true" then
  342. backwards = true
  343. else
  344. backwards = false
  345. end
  346. totalMined = tonumber(fPos.readLine())
  347. lastSlot = tonumber(fPos.readLine())
  348. fPos.close()
  349. shell.run("goto","special",currentpos.x,currentpos.y,currentpos.z,currentpos.f,0,0,0,0) --Go to position where turtle left off
  350. restarting = true
  351. print("Let the diggy-hole recommence!")
  352. end
  353.  
  354. totalMined = 0 --Total mined out blocks over course of excavation
  355. restarting = false --Whether turtle is restarting from save
  356. done = false --Whether turtle has completed excavation
  357. running = true --Whether turtle is currently digging
  358. backwards = false --Which direction turtle is digging the layers currently
  359. currentpos = {} --Current position storage. It's a table just because it is easier, no real need for it
  360. edge = {} --Boundaries of hole. Same deal as currentpos, no real reason to have it as a table
  361. id = -1 --Id of computer that sent the rednet message. This is so that it can reply when it has stopped
  362. w, l, d = 0, 0, 0 --Width, length, depth of hole. This is just for input of numbers
  363. currentpos.x, currentpos.y, currentpos.z, currentpos.f = 0, 0, 0, 0 --Initialise pieces of currentpos
  364. lastSlot = 16 --Slot in which to make sure there are no blocks: this is to keep any comparing slots free
  365.  
  366. if #args == 1 and args[1] == "restart" then --If restarting from save
  367. restart()
  368. elseif #args == 2 and tonumber(args[1]) > 1 and tonumber(args[2]) > 2 then --If a square hole is wanted
  369. w = tonumber(args[1])
  370. l = w
  371. d = tonumber(args[2])
  372. elseif #args == 3 and tonumber(args[1]) > 1 and tonumber(args[2]) > 1 and tonumber(args[3]) > 2 then --If a non-square hole is wanted
  373. w = tonumber(args[1])
  374. l = tonumber(args[2])
  375. d = tonumber(args[3])
  376. else --If arguments improperly input, print usage
  377. print("Usage: \"GPSExcavate <side> <depth>\" or \"GPSExcavate <width> <length> <depth>\"")
  378. print("Note: depth must be at least 3.")
  379. print("To restart digging, use: \"GPSExcavate restart\"")
  380. error()
  381. end
  382. if not restarting then --Input edge locations
  383. edge.x = w - 1
  384. edge.y = -(d - 1)
  385. edge.z = l - 1
  386. print("Would you like the turtle not to collect certain blocks?")
  387. print("\(y/n\)")
  388. while true do
  389. local event, character = os.pullEvent()
  390. if event == "char" and character == "y" then
  391. lastSlot = 12
  392. print("Please put unwanted blocks in the bottom four slots, then press space to continue.")
  393. while true do
  394. local event, character = os.pullEvent()
  395. if event == "key" and character == 57 then
  396. print("Turtle will not collect these blocks.")
  397. break
  398. end
  399. end
  400. break
  401. elseif event == "char" and character == "n" then
  402. lastSlot = 16
  403. break
  404. end
  405. end
  406. print("Let the diggy-hole commence! Digging a hole "..w.." by "..l.." by "..d.." meters.")
  407. end
  408. print("Press \"p\" to save and stop at any time.")
  409. parallel.waitForAll(excavate,stop) --Actual running of program. This is to enable stopping mid-digging
  410. if not done then --If turtle was stopped before the end
  411. print("Saving position and dimensions...")
  412. sleep(1)
  413. saveLoc()
  414. print("Position and dimensions saved. Returning to base...")
  415. shell.run("goto","special",0,0,0,0,currentpos.x,currentpos.y,currentpos.z,currentpos.f) --Return to start
  416. dropAll()
  417. print("Excavation stopped.")
  418. end
  419. else --Get rid of save file if turtle is done excavating. I will find a way to have rednet in here too
  420. fs.delete("GPSExcavateCurrentpos")
  421. end
  422. print("Next hole please? :D/>/>/>")
  423. --Delete variables so they don't persist
  424. args, currentpos, edge, id, done, restarting, running, w, l, d, backwards, lastSlot = nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil
RAW Paste Data