PHOBOSS

Swarm3.lua

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