Advertisement
PHOBOSS

SWARM7_SPAGET.lua

Apr 6th, 2022 (edited)
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component = require("component")
  2. local event = require("event")
  3. local serialization= require("serialization")
  4. local computer= require("computer")
  5. local thread= require("thread")
  6. local modem = component.modem
  7. local Tn = component.navigation
  8. local Tr = component.radar
  9. local QueensChannel = 2412
  10. local SoldiersChannel = 2413
  11.  
  12. --custom libraries
  13. local s_utils = require("swarm_utilities")
  14. --local q_firmware = require("queen_firmware")
  15. --local flightform = require("flight_formation")
  16.  
  17. modem.open(QueensChannel)
  18.  
  19. --q_firmware.broadcastFirmWare(QueensChannel)
  20. function Q_FIRMWARE()
  21. modem.broadcast(QueensChannel,
  22. [[
  23. Qr= component.proxy(component.list('radar')())
  24. Qn= component.proxy(component.list('navigation')())
  25. Qd= component.proxy(component.list('drone')())
  26. Qm= component.proxy(component.list('modem')())
  27. QueensChannel = 2412
  28. Qm.open(QueensChannel)
  29. ]]
  30. )
  31.  
  32. modem.broadcast(QueensChannel,
  33. [[
  34. function sleep(timeout)
  35. checkArg(1, timeout, 'number', 'nil')
  36. local deadline = computer.uptime() + (timeout or 0)
  37. repeat
  38. computer.pullSignal(deadline - computer.uptime())
  39. until computer.uptime() >= deadline
  40. end
  41. ]]
  42. )
  43.  
  44. modem.broadcast(QueensChannel,
  45. [[
  46. function getPlayerCoord(e_name)
  47. checkArg(1,e_name,'string','nil')
  48. for k,v in ipairs(Qr.getPlayers()) do
  49. if v.name == e_name then
  50. return {v.x,v.y,v.z},v.distance
  51. end
  52. end
  53. return {0,0,0},0
  54. end
  55. ]]
  56. )
  57. modem.broadcast(QueensChannel,
  58. [[
  59. function move(x,y,z)
  60. checkArg(1,x,'number','nil')
  61. checkArg(1,y,'number','nil')
  62. checkArg(1,z,'number','nil')
  63. if x and y and z then
  64. Qd.setLightColor(0x00FFAF)
  65. Qd.move(x,y,z)
  66. end
  67. end
  68. ]]
  69. )
  70. modem.broadcast(QueensChannel,
  71. [[
  72. function sub_vec(A,B) return {A[1]-B[1],A[2]-B[2],A[3]-B[3]} end
  73. function math.trunc(v)
  74. local t = math.modf(v)
  75. return t
  76. end
  77. function vec_trunc(A)
  78. if A then
  79. return {math.trunc(A[1]),math.trunc(A[2]),math.trunc(A[3])}
  80. end
  81. return nil
  82. end
  83. function navMoveToPlayer(e_name)
  84. checkArg(1,e_name,"string","nil")
  85. local trgPos = {Qn.getPosition()},stp
  86.  
  87. if not trgPos[1] then Qd.setLightColor(0xFF0000) return "Out Of\nRange" end
  88.  
  89. trgPos = vec_trunc(trgPos)
  90. local mv = {0,0,0}
  91. local mapRange = Qn.getRange()
  92.  
  93. repeat
  94. local v = getPlayerCoord(e_name)
  95. v = vec_trunc(v)
  96. local Qnpos = {Qn.getPosition()}
  97. if Qnpos[1] then
  98. Qnpos = vec_trunc(Qnpos)
  99. local Qpn = {Qnpos[1] + v[1], Qnpos[2] + v[2] + 2, Qnpos[3] + v[3]}
  100. mv = sub_vec(Qpn,trgPos)
  101. if math.abs(Qpn[1]) < mapRange-5 and math.abs(Qpn[3]) < mapRange-5 then
  102. Qd.move(mv[1],mv[2],mv[3])
  103. trgPos = Qpn
  104. end
  105. else
  106. Qd.setLightColor(0xFF0000)
  107. Qd.setStatusText("Out Of\nRange")
  108. Qd.move(-mv[1],-mv[2],-mv[3])
  109. end
  110. stp = select(6,computer.pullSignal(0.5))
  111. until stp == "stop"
  112. return Qd.name()
  113. end
  114. ]]
  115. )
  116. modem.broadcast(QueensChannel,
  117. [[
  118. function navSwarmPlayer(e_name)
  119. checkArg(1,e_name,"string","nil")
  120. local trgPos = {Qn.getPosition()},stp
  121.  
  122. if not trgPos[1] then Qd.setLightColor(0xFF0000) return "Out Of\nRange" end
  123.  
  124. trgPos = vec_trunc(trgPos)
  125. local mv = {0,0,0}
  126. local mapRange = Qn.getRange()
  127.  
  128. repeat
  129. local v = getPlayerCoord(e_name)
  130. v = vec_trunc(v)
  131. local Qnpos = {Qn.getPosition()}
  132. if Qnpos[1] then
  133. Qnpos = vec_trunc(Qnpos)
  134. local Qpn = {Qnpos[1] + v[1] +math.random(-3,3), Qnpos[2] + v[2] +math.random(-3,3), Qnpos[3] + v[3]+math.random(-3,3)}
  135. mv = sub_vec(Qpn,trgPos)
  136. if math.abs(Qpn[1]) < mapRange-5 and math.abs(Qpn[3]) < mapRange-5 then
  137. Qd.move(mv[1],mv[2],mv[3])
  138. trgPos = Qpn
  139. end
  140. else
  141. Qd.setLightColor(0xFF0000)
  142. Qd.setStatusText("Out Of\nRange")
  143. Qd.move(-mv[1],-mv[2],-mv[3])
  144. end
  145. stp = select(6,computer.pullSignal(0.5))
  146. until stp == "stop"
  147. return Qd.name()
  148. end
  149. ]]
  150. )
  151. modem.broadcast(QueensChannel,
  152. [[
  153. local cc = 1
  154. isFree = true
  155. function replyInv(add)
  156. if isFree then
  157. Qd.setLightColor(0xFF00BB)
  158. Qm.send(add,QueensChannel,"pkme")
  159. --Qd.setStatusText(tostring(cc))
  160. cc=cc+1
  161. end
  162. end
  163. ]]
  164. )
  165. modem.broadcast(QueensChannel,
  166. [[
  167. acts = {
  168. ["go"] = function(r_add,tag,x,y,z) Qd.setLightColor(0x00FF00) Qd.setStatusText(navMoveToPlayer(tag)) end,
  169. ["bzz"] = function(r_add,tag,x,y,z) Qd.setLightColor(0x0000FF) Qd.setStatusText(navSwarmPlayer(tag)) end,
  170. ["move"] = function(r_add,tag,x,y,z) move(x,y,z) end,
  171. ["inv"] = function(r_add,tag,x,y,z) replyInv(r_add) end,
  172. ["commit"] = function(r_add,tag,x,y,z) Qd.setLightColor(0xFF1F00) isFree = false Qd.setStatusText("cmt "..tostring(isFree)) end,
  173. ["uncommit"] = function(r_add,tag,x,y,z) isFree = true end,
  174. ["uok"] = function(r_add,tag,x,y,z) Qd.setLightColor(0x77FF77) isFree = true Qm.send(r_add,QueensChannel,"actv") end,
  175. ["formup"] = function(r_add,tag,x,y,z) Qd.setLightColor(0x0011FF) Qd.setStatusText(navMoveToPlayerWOffset(tag,x,y,z)) end,
  176. ["HUSH"] = function(r_add,tag,x,y,z) computer.shutdown() end
  177. }
  178. ]]
  179. )
  180. modem.broadcast(QueensChannel,
  181. [[
  182. gpsChannel = 2
  183. trgChannel = 2472
  184. function QgpsBroadcast()
  185. Qm.broadcast(gpsChannel,"Qgps")
  186. end
  187. ]]
  188. )
  189. modem.broadcast(QueensChannel,
  190. [[
  191. function navMoveToPlayerWOffset(e_name,xo,yo,zo)
  192. checkArg(1,e_name,"string","nil")
  193. local trgPos = {Qn.getPosition()}
  194. if not trgPos[1] then Qd.setLightColor(0xFF0000) return "Out Of\nRange" end
  195. trgPos = vec_trunc(trgPos)
  196. local mv = {0,0,0}
  197. local stp = "bingus"
  198. local mapRange = Qn.getRange()
  199. repeat
  200. local v = getPlayerCoord(e_name)
  201. v = vec_trunc(v)
  202. Qm.broadcast(gpsChannel,"gps",-v[1],-v[2],-v[3])
  203. local Qnpos = {Qn.getPosition()}
  204. if Qnpos[1] then
  205. Qnpos = vec_trunc(Qnpos)
  206. local Qpn = {Qnpos[1] + v[1] + xo, Qnpos[2] + v[2] + yo, Qnpos[3] + v[3] + zo}
  207. mv = sub_vec(Qpn,trgPos)
  208. if math.abs(Qpn[1]) < mapRange-5 and math.abs(Qpn[3]) < mapRange-5 then
  209. Qd.move(mv[1],mv[2],mv[3])
  210. trgPos = Qpn
  211. end
  212. else
  213. Qd.setLightColor(0xFF0000)
  214. Qd.setStatusText("Out Of\nRange")
  215. Qd.move(-mv[1],-mv[2],-mv[3])
  216. end
  217. stp = select(6,computer.pullSignal(0.5))
  218. until stp == "stop"
  219. return Qd.name()
  220. end
  221. ]]
  222. )
  223. modem.broadcast(QueensChannel,
  224. [[
  225. Qd.setAcceleration(100)
  226. local cmd,tag,x,y,z
  227. Qd.setLightColor(0xFFAF00)
  228. while true do
  229. _,_,r_add,_,_,cmd,tag,x,y,z = computer.pullSignal(0.5)
  230. if Qd.name():match("^Q%d+$") then
  231. if acts[cmd] then
  232. acts[cmd](r_add,tag,x,y,z)
  233. end
  234. end
  235. --Qd.setLightColor(0xFFAF00)
  236. --Qd.setLightColor(0x77FF77)
  237. end
  238. ]]
  239. )
  240. end
  241. modem.open(SoldiersChannel)
  242. modem.broadcast(SoldiersChannel,"Sr= component.proxy(component.list('radar')())")
  243. modem.broadcast(SoldiersChannel,"Sn= component.proxy(component.list('navigation')())")
  244. modem.broadcast(SoldiersChannel,"Sd= component.proxy(component.list('drone')())")
  245. modem.broadcast(SoldiersChannel,"function sleep(timeout) checkArg(1, timeout, 'number', 'nil') local deadline = computer.uptime() + (timeout or 0) repeat computer.pullSignal(deadline - computer.uptime()) until computer.uptime() >= deadline end")
  246.  
  247.  
  248. flightformation={}
  249. ffbook={flightformation}
  250. ffbook[2]={}
  251. ffbook[3]={}
  252. form1 = {{3,2,-2},{-3,2,-2},{0,2,3}}
  253. form2 = {{-2,2,2},{2,2,2}}
  254. form3 = {{-2,2,-2},{2,2,-2}}
  255. fbook={form1,form2,form3}
  256. dynamic_fbook = fbook
  257.  
  258. pool={}
  259.  
  260. function clearPool()
  261. end
  262.  
  263.  
  264. msg_reaction = {
  265. }
  266.  
  267. function msg_handler(evt,l_add,r_add,port,dist,msg,...)
  268. end
  269.  
  270.  
  271. function openFlighFormComms()
  272. end
  273. function closeFlighFormComms() --**********************--
  274. end
  275.  
  276. function populatePool(port) --**********************--
  277. end
  278.  
  279. function refreshPool(port)
  280. end
  281.  
  282. function add2Pool(addr)
  283. end
  284.  
  285. function setAsActive(addr)
  286. end
  287.  
  288. function add2FF(ff,f,addr,port)
  289. end
  290.  
  291. function populateFF(ff,f,port)
  292. end
  293.  
  294. function pruneFF(ff,f,port)
  295. end
  296.  
  297. function refreshFF(ff,f,port) --**********************--
  298. end
  299.  
  300. function formFF(ff,f,port) --**********************--
  301. end
  302. function formUP(e_name,ff,f,port) --**********************--
  303. end
  304.  
  305. function breakFormation(ff,f,port) --**********************--
  306. end
  307.  
  308. function printDronePool() --**********************--
  309. end
  310. function printFFAssignment(ffb) --**********************--
  311. end
  312.  
  313.  
  314. print("Bingus28")
  315.  
  316.  
  317. local gpsChannel = 2
  318. local trgChannel = 2472
  319.  
  320.  
  321. function gpsBroadcast(gps_port)
  322. while true do
  323. modem.broadcast(gps_port,"gps",0,0,0)
  324. --print("broadcasting..")
  325. os.sleep()
  326. end
  327. end
  328.  
  329. local gpsThread
  330. local sat_mode = false
  331. function toggleGPSBroadCast(channel) --**********************--
  332. sat_mode = not sat_mode
  333. print("sat_mode: "..tostring(sat_mode))
  334. if sat_mode then
  335. print("creating GPS Thread..")
  336. gpsThread = thread.create(function(port) gpsBroadcast(port) end,channel)
  337. else
  338. gpsThread:kill()
  339. end
  340. end
  341.  
  342. function getPlayerCoord(e_name)
  343. checkArg(1,e_name,'string','nil')
  344. for k,v in ipairs(Tr.getPlayers()) do
  345. if v.name == e_name then
  346. return {v.x,v.y,v.z},v.distance
  347. end
  348. end
  349. return {0,0,0},0
  350. end
  351.  
  352. function trgBroadcast(e_name)
  353. while true do
  354. local player_co = getPlayerCoord(e_name)
  355. player_co = s_utils.vec_trunc(player_co)
  356. modem.broadcast(trgChannel,"trg",player_co[1],player_co[2],player_co[3])
  357. --print("broadcasting target..")
  358. os.sleep()
  359. end
  360. end
  361. local trgThread
  362. local send_trg = false
  363. function toggleTargetBroadCast(target) --**********************--
  364. send_trg = not send_trg
  365. print("send_trg: "..tostring(send_trg))
  366. if send_trg then
  367. print("creating TRG Thread..")
  368. trgThread = thread.create(function(t) trgBroadcast(t) end,target)
  369. else
  370. trgThread:kill()
  371. end
  372. end
  373.  
  374.  
  375. while true do
  376. local cmd=io.read()
  377. if not cmd then return end
  378. if(cmd == "F") then
  379. print("Broadcasting Queen FIRMWARE")
  380. Q_FIRMWARE()
  381. os.sleep(0.5)
  382. elseif(cmd == "G") then
  383. modem.broadcast(QueensChannel,"stop")
  384. modem.broadcast(QueensChannel,"go","ph0")
  385. os.sleep(0.5)
  386. elseif(cmd == "B") then
  387. modem.broadcast(QueensChannel,"stop")
  388. modem.broadcast(QueensChannel,"bzz","ph0")
  389. os.sleep(0.5)
  390. elseif(cmd == "M") then
  391. modem.broadcast(QueensChannel,"stop")
  392. modem.broadcast(QueensChannel,"move","",0,3,0)
  393. os.sleep(0.5)
  394. elseif(cmd == "P") then --refreshFormation
  395. populatePool(QueensChannel)
  396. os.sleep(0.5)
  397. elseif(cmd == "T") then
  398. formFF(ffbook[1],dynamic_fbook[1],QueensChannel)
  399. formUP("ph0",ffbook[1],dynamic_fbook[1],QueensChannel)
  400. os.sleep(0.5)
  401. elseif(cmd == "Q") then
  402. formFF(ffbook[2],dynamic_fbook[2],QueensChannel)
  403. formFF(ffbook[3],dynamic_fbook[3],QueensChannel)
  404. formUP("ph0",ffbook[2],dynamic_fbook[2],QueensChannel)
  405. formUP("ph0",ffbook[3],dynamic_fbook[3],QueensChannel)
  406. os.sleep(0.5)
  407. elseif(cmd == "E") then
  408. for i = 1,#ffbook do
  409. breakFormation(ffbook[i],dynamic_fbook[i],QueensChannel)
  410. end
  411. os.sleep(0.5)
  412. elseif(cmd == "R") then --refreshFormation
  413. for i = 1,#ffbook do
  414. refreshFF(ffbook[i],dynamic_fbook[i],QueensChannel)
  415. end
  416. os.sleep(0.5)
  417. elseif(cmd == "PRINT") then --printGroup
  418. printDronePool()
  419. printFFAssignment(ffbook)
  420. os.sleep(0.5)
  421. elseif(cmd == "GPS") then
  422. toggleGPSBroadCast(gpsChannel)
  423. os.sleep(0.5)
  424. elseif(cmd == "TRG") then
  425. toggleTargetBroadCast("ph0")
  426. os.sleep(0.5)
  427. elseif(cmd == "EXIT") then
  428. for i = 1,#ffbook do
  429. breakFormation(ffbook[i],dynamic_fbook[i],QueensChannel)
  430. end
  431. closeFlighFormComms()
  432. clearPool()
  433. os.exit()
  434. elseif(cmd == "S") then
  435. modem.broadcast(QueensChannel,"stop")
  436. event.ignore("modem_message",msg_handler)
  437. os.sleep(0.5)
  438. else
  439. modem.broadcast(QueensChannel,cmd)
  440. end
  441. end
  442.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement