Advertisement
PHOBOSS

Swarm7_backup.lua

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