ham_radio/helpers.lua

41 lines
1.3 KiB
Lua
Raw Normal View History

2019-12-07 20:24:49 +01:00
function ham_radio.validate_frequency(frequency)
local transmission_is_allowed = true
2019-12-07 20:54:21 +01:00
local num_freq = tonumber(frequency)
2019-12-07 20:24:49 +01:00
local freq = tostring(num_freq)
if next(ham_radio.find_transmitters(frequency)) then
if num_freq >= ham_radio.settings.locked_frequency.min
and num_freq <= ham_radio.settings.locked_frequency.max then
-- transmitter is in locked frequency range
transmission_is_allowed = false
end
end
2019-12-07 20:24:49 +01:00
return freq == frequency
2019-12-07 20:54:21 +01:00
and num_freq ~= nil
and num_freq == math.floor(num_freq)
2019-12-07 20:24:49 +01:00
and num_freq >= ham_radio.settings.frequency.min
and num_freq <= ham_radio.settings.frequency.max
and transmission_is_allowed
2019-12-07 20:24:49 +01:00
end
2019-12-07 23:46:20 +01:00
function ham_radio.find_transmitters(frequency)
local transmitter_list = {}
for key, transmitter in pairs(ham_radio.transmitters) do
if transmitter.frequency == frequency then
transmitter_list[key] = transmitter
end
end
return transmitter_list
end
function ham_radio.find_free_frequency(range)
2019-12-07 23:46:20 +01:00
local frequency = -1
while frequency == -1 do
frequency = tostring(math.floor(math.random(range.min, range.max)));
2019-12-07 23:46:20 +01:00
local are_there_transmitters = ham_radio.find_transmitters(frequency)
if next(are_there_transmitters) then
frequency = -1
end
end
return frequency
end