diff --git a/hud.lua b/hud.lua index 7e526e8..4effa50 100644 --- a/hud.lua +++ b/hud.lua @@ -1,70 +1,70 @@ - -function ham_radio.init_hud(player) - local name = player:get_player_name() - ham_radio.playerhuds[name] = player:hud_add({ - hud_elem_type = "text", - text = "", - position = ham_radio.settings.hud_pos, - offset = { x = ham_radio.settings.hud_offset.x, y = ham_radio.settings.hud_offset.y }, - alignment = ham_radio.settings.hud_alignment, - number = 0xFFFFFF, - scale= { x = 100, y = 20 }, - }) -end - - -function ham_radio:update_hud_display(player) - local transmitter_signal = 0 - - local name = player:get_player_name() - local item = player:get_wielded_item() - - if item:get_name() ~= "ham_radio:receiver" then - if self.playerlocators[name] then - player:hud_change(self.playerhuds[name], "text", "") - self.playerlocators[name] = false - end - return - end - self.playerlocators[name] = true - - local meta = item:get_meta() - local frequency = meta:get_string("frequency") - - minetest.chat_send_player(player:get_player_name(), "Configured freq:"..frequency) - - if frequency ~= nil and frequency ~= "" then - local transmitter = self.read_transmitter(frequency) - -- minetest.chat_send_player(player:get_player_name(), "Found transmitter:"..minetest.serialize(transmitter)) - if transmitter.pos then - transmitter_signal = self:locate_transmitter(player, transmitter.pos) - end - end - -- local target_pos = {x=-407, y = 59, z = 70} - - --local indicator = string.rep('|', transmitter.distance)..string.rep('|', transmitter.signal)..string.rep(':', 25-(transmitter.distance + transmitter.signal)) - local indicator = string.rep('|', transmitter_signal)..string.rep(':', 20 - transmitter_signal) - - local text = "[ Frequency: "..tostring(meta:get_string("frequency")).." ]"..indicator - - player:hud_change(self.playerhuds[name], "text", text) -end - -minetest.register_on_newplayer(ham_radio.init_hud) -minetest.register_on_joinplayer(ham_radio.init_hud) - -minetest.register_on_leaveplayer(function(player) - 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) + +function ham_radio.init_hud(player) + local name = player:get_player_name() + ham_radio.playerhuds[name] = player:hud_add({ + hud_elem_type = "text", + text = "", + position = ham_radio.settings.hud_pos, + offset = { x = ham_radio.settings.hud_offset.x, y = ham_radio.settings.hud_offset.y }, + alignment = ham_radio.settings.hud_alignment, + number = 0xFFFFFF, + scale= { x = 100, y = 20 }, + }) +end + + +function ham_radio:update_hud_display(player) + local transmitter_signal = 0 + + local name = player:get_player_name() + local item = player:get_wielded_item() + + if item:get_name() ~= "ham_radio:receiver" then + if self.playerlocators[name] then + player:hud_change(self.playerhuds[name], "text", "") + self.playerlocators[name] = false + end + return + end + self.playerlocators[name] = true + + local meta = item:get_meta() + local frequency = meta:get_string("frequency") + + minetest.chat_send_player(player:get_player_name(), "Configured freq:"..frequency) + + if frequency ~= nil and frequency ~= "" then + local transmitter = self.read_transmitter(frequency) + -- minetest.chat_send_player(player:get_player_name(), "Found transmitter:"..minetest.serialize(transmitter)) + if transmitter.pos then + transmitter_signal = self:locate_transmitter(player, transmitter.pos) + end + end + -- local target_pos = {x=-407, y = 59, z = 70} + + --local indicator = string.rep('|', transmitter.distance)..string.rep('|', transmitter.signal)..string.rep(':', 25-(transmitter.distance + transmitter.signal)) + local indicator = string.rep('|', transmitter_signal)..string.rep(':', 20 - transmitter_signal) + + local text = "[ Frequency: "..tostring(meta:get_string("frequency")).." ]"..indicator + + player:hud_change(self.playerhuds[name], "text", text) +end + +minetest.register_on_newplayer(ham_radio.init_hud) +minetest.register_on_joinplayer(ham_radio.init_hud) + +minetest.register_on_leaveplayer(function(player) + 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) diff --git a/init.lua b/init.lua index 2dac7c2..37d234d 100644 --- a/init.lua +++ b/init.lua @@ -4,29 +4,29 @@ local mod_storage = minetest.get_mod_storage() ham_radio = rawget(_G, "ham_radio") or {} ham_radio = { - playerhuds = {}, - playerlocators = {}, - settings = { - hud_pos = { x = 0.45, y = 0.7 }, - hud_offset = { x = 15, y = 15 }, - hud_alignment = { x = 1, y = 0 } - } + playerhuds = {}, + playerlocators = {}, + settings = { + hud_pos = { x = 0.45, y = 0.7 }, + hud_offset = { x = 15, y = 15 }, + hud_alignment = { x = 1, y = 0 } + } } function ham_radio.save_transmitter(frequency, transmitter_data) - mod_storage:set_string(tostring(frequency), minetest.write_json(transmitter_data)) + mod_storage:set_string(tostring(frequency), minetest.write_json(transmitter_data)) 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) - end - return {} + local transmitter_data = mod_storage:get_string(tostring(frequency)) + if transmitter_data ~= nil and transmitter_data ~= "" then + return minetest.parse_json(transmitter_data) + end + return {} end function ham_radio.delete_transmitter(frequency) - mod_storage:set_string(tostring(frequency), nil) + mod_storage:set_string(tostring(frequency), nil) end dofile(modpath.."/transmitter.lua") @@ -37,5 +37,3 @@ dofile(modpath.."/hud.lua") -- TODO: configure transmitter -- TODO: craft pelengator -- TODO: set pelengator frequency - - diff --git a/receiver.lua b/receiver.lua index 8b1bcb3..1bf193a 100644 --- a/receiver.lua +++ b/receiver.lua @@ -1,64 +1,63 @@ --- minetest.chat_send_player(user:get_player_name(), "Itemstack"..meta:get_string("frequency").."^"..itemstack:get_name()) - - -minetest.register_tool("ham_radio:receiver", { - description = "Ham Radio Receiver", - wield_image = "ham_radio_receiver_wield.png", - inventory_image = "ham_radio_receiver_wield.png", - groups = { disable_repair = 1 }, - on_use = function(itemstack, user, pointed_thing) - local meta = itemstack:get_meta() - local frequency = meta:get_string("frequency") - minetest.show_formspec(user:get_player_name(), "ham_radio:configure_receiver", - "size[4,3]".. - "field[0.25,0.25;3,1;frequency;Frequency;"..tostring(frequency).."]".. - "button_exit[0.25,1.5;3,1;;Done]" - ) - return itemstack - end, -}) - -minetest.register_on_player_receive_fields(function(player, formname, fields) - if formname ~= "ham_radio:configure_receiver" or not minetest.is_player(player) then - return false - end - if fields.frequency == "" or fields.frequency == nil then - return false - end - 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 - return true -end) - - -minetest.register_craft({ - output = "ham_radio:receiver", - recipe = { - {"default:glass"}, - {"default:steel_ingot"}, - {"default:glass"}, - } -}) - -function ham_radio:locate_transmitter(player, transmitter_pos) - local player_pos = player:get_pos() - local player_look_vector = player:get_look_dir() - local player_direction = vector.add(player_pos, player_look_vector) - - local distance = vector.distance(player_pos, transmitter_pos) - - -- local distance_to_target = 13 - math.floor(math.log(distance*30)) - local distance_to_target = 24 - math.floor(2 * math.log(distance*10)) - - local distance2 = vector.distance(player_direction, transmitter_pos) - local signal_power = 1 - ((1 + distance2 - distance) / 2) - - return math.floor(distance_to_target * signal_power); - - --return { - -- distance = distance_to_target, - -- signal = signal_power - -- } -end \ No newline at end of file +-- minetest.chat_send_player(user:get_player_name(), "Itemstack"..meta:get_string("frequency").."^"..itemstack:get_name()) + + +minetest.register_tool("ham_radio:receiver", { + description = "Ham Radio Receiver", + wield_image = "ham_radio_receiver_wield.png", + inventory_image = "ham_radio_receiver_wield.png", + groups = { disable_repair = 1 }, + on_use = function(itemstack, user, pointed_thing) + local meta = itemstack:get_meta() + local frequency = meta:get_string("frequency") + minetest.show_formspec(user:get_player_name(), "ham_radio:configure_receiver", + "size[4,3]".. + "field[0.25,0.25;3,1;frequency;Frequency;"..tostring(frequency).."]".. + "button_exit[0.25,1.5;3,1;;Done]" + ) + return itemstack + end, +}) + +minetest.register_on_player_receive_fields(function(player, formname, fields) + if formname ~= "ham_radio:configure_receiver" or not minetest.is_player(player) then + return false + end + if fields.frequency == "" or fields.frequency == nil then + return false + end + 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 + return true +end) + +minetest.register_craft({ + output = "ham_radio:receiver", + recipe = { + {"default:glass"}, + {"default:steel_ingot"}, + {"default:glass"}, + } +}) + +function ham_radio:locate_transmitter(player, transmitter_pos) + local player_pos = player:get_pos() + local player_look_vector = player:get_look_dir() + local player_direction = vector.add(player_pos, player_look_vector) + + local distance = vector.distance(player_pos, transmitter_pos) + + -- local distance_to_target = 13 - math.floor(math.log(distance*30)) + local distance_to_target = 24 - math.floor(2 * math.log(distance*10)) + + local distance2 = vector.distance(player_direction, transmitter_pos) + local signal_power = 1 - ((1 + distance2 - distance) / 2) + + return math.floor(distance_to_target * signal_power); + + --return { + -- distance = distance_to_target, + -- signal = signal_power + -- } +end diff --git a/transmitter.lua b/transmitter.lua index 506e263..4365c12 100644 --- a/transmitter.lua +++ b/transmitter.lua @@ -1,51 +1,50 @@ - -minetest.register_craft({ - output = "ham_radio:transmitter", - recipe = { - {"default:glass"}, - {"default:mese"}, - {"default:steel_ingot"}, - } -}) - -minetest.register_node("ham_radio:transmitter", { - description = "Ham Radio Transmitter", - tiles = {"ham_radio_transmitter_top.png", "ham_radio_transmitter.png"}, - groups = {cracky=2,oddly_breakable_by_hand=2}, - sounds = default.node_sound_metal_defaults(), - light_source = 5, - on_construct = function(pos) - local meta = minetest.get_meta(pos) - meta:set_string( - "formspec", - "size[6,8]".. - "field[0.25,0.25;3,1;frequency;Frequency;${frequency}]".. - "button_exit[1.5,7.5;3,1;;Done]" - ) - meta:set_string("infotext", '') - end, - on_receive_fields = function(pos, formname, fields, sender) - if fields.quit ~= "true" then - return - end - if fields.frequency ~= nil and fields.frequency ~= '' then - 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, { - pos = pos, - broadcast_message = "Test Ham Radio Broadcast!" - }) - end - end, - can_dig = function(pos,player) - local meta = minetest.get_meta(pos); - local inv = meta:get_inventory() - local name = player:get_player_name() - 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) - end - } -); \ No newline at end of file + +minetest.register_craft({ + output = "ham_radio:transmitter", + recipe = { + {"default:glass"}, + {"default:mese"}, + {"default:steel_ingot"}, + } +}) + +minetest.register_node("ham_radio:transmitter", { + description = "Ham Radio Transmitter", + tiles = {"ham_radio_transmitter_top.png", "ham_radio_transmitter.png"}, + groups = {cracky=2,oddly_breakable_by_hand=2}, + sounds = default.node_sound_metal_defaults(), + light_source = 5, + on_construct = function(pos) + local meta = minetest.get_meta(pos) + meta:set_string( + "formspec", + "size[6,8]".. + "field[0.25,0.25;3,1;frequency;Frequency;${frequency}]".. + "button_exit[1.5,7.5;3,1;;Done]" + ) + meta:set_string("infotext", '') + end, + on_receive_fields = function(pos, formname, fields, sender) + if fields.quit ~= "true" then + return + end + if fields.frequency ~= nil and fields.frequency ~= '' then + 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, { + pos = pos, + broadcast_message = "Test Ham Radio Broadcast!" + }) + end + end, + can_dig = function(pos,player) + local meta = minetest.get_meta(pos); + local inv = meta:get_inventory() + local name = player:get_player_name() + 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) + end +});