Advertisement
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
- --custom libraries
- local s_utils = require("swarm_utilities")
- local q_firmware = require("queen_firmware")
- modem.open(QueensChannel)
- q_firmware.broadcastFirmWare(QueensChannel)
- 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 pool={}
- flightformation={}
- ffbook={flightformation}
- ffbook[2]={}
- ffbook[3]={}
- 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
- 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
- local reaction
- function openFlighFormComms()
- killEvent(reaction)
- reaction = event.listen("modem_message",msg_handler)
- end
- function closeFlighFormComms() --**********************--
- killEvent(reaction)
- end
- function populatePool() --**********************--
- --killEvent(reaction)
- --reaction = event.listen("modem_message",msg_handler)
- openFlighFormComms()
- for i = 0,3 do modem.broadcast(QueensChannel,"inv") end
- os.sleep(1)
- end
- function refreshPool()
- --print("refreshing..")
- --killEvent(reaction)
- --reaction = event.listen("modem_message",msg_handler)
- openFlighFormComms()
- for addr,v in pairs(pool) do
- pool[addr][1] = false
- modem.send(addr,QueensChannel,"stop")
- os.sleep(0.5)
- modem.send(addr,QueensChannel,"uok")
- end
- end
- function add2Pool(addr)
- if pool[addr] then return end
- pool[addr]={true,true}
- end
- function setAsActive(addr)
- if pool[addr][1] then return end --if isActive or nil {return}
- pool[addr][1]=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 refreshFF(ff,f) --**********************--
- print("refreshingFF..")
- refreshPool()
- os.sleep(1)
- pruneFF(ff,f)
- populateFF(ff,f)
- end
- 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("Bingus28")
- local gpsChannel = 2
- local trgChannel = 2472
- function gpsBroadcast(gps_port)
- while true do
- modem.broadcast(gps_port,"gps",0,0,0)
- --print("broadcasting..")
- os.sleep()
- end
- end
- local gpsThread
- local sat_mode = false
- function toggleGPSBroadCast(channel) --**********************--
- sat_mode = not sat_mode
- print("sat_mode: "..tostring(sat_mode))
- if sat_mode then
- print("creating GPS Thread..")
- gpsThread = thread.create(function(port) gpsBroadcast(port) end,channel)
- 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 trgBroadcast(e_name)
- while true do
- local player_co = getPlayerCoord(e_name)
- player_co = s_utils.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()
- if not cmd then return end
- if(cmd == "G") then
- modem.broadcast(QueensChannel,"stop")
- modem.broadcast(QueensChannel,"go","ph0")
- os.sleep(0.5)
- elseif(cmd == "B") then
- modem.broadcast(QueensChannel,"stop")
- modem.broadcast(QueensChannel,"bzz","ph0")
- os.sleep(0.5)
- elseif(cmd == "M") then
- modem.broadcast(QueensChannel,"stop")
- modem.broadcast(QueensChannel,"move","",0,3,0)
- os.sleep(0.5)
- elseif(cmd == "P") then --refreshFormation
- populatePool()
- os.sleep(0.5)
- elseif(cmd == "T") then
- formFF(ffbook[1],dynamic_fbook[1])
- formUP("ph0",ffbook[1],dynamic_fbook[1])
- os.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])
- os.sleep(0.5)
- elseif(cmd == "E") then
- for i = 1,#ffbook do
- breakFormation(ffbook[i],dynamic_fbook[i])
- end
- killEvent(reaction)
- os.sleep(0.5)
- elseif(cmd == "R") then --refreshFormation
- for i = 1,#ffbook do
- refreshFF(ffbook[i],dynamic_fbook[i])
- end
- os.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
- os.sleep(0.5)
- elseif(cmd == "GPS") then
- toggleGPSBroadCast(gpsChannel)
- os.sleep(0.5)
- elseif(cmd == "TRG") then
- toggleTargetBroadCast("ph0")
- os.sleep(0.5)
- elseif(cmd == "S") then
- modem.broadcast(QueensChannel,"stop")
- event.ignore("modem_message",msg_handler)
- os.sleep(0.5)
- elseif(cmd == "EXIT") then
- closeFlighFormComms()
- os.exit()
- else
- modem.broadcast(QueensChannel,cmd)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement