PHOBOSS

Swarm4.lua

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