Advertisement
JakobM7

Computercraft Turtle place Windmill

May 25th, 2023 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.07 KB | Gaming | 0 0
  1. local forwardStep = 0
  2. local continue = true
  3. local goHome = false
  4. local goBack = false
  5. local atHome = true
  6.  
  7.  
  8. --functions for Preparing Work--
  9. function refuel()
  10. turtle.select(16)
  11. turtle.refuel()
  12. turtle.select(1)
  13. end
  14.  
  15. function getWindmills()
  16. for i=1,14 do
  17. turtle.select(i)
  18. if turtle.getItemCount() == 0 then
  19. turtle.suck(1)
  20. end
  21. end
  22. turtle.select(15)
  23. end
  24.  
  25. function getCablesAndBlocks()
  26. turtle.turnRight()
  27. turtle.select(15)
  28. if turtle.getItemCount() >= 1 then
  29. if turtle.getItemDetail().name == "mekanism:transmitter" then
  30. turtle.suck(turtle.getItemSpace())
  31. else
  32. os.exit()
  33. end
  34. else turtle.suck(turtle.getItemSpace())
  35. end
  36. turtle.turnLeft()
  37. turtle.turnLeft()
  38. turtle.select(16)
  39. if turtle.getItemCount() >= 1 then
  40. if turtle.getItemDetail().name == "minecraft:quartz_block" then
  41. turtle.suck(turtle.getItemSpace())
  42. else
  43. os.exit()
  44. end
  45. else turtle.suck(turtle.getItemSpace())
  46. end
  47. turtle.turnRight()
  48. end
  49.  
  50. --Functions for Work
  51.  
  52. function goToStart()
  53. turtle.down()
  54. turtle.down()
  55. turtle.down()
  56. while not turtle.detect() do
  57. turtle.forward()
  58. end
  59. while not turtle.detectDown() do
  60. turtle.down()
  61. end
  62. turtle.forward()
  63. turtle.forward()
  64. turtle.forward()
  65. turtle.select(16)
  66. if not turtle.detectUp() then
  67. turtle.placeUp()
  68. end
  69. end
  70.  
  71. function goBackInLine()
  72. turtle.turnLeft()
  73. turtle.turnLeft()
  74. while not turtle.detectUp() do
  75. turtle.forward()
  76. end
  77. end
  78.  
  79. function returnToHome()
  80. goBackInLine()
  81. if turtle.detect() then
  82. local atReturnPoint = false
  83. turtle.turnLeft()
  84.  
  85. while not atReturnPoint do
  86. turtle.forward()
  87. if not turtle.detectUp() then
  88. turtle.forward()
  89. else
  90. turtle.turnRight()
  91. if turtle.detect() then
  92. atReturnPoint = false
  93. turtle.turnLeft()
  94. else
  95. atReturnPoint = true
  96. end
  97.  
  98. end
  99. end
  100. turtle.forward()
  101. turtle.forward()
  102. turtle.forward()
  103. while not turtle.detectUp() do turtle.up() end
  104. while not turtle.detect() do turtle.forward() end
  105. turtle.up()
  106. turtle.up()
  107. turtle.up()
  108. turtle.turnLeft()
  109. turtle.turnLeft()
  110. end
  111. goHome = false
  112. end
  113.  
  114.  
  115.  
  116. local function getCurrentTurbineSlot()
  117. for i=1,14 do
  118. turtle.select(i)
  119. if turtle.getItemCount() >= 1 then
  120. return i
  121. end
  122. end
  123. return 16
  124. end
  125.  
  126. local function checkIfSufficientLeft()
  127. if getCurrentTurbineSlot() >= 15 then
  128. write("Not enough Wind generators. \nGoing to return Home\n")
  129. goHome = true
  130. return false
  131. else
  132. write("Enough Wind generators available.\n")
  133. goHome = false
  134. return true
  135. end
  136. end
  137.  
  138. function placeTurbines()
  139. turtle.down()
  140. turtle.down()
  141. turtle.turnRight()
  142. turtle.select(16)
  143. turtle.place()
  144. turtle.turnLeft()
  145. turtle.turnLeft()
  146. turtle.place()
  147. turtle.up()
  148. turtle.select(getCurrentTurbineSlot())
  149. turtle.place()
  150. turtle.turnRight()
  151. turtle.turnRight()
  152. turtle.select(getCurrentTurbineSlot())
  153. turtle.place()
  154. turtle.turnLeft()
  155. turtle.up()
  156. turtle.select(15)
  157. turtle.placeDown()
  158. end
  159.  
  160.  
  161. function toNextStop()
  162. for i=1,4 do
  163. if not turtle.detectDown() then
  164. turtle.select(15)
  165. turtle.placeDown()
  166. end
  167. turtle.forward()
  168. end
  169. end
  170.  
  171. function doLine()
  172. toNextStop()
  173. forwardStep = 1
  174. while forwardStep <=21 and not goHome do
  175. if not turtle.detectDown() then
  176. placeTurbines()
  177. end
  178. checkIfSufficientLeft()
  179. if not goHome then
  180. toNextStop()
  181. end
  182. forwardStep = forwardStep + 1
  183. end
  184. if forwardStep > 21 then
  185. goBack = true
  186. end
  187.  
  188. end
  189.  
  190. function operateNormal()
  191. write("Starting Operation\n")
  192. checkIfSufficientLeft() --sets goHome to true if not enough Windmills left
  193. if not goHome and not goBack then
  194. write("sufficient Windmills left\n")
  195. -- Checking if active Line is to be used (defined by block)
  196. if not turtle.detect() and not turtle.detectUp() then --if block is detected means line is closed
  197. write("line is not yet started\n")
  198. --Placing block left and right
  199. turtle.turnLeft()
  200. turtle.select(16)
  201. turtle.place()
  202. turtle.turnRight()
  203. turtle.turnRight()
  204. turtle.place()
  205. turtle.turnLeft()
  206.  
  207. doLine()
  208.  
  209. elseif not turtle.detect() and turtle.detectUp() then
  210. write("this line is already active. continuing\n")
  211. doLine()
  212.  
  213. else
  214. write("this line is done. looking for other line\n")
  215. --Turtle goes to furthest Station Left until hitting Block
  216. turtle.turnLeft()
  217. while not turtle.detect() do
  218. toNextStop()
  219. end
  220. write("Found End. Checking if Active line\n")
  221. turtle.turnRight()
  222. if turtle.detect() then
  223. write("Line not active. Starting new Line. \n Building new Station\n")
  224. turtle.turnLeft()
  225. --Turtle makes new Station (Station = point from which line goes)
  226. turtle.dig()
  227. toNextStop()
  228. turtle.select(16)
  229. turtle.placeUp()
  230. turtle.place()
  231. turtle.turnLeft()
  232. turtle.place()
  233. turtle.turnRight()
  234. turtle.turnRight()
  235. --Turtle does new line
  236. doLine()
  237. else
  238. write("Active Line detected. Continuing\n")
  239. doLine()
  240. end
  241. end
  242. elseif goBack then
  243. write("going back to start of Lane,\n")
  244. goBackInLine()
  245. turtle.turnRight()
  246. turtle.turnRight()
  247. turtle.select(16)
  248. turtle.place()
  249. goBack = false
  250. elseif goHome then
  251. write("going Home.\n")
  252. returnToHome()
  253. getCablesAndBlocks()
  254. getWindmills()
  255. atHome = true
  256. continue = not redstone.getInput("back")
  257. if continue then
  258. goToStart()
  259. atHome = false
  260. end
  261.  
  262. end
  263. end
  264.  
  265. --Calls for preparing work--
  266.  
  267. --Optional:
  268. --refuel()
  269.  
  270.  
  271.  
  272. --Calls for Starting to Work
  273.  
  274. while true do
  275. if atHome then
  276. continue = not redstone.getInput("back")
  277. if continue then
  278. getCablesAndBlocks()
  279. getWindmills()
  280. goToStart()
  281. end
  282. else
  283. continue = true
  284. end
  285. while continue do
  286. operateNormal()
  287. end
  288. end
  289.  
  290.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement