Advertisement
PHOBOSS

fflib_backup

Apr 8th, 2022
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local event = require 'event'
  2. local component = require 'component'
  3. local modem = component.modem
  4.  
  5. local flight_formation = {}
  6.  
  7. flight_formation.pool_Q={}
  8. flight_formation.pool_S={}
  9.  
  10. function flight_formation.clearPool_Q(p)
  11. flight_formation.pool_Q={}
  12. end
  13. function flight_formation.clearPool_S(p)
  14. flight_formation.pool_S={}
  15. end
  16.  
  17.  
  18. msg_reaction = {
  19. ["stats"] = function(l_add,r_add,port,dist,status,isQ,...) add2Pool(r_add,status,isQ) end
  20. }
  21.  
  22. function msg_handler(evt,l_add,r_add,port,dist,msg,status,isQ,...)
  23. if msg_reaction[msg] then
  24. msg_reaction[msg](l_add,r_add,port,dist,status,isQ,...)
  25. end
  26. end
  27.  
  28.  
  29. function openFlighFormComms()
  30. event.ignore("modem_message",msg_handler)
  31. event.listen("modem_message",msg_handler)
  32. print("event listener created!!!")
  33. end
  34. function flight_formation.closeFlighFormComms() --**********************--
  35. event.ignore("modem_message",msg_handler)
  36. print("event listener destroyed!!!")
  37. end
  38.  
  39. function flight_formation.populatePool(port,is_Queen) --**********************--
  40. openFlighFormComms()
  41. if is_Queen then
  42. modem.broadcast(port,"inv_q")
  43. else
  44. modem.broadcast(port,"inv_s")
  45. end
  46. end
  47.  
  48. function add2Pool(addr,is_free,is_Q)
  49. if is_Q then
  50. flight_formation.pool_Q[addr] = is_free
  51. else
  52. flight_formation.pool_S[addr] = is_free
  53. end
  54. end
  55.  
  56. function flight_formation.refreshPool(port,is_Queen) --**********************--
  57. --print("refreshing..")
  58. openFlighFormComms()
  59. if is_Queen then
  60. flight_formation.pool_Q={}
  61. modem.broadcast(port,"inv_q")
  62. else
  63. flight_formation.pool_S={}
  64. modem.broadcast(port,"inv_s")
  65. end
  66. end
  67.  
  68.  
  69. function populateFFT(ff,f,port,is_Queen)
  70. if is_Queen then
  71. for addr,is_free in ipairs(flight_formation.pool_Q) do
  72. if not next(f) then return end
  73. if is_free then
  74. if not ff[addr] then
  75. ff[addr] = table.remove(f)
  76. flight_formation.pool_Q[addr] = false
  77. modem.send(addr,port,"commit")
  78. end
  79. end
  80. end
  81. else
  82. for addr,is_free in ipairs(flight_formation.pool_S) do
  83. if not next(f) then return end
  84. if is_free then
  85. if not ff[addr] then
  86. ff[addr] = table.remove(f)
  87. flight_formation.pool_S[addr] = false
  88. modem.send(addr,port,"commit")
  89. end
  90. end
  91. end
  92. end
  93. end
  94.  
  95. function pruneFFT(ff,f,is_Queen)
  96. local drone_pool = {}
  97. if is_Queen then drone_pool = flight_formation.pool_Q
  98. else drone_pool = flight_formation.pool_S
  99. end
  100. for addr,c in pairs(ff) do
  101. if drone_pool[addr]==nil then
  102. table.insert(f,1,c)
  103. ff[addr] = nil
  104. end
  105. end
  106. end
  107.  
  108. function flight_formation.refreshFFT(ff,f,port,is_Queen) --**********************--
  109. print("refreshing FFT..")
  110. flight_formation.refreshPool(port,is_Queen)
  111. os.sleep(1)
  112. pruneFFT(ff,f,is_Queen)
  113. end
  114.  
  115. function flight_formation.formFF(ff,f,port,is_Queen) --**********************--
  116. populateFFT(ff,f,port,is_Queen)
  117. flight_formation.refreshFFT(ff,f,port,is_Queen)
  118. end
  119. function flight_formation.formUP(e_name,ff,f,port) --**********************--
  120. for addr,pos in pairs(ff) do
  121. modem.send(addr,port,"formup",e_name,pos[1],pos[2],pos[3])
  122. end
  123. end
  124.  
  125. function flight_formation.breakFormation(ff,f,port,is_Queen) --**********************--
  126. for addr,pos in pairs(ff) do
  127. table.insert(f,1,pos)
  128. ff[addr]=nil
  129. if is_Queen then
  130. if not flight_formation.pool_Q[addr] == nil then
  131. flight_formation.pool_Q[addr]=true
  132. end
  133. else
  134. if not flight_formation.pool_S[addr] == nil then
  135. flight_formation.pool_S[addr]=true
  136. end
  137. end
  138. modem.send(addr,port,"uncommit")
  139.  
  140. end
  141. os.sleep(1)
  142. flight_formation.refreshPool(port,is_Queen)
  143. end
  144.  
  145. function flight_formation.printDronePool(is_Queen) --**********************--
  146. print("Drone Pool:")
  147. print("Address: isFree:")
  148. if is_Queen then
  149. print("is queen")
  150. for k,v in ipairs(flight_formation.pool_Q) do
  151. print("this")
  152. print(k.." :: "..v)
  153. end
  154. else
  155. for k,v in ipairs(flight_formation.pool_S) do
  156. print(k.." :: "..v)
  157. end
  158. end
  159. end
  160. function flight_formation.printFFAssignment(ffb) --**********************--
  161. print("Flight Formation Assignment:")
  162. for i = 1,#ffb do
  163. print("Formation["..i.."]:")
  164. print("Address: X: Y: Z:")
  165. for k,v in pairs(ffb[i]) do
  166. print(k.." :: "..tostring(v[1]).." "..tostring(v[2]).." "..tostring(v[3]))
  167. end
  168. end
  169. end
  170.  
  171. return flight_formation
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement