Advertisement
Blackhome

smart Tunneling (modem on left)

Dec 18th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.68 KB | None | 0 0
  1. local modemSide = "left"
  2. -- What to do?:
  3. -- Droping Items in Chest
  4. -- Communication of Fuel and Cobblestone
  5. -- Communication of Commands
  6. -- Communication Turtle look on each other
  7.  
  8. local turtlePosition
  9. local controleId
  10.  
  11. local notWishedItems = {}
  12. notWishedItems[0] = "minecraft:dirt"
  13. notWishedItems[1] = "minecraft:cobblestone"
  14. notWishedItems[2] = "minecraft:stone"
  15. notWishedItems[3] = "minecraft:obsidian"
  16. notWishedItems[4] = "minecraft:sand"
  17. notWishedItems[5] = "minecraft:gravel"
  18. notWishedItems[6] = "minecraft:rail"
  19. notWishedItems[7] = "minecraft:planks"
  20. notWishedItems[8] = "minecraft:fence"
  21. notWishedItems[9] = "bluepower:marble"
  22. notWishedItems[10] = "minecraft:torch"
  23. notWishedItems[11] = "minecraft:trapped_chest"
  24. notWishedItems[12] = "minecraft:flowing_water"
  25. notWishedItems[13] = "minecraft:flowing_lava"
  26. notWishedItems[14] = "minecraft:chest"
  27. notWishedItems[15] = "computercraft:CC-TurtleExpanded"
  28.  
  29.  
  30. --get the position of the turtles building the tunnel. TopLeft is position one, BottomRight is position 9
  31. function getPositionOfTurtle()
  32. local number = 10
  33. local bNum = (number > 9 or number < 1)
  34. local bShow = true
  35. while (bNum) do
  36. if(bShow) then
  37. io.write("Geben Sie an, um welche Turtel es sich handelt")
  38. io.write("Folgende Turtel git es:")
  39. io.write("Linksoben: 1\nMitteoben: 2\nRechtsoben: 3\n")
  40. io.write("Linksmitte: 4\nMitte: 5\nRechtsmitte: 6\n")
  41. io.write("Linksunten: 7\nMitteunten: 8\nRechtsunten: 9\n")
  42. bShow = false
  43. end
  44. number = tonumber(io.read())
  45. bNum = (number > 9 or number < 1)
  46. if (bNum) then
  47. io.write("Falsche Nummer, bitte erneut angeben")
  48. io.write("Wollen Sie die Nummern der Turteln nochmal eingeblendet haben?\n")
  49. io.write("Dann geben Sie die 1 ein. Ansonsten eine andere Zahl\n ")
  50. bShow = (tonumber(io.read()) == 1)
  51. end
  52. end
  53. return number
  54. end
  55.  
  56. --register the turtles to interact with
  57. function registerAllTurtles(turtleArr)
  58. if(not(rednet.isOpen(modemSide))) then
  59. rednet.open(modemSide)
  60. end
  61. while(true) do
  62. rednet.broadcast("Give Position", "registerTurtles")
  63. local tId, tPos, prot = rednet.receive("register", 0.5)
  64. if(tId and (tonumber(tPos)) > 0 and tonumber(tPos) < 10 and tonumber(tPos) ~= 5)then
  65. printMsgGotBy(tId, tPos)
  66. if(tPos < 5)then
  67. turtleArr[tPos-1] = tId
  68. else
  69. turtleArr[tPos-2] = tId
  70. end
  71. local msg = "RegisterSuccessfull"
  72. rednet.send(tId, msg)
  73. printMsgSendTo(tId, msg)
  74. end
  75. if(getArrLength(turtleArr) == 8)then
  76. while (true)do
  77. local tId, tPos, prot = rednet.receive("register", 1)
  78. if(tId == nil)then
  79. return 0
  80. else
  81. local msg = "RegisterSuccessfull"
  82. rednet.send(tId, msg)
  83. printMsgSendTo(tId, msg)
  84. end
  85. end
  86. end
  87. end
  88. end
  89. function registerAndGetSenderId(tPosition)
  90. if(not(rednet.isOpen(modemSide))) then
  91. rednet.open(modemSide)
  92. end
  93. local senderId, message, protocoll = rednet.receive("registerTurtles")
  94. printMsgGotBy(senderId, message)
  95. while(true) do
  96. rednet.send(senderId, tPosition, "register")
  97. printMsgSendTo(senderId, tPosition)
  98. local s1Id
  99. s1Id, message, protocoll = rednet.receive(0.25)
  100. if (s1Id) then
  101. printMsgGotBy(s1Id, message)
  102. end
  103.  
  104. if((s1Id == senderId) and (message == "RegisterSuccessfull")) then
  105. return senderId;
  106. end
  107. end
  108. end
  109.  
  110. function getArrLength(arr)
  111. local num1 = 0
  112. local length = 0;
  113. while (num1 <= table.getn(arr))do
  114. if (arr[num1] ~= nil)then
  115. length = length + 1
  116. end
  117. num1 = num1 + 1
  118. end
  119. return length
  120. end
  121.  
  122. --shoud be used after sending a onetime message, to make a message protocoll
  123. function printMsgSendTo(rId, msg )
  124. print("Message send to ", rId, ": ", msg, "\n")
  125. end
  126. function printMsgGotBy(sId, msg)
  127. print("Message received from ", sId, ": ", msg, "\n")
  128. end
  129.  
  130. function printArr(arr)
  131. local cnt
  132. if (arr[0] ~= nil)then
  133. cnt = 0
  134. else
  135. cnt = 1
  136. end
  137. while(cnt <= #arr)do
  138. io.write(arr[cnt], " ")
  139. cnt = cnt + 1
  140. if(cnt < #arr)then
  141. print(arr[cnt])
  142. cnt = cnt + 1
  143. end
  144. end
  145.  
  146. end
  147.  
  148. --if there is no block in the used direction, the turtle places one
  149. function setBlockOnTop()
  150. local success, data = turtle.inspectUp()
  151. if (success and data.name == "minecraft:gravel")then
  152. while(turtle.detectUp())do
  153. turtle.digUp()
  154. os.sleep(0.25)
  155. end
  156. end
  157. if(turtle.detectUp() == false or detectWaterOrLavaOnTop())then
  158. placeOutsideCobblestone("top")
  159. return true
  160. end
  161. return false
  162. end
  163. function setBlockOnBottom()
  164. if(turtle.detectDown() == false or detectWaterOrLavaOnBottom())then
  165. placeOutsideCobblestone("bottom")
  166. return true
  167. end
  168. return false
  169. end
  170. function setBlockInFront()
  171. local success, data = turtle.inspect()
  172. if (success and data.name == "minecraft:gravel")then
  173. while(turtle.detect())do
  174. turtle.dig()
  175. os.sleep(0.25)
  176. end
  177. end
  178. if(turtle.detect() == false or detectWaterOrLavaInFront())then
  179. placeOutsideCobblestone("front")
  180. return true
  181. end
  182. return false
  183. end
  184.  
  185. --starting mining on the top side of the turtle
  186. function miningOnTop(locData)
  187. local success, data = turtle.inspectUp()
  188. if(success and isAWishedBlock(data.name))then
  189. turtleUp()
  190. locData.highDis = locData.highDis + 1
  191. mining(locData.rightDis, locData.highDis, locData.lookDir)
  192. turtleDown()
  193. locData.highDis = locData.highDis - 1
  194. placeOutsideCobblestone("top")
  195. end
  196. end
  197. function miningOnBottom(locData)
  198. local success, data = turtle.inspectDown()
  199. if(success and isAWishedBlock(data.name))then
  200. turtleDown()
  201. locData.highDis = locData.highDis - 1
  202. mining(locData.rightDis, locData.highDis, locData.lookDir)
  203. turtleUp()
  204. locData.highDis = locData.highDis + 1
  205. placeOutsideCobblestone("bottom")
  206. end
  207. end
  208. function miningInFront(locData)
  209. local success, data = turtle.inspect()
  210. if(success and isAWishedBlock(data.name))then
  211. turtleForward()
  212. if (locData.lookDir % 4 == 1)then
  213. locData.rightDis = locData.rightDis + 1
  214. elseif (locData.lookDir % 4 == 3)then
  215. locData.rightDis = locData.rightDis - 1
  216. end
  217. mining(locData.rightDis, locData.highDis, locData.lookDir)
  218. turtle.turnLeft()
  219. turtle.turnLeft()
  220. turtleForward()
  221. turtle.turnLeft()
  222. turtle.turnLeft()
  223. if (locData.lookDir % 4 == 1)then
  224. locData.rightDis = locData.rightDis - 1
  225. elseif (locData.lookDir % 4 == 3)then
  226. locData.rightDis = locData.rightDis + 1
  227. end
  228. placeOutsideCobblestone("front")
  229. end
  230. end
  231.  
  232. --complete moving actions of any turtle
  233. function moveTurtleLeftTop(miningActive, locData)
  234. turtleForward()
  235. turtle.turnLeft()
  236. local sBoT = setBlockOnTop()
  237. local sBiF = setBlockInFront()
  238. local goMiningTop = miningActive and (not sBoT)
  239. local goMiningFront = miningActive and (not sBiF)
  240. if(goMiningTop)then
  241. miningOnTop(locData)
  242. end
  243. if(goMiningFront)then
  244. miningInFront(locData)
  245. end
  246. turtle.turnRight()
  247. return true
  248. end
  249. function moveTurtleMiddleTop(miningActive, locData)
  250. turtleForward()
  251. local sBoT = setBlockOnTop()
  252. local goMiningTop = miningActive and (not sBoT)
  253. if (goMiningTop)then
  254. miningOnTop(locData)
  255. end
  256. return true
  257. end
  258. function moveTurtleRightTop(miningActive, locData)
  259. turtleForward()
  260. turtle.turnRight()
  261. local sBoT = setBlockOnTop()
  262. local sBiF = setBlockInFront()
  263. local goMiningTop = miningActive and (not sBoT)
  264. local goMiningFront = miningActive and (not sBiF)
  265. if(goMiningTop)then
  266. miningOnTop(locData)
  267. end
  268. if(goMiningFront)then
  269. miningInFront(locData)
  270. end
  271. turtle.turnLeft()
  272. return true
  273. end
  274. function moveTurtleLeftMiddle(miningActive, locData)
  275. turtleForward()
  276. turtle.turnLeft()
  277. local sBiF = setBlockInFront()
  278. local goMiningFront = miningActive and (not sBiF)
  279. if(goMiningFront)then
  280. miningInFront(locData)
  281. end
  282. turtle.turnRight()
  283. return true
  284. end
  285. function moveTurtleMiddleMiddle(miningActive, locData)
  286. turtleForward()
  287. return true
  288. end
  289. function moveTurtleRightMiddle(miningActive, locData)
  290. turtleForward()
  291. turtle.turnRight()
  292. local sBiF = setBlockInFront()
  293. local goMiningFront = miningActive and (not sBiF)
  294. if(goMiningFront)then
  295. miningInFront(locData)
  296. end
  297. turtle.turnLeft()
  298. return true
  299. end
  300. function moveTurtleLeftBottom(miningActive, locData)
  301. turtleForward()
  302. turtle.turnLeft()
  303. local sBoB = setBlockOnBottom()
  304. local sBiF = setBlockInFront()
  305. local goMiningBottom = miningActive and (not sBoB)
  306. local goMiningFront = miningActive and (not sBiF)
  307. if(goMiningBottom)then
  308. miningOnBottom(locData)
  309. end
  310. if(goMiningFront)then
  311. miningInFront(locData)
  312. end
  313. turtle.turnRight()
  314. return true
  315. end
  316. function moveTurtleMiddleBottom(miningActive, locData)
  317. turtleForward()
  318. local sBoB = setBlockOnBottom()
  319. local goMiningBottom = miningActive and (not sBoB)
  320. if (goMiningBottom)then
  321. miningOnBottom(locData)
  322. end
  323. return true
  324. end
  325. function moveTurtleRightBottom(miningActive, locData)
  326. turtleForward()
  327. turtle.turnRight()
  328. local sBoB = setBlockOnBottom()
  329. local sBiF = setBlockInFront()
  330. local goMiningBottom = miningActive and (not sBoB)
  331. local goMiningFront = miningActive and (not sBiF)
  332. if(goMiningBottom)then
  333. miningOnBottom(locData)
  334. end
  335. if(goMiningFront)then
  336. miningInFront(locData)
  337. end
  338. turtle.turnLeft()
  339. return true
  340. end
  341.  
  342. function moveLeftTurtleTurningLeft()
  343. turtle.turnLeft()
  344. turtle.turnLeft()
  345. turtleForward()
  346. turtle.turnRight()
  347. end
  348. function moveRightTurtleTurningRight()
  349. turtle.turnRight()
  350. turtle.turnRight()
  351. turtleForward()
  352. turtle.turnLeft()
  353. end
  354.  
  355. function moveLeftTurtleTurningRight(miningActive)
  356. local locData ={}
  357. if(turtlePosition == 1)then
  358. locData["rightDis"] = -2
  359. locData["highDis"] = 0
  360. locData["lookDir"] = 3
  361. moveTurtleLeftTop(bMining, locData)
  362. local sBiF = setBlockInFront()
  363. local goMiningFront = miningActive and (not sBiF)
  364. if(goMiningFront)then
  365. miningInFront(locData)
  366. end
  367. turtle.turnRight()
  368. moveTurtleLeftTop(bMining, locData)
  369. moveTurtleLeftTop(bMining, locData)
  370. elseif(turtlePosition == 4)then
  371. locData["rightDis"] = -2
  372. locData["highDis"] = -1
  373. locData["lookDir"] = 3
  374. moveTurtleLeftMiddle(bMining, locData)
  375. local sBiF = setBlockInFront()
  376. local goMiningFront = miningActive and (not sBiF)
  377. if(goMiningFront)then
  378. miningInFront(locData)
  379. end
  380. turtle.turnRight()
  381. moveTurtleLeftMiddle(bMining, locData)
  382. moveTurtleLeftMiddle(bMining, locData)
  383. elseif(turtlePosition == 7)then
  384. locData["rightDis"] = -2
  385. locData["highDis"] = -2
  386. locData["lookDir"] = 3
  387. moveTurtleLeftBottom(bMining, locData)
  388. local sBiF = setBlockInFront()
  389. local goMiningFront = miningActive and (not sBiF)
  390. if(goMiningFront)then
  391. miningInFront(locData)
  392. end
  393. turtle.turnRight()
  394. moveTurtleLeftBottom(bMining, locData)
  395. moveTurtleLeftBottom(bMining, locData)
  396. end
  397. end
  398. function moveRightTurtleTurningLeft(miningActive)
  399. local locData = {}
  400. if(turtlePosition == 3)then
  401. locData["rightDis"] = 0
  402. locData["highDis"] = 0
  403. locData["lookDir"] = 1
  404. moveTurtleRightTop(bMining, locData)
  405. local sBiF = setBlockInFront()
  406. local goMiningFront = miningActive and (not sBiF)
  407. if(goMiningFront)then
  408. miningInFront(locData)
  409. end
  410. turtle.turnLeft()
  411. moveTurtleRightTop(bMining, locData)
  412. moveTurtleRightTop(bMining, locData)
  413. elseif(turtlePosition == 6)then
  414. locData["rightDis"] = 0
  415. locData["highDis"] = -1
  416. locData["lookDir"] = 1
  417. moveTurtleRightMiddle(bMining, locData)
  418. local sBiF = setBlockInFront()
  419. local goMiningFront = miningActive and (not sBiF)
  420. if(goMiningFront)then
  421. miningInFront(locData)
  422. end
  423. turtle.turnLeft()
  424. moveTurtleRightMiddle(bMining, locData)
  425. moveTurtleRightMiddle(bMining, locData)
  426. elseif(turtlePosition == 9)then
  427. locData["rightDis"] = 0
  428. locData["highDis"] = -2
  429. locData["lookDir"] = 1
  430. moveTurtleRightBottom(bMining, locData)
  431. local sBiF = setBlockInFront()
  432. local goMiningFront = miningActive and (not sBiF)
  433. if(goMiningFront)then
  434. miningInFront(locData)
  435. end
  436. turtle.turnLeft()
  437. moveTurtleRightBottom(bMining, locData)
  438. moveTurtleRightBottom(bMining, locData)
  439. end
  440. end
  441.  
  442. --moving Turtle. If there is no Fuel calling for fuel.
  443. function turtleForward()
  444. haveEnoughFuel()
  445. local numOfWait = 0
  446. while(turtle.forward() == false)do
  447. local success, data = turtle.inspect()
  448. if (success and data.name == "computercraft:CC-TurtleExpanded")then
  449. os.sleep(0.5)
  450. if (numOfWait%10 == 0)then
  451. print("Turtle is waiting in front of me")
  452. end
  453. numOfWait = numOfWait + 1
  454. else
  455. turtle.dig()
  456. end
  457. end
  458. end
  459. function turtleUp()
  460. haveEnoughFuel()
  461. local numOfWait = 0
  462. while(turtle.up() == false)do
  463. local success, data = turtle.inspectUp()
  464. if (success and data.name == "computercraft:CC-TurtleExpanded")then
  465. os.sleep(0.5)
  466. if (numOfWait%10 == 0)then
  467. print("Turtle is waiting on top of me")
  468. end
  469. numOfWait = numOfWait + 1
  470. else
  471. turtle.digUp()
  472. end
  473. end
  474. end
  475. function turtleDown()
  476. haveEnoughFuel()
  477. local numOfWait = 0
  478. while(turtle.down() == false)do
  479. local success, data = turtle.inspectDown()
  480. if (success and data.name == "computercraft:CC-TurtleExpanded")then
  481. os.sleep(0.5)
  482. if (numOfWait%10 == 0)then
  483. print("Turtle is waiting on bottom of me")
  484. end
  485. numOfWait = numOfWait + 1
  486. else
  487. turtle.digDown()
  488. end
  489. end
  490. end
  491.  
  492. --get all special blocks the turtle crosses
  493. function mining()
  494. local cnt = 0
  495. while (cnt < 6)do
  496. if (cnt == 0)then
  497. local success, data = turtle.inspectUp()
  498. if (success and isAWishedBlock(data.name))then
  499. turtleUp()
  500. mining()
  501. turtleDown()
  502. end
  503. elseif (cnt == 1)then
  504. local success, data = turtle.inspectDown()
  505. if (success and isAWishedBlock(data.name))then
  506. turtleDown()
  507. mining()
  508. turtleUp()
  509. end
  510. else
  511. local success, data = turtle.inspect()
  512. if (success and isAWishedBlock(data.name))then
  513. turtleForward()
  514. mining()
  515. turtle.turnRight()
  516. turtle.turnRight()
  517. turtleForward()
  518. turtle.turnRight()
  519. turtle.turnRight()
  520. end
  521. turtle.turnRight()
  522. end
  523. cnt = cnt + 1
  524. end
  525. end
  526. function mining(rightDis, highDis, lookDir)
  527. -- point (0|0) is the point of the right-top-turtle
  528. -- looking right is lookDir 1, looking back is lookDir 2, looking left is lookDir 3, looking forwards is lookDir 0
  529. local cnt = 0
  530. while (cnt < 6)do
  531. if (cnt == 0)then
  532. local success, data = turtle.inspectUp()
  533. if (success and isAWishedBlock(data.name) and ((highDis > -1 or highDis < -3) or (rightDis > 0 or rightDis < -2)))then
  534. turtleUp()
  535. highDis = highDis + 1
  536. mining(rightDis, highDis, lookDir)
  537. turtleDown()
  538. highDis = highDis - 1
  539. end
  540. elseif (cnt == 1)then
  541. local success, data = turtle.inspectDown()
  542. if (success and isAWishedBlock(data.name) and ((highDis > 1 or highDis < -1) or (rightDis > 0 or rightDis < -2)))then
  543. turtleDown()
  544. highDis = highDis - 1
  545. mining(rightDis, highDis, lookDir)
  546. turtleUp()
  547. highDis = highDis + 1
  548. end
  549. else
  550. local success, data = turtle.inspect()
  551. local bRightSide = (lookDir % 4 == 3 and rightDis < 2 and rightDis > -2 and highDis < 1 and highDis > -3)
  552. local bLeftSide = (lookDir % 4 == 1 and rightDis > -4 and rightDis < 0 and highDis < 1 and highDis > -3)
  553. if (success and isAWishedBlock(data.name) and (not bRightSide) and (not bLeftSide))then
  554. turtleForward()
  555. if (lookDir % 4 == 1)then
  556. rightDis = rightDis + 1
  557. elseif (lookDir % 4 == 3)then
  558. rightDis = rightDis - 1
  559. end
  560. mining(rightDis, highDis, lookDir)
  561. turtle.turnRight()
  562. turtle.turnRight()
  563. turtleForward()
  564. turtle.turnRight()
  565. turtle.turnRight()
  566. if (lookDir % 4 == 1)then
  567. rightDis = rightDis - 1
  568. elseif (lookDir % 4 == 3)then
  569. rightDis = rightDis + 1
  570. end
  571. end
  572. turtle.turnRight()
  573. lookDir = lookDir + 1
  574. end
  575. cnt = cnt + 1
  576. end
  577. end
  578.  
  579.  
  580. --returns whether there is water or lava in the used direction
  581. function detectWaterOrLavaOnTop()
  582. local success, data = turtle.inspectUp()
  583. if(data.name == "minecraft:flowing_water" or data.name == "minecraft:flowing_lava")then
  584. return true
  585. end
  586. return false
  587. end
  588. function detectWaterOrLavaOnBottom()
  589. local success, data = turtle.inspectDown()
  590. if(data.name == "minecraft:flowing_water" or data.name == "minecraft:flowing_lava")then
  591. return true
  592. end
  593. return false
  594. end
  595. function detectWaterOrLavaInFront()
  596. local success, data = turtle.inspect()
  597. if(data.name == "minecraft:flowing_water" or data.name == "minecraft:flowing_lava")then
  598. return true
  599. end
  600. return false
  601. end
  602.  
  603. --returns true, if user wants to get the item from the object
  604. function isAWishedBlock(objectName)
  605. local i = 0
  606. while (i <= #notWishedItems)do
  607. if(objectName == notWishedItems[i])then
  608. return false
  609. end
  610. i = i + 1
  611. end
  612. return true
  613. end
  614.  
  615. --place a cobblestone on the entered side of the turtle. sides are only "top", "bottom", "front"
  616. function placeOutsideCobblestone(side)
  617. local slotsLeft = 16
  618. while (slotsLeft > 0)do
  619. turtle.select(slotsLeft)
  620. local data = turtle.getItemDetail()
  621. if (data)then
  622. if (data.name == "minecraft:cobblestone")then
  623. break
  624. elseif(slotsLeft == 1 and turtlePosition ~= 5)then
  625. getCobbleWithMsg()
  626. elseif(slotsLeft == 1 and turtlePosition == 5)then
  627. print("Please give some Cobblestone to this turtle! After that press enter!")
  628. io.read()
  629. slotsLeft = 17
  630. end
  631. elseif (slotsLeft == 1 and turtlePosition == 5)then
  632. print("Please give some Cobblestone to this turtle! After that press enter!")
  633. io.read()
  634. slotsLeft = 17
  635. elseif (slotsLeft == 1 and turtlePosition ~= 5)then
  636. getCobbleWithMsg()
  637. end
  638. slotsLeft = slotsLeft - 1
  639. end
  640.  
  641. if(side == "top")then
  642. turtle.placeUp()
  643. elseif(side == "bottom")then
  644. turtle.placeDown()
  645. elseif(side == "front")then
  646. turtle.place()
  647. end
  648. end
  649. function getCobbleWithMsg()
  650. callConTurtle("needCobble", "msgCobReceived")
  651. local slotsLeft = 16
  652. while (slotsLeft > 0)do
  653. turtle.select(slotsLeft)
  654. local data = turtle.getItemDetail()
  655. if (data and data.name == "minecraft:cobblestone")then
  656. break
  657. elseif (slotsLeft == 1)then
  658. print("Please give some Cobblestone to this turtle! After that press enter!")
  659. io.read()
  660. slotsLeft = 17
  661. end
  662. slotsLeft = slotsLeft - 1
  663. end
  664. end
  665.  
  666. --makes enough fuel by filling up with own fuel or calling for new one
  667. function haveEnoughFuel()
  668. if (turtle.getFuelLevel() == 0 and turtlePosition ~= 5)then
  669. refuelWithMsg()
  670. elseif(turtle.getFuelLevel() == 0 and turtlePosition == 5)then
  671. while(fuelWithOwnRessources() == false)do
  672. print("Please give some fuel to this turtle! After that press enter!")
  673. io.read()
  674. end
  675. end
  676. return true
  677. end
  678. function fuelWithOwnRessources()
  679. local slotsLeft = 16
  680. while (slotsLeft > 0)do
  681. turtle.select(slotsLeft)
  682. if(turtle.refuel())then
  683. return true
  684. end
  685. slotsLeft = slotsLeft - 1
  686. end
  687. return false
  688. end
  689. function refuelWithMsg()
  690. callConTurtle("refuel", "msgFuelReceived")
  691. while(fuelWithOwnRessources() == false)do
  692. print("Please give some fuel to this turtle! After that press enter!")
  693. io.read()
  694. end
  695. end
  696.  
  697. function getMsgByConsoleTurtle()
  698. local sId, msg, protocoll
  699. while(true)do
  700. sId, msg, protocoll = rednet.receive()
  701. printMsgGotBy(sId, msg)
  702. if(sId == controleId)then
  703. break
  704. end
  705. end
  706.  
  707. return msg
  708. end
  709.  
  710. function callConTurtle(msg, msgB)
  711. local answer = false
  712. local num = 0
  713. while(not answer)do
  714. rednet.send(controleId, msg)
  715. if(num % 20 == 0)then
  716. printMsgSendTo(controleId, msg)
  717. end
  718. local sId, msgA, protocoll = rednet.receive(0.25)
  719. printMsgGotBy(sId, msgA)
  720. if (sId == controleId and msgA == msgB)then
  721. answer = true
  722. end
  723. num = num + 1
  724. end
  725. end
  726.  
  727. function callAllTurtles(msg, turtleList)
  728. local num = 0
  729. while (num <= #turtleList)do
  730. print(turtleList[num])
  731. rednet.send(turtleList[num], msg)
  732. printMsgSendTo(turtleList[num], msg)
  733. num = num + 1
  734. end
  735. end
  736. function giveTurtlesPosNames(turtList, posNames)
  737. local turtleList = {}
  738. local num = 0
  739. while (num < 8)do
  740. turtleList[posNames[num]] = turtList[num]
  741. num = num + 1
  742. end
  743. return turtleList
  744. end
  745. function getNumInList(id, list)
  746. local cnt = 0
  747. while (cnt < 8)do
  748. if (list[cnt] == id)then
  749. return cnt
  750. end
  751. cnt = cnt + 1
  752. end
  753. return false
  754. end
  755. function isEveryEntryLookedWord(list, lWord)
  756. local num = 1
  757. while (num <= #list)do
  758. if (list[num] ~= lWord)then
  759. return false
  760. end
  761. num = num + 1
  762. end
  763. return true
  764. end
  765. function getAllMessages(posNames, sideTurtleArray)
  766. local bSuccess = false
  767. local successList = {false, false, false, false, false, false, false, false}
  768. local loopNum = 1
  769. while(not bSuccess)do
  770. local sId, message, protocoll = rednet.receive()
  771. printMsgGotBy(sId, message)
  772. local pos = getNumInList(sId, sideTurtleArray)
  773. if (pos ~= false)then
  774. if (message == "refuel")then
  775. rednet.send(sId, "msgFuelReceived")
  776. printMsgSendTo(sId, "msgFuelReceived")
  777. print("Turtle at ", posNames[pos], "needs more Fuel. Please refuel there")
  778. print("Press enter to continue!")
  779. io.read()
  780. elseif (message == "needCobble")then
  781. rednet.send(sId, "msgCobReceived")
  782. printMsgSendTo(sId, "msgCobReceived")
  783. print("Turtle at ", posNames[pos], "needs more Cobblestone. Please fill it up there")
  784. print("Press enter to continue!")
  785. elseif (message == "movedForwardCleared")then
  786. rednet.send(sId, "fullMovementReceived")
  787. printMsgSendTo(sId, "fullMovementReceived")
  788. successList[pos + 1] = true
  789. end
  790. end
  791. bSuccess = isEveryEntryLookedWord(successList, true)
  792. loopNum = loopNum + 1
  793. end
  794. end
  795. function changeSettings(settings)
  796. print("Do you want to mine? Then enter an one")
  797. if (tonumber(io.read()) == 1)then
  798. settings.bMining = true
  799. end
  800. print("Do you want to go to an point or an choosen distance forward?")
  801. print("Enter an one for special coordinates")
  802. if (tonumber(io.read()) == 1)then
  803. print("Please enter the X-coordinate of the middle turtle!")
  804. settings.ownCoordX = tonumber(io.read())
  805. print("Please enter the Y-coordinate of the middle turtle!")
  806. settings.ownCoordY = tonumber(io.read())
  807. print("Please enter the Z-coordinate of the middle turtle!")
  808. settings.ownCoordZ = tonumber(io.read())
  809.  
  810. print("Please enter the X-coordinate of the target!")
  811. settings.targetCoordX = tonumber(io.read())
  812. print("Please enter the Y-coordinate of the target!")
  813. settings.targetCoordY = tonumber(io.read())
  814. print("Please enter the Z-coordinate of the target!")
  815. settings.targetCoordZ = tonumber(io.read())
  816.  
  817. print("Please enter the direction the turtle is looking at:")
  818. print("For higher X enter 0")
  819. print("For higher Z enter 1")
  820. print("For lower X enter 2")
  821. print("For lower Z enter 3")
  822. settings.lookingIn = tonumber(io.read()) -- 2
  823.  
  824. local distanceX = settings.targetCoordX - settings.ownCoordX -- -21
  825. local distanceY = settings.targetCoordY - settings.ownCoordY -- 0
  826. local distanceZ = settings.targetCoordZ - settings.ownCoordZ -- 47
  827.  
  828. if (settings.lookingIn == 0)then
  829. settings.disForwards = distanceX
  830. elseif(settings.lookingIn == 1)then
  831. settings.disForwards = distanceZ --
  832. elseif(settings.lookingIn == 2)then
  833. settings.disForwards = -distanceX
  834. else
  835. settings.disForwards = -distanceZ
  836. end
  837. settings.disUp = distanceY
  838. if (settings.lookingIn == 0)then
  839. if(settings.disForwards > 0)then
  840. settings.disLeft = -distanceZ
  841. else
  842. settings.disLeft = distanceZ
  843. end
  844. elseif (settings.lookingIn == 1)then
  845. if(settings.disForwards > 0)then
  846. settings.disLeft = distanceX
  847. else
  848. settings.disLeft = -distanceX
  849. end
  850. elseif (settings.lookingIn == 2)then
  851. if(settings.disForwards < 0)then
  852. settings.disLeft = -distanceZ
  853. else
  854. settings.disLeft = distanceZ
  855. end
  856. else
  857. if(settings.disForwards < 0)then
  858. settings.disLeft = distanceX
  859. else
  860. settings.disLeft = -distanceX
  861. end
  862. end
  863. print("Forwards: ", settings.disForwards)
  864. print("Upwards: ", settings.disUp)
  865. print("Leftwards: ", settings.disLeft)
  866. else
  867. print("Please give the distance for the tunnel")
  868. settings.disForwards = tonumber(io.read())
  869. end
  870. end
  871. function changePositionsInList(sideTurtleArray)
  872. local b = sideTurtleArray[0]
  873. sideTurtleArray[0] = sideTurtleArray[2]
  874. sideTurtleArray[2] = b
  875. b = sideTurtleArray[3]
  876. sideTurtleArray[3] = sideTurtleArray[4]
  877. sideTurtleArray[4] = b
  878. b = sideTurtleArray[5]
  879. sideTurtleArray[5] = sideTurtleArray[7]
  880. sideTurtleArray[7] = b
  881. end
  882.  
  883. --every action of the middle turtle, being also the controlling turtle
  884. function middleControl()
  885. local settings = {}
  886. settings["bMining"] = false
  887. settings["disForwards"] = 10
  888. settings["disLeft"] = -10
  889. settings["disUp"] = 0
  890. settings["ownCoordX"] = 0
  891. settings["ownCoordY"] = 0
  892. settings["ownCoordZ"] = 0
  893. settings["targetCoordX"] = 0
  894. settings["targetCoordY"] = 0
  895. settings["targetCoordZ"] = 0
  896. settings["lookingIn"] = 0
  897. -- bMining = settings.bMining
  898. print("To change the settings please enter 1")
  899. if(tonumber(io.read()) == 1)then
  900. changeSettings(settings)
  901. end
  902. local disForwards = settings.disForwards
  903. local disLeft = settings.disLeft
  904. local sideTurtleArray = {nil, nil, nil, nil, nil, nil, nil, nil}
  905. registerAllTurtles(sideTurtleArray)
  906. local posNames = {"topLeft", "topMiddle", "topRight", "middleLeft", "middleRight", "bottomLeft", "bottomMiddle", "bottomRight"}
  907. --local turtleList = giveTurtlesPosNames(sideTurtleArray, posNames)
  908. while(true)do
  909. local textMining
  910. if (bMining)then
  911. textMining = "mining"
  912. else
  913. textMining = "notMininig"
  914. end
  915. callAllTurtles(textMining, sideTurtleArray)
  916. break
  917. end
  918. local blocksMoved = 0
  919. while(disForwards ~= 0)do
  920. if (disForwards < 0)then
  921. turtle.turnLeft()
  922. turtle.turnLeft()
  923. callAllTurtles("turnAround", sideTurtleArray)
  924. getAllMessages(posNames, sideTurtleArray)
  925. changePositionsInList(sideTurtleArray)
  926. disForwards = -disForwards
  927. end
  928. turtleForward()
  929. callAllTurtles("moveForward", sideTurtleArray)
  930. getAllMessages(posNames, sideTurtleArray)
  931. disForwards = disForwards - 1
  932. blocksMoved = blocksMoved + 1
  933. if (blocksMoved % 50 == 0)then
  934. print(blocksMoved, " blocks moved, to continue press enter")
  935. io.read()
  936. end
  937. if (disForwards == 0)then
  938. if (settings.disLeft > 0)then
  939. disForwards = settings.disLeft - 1
  940. settings.disLeft = 0
  941. turtle.turnLeft()
  942. callAllTurtles("turnLeft", sideTurtleArray)
  943. turtleForward()
  944. getAllMessages(posNames, sideTurtleArray)
  945. elseif(settings.disLeft < 0)then
  946. disForwards = -settings.disLeft - 1
  947. settings.disLeft = 0
  948. turtle.turnRight()
  949. callAllTurtles("turnRight", sideTurtleArray)
  950. turtleForward()
  951. getAllMessages(posNames, sideTurtleArray)
  952. end
  953. end
  954. if(disForwards == 0)then
  955. callAllTurtles("finish", sideTurtleArray)
  956. break
  957. end
  958. end
  959. print("IDs: ", sideTurtleArray[0], sideTurtleArray[1], sideTurtleArray[2], sideTurtleArray[3], sideTurtleArray[4], sideTurtleArray[5], sideTurtleArray[6], sideTurtleArray[7])
  960. -- moveTurtleMiddleMiddle(false)
  961. end
  962.  
  963. --every action of all turtles except the middle one
  964. function sideTurtle()
  965. controleId = registerAndGetSenderId(turtlePosition)
  966. local bMining = ""
  967. while(true)do
  968. bMining = getMsgByConsoleTurtle()
  969. if (bMining == "mining")then
  970. bMining = true
  971. break
  972. elseif (bMining == "notMininig")then
  973. bMining = false
  974. break
  975. end
  976. end
  977. while(true)do
  978. local msg = getMsgByConsoleTurtle()
  979. if(msg == "moveForward") then
  980. local locData ={}
  981. if(turtlePosition == 1)then
  982. locData["rightDis"] = -2
  983. locData["highDis"] = 0
  984. locData["lookDir"] = 3
  985. moveTurtleLeftTop(bMining, locData)
  986. elseif(turtlePosition == 2)then
  987. locData["rightDis"] = -1
  988. locData["highDis"] = 0
  989. locData["lookDir"] = 0
  990. moveTurtleMiddleTop(bMining, locData)
  991. elseif(turtlePosition == 3)then
  992. locData["rightDis"] = 0
  993. locData["highDis"] = 0
  994. locData["lookDir"] = 1
  995. moveTurtleRightTop(bMining, locData)
  996. elseif(turtlePosition == 4)then
  997. locData["rightDis"] = -2
  998. locData["highDis"] = -1
  999. locData["lookDir"] = 3
  1000. moveTurtleLeftMiddle(bMining, locData)
  1001. elseif(turtlePosition == 6)then
  1002. locData["rightDis"] = 0
  1003. locData["highDis"] = -1
  1004. locData["lookDir"] = 1
  1005. moveTurtleRightMiddle(bMining, locData)
  1006. elseif(turtlePosition == 7)then
  1007. locData["rightDis"] = -2
  1008. locData["highDis"] = -2
  1009. locData["lookDir"] = 3
  1010. moveTurtleLeftBottom(bMining, locData)
  1011. elseif(turtlePosition == 8)then
  1012. locData["rightDis"] = -1
  1013. locData["highDis"] = -2
  1014. locData["lookDir"] = 0
  1015. moveTurtleMiddleBottom(bMining, locData)
  1016. elseif(turtlePosition == 9)then
  1017. locData["rightDis"] = 0
  1018. locData["highDis"] = -2
  1019. locData["lookDir"] = 1
  1020. moveTurtleRightBottom(bMining, locData)
  1021. end
  1022. callConTurtle("movedForwardCleared", "fullMovementReceived")
  1023. elseif(msg == "finish")then
  1024. print("Programm closed successfull!")
  1025. break
  1026. elseif(msg == "turnAround")then
  1027. turtle.turnLeft()
  1028. turtle.turnLeft()
  1029. if (turtlePosition % 3 == 1)then
  1030. turtlePosition = turtlePosition + 2
  1031. elseif (turtlePosition % 3 == 0)then
  1032. turtlePosition = turtlePosition - 2
  1033. end
  1034. callConTurtle("movedForwardCleared", "fullMovementReceived")
  1035. elseif(msg == "turnLeft")then
  1036. if(turtlePosition % 3 == 2)then
  1037. turtle.turnLeft()
  1038. turtleForward()
  1039. elseif(turtlePosition % 3 == 1)then
  1040. moveLeftTurtleTurningLeft()
  1041. else
  1042. moveRightTurtleTurningLeft(bMining)
  1043. end
  1044. callConTurtle("movedForwardCleared", "fullMovementReceived")
  1045. elseif(msg == "turnRight")then
  1046. if(turtlePosition % 3 == 2)then
  1047. turtle.turnRight()
  1048. turtleForward()
  1049. elseif(turtlePosition % 3 == 1)then
  1050. moveLeftTurtleTurningRight(bMining)
  1051. else
  1052. moveRightTurtleTurningRight()
  1053. end
  1054. callConTurtle("movedForwardCleared", "fullMovementReceived")
  1055. end
  1056. end
  1057. end
  1058.  
  1059. function main()
  1060. turtlePosition = getPositionOfTurtle()
  1061.  
  1062. if(turtlePosition == 5) then
  1063. middleControl()
  1064. else
  1065. sideTurtle()
  1066. end
  1067. end
  1068.  
  1069.  
  1070. main()
  1071. -- pastebin get WNLvCqqp sTunnel
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement