Use mod storage instead of LBMs to revover wireless and teleporter tables

This commit is contained in:
upsilon 2017-04-17 11:17:09 +02:00
parent 79aa35bc62
commit 3a6a2e616c
No known key found for this signature in database
GPG Key ID: A80DAE1F266E1C3C
2 changed files with 35 additions and 29 deletions

@ -1,15 +1,26 @@
local teleporters = {} local teleporters
local teleporters_rids = {} local teleporters_rids
local storage = minetest.get_mod_storage()
teleporters = minetest.deserialize(storage:get_string("teleporters")) or {}
teleporters_rids = minetest.deserialize(storage:get_string("teleporters_rids")) or {}
jammers = minetest.deserialize(storage:get_string("jammers")) or {}
local function update_mod_storage()
storage:set_string("teleporters", minetest.serialize(teleporters))
storage:set_string("teleporters_rids", minetest.serialize(teleporters_rids))
end
local register = function(pos) local function register(pos)
if not vector.get_data_from_pos(teleporters_rids, pos.z,pos.y,pos.x) then if not vector.get_data_from_pos(teleporters_rids, pos.z,pos.y,pos.x) then
table.insert(teleporters, pos) table.insert(teleporters, pos)
vector.set_data_to_pos(teleporters_rids, pos.z,pos.y,pos.x, #teleporters) vector.set_data_to_pos(teleporters_rids, pos.z,pos.y,pos.x, #teleporters)
update_mod_storage()
end end
end end
local teleport_nearest = function(pos) local function teleport_nearest(pos)
local MAX_TELEPORTATION_DISTANCE = tonumber(minetest.setting_get("moremesecons_teleporter.max_t2t_distance")) or 50 local MAX_TELEPORTATION_DISTANCE = tonumber(minetest.setting_get("moremesecons_teleporter.max_t2t_distance")) or 50
if MAX_TELEPORTATION_DISTANCE <= 0 then if MAX_TELEPORTATION_DISTANCE <= 0 then
MAX_TELEPORTATION_DISTANCE = 1 MAX_TELEPORTATION_DISTANCE = 1
@ -94,14 +105,7 @@ minetest.register_node("moremesecons_teleporter:teleporter", {
if RID then if RID then
table.remove(teleporters, RID) table.remove(teleporters, RID)
vector.remove_data_from_pos(teleporters_rids, pos.z,pos.y,pos.x) vector.remove_data_from_pos(teleporters_rids, pos.z,pos.y,pos.x)
update_mod_storage()
end end
end, end,
}) })
minetest.register_lbm({
name = "moremesecons_teleporter:add_teleporter",
nodenames = {"moremesecons_teleporter:teleporter"},
run_at_every_load = true,
action = register
})

@ -1,5 +1,17 @@
local wireless = {} local wireless
local wireless_rids = {} local wireless_rids
local jammers
local storage = minetest.get_mod_storage()
wireless = minetest.deserialize(storage:get_string("wireless")) or {}
wireless_rids = minetest.deserialize(storage:get_string("wireless_rids")) or {}
jammers = minetest.deserialize(storage:get_string("jammers")) or {}
local function update_mod_storage()
storage:set_string("wireless", minetest.serialize(wireless))
storage:set_string("wireless_rids", minetest.serialize(wireless_rids))
storage:set_string("jammers", minetest.serialize(jammers))
end
-- localize these functions with small names because they work fairly fast -- localize these functions with small names because they work fairly fast
local get = vector.get_data_from_pos local get = vector.get_data_from_pos
@ -14,6 +26,7 @@ local function register_RID(pos)
local RID = #wireless+1 local RID = #wireless+1
wireless[RID] = pos wireless[RID] = pos
set(wireless_rids, pos.z,pos.y,pos.x, RID) set(wireless_rids, pos.z,pos.y,pos.x, RID)
update_mod_storage()
end end
local is_jammed local is_jammed
@ -88,7 +101,8 @@ minetest.register_node("moremesecons_wireless:wireless", {
local RID = get(wireless_rids, pos.z,pos.y,pos.x) local RID = get(wireless_rids, pos.z,pos.y,pos.x)
if RID then if RID then
table.remove(wireless, RID) table.remove(wireless, RID)
vector.remove_data_from_pos(wireless_rids, pos.z,pos.y,pos.x) remove(wireless_rids, pos.z,pos.y,pos.x)
update_mod_storage()
end end
mesecon.receptor_off(pos) mesecon.receptor_off(pos)
end, end,
@ -106,10 +120,12 @@ local function add_jammer(pos)
return return
end end
set(jammers, pos.z,pos.y,pos.x, true) set(jammers, pos.z,pos.y,pos.x, true)
update_mod_storage()
end end
local function remove_jammer(pos) local function remove_jammer(pos)
remove(jammers, pos.z,pos.y,pos.x) remove(jammers, pos.z,pos.y,pos.x)
update_mod_storage()
end end
-- looks big, but should work fast -- looks big, but should work fast
@ -216,17 +232,3 @@ minetest.register_craft({
{"group:mesecon_conductor_craftable", "", "group:mesecon_conductor_craftable"}, {"group:mesecon_conductor_craftable", "", "group:mesecon_conductor_craftable"},
} }
}) })
minetest.register_lbm({
name = "moremesecons_wireless:add_jammer",
nodenames = {"moremesecons_wireless:jammer_on"},
run_at_every_load = true,
action = add_jammer
})
minetest.register_lbm({
name = "moremesecons_wireless:add_wireless",
nodenames = {"moremesecons_wireless:wireless"},
run_at_every_load = true,
action = register_RID
})