mirror of
https://github.com/minetest-mods/MoreMesecons.git
synced 2025-01-09 01:10:22 +01:00
change a bit code
This commit is contained in:
parent
7877d8ac92
commit
708c8b045a
@ -2,8 +2,7 @@
|
||||
-- Detects players in a certain radius
|
||||
-- The radius can be changes by right-click (by default 6)
|
||||
|
||||
local object_detector_make_formspec = function (pos)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local function make_formspec(meta)
|
||||
meta:set_string("formspec", "size[9,5]" ..
|
||||
"field[0.3, 0;9,2;scanname;Name of player to scan for (empty for any):;${scanname}]"..
|
||||
"field[0.3,1.5;4,2;digiline_channel;Digiline Channel (optional):;${digiline_channel}]"..
|
||||
@ -11,26 +10,41 @@ local object_detector_make_formspec = function (pos)
|
||||
"button_exit[3.5,3.5;2,3;;Save]")
|
||||
end
|
||||
|
||||
local object_detector_on_receive_fields = function(pos, formname, fields)
|
||||
if not fields.scanname or not fields.digiline_channel then return end;
|
||||
local function object_detector_make_formspec(pos)
|
||||
make_formspec(minetest.get_meta(pos))
|
||||
end
|
||||
|
||||
local function object_detector_on_receive_fields(pos, formname, fields)
|
||||
if not fields.scanname
|
||||
or not fields.digiline_channel then
|
||||
return
|
||||
end
|
||||
|
||||
local meta = minetest.get_meta(pos)
|
||||
meta:set_string("scanname", fields.scanname)
|
||||
meta:set_string("digiline_channel", fields.digiline_channel)
|
||||
if tonumber(fields.radius) then meta:set_int("radius", tonumber(fields.radius)) end
|
||||
object_detector_make_formspec(pos)
|
||||
local r = tonumber(fields.radius)
|
||||
if r then
|
||||
meta:set_int("radius", r)
|
||||
end
|
||||
if not meta:get_string("formspec") then
|
||||
make_formspec(meta)
|
||||
end
|
||||
end
|
||||
|
||||
-- returns true if player was found, false if not
|
||||
local object_detector_scan = function (pos)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local scanname = meta:get_string("scanname")
|
||||
local scan_all = scanname == ""
|
||||
local radius = meta:get_int("radius")
|
||||
if radius == 0 then radius = 6 end
|
||||
local objs = minetest.get_objects_inside_radius(pos, radius)
|
||||
for k, obj in pairs(objs) do
|
||||
if radius == 0 then
|
||||
radius = 6
|
||||
end
|
||||
for _,obj in pairs(minetest.get_objects_inside_radius(pos, radius)) do
|
||||
local isname = obj:get_player_name() -- "" is returned if it is not a player; "" ~= nil!
|
||||
local scanname = meta:get_string("scanname")
|
||||
if (isname == scanname and isname ~= "") or (isname ~= "" and scanname == "") then -- player with scanname found or not scanname specified
|
||||
if isname ~= ""
|
||||
and (scan_all or isname == scanname) then -- player with scanname found or not scanname specified
|
||||
return true
|
||||
end
|
||||
end
|
||||
@ -43,9 +57,12 @@ local object_detector_digiline = {
|
||||
action = function (pos, node, channel, msg)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local active_channel = meta:get_string("digiline_channel")
|
||||
if channel == active_channel then
|
||||
meta:set_string("scanname", msg)
|
||||
object_detector_make_formspec(pos)
|
||||
if channel ~= active_channel then
|
||||
return
|
||||
end
|
||||
meta:set_string("scanname", msg)
|
||||
if not meta:get_string("formspec") then
|
||||
make_formspec(meta)
|
||||
end
|
||||
end,
|
||||
}
|
||||
|
@ -1,51 +1,57 @@
|
||||
local kill_nearest_player = function(pos)
|
||||
local MAX_DISTANCE = 8 -- Use this number to set maximal distance to kill
|
||||
|
||||
|
||||
-- Search the nearest player
|
||||
local nearest = nil
|
||||
local min_distance = math.huge
|
||||
local players = minetest.get_connected_players()
|
||||
for index, player in pairs(players) do
|
||||
local nearest
|
||||
local min_distance = MAX_DISTANCE
|
||||
for index, player in pairs(minetest.get_connected_players()) do
|
||||
local distance = vector.distance(pos, player:getpos())
|
||||
if distance < min_distance then
|
||||
if distance < MAX_DISTANCE
|
||||
and distance < min_distance then
|
||||
min_distance = distance
|
||||
nearest = player
|
||||
end
|
||||
end
|
||||
|
||||
-- And kill him
|
||||
meta = minetest.get_meta(pos)
|
||||
owner = meta:get_string("owner")
|
||||
if owner then
|
||||
if vector.distance(pos, nearest:getpos()) < MAX_DISTANCE and owner ~= nearest:get_player_name() then
|
||||
nearest:set_hp(0)
|
||||
end
|
||||
|
||||
if not nearest then
|
||||
-- no nearby player
|
||||
return
|
||||
end
|
||||
|
||||
local owner = minetest.get_meta(pos):get_string("owner")
|
||||
if not owner then
|
||||
-- maybe some mod placed it
|
||||
return
|
||||
end
|
||||
|
||||
-- And kill him
|
||||
nearest:set_hp(0)
|
||||
end
|
||||
|
||||
minetest.register_craft({
|
||||
output = "moremesecons_playerkiller:playerkiller 1",
|
||||
output = "moremesecons_playerkiller:playerkiller",
|
||||
recipe = { {"","default:apple",""},
|
||||
{"default:apple","mesecons_detector:object_detector_off","default:apple"},
|
||||
{"","default:apple",""}}
|
||||
})
|
||||
|
||||
minetest.register_node("moremesecons_playerkiller:playerkiller", {
|
||||
tiles = {"moremesecons_playerkiller_top.png", "moremesecons_playerkiller_top.png", "moremesecons_playerkiller_side.png", "moremesecons_playerkiller_side.png", "moremesecons_playerkiller_side.png", "moremesecons_playerkiller_side.png"},
|
||||
description = "Player Killer",
|
||||
tiles = {"moremesecons_playerkiller_top.png", "moremesecons_playerkiller_top.png", "moremesecons_playerkiller_side.png"},
|
||||
paramtype = "light",
|
||||
walkable = true,
|
||||
groups = {cracky=3},
|
||||
description="Player Killer",
|
||||
mesecons = {effector = {
|
||||
state = mesecon.state.off,
|
||||
action_on = kill_nearest_player
|
||||
}},
|
||||
after_place_node = function(pos, placer)
|
||||
meta = minetest.get_meta(pos)
|
||||
if placer then
|
||||
local meta = minetest.get_meta(pos)
|
||||
meta:set_string("owner", placer:get_player_name())
|
||||
meta:set_string("infotext", "PlayerKiller owned by " .. meta:get_string("owner"))
|
||||
if not placer then
|
||||
return
|
||||
end
|
||||
local meta = minetest.get_meta(pos)
|
||||
meta:set_string("owner", placer:get_player_name())
|
||||
meta:set_string("infotext", "PlayerKiller owned by " .. meta:get_string("owner"))
|
||||
end,
|
||||
sounds = default.node_sound_stone_defaults(),
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user