transmit and save function names

This commit is contained in:
Baipyrus 2023-11-03 21:31:45 +01:00
parent 4477ab941f
commit 715beb6d00
2 changed files with 29 additions and 2 deletions

View File

@ -145,7 +145,7 @@ function init_modem(options)
local channel = math.random(2, 1 + options.channels)
-- open reply channel
system.modem.open(channel)
system.modem.transmit(1, channel, options.name)
system.modem.transmit(1, channel, options.name .. '\n' .. table.concat(system.names, '\n'))
term.setCursorPos(1, 6)
print('\nTry #' .. count)
@ -283,6 +283,11 @@ end
function run_client(options, funcs)
-- save response functions
system.funcs = funcs
-- save function names for transmit
system.names = {}
for key, _ in pairs(funcs) do
table.insert(system.names, key)
end
-- get components and run permanently
while wait_for_components(options) do

View File

@ -46,12 +46,22 @@ function channel_used(channel)
return false
end
-- split string by delimiter
function split_string(text, delimiter)
local result = {}
for match in (text .. delimiter):gmatch('(.-)' .. delimiter) do
table.insert(result, match)
end
return result
end
-- wait for components to connect to receiver
function wait_for_components()
-- reset system variables
system.timeout = false
system.components = {}
system.channels = {}
system.funcs = {}
system.index = 1
-- open initializer channel
@ -72,10 +82,22 @@ function wait_for_components()
if channel_used(replyChannel) then
system.modem.transmit(replyChannel, replyChannel, 'used')
else
local result = split_string(message, '\n')
-- copy function names
local funcs = {}
for i, value in ipairs(result) do
if i > 1 then
table.insert(funcs, value)
end
end
-- save component
table.insert(system.channels, replyChannel)
table.insert(system.components, message)
table.insert(system.components, result[1])
table.insert(system.funcs, funcs)
-- keep track of values
component = message
count = count + 1
success = true