Techtronic

startup for mining turtles

Jan 17th, 2014
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.90 KB | None | 0 0
  1. -- Custom Variables Changable.
  2. local port = "right"
  3. local timeOutOnRednet = 2
  4. local limitSize = 64 -- I do not reccomend changing this as its very tempermental
  5. local fuelSlots = {}
  6.  
  7. -- DO NOT CHANGE ANYTHING BELOW
  8. rednet.open(port)
  9.  
  10. -- These are XYZ values relative to the quarry. At default they will all be 0 as quarry is at 0,0
  11. local qu_x = 0
  12. local qu_y = 0
  13. local qu_z = -1
  14. local facing = 2
  15.  
  16. -- Misc Functions
  17. function checkFuel (blocksNeeded)
  18. if turtle.getFuelLevel() < blocksNeeded then
  19. local moreFuel = true
  20. local fuelNeeded = blocksNeeded - turtle.getFuelLevel()
  21. while fuelNeeded > 0 do
  22. if turtle.refuel(1) == false then
  23. print("TAKING ITEMS")
  24. if takeItems("front") == false then return false end
  25. end
  26. fuelNeeded = blocksNeeded - turtle.getFuelLevel()
  27. end
  28. print("DROPPING ITEMS")
  29. turtle.drop()
  30. return true
  31. else
  32. print("HAVE ENOUGH")
  33. return true
  34. end
  35. return false
  36. end
  37.  
  38. function split(str, pat)
  39. local t = {}
  40. local fpat = "(.-)" .. pat
  41. local last_end = 1
  42. print(str)
  43. local s, e, cap = str:find(fpat, 1)
  44. while s do
  45. if s ~= 1 or cap ~= "" then
  46. table.insert(t,cap)
  47. end
  48. last_end = e+1
  49. s, e, cap = str:find(fpat, last_end)
  50. end
  51. if last_end <= #str then
  52. cap = str:sub(last_end)
  53. table.insert(t, cap)
  54. end
  55. return t
  56. end
  57.  
  58. local smallestDis = 0
  59. local mainPCIsLocatedAt = {}
  60. local mainBot = false
  61. -- Function finds and sets up with the nearest quarry
  62. function setupToNearestQuarry ()
  63. local ownerID = 0
  64. rednet.broadcast("CCQuarry LOCATE")
  65. local startedTime = os.clock()
  66. while (os.clock() - startedTime) <= timeOutOnRednet do
  67. local id, mess, dis = rednet.receive(timeOutOnRednet)
  68. if (mess == "CCQuarry PING") then
  69. if ownerID == 0 or smallestDis > dis then
  70. smallestDis = dis
  71. ownerID = id
  72. end
  73. end
  74. end
  75. if (smallestDis == 0) then
  76. return false
  77. else return ownerID end
  78. end
  79.  
  80. function checkIfMainBotWithQuarry (qid)
  81. local setupInformation = sendAndGetResponse(qid,"SETUP","")
  82. if setupInformation ~= false then
  83. if setupInformation == "CCQuarry SETUP MAINBOT" then
  84. return true
  85. elseif setupInformation == "CCQuarry SETUP SLAVE" then
  86. return false
  87. end
  88. else
  89. print("Quarry PC did not respond. Moving self out of way and placing other bots in inventory if any.")
  90. turtle.up()
  91. turtle.digUp()
  92. turtle.up()
  93. endWithError()
  94. end
  95. end
  96.  
  97. function turn(direction)
  98. if direction == "left" or direction == "right" then
  99. local d = 0
  100. if direction == "left" then d = 1; turtle.turnLeft() else d = -1; turtle.turnRight() end
  101. facing = facing + d
  102. if (facing == -1) then facing = 3 elseif (facing == 4) then facing = 0 end
  103. return true
  104. end
  105. return false
  106. end
  107.  
  108. -- Function turns to the specified facing location.
  109. function turnToN (face)
  110. if face == facing then return true end
  111. local lOrR = (facing - face + 4) % 4
  112. if lOrR > 2 then
  113. turn("left")
  114. elseif lOrR < 2 then
  115. turn("right")
  116. else
  117. turn("left")
  118. turn("left")
  119. end
  120. return true
  121. end
  122. function turnTo (nesw)
  123. if nesw == "north" then
  124. turnToN(0)
  125. elseif nesw == "south" then
  126. turnToN(2)
  127. elseif nesw == "west" then
  128. turnToN(1)
  129. elseif nesw == "east" then
  130. turnToN(3)
  131. end
  132. end
  133. function move (dir)
  134. if dir == "up" then
  135. if turtle.up() then
  136. qu_y = qu_y + 1
  137. return true
  138. else return false end
  139. elseif dir == "down" then
  140. if turtle.down() then
  141. qu_y = qu_y - 1
  142. return true
  143. else return false end
  144. elseif dir == "north" or dir == "south" or dir == "west" or dir == "east" then
  145. turnTo(dir)
  146. local moved = turtle.forward()
  147. if moved then
  148. if dir == "north" then qu_z = qu_z + 1 elseif dir == "south" then qu_z = qu_z - 1 elseif dir == "west" then qu_x = qu_x - 1 elseif dir == "east" then qu_x = qu_x + 1 end
  149. return true
  150. else return false end
  151. end
  152. return false
  153. end
  154. function moveTo (targetX, targetZ)
  155. local way = "west"
  156. if targetX > qu_x then
  157. way = "east"
  158. end
  159. while targetX ~= qu_x do move(way) end
  160. way = "south"
  161. if targetZ > qu_z then
  162. way = "north"
  163. end
  164. while targetZ ~= qu_z do move(way) end
  165. end
  166. local lengthOfCurrentQuarry = 0
  167. local widthOfCurrentQuarry = 1
  168. function doMainbotFunctions(qid)
  169. -- Setting new orientation without using turn functions so we get our 0 point.
  170. for side = 1, 4, 1 do
  171. if turtle.detect() == false then
  172. break
  173. end
  174. turtle.turnLeft()
  175. if side == 4 then
  176. print ("Error, you seem to have not given me anywhere to go :(")
  177. endWithError()
  178. end
  179. end
  180. facing = 0
  181. turnTo("east")
  182.  
  183. while not checkFuel((limitSize * 2) + 32) do
  184. print("Need more fuel. Add more fuel then type to continue");
  185. read()
  186. end
  187.  
  188. -- Give extra fuel just in case!
  189. while move("north") do
  190. lengthOfCurrentQuarry = lengthOfCurrentQuarry + 1
  191. if (lengthOfCurrentQuarry >= limitSize) then
  192. break
  193. end
  194. end
  195. sendMessage(qid, "LENGTH", lengthOfCurrentQuarry)
  196. for a = 2, lengthOfCurrentQuarry, 1 do
  197. move("south")
  198. end
  199. while move("east") do
  200. widthOfCurrentQuarry = widthOfCurrentQuarry + 1
  201. if (widthOfCurrentQuarry >= limitSize) then
  202. break
  203. end
  204. end
  205. sendMessage(qid, "WIDTH", widthOfCurrentQuarry)
  206. print("Quarry is "..lengthOfCurrentQuarry.. " long and ".. widthOfCurrentQuarry.." wide")
  207. for a = 2, widthOfCurrentQuarry, 1 do
  208. move("west")
  209. end
  210.  
  211. moveTo(-1,0)
  212. turnTo("south")
  213. local items = checkChangedSlot({}, false)
  214. while takeItems("front") do end
  215. local changedItems = checkChangedSlot(items, true)
  216. moveTo(0,0)
  217. turnTo("south")
  218. setupNewBots(changedItems, qid)
  219. move("south")
  220. end
  221. function waitForBotToMove()
  222. while turtle.detect() do sleep(1) end
  223. return true
  224. end
  225. -- Function assumes it is positioned in the correct location
  226. function setupNewBots (arr, qid)
  227. sendMessage(qid, "STARTUP", "")
  228. for botSlot = 1, table.getn(arr), 1 do
  229. turtle.select(arr[botSlot])
  230. while (turtle.getItemCount(arr[botSlot]) ~= 0) do
  231. turtle.place()
  232. sendMessage(qid, "TURNONBOT","")
  233. waitForBotToMove()
  234. end
  235. end
  236. sendMessage(qid, "DONESTARTUP", "")
  237. end
  238. function endWithError () os.exit() end
  239.  
  240. -- Function assumes there will be a response... if not youre fucked!
  241. function sendAndGetResponse (recid, messageID, message)
  242. sendMessage(recid,messageID,message)
  243. local startedTime = os.clock()
  244. while (os.clock() - startedTime) <= timeOutOnRednet do
  245. local id, mess, dis = rednet.receive(timeOutOnRednet)
  246. if id == recid or recid == 0 then
  247. local splitMessage = split(mess, " ")
  248. if (splitMessage[1] == "CCQuarry") then
  249. if (splitMessage[2] == messageID) then
  250. return mess
  251. end
  252. end
  253. end
  254. end
  255. return false
  256. end
  257. function sendAndGetResponseNoTimeout (recid, messageID, message)
  258. sendMessage(recid,messageID,message)
  259. while true do
  260. local id, mess, dis = rednet.receive(0)
  261. if id == recid or recid == 0 then
  262. local splitMessage = split(mess, " ")
  263. if (splitMessage[1] == "CCQuarry") then
  264. if (splitMessage[2] == messageID) then
  265. return mess
  266. end
  267. end
  268. end
  269. end
  270. -- I assume this will never be reached but just in case >.>
  271. return false
  272. end
  273. function sendMessage (qid, messageID, message)
  274. rednet.send(qid,"CCQuarry "..messageID.." "..message)
  275. end
  276. -- Function assumes no obsticals. Checks if turtle can get to a specified location without fuel. True if needs fuel false if not
  277. function needFuel (currX, currZ, currY, neededX, neededZ, neededY)
  278. if currY ~= neededY then return true end
  279. if (currX == neededX) and ((neededZ - currZ) == 1 or (currZ - neededZ) == 1) then
  280. return false
  281. elseif (currZ == neededZ) and ((neededX - currX) == 1 or (currX - neededX) == 1) then
  282. return false
  283. end
  284. return true
  285. end
  286. local knowsWhichWayIsX = false;
  287. local directionXIs = 0;
  288.  
  289. -- Function WILL NOT move to block. It will not use fuel and it assumes you can actually face the block without moving and still be next to it.
  290. function faceBlock (currX, currZ, currY, neededX, neededZ, neededY)
  291. if not needFuel(currX, currZ, currY, neededX, neededZ, neededY) then
  292.  
  293. end
  294. end
  295. -- Function returns array of the slots it placed the items from or false if could not take items
  296. function takeItems (direction)
  297. if direction ~= "up" and direction ~= "down" and direction ~= "front" then print("Error direction "..direction.." matched no valid direction takeItems(direction)"); endWithError(); end
  298. local emptySlot = getNextEmptySlot()
  299. local checkArr = {}
  300. if (emptySlot ~= false) then
  301. turtle.select(emptySlot)
  302. else
  303. checkArr = checkChangedSlot({}, false)
  304. end
  305. local success = false
  306. if direction == "up" then
  307. success = turtle.suckUp()
  308. elseif direction == "down" then
  309. success = turtle.suckDown()
  310. else
  311. success = turtle.suck()
  312. end
  313. if success then
  314. if emptySlot ~= false then return {emptySlot}
  315. else
  316. return checkChangedSlot(checkArr, true)
  317. end
  318. else return false; end
  319. end
  320.  
  321. -- Function gives an array of all the slots and their counts if secondCheck is false and array is just a blank array
  322. -- If secondCheck is true it expects the array and loops through until it finds changes. It will return an array of changed slots
  323. -- Useful for when you take from an inventory and it fills a slot up you can find where it put it.
  324. function checkChangedSlot (array, secondCheck)
  325. local newArr = {}
  326. if secondCheck == false then
  327. for a = 1, 16, 1 do
  328. newArr[a] = turtle.getItemCount(a)
  329. end
  330. return newArr
  331. else
  332. if table.getn(array) == 16 then
  333. for a = 1, 16, 1 do
  334. if array[a] ~= turtle.getItemCount(a) then
  335. newArr[(table.getn(newArr) + 1)] = a
  336. end
  337. end
  338. return newArr
  339. else
  340. print("Array given to checkChangedSlot is not 16 in length. Cannot continue.")
  341. end
  342. end
  343. end
  344.  
  345. function getNextEmptySlot ()
  346. for a = 1, 16, 1 do
  347. if turtle.getItemCount(a) == 0 then
  348. return a
  349. end
  350. end
  351. return false
  352. end
  353.  
  354. function getWorkingLane (qid)
  355. local dd = sendAndGetResponseNoTimeout(qid, "LANE","")
  356. return dd
  357. end
  358.  
  359. function requestServiceLane (qid)
  360. local canGoYet = sendAndGetResponseNoTimeout(qid, "SERVICE", "")
  361.  
  362. if canGoYet == "CCQuarry SERVICE GO" then
  363. move("up")
  364. return true
  365. end
  366. endWithError()
  367. end
  368.  
  369. function startDigging(qid)
  370. turtle.digDown()
  371. while turtle.digDown() or move("down") do end
  372. while qu_y ~= 0 do move("up") end
  373. end
  374. function makeDropAtStation (qid)
  375. moveTo(0,-1)
  376. move("down")
  377. turnTo("west")
  378. for inv = 1, 16, 1 do
  379. turtle.select(inv)
  380. turtle.drop()
  381. end
  382. turnTo("east")
  383. return true
  384. end
  385. local doneFirstLane= false
  386. local currentLane = {}
  387. local mainbot = false
  388. function processLane (qid)
  389. local getCurrLane = ""
  390. if doneFirstLane == false and mainbot == true then getCurrLane = "CCQuarry LANE 1 1"; doneFirstLane = true else getCurrLane = getWorkingLane(qid) end
  391. print(getCurrLane)
  392. if getCurrLane ~= false then
  393. if getCurrLane ~= "CCQuarry LANE ENDLANE" then
  394. local splitMess = split(getCurrLane, " ")
  395. print(splitMess[3])
  396. currentLane[1] = splitMess[3] + 0
  397. currentLane[2] = splitMess[4] + 0
  398. turnTo("north")
  399. print("Supposed to get to ".. currentLane[1] .. " , "..currentLane[2])
  400. turnTo("east")
  401. checkFuel(2 + currentLane[1] + currentLane[2] + 256)
  402. move("up")
  403. moveTo(currentLane[1] - 1, currentLane[2] - 1)
  404. move("down")
  405. sendMessage(qid, "DONESERVICE", "")
  406. startDigging(qid)
  407. requestServiceLane(qid)
  408. makeDropAtStation(qid)
  409. return true
  410. else
  411. return false
  412. end
  413. else
  414. print("Quarry did not respond in time.")
  415. endWithError()
  416. end
  417. end
  418.  
  419.  
  420. local MasterID = setupToNearestQuarry()
  421.  
  422. print("Starting Up")
  423. if MasterID ~= false then
  424. print("Am I Mainbot?")
  425. mainbot = checkIfMainBotWithQuarry (MasterID)
  426. if mainbot then doMainbotFunctions(MasterID) end
  427. while processLane(MasterID) do end
  428. print ("Requesting again :D!")
  429. requestServiceLane(MasterID)
  430. move("up")
  431. moveTo(currentLane[1] - 1,currentLane[2] - 1)
  432. move("up")
  433. sendMessage(qid, "DONESERVICE", "")
  434. else
  435. print("No Quarry PC found. Please set that up first before me.")
  436. endWithError()
  437. end
Add Comment
Please, Sign In to add comment