View difference between Paste ID: 3LdteqQc and RLFC3EGd
SHOW: | | - or go back to the newest paste.
1
local args={...}
2
3
local hopSlice = tonumber(args[1]) or 1
4
5
local modems = {}
6
for k,v in pairs(rs.getSides()) do
7
	if peripheral.getType(v)=="modem" then
8
		modems[#modems+1] = peripheral.wrap(v)
9
	end
10
end
11
12
local modemColors = {}
13
14
local count = 1
15-
	for i=0,65535,128 * #modems do
15+
for k,v in pairs(colors) do
16-
		print("Hopping on channel range " .. i .. " to " .. i+127)
16+
	if type(v) == "number" then
17
		modemColors[count] = v
18
		count = count + 1
19-
				modems[j].open((i + math.floor(65535 / #modems)) % 65535)
19+
		if count > #modems then break end
20
	end
21
end
22
23
print("Found " .. #modems .. " modem"..(#modems>1 and "s " or " ")..", estimated total hop time is " .. (65535 / (#modems * 128)) * hopSlice .. " seconds.")
24
25
local i=0
26
local num = 0
27
28
local logFile = fs.open("log","a")
29
30
function log(s)
31
	logFile.writeLine(s)
32
end
33
34
local space = 65536 / #modems
35
36-
end
36+
term.clear()
37
term.setBackgroundColor(colors.white)
38
39
while true do
40
	for i=0,space,128 do -- get the block size
41
		local open={}
42
		
43
		print("Hopping block " .. i)
44
		for i=i,i+127 do
45
			for j=1,#modems do
46
				local c = math.floor(i + ((j-1) * space)) % 65535
47
				modems[j].open(c)
48
				open[c]=j
49
			end
50
		end
51
		
52
		term.setBackgroundColor(colors.black)
53
		term.clear()
54
		term.setBackgroundColor(colors.white)
55
		term.setCursorPos(1,1)
56
		
57
		local xSize,ySize = term.getSize()
58
		for j = 1,512 do
59
			if open[j * 128] then
60
				term.setBackgroundColor(modemColors[open[j * 128]])
61
			end
62
			
63
			io.write(open[j*128] or " ")
64
			term.setBackgroundColor(colors.white)	
65
		end
66
		
67
		os.startTimer(hopSlice)
68
		while true do
69
			local e,side,recvChan,replyChan,msg,dist = os.pullEvent()
70
			if e == "timer" then
71
				break
72
			elseif e == "modem_message" then
73
				log(recvChan .. ":" .. replyChan .. ":"..msg)
74
				print("Message on " .. recvChan .. ":" .. replyChan .. ", '"..msg.."'")
75
				num = num + 1
76
			end
77
		end
78
		for j=1,#modems do
79
			modems[j].closeAll()
80
		end
81
	end
82
	
83
	print("Round finished, logged " .. num .. " messages, press any key to continue, enter to finish.")
84
	if io.read()=="" then break end
85
	num = 0
86
end
87
88
for j=1,#modems do
89
	modems[j].closeAll()
90
end
91
92
logFile.close()