Advertisement
Seatrano

Untitled

Feb 16th, 2019
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.43 KB | None | 0 0
  1. function FirstScreen()
  2. term.clear()
  3. term.setCursorPos(1,1)
  4. write("My First Minining Program")
  5. sleep(0.5)
  6. end
  7.  
  8. function setVariables()
  9.  
  10. fuelLevel = 0
  11. x = 0
  12. z = 0
  13. y = 0
  14. f = 0
  15. DigX = 0
  16. DigZ = 0
  17. DigDepth = 0
  18. OriginX = 0
  19. OriginZ = 0
  20. OriginY = 0
  21. OriginF = 0
  22. GoBackToY = 0
  23. GoBackToX = 0
  24. GoBackToZ = 0
  25. GoBackToF = 0
  26. BlocksMined = 1
  27. BlocksToBeMined = 0
  28. BlocksMinedPercent = 0
  29. ErrorMovingCount = 0
  30. ComputerID = 0
  31. TurtleID = os.getComputerID()
  32. TurtleLabel = os.getComputerLabel()
  33.  
  34. end
  35.  
  36.  
  37. function ClearScreen()
  38. term.clear()
  39. term.setCursorPos(1,1)
  40. end
  41.  
  42. function AskForDimension()
  43. term.setCursorPos(1,1)
  44. write("Width?")
  45. term.setCursorPos(1,2)
  46. input = read()
  47. DigX = input
  48. ClearScreen()
  49.  
  50. term.setCursorPos(1,1)
  51. write("Length?")
  52. term.setCursorPos(1,2)
  53. input = read()
  54. DigZ = input
  55. ClearScreen()
  56.  
  57. --term.setCursorPos(1,1)
  58. --write("Depth?")
  59. --input = read()
  60. --DigDepth = input
  61. --ClearScreen()
  62.  
  63. term.setCursorPos(1,1)
  64. write("Please place unwanted blocks in slot 1 and slot 2")
  65. term.setCursorPos(1,3)
  66. write("Press any key to continue")
  67. input = read()
  68. end
  69.  
  70. function CalculatePercent()
  71. BlocksMinedPercent = tonumber(BlocksMined) / tonumber(BlocksToBeMined) * 100
  72. return BlocksMinedPercent
  73. end
  74.  
  75. function AskForCoordinate()
  76. ClearScreen()
  77.  
  78. term.setCursorPos(1,1)
  79. write("What is the ComputerID?")
  80. input = read()
  81. ComputerID = input
  82. ClearScreen()
  83.  
  84. term.setCursorPos(1,1)
  85. write("What is the x-Position of the Mining Turtle?")
  86. input = read()
  87. x = input
  88. ClearScreen()
  89.  
  90. term.setCursorPos(1,1)
  91. write("What is the z-Position of the Mining Turtle?")
  92. input = read()
  93. z = input
  94. ClearScreen()
  95.  
  96. term.setCursorPos(1,1)
  97. write("What is the y-Position of the Mining Turtle?")
  98. input = read()
  99. y = input
  100. ClearScreen()
  101.  
  102. term.setCursorPos(1,1)
  103. write("What is the direction of the Mining Turtle? (0 = south, 1 = west, 2 = north, 3 = east)")
  104. input = read()
  105. f = input
  106. ClearScreen()
  107.  
  108. term.setCursorPos(1,1)
  109. write("Is that right? (yes/no)")
  110.  
  111. term.setCursorPos(1,2)
  112. write("x: "..x.."")
  113.  
  114. term.setCursorPos(1,3)
  115. write("z: "..z.."")
  116.  
  117. term.setCursorPos(1,4)
  118. write("y: "..y.."")
  119.  
  120. term.setCursorPos(1,5)
  121. write("Cardinal direction: "..CardinalPoint(f).." ("..f..")")
  122.  
  123. term.setCursorPos(1,6)
  124.  
  125. input = read()
  126. if input == "no" then
  127. AskForCoordinate()
  128. else
  129. ClearScreen()
  130. write("Nice!")
  131. FirstMonitorScreen()
  132. end
  133.  
  134. OriginX = x
  135. OriginY = y
  136. OriginZ = z
  137. OriginF = f
  138.  
  139. end
  140.  
  141. function CardinalPoint(f)
  142. if tostring(f) == "0" then
  143. return "south"
  144. elseif tostring(f) == "1" then
  145. return "west"
  146. elseif tostring(f) == "2" then
  147. return "north"
  148. elseif tostring(f) == "3" then
  149. return "east"
  150. else
  151. return "INVALID"
  152. end
  153. end
  154.  
  155. function ClearMonitor()
  156. sleep(.1)
  157. rednet.send(tonumber(ComputerID), "clear()")
  158. sleep(.1)
  159. end
  160.  
  161. function OpenConnection()
  162. rednet.open("left")
  163. end
  164.  
  165. function DetermineBlocksToBeMined()
  166. BlocksToBeMined = tonumber(DigX) * tonumber(DigZ) * tonumber(OriginY)
  167. end
  168.  
  169. function UpdateMonitor()
  170.  
  171. message = ""
  172.  
  173. message = message.."SCP(13,2);"..getFuelLevel().." ;"
  174.  
  175. message = message.."SCP(4,3);"..x.." ;"
  176.  
  177. message = message.."SCP(4,4);"..z.." ;"
  178.  
  179. message = message.."SCP(4,5);"..y.." ;"
  180.  
  181. message = message.."SCP(21,6);"..CardinalPoint(f).." ("..f..") ;"
  182.  
  183. message = message.."SCP(21,7);"..BlocksToBeMined.." ;"
  184.  
  185. message = message.."SCP(15,8);"..BlocksMined..", "..BlocksMinedPercent.."% ;"
  186.  
  187. rednet.send(tonumber(ComputerID), message)
  188.  
  189. end
  190.  
  191. function FirstMonitorScreen()
  192.  
  193. -- ";" for the end of a message
  194. -- "_" for the next line, with ";" so "_;" !
  195.  
  196. ClearMonitor()
  197.  
  198. message = ""
  199.  
  200. message = message.."Name: "..TurtleLabel..", ID: "..TurtleID..";_;"
  201.  
  202. message = message.."Fuel Level: "..getFuelLevel()..";_;"
  203.  
  204. message = message.."x: "..x..";_;"
  205.  
  206. message = message.."z: "..z..";_;"
  207.  
  208. message = message.."y: "..y..";_;"
  209.  
  210. message = message.."Cardinal direction: "..CardinalPoint(f).." ("..f..");_;"
  211.  
  212. message = message.."Blocks to be mined: "..BlocksToBeMined..";_;"
  213.  
  214. message = message.."Blocks mined: "..BlocksMined..";_;"
  215.  
  216. rednet.send(tonumber(ComputerID), message)
  217.  
  218. end
  219.  
  220. function getFuelLevel()
  221. fuelLevel = turtle.getFuelLevel()
  222. return fuelLevel
  223. end
  224.  
  225. function MoveForward()
  226.  
  227.  
  228.  
  229. if turtle.forward() then
  230.  
  231. ErrorMovingCount = 0
  232.  
  233. if tonumber(f) == tonumber(0) then
  234. z = z + 1
  235. elseif tonumber(f) == tonumber(1) then
  236. x = x - 1
  237. elseif tonumber(f) == tonumber(2) then
  238. z = z - 1
  239. elseif tonumber(f) == tonumber(3) then
  240. x = x + 1
  241. end
  242.  
  243. else
  244. print("cannot move for: "..ErrorMovingCount)
  245. turtle.attack()
  246. MoveForward()
  247. ErrorMovingCount = ErrorMovingCount + 1
  248. end
  249.  
  250. UpdateMonitor()
  251.  
  252. end
  253.  
  254. function digDown()
  255. turtle.digDown()
  256. BlocksMined = BlocksMined + 1
  257. MoveDown()
  258. InventoryManagement()
  259. end
  260.  
  261. function digForward()
  262. turtle.dig()
  263. BlocksMined = BlocksMined + 1
  264. MoveForward()
  265. InventoryManagement()
  266. end
  267.  
  268. function digUp()
  269. turtle.digUp()
  270. BlocksMined = BlocksMined + 1
  271. MoveDown()
  272. InventoryManagement()
  273. end
  274.  
  275. function MoveUp()
  276. turtle.up()
  277. y = y + 1
  278. UpdateMonitor()
  279. end
  280.  
  281. function MoveDown()
  282. turtle.down()
  283. y = y - 1
  284. UpdateMonitor()
  285. end
  286.  
  287. function TurnRight()
  288.  
  289. turtle.turnRight()
  290. if tonumber(f) == tonumber(3) then
  291. f = 0
  292. else
  293. f = f + 1
  294. end
  295.  
  296. UpdateMonitor()
  297.  
  298. end
  299.  
  300. function TurnLeft()
  301.  
  302. turtle.turnLeft()
  303. if f == 0 then
  304. f = 3
  305. else
  306. f = f - 1
  307. end
  308.  
  309. UpdateMonitor()
  310.  
  311. end
  312.  
  313. function InventoryManagement()
  314.  
  315. local freeSlots = 0
  316.  
  317. if turtle.getItemCount(16) ~= 0 then
  318.  
  319. -- Drop unwanted blocks set in slot 1
  320. for i = 3, 16 do
  321. turtle.select(i)
  322. if turtle.compareTo(1) then
  323. turtle.drop()
  324. end
  325. end
  326.  
  327. -- Drop unwanted blocks set in slot 2
  328. for i = 3, 16 do
  329. turtle.select(i)
  330. if turtle.compareTo(2) then
  331. turtle.drop()
  332. end
  333. end
  334.  
  335. for i = 1, 2 do
  336. turtle.select(i)
  337. turtle.drop(tonumber(turtle.getItemCount((i)) - tonumber(1)))
  338. end
  339.  
  340. end
  341.  
  342. -- -- Rearrange the Inventory
  343. -- --Get number of free slots
  344. -- for i = 3, 16 do
  345. -- if tonumber(turtle.getItemCount(i)) == tonumber(0) then
  346. -- freeSlots = freeSlots + 1
  347. -- end
  348. -- end
  349.  
  350. -- turtle.select(16)
  351. -- for i = 3, 15 do
  352. -- if turtle.transferTo(i) then
  353. -- break
  354. -- end
  355. -- end
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362. -- setzen auf startposition
  363. turtle.select(1)
  364.  
  365.  
  366. if turtle.getItemCount(16) ~= 0 then
  367. GoToChest()
  368. EmptyTurtle()
  369. GoBackToMining()
  370. end
  371.  
  372.  
  373. end
  374.  
  375. function EmptyTurtle()
  376.  
  377. for o = 1, 2 do
  378. turtle.select(o)
  379. turtle.drop(tonumber(turtle.getItemCount(o)) - tonumber(1))
  380. end
  381.  
  382. for i = 3, 16 do
  383. turtle.select(i)
  384. turtle.drop()
  385. end
  386. turtle.select(1)
  387. end
  388.  
  389. function GoBackToMining()
  390.  
  391. print("X: "..GoBackToX.." Z: "..GoBackToZ)
  392.  
  393. TurnLeft()
  394. TurnLeft()
  395.  
  396. if tonumber(GoBackToZ) > 0 then
  397. for i = 1, GoBackToZ do
  398. MoveForward()
  399. end
  400. end
  401.  
  402. TurnRight()
  403.  
  404. if tonumber(GoBackToX) > 0 then
  405. for i = 1, GoBackToX do
  406. MoveForward()
  407. end
  408. end
  409.  
  410. if tonumber(GoBackToY) > 0 then
  411. for i = 1, GoBackToY do
  412. MoveDown()
  413. end
  414. end
  415.  
  416. while tonumber(f) ~= tonumber(GoBackToF) do
  417. TurnRight()
  418. end
  419.  
  420. GoBackToX = 0
  421. GoBackToY = 0
  422. GoBackToZ = 0
  423.  
  424. end
  425.  
  426. function OppositeDirection(Direction)
  427. if tonumber(Direction) == tonumber(0) then
  428. return "2"
  429. elseif tonumber(Direction) == tonumber(1) then
  430. return "3"
  431. elseif tonumber(Direction) == tonumber(2) then
  432. return "0"
  433. elseif tonumber(Direction) == tonumber(3) then
  434. return "1"
  435. end
  436. end
  437.  
  438. function GoToChest()
  439.  
  440. GoBackToF = f
  441.  
  442. -- Y-Richtung
  443. while tonumber(y) ~= tonumber(OriginY) do
  444. MoveUp()
  445. GoBackToY = GoBackToY + 1
  446. end
  447.  
  448.  
  449. -- X-Richtung
  450.  
  451.  
  452. if tonumber(x) ~= tonumber(OriginX) then
  453. --print("x: "..x.."; OriginalX"..OriginX.."f: "..f.."; OriginalF: "..OriginF)
  454. if tonumber(x) <= tonumber(OriginX) then
  455.  
  456. while tonumber(f) ~= tonumber(3) do
  457. TurnRight()
  458. end
  459. elseif tonumber(x) >= tonumber(OriginX) then
  460.  
  461. while tonumber(f) ~= tonumber(1) do
  462. TurnRight()
  463. end
  464. end
  465. end
  466.  
  467. while tonumber(x) ~= tonumber(OriginX) do
  468. MoveForward()
  469. GoBackToX = GoBackToX + 1
  470. -- print("x: "..x.."; OriginalX"..OriginX.."f: "..f.."; OriginalF: "..OriginF)
  471. end
  472.  
  473.  
  474. -- Z-Richtung
  475.  
  476. if tonumber(z) ~= tonumber(OriginZ) then
  477. -- print("x: "..z.."; OriginalX"..OriginZ.."f: "..f.."; OriginalF: "..OriginF)
  478. if tonumber(z) <= tonumber(OriginZ) then
  479.  
  480. while tonumber(f) ~= tonumber(0) do
  481. TurnRight()
  482. end
  483. elseif tonumber(z) >= tonumber(OriginZ) then
  484.  
  485. while tonumber(f) ~= tonumber(2) do
  486. TurnRight()
  487. end
  488. end
  489. end
  490.  
  491. while tonumber(z) ~= tonumber(OriginZ) do
  492. MoveForward()
  493. GoBackToZ = GoBackToZ + 1
  494. -- print("x: "..z.."; OriginalX"..OriginZ.."f: "..f.."; OriginalF: "..OriginF)
  495. end
  496.  
  497. -- print("f: "..f.."Opposite: "..OppositeDirection(OriginF))
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504. while tonumber(f) ~= tonumber(OppositeDirection(OriginF)) do
  505.  
  506. TurnLeft()
  507.  
  508. print("f: "..f.."Opposite: "..OppositeDirection(OriginF))
  509. end
  510.  
  511. end
  512.  
  513. function CurveRight()
  514. TurnRight()
  515. digForward()
  516. TurnRight()
  517. end
  518.  
  519. function CurveLeft()
  520. TurnLeft()
  521. digForward()
  522. TurnLeft()
  523. end
  524.  
  525. function ExcavateOneLayer()
  526.  
  527. CurveDirection = "right"
  528.  
  529. for o = 2, DigZ do
  530. for i = 2, DigX do
  531. digForward()
  532. end
  533.  
  534. if CurveDirection == "right" then
  535. CurveRight()
  536. CurveDirection = "left"
  537. elseif CurveDirection == "left" then
  538. CurveLeft()
  539. CurveDirection = "right"
  540. end
  541.  
  542. end
  543.  
  544. for i = 2, DigX do
  545. digForward()
  546. end
  547.  
  548.  
  549.  
  550. if CurveDirection == "right" then
  551. TurnLeft()
  552. for p = 2, DigZ do
  553. MoveForward()
  554. end
  555. TurnLeft()
  556.  
  557. for p = 2, DigX do
  558. MoveForward()
  559. end
  560. TurnLeft()
  561. TurnLeft()
  562.  
  563. elseif CurveDirection == "left" then
  564. TurnRight()
  565.  
  566. for p = 2, DigZ do
  567. MoveForward()
  568. end
  569. TurnRight()
  570. end
  571.  
  572.  
  573. end
  574.  
  575. function DigToBedrock()
  576.  
  577. while true do
  578.  
  579. if tonumber(y) == tonumber(1) then
  580. break
  581. end
  582.  
  583. ExcavateOneLayer()
  584.  
  585. digDown()
  586. end
  587.  
  588. ExcavateOneLayer()
  589. GoToChest()
  590. EmptyTurtle()
  591. end
  592.  
  593.  
  594.  
  595.  
  596.  
  597. setVariables()
  598. OpenConnection()
  599. FirstScreen()
  600. AskForCoordinate()
  601. getFuelLevel()
  602. AskForDimension()
  603. DetermineBlocksToBeMined()
  604. FirstMonitorScreen()
  605. DigToBedrock()
  606.  
  607.  
  608.  
  609.  
  610. write("finished!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement