Susceptance

turtle_start

May 6th, 2022 (edited)
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.60 KB | None | 0 0
  1. dirTable = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}}
  2.  
  3. posX = 0
  4. posY = 0
  5. posZ = 0
  6.  
  7. dir = 1
  8.  
  9. fuel = 0
  10.  
  11. protectedBlocks = {'computercraft:turtle_normal', 'computercraft:turtle_advanced'}
  12. protectedChunks = {}
  13. nodes = {}
  14.  
  15. function GetPosition()
  16. x, y, z = gps.locate()
  17. if x ~= nil then --Recieved our position
  18. posX, posY, posZ = x, y, z
  19. return true
  20. end
  21.  
  22. return false
  23. end
  24.  
  25. function UpdatePChunkLoop()
  26. rednet.open('left')
  27. while true do
  28. sender,message,distance = rednet.receive("V_PC")
  29. protectedChunks = textutils.unserialise(message)
  30. end
  31. end
  32.  
  33. local function has_value (tab, val)
  34. for index, value in ipairs(tab) do
  35. if value == val then
  36. return true
  37. end
  38. end
  39.  
  40. return false
  41. end
  42.  
  43. function IsTurtle(name)
  44. if name == protectedBlocks[1] or name == protectedBlocks[2] then
  45. return true
  46. end
  47.  
  48. return false
  49. end
  50.  
  51. function IsInProtectedChunk()
  52. chunkX = math.floor(posX/16)
  53. chunkZ = math.floor(posZ/16)
  54.  
  55. if has_value(protectedChunks, chunkX..','..chunkZ) then
  56. return true
  57. end
  58.  
  59. return false
  60. end
  61.  
  62. function ifProtected(data, ignorePChunk)
  63. -- Check if block is turtle
  64. if IsTurtle(data.name) then
  65. return true
  66. end
  67.  
  68. if ignorePChunk then
  69. return false
  70. end
  71.  
  72. return IsInProtectedChunk()
  73. end
  74.  
  75. function DirToCoords(direction)
  76. x = dirTable[direction][1]
  77. z = dirTable[direction][2]
  78.  
  79. return x, z
  80. end
  81.  
  82. function IncrementDir(amount)
  83. if dir + amount > 4 then
  84. dir = 1
  85. elseif dir + amount < 1 then
  86. dir = 4
  87. end
  88. end
  89.  
  90. function Forward(ignorePChunk)
  91. success, data = turtle.inspect()
  92. if ifProtected(data, ignorePChunk) ~= true then
  93. if success then
  94. turtle.dig('right')
  95. end
  96. if turtle.forward() then
  97. x, z = DirToCoords(dir)
  98. posX = posX + x
  99. posZ = posZ + z
  100. return true
  101. end
  102. end
  103. return false
  104. end
  105.  
  106. function Up(ignorePChunk)
  107. success, data = turtle.inspect()
  108. if ifProtected(data, ignorePChunk) ~= true then
  109. if success then
  110. turtle.digUp('right')
  111. end
  112. if turtle.up() then
  113. posY = posY + 1
  114. return true
  115. end
  116. end
  117. return false
  118. end
  119.  
  120. function Down(ignorePChunk)
  121. success, data = turtle.inspect()
  122. if ifProtected(data, ignorePChunk) ~= true then
  123. if success then
  124. turtle.digDown('right')
  125. end
  126. if turtle.down() then
  127. posY = posY - 1
  128. return true
  129. end
  130. end
  131. return false
  132. end
  133.  
  134. function ToWorldHeight()
  135. while true do
  136. success, data = turtle.inspectUp()
  137. if success then -- If a block above
  138. while true do
  139. success, data = turtle.inspectUp()
  140. if IsTurtle(data.name) == false then
  141. break
  142. end
  143. Forward(true) -- Otherwise go forward and try again
  144. end
  145.  
  146. turtle.digUp()
  147. end
  148.  
  149. if turtle.up() == false then --Reached height limit
  150. break
  151. end
  152. end
  153. end
  154.  
  155. function OutOfFuelCall()
  156. while true do
  157. if fuel <= 1 then
  158. rednet.broadcast("OUT OF FUEL X"..posX..', Y'..posY..', Z'..posZ, "V_M")
  159. fuel = turtle.getFuelLevel()
  160. end
  161. sleep(60)
  162. end
  163. end
  164.  
  165. function DownUntil()
  166. while true do
  167. success, data = turtle.inspectDown()
  168.  
  169. if success then
  170. if IsTurtle(data.name) then
  171. sleep(1)
  172. else
  173. break -- We reached a block that is not a tutel
  174. end
  175. else
  176. if turtle.down() then
  177. posY = posY - 1
  178. end
  179. end
  180. end
  181. end
  182.  
  183. function Refuel()
  184. for i=1, 16 do
  185. turtle.select(i)
  186. turtle.refuel()
  187. if turtle.getFuelLevel() == turtle.getFuelLimit() then
  188. break
  189. end
  190. end
  191. fuel = turtle.getFuelLevel()
  192.  
  193. turtle.select(1)
  194. end
  195.  
  196. function LeaveProtectedChunk()
  197. ToWorldHeight()
  198. while true do
  199. Forward(true)
  200. if IsInProtectedChunk() == false then
  201. break
  202. end
  203. end
  204. DownUntil()
  205. end
  206.  
  207. function Dump()
  208. for i=1, 16 do
  209. turtle.select(i)
  210. turtle.dropDown()
  211. end
  212. turtle.select(1)
  213. end
  214.  
  215. function ToNode()
  216. print('Moving to node...')
  217. -- Face south
  218. print('Facing south...')
  219. while true do
  220. if dir == 1 then
  221. break
  222. end
  223. turtle.turnRight()
  224. IncrementDir(1)
  225. end
  226.  
  227. -- Go to height limit
  228. print('Moving to world height...')
  229. ToWorldHeight()
  230.  
  231. -- Get nodes
  232. rednet.broadcast("Node request", "V_RN")
  233.  
  234. nodes = {}
  235. while true do
  236. sender,message,distance = rednet.receive("V_N", 2)
  237. if message == nil then
  238. break
  239. end
  240. print('Recieved a node')
  241. table.insert(nodes, {textutils.unserialise(message), distance})
  242. end
  243.  
  244. -- Choose nearest node
  245. closestNode = {0,0}
  246. closestDist = 99999999
  247. for i=1,#nodes do
  248. nX = nodes[i][1][1]
  249. nZ = nodes[i][1][2]
  250. nDist = math.sqrt(math.pow(nX - posX, 2) + math.pow(nZ - posZ, 2))
  251. if nDist < closestDist then
  252. closestNode = nodes[i][1]
  253. closestDist = nDist
  254. end
  255. end
  256.  
  257. print('Moving to node at '..closestNode[1]..', '..closestNode[2])
  258.  
  259. -- Go to node
  260. if closestNode[1] > posX then
  261. turtle.turnLeft()
  262. IncrementDir(-1)
  263. while posX ~= closestNode[1] do
  264. Forward(true)
  265. end
  266. turtle.turnRight()
  267. IncrementDir(1)
  268. else
  269. turtle.turnRight()
  270. IncrementDir(1)
  271. while posX ~= closestNode[1] do
  272. Forward(true)
  273. end
  274. turtle.turnLeft()
  275. IncrementDir(-1)
  276. end
  277. print('Arrived at node X')
  278.  
  279. if closestNode[2] > posZ then
  280. while posZ ~= closestNode[2] do
  281. Forward(true)
  282. end
  283. else
  284. turtle.turnLeft()
  285. turtle.turnLeft()
  286. IncrementDir(-2)
  287. while posZ ~= closestNode[2] do
  288. Forward(true)
  289. end
  290. turtle.turnRight()
  291. turtle.turnRight()
  292. IncrementDir(2)
  293. end
  294. print('Arrived at node Z')
  295.  
  296. -- Go down until block is reached
  297. DownUntil()
  298.  
  299. -- Recheck that we have arrived at a node
  300. while GetPosition() == false do
  301. rednet.broadcast("COULD NOT RESOLVE POSITION X"..posX..', Y'..posY..', Z'..posZ, "V_M")
  302. sleep(60)
  303. end
  304.  
  305. if posX == closestNode[1] and posZ == closestNode[2] then
  306. -- Dump inventory into node
  307. Dump()
  308.  
  309. -- Refuel
  310. turtle.select(1)
  311. while turtle.getFuelLevel() ~= turtle.getFuelLimit() do
  312. if turtle.suck(64) == false then
  313. sleep(5)
  314. end
  315. turtle.refuel(64)
  316. end
  317.  
  318. -- Dump exess fuel
  319. Dump()
  320.  
  321. -- Leave node area
  322. turtle.turnRight()
  323. turtle.forward()
  324. turtle.turnLeft()
  325. LeaveProtectedChunk()
  326. else
  327. rednet.broadcast("FAILED TO REACH NODE X"..closestNode[1]..', Z'..closestNode[2], "V_M")
  328. ToNode()
  329. end
  330. end
  331.  
  332. function RunCommands()
  333. while true do
  334. sender,message,distance = rednet.receive("V_CT")
  335. if message == 'pos' then
  336. rednet.send(sender, 'pos '..posX..' '..posY..' '..posZ, "V_M")
  337. elseif message == 'upd' then
  338. shell.run('update.lua')
  339. end
  340. end
  341. end
  342.  
  343. function Main()
  344. if IsInProtectedChunk() then
  345. print('Inside protected chunk!')
  346. LeaveProtectedChunk()
  347. end
  348.  
  349. lastRot = 0
  350. lastV = 0
  351. while true do
  352. fuel = turtle.getFuelLevel()
  353.  
  354. if fuel < 5000 or turtle.getItemCount(16) > 0 then
  355. ToNode()
  356. end
  357.  
  358. while fuel <= 1 do
  359. rednet.broadcast("OUT OF FUEL X"..posX..', Y'..posY..', Z'..posZ, "V_M")
  360. fuel = turtle.getFuelLevel()
  361. sleep(60)
  362. end
  363.  
  364. rand = math.random(5)
  365.  
  366. if rand == 1 and lastRot ~= -1 then
  367. turtle.turnRight()
  368. IncrementDir(1)
  369. Forward(false)
  370.  
  371. lastRot = 1
  372. lastV = 0
  373. elseif rand == 2 and lastRot ~= 1 then
  374. turtle.turnLeft()
  375. IncrementDir(-1)
  376. Forward(false)
  377.  
  378. lastRot = -1
  379. lastV = 0
  380. elseif rand == 3 then
  381. Forward(false)
  382. lastV = 0
  383. elseif rand == 4 and lastV ~= -1 then
  384. Up(false)
  385. lastV = 1
  386. elseif lastV ~= 1 then
  387. Down(false)
  388. lastV = -1
  389. end
  390. end
  391. end
  392.  
  393. print('Add fuel and equip tools')
  394. print('Ensure Turtle is near gps node')
  395. print('Ensure Turtle is facing south')
  396.  
  397. print('Initializing Turtle...')
  398. Refuel()
  399. turtle.select(1)
  400. print('Fuel: '..turtle.getFuelLevel())
  401.  
  402. while GetPosition() == false do
  403. print('Could not resolve position. Ensure turtle is near functional GPS node')
  404. sleep(30)
  405. end
  406. print('Received position X'..posX..' Y'..posY..' Z'..posZ)
  407.  
  408. rednet.open('left')
  409. rednet.broadcast('V_RPC', 'V_RPC')
  410. sender,message,distance = rednet.receive("V_PC")
  411. rednet.close()
  412. protectedChunks = textutils.unserialise(message)
  413. print('Received '..#protectedChunks..' protected chunks')
  414.  
  415. print('Initialization complete! m a y t h e v o i d e m b r a c e y o u')
  416.  
  417. parallel.waitForAll(UpdatePChunkLoop, Main, OutOfFuelCall, RunCommands)
Add Comment
Please, Sign In to add comment