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 return
end end
local transmitter_signal = 0 local signal_power = 0
local name = player:get_player_name() local name = player:get_player_name()
local meta = player:get_wielded_item():get_meta() local meta = player:get_wielded_item():get_meta()
local frequency = meta:get_string("frequency") local frequency = meta:get_string("frequency")
if frequency ~= nil and frequency ~= "" then if frequency ~= nil and frequency ~= "" then
local transmitter = self.read_transmitter(frequency) local transmitters = self.read_transmitters(frequency)
if transmitter.pos then
transmitter_signal = self:locate_transmitter(player, transmitter.pos) 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
end end
local text = "FQ "..tostring(meta:get_string("frequency")) local text = "FQ "..tostring(meta:get_string("frequency"))
@ -86,7 +91,7 @@ function ham_radio:update_hud_display(player)
player:hud_change( player:hud_change(
self.playerhuds[name].signal_level, self.playerhuds[name].signal_level,
"scale", "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 end

@ -11,20 +11,35 @@ ham_radio = {
} }
} }
function ham_radio.save_transmitter(frequency, transmitter_data) function ham_radio.save_transmitter(frequency, pos, transmitter_properties)
mod_storage:set_string(tostring(frequency), minetest.write_json(transmitter_data)) 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 end
function ham_radio.read_transmitter(frequency) function ham_radio.read_transmitters(frequency)
local transmitter_data = mod_storage:get_string(tostring(frequency)) local transmitters = mod_storage:get_string(tostring(frequency))
if transmitter_data ~= nil and transmitter_data ~= "" then if transmitters ~= "" then
return minetest.parse_json(transmitter_data) return minetest.parse_json(transmitters)
end end
return {} return {}
end end
function ham_radio.delete_transmitter(frequency) function ham_radio.delete_transmitter(frequency, pos)
mod_storage:set_string(tostring(frequency), nil) 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 end
dofile(modpath.."/craft.lua") dofile(modpath.."/craft.lua")

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