Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- local serialization= require("serialization")
- local computer= require("computer")
- local thread= require("thread")
- local modem = component.modem
- local Tn = component.navigation
- local Tr = component.radar
- local QueensChannel = 2412
- local SoldiersChannel = 2413
- modem.open(QueensChannel)
- modem.broadcast(QueensChannel,"Qr= component.proxy(component.list('radar')())")
- modem.broadcast(QueensChannel,"Qn= component.proxy(component.list('navigation')())")
- modem.broadcast(QueensChannel,"Qd= component.proxy(component.list('drone')())")
- modem.broadcast(QueensChannel,"Qm= component.proxy(component.list('modem')())")
- modem.broadcast(QueensChannel,"QueensChannel = 2412")
- modem.broadcast(QueensChannel,"Qm.open(QueensChannel)")
- modem.broadcast(QueensChannel,
- [[
- 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
- ]])
- modem.broadcast(QueensChannel,
- [[
- function getPlayerCoord(e_name)
- checkArg(1,e_name,'string','nil')
- for k,v in ipairs(Qr.getPlayers()) do
- if v.name == e_name then
- return {v.x,v.y,v.z},v.distance
- end
- end
- return {0,0,0},0
- end
- ]])
- modem.broadcast(QueensChannel,
- [[
- function move(x,y,z)
- checkArg(1,x,'number','nil')
- checkArg(1,y,'number','nil')
- checkArg(1,z,'number','nil')
- if x and y and z then
- Qd.setLightColor(0x00FFAF)
- Qd.move(x,y,z)
- end
- end
- ]])
- modem.broadcast(QueensChannel,
- [[
- function sub_vec(A,B) return {A[1]-B[1],A[2]-B[2],A[3]-B[3]} end
- function math.trunc(v)
- local t = math.modf(v)
- return t
- end
- function vec_trunc(A)
- if A then
- return {math.trunc(A[1]),math.trunc(A[2]),math.trunc(A[3])}
- end
- return nil
- end
- function navMoveToPlayer(e_name)
- checkArg(1,e_name,"string","nil")
- local trgPos = {Qn.getPosition()},stp
- if not trgPos[1] then Qd.setLightColor(0xFF0000) return "Out Of\nRange" end
- trgPos = vec_trunc(trgPos)
- local mv = {0,0,0}
- local mapRange = Qn.getRange()
- repeat
- local v = getPlayerCoord(e_name)
- v = vec_trunc(v)
- local Qnpos = {Qn.getPosition()}
- if Qnpos[1] then
- Qnpos = vec_trunc(Qnpos)
- local Qpn = {Qnpos[1] + v[1], Qnpos[2] + v[2] + 2, Qnpos[3] + v[3]}
- mv = sub_vec(Qpn,trgPos)
- if math.abs(Qpn[1]) < mapRange-5 and math.abs(Qpn[3]) < mapRange-5 then
- Qd.move(mv[1],mv[2],mv[3])
- trgPos = Qpn
- end
- else
- Qd.setLightColor(0xFF0000)
- Qd.setStatusText("Out Of\nRange")
- Qd.move(-mv[1],-mv[2],-mv[3])
- end
- stp = select(6,computer.pullSignal(0.5))
- until stp == "stop"
- return Qd.name()
- end
- ]])
- modem.broadcast(QueensChannel,
- [[
- function navSwarmPlayer(e_name)
- checkArg(1,e_name,"string","nil")
- local trgPos = {Qn.getPosition()},stp
- if not trgPos[1] then Qd.setLightColor(0xFF0000) return "Out Of\nRange" end
- trgPos = vec_trunc(trgPos)
- local mv = {0,0,0}
- local mapRange = Qn.getRange()
- repeat
- local v = getPlayerCoord(e_name)
- v = vec_trunc(v)
- local Qnpos = {Qn.getPosition()}
- if Qnpos[1] then
- Qnpos = vec_trunc(Qnpos)
- 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)}
- mv = sub_vec(Qpn,trgPos)
- if math.abs(Qpn[1]) < mapRange-5 and math.abs(Qpn[3]) < mapRange-5 then
- Qd.move(mv[1],mv[2],mv[3])
- trgPos = Qpn
- end
- else
- Qd.setLightColor(0xFF0000)
- Qd.setStatusText("Out Of\nRange")
- Qd.move(-mv[1],-mv[2],-mv[3])
- end
- stp = select(6,computer.pullSignal(0.5))
- until stp == "stop"
- return Qd.name()
- end
- ]])
- modem.broadcast(QueensChannel,
- [[
- local cc = 1
- isFree = true
- function replyInv(add)
- if isFree then
- Qd.setLightColor(0xFF00BB)
- Qm.send(add,QueensChannel,"pkme")
- --Qd.setStatusText(tostring(cc))
- cc=cc+1
- end
- end
- ]])
- modem.broadcast(QueensChannel,
- [[
- acts = {
- ["go"] = function(r_add,tag,x,y,z) Qd.setLightColor(0x00FF00) Qd.setStatusText(navMoveToPlayer(tag)) end,
- ["bzz"] = function(r_add,tag,x,y,z) Qd.setLightColor(0x0000FF) Qd.setStatusText(navSwarmPlayer(tag)) end,
- ["move"] = function(r_add,tag,x,y,z) move(x,y,z) end,
- ["inv"] = function(r_add,tag,x,y,z) replyInv(r_add) end,
- ["commit"] = function(r_add,tag,x,y,z) Qd.setLightColor(0xFF1F00) isFree = false Qd.setStatusText("cmt "..tostring(isFree)) end,
- ["uncommit"] = function(r_add,tag,x,y,z) isFree = true end,
- ["uok"] = function(r_add,tag,x,y,z) Qd.setLightColor(0x77FF77) isFree = true Qm.send(r_add,QueensChannel,"actv") end,
- ["formup"] = function(r_add,tag,x,y,z) Qd.setLightColor(0x0011FF) Qd.setStatusText(navMoveToPlayerWOffset(tag,x,y,z)) end,
- ["HUSH"] = function(r_add,tag,x,y,z) computer.shutdown() end
- }
- ]])
- modem.broadcast(QueensChannel,[[
- local gpsChannel = 2471
- local trgChannel = 2472
- Qm.open(gpsChannel)
- function QgpsBroadcast(xp,yp,zp)
- Qm.broadcast(gpsChannel,"gps",xp,yp,zp)
- end
- ]])
- modem.broadcast(QueensChannel,
- [[
- function navMoveToPlayerWOffset(e_name,xo,yo,zo)
- checkArg(1,e_name,"string","nil")
- local trgPos = {Qn.getPosition()},stp
- if not trgPos[1] then Qd.setLightColor(0xFF0000) return "Out Of\nRange" end
- trgPos = vec_trunc(trgPos)
- local mv = {0,0,0}
- local mapRange = Qn.getRange()
- repeat
- local v = getPlayerCoord(e_name)
- v = vec_trunc(v)
- QgpsBroadcast(-v[1],-v[2],-v[3])
- local Qnpos = {Qn.getPosition()}
- if Qnpos[1] then
- Qnpos = vec_trunc(Qnpos)
- local Qpn = {Qnpos[1] + v[1] + xo, Qnpos[2] + v[2] + yo, Qnpos[3] + v[3] + zo}
- mv = sub_vec(Qpn,trgPos)
- if math.abs(Qpn[1]) < mapRange-5 and math.abs(Qpn[3]) < mapRange-5 then
- Qd.move(mv[1],mv[2],mv[3])
- trgPos = Qpn
- end
- else
- Qd.setLightColor(0xFF0000)
- Qd.setStatusText("Out Of\nRange")
- Qd.move(-mv[1],-mv[2],-mv[3])
- end
- stp = select(6,computer.pullSignal(0.5))
- until stp == "stop"
- return Qd.name()
- end
- ]])
- modem.open(SoldiersChannel)
- modem.broadcast(SoldiersChannel,"Sr= component.proxy(component.list('radar')())")
- modem.broadcast(SoldiersChannel,"Sn= component.proxy(component.list('navigation')())")
- modem.broadcast(SoldiersChannel,"Sd= component.proxy(component.list('drone')())")
- 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")
- local 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
- local queensOnly7 =
- [[
- Qd.setAcceleration(100)
- local cmd,tag,x,y,z
- Qd.setLightColor(0xFFAF00)
- while true do
- _,_,r_add,_,_,cmd,tag,x,y,z = computer.pullSignal(0.5)
- if Qd.name():match("^Q%d+$") then
- if acts[cmd] then
- acts[cmd](r_add,tag,x,y,z)
- end
- end
- --Qd.setLightColor(0xFFAF00)
- --Qd.setLightColor(0x77FF77)
- end
- ]]
- modem.broadcast(QueensChannel,queensOnly7)
- pool={}
- flightformation={}
- ffbook={flightformation}
- form1 = {{3,2,-2},{-3,2,-2},{0,2,3}}
- form2 = {{-2,2,2},{2,2,2}}
- form3 = {{-2,2,-2},{2,2,-2}}
- fbook={form1,form2,form3}
- dynamic_fbook = fbook
- function killEvent(e)
- if e then
- event.cancel(e)
- end
- end
- local reaction
- function populatePool()
- killEvent(reaction)
- reaction = event.listen("modem_message",msg_handler)
- for i = 0,3 do modem.broadcast(QueensChannel,"inv") end
- sleep(1)
- end
- function add2Pool(addr)
- if pool[addr] then return end
- pool[addr]={true,true}
- end
- function add2FF(ff,f,addr)
- if not next(f) then return end
- if ff[addr] then return end
- ff[addr] = table.remove(f)
- modem.send(addr,QueensChannel,"commit")
- pool[addr][2]=false
- end
- function populateFF(ff,f)
- for addr,v in pairs(pool) do
- if v[1] and v[2] then add2FF(ff,f,addr) end
- if not next(f) then return end
- end
- end
- function pruneFF(ff,f)
- for addr,v in pairs(ff) do
- if pool[addr] then
- if not pool[addr][1] then
- table.insert(f,1,ff[addr])
- ff[addr] = nil
- modem.send(addr,QueensChannel,"uncommit")
- pool[addr][2]=true
- end
- end
- end
- end
- function setAsActive(addr)
- if pool[addr][1] then return end --if isActive or nil {return}
- pool[addr][1]=true
- end
- function refreshPool()
- --print("refreshing..")
- killEvent(reaction)
- reaction = event.listen("modem_message",msg_handler)
- for addr,v in pairs(pool) do
- pool[addr][1] = false
- modem.send(addr,QueensChannel,"stop")
- sleep(0.5)
- modem.send(addr,QueensChannel,"uok")
- end
- end
- function refreshFF(ff,f)
- print("refreshingFF..")
- refreshPool()
- sleep(1)
- pruneFF(ff,f)
- populateFF(ff,f)
- end
- msg_reaction = {
- ["pkme"] = function(l_add,r_add,port,dist,...) add2Pool(r_add) end,
- ["actv"] = function(l_add,r_add,port,dist,...) setAsActive(r_add) end
- }
- function msg_handler(evt,l_add,r_add,port,dist,msg,...)
- if msg_reaction[msg] then
- msg_reaction[msg](l_add,r_add,port,dist,...)
- end
- end
- ffbook[2]={}
- ffbook[3]={}
- function formFF(ff,f)
- populateFF(ff,f)
- refreshFF(ff,f)
- end
- function formUP(e_name,ff,f)
- for addr,pos in pairs(ff) do
- modem.send(addr,QueensChannel,"formup",e_name,pos[1],pos[2],pos[3])
- end
- end
- function breakFormation(ff,f)
- for addr,pos in pairs(ff) do
- table.insert(f,1,ff[addr])
- ff[addr]=nil
- modem.send(addr,QueensChannel,"uncommit")
- pool[addr][2]=true
- end
- refreshPool()
- end
- print("Bingus27")
- local gpsChannel = 2471
- local trgChannel = 2472
- function gpsBroadcast()
- while true do
- modem.broadcast(gpsChannel,"gps",0,0,0)
- --print("broadcasting..")
- os.sleep()
- end
- end
- local gpsThread
- local sat_mode = false
- function toggleGPSBroadCast()
- sat_mode = not sat_mode
- print("sat_mode: "..tostring(sat_mode))
- if sat_mode then
- print("creating GPS Thread..")
- gpsThread = thread.create(function() gpsBroadcast() end)
- else
- gpsThread:kill()
- end
- end
- function getPlayerCoord(e_name)
- checkArg(1,e_name,'string','nil')
- for k,v in ipairs(Tr.getPlayers()) do
- if v.name == e_name then
- return {v.x,v.y,v.z},v.distance
- end
- end
- return {0,0,0},0
- end
- function math.trunc(v)
- local t = math.modf(v)
- return t
- end
- function vec_trunc(A)
- if A then
- return {math.trunc(A[1]),math.trunc(A[2]),math.trunc(A[3])}
- end
- return nil
- end
- function trgBroadcast(e_name)
- while true do
- local player_co = getPlayerCoord(e_name)
- player_co = vec_trunc(player_co)
- modem.broadcast(trgChannel,"trg",player_co[1],player_co[2],player_co[3])
- --print("broadcasting target..")
- os.sleep()
- end
- end
- local trgThread
- local send_trg = false
- function toggleTargetBroadCast(target)
- send_trg = not send_trg
- print("send_trg: "..tostring(send_trg))
- if send_trg then
- print("creating GPS Thread..")
- trgThread = thread.create(function(t) trgBroadcast(t) end,target)
- else
- trgThread:kill()
- end
- end
- while true do
- local cmd=io.read()
- print("looping")
- if not cmd then return end
- if(cmd == "G") then
- modem.broadcast(QueensChannel,"stop")
- modem.broadcast(QueensChannel,"go","ph0")
- sleep(0.5)
- elseif(cmd == "B") then
- modem.broadcast(QueensChannel,"stop")
- modem.broadcast(QueensChannel,"bzz","ph0")
- sleep(0.5)
- elseif(cmd == "M") then
- modem.broadcast(QueensChannel,"stop")
- modem.broadcast(QueensChannel,"move","",0,3,0)
- sleep(0.5)
- elseif(cmd == "P") then --refreshFormation
- populatePool()
- sleep(0.5)
- elseif(cmd == "T") then
- formFF(ffbook[1],dynamic_fbook[1])
- formUP("ph0",ffbook[1],dynamic_fbook[1])
- sleep(0.5)
- elseif(cmd == "Q") then
- formFF(ffbook[2],dynamic_fbook[2])
- formFF(ffbook[3],dynamic_fbook[3])
- formUP("ph0",ffbook[2],dynamic_fbook[2])
- formUP("ph0",ffbook[3],dynamic_fbook[3])
- sleep(0.5)
- elseif(cmd == "E") then
- for i = 1,#ffbook do
- breakFormation(ffbook[i],dynamic_fbook[i])
- end
- killEvent(reaction)
- sleep(0.5)
- elseif(cmd == "R") then --refreshFormation
- for i = 1,#ffbook do
- refreshFF(ffbook[i],dynamic_fbook[i])
- end
- sleep(0.5)
- elseif(cmd == "PRINT") then --printGroup
- for k,v in pairs(pool) do
- print(k.." :: "..tostring(v[1]).." "..tostring(v[2]))
- end
- print(tostring(groupSize))
- for i = 1,#ffbook do
- print("ffbook["..i.."]:")
- for k,v in pairs(ffbook[i]) do
- print(k.." :: "..tostring(v[1]).." "..tostring(v[2]).." "..tostring(v[3]))
- end
- end
- sleep(0.5)
- elseif(cmd == "GPS") then
- toggleGPSBroadCast()
- sleep(0.5)
- elseif(cmd == "TRG") then
- toggleTargetBroadCast("ph0")
- sleep(0.5)
- elseif(cmd == "S") then
- modem.broadcast(QueensChannel,"stop")
- event.ignore("modem_message",msg_handler)
- sleep(0.5)
- else
- modem.broadcast(QueensChannel,cmd)
- end
- end
Add Comment
Please, Sign In to add comment