View difference between Paste ID: pdPYPT4W and 4SizNPd3
SHOW: | | - or go back to the newest paste.
1
local event = require 'event'
2
local component = require 'component'
3
local modem = component.modem
4
5
local flight_formation = {}
6
7
flight_formation.pool_Q={}
8
flight_formation.pool_S={}
9
10
function flight_formation.clearPool_Q(p)
11
	flight_formation.pool_Q={}
12
end
13
function flight_formation.clearPool_S(p)
14
	flight_formation.pool_S={}
15
end
16
17
18
msg_reaction = {
19
	["stats"] = function(l_add,r_add,port,dist,status,isQ,...) add2Pool(r_add,status,isQ) end
20
}
21
 
22
function msg_handler(evt,l_add,r_add,port,dist,msg,status,isQ,...)
23
	if msg_reaction[msg] then
24
		msg_reaction[msg](l_add,r_add,port,dist,status,isQ,...)
25
	end
26
end
27
 
28
29
function openFlighFormComms()
30
		event.ignore("modem_message",msg_handler)
31
		event.listen("modem_message",msg_handler)
32
		print("event listener created!!!")
33
end
34
function flight_formation.closeFlighFormComms() --**********************--
35
		event.ignore("modem_message",msg_handler)
36
		print("event listener destroyed!!!")
37
end
38
 
39
function flight_formation.populatePool(port,is_Queen) --**********************--
40
	openFlighFormComms()
41
	if is_Queen then
42
		modem.broadcast(port,"inv_q")
43
	else
44
		modem.broadcast(port,"inv_s")
45
	end
46
end
47
 
48
function add2Pool(addr,is_free,is_Q)
49
	if is_Q then
50
		flight_formation.pool_Q[addr] = is_free
51
	else
52
		flight_formation.pool_S[addr] = is_free
53
	end
54
end
55
 
56
function flight_formation.refreshPool(port,is_Queen) --**********************--
57
	--print("refreshing..")
58
	openFlighFormComms()
59
	if is_Queen then
60
		flight_formation.pool_Q={}
61
		modem.broadcast(port,"inv_q")
62
	else
63
		flight_formation.pool_S={}
64
		modem.broadcast(port,"inv_s")
65
	end
66
end
67
 
68
 
69
function populateFFT(ff,f,port,is_Queen)
70-
	print("f length: ",#f)
70+
71
		for addr,is_free in ipairs(flight_formation.pool_Q) do
72-
		for addr,is_free in pairs(flight_formation.pool_Q) do
72+
73
			if is_free then
74
				if not ff[addr] then
75
						ff[addr] = table.remove(f)
76
						flight_formation.pool_Q[addr] = false
77
						modem.send(addr,port,"commit")
78
				end
79
			end
80
		end
81
	else
82
		for addr,is_free in ipairs(flight_formation.pool_S) do
83-
		for addr,is_free in pairs(flight_formation.pool_S) do
83+
84
			if is_free then
85
				if not ff[addr] then
86
						ff[addr] = table.remove(f)
87
						flight_formation.pool_S[addr] = false
88
						modem.send(addr,port,"commit")
89
				end
90
			end
91
		end
92
	end
93
end
94
 
95
function pruneFFT(ff,f,is_Queen)
96
	local drone_pool = {}
97
	if is_Queen then drone_pool = flight_formation.pool_Q
98
	else drone_pool = flight_formation.pool_S
99
	end
100
	for addr,c in pairs(ff) do
101
		if drone_pool[addr]==nil then
102
			table.insert(f,1,c)
103
			ff[addr] = nil
104
		end
105
	end
106
end
107
 
108
function flight_formation.refreshFFT(ff,f,port,is_Queen) --**********************--
109-
function refreshFF(ff,f,port,is_Queen)
109+
110-
	print("refreshing FF..")
110+
111
	os.sleep(1)
112-
	os.sleep(2)
112+
113
end
114
 
115-
		modem.send(addr,port,"commit")
115+
116
	populateFFT(ff,f,port,is_Queen)
117-
			if not (flight_formation.pool_Q[addr] == nil) then flight_formation.pool_Q[addr] = false end
117+
	flight_formation.refreshFFT(ff,f,port,is_Queen)
118
end
119-
			if not (flight_formation.pool_S[addr] == nil) then flight_formation.pool_S[addr] = false end
119+
function flight_formation.formUP(e_name,ff,f,port) --**********************--
120
	for addr,pos in pairs(ff) do
121
		modem.send(addr,port,"formup",e_name,pos[1],pos[2],pos[3])
122
	end
123
end
124-
function flight_formation.refreshFFT(ffbook,fbook,port,is_Queen) --**********************--
124+
125
function flight_formation.breakFormation(ff,f,port,is_Queen) --**********************--
126-
	for i = 1,#ffbook do
126+
127-
		refreshFF(ffbook[i],fbook[i],port,is_Queen)
127+
128
		ff[addr]=nil
129
		if is_Queen then
130
			if not flight_formation.pool_Q[addr] == nil then
131
				flight_formation.pool_Q[addr]=true
132
			end
133
		else
134
			if not flight_formation.pool_S[addr] == nil then
135
				flight_formation.pool_S[addr]=true
136
			end
137-
function flight_formation.QformUP(e_name,ff,port) --**********************--
137+
138
		modem.send(addr,port,"uncommit")
139-
		--modem.send(addr,port,"formup",e_name,pos[1],pos[2],pos[3])
139+
140-
		local comp_pos = {}
140+
141
	os.sleep(1)
142-
		if pos[1]>0 then comp_pos[1]=pos[1]-1 else comp_pos[1]=pos[1] end --compensate positive coordinate component position
142+
143-
		if pos[2]>0 then comp_pos[2]=pos[2]-1 else comp_pos[2]=pos[2] end
143+
144-
		if pos[3]>0 then comp_pos[3]=pos[3]-1 else comp_pos[3]=pos[3] end
144+
145-
		modem.send(addr,port,"formup",e_name,comp_pos[1],comp_pos[2],comp_pos[3])
145+
146
	print("Drone Pool:")
147
	print("Address:				isFree:")
148
	if is_Queen then
149-
function flight_formation.PformUP(trgPort,ff,port) --**********************--
149+
		print("is queen")
150
		for k,v in ipairs(flight_formation.pool_Q) do
151-
		modem.send(addr,port,"formup",pos[1],pos[2],pos[3],trgPort)
151+
			print("this")
152
			print(k.." ::	"..v)
153
		end
154
	else
155
		for k,v in ipairs(flight_formation.pool_S) do
156-
function flight_formation.POrbit(trgPort,ff,port) --**********************--
156+
			print(k.." ::	"..v)
157
		end
158-
		print("orbiting: ",addr,"trgPort: ",trgPort)
158+
159-
		modem.send(addr,port,"orbit",pos[1],pos[2],pos[3],trgPort)
159+
160
function flight_formation.printFFAssignment(ffb) --**********************--
161
	print("Flight Formation Assignment:")
162
	for i = 1,#ffb do
163
		print("Formation["..i.."]:")
164
		print("Address:				X:	Y:	Z:")
165
		for k,v in pairs(ffb[i]) do
166
			print(k.." ::	"..tostring(v[1]).."	"..tostring(v[2]).."	"..tostring(v[3]))
167
		end
168
	end
169
end
170
171
return flight_formation