From 75cac91beab7a3ff04b64fb6ed2b56551ed6171d Mon Sep 17 00:00:00 2001 From: Vanessa Dannenberg Date: Wed, 19 Jun 2019 14:23:14 -0400 Subject: =?UTF-8?q?*=20refactor=20a=20few=20things=20*=20rename=20nodes=20?= =?UTF-8?q?to=20.."=5Flarge"=20*=20add=20smaller=20size,=20default=2015?= =?UTF-8?q?=C2=B3=20m=20volume,=20make=20large=20default=20to=2033=C2=B3?= =?UTF-8?q?=20=20=20this=20makes=20the=20large=20one=20fit=20nicely=20at?= =?UTF-8?q?=20the=20dead=20center=20of=20a=20"city=20block",=20=20=20with?= =?UTF-8?q?=205m=20wide=20roads=20and=201m=20easement,=20on=20a=2040m=20gr?= =?UTF-8?q?id.=20=20Small=20one=20fits=20well=20in=20=20=20a=20plot=201/4?= =?UTF-8?q?=20of=20that,=20i.e.=205m/1m/40m=20roads,=20with=201-3m=20wide?= =?UTF-8?q?=20alleys=20on=20a=2020m=20=20=20grid=20between=20them.=20*=20a?= =?UTF-8?q?dd=20separate=20height=20setting=20(default=20same=20as=20x/z?= =?UTF-8?q?=20"radius")=20*=20add=20coordinates=20of=20protector=20to=20de?= =?UTF-8?q?scription=20that's=20passed=20to=20`areas`=20mod=20*=20make=20s?= =?UTF-8?q?tandard=20recipe=20point=20to=20small=20size,=20gives=202=20=20?= =?UTF-8?q?=20(recipe=20for=20large=20is=20just=20small=20x=208)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- init.lua | 296 ++++++++++++++++++++---------- textures/areasprotector_large_overlay.png | Bin 0 -> 128 bytes 2 files changed, 202 insertions(+), 94 deletions(-) create mode 100644 textures/areasprotector_large_overlay.png diff --git a/init.lua b/init.lua index b2ac282..48c4b12 100644 --- a/init.lua +++ b/init.lua @@ -9,7 +9,21 @@ local function red(str) return minetest.colorize("#FF5555",str) end -local radius = minetest.setting_get("areasprotector_radius") or 8 +local radius_large = minetest.setting_get("areasprotector_radius_large") + or minetest.setting_get("areasprotector_radius") + or 16 + +local height_large = minetest.setting_get("areasprotector_height_large") + or minetest.setting_get("areasprotector_radius_large") + or minetest.setting_get("areasprotector_radius") + or 16 + +local radius_small = minetest.setting_get("areasprotector_radius_small") + or 7 + +local height_small = minetest.setting_get("areasprotector_height_small") + or minetest.setting_get("areasprotector_radius_small") + or 7 local function remove_display(pos) local objs = minetest.get_objects_inside_radius(pos, 0.5) @@ -18,136 +32,230 @@ local function remove_display(pos) end end -minetest.register_node("areasprotector:protector",{ - description = "Protector Block", +local function on_place(itemstack, player, pointed, radius, height, sizeword) + local pos = pointed.above + local pos1 = vector.add(pos,vector.new(radius, height, radius)) + local pos2 = vector.add(pos,vector.new(-radius, -height, -radius)) + local name = player:get_player_name() + local perm,err = areas:canPlayerAddArea(pos1,pos2,name) + if not perm then + minetest.chat_send_player(name,red("You are not allowed to protect that area: ")..err) + return itemstack + end + local id = areas:add(name,"Protected by Protector Block at "..minetest.pos_to_string(pos, 0),pos1,pos2) + areas:save() + local msg = string.format("The area from %s to %s has been protected as #%s",cyan(minetest.pos_to_string(pos1)),cyan(minetest.pos_to_string(pos2)),cyan(id)) + minetest.chat_send_player(name,msg) + minetest.set_node(pos,{name="areasprotector:protector_"..sizeword}) + local meta = minetest.get_meta(pos) + local infotext = string.format("Protecting area %d owned by %s",id,name) + meta:set_string("infotext",infotext) + meta:set_int("area_id",id) + meta:set_string("owner",name) + if not minetest.setting_getbool("creative_mode") then + itemstack:take_item() + end + return itemstack +end + +local function after_dig(pos, oldnode, oldmetadata, digger, sizeword) + if oldmetadata and oldmetadata.fields then + local owner = oldmetadata.fields.owner + local id = tonumber(oldmetadata.fields.area_id) + local playername = digger:get_player_name() + if areas.areas[id] and areas:isAreaOwner(id,owner) then + if digger:get_player_control().sneak then + local inv = digger:get_inventory() + if not creative_mode then + if inv:room_for_item("main", "default:steel_ingot 6") then + inv:remove_item("main", "areasprotector:protector_"..sizeword.." 1") + inv:add_item("main", "default:steel_ingot 6") + else + minetest.chat_send_player(playername, "No room for the replacement ingots, just digging the protector and deleting the area normally.") + areas:remove(id) + areas:save() + end + else + inv:remove_item("main", "areasprotector:protector_"..sizeword.." 1") + end + else + areas:remove(id) + areas:save() + end + end + end +end + +local function on_punch(pos, node, puncher, sizeword) + local objs = minetest.get_objects_inside_radius(pos,.5) -- a radius of .5 since the entity serialization seems to be not that precise + local removed = false + for _, o in pairs(objs) do + if (not o:is_player()) and o:get_luaentity().name == "areasprotector:display_"..sizeword then + o:remove() + removed = true + end + end + if not removed then -- nothing was removed: there wasn't the entity + minetest.add_entity(pos, "areasprotector:display_"..sizeword) + minetest.after(4, remove_display, pos) + end +end + +local function on_step(self, dtime, sizeword) + if minetest.get_node(self.object:getpos()).name ~= "areasprotector:protector_"..sizeword then + self.object:remove() + return + end +end + +local function make_display_nodebox(radius, height) + local nb_radius = radius + 0.55 + local nb_height = height + 0.55 + local t = { + -- sides + { -nb_radius, -nb_height, -nb_radius, -nb_radius, nb_height, nb_radius }, + { -nb_radius, -nb_height, nb_radius, nb_radius, nb_height, nb_radius }, + { nb_radius, -nb_height, -nb_radius, nb_radius, nb_height, nb_radius }, + { -nb_radius, -nb_height, -nb_radius, nb_radius, nb_height, -nb_radius }, + -- top + { -nb_radius, nb_height, -nb_radius, nb_radius, nb_height, nb_radius }, + -- bottom + { -nb_radius, -nb_height, -nb_radius, nb_radius, -nb_height, nb_radius }, + -- middle (surround protector) + {-.55,-.55,-.55, .55,.55,.55}, + } + return t +end + +local nbox = { + type = "fixed", + fixed = { -0.5, -0.5, -0.5, 0.5, 0.5, 0.5 }, +} + +minetest.register_node("areasprotector:protector_large", { + description = "Protector Block (large volume)", groups = {cracky=1}, tiles = { "default_steel_block.png", "default_steel_block.png", - "default_steel_block.png^basic_materials_padlock.png" + "default_steel_block.png^areasprotector_large_overlay.png^basic_materials_padlock.png" }, paramtype = "light", drawtype = "nodebox", - node_box = { - type = "fixed", - fixed = { -0.5, -0.5, -0.5, 0.5, 0.5, 0.5 }, + node_box = nbox, + on_place = function(itemstack, player, pointed_thing) + on_place(itemstack, player, pointed_thing, radius_large, height_large, "large") + end, + after_dig_node = function(pos, oldnode, oldmetadata, digger) + after_dig(pos, oldnode, oldmetadata, digger, "large") + end, + on_punch = function(pos, node, puncher) + on_punch(pos, node, puncher, "large") + end +}) + +minetest.register_node("areasprotector:protector_small", { + description = "Protector Block (small volume)", + groups = {cracky=1}, + tiles = { + "default_steel_block.png", + "default_steel_block.png", + "default_steel_block.png^basic_materials_padlock.png" }, - on_place = function(itemstack,player,pointed) - local pos = pointed.above - local pos1 = vector.add(pos,vector.new(radius,radius,radius)) - local pos2 = vector.add(pos,vector.new(-1*radius,-1*radius,-1*radius)) - local name = player:get_player_name() - local perm,err = areas:canPlayerAddArea(pos1,pos2,name) - if not perm then - minetest.chat_send_player(name,red("You are not allowed to protect that area: ")..err) - return itemstack - end - local id = areas:add(name,"Protected by Protector Block",pos1,pos2) - areas:save() - local msg = string.format("The area from %s to %s has been protected as #%s",cyan(minetest.pos_to_string(pos1)),cyan(minetest.pos_to_string(pos2)),cyan(id)) - minetest.chat_send_player(name,msg) - minetest.set_node(pos,{name="areasprotector:protector"}) - local meta = minetest.get_meta(pos) - local infotext = string.format("Protecting area %d owned by %s",id,name) - meta:set_string("infotext",infotext) - meta:set_int("area_id",id) - meta:set_string("owner",name) - if not minetest.setting_getbool("creative_mode") then - itemstack:take_item() - end - return itemstack + paramtype = "light", + drawtype = "nodebox", + node_box = nbox, + on_place = function(itemstack, player, pointed_thing) + on_place(itemstack, player, pointed_thing, radius_small, height_small, "small") end, after_dig_node = function(pos, oldnode, oldmetadata, digger) - if oldmetadata and oldmetadata.fields then - local owner = oldmetadata.fields.owner - local id = tonumber(oldmetadata.fields.area_id) - local playername = digger:get_player_name() - if areas.areas[id] and areas:isAreaOwner(id,owner) then - if digger:get_player_control().sneak then - local inv = digger:get_inventory() - if not creative_mode then - if inv:room_for_item("main", "default:steel_ingot 6") then - inv:remove_item("main", "areasprotector:protector 1") - inv:add_item("main", "default:steel_ingot 6") - else - minetest.chat_send_player(playername, "No room for the replacement ingots, just digging the protector and deleting the area normally.") - areas:remove(id) - areas:save() - end - else - inv:remove_item("main", "areasprotector:protector 1") - end - else - areas:remove(id) - areas:save() - end - end - end + after_dig(pos, oldnode, oldmetadata, digger, "small") end, on_punch = function(pos, node, puncher) - local objs = minetest.get_objects_inside_radius(pos,.5) -- a radius of .5 since the entity serialization seems to be not that precise - local removed = false - for _, o in pairs(objs) do - if (not o:is_player()) and o:get_luaentity().name == "areasprotector:display" then - o:remove() - removed = true - end - end - if not removed then -- nothing was removed: there wasn't the entity - minetest.add_entity(pos, "areasprotector:display") - minetest.after(4, remove_display, pos) - end + on_punch(pos, node, puncher, "small") end }) -- entities code below (and above) mostly copied-pasted from Zeg9's protector mod -minetest.register_entity("areasprotector:display", { +-- wielditem seems to be scaled to 1.5 times original node size +local vsize = {x=1.0/1.5, y=1.0/1.5} +local ecbox = {0, 0, 0, 0, 0, 0} + +minetest.register_entity("areasprotector:display_large", { physical = false, - collisionbox = {0,0,0,0,0,0}, + collisionbox = ecbox, visual = "wielditem", - visual_size = {x=1.0/1.5,y=1.0/1.5}, -- wielditem seems to be scaled to 1.5 times original node size - textures = {"areasprotector:display_node"}, + visual_size = vsize, + textures = {"areasprotector:display_node_large"}, on_step = function(self, dtime) - if minetest.get_node(self.object:getpos()).name ~= "areasprotector:protector" then - self.object:remove() - return - end - end, + on_step(self, dtime, "large") + end +}) + +minetest.register_entity("areasprotector:display_small", { + physical = false, + collisionbox = ecbox, + visual = "wielditem", + visual_size = vsize, + textures = {"areasprotector:display_node_small"}, + on_step = function(self, dtime) + on_step(self, dtime, "small") + end }) -local nb_radius = radius + 0.55 +minetest.register_node("areasprotector:display_node_large", { + tiles = {"areasprotector_display.png"}, + walkable = false, + drawtype = "nodebox", + node_box = { + type = "fixed", + fixed = make_display_nodebox(radius_large, height_large) + }, + selection_box = { + type = "regular", + }, + paramtype = "light", + groups = {dig_immediate=3,not_in_creative_inventory=1}, + drop = "" +}) -minetest.register_node("areasprotector:display_node", { +minetest.register_node("areasprotector:display_node_small", { tiles = {"areasprotector_display.png"}, walkable = false, drawtype = "nodebox", node_box = { type = "fixed", - fixed = { - -- sides - { -nb_radius, -nb_radius, -nb_radius, -nb_radius, nb_radius, nb_radius }, - { -nb_radius, -nb_radius, nb_radius, nb_radius, nb_radius, nb_radius }, - { nb_radius, -nb_radius, -nb_radius, nb_radius, nb_radius, nb_radius }, - { -nb_radius, -nb_radius, -nb_radius, nb_radius, nb_radius, -nb_radius }, - -- top - { -nb_radius, nb_radius, -nb_radius, nb_radius, nb_radius, nb_radius }, - -- bottom - { -nb_radius, -nb_radius, -nb_radius, nb_radius, -nb_radius, nb_radius }, - -- middle (surround protector) - {-.55,-.55,-.55, .55,.55,.55}, - }, + fixed = make_display_nodebox(radius_small, height_small) }, selection_box = { type = "regular", }, paramtype = "light", groups = {dig_immediate=3,not_in_creative_inventory=1}, - drop = "", + drop = "" }) minetest.register_craft({ - output = "areasprotector:protector", + output = "areasprotector:protector_small 2", type = "shapeless", recipe = {"default:steelblock","basic_materials:padlock"}, }) + +minetest.register_craft({ + output = "areasprotector:protector_large", + type = "shapeless", + recipe = { + "areasprotector:protector_small", + "areasprotector:protector_small", + "areasprotector:protector_small", + "areasprotector:protector_small", + "areasprotector:protector_small", + "areasprotector:protector_small", + "areasprotector:protector_small", + "areasprotector:protector_small" + } +}) + +minetest.register_alias("areasprotector:protector", "areasprotector:protector_large") +minetest.register_alias("areasprotector:display_node", "areasprotector:display_node_large") diff --git a/textures/areasprotector_large_overlay.png b/textures/areasprotector_large_overlay.png new file mode 100644 index 0000000..38e92f3 Binary files /dev/null and b/textures/areasprotector_large_overlay.png differ -- cgit v1.2.3