initial commit

This commit is contained in:
Serhii Mozhaiskyi 2019-12-06 13:58:49 +00:00
commit bb03442083
14 changed files with 279 additions and 0 deletions

52
craft_recipes.txt Normal file

@ -0,0 +1,52 @@
Circuit
=======
- basic_materials:ic
- default:mese_crystal_fragment
Body
=======
- basic_materials:plastic_sheet
- default:steel_ingot
Wires
=======
- basic_materials:copper_wire
- default:copper_ingot
Antenna
=======
- technic:copper_coil
wires
Glass
======
- default:glass
Battery
======
- technic:battery
- basic_materials:simple_energy_crystal
- default:mese_crystal
------------------
Receiver
-, antenna, -
body, circuit, body
body, battery, body
-------------------
Transmitter
wires, antenna, wires
glass, circuit, glass
body, battery, body
-------------------
Beacon
wires, circuit, wires
body, battery, body

1
depends.txt Normal file

@ -0,0 +1 @@
default

70
hud.lua Normal file

@ -0,0 +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)

41
init.lua Normal file

@ -0,0 +1,41 @@
local modpath = minetest.get_modpath("ham_radio")
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 }
}
}
function ham_radio.save_transmitter(frequency, 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 {}
end
function ham_radio.delete_transmitter(frequency)
mod_storage:set_string(tostring(frequency), nil)
end
dofile(modpath.."/transmitter.lua")
dofile(modpath.."/receiver.lua")
dofile(modpath.."/hud.lua")
-- TODO: craft transmitter
-- TODO: configure transmitter
-- TODO: craft pelengator
-- TODO: set pelengator frequency

64
receiver.lua Normal file

@ -0,0 +1,64 @@
-- 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

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 344 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

51
transmitter.lua Normal file

@ -0,0 +1,51 @@
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
}
);