2015-09-30 18:09:51 +02:00
|
|
|
-- see wireless jammer
|
2017-06-23 13:47:41 +02:00
|
|
|
local storage = minetest.get_mod_storage()
|
|
|
|
|
2020-04-13 19:30:15 +02:00
|
|
|
local jammers = moremesecons.load_MapDataStorage_legacy(storage, "jammers_v2",
|
|
|
|
"jammers")
|
2017-04-19 11:23:08 +02:00
|
|
|
|
|
|
|
local function update_mod_storage()
|
2020-04-13 19:30:15 +02:00
|
|
|
storage:set_string("jammers_v2", jammers:serialize())
|
2017-04-19 11:23:08 +02:00
|
|
|
end
|
|
|
|
|
2015-09-30 18:09:51 +02:00
|
|
|
local function add_jammer(pos)
|
2020-04-13 19:30:15 +02:00
|
|
|
if jammers:getAt(pos) then
|
2015-09-30 18:09:51 +02:00
|
|
|
return
|
|
|
|
end
|
2020-04-13 19:30:15 +02:00
|
|
|
jammers:setAt(pos, true)
|
2017-04-19 11:23:08 +02:00
|
|
|
update_mod_storage()
|
2015-09-30 18:09:51 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function remove_jammer(pos)
|
2020-04-13 19:30:15 +02:00
|
|
|
jammers:removeAt(pos)
|
2017-04-19 11:23:08 +02:00
|
|
|
update_mod_storage()
|
2015-09-30 18:09:51 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function is_jammed(pos)
|
2017-04-19 11:08:58 +02:00
|
|
|
local JAMMER_MAX_DISTANCE = moremesecons.setting("jammer", "max_distance", 10, 1)
|
2017-02-11 14:01:20 +01:00
|
|
|
|
2020-04-13 19:30:15 +02:00
|
|
|
local minp = vector.subtract(pos, JAMMER_MAX_DISTANCE)
|
|
|
|
local maxp = vector.add(pos, JAMMER_MAX_DISTANCE)
|
|
|
|
for p in jammers:iter(minp, maxp) do
|
|
|
|
local d = vector.subtract(pos, p)
|
|
|
|
if d.x ^ 2 + d.y ^ 2 + d.z ^ 2 <= JAMMER_MAX_DISTANCE^2 then
|
|
|
|
return true
|
2015-09-30 18:09:51 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2015-12-23 17:14:55 +01:00
|
|
|
minetest.after(0, function() -- After loading all mods, override some functions
|
|
|
|
local jammed
|
2015-09-11 18:20:19 +02:00
|
|
|
|
2017-04-19 11:31:33 +02:00
|
|
|
local actual_node_get = mesecon.get_node_force
|
2015-12-23 14:36:07 +01:00
|
|
|
local function temp_node_get(pos, ...)
|
2015-12-23 17:14:55 +01:00
|
|
|
local node = actual_node_get(pos, ...)
|
|
|
|
if jammed == nil
|
|
|
|
and node then
|
|
|
|
jammed = is_jammed(pos)
|
|
|
|
end
|
|
|
|
return node
|
2015-12-23 14:36:07 +01:00
|
|
|
end
|
2015-09-11 18:20:19 +02:00
|
|
|
|
2015-12-23 14:36:07 +01:00
|
|
|
local actual_is_conductor_off = mesecon.is_conductor_off
|
2015-12-23 17:14:55 +01:00
|
|
|
local function temp_is_conductor_off(...)
|
|
|
|
if jammed then
|
|
|
|
-- go to the next elseif, there's is_effector
|
2015-12-23 14:36:07 +01:00
|
|
|
return
|
|
|
|
end
|
2015-12-23 17:14:55 +01:00
|
|
|
local v = actual_is_conductor_off(...)
|
|
|
|
if v then
|
|
|
|
-- it continues to the next frontier
|
|
|
|
jammed = nil
|
|
|
|
end
|
|
|
|
return v
|
2015-12-23 14:36:07 +01:00
|
|
|
end
|
2015-09-11 18:20:19 +02:00
|
|
|
|
2015-12-23 14:36:07 +01:00
|
|
|
local actual_is_effector = mesecon.is_effector
|
2015-12-23 17:14:55 +01:00
|
|
|
local function temp_is_effector(...)
|
|
|
|
local abort_here = jammed
|
|
|
|
-- the last elseif before continuing, jammed needs to be nil then
|
|
|
|
jammed = nil
|
|
|
|
if abort_here then
|
2015-12-23 14:36:07 +01:00
|
|
|
return
|
2015-09-11 18:20:19 +02:00
|
|
|
end
|
2015-12-23 14:36:07 +01:00
|
|
|
return actual_is_effector(...)
|
2015-09-11 18:20:19 +02:00
|
|
|
end
|
2015-12-23 17:14:55 +01:00
|
|
|
|
|
|
|
local actual_turnon = mesecon.turnon
|
|
|
|
function mesecon.turnon(...)
|
|
|
|
--set those to the temporary functions
|
2017-04-19 11:31:33 +02:00
|
|
|
mesecon.get_node_force = temp_node_get
|
2015-12-23 17:14:55 +01:00
|
|
|
mesecon.is_conductor_off = temp_is_conductor_off
|
|
|
|
mesecon.is_effector = temp_is_effector
|
|
|
|
|
|
|
|
actual_turnon(...)
|
|
|
|
|
2017-04-19 11:31:33 +02:00
|
|
|
mesecon.get_node_force = actual_node_get
|
2015-12-23 17:14:55 +01:00
|
|
|
mesecon.is_conductor_off = actual_is_conductor_off
|
|
|
|
mesecon.is_effector = actual_is_effector
|
|
|
|
|
|
|
|
-- safety
|
|
|
|
jammed = nil
|
|
|
|
end
|
2015-09-11 18:20:19 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
mesecon.register_node("moremesecons_jammer:jammer", {
|
|
|
|
description = "Mesecons Jammer",
|
|
|
|
paramtype = "light",
|
|
|
|
},{
|
|
|
|
tiles = {"moremesecons_jammer_off.png"},
|
|
|
|
groups = {dig_immediate=2},
|
|
|
|
mesecons = {effector = {
|
|
|
|
action_on = function(pos)
|
2015-09-30 18:09:51 +02:00
|
|
|
add_jammer(pos)
|
2016-09-01 15:57:55 +02:00
|
|
|
minetest.sound_play("moremesecons_jammer", {pos = pos})
|
2015-09-11 18:20:19 +02:00
|
|
|
minetest.swap_node(pos, {name="moremesecons_jammer:jammer_on"})
|
2015-09-30 18:09:51 +02:00
|
|
|
end
|
|
|
|
}},
|
2015-09-11 18:20:19 +02:00
|
|
|
},{
|
|
|
|
tiles = {"moremesecons_jammer_on.png"},
|
|
|
|
groups = {dig_immediate=2, not_in_creative_inventory=1},
|
|
|
|
mesecons = {effector = {
|
|
|
|
action_off = function(pos)
|
2015-09-30 18:09:51 +02:00
|
|
|
remove_jammer(pos)
|
2015-09-11 18:20:19 +02:00
|
|
|
minetest.swap_node(pos, {name="moremesecons_jammer:jammer_off"})
|
2015-09-30 18:09:51 +02:00
|
|
|
end
|
|
|
|
}},
|
|
|
|
on_destruct = remove_jammer,
|
|
|
|
on_construct = add_jammer,
|
2015-09-11 18:20:19 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craft({
|
|
|
|
output = "moremesecons_jammer:jammer_off",
|
|
|
|
recipe = {{"group:mesecon_conductor_craftable", "default:mese", "group:mesecon_conductor_craftable"},
|
|
|
|
{"", "moremesecons_wireless:jammer_off", ""}}
|
|
|
|
})
|
2015-09-30 18:09:51 +02:00
|
|
|
|
2017-06-23 13:47:41 +02:00
|
|
|
if moremesecons.setting("jammer", "enable_lbm", false) then
|
2017-04-19 11:23:08 +02:00
|
|
|
minetest.register_lbm({
|
|
|
|
name = "moremesecons_jammer:add_jammer",
|
|
|
|
nodenames = {"moremesecons_jammer:jammer_on"},
|
|
|
|
run_at_every_load = true,
|
|
|
|
action = add_jammer
|
|
|
|
})
|
|
|
|
end
|
2018-12-31 17:26:41 +01:00
|
|
|
|
|
|
|
minetest.log("action", "[moremesecons_jammer] loaded.")
|