remie92

MainTurtle

Aug 4th, 2025 (edited)
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. serverProtocol="turtleServer"
  2. serverId=0
  3. salt = require("salt")
  4. print("Starting turtle!")
  5.  
  6. posX,posY,posZ=gps.locate()
  7.  
  8. function locate()
  9. posX,posY,poZ=gps.locate()
  10. end
  11.  
  12. for i=1,5 do
  13. for i=16,1,-1 do
  14. slot=turtle.getItemDetail(i)
  15.  
  16. if slot~=nil then
  17. if(slot.name=="computercraft:wireless_modem_advanced") then
  18. print("Equiping modem")
  19. turtle.select(i)
  20. turtle.equipLeft()
  21. break
  22. end
  23. if(slot.name=="minecraft:diamond_pickaxe") then
  24. print("Equiping Pickaxe")
  25. turtle.select(i)
  26. turtle.equipRight()
  27. slot=turtle.getItemDetail(i)
  28. if slot~=nil then
  29. if(slot.name=="advancedperipherals:geo_scanner") then
  30. print("Transfering Geo Scanner!")
  31. turtle.select(i)
  32. turtle.transferTo(16,1)
  33. break
  34. end
  35. end
  36. break
  37. end
  38. if(slot.name=="advancedperipherals:geo_scanner" and i~=16) then
  39. print("Transfering Geo Scanner!")
  40. turtle.select(i)
  41. turtle.transferTo(16,1)
  42. break
  43. end
  44. end
  45. end
  46. end
  47.  
  48.  
  49.  
  50.  
  51.  
  52. peripheral.find("modem", rednet.open)
  53.  
  54. while rednet.lookup(serverProtocol)==nil do
  55. sleep(1)
  56. print("Couldn't find main server. Retrying again soon!")
  57. end
  58. serverId=rednet.lookup(serverProtocol)
  59. print("Found the main server! id:",serverId)
  60.  
  61. rednet.send(serverId,"startup",serverProtocol)
  62.  
  63.  
  64. currentAction="Starting Up"
  65.  
  66. function main()
  67. currentAction="Idle"
  68. scanWorld()
  69. parallel.waitForAny(checkRednet)
  70. print("Program broke :(")
  71. end
  72.  
  73.  
  74. function scanWorld()
  75. turtle.select(16)
  76. turtle.equipRight()
  77. geo=peripheral.wrap("right")
  78. scan=geo.scan(8)
  79. salt.save(scan,"scan1")
  80. locate()
  81. for i,v in ipairs(scan) do
  82. scan.x=scan.x+posX
  83. scan.y=scan.y+posX
  84. scan.z=scan.z+posX
  85. end
  86. salt.save(scan,"scan2")
  87. print(textutils.serialize(scan))
  88. response={action=currentAction,x=posX,y=posY,z=posZ,fuelLevel=turtle.getFuelLevel()}
  89. messageBack="worldScan$"..textutils.serialize(response)
  90. messageBack = messageBack:gsub("\n", "")
  91. rednet.send(serverId,messageBack,serverProtocol)
  92. rednet.send(serverId,)
  93. end
  94.  
  95. function splitString(inputstr, sep)
  96. if sep == nil then
  97. sep = "%s"
  98. end
  99. i=1
  100. local t = {}
  101. for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  102. t[i]=str
  103. end
  104. return t
  105. end
  106.  
  107. function checkRednet()
  108. while true do
  109. local event, sender, message, protocol = os.pullEvent("rednet_message")
  110. if protocol ~= nil then
  111. --print("Received message from " .. sender .. " with protocol " .. protocol .. " and message " .. tostring(message))
  112. if protocol==serverProtocol then
  113. --print("Received message from " .. sender .. " with protocol " .. protocol .. " and message " .. tostring(message))
  114. if message=="status" then
  115. posX,posY,posZ=gps.locate()
  116. response={action=currentAction,x=posX,y=posY,z=posZ,fuelLevel=turtle.getFuelLevel()}
  117. messageBack="responseStatus$"..textutils.serialize(response)
  118. messageBack = messageBack:gsub("\n", "")
  119. rednet.send(serverId,messageBack,serverProtocol)
  120. elseif message=="rebootAll" then
  121. shell.run("reboot")
  122. end
  123.  
  124. end
  125. else
  126. --print("Received message from " .. sender .. " with message " .. tostring(message))
  127. end
  128. end
  129. end
  130.  
  131.  
  132.  
  133. main()
Advertisement
Add Comment
Please, Sign In to add comment