allow multiple transmitters on same frequency

This commit is contained in:
techniX 2019-12-07 19:48:07 +02:00
parent 14ec72cfe6
commit 7c40bd0c19
3 changed files with 49 additions and 19 deletions

15
hud.lua

@ -70,15 +70,20 @@ function ham_radio:update_hud_display(player)
return
end
local transmitter_signal = 0
local signal_power = 0
local name = player:get_player_name()
local meta = player:get_wielded_item():get_meta()
local frequency = meta:get_string("frequency")
if frequency ~= nil and frequency ~= "" then
local transmitter = self.read_transmitter(frequency)
if transmitter.pos then
transmitter_signal = self:locate_transmitter(player, transmitter.pos)
local transmitters = self.read_transmitters(frequency)
for key, transmitter in pairs(transmitters) do
local transmitter_signal = self:locate_transmitter(player, transmitter.pos)
if transmitter_signal > signal_power then
-- use max power from transmitters nearby
signal_power = transmitter_signal
end
end
end
local text = "FQ "..tostring(meta:get_string("frequency"))
@ -86,7 +91,7 @@ function ham_radio:update_hud_display(player)
player:hud_change(
self.playerhuds[name].signal_level,
"scale",
{ x = transmitter_signal/50 or 0.1, y = 1 } -- x scale should be 0-2
{ x = signal_power/50 or 0.1, y = 1 } -- x scale should be 0-2
)
end

@ -11,20 +11,35 @@ ham_radio = {
}
}
function ham_radio.save_transmitter(frequency, transmitter_data)
mod_storage:set_string(tostring(frequency), minetest.write_json(transmitter_data))
function ham_radio.save_transmitter(frequency, pos, transmitter_properties)
local transmitters = mod_storage:get_string(tostring(frequency))
local transmitter_list = {}
if transmitters ~= "" then
transmitter_list = minetest.parse_json(transmitters)
end
transmitter_list[minetest.pos_to_string(pos, 0)] = transmitter_properties
mod_storage:set_string(tostring(frequency), minetest.write_json(transmitter_list))
end
function ham_radio.read_transmitter(frequency)
local transmitter_data = mod_storage:get_string(tostring(frequency))
if transmitter_data ~= nil and transmitter_data ~= "" then
return minetest.parse_json(transmitter_data)
function ham_radio.read_transmitters(frequency)
local transmitters = mod_storage:get_string(tostring(frequency))
if transmitters ~= "" then
return minetest.parse_json(transmitters)
end
return {}
end
function ham_radio.delete_transmitter(frequency)
mod_storage:set_string(tostring(frequency), nil)
function ham_radio.delete_transmitter(frequency, pos)
local transmitters = mod_storage:get_string(tostring(frequency))
if transmitters ~= "" then
local transmitter_list = minetest.parse_json(transmitters)
transmitter_list[minetest.pos_to_string(pos, 0)] = nil
if next(transmitter_list) == nil then
mod_storage:set_string(tostring(frequency),"")
else
mod_storage:set_string(tostring(frequency), minetest.write_json(transmitter_list))
end
end
end
dofile(modpath.."/craft.lua")

@ -23,6 +23,11 @@ minetest.register_node("ham_radio:transmitter", {
)
meta:set_string("infotext", '')
end,
after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos);
local name = placer:get_player_name()
meta:set_string('operated_by', name)
end,
on_receive_fields = function(pos, formname, fields, sender)
if fields.quit ~= "true" then
return
@ -31,10 +36,15 @@ minetest.register_node("ham_radio:transmitter", {
local meta = minetest.get_meta(pos)
meta:set_string("frequency", fields.frequency)
meta:set_string("infotext", 'Frequency: '..fields.frequency)
ham_radio.save_transmitter(fields.frequency, {
ham_radio.save_transmitter(
fields.frequency,
pos,
{
pos = pos,
broadcast_message = "Test Ham Radio Broadcast!"
})
broadcast_message = "Test Ham Radio Broadcast!",
operated_by = meta:get_string('operated_by')
}
)
end
end,
can_dig = function(pos,player)
@ -44,6 +54,6 @@ minetest.register_node("ham_radio:transmitter", {
return inv:is_empty("main") and not minetest.is_protected(pos, name)
end,
after_dig_node = function(pos, oldnode, oldmetadata, player)
ham_radio.delete_transmitter(oldmetadata.fields.frequency)
ham_radio.delete_transmitter(oldmetadata.fields.frequency, pos)
end
});