1 -- signs_lib api, backported from street_signs
3 local S = signs_lib.gettext
5 signs_lib.lbm_restore_nodes = {}
6 signs_lib.old_fenceposts = {}
7 signs_lib.old_fenceposts_replacement_signs = {}
8 signs_lib.old_fenceposts_with_signs = {}
9 signs_lib.allowed_poles = {}
11 -- Settings used for a standard wood or steel wall sign
12 signs_lib.standard_lines = 6
13 signs_lib.standard_hscale = 1
14 signs_lib.standard_vscale = 1
15 signs_lib.standard_lspace = 1
16 signs_lib.standard_fsize = 15
17 signs_lib.standard_xoffs = 4
18 signs_lib.standard_yoffs = 0
19 signs_lib.standard_cpl = 35
21 signs_lib.standard_wood_groups = table.copy(minetest.registered_items["default:sign_wall_wood"].groups)
22 signs_lib.standard_wood_groups.sign = 1
23 signs_lib.standard_wood_groups.attached_node = nil
25 signs_lib.standard_steel_groups = table.copy(minetest.registered_items["default:sign_wall_steel"].groups)
26 signs_lib.standard_steel_groups.sign = 1
27 signs_lib.standard_steel_groups.attached_node = nil
29 signs_lib.standard_wood_sign_sounds = table.copy(minetest.registered_items["default:sign_wall_wood"].sounds)
30 signs_lib.standard_steel_sign_sounds = table.copy(minetest.registered_items["default:sign_wall_steel"].sounds)
32 signs_lib.standard_yaw = {
39 signs_lib.wallmounted_yaw = {
48 signs_lib.fdir_to_back = {
55 signs_lib.wall_fdir_to_back = {
64 signs_lib.rotate_facedir = {
74 signs_lib.rotate_walldir = {
83 -- Initialize character texture cache
86 signs_lib.wallmounted_rotate = function(pos, node, user, mode)
87 if not signs_lib.can_modify(pos, user) then return false end
89 if mode ~= screwdriver.ROTATE_FACE or string.match(node.name, "_onpole") then
93 local newparam2 = signs_lib.rotate_walldir[node.param2] or 0
95 minetest.swap_node(pos, { name = node.name, param2 = newparam2 })
96 for _, v in ipairs(minetest.get_objects_inside_radius(pos, 0.5)) do
97 local e = v:get_luaentity()
98 if e and e.name == "signs_lib:text" then
102 signs_lib.update_sign(pos)
106 signs_lib.facedir_rotate = function(pos, node, user, mode)
107 if not signs_lib.can_modify(pos, user) then return false end
109 if mode ~= screwdriver.ROTATE_FACE or string.match(node.name, "_onpole") then
113 local newparam2 = signs_lib.rotate_facedir[node.param2] or 0
115 minetest.swap_node(pos, { name = node.name, param2 = newparam2 })
116 for _, v in ipairs(minetest.get_objects_inside_radius(pos, 0.5)) do
117 local e = v:get_luaentity()
118 if e and e.name == "signs_lib:text" then
122 signs_lib.update_sign(pos)
126 local DEFAULT_TEXT_SCALE = {x=10, y=10}
130 if not minetest.settings:get_bool("creative_mode") then
131 signs_lib.expect_infinite_stacks = false
133 signs_lib.expect_infinite_stacks = true
138 -- Path to the textures.
139 local TP = signs_lib.path .. "/textures"
140 -- Font file formatter
141 local CHAR_FILE = "%s_%02x.png"
143 local CHAR_PATH = TP .. "/" .. CHAR_FILE
145 -- Lots of overkill here. KISS advocates, go away, shoo! ;) -- kaeza
147 local PNG_HDR = string.char(0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A)
149 -- check if a file does exist
150 -- to avoid reopening file after checking again
151 -- pass TRUE as second argument
152 local function file_exists(name, return_handle, mode)
154 local f = io.open(name, mode)
156 if (return_handle) then
166 -- Read the image size from a PNG file.
167 -- Returns image_w, image_h.
168 -- Only the LSB is read from each field!
169 function signs_lib.read_image_size(filename)
170 local f = file_exists(filename, true, "rb")
171 -- file might not exist (don't crash the game)
176 local hdr = f:read(string.len(PNG_HDR))
177 if hdr ~= PNG_HDR then
186 return ws:byte(), hs:byte()
189 -- 4 rows, max 80 chars per, plus a bit of fudge to
190 -- avoid excess trimming (e.g. due to color codes)
192 local MAX_INPUT_CHARS = 400
194 -- helper functions to trim sign text input/output
196 local function trim_input(text)
197 return text:sub(1, math.min(MAX_INPUT_CHARS, text:len()))
200 local function build_char_db(font_size)
204 -- To calculate average char width.
205 local total_width = 0
209 local w, h = signs_lib.read_image_size(CHAR_PATH:format("signs_lib_font_"..font_size.."px", c))
211 local ch = string.char(c)
213 total_width = total_width + w
214 char_count = char_count + 1
218 local cbw, cbh = signs_lib.read_image_size(TP.."/signs_lib_color_"..font_size.."px_n.png")
219 assert(cbw and cbh, "error reading bg dimensions")
220 return cw, cbw, cbh, (total_width / char_count)
223 signs_lib.charwidth15,
224 signs_lib.colorbgw15,
225 signs_lib.lineheight15,
226 signs_lib.avgwidth15 = build_char_db(15)
228 signs_lib.charwidth31,
229 signs_lib.colorbgw31,
230 signs_lib.lineheight31,
231 signs_lib.avgwidth31 = build_char_db(31)
233 local sign_groups = {choppy=2, dig_immediate=2}
234 local fences_with_sign = { }
236 -- some local helper functions
238 local function split_lines_and_words(text)
239 if not text then return end
241 for _, line in ipairs(text:split("\n")) do
242 table.insert(lines, line:split(" "))
247 local math_max = math.max
249 local function fill_line(x, y, w, c, font_size, colorbgw)
252 for xx = 0, math.max(0, w), colorbgw do
253 table.insert(tex, (":%d,%d=signs_lib_color_"..font_size.."px_%s.png"):format(x + xx, y, c))
255 return table.concat(tex)
258 -- make char texture file name
259 -- if texture file does not exist use fallback texture instead
260 local function char_tex(font_name, ch)
261 if ctexcache[font_name..ch] then
262 return ctexcache[font_name..ch], true
265 local exists, tex = file_exists(CHAR_PATH:format(font_name, c))
266 if exists and c ~= 14 then
267 tex = CHAR_FILE:format(font_name, c)
269 tex = CHAR_FILE:format(font_name, 0x0)
271 ctexcache[font_name..ch] = tex
276 local function make_line_texture(line, lineno, pos, line_width, line_height, cwidth_tab, font_size, colorbgw)
279 local font_name = "signs_lib_font_"..font_size.."px"
282 local node = minetest.get_node(pos)
283 local def = minetest.registered_items[node.name]
284 local default_color = def.default_color or 0
286 local cur_color = tonumber(default_color, 16)
288 -- We check which chars are available here.
289 for word_i, word in ipairs(line) do
292 word = string.gsub(word, "%^[12345678abcdefgh]", {
293 ["^1"] = string.char(0x81),
294 ["^2"] = string.char(0x82),
295 ["^3"] = string.char(0x83),
296 ["^4"] = string.char(0x84),
297 ["^5"] = string.char(0x85),
298 ["^6"] = string.char(0x86),
299 ["^7"] = string.char(0x87),
300 ["^8"] = string.char(0x88),
301 ["^a"] = string.char(0x8a),
302 ["^b"] = string.char(0x8b),
303 ["^c"] = string.char(0x8c),
304 ["^d"] = string.char(0x8d),
305 ["^e"] = string.char(0x8e),
306 ["^f"] = string.char(0x8f),
307 ["^g"] = string.char(0x90),
308 ["^h"] = string.char(0x91)
313 local c = word:sub(i, i)
315 local cc = tonumber(word:sub(i+1, i+1), 16)
321 local w = cwidth_tab[c]
323 width = width + w + 1
324 if width >= (line_width - cwidth_tab[" "]) then
327 maxw = math_max(width, maxw)
329 if #chars < MAX_INPUT_CHARS then
330 table.insert(chars, {
332 tex = char_tex(font_name, c),
333 col = ("%X"):format(cur_color),
336 ch_offs = ch_offs + w
341 width = width + cwidth_tab[" "] + 1
342 maxw = math_max(width, maxw)
343 table.insert(words, { chars=chars, w=ch_offs })
346 -- Okay, we actually build the "line texture" here.
350 local start_xpos = math.floor((line_width - maxw) / 2) + def.x_offset
352 local xpos = start_xpos
353 local ypos = (line_height + def.line_spacing)* lineno + def.y_offset
357 for word_i, word in ipairs(words) do
358 local xoffs = (xpos - start_xpos)
359 if (xoffs > 0) and ((xoffs + word.w) > maxw) then
360 table.insert(texture, fill_line(xpos, ypos, maxw, "n", font_size, colorbgw))
362 ypos = ypos + line_height + def.line_spacing
364 if lineno >= def.number_of_lines then break end
365 table.insert(texture, fill_line(xpos, ypos, maxw, cur_color, font_size, colorbgw))
367 for ch_i, ch in ipairs(word.chars) do
368 if ch.col ~= cur_color then
370 table.insert(texture, fill_line(xpos + ch.off, ypos, maxw, cur_color, font_size, colorbgw))
372 table.insert(texture, (":%d,%d=%s"):format(xpos + ch.off, ypos, ch.tex))
376 (":%d,%d="):format(xpos + word.w, ypos) .. char_tex(font_name, " ")
378 xpos = xpos + word.w + cwidth_tab[" "]
379 if xpos >= (line_width + cwidth_tab[" "]) then break end
382 table.insert(texture, fill_line(xpos, ypos, maxw, "n", font_size, colorbgw))
383 table.insert(texture, fill_line(start_xpos, ypos + line_height, maxw, "n", font_size, colorbgw))
385 return table.concat(texture), lineno
388 local function make_sign_texture(lines, pos)
389 local node = minetest.get_node(pos)
390 local def = minetest.registered_items[node.name]
398 if def.font_size and def.font_size == 31 then
400 line_width = math.floor(signs_lib.avgwidth31 * def.chars_per_line) * def.horiz_scaling
401 line_height = signs_lib.lineheight31
402 char_width = signs_lib.charwidth31
403 colorbgw = signs_lib.colorbgw31
406 line_width = math.floor(signs_lib.avgwidth15 * def.chars_per_line) * def.horiz_scaling
407 line_height = signs_lib.lineheight15
408 char_width = signs_lib.charwidth15
409 colorbgw = signs_lib.colorbgw15
412 local texture = { ("[combine:%dx%d"):format(line_width, (line_height + def.line_spacing) * def.number_of_lines * def.vert_scaling) }
416 if lineno >= def.number_of_lines then break end
417 local linetex, ln = make_line_texture(lines[i], lineno, pos, line_width, line_height, char_width, font_size, colorbgw)
418 table.insert(texture, linetex)
421 table.insert(texture, "^[makealpha:0,0,0")
422 return table.concat(texture, "")
425 local function set_obj_text(obj, text, x, pos)
426 local split = split_lines_and_words
427 local text_ansi = Utf8ToAnsi(text)
428 local n = minetest.registered_nodes[minetest.get_node(pos).name]
429 local text_scale = (n and n.text_scale) or DEFAULT_TEXT_SCALE
430 local texture = make_sign_texture(split(text_ansi), pos)
433 visual_size = text_scale,
437 signs_lib.construct_sign = function(pos)
438 local meta = minetest.get_meta(pos)
442 "textarea[0,-0.3;6.5,3;text;;${text}]"..
443 "button_exit[2,3.4;2,1;ok;"..S("Write").."]"..
444 "background[-0.5,-0.5;7,5;signs_lib_sign_bg.jpg]")
445 local i = meta:get_string("infotext")
446 if i == "" then -- it wasn't even set, so set it.
447 meta:set_string("infotext", "")
451 function signs_lib.destruct_sign(pos)
452 local objects = minetest.get_objects_inside_radius(pos, 0.5)
453 for _, v in ipairs(objects) do
454 local e = v:get_luaentity()
455 if e and e.name == "signs_lib:text" then
461 local function make_infotext(text)
462 text = trim_input(text)
463 local lines = split_lines_and_words(text) or {}
465 for _, line in ipairs(lines) do
466 table.insert(lines2, (table.concat(line, " "):gsub("#[0-9a-fA-F]", ""):gsub("##", "#")))
468 return table.concat(lines2, "\n")
471 function signs_lib.update_sign(pos, fields)
472 local meta = minetest.get_meta(pos)
474 local text = fields and fields.text or meta:get_string("text")
475 text = trim_input(text)
477 local owner = meta:get_string("owner")
479 if owner ~= "" then ownstr = S("Locked sign, owned by @1\n", owner) end
481 meta:set_string("text", text)
482 meta:set_string("infotext", ownstr..make_infotext(text).." ")
484 local objects = minetest.get_objects_inside_radius(pos, 0.5)
486 for _, v in ipairs(objects) do
487 local e = v:get_luaentity()
488 if e and e.name == "signs_lib:text" then
492 set_obj_text(v, text, nil, pos)
501 -- if there is no entity
502 local signnode = minetest.get_node(pos)
503 local signname = signnode.name
504 local def = minetest.registered_items[signname]
505 if not def.entity_info or not def.entity_info.yaw[signnode.param2 + 1] then return end
506 local obj = minetest.add_entity(pos, "signs_lib:text")
508 obj:setyaw(def.entity_info.yaw[signnode.param2 + 1])
510 mesh = def.entity_info.mesh,
514 function signs_lib.receive_fields(pos, formname, fields, sender)
515 if fields and fields.text and fields.ok and signs_lib.can_modify(pos, sender) then
516 minetest.log("action", S("@1 wrote \"@2\" to sign at @3",
517 (sender:get_player_name() or ""),
518 fields.text:gsub('\\', '\\\\'):gsub("\n", "\\n"),
519 minetest.pos_to_string(pos)
521 signs_lib.update_sign(pos, fields)
525 function signs_lib.can_modify(pos, player)
526 local meta = minetest.get_meta(pos)
527 local owner = meta:get_string("owner")
528 local playername = player:get_player_name()
530 if minetest.is_protected(pos, playername) then
531 minetest.record_protection_violation(pos, playername)
536 or playername == owner
537 or (minetest.check_player_privs(playername, {sign_editor=true}))
538 or (playername == minetest.settings:get("name")) then
541 minetest.record_protection_violation(pos, playername)
545 local signs_text_on_activate = function(self)
546 local pos = self.object:getpos()
547 local meta = minetest.get_meta(pos)
548 local signnode = minetest.get_node(pos)
549 local signname = signnode.name
550 local def = minetest.registered_items[signname]
551 local text = meta:get_string("text")
552 if text and def and def.entity_info then
553 text = trim_input(text)
554 set_obj_text(self.object, text, nil, pos)
555 self.object:set_properties({
556 mesh = def.entity_info.mesh,
561 minetest.register_entity("signs_lib:text", {
562 collisionbox = { 0, 0, 0, 0, 0, 0 },
564 mesh = "signs_lib_standard_wall_sign_entity.obj",
566 on_activate = signs_text_on_activate,
569 -- make selection boxes
570 -- sizex/sizey specified in inches because that's what MUTCD uses.
572 function signs_lib.make_selection_boxes(sizex, sizey, foo, xoffs, yoffs, zoffs, is_facedir)
574 local tx = (sizex * 0.0254 ) / 2
575 local ty = (sizey * 0.0254 ) / 2
576 local xo = xoffs and xoffs * 0.0254 or 0
577 local yo = yoffs and yoffs * 0.0254 or 0
578 local zo = zoffs and zoffs * 0.0254 or 0
580 if not is_facedir then
582 type = "wallmounted",
583 wall_side = { -0.5 + zo, -ty + yo, -tx + xo, -0.4375 + zo, ty + yo, tx + xo },
584 wall_top = { -tx - xo, 0.5 + zo, -ty + yo, tx - xo, 0.4375 + zo, ty + yo},
585 wall_bottom = { -tx - xo, -0.5 + zo, -ty + yo, tx - xo, -0.4375 + zo, ty + yo }
590 fixed = { -tx + xo, -ty + yo, 0.5 + zo, tx + xo, ty + yo, 0.4375 + zo}
595 function signs_lib.check_for_pole(pos, pointed_thing)
596 local ppos = minetest.get_pointed_thing_position(pointed_thing)
597 local pnode = minetest.get_node(ppos)
598 local pdef = minetest.registered_items[pnode.name]
600 if (signs_lib.allowed_poles[pnode.name]
601 or (pdef and pdef.drawtype == "fencelike")
602 or string.find(pnode.name, "default:fence_")
603 or string.find(pnode.name, "_post")
604 or string.find(pnode.name, "fencepost")
605 or (pnode.name == "streets:bigpole" and pnode.param2 < 4)
606 or (pnode.name == "streets:bigpole" and pnode.param2 > 19 and pnode.param2 < 24)
609 (pos.x ~= ppos.x or pos.z ~= ppos.z) then
614 function signs_lib.after_place_node(pos, placer, itemstack, pointed_thing, locked)
615 local playername = placer:get_player_name()
616 local def = minetest.registered_items[itemstack:get_name()]
618 local ppos = minetest.get_pointed_thing_position(pointed_thing)
619 local pnode = minetest.get_node(ppos)
620 local pdef = minetest.registered_items[pnode.name]
622 if (def.allow_onpole ~= false) and signs_lib.check_for_pole(pos, pointed_thing) then
623 local node = minetest.get_node(pos)
624 minetest.swap_node(pos, {name = itemstack:get_name().."_onpole", param2 = node.param2})
627 local meta = minetest.get_meta(pos)
628 meta:set_string("owner", playername)
629 meta:set_string("infotext", S("Locked sign, owned by @1\n", playername))
633 function signs_lib.register_fence_with_sign()
634 minetest.log("warning", "[signs_lib] ".."Attempt to call no longer used function signs_lib.register_fence_with_sign()")
638 The main sign registration function
639 ===================================
641 Example minimal recommended def for writable signs:
643 signs_lib.register_sign("foo:my_cool_sign", {
644 description = "Wooden cool sign",
645 inventory_image = "signs_lib_sign_cool_inv.png",
647 "signs_lib_sign_cool.png",
648 "signs_lib_sign_cool_edges.png"
658 entity_info = "standard"
661 * default def assumes a wallmounted sign with on-pole being allowed.
663 *For signs that can't support onpole, include in the def:
664 allow_onpole = false,
666 * "standard" entity info implies the standard wood/steel sign model, in
667 wallmounted mode. For facedir signs using the standard model, use:
670 mesh = "signs_lib_standard_wall_sign_entity.obj",
671 yaw = signs_lib.standard_yaw
676 function signs_lib.register_sign(name, rdef)
678 local def = table.copy(rdef)
680 if rdef.entity_info == "standard" then
682 mesh = "signs_lib_standard_wall_sign_entity.obj",
683 yaw = signs_lib.wallmounted_yaw
685 elseif rdef.entity_info then
686 def.entity_info = rdef.entity_info
689 if rdef.entity_info then
690 def.on_rightclick = rdef.on_rightclick or signs_lib.construct_sign
691 def.on_construct = rdef.on_construct or signs_lib.construct_sign
692 def.on_destruct = rdef.on_destruct or signs_lib.destruct_sign
693 def.on_receive_fields = rdef.on_receive_fields or signs_lib.receive_fields
694 def.on_punch = rdef.on_punch or signs_lib.update_sign
695 def.number_of_lines = rdef.number_of_lines or signs_lib.standard_lines
696 def.horiz_scaling = rdef.horiz_scaling or signs_lib.standard_hscale
697 def.vert_scaling = rdef.vert_scaling or signs_lib.standard_vscale
698 def.line_spacing = rdef.line_spacing or signs_lib.standard_lspace
699 def.font_size = rdef.font_size or signs_lib.standard_fsize
700 def.x_offset = rdef.x_offset or signs_lib.standard_xoffs
701 def.y_offset = rdef.y_offset or signs_lib.standard_yoffs
702 def.chars_per_line = rdef.chars_per_line or signs_lib.standard_cpl
703 def.default_color = rdef.default_color or "0"
704 if rdef.locked and not rdef.after_place_node then
705 def.after_place_node = function(pos, placer, itemstack, pointed_thing)
706 signs_lib.after_place_node(pos, placer, itemstack, pointed_thing, true)
709 def.after_place_node = rdef.after_place_node or signs_lib.after_place_node
713 def.paramtype = rdef.paramtype or "light"
714 def.paramtype2 = rdef.paramtype2 or "wallmounted"
715 def.drawtype = rdef.drawtype or "mesh"
716 def.mesh = rdef.mesh or "signs_lib_standard_wall_sign.obj"
717 def.wield_image = rdef.wield_image or def.inventory_image
718 def.drop = rdef.drop or name
719 def.sounds = rdef.sounds or signs_lib.standard_wood_sign_sounds
720 def.on_rotate = rdef.on_rotate or signs_lib.wallmounted_rotate
722 if rdef.on_rotate then
723 def.on_rotate = rdef.on_rotate
724 elseif rdef.drawtype == "wallmounted" then
725 def.on_rotate = signs_lib.wallmounted_rotate
727 def.on_rotate = signs_lib.facedir_rotate
731 def.groups = rdef.groups
733 def.groups = signs_lib.standard_wood_groups
736 local cbox = signs_lib.make_selection_boxes(35, 25, allow_onpole)
738 def.selection_box = rdef.selection_box or cbox
739 def.node_box = table.copy(rdef.node_box or rdef.selection_box or cbox)
741 if def.sunlight_propagates ~= false then
742 def.sunlight_propagates = true
745 minetest.register_node(":"..name, def)
746 table.insert(signs_lib.lbm_restore_nodes, name)
748 if rdef.allow_onpole ~= false then
750 local opdef = table.copy(def)
752 local offset = 0.3125
753 if opdef.uses_slim_pole_mount then
757 if opdef.paramtype2 == "wallmounted" then
758 opdef.node_box.wall_side[1] = def.node_box.wall_side[1] - offset
759 opdef.node_box.wall_side[4] = def.node_box.wall_side[4] - offset
761 opdef.selection_box.wall_side[1] = def.selection_box.wall_side[1] - offset
762 opdef.selection_box.wall_side[4] = def.selection_box.wall_side[4] - offset
764 opdef.node_box.fixed[3] = def.node_box.fixed[3] + offset
765 opdef.node_box.fixed[6] = def.node_box.fixed[6] + offset
767 opdef.selection_box.fixed[3] = def.selection_box.fixed[3] + offset
768 opdef.selection_box.fixed[6] = def.selection_box.fixed[6] + offset
771 opdef.groups.not_in_creative_inventory = 1
772 opdef.tiles[3] = "signs_lib_pole_mount.png"
773 opdef.mesh = string.gsub(opdef.mesh, ".obj$", "_onpole.obj")
774 opdef.on_rotate = nil
777 if opdef.entity_info then
778 opdef.entity_info.mesh = string.gsub(opdef.entity_info.mesh, ".obj$", "_onpole.obj")
780 minetest.register_node(":"..name.."_onpole", opdef)
781 table.insert(signs_lib.lbm_restore_nodes, name.."_onpole")
785 -- restore signs' text after /clearobjects and the like, the next time
786 -- a block is reloaded by the server.
788 minetest.register_lbm({
789 nodenames = signs_lib.lbm_restore_nodes,
790 name = "signs_lib:restore_sign_text",
791 label = "Restore sign text",
792 run_at_every_load = true,
793 action = function(pos, node)
794 signs_lib.update_sign(pos,nil,nil,node)
798 -- Convert old signs on fenceposts into signs on.. um.. fence posts :P
800 minetest.register_lbm({
801 nodenames = signs_lib.old_fenceposts_with_signs,
802 name = "signs_lib:fix_fencepost_signs",
803 label = "Change single-node signs on fences into normal",
804 run_at_every_load = true,
805 action = function(pos, node)
807 local fdir = node.param2 % 8
809 x = pos.x + signs_lib.fdir_to_back[fdir+1][1],
811 z = pos.z + signs_lib.fdir_to_back[fdir+1][2]
814 if minetest.get_node(signpos).name == "air" then
815 local new_wmdir = minetest.dir_to_wallmounted(minetest.facedir_to_dir(fdir))
816 local oldfence = signs_lib.old_fenceposts[node.name]
817 local newsign = signs_lib.old_fenceposts_replacement_signs[node.name]
819 for _, v in ipairs(minetest.get_objects_inside_radius(pos, 0.5)) do
820 local e = v:get_luaentity()
821 if e and e.name == "signs_lib:text" then
826 local oldmeta = minetest.get_meta(pos):to_table()
827 minetest.set_node(pos, {name = oldfence})
828 minetest.set_node(signpos, { name = newsign, param2 = new_wmdir })
829 local newmeta = minetest.get_meta(signpos)
830 newmeta:from_table(oldmeta)
831 signs_lib.update_sign(signpos)
836 signs_lib.block_list = {}
837 signs_lib.totalblocks = 0
839 -- Maintain a list of currently-loaded blocks
840 minetest.register_lbm({
841 nodenames = {"group:sign"},
842 name = "signs_lib:update_block_list",
843 label = "Update list of loaded blocks, log only those with signs",
844 run_at_every_load = true,
845 action = function(pos, node)
846 -- yeah, yeah... I know I'm hashing a block pos, but it's still just a set of coords
847 local hash = minetest.hash_node_position(vector.floor(vector.divide(pos, core.MAP_BLOCKSIZE)))
848 if not signs_lib.block_list[hash] then
849 signs_lib.block_list[hash] = true
850 signs_lib.totalblocks = signs_lib.totalblocks + 1
855 minetest.register_chatcommand("regen_signs", {
857 privs = {server = true},
858 description = "Skims through all currently-loaded sign-bearing mapblocks, clears away any entities within each sign's node space, and regenerates their text entities, if any.",
859 func = function(player_name, params)
862 for b in pairs(signs_lib.block_list) do
863 local blockpos = minetest.get_position_from_hash(b)
864 local pos1 = vector.multiply(blockpos, core.MAP_BLOCKSIZE)
865 local pos2 = vector.add(pos1, core.MAP_BLOCKSIZE - 1)
866 if minetest.get_node_or_nil(vector.add(pos1, core.MAP_BLOCKSIZE/2)) then
867 local signs_in_block = minetest.find_nodes_in_area(pos1, pos2, {"group:sign"})
868 allsigns[#allsigns + 1] = signs_in_block
869 totalsigns = totalsigns + #signs_in_block
871 signs_lib.block_list[b] = nil -- if the block is no longer loaded, remove it from the table
872 signs_lib.totalblocks = signs_lib.totalblocks - 1
875 if signs_lib.totalblocks < 0 then signs_lib.totalblocks = 0 end
876 if totalsigns == 0 then
877 minetest.chat_send_player(player_name, "There are no signs in the currently-loaded terrain.")
878 signs_lib.block_list = {}
882 minetest.chat_send_player(player_name, "Found a total of "..totalsigns.." sign nodes across "..signs_lib.totalblocks.." blocks.")
883 minetest.chat_send_player(player_name, "Regenerating sign entities...")
885 for _, b in pairs(allsigns) do
886 for _, pos in ipairs(b) do
887 local objects = minetest.get_objects_inside_radius(pos, 0.5)
889 for _, v in ipairs(objects) do
893 local node = minetest.get_node(pos)
894 local def = minetest.registered_items[node.name]
895 if def and def.entity_info then
896 signs_lib.update_sign(pos)
900 minetest.chat_send_player(player_name, "Finished.")