Advertisement
sviridovt

Untitled

Jun 29th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. local x
  2. local y
  3. local z
  4. local r
  5.  
  6. local minFuel = 150
  7.  
  8. local mx = 9
  9. local mz = 5
  10.  
  11. function readNumber(inputFile)
  12.  
  13. local returnVal
  14. local nextLine = inputFile.readLine()
  15. if (nextLine ~= nil) then
  16. returnVal = tonumber(nextLine)
  17. end
  18.  
  19. return returnVal
  20. end
  21.  
  22. function updateCoords()
  23. local fl = fs.open("coords", "w")
  24. fl.write(x)
  25. fl.write("\n")
  26. fl.write(y)
  27. fl.write("\n")
  28. fl.write(z)
  29. fl.write("\n")
  30. fl.write(r)
  31. fl.close()
  32. end
  33.  
  34. function turnRight()
  35. turtle.turnRight()
  36. r = r + 1
  37. if r == 4 then r = r - 4 end
  38. updateCoords()
  39. end
  40.  
  41. function turnLeft()
  42. turtle.turnLeft()
  43. r = r - 1
  44. if r == -1 then r = r + 4 end
  45. updateCoords()
  46. end
  47.  
  48. function updateForward()
  49. if r == 0 then
  50. z = z + 1
  51. elseif r == 1 then
  52. x = x - 1
  53. elseif r == 2 then
  54. z = z - 1
  55. elseif r == 3 then
  56. x = x + 1
  57. end
  58. updateCoords()
  59. end
  60.  
  61. function treeFeller()
  62. if not turtle.forward() then
  63. turtle.select(1)
  64. if turtle.compare() then
  65. turtle.dig()
  66. turtle.forward()
  67. updateForward()
  68. turtle.digDown()
  69. while turtle.digUp() do
  70. turtle.up()
  71. y = y + 1
  72. updateCoords()
  73. end
  74. while y > 0 do
  75. turtle.digDown()
  76. turtle.down()
  77. y = y - 1
  78. updateCoords()
  79. end
  80. else
  81. turtle.dig()
  82. turtle.forward()
  83. updateForward()
  84. end
  85. else
  86. updateForward()
  87. end
  88. turtle.select(16)
  89. turtle.suckDown()
  90. turtle.placeDown()
  91. end
  92.  
  93. function dropWood()
  94. for i = 2, 15, 1 do
  95. turtle.select(i)
  96. turtle.dropDown()
  97. end
  98. end
  99.  
  100. function getCoords()
  101. local f = fs.open("coords", "r")
  102. if f ~= nil then
  103. x = readNumber(f)
  104. y = readNumber(f)
  105. z = readNumber(f)
  106. r = readNumber(f)
  107. fl.close()
  108. else
  109. x = 0
  110. y = 0
  111. z = 0
  112. r = 0
  113. updateCoords()
  114. end
  115. end
  116.  
  117. function returnHome()
  118.  
  119. while r ~= 1 do turnLeft() end
  120. while x > 0 do treeFeller() end
  121. while r ~= 2 do turnRight() end
  122. while z > 0 do treeFeller() end
  123. end
  124.  
  125. -- main
  126.  
  127. getCoords()
  128. returnHome()
  129. dropWood()
  130. while true do
  131. while turtle.getFuelLevel() < minFuel do
  132. print("Waiting for fuel in slot 1")
  133. turtle.select(1)
  134. turtle.refuel()
  135. end
  136. turnRight()
  137. while z < mz do
  138. while x < mx do
  139. treeFeller()
  140. end
  141. turnRight()
  142. treeFeller()
  143. turnRight()
  144. while x > 0 do
  145. treeFeller()
  146. end
  147. if z == mz then
  148. break
  149. else
  150. turnLeft()
  151. treeFeller()
  152. turnLeft()
  153. end
  154. end
  155. returnHome()
  156. dropWood()
  157. sleep(120)
  158. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement