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.default_text_scale = {x=10, y=10}
34 signs_lib.standard_yaw = {
41 signs_lib.wallmounted_yaw = {
50 signs_lib.fdir_to_back = {
57 signs_lib.wall_fdir_to_back = {
66 signs_lib.rotate_facedir = {
76 signs_lib.rotate_walldir = {
85 -- Initialize character texture cache
90 minetest.register_entity("signs_lib:text", {
91 collisionbox = { 0, 0, 0, 0, 0, 0 },
93 mesh = "signs_lib_standard_wall_sign_entity.obj",
98 function signs_lib.delete_objects(pos)
99 print("delete_objects()")
100 local objects = minetest.get_objects_inside_radius(pos, 0.5)
101 for _, v in ipairs(objects) do
106 function signs_lib.spawn_entity(pos, texture)
107 print("spawn_entity()")
108 local node = minetest.get_node(pos)
109 local def = minetest.registered_items[node.name]
110 if not def or not def.entity_info or not def.entity_info.yaw[node.param2 + 1] then return end
112 local text_scale = (node and node.text_scale) or signs_lib.default_text_scale
113 local objects = minetest.get_objects_inside_radius(pos, 0.5)
119 obj = minetest.add_entity(pos, "signs_lib:text")
122 obj:setyaw(def.entity_info.yaw[node.param2 + 1])
126 mesh = def.entity_info.mesh,
127 visual_size = text_scale,
131 mesh = def.entity_info.mesh,
132 visual_size = text_scale,
140 function signs_lib.wallmounted_rotate(pos, node, user, mode)
141 if not signs_lib.can_modify(pos, user) then return false end
143 if mode ~= screwdriver.ROTATE_FACE or string.match(node.name, "_onpole") then
147 local newparam2 = signs_lib.rotate_walldir[node.param2] or 0
149 minetest.swap_node(pos, { name = node.name, param2 = newparam2 })
150 signs_lib.delete_objects(pos)
151 signs_lib.update_sign(pos)
155 function signs_lib.facedir_rotate(pos, node, user, mode)
156 if not signs_lib.can_modify(pos, user) then return false end
158 if mode ~= screwdriver.ROTATE_FACE or string.match(node.name, "_onpole") then
162 local newparam2 = signs_lib.rotate_facedir[node.param2] or 0
164 minetest.swap_node(pos, { name = node.name, param2 = newparam2 })
165 signs_lib.delete_objects(pos)
166 signs_lib.update_sign(pos)
172 if not minetest.settings:get_bool("creative_mode") then
173 signs_lib.expect_infinite_stacks = false
175 signs_lib.expect_infinite_stacks = true
180 -- Path to the textures.
181 local TP = signs_lib.path .. "/textures"
182 -- Font file formatter
183 local CHAR_FILE = "%s_%02x.png"
185 local CHAR_PATH = TP .. "/" .. CHAR_FILE
187 -- Lots of overkill here. KISS advocates, go away, shoo! ;) -- kaeza
189 local PNG_HDR = string.char(0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A)
191 -- check if a file does exist
192 -- to avoid reopening file after checking again
193 -- pass TRUE as second argument
194 local function file_exists(name, return_handle, mode)
196 local f = io.open(name, mode)
198 if (return_handle) then
208 -- Read the image size from a PNG file.
209 -- Returns image_w, image_h.
210 -- Only the LSB is read from each field!
211 function signs_lib.read_image_size(filename)
212 local f = file_exists(filename, true, "rb")
213 -- file might not exist (don't crash the game)
218 local hdr = f:read(string.len(PNG_HDR))
219 if hdr ~= PNG_HDR then
228 return ws:byte(), hs:byte()
231 -- 4 rows, max 80 chars per, plus a bit of fudge to
232 -- avoid excess trimming (e.g. due to color codes)
234 local MAX_INPUT_CHARS = 400
236 -- helper functions to trim sign text input/output
238 local function trim_input(text)
239 return text:sub(1, math.min(MAX_INPUT_CHARS, text:len()))
242 local function build_char_db(font_size)
246 -- To calculate average char width.
247 local total_width = 0
251 local w, h = signs_lib.read_image_size(CHAR_PATH:format("signs_lib_font_"..font_size.."px", c))
253 local ch = string.char(c)
255 total_width = total_width + w
256 char_count = char_count + 1
260 local cbw, cbh = signs_lib.read_image_size(TP.."/signs_lib_color_"..font_size.."px_n.png")
261 assert(cbw and cbh, "error reading bg dimensions")
262 return cw, cbw, cbh, (total_width / char_count)
265 signs_lib.charwidth15,
266 signs_lib.colorbgw15,
267 signs_lib.lineheight15,
268 signs_lib.avgwidth15 = build_char_db(15)
270 signs_lib.charwidth31,
271 signs_lib.colorbgw31,
272 signs_lib.lineheight31,
273 signs_lib.avgwidth31 = build_char_db(31)
275 local sign_groups = {choppy=2, dig_immediate=2}
276 local fences_with_sign = { }
278 -- some local helper functions
280 local math_max = math.max
282 local function fill_line(x, y, w, c, font_size, colorbgw)
285 for xx = 0, math.max(0, w), colorbgw do
286 table.insert(tex, (":%d,%d=signs_lib_color_"..font_size.."px_%s.png"):format(x + xx, y, c))
288 return table.concat(tex)
291 -- make char texture file name
292 -- if texture file does not exist use fallback texture instead
293 local function char_tex(font_name, ch)
294 if ctexcache[font_name..ch] then
295 return ctexcache[font_name..ch], true
298 local exists, tex = file_exists(CHAR_PATH:format(font_name, c))
299 if exists and c ~= 14 then
300 tex = CHAR_FILE:format(font_name, c)
302 tex = CHAR_FILE:format(font_name, 0x0)
304 ctexcache[font_name..ch] = tex
309 local function make_line_texture(line, lineno, pos, line_width, line_height, cwidth_tab, font_size, colorbgw)
312 local font_name = "signs_lib_font_"..font_size.."px"
315 local node = minetest.get_node(pos)
316 local def = minetest.registered_items[node.name]
317 local default_color = def.default_color or 0
319 local cur_color = tonumber(default_color, 16)
321 -- We check which chars are available here.
322 for word_i, word in ipairs(line) do
325 word = string.gsub(word, "%^[12345678abcdefgh]", {
326 ["^1"] = string.char(0x81),
327 ["^2"] = string.char(0x82),
328 ["^3"] = string.char(0x83),
329 ["^4"] = string.char(0x84),
330 ["^5"] = string.char(0x85),
331 ["^6"] = string.char(0x86),
332 ["^7"] = string.char(0x87),
333 ["^8"] = string.char(0x88),
334 ["^a"] = string.char(0x8a),
335 ["^b"] = string.char(0x8b),
336 ["^c"] = string.char(0x8c),
337 ["^d"] = string.char(0x8d),
338 ["^e"] = string.char(0x8e),
339 ["^f"] = string.char(0x8f),
340 ["^g"] = string.char(0x90),
341 ["^h"] = string.char(0x91)
346 local c = word:sub(i, i)
348 local cc = tonumber(word:sub(i+1, i+1), 16)
354 local w = cwidth_tab[c]
356 width = width + w + 1
357 if width >= (line_width - cwidth_tab[" "]) then
360 maxw = math_max(width, maxw)
362 if #chars < MAX_INPUT_CHARS then
363 table.insert(chars, {
365 tex = char_tex(font_name, c),
366 col = ("%X"):format(cur_color),
369 ch_offs = ch_offs + w
374 width = width + cwidth_tab[" "] + 1
375 maxw = math_max(width, maxw)
376 table.insert(words, { chars=chars, w=ch_offs })
379 -- Okay, we actually build the "line texture" here.
383 local start_xpos = math.floor((line_width - maxw) / 2) + def.x_offset
385 local xpos = start_xpos
386 local ypos = (line_height + def.line_spacing)* lineno + def.y_offset
390 for word_i, word in ipairs(words) do
391 local xoffs = (xpos - start_xpos)
392 if (xoffs > 0) and ((xoffs + word.w) > maxw) then
393 table.insert(texture, fill_line(xpos, ypos, maxw, "n", font_size, colorbgw))
395 ypos = ypos + line_height + def.line_spacing
397 if lineno >= def.number_of_lines then break end
398 table.insert(texture, fill_line(xpos, ypos, maxw, cur_color, font_size, colorbgw))
400 for ch_i, ch in ipairs(word.chars) do
401 if ch.col ~= cur_color then
403 table.insert(texture, fill_line(xpos + ch.off, ypos, maxw, cur_color, font_size, colorbgw))
405 table.insert(texture, (":%d,%d=%s"):format(xpos + ch.off, ypos, ch.tex))
409 (":%d,%d="):format(xpos + word.w, ypos) .. char_tex(font_name, " ")
411 xpos = xpos + word.w + cwidth_tab[" "]
412 if xpos >= (line_width + cwidth_tab[" "]) then break end
415 table.insert(texture, fill_line(xpos, ypos, maxw, "n", font_size, colorbgw))
416 table.insert(texture, fill_line(start_xpos, ypos + line_height, maxw, "n", font_size, colorbgw))
418 return table.concat(texture), lineno
421 local function make_sign_texture(lines, pos)
422 local node = minetest.get_node(pos)
423 local def = minetest.registered_items[node.name]
431 if def.font_size and def.font_size == 31 then
433 line_width = math.floor(signs_lib.avgwidth31 * def.chars_per_line) * def.horiz_scaling
434 line_height = signs_lib.lineheight31
435 char_width = signs_lib.charwidth31
436 colorbgw = signs_lib.colorbgw31
439 line_width = math.floor(signs_lib.avgwidth15 * def.chars_per_line) * def.horiz_scaling
440 line_height = signs_lib.lineheight15
441 char_width = signs_lib.charwidth15
442 colorbgw = signs_lib.colorbgw15
445 local texture = { ("[combine:%dx%d"):format(line_width, (line_height + def.line_spacing) * def.number_of_lines * def.vert_scaling) }
449 if lineno >= def.number_of_lines then break end
450 local linetex, ln = make_line_texture(lines[i], lineno, pos, line_width, line_height, char_width, font_size, colorbgw)
451 table.insert(texture, linetex)
454 table.insert(texture, "^[makealpha:0,0,0")
455 return table.concat(texture, "")
458 function signs_lib.split_lines_and_words(text)
459 if not text then return end
461 for _, line in ipairs(text:split("\n")) do
462 table.insert(lines, line:split(" "))
467 function signs_lib.set_obj_text(pos, text)
468 print("set_obj_text()")
469 local split = signs_lib.split_lines_and_words
470 local text_ansi = Utf8ToAnsi(text)
471 local n = minetest.registered_nodes[minetest.get_node(pos).name]
472 signs_lib.delete_objects(pos)
473 signs_lib.spawn_entity(pos, make_sign_texture(split(text_ansi), pos))
476 local function make_widefont_nodename(name)
477 if string.find(name, "_widefont") then return name end
478 if string.find(name, "_onpole") then
479 return string.gsub(name, "_onpole", "_widefont_onpole")
480 elseif string.find(name, "_hanging") then
481 return string.gsub(name, "_hanging", "_widefont_hanging")
483 return name.."_widefont"
487 function signs_lib.construct_sign(pos)
488 local form = "size[6,4]"..
489 "textarea[0,-0.3;6.5,3;text;;${text}]"..
490 "background[-0.5,-0.5;7,5;signs_lib_sign_bg.jpg]"
491 local node = minetest.get_node(pos)
492 local wname = make_widefont_nodename(node.name)
494 if minetest.registered_items[wname] then
496 if string.find(node.name, "widefont") then state = "on" end
497 form = form.."label[1,3.4;Use wide font]"..
498 "image_button[1.1,3.7;1,0.6;signs_lib_switch_"..
500 state..";;;false;signs_lib_switch_interm.png]"..
501 "button_exit[3,3.4;2,1;ok;"..S("Write").."]"
503 form = form.."button_exit[2,3.4;2,1;ok;"..S("Write").."]"
506 local meta = minetest.get_meta(pos)
507 meta:set_string("formspec", form)
508 local i = meta:get_string("infotext")
509 if i == "" then -- it wasn't even set, so set it.
510 meta:set_string("infotext", "")
514 function signs_lib.destruct_sign(pos)
515 signs_lib.delete_objects(pos)
518 local function make_infotext(text)
519 text = trim_input(text)
520 local lines = signs_lib.split_lines_and_words(text) or {}
522 for _, line in ipairs(lines) do
523 table.insert(lines2, (table.concat(line, " "):gsub("#[0-9a-fA-F]", ""):gsub("##", "#")))
525 return table.concat(lines2, "\n")
528 function signs_lib.update_sign(pos, fields)
529 local meta = minetest.get_meta(pos)
531 local text = fields and fields.text or meta:get_string("text")
532 text = trim_input(text)
534 local owner = meta:get_string("owner")
536 if owner ~= "" then ownstr = S("Locked sign, owned by @1\n", owner) end
538 meta:set_string("text", text)
539 meta:set_string("infotext", ownstr..make_infotext(text).." ")
540 signs_lib.set_obj_text(pos, text)
543 function signs_lib.receive_fields(pos, formname, fields, sender)
545 if not fields or not signs_lib.can_modify(pos, sender) then return end
547 if fields.text and fields.ok then
548 minetest.log("action", S("@1 wrote \"@2\" to sign at @3",
549 (sender:get_player_name() or ""),
550 fields.text:gsub('\\', '\\\\'):gsub("\n", "\\n"),
551 minetest.pos_to_string(pos)
553 signs_lib.update_sign(pos, fields)
554 elseif fields.on or fields.off then
555 local node = minetest.get_node(pos)
558 if fields.on and string.find(node.name, "widefont") then
559 newname = string.gsub(node.name, "_widefont", "")
560 elseif fields.off and not string.find(node.name, "widefont") then
561 newname = make_widefont_nodename(node.name)
564 minetest.log("action", S("@1 flipped the wide-font switch to \"@2\" at @3",
565 (sender:get_player_name() or ""),
566 (fields.on and "off" or "on"),
567 minetest.pos_to_string(pos)
570 minetest.swap_node(pos, {name = newname, param2 = node.param2})
571 signs_lib.construct_sign(pos)
572 signs_lib.update_sign(pos, fields)
577 function signs_lib.can_modify(pos, player)
578 local meta = minetest.get_meta(pos)
579 local owner = meta:get_string("owner")
580 local playername = player:get_player_name()
582 if minetest.is_protected(pos, playername) then
583 minetest.record_protection_violation(pos, playername)
588 or playername == owner
589 or (minetest.check_player_privs(playername, {sign_editor=true}))
590 or (playername == minetest.settings:get("name")) then
593 minetest.record_protection_violation(pos, playername)
597 -- make selection boxes
598 -- sizex/sizey specified in inches because that's what MUTCD uses.
600 function signs_lib.make_selection_boxes(sizex, sizey, foo, xoffs, yoffs, zoffs, is_facedir)
602 local tx = (sizex * 0.0254 ) / 2
603 local ty = (sizey * 0.0254 ) / 2
604 local xo = xoffs and xoffs * 0.0254 or 0
605 local yo = yoffs and yoffs * 0.0254 or 0
606 local zo = zoffs and zoffs * 0.0254 or 0
608 if not is_facedir then
610 type = "wallmounted",
611 wall_side = { -0.5 + zo, -ty + yo, -tx + xo, -0.4375 + zo, ty + yo, tx + xo },
612 wall_top = { -tx - xo, 0.5 + zo, -ty + yo, tx - xo, 0.4375 + zo, ty + yo},
613 wall_bottom = { -tx - xo, -0.5 + zo, -ty + yo, tx - xo, -0.4375 + zo, ty + yo }
618 fixed = { -tx + xo, -ty + yo, 0.5 + zo, tx + xo, ty + yo, 0.4375 + zo}
623 function signs_lib.check_for_pole(pos, pointed_thing)
624 local ppos = minetest.get_pointed_thing_position(pointed_thing)
625 local pnode = minetest.get_node(ppos)
626 local pdef = minetest.registered_items[pnode.name]
628 if (signs_lib.allowed_poles[pnode.name]
629 or (pdef and pdef.drawtype == "fencelike")
630 or string.find(pnode.name, "default:fence_")
631 or string.find(pnode.name, "_post")
632 or string.find(pnode.name, "fencepost")
633 or string.find(pnode.name, "streets:streetlamp_basic_top")
634 or (pnode.name == "streets:bigpole" and pnode.param2 < 4)
635 or (pnode.name == "streets:bigpole" and pnode.param2 > 19 and pnode.param2 < 24)
638 (pos.x ~= ppos.x or pos.z ~= ppos.z) then
643 function signs_lib.check_for_ceiling(pointed_thing)
644 if pointed_thing.above.x == pointed_thing.under.x
645 and pointed_thing.above.z == pointed_thing.under.z
646 and pointed_thing.above.y < pointed_thing.under.y then
651 function signs_lib.after_place_node(pos, placer, itemstack, pointed_thing, locked)
652 print("after_place_node")
653 local playername = placer:get_player_name()
654 local def = minetest.registered_items[itemstack:get_name()]
656 local ppos = minetest.get_pointed_thing_position(pointed_thing)
657 local pnode = minetest.get_node(ppos)
658 local pdef = minetest.registered_items[pnode.name]
660 if (def.allow_onpole ~= false) and signs_lib.check_for_pole(pos, pointed_thing) then
661 local node = minetest.get_node(pos)
662 minetest.swap_node(pos, {name = itemstack:get_name().."_onpole", param2 = node.param2})
663 elseif def.allow_hanging and signs_lib.check_for_ceiling(pointed_thing) then
664 local newparam2 = minetest.dir_to_facedir(placer:get_look_dir())
665 local node = minetest.get_node(pos)
666 minetest.swap_node(pos, {name = itemstack:get_name().."_hanging", param2 = newparam2})
669 local meta = minetest.get_meta(pos)
670 meta:set_string("owner", playername)
671 meta:set_string("infotext", S("Locked sign, owned by @1\n", playername))
675 function signs_lib.register_fence_with_sign()
676 minetest.log("warning", "[signs_lib] ".."Attempt to call no longer used function signs_lib.register_fence_with_sign()")
679 local function register_sign(name, rdef)
680 local def = table.copy(rdef)
682 if rdef.entity_info == "standard" then
684 mesh = "signs_lib_standard_wall_sign_entity.obj",
685 yaw = signs_lib.wallmounted_yaw
687 elseif rdef.entity_info then
688 def.entity_info = rdef.entity_info
691 def.after_place_node = rdef.after_place_node or signs_lib.after_place_node
693 if rdef.entity_info then
694 def.on_rightclick = rdef.on_rightclick or signs_lib.construct_sign
695 def.on_construct = rdef.on_construct or signs_lib.construct_sign
696 def.on_destruct = rdef.on_destruct or signs_lib.destruct_sign
697 def.on_receive_fields = rdef.on_receive_fields or signs_lib.receive_fields
698 def.on_punch = rdef.on_punch or signs_lib.update_sign
699 def.number_of_lines = rdef.number_of_lines or signs_lib.standard_lines
700 def.horiz_scaling = rdef.horiz_scaling or signs_lib.standard_hscale
701 def.vert_scaling = rdef.vert_scaling or signs_lib.standard_vscale
702 def.line_spacing = rdef.line_spacing or signs_lib.standard_lspace
703 def.font_size = rdef.font_size or signs_lib.standard_fsize
704 def.x_offset = rdef.x_offset or signs_lib.standard_xoffs
705 def.y_offset = rdef.y_offset or signs_lib.standard_yoffs
706 def.chars_per_line = rdef.chars_per_line or signs_lib.standard_cpl
707 def.default_color = rdef.default_color or "0"
708 if rdef.locked and not rdef.after_place_node then
709 def.after_place_node = function(pos, placer, itemstack, pointed_thing)
710 signs_lib.after_place_node(pos, placer, itemstack, pointed_thing, true)
715 def.paramtype = rdef.paramtype or "light"
716 def.drawtype = rdef.drawtype or "mesh"
717 def.mesh = rdef.mesh or "signs_lib_standard_wall_sign.obj"
718 def.wield_image = rdef.wield_image or def.inventory_image
719 def.drop = rdef.drop or name
720 def.sounds = rdef.sounds or signs_lib.standard_wood_sign_sounds
721 def.on_rotate = rdef.on_rotate or signs_lib.wallmounted_rotate
722 def.paramtype2 = rdef.paramtype2 or "wallmounted"
724 if rdef.on_rotate then
725 def.on_rotate = rdef.on_rotate
726 elseif rdef.drawtype == "wallmounted" then
727 def.on_rotate = signs_lib.wallmounted_rotate
729 def.on_rotate = signs_lib.facedir_rotate
733 def.groups = rdef.groups
735 def.groups = signs_lib.standard_wood_groups
738 local cbox = signs_lib.make_selection_boxes(35, 25, allow_onpole)
740 def.selection_box = rdef.selection_box or cbox
741 def.node_box = table.copy(rdef.node_box or rdef.selection_box or cbox)
743 if def.sunlight_propagates ~= false then
744 def.sunlight_propagates = true
747 minetest.register_node(":"..name, def)
748 table.insert(signs_lib.lbm_restore_nodes, name)
750 if rdef.allow_onpole ~= false then
752 local opdef = table.copy(def)
754 local offset = 0.3125
755 if opdef.uses_slim_pole_mount then
759 opdef.selection_box = rdef.onpole_selection_box or opdef.selection_box
760 opdef.node_box = rdef.onpole_node_box or opdef.selection_box
762 if opdef.paramtype2 == "wallmounted" then
763 opdef.node_box.wall_side[1] = def.node_box.wall_side[1] - offset
764 opdef.node_box.wall_side[4] = def.node_box.wall_side[4] - offset
766 opdef.selection_box.wall_side[1] = def.selection_box.wall_side[1] - offset
767 opdef.selection_box.wall_side[4] = def.selection_box.wall_side[4] - offset
769 opdef.node_box.fixed[3] = def.node_box.fixed[3] + offset
770 opdef.node_box.fixed[6] = def.node_box.fixed[6] + offset
772 opdef.selection_box.fixed[3] = def.selection_box.fixed[3] + offset
773 opdef.selection_box.fixed[6] = def.selection_box.fixed[6] + offset
776 opdef.groups.not_in_creative_inventory = 1
777 opdef.tiles[3] = "signs_lib_pole_mount.png"
778 opdef.mesh = string.gsub(opdef.mesh, ".obj$", "_onpole.obj")
779 opdef.on_rotate = nil
782 if opdef.entity_info then
783 opdef.entity_info.mesh = string.gsub(opdef.entity_info.mesh, ".obj$", "_onpole.obj")
785 minetest.register_node(":"..name.."_onpole", opdef)
786 table.insert(signs_lib.lbm_restore_nodes, name.."_onpole")
789 if rdef.allow_hanging then
791 local hdef = table.copy(def)
792 hdef.paramtype2 = "facedir"
794 local hcbox = signs_lib.make_selection_boxes(35, 32, false, 0, 3, -18.5, true)
796 hdef.selection_box = rdef.hanging_selection_box or hcbox
797 hdef.node_box = rdef.hanging_node_box or rdef.hanging_selection_box or hcbox
799 hdef.groups.not_in_creative_inventory = 1
800 hdef.tiles[3] = "signs_lib_hangers.png"
801 hdef.mesh = string.gsub(string.gsub(hdef.mesh, "_facedir.obj", ".obj"), ".obj$", "_hanging.obj")
804 if hdef.entity_info then
805 hdef.entity_info.mesh = string.gsub(string.gsub(hdef.entity_info.mesh, "_facedir.obj", ".obj"), ".obj$", "_hanging.obj")
806 hdef.entity_info.yaw = signs_lib.standard_yaw
809 minetest.register_node(":"..name.."_hanging", hdef)
810 table.insert(signs_lib.lbm_restore_nodes, name.."_hanging")
815 The main sign registration function
816 ===================================
818 Example minimal recommended def for writable signs:
820 signs_lib.register_sign("foo:my_cool_sign", {
821 description = "Wooden cool sign",
822 inventory_image = "signs_lib_sign_cool_inv.png",
824 "signs_lib_sign_cool.png",
825 "signs_lib_sign_cool_edges.png"
835 entity_info = "standard"
838 * default def assumes a wallmounted sign with on-pole being allowed.
840 *For signs that can't support onpole, include in the def:
841 allow_onpole = false,
843 * "standard" entity info implies the standard wood/steel sign model, in
844 wallmounted mode. For facedir signs using the standard model, use:
847 mesh = "signs_lib_standard_wall_sign_entity.obj",
848 yaw = signs_lib.standard_yaw
853 function signs_lib.register_sign(name, rdef)
854 register_sign(name, rdef)
856 if rdef.allow_widefont then
858 wdef = table.copy(minetest.registered_items[name])
859 wdef.groups.not_in_creative_inventory = 1
860 wdef.horiz_scaling = wdef.horiz_scaling / 2
862 register_sign(name.."_widefont", wdef)
866 -- restore signs' text after /clearobjects and the like, the next time
867 -- a block is reloaded by the server.
869 minetest.register_lbm({
870 nodenames = signs_lib.lbm_restore_nodes,
871 name = "signs_lib:restore_sign_text",
872 label = "Restore sign text",
873 run_at_every_load = true,
874 action = function(pos, node)
875 signs_lib.update_sign(pos,nil,nil,node)
879 -- Convert old signs on fenceposts into signs on.. um.. fence posts :P
881 minetest.register_lbm({
882 nodenames = signs_lib.old_fenceposts_with_signs,
883 name = "signs_lib:fix_fencepost_signs",
884 label = "Change single-node signs on fences into normal",
885 run_at_every_load = true,
886 action = function(pos, node)
888 local fdir = node.param2 % 8
890 x = pos.x + signs_lib.fdir_to_back[fdir+1][1],
892 z = pos.z + signs_lib.fdir_to_back[fdir+1][2]
895 if minetest.get_node(signpos).name == "air" then
896 local new_wmdir = minetest.dir_to_wallmounted(minetest.facedir_to_dir(fdir))
897 local oldfence = signs_lib.old_fenceposts[node.name]
898 local newsign = signs_lib.old_fenceposts_replacement_signs[node.name]
900 signs_lib.delete_objects(pos)
902 local oldmeta = minetest.get_meta(pos):to_table()
903 minetest.set_node(pos, {name = oldfence})
904 minetest.set_node(signpos, { name = newsign, param2 = new_wmdir })
905 local newmeta = minetest.get_meta(signpos)
906 newmeta:from_table(oldmeta)
907 signs_lib.update_sign(signpos)
912 signs_lib.block_list = {}
913 signs_lib.totalblocks = 0
915 -- Maintain a list of currently-loaded blocks
916 minetest.register_lbm({
917 nodenames = {"group:sign"},
918 name = "signs_lib:update_block_list",
919 label = "Update list of loaded blocks, log only those with signs",
920 run_at_every_load = true,
921 action = function(pos, node)
922 -- yeah, yeah... I know I'm hashing a block pos, but it's still just a set of coords
923 local hash = minetest.hash_node_position(vector.floor(vector.divide(pos, core.MAP_BLOCKSIZE)))
924 if not signs_lib.block_list[hash] then
925 signs_lib.block_list[hash] = true
926 signs_lib.totalblocks = signs_lib.totalblocks + 1
931 minetest.register_chatcommand("regen_signs", {
933 privs = {server = true},
934 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.",
935 func = function(player_name, params)
938 for b in pairs(signs_lib.block_list) do
939 local blockpos = minetest.get_position_from_hash(b)
940 local pos1 = vector.multiply(blockpos, core.MAP_BLOCKSIZE)
941 local pos2 = vector.add(pos1, core.MAP_BLOCKSIZE - 1)
942 if minetest.get_node_or_nil(vector.add(pos1, core.MAP_BLOCKSIZE/2)) then
943 local signs_in_block = minetest.find_nodes_in_area(pos1, pos2, {"group:sign"})
944 allsigns[#allsigns + 1] = signs_in_block
945 totalsigns = totalsigns + #signs_in_block
947 signs_lib.block_list[b] = nil -- if the block is no longer loaded, remove it from the table
948 signs_lib.totalblocks = signs_lib.totalblocks - 1
951 if signs_lib.totalblocks < 0 then signs_lib.totalblocks = 0 end
952 if totalsigns == 0 then
953 minetest.chat_send_player(player_name, "There are no signs in the currently-loaded terrain.")
954 signs_lib.block_list = {}
958 minetest.chat_send_player(player_name, "Found a total of "..totalsigns.." sign nodes across "..signs_lib.totalblocks.." blocks.")
959 minetest.chat_send_player(player_name, "Regenerating sign entities...")
961 for _, b in pairs(allsigns) do
962 for _, pos in ipairs(b) do
963 signs_lib.delete_objects(pos)
964 local node = minetest.get_node(pos)
965 local def = minetest.registered_items[node.name]
966 if def and def.entity_info then
967 signs_lib.update_sign(pos)
971 minetest.chat_send_player(player_name, "Finished.")