receive broadcast messages

This commit is contained in:
techniX 2019-12-07 22:54:19 +02:00
parent d3994663c8
commit 6d3abeb65a
4 changed files with 90 additions and 28 deletions

43
broadcast.lua Normal file

@ -0,0 +1,43 @@
function ham_radio.get_broadcast_messages(frequency)
local transmitters = ham_radio.find_transmitters(frequency)
local broadcasts = {}
for position, transmitter in pairs(transmitters) do
if transmitter.broadcast_message ~= "" then
-- construct message
local message = table.concat({
'[ Radio Broadcast: ',
transmitter.operated_by,
' ] ',
transmitter.broadcast_message,
}, "")
table.insert(broadcasts, message)
end
end
return broadcasts
end
function ham_radio:update_broadcast(player)
local name = player:get_player_name()
local item = player:get_wielded_item()
if item:get_name() ~= "ham_radio:receiver" then
return
end
local frequency = item:get_meta():get_string("frequency")
if ham_radio.player_broadcasts[name] == nil then
ham_radio.player_broadcasts[name] = ham_radio.get_broadcast_messages(frequency)
end
local message = table.remove(ham_radio.player_broadcasts[name])
if message ~= nil then
minetest.chat_send_player(player:get_player_name(), minetest.colorize(ham_radio.settings.broadcast_color, message))
-- when all broadcast messages are shown, reload them again
if not next(ham_radio.player_broadcasts[name]) then
ham_radio.player_broadcasts[name] = ham_radio.get_broadcast_messages(frequency)
end
end
end

33
hud.lua

@ -3,14 +3,15 @@ function ham_radio.toggle_hud(player)
local name = player:get_player_name()
local item = player:get_wielded_item()
-- remove hud if user does not wield a receiver
-- remove hud and broadcasts if user does not wield a receiver
if item:get_name() ~= "ham_radio:receiver" then
if ham_radio.is_receiver_wielded[name] then
player:hud_remove(ham_radio.playerhuds[name].background)
player:hud_remove(ham_radio.playerhuds[name].frequency)
player:hud_remove(ham_radio.playerhuds[name].signal_meter)
player:hud_remove(ham_radio.playerhuds[name].signal_level)
ham_radio.is_receiver_wielded[name] = false
player:hud_remove(ham_radio.playerhuds[name].background)
player:hud_remove(ham_radio.playerhuds[name].frequency)
player:hud_remove(ham_radio.playerhuds[name].signal_meter)
player:hud_remove(ham_radio.playerhuds[name].signal_level)
ham_radio.is_receiver_wielded[name] = false
ham_radio.player_broadcasts[name] = nil
end
return false
end
@ -93,23 +94,3 @@ function ham_radio:update_hud_display(player)
{ x = signal_power/50 or 0.1, y = 1 } -- x scale should be 0-2
)
end
minetest.register_on_newplayer(ham_radio.toggle_hud)
minetest.register_on_joinplayer(ham_radio.toggle_hud)
minetest.register_on_leaveplayer(function(player)
ham_radio.is_receiver_wielded[name] = false
ham_radio.playerhuds[player:get_player_name()] = nil
end)
local updatetimer = 0
minetest.register_globalstep(function(dtime)
updatetimer = updatetimer + dtime
if updatetimer > 0.1 then
local players = minetest.get_connected_players()
for i=1, #players do
ham_radio:update_hud_display(players[i])
end
updatetimer = updatetimer - dtime
end
end)

@ -5,9 +5,12 @@ ham_radio = rawget(_G, "ham_radio") or {}
ham_radio = {
playerhuds = {},
player_broadcasts = {},
is_receiver_wielded = {},
transmitters = {},
settings = {
broadcast_color = '#607d8b',
broadcast_interval = 10, -- seconds
hud_pos = { x = 0.5, y = 0.8 },
frequency = {
min = 0,
@ -47,9 +50,41 @@ end
dofile(modpath.."/helpers.lua")
dofile(modpath.."/craft.lua")
dofile(modpath.."/transmitter.lua")
dofile(modpath.."/receiver.lua")
dofile(modpath.."/receiver.lua")
dofile(modpath.."/broadcast.lua")
dofile(modpath.."/hud.lua")
-- globals
minetest.register_on_newplayer(ham_radio.toggle_hud)
minetest.register_on_joinplayer(ham_radio.toggle_hud)
minetest.register_on_leaveplayer(function(player)
ham_radio.is_receiver_wielded[name] = false
ham_radio.playerhuds[player:get_player_name()] = nil
end)
local updatetimer = 0
local broadcasttimer = 0
minetest.register_globalstep(function(dtime)
updatetimer = updatetimer + dtime
broadcasttimer = broadcasttimer + dtime
if updatetimer > 0.1 then
local players = minetest.get_connected_players()
for i=1, #players do
ham_radio:update_hud_display(players[i])
end
updatetimer = 0
end
if broadcasttimer > ham_radio.settings.broadcast_interval then
local players = minetest.get_connected_players()
for i=1, #players do
ham_radio:update_broadcast(players[i])
end
broadcasttimer = 0
end
end)
-- TODO: craft transmitter
-- TODO: configure transmitter
-- TODO: craft pelengator

@ -34,7 +34,10 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
local item = player:get_wielded_item()
local meta = item:get_meta()
meta:set_string("frequency", fields.frequency)
player:set_wielded_item(item) -- replace wielded item with new metadata
-- replace wielded item with new metadata
player:set_wielded_item(item)
-- reset broadcast messages
ham_radio.player_broadcasts[player:get_player_name()] = nil
return true
end)