better data format
This commit is contained in:
parent
7c40bd0c19
commit
ee047a6266
9
hud.lua
9
hud.lua
@ -74,12 +74,11 @@ function ham_radio:update_hud_display(player)
|
||||
local name = player:get_player_name()
|
||||
local meta = player:get_wielded_item():get_meta()
|
||||
local frequency = meta:get_string("frequency")
|
||||
|
||||
|
||||
if frequency ~= nil and frequency ~= "" then
|
||||
local transmitters = self.read_transmitters(frequency)
|
||||
|
||||
for key, transmitter in pairs(transmitters) do
|
||||
local transmitter_signal = self:locate_transmitter(player, transmitter.pos)
|
||||
local transmitters = self.find_transmitters(frequency)
|
||||
for position, transmitter in pairs(transmitters) do
|
||||
local transmitter_signal = self:locate_transmitter(player, minetest.string_to_pos(position))
|
||||
if transmitter_signal > signal_power then
|
||||
-- use max power from transmitters nearby
|
||||
signal_power = transmitter_signal
|
||||
|
48
init.lua
48
init.lua
@ -11,35 +11,31 @@ ham_radio = {
|
||||
}
|
||||
}
|
||||
|
||||
function ham_radio.save_transmitter(frequency, pos, transmitter_properties)
|
||||
local transmitters = mod_storage:get_string(tostring(frequency))
|
||||
function ham_radio.save_transmitter(pos, transmitter_properties)
|
||||
mod_storage:set_string(
|
||||
minetest.pos_to_string(pos, 0),
|
||||
minetest.write_json(transmitter_properties)
|
||||
)
|
||||
end
|
||||
|
||||
function ham_radio.read_transmitter(pos)
|
||||
return mod_storage:get_string(minetest.pos_to_string(pos, 0))
|
||||
end
|
||||
|
||||
function ham_radio.find_transmitters(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
|
||||
|
||||
function ham_radio.read_transmitters(frequency)
|
||||
local transmitters = mod_storage:get_string(tostring(frequency))
|
||||
if transmitters ~= "" then
|
||||
return minetest.parse_json(transmitters)
|
||||
end
|
||||
return {}
|
||||
end
|
||||
|
||||
function ham_radio.delete_transmitter(frequency, pos)
|
||||
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))
|
||||
local all_transmitters = mod_storage:to_table().fields
|
||||
for key, transmitter_data in pairs(all_transmitters) do
|
||||
local transmitter = minetest.parse_json(transmitter_data)
|
||||
if transmitter.frequency == frequency then
|
||||
transmitter_list[key] = transmitter
|
||||
end
|
||||
end
|
||||
return transmitter_list
|
||||
end
|
||||
|
||||
function ham_radio.delete_transmitter(pos)
|
||||
mod_storage:set_string(minetest.pos_to_string(pos, 0), '')
|
||||
end
|
||||
|
||||
dofile(modpath.."/craft.lua")
|
||||
|
@ -37,10 +37,9 @@ minetest.register_node("ham_radio:transmitter", {
|
||||
meta:set_string("frequency", fields.frequency)
|
||||
meta:set_string("infotext", 'Frequency: '..fields.frequency)
|
||||
ham_radio.save_transmitter(
|
||||
fields.frequency,
|
||||
pos,
|
||||
{
|
||||
pos = pos,
|
||||
frequency = fields.frequency,
|
||||
broadcast_message = "Test Ham Radio Broadcast!",
|
||||
operated_by = meta:get_string('operated_by')
|
||||
}
|
||||
@ -54,6 +53,6 @@ minetest.register_node("ham_radio:transmitter", {
|
||||
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, pos)
|
||||
ham_radio.delete_transmitter(pos)
|
||||
end
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user