remie92

Turtle Startup

Aug 4th, 2025 (edited)
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1.  
  2. serverProtocol="update"
  3. serverId=0
  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.  
  60. function runProgram()
  61. shell.run("mainTurtle.lua")
  62. end
  63.  
  64.  
  65. lastMessage=os.clock()
  66. isWaiting=false
  67.  
  68.  
  69. function resetIfIdle()
  70. sleep(5)
  71. while isWaiting==false or lastMessage+20<os.clock() do
  72. sleep(2)
  73. end
  74. print("DIDNT RECEIVE CODE, RESENDING")
  75. end
  76.  
  77. function getCode()
  78. while true do
  79.  
  80. lastMessage=os.clock()
  81. isWaiting=true
  82. rednet.send(serverId,"mainTurtle.lua",serverProtocol)
  83. local event, sender, message, protocol = os.pullEvent("rednet_message")
  84. isWaiting=false
  85. if protocol ~= nil then
  86. -- print("Received message from " .. sender .. " with protocol " .. protocol .. " and message " .. tostring(message))
  87. if protocol==serverProtocol then
  88. lastMessage=os.clock()
  89.  
  90. shell.run("rm mainTurtle.lua")
  91. write_string_to_file("mainTurtle.lua", message)
  92. success,err=pcall(runProgram)
  93. if success==false then
  94. print(err)
  95.  
  96. end
  97. print("Restarting soon!")
  98.  
  99. sleep(10)
  100. lastMessage=os.clock()
  101.  
  102.  
  103. end
  104. end
  105. end
  106. end
  107. function write_string_to_file(filename, content)
  108. local file = assert(io.open(filename, "w")) -- open in write mode (overwrite)
  109. file:write(content)
  110. file:close()
  111. end
  112.  
  113. while true do
  114. parallel.waitForAny(resetIfIdle,getCode)
  115. sleep(0)
  116. end
Advertisement
Add Comment
Please, Sign In to add comment