Advertisement
Guest User

Untitled

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