2019-12-08 13:46:12 +01:00
|
|
|
minetest.register_tool("ham_radio:handheld_receiver", {
|
2019-12-08 15:50:18 +01:00
|
|
|
description = "Handheld Radio Receiver",
|
2019-12-08 13:46:12 +01:00
|
|
|
wield_image = "ham_radio_receiver_handheld.png",
|
|
|
|
inventory_image = "ham_radio_receiver_handheld.png",
|
2019-12-06 15:25:03 +01:00
|
|
|
groups = { disable_repair = 1 },
|
2019-12-08 17:42:40 +01:00
|
|
|
-- left click - change frequency
|
2019-12-06 15:25:03 +01:00
|
|
|
on_use = function(itemstack, user, pointed_thing)
|
|
|
|
local meta = itemstack:get_meta()
|
|
|
|
local frequency = meta:get_string("frequency")
|
2019-12-08 13:46:12 +01:00
|
|
|
minetest.show_formspec(user:get_player_name(), "ham_radio:configure_handheld_receiver",
|
2019-12-07 21:14:01 +01:00
|
|
|
table.concat({
|
|
|
|
"size[3,4]",
|
2019-12-08 13:46:12 +01:00
|
|
|
"image[1,0;1,1;ham_radio_receiver_handheld.png]",
|
2019-12-07 21:14:01 +01:00
|
|
|
"field[0.25,2;3,1;frequency;Frequency;",tostring(frequency),"]",
|
|
|
|
"tooltip[frequency;Integer number ",
|
|
|
|
ham_radio.settings.frequency.min,"-",
|
|
|
|
ham_radio.settings.frequency.max, "]",
|
|
|
|
"button_exit[0,3.5;3,1;;Done]"
|
|
|
|
},'')
|
2019-12-06 15:25:03 +01:00
|
|
|
)
|
|
|
|
return itemstack
|
|
|
|
end,
|
2019-12-08 17:42:40 +01:00
|
|
|
-- right click - RDS on/off
|
|
|
|
on_secondary_use = function(itemstack, user, pointed_thing)
|
|
|
|
local meta = itemstack:get_meta()
|
2019-12-08 18:07:18 +01:00
|
|
|
local is_rds_disabled = meta:get_string("rds_disabled")
|
|
|
|
if is_rds_disabled == "" then
|
|
|
|
meta:set_string("rds_disabled", "true")
|
2019-12-08 17:42:40 +01:00
|
|
|
else
|
2019-12-08 18:07:18 +01:00
|
|
|
meta:set_string("rds_disabled", "")
|
2019-12-08 17:42:40 +01:00
|
|
|
end
|
|
|
|
return itemstack
|
|
|
|
end
|
2019-12-06 15:25:03 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
2019-12-08 13:46:12 +01:00
|
|
|
if formname ~= "ham_radio:configure_handheld_receiver" or not minetest.is_player(player) then
|
2019-12-06 15:25:03 +01:00
|
|
|
return false
|
|
|
|
end
|
2019-12-07 20:24:49 +01:00
|
|
|
if not ham_radio.validate_frequency(fields.frequency) then
|
2019-12-06 15:25:03 +01:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
local item = player:get_wielded_item()
|
|
|
|
local meta = item:get_meta()
|
|
|
|
meta:set_string("frequency", fields.frequency)
|
2019-12-08 19:27:24 +01:00
|
|
|
-- play radio sound
|
|
|
|
ham_radio.play_tuning_sound(player)
|
2019-12-07 21:54:19 +01:00
|
|
|
-- replace wielded item with new metadata
|
|
|
|
player:set_wielded_item(item)
|
2019-12-08 18:07:18 +01:00
|
|
|
-- reset rds messages
|
|
|
|
ham_radio.player_rds[player:get_player_name()] = nil
|
2019-12-06 15:25:03 +01:00
|
|
|
return true
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2019-12-07 13:58:47 +01:00
|
|
|
local coeff = 0.9
|
|
|
|
local distance_to_target = 0
|
2019-12-06 15:25:03 +01:00
|
|
|
|
2019-12-07 13:58:47 +01:00
|
|
|
local distance = vector.distance(player_pos, transmitter_pos)
|
|
|
|
if distance < 3 then
|
|
|
|
distance_to_target = 100
|
|
|
|
coeff = 0.99
|
|
|
|
else
|
|
|
|
distance_to_target = -0.0000000001*math.pow(distance,3)+0.00000145*math.pow(distance,2)-0.03*distance+100
|
|
|
|
if distance_to_target < 3 then
|
|
|
|
distance_to_target = 3
|
|
|
|
end
|
|
|
|
end
|
2019-12-06 15:25:03 +01:00
|
|
|
|
|
|
|
local distance2 = vector.distance(player_direction, transmitter_pos)
|
2019-12-07 13:58:47 +01:00
|
|
|
local signal_power = distance - distance2;
|
2019-12-06 15:25:03 +01:00
|
|
|
|
2019-12-07 13:58:47 +01:00
|
|
|
-- 0-100
|
|
|
|
return distance_to_target * coeff + distance_to_target * (1 - coeff) * signal_power;
|
2019-12-06 15:25:03 +01:00
|
|
|
end
|