PHOBOSS

fligh_formation_lib.lua

Apr 5th, 2022 (edited)
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.43 KB | None | 0 0
  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. print("f length: ",#f)
  71. if is_Queen then
  72. for addr,is_free in pairs(flight_formation.pool_Q) do
  73. if not next(f) then return end
  74. if is_free then
  75. if not ff[addr] then
  76. ff[addr] = table.remove(f)
  77. flight_formation.pool_Q[addr] = false
  78. modem.send(addr,port,"commit")
  79. end
  80. end
  81. end
  82. else
  83. for addr,is_free in pairs(flight_formation.pool_S) do
  84. if not next(f) then return end
  85. if is_free then
  86. if not ff[addr] then
  87. ff[addr] = table.remove(f)
  88. flight_formation.pool_S[addr] = false
  89. modem.send(addr,port,"commit")
  90. end
  91. end
  92. end
  93. end
  94. end
  95.  
  96. function pruneFFT(ff,f,is_Queen)
  97. local drone_pool = {}
  98. if is_Queen then drone_pool = flight_formation.pool_Q
  99. else drone_pool = flight_formation.pool_S
  100. end
  101. for addr,c in pairs(ff) do
  102. if drone_pool[addr]==nil then
  103. table.insert(f,1,c)
  104. ff[addr] = nil
  105. end
  106. end
  107. end
  108.  
  109. function refreshFF(ff,f,port,is_Queen)
  110. print("refreshing FF..")
  111. flight_formation.refreshPool(port,is_Queen)
  112. os.sleep(2)
  113. pruneFFT(ff,f,is_Queen)
  114. for addr,c in pairs(ff) do
  115. modem.send(addr,port,"commit")
  116. if is_Queen then
  117. if not (flight_formation.pool_Q[addr] == nil) then flight_formation.pool_Q[addr] = false end
  118. else
  119. if not (flight_formation.pool_S[addr] == nil) then flight_formation.pool_S[addr] = false end
  120. end
  121. end
  122. end
  123.  
  124. function flight_formation.refreshFFT(ffbook,fbook,port,is_Queen) --**********************--
  125. print("refreshing FFT..")
  126. for i = 1,#ffbook do
  127. refreshFF(ffbook[i],fbook[i],port,is_Queen)
  128. end
  129. end
  130.  
  131.  
  132.  
  133. function flight_formation.formFF(ff,f,port,is_Queen) --**********************--
  134. populateFFT(ff,f,port,is_Queen)
  135. end
  136.  
  137. function flight_formation.QformUP(e_name,ff,port) --**********************--
  138. for addr,pos in pairs(ff) do
  139. --modem.send(addr,port,"formup",e_name,pos[1],pos[2],pos[3])
  140. local comp_pos = {}
  141.  
  142. if pos[1]>0 then comp_pos[1]=pos[1]-1 else comp_pos[1]=pos[1] end --compensate positive coordinate component position
  143. if pos[2]>0 then comp_pos[2]=pos[2]-1 else comp_pos[2]=pos[2] end
  144. if pos[3]>0 then comp_pos[3]=pos[3]-1 else comp_pos[3]=pos[3] end
  145. modem.send(addr,port,"formup",e_name,comp_pos[1],comp_pos[2],comp_pos[3])
  146. end
  147. end
  148.  
  149. function flight_formation.PformUP(trgPort,ff,port) --**********************--
  150. for addr,pos in pairs(ff) do
  151. modem.send(addr,port,"formup",pos[1],pos[2],pos[3],trgPort)
  152. end
  153. end
  154.  
  155.  
  156. function flight_formation.POrbit(trgPort,ff,port) --**********************--
  157. for addr,pos in pairs(ff) do
  158. print("orbiting: ",addr,"trgPort: ",trgPort)
  159. modem.send(addr,port,"orbit",pos[1],pos[2],pos[3],trgPort)
  160. end
  161. end
  162.  
  163. function flight_formation.breakFormation(ff,f,port,is_Queen) --**********************--
  164. for addr,pos in pairs(ff) do
  165. table.insert(f,1,pos)
  166. ff[addr]=nil
  167. if is_Queen then
  168. if not flight_formation.pool_Q[addr] == nil then
  169. flight_formation.pool_Q[addr]=true
  170. end
  171. else
  172. if not flight_formation.pool_S[addr] == nil then
  173. flight_formation.pool_S[addr]=true
  174. end
  175. end
  176. modem.send(addr,port,"uncommit")
  177.  
  178. end
  179. os.sleep(1)
  180. flight_formation.refreshPool(port,is_Queen)
  181. end
  182.  
  183. function flight_formation.printDronePool(is_Queen) --**********************--
  184. print("Drone Pool:")
  185. print("Address: isFree:")
  186. if is_Queen then
  187. for k,v in pairs(flight_formation.pool_Q) do
  188. print(k.." :: "..tostring(v))
  189. end
  190. else
  191. for k,v in pairs(flight_formation.pool_S) do
  192. print(k.." :: "..tostring(v))
  193. end
  194. end
  195. end
  196. function flight_formation.printFFAssignment(ffb) --**********************--
  197. print("Flight Formation Assignment:")
  198. for i = 1,#ffb do
  199. print("Formation["..i.."]:")
  200. print("Address: X: Y: Z:")
  201. for k,v in pairs(ffb[i]) do
  202. print(k.." :: "..tostring(v[1]).." "..tostring(v[2]).." "..tostring(v[3]))
  203. end
  204. end
  205. end
  206.  
  207. return flight_formation
  208.  
Advertisement
Add Comment
Please, Sign In to add comment