Susceptance

node_start

May 7th, 2022 (edited)
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. print('Ensure Node is near a gps node')
  2. print('Ensure Node is equipped with an Ender Modem on the top')
  3. print('Press Enter once finished')
  4. read()
  5.  
  6. posX = 0
  7. posY = 0
  8. posZ = 0
  9.  
  10. chunkX = 0
  11. chunkZ = 0
  12.  
  13. protectedChunks = {}
  14. nodes = {}
  15.  
  16. print(posZ)
  17. print('Initializing Node...')
  18. posX, posY, posZ = gps.locate()
  19. print('Received position: X'..posX..' Y'..posY..' Z'..posZ)
  20.  
  21. chunkX = math.floor(posX/16)
  22. chunkZ = math.floor(posZ/16)
  23.  
  24. rednet.open('top')
  25. rednet.broadcast('V_RPC', 'V_RPC')
  26. sender,message,distance = rednet.receive("V_PC", 2)
  27.  
  28. protectedChunks = textutils.unserialise(message)
  29. print('Received '..#protectedChunks..' protected chunks')
  30.  
  31. function BroadcastProtectedChunks()
  32. print('broadcasting protected chunks')
  33. rednet.broadcast(textutils.serialise(protectedChunks), "V_PC")
  34. print('broadcasted protected chunks')
  35. end
  36.  
  37. table.insert(protectedChunks, chunkX..','..chunkZ)
  38. print('Added own chunk to protected chunks')
  39. BroadcastProtectedChunks()
  40.  
  41. function UpdatePChunkLoop()
  42. while true do
  43. sender,message,distance = rednet.receive("V_PC")
  44. protectedChunks = textutils.unserialise(message)
  45. end
  46. end
  47.  
  48. function ServeProtectedChunks()
  49. while true do
  50. sender,message,distance = rednet.receive("V_RPC")
  51. rednet.send(sender, textutils.serialise(protectedChunks), "V_PC")
  52. print("Served PChunk request")
  53. end
  54. end
  55.  
  56. function ServeNodes()
  57. while true do
  58. sender,message,distance = rednet.receive("V_RN")
  59. rednet.send(sender, textutils.serialise({posX,posZ}), "V_N")
  60. print("Served Node request")
  61. end
  62. end
  63.  
  64. function PostMsgs()
  65. while true do
  66. sender,message,distance = rednet.receive("V_M")
  67. print('msg from '..sender..': '..message)
  68. end
  69. end
  70.  
  71. function Main()
  72. while true do
  73. print('add protected chunk: a')
  74. print('remove protected chunk: r')
  75. print('locate all turtles: l')
  76. print('update network: u')
  77. command = read()
  78.  
  79. if command == 'a' then
  80. print('chunk X')
  81. x = tonumber(read())
  82. print('chunk Z')
  83. z = tonumber(read())
  84. if protectedChunks[x..','..z] == nil then
  85. table.insert(protectedChunks, x..','..z)
  86. BroadcastProtectedChunks()
  87. else
  88. print('chunk already exists')
  89. end
  90. elseif command == 'r' then
  91. print('chunk X')
  92. x = tonumber(read())
  93. print('chunk Z')
  94. z = tonumber(read())
  95. if protectedChunks[x..','..z] ~= nil then
  96. table.remove(x..','..z)
  97. BroadcastProtectedChunks()
  98. else
  99. print('chunk does not exist')
  100. end
  101. elseif command == 'l' then
  102. print('locating tutels...')
  103. rednet.broadcast('pos', "V_CT")
  104. elseif command == 'u' then
  105. print('updating...')
  106. rednet.broadcast('upd', "V_CT")
  107. else
  108. print('unknown command')
  109. end
  110. end
  111. end
  112.  
  113. parallel.waitForAll(UpdatePChunkLoop, ServeProtectedChunks, ServeNodes, Main)
Add Comment
Please, Sign In to add comment