basic_protect/init.lua

101 lines
3.5 KiB
Lua
Raw Normal View History

2016-06-13 15:05:26 -07:00
--Basic protect by rnd, 2016
local protector = {};
protector.radius = 20; -- 20x20x20 area
protector.cache = {};
2016-06-14 00:36:04 -07:00
local round = math.floor;
2016-06-13 15:05:26 -07:00
local old_is_protected = minetest.is_protected
function minetest.is_protected(pos, digger)
local r = protector.radius;
2016-06-14 00:36:04 -07:00
local p = {x=round(pos.x/r+0.5)*r,y=round(pos.y/r+0.5)*r,z=round(pos.z/r+0.5)*r}
2016-06-13 15:05:26 -07:00
if not protector.cache[digger] then -- cache current check for faster future lookups
protector.cache[digger] = p;
else
local p0 = protector.cache[digger];
if (p0.x==p.x and p0.y==p.y and p0.z==p.z) then -- already checked, just lookup
return protector.cache[digger].is_protected
else
2016-06-14 03:46:09 -07:00
--minetest.chat_send_all(digger .. " " .. minetest.pos_to_string(p0) .. " : " .. minetest.pos_to_string(p))
protector.cache[digger] = {x=p.x,y=p.y,z=p.z, is_protected = false}; -- refresh cache
2016-06-13 15:05:26 -07:00
end
end
if minetest.get_node(p).name == "basic_protect:protector" then
local meta = minetest.get_meta(p);
local owner = meta:get_string("owner");
if digger~=owner then
minetest.chat_send_player(digger, "area owned by " .. owner);
protector.cache[digger].is_protected = true;
return true
end
end
protector.cache[digger].is_protected = old_is_protected(pos, digger);
return protector.cache[digger].is_protected;
end
minetest.register_node("basic_protect:protector", {
description = "Protects a rectangle area of size " .. protector.radius,
tiles = {"basic_protector.png"},
groups = {oddly_breakable_by_hand=2},
sounds = default.node_sound_wood_defaults(),
on_place = function(itemstack, placer, pointed_thing)
--after_place_node = function(pos, placer)
local pos = pointed_thing.under;
local name = placer:get_player_name();
local r = protector.radius;
2016-06-14 00:36:04 -07:00
local p = {x=round(pos.x/r+0.5)*r,y=round(pos.y/r+0.5)*r,z=round(pos.z/r+0.5)*r}
2016-06-13 15:05:26 -07:00
if minetest.get_node(p).name == "basic_protect:protector" then
minetest.chat_send_player(name,"area already protected at " .. minetest.pos_to_string(p));
return nil
end
minetest.set_node(p, {name = "basic_protect:protector"});
local meta = minetest.get_meta(p);meta:set_string("owner",name);
2016-06-14 00:36:04 -07:00
minetest.chat_send_player(name, "#protector: protected new area (" .. p.x .. "," .. p.y .. "," .. p.z .. ") + radius " .. 0.5*protector.radius .. " around");
2016-06-13 15:05:26 -07:00
meta:set_string("infotext", "property of " .. name);
protector.cache = {}; -- reset cache
itemstack:take_item(); return itemstack
end,
2016-06-14 00:36:04 -07:00
on_punch = function(pos, node, puncher, pointed_thing) -- for unknown reason texture is unknown
-- local meta = minetest.get_meta(pos);local owner = meta:get_string("owner");
-- if owner == puncher:get_player_name() then
-- minetest.add_entity({x=pos.x-0.5,y=pos.y-0.5,z=pos.z-0.5}, "basic_protect:display")
-- end
2016-06-13 15:05:26 -07:00
end
});
minetest.register_entity("basic_protect:display", {
physical = false,
collisionbox = {0, 0, 0, 0, 0, 0},
visual = "wielditem",
-- wielditem seems to be scaled to 1.5 times original node size
2016-06-13 15:18:11 -07:00
visual_size = {x = 1.29*protector.radius/21, y = 1.29*protector.radius/21},
2016-06-13 15:05:26 -07:00
timer = 0,
on_activate = function(self, staticdata)
self.timer = 0;
2016-06-14 00:36:04 -07:00
self.object:set_properties({textures={"area_display.png"}})
2016-06-13 15:05:26 -07:00
end,
on_step = function(self, dtime)
self.timer = self.timer + dtime
if self.timer > 20 then
self.object:remove()
end
end,
})
minetest.register_craft({
output = "basic_protect:protector",
recipe = {
{"default:stone", "default:stone","default:stone"},
{"default:stone", "default:steel_ingot","default:stone"},
{"default:stone", "default:stone", "default:stone"}
}
})