]> git.lizzy.rs Git - signs_lib.git/blob - api.lua
get standard sign groups directly from the original default node defs
[signs_lib.git] / api.lua
1 -- signs_lib api, backported from street_signs
2
3 local S = signs_lib.gettext
4
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 = {}
10
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 = 2
19 signs_lib.standard_cpl = 35
20
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
24 signs_lib.standard_steel_groups = table.copy(minetest.registered_items["default:sign_wall_steel"].groups)
25 signs_lib.standard_steel_groups.sign = 1
26
27 signs_lib.standard_yaw = {
28         0,
29         math.pi / -2,
30         math.pi,
31         math.pi / 2,
32 }
33
34 signs_lib.wallmounted_yaw = {
35         nil,
36         nil,
37         math.pi / -2,
38         math.pi / 2,
39         0,
40         math.pi,
41 }
42
43 signs_lib.fdir_to_back = {
44         {  0, -1 },
45         { -1,  0 },
46         {  0,  1 },
47         {  1,  0 },
48 }
49
50 signs_lib.wall_fdir_to_back = {
51         nil,
52         nil,
53         {  0,  1 },
54         {  0, -1 },
55         { -1,  0 },
56         {  1,  0 },
57 }
58
59 signs_lib.rotate_facedir = {
60         [0] = 1,
61         [1] = 6,
62         [2] = 3,
63         [3] = 0,
64         [4] = 2,
65         [5] = 6,
66         [6] = 4
67 }
68
69 signs_lib.rotate_walldir = {
70         [0] = 1,
71         [1] = 5,
72         [2] = 0,
73         [3] = 4,
74         [4] = 2,
75         [5] = 3
76 }
77
78 -- Initialize character texture cache
79 local ctexcache = {}
80
81 signs_lib.wallmounted_rotate = function(pos, node, user, mode)
82         if not signs_lib.can_modify(pos, user) then return false end
83
84         if mode ~= screwdriver.ROTATE_FACE or string.match(node.name, "_onpole") then
85                 return false
86         end
87
88         local newparam2 = signs_lib.rotate_walldir[node.param2] or 0
89
90         minetest.swap_node(pos, { name = node.name, param2 = newparam2 })
91         for _, v in ipairs(minetest.get_objects_inside_radius(pos, 0.5)) do
92                 local e = v:get_luaentity()
93                 if e and e.name == "signs_lib:text" then
94                         v:remove()
95                 end
96         end
97         signs_lib.update_sign(pos)
98         return true
99 end
100
101 signs_lib.facedir_rotate = function(pos, node, user, mode)
102         if not signs_lib.can_modify(pos, user) then return false end
103
104         if mode ~= screwdriver.ROTATE_FACE or string.match(node.name, "_onpole") then
105                 return false
106         end
107
108         local newparam2 = signs_lib.rotate_facedir[node.param2] or 0
109
110         minetest.swap_node(pos, { name = node.name, param2 = newparam2 })
111         for _, v in ipairs(minetest.get_objects_inside_radius(pos, 0.5)) do
112                 local e = v:get_luaentity()
113                 if e and e.name == "signs_lib:text" then
114                         v:remove()
115                 end
116         end
117         signs_lib.update_sign(pos)
118         return true
119 end
120
121 local DEFAULT_TEXT_SCALE = {x=10, y=10}
122
123 -- infinite stacks
124
125 if not minetest.settings:get_bool("creative_mode") then
126         signs_lib.expect_infinite_stacks = false
127 else
128         signs_lib.expect_infinite_stacks = true
129 end
130
131 -- CONSTANTS
132
133 -- Path to the textures.
134 local TP = signs_lib.path .. "/textures"
135 -- Font file formatter
136 local CHAR_FILE = "%s_%02x.png"
137 -- Fonts path
138 local CHAR_PATH = TP .. "/" .. CHAR_FILE
139
140 -- Lots of overkill here. KISS advocates, go away, shoo! ;) -- kaeza
141
142 local PNG_HDR = string.char(0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A)
143
144 -- check if a file does exist
145 -- to avoid reopening file after checking again
146 -- pass TRUE as second argument
147 local function file_exists(name, return_handle, mode)
148         mode = mode or "r";
149         local f = io.open(name, mode)
150         if f ~= nil then
151                 if (return_handle) then
152                         return f
153                 end
154                 io.close(f) 
155                 return true 
156         else 
157                 return false 
158         end
159 end
160
161 -- Read the image size from a PNG file.
162 -- Returns image_w, image_h.
163 -- Only the LSB is read from each field!
164 function signs_lib.read_image_size(filename)
165         local f = file_exists(filename, true, "rb")
166         -- file might not exist (don't crash the game)
167         if (not f) then
168                 return 0, 0
169         end
170         f:seek("set", 0x0)
171         local hdr = f:read(string.len(PNG_HDR))
172         if hdr ~= PNG_HDR then
173                 f:close()
174                 return
175         end
176         f:seek("set", 0x13)
177         local ws = f:read(1)
178         f:seek("set", 0x17)
179         local hs = f:read(1)
180         f:close()
181         return ws:byte(), hs:byte()
182 end
183
184 -- 4 rows, max 80 chars per, plus a bit of fudge to
185 -- avoid excess trimming (e.g. due to color codes)
186
187 local MAX_INPUT_CHARS = 400
188
189 -- helper functions to trim sign text input/output
190
191 local function trim_input(text)
192         return text:sub(1, math.min(MAX_INPUT_CHARS, text:len()))
193 end
194
195 local function build_char_db(font_size)
196
197         local cw = {}
198
199         -- To calculate average char width.
200         local total_width = 0
201         local char_count = 0
202
203         for c = 32, 255 do
204                 local w, h = signs_lib.read_image_size(CHAR_PATH:format("signs_lib_font_"..font_size.."px", c))
205                 if w and h then
206                         local ch = string.char(c)
207                         cw[ch] = w
208                         total_width = total_width + w
209                         char_count = char_count + 1
210                 end
211         end
212
213         local cbw, cbh = signs_lib.read_image_size(TP.."/signs_lib_color_"..font_size.."px_n.png")
214         assert(cbw and cbh, "error reading bg dimensions")
215         return cw, cbw, cbh, (total_width / char_count)
216 end
217
218 signs_lib.charwidth15,
219 signs_lib.colorbgw15,
220 signs_lib.lineheight15,
221 signs_lib.avgwidth15 = build_char_db(15)
222
223 signs_lib.charwidth31,
224 signs_lib.colorbgw31,
225 signs_lib.lineheight31,
226 signs_lib.avgwidth31 = build_char_db(31)
227
228 local sign_groups = {choppy=2, dig_immediate=2}
229 local fences_with_sign = { }
230
231 -- some local helper functions
232
233 local function split_lines_and_words(text)
234         if not text then return end
235         local lines = { }
236         for _, line in ipairs(text:split("\n")) do
237                 table.insert(lines, line:split(" "))
238         end
239         return lines
240 end
241
242 local math_max = math.max
243
244 local function fill_line(x, y, w, c, font_size, colorbgw)
245         c = c or "0"
246         local tex = { }
247         for xx = 0, math.max(0, w), colorbgw do
248                 table.insert(tex, (":%d,%d=signs_lib_color_"..font_size.."px_%s.png"):format(x + xx, y, c))
249         end
250         return table.concat(tex)
251 end
252
253 -- make char texture file name
254 -- if texture file does not exist use fallback texture instead
255 local function char_tex(font_name, ch)
256         if ctexcache[font_name..ch] then
257                 return ctexcache[font_name..ch], true
258         else
259                 local c = ch:byte()
260                 local exists, tex = file_exists(CHAR_PATH:format(font_name, c))
261                 if exists and c ~= 14 then
262                         tex = CHAR_FILE:format(font_name, c)
263                 else
264                         tex = CHAR_FILE:format(font_name, 0x0)
265                 end
266                 ctexcache[font_name..ch] = tex
267                 return tex, exists
268         end
269 end
270
271 local function make_line_texture(line, lineno, pos, line_width, line_height, cwidth_tab, font_size, colorbgw)
272         local width = 0
273         local maxw = 0
274         local font_name = "signs_lib_font_"..font_size.."px"
275
276         local words = { }
277         local node = minetest.get_node(pos)
278         local def = minetest.registered_items[node.name]
279         local default_color = def.default_color or 0
280
281         local cur_color = tonumber(default_color, 16)
282
283         -- We check which chars are available here.
284         for word_i, word in ipairs(line) do
285                 local chars = { }
286                 local ch_offs = 0
287                 word = string.gsub(word, "%^[12345678abcdefgh]", {
288                         ["^1"] = string.char(0x81),
289                         ["^2"] = string.char(0x82),
290                         ["^3"] = string.char(0x83),
291                         ["^4"] = string.char(0x84),
292                         ["^5"] = string.char(0x85),
293                         ["^6"] = string.char(0x86),
294                         ["^7"] = string.char(0x87),
295                         ["^8"] = string.char(0x88),
296                         ["^a"] = string.char(0x8a),
297                         ["^b"] = string.char(0x8b),
298                         ["^c"] = string.char(0x8c),
299                         ["^d"] = string.char(0x8d),
300                         ["^e"] = string.char(0x8e),
301                         ["^f"] = string.char(0x8f),
302                         ["^g"] = string.char(0x90),
303                         ["^h"] = string.char(0x91)
304                 })
305                 local word_l = #word
306                 local i = 1
307                 while i <= word_l  do
308                         local c = word:sub(i, i)
309                         if c == "#" then
310                                 local cc = tonumber(word:sub(i+1, i+1), 16)
311                                 if cc then
312                                         i = i + 1
313                                         cur_color = cc
314                                 end
315                         else
316                                 local w = cwidth_tab[c]
317                                 if w then
318                                         width = width + w + 1
319                                         if width >= (line_width - cwidth_tab[" "]) then
320                                                 width = 0
321                                         else
322                                                 maxw = math_max(width, maxw)
323                                         end
324                                         if #chars < MAX_INPUT_CHARS then
325                                                 table.insert(chars, {
326                                                         off = ch_offs,
327                                                         tex = char_tex(font_name, c),
328                                                         col = ("%X"):format(cur_color),
329                                                 })
330                                         end
331                                         ch_offs = ch_offs + w
332                                 end
333                         end
334                         i = i + 1
335                 end
336                 width = width + cwidth_tab[" "] + 1
337                 maxw = math_max(width, maxw)
338                 table.insert(words, { chars=chars, w=ch_offs })
339         end
340
341         -- Okay, we actually build the "line texture" here.
342
343         local texture = { }
344
345         local start_xpos = math.floor((line_width - maxw) / 2) + def.x_offset
346
347         local xpos = start_xpos
348         local ypos = (line_height + def.line_spacing)* lineno + def.y_offset
349
350         cur_color = nil
351
352         for word_i, word in ipairs(words) do
353                 local xoffs = (xpos - start_xpos)
354                 if (xoffs > 0) and ((xoffs + word.w) > maxw) then
355                         table.insert(texture, fill_line(xpos, ypos, maxw, "n", font_size, colorbgw))
356                         xpos = start_xpos
357                         ypos = ypos + line_height + def.line_spacing
358                         lineno = lineno + 1
359                         if lineno >= def.number_of_lines then break end
360                         table.insert(texture, fill_line(xpos, ypos, maxw, cur_color, font_size, colorbgw))
361                 end
362                 for ch_i, ch in ipairs(word.chars) do
363                         if ch.col ~= cur_color then
364                                 cur_color = ch.col
365                                 table.insert(texture, fill_line(xpos + ch.off, ypos, maxw, cur_color, font_size, colorbgw))
366                         end
367                         table.insert(texture, (":%d,%d=%s"):format(xpos + ch.off, ypos, ch.tex))
368                 end
369                 table.insert(
370                         texture, 
371                         (":%d,%d="):format(xpos + word.w, ypos) .. char_tex(font_name, " ")
372                 )
373                 xpos = xpos + word.w + cwidth_tab[" "]
374                 if xpos >= (line_width + cwidth_tab[" "]) then break end
375         end
376
377         table.insert(texture, fill_line(xpos, ypos, maxw, "n", font_size, colorbgw))
378         table.insert(texture, fill_line(start_xpos, ypos + line_height, maxw, "n", font_size, colorbgw))
379
380         return table.concat(texture), lineno
381 end
382
383 local function make_sign_texture(lines, pos)
384         local node = minetest.get_node(pos)
385         local def = minetest.registered_items[node.name]
386
387         local font_size
388         local line_width
389         local line_height
390         local char_width
391         local colorbgw
392
393         if def.font_size and def.font_size == 31 then
394                 font_size = 31
395                 line_width = math.floor(signs_lib.avgwidth31 * def.chars_per_line) * def.horiz_scaling
396                 line_height = signs_lib.lineheight31
397                 char_width = signs_lib.charwidth31
398                 colorbgw = signs_lib.colorbgw31
399         else
400                 font_size = 15
401                 line_width = math.floor(signs_lib.avgwidth15 * def.chars_per_line) * def.horiz_scaling
402                 line_height = signs_lib.lineheight15
403                 char_width = signs_lib.charwidth15
404                 colorbgw = signs_lib.colorbgw15
405         end
406
407         local texture = { ("[combine:%dx%d"):format(line_width, (line_height + def.line_spacing) * def.number_of_lines * def.vert_scaling) }
408
409         local lineno = 0
410         for i = 1, #lines do
411                 if lineno >= def.number_of_lines then break end
412                 local linetex, ln = make_line_texture(lines[i], lineno, pos, line_width, line_height, char_width, font_size, colorbgw)
413                 table.insert(texture, linetex)
414                 lineno = ln + 1
415         end
416         table.insert(texture, "^[makealpha:0,0,0")
417         return table.concat(texture, "")
418 end
419
420 local function set_obj_text(obj, text, x, pos)
421         local split = split_lines_and_words
422         local text_ansi = Utf8ToAnsi(text)
423         local n = minetest.registered_nodes[minetest.get_node(pos).name]
424         local text_scale = (n and n.text_scale) or DEFAULT_TEXT_SCALE
425         local texture = make_sign_texture(split(text_ansi), pos)
426         obj:set_properties({
427                 textures={texture},
428                 visual_size = text_scale,
429         })
430 end
431
432 signs_lib.construct_sign = function(pos)
433         local meta = minetest.get_meta(pos)
434         meta:set_string(
435                 "formspec",
436                 "size[6,4]"..
437                 "textarea[0,-0.3;6.5,3;text;;${text}]"..
438                 "button_exit[2,3.4;2,1;ok;"..S("Write").."]"..
439                 "background[-0.5,-0.5;7,5;signs_lib_sign_bg.jpg]")
440         local i = meta:get_string("infotext")
441         if i == "" then -- it wasn't even set, so set it.
442                 meta:set_string("infotext", "")
443         end
444 end
445
446 function signs_lib.destruct_sign(pos)
447         local objects = minetest.get_objects_inside_radius(pos, 0.5)
448         for _, v in ipairs(objects) do
449                 local e = v:get_luaentity()
450                 if e and e.name == "signs_lib:text" then
451                         v:remove()
452                 end
453         end
454 end
455
456 local function make_infotext(text)
457         text = trim_input(text)
458         local lines = split_lines_and_words(text) or {}
459         local lines2 = { }
460         for _, line in ipairs(lines) do
461                 table.insert(lines2, (table.concat(line, " "):gsub("#[0-9a-fA-F]", ""):gsub("##", "#")))
462         end
463         return table.concat(lines2, "\n")
464 end
465
466 function signs_lib.update_sign(pos, fields)
467         local meta = minetest.get_meta(pos)
468
469         local text = fields and fields.text or meta:get_string("text")
470         text = trim_input(text)
471
472         local owner = meta:get_string("owner")
473         ownstr = ""
474         if owner ~= "" then ownstr = S("Locked sign, owned by @1\n", owner) end
475
476         meta:set_string("text", text)
477         meta:set_string("infotext", ownstr..make_infotext(text).." ")
478
479         local objects = minetest.get_objects_inside_radius(pos, 0.5)
480         local found
481         for _, v in ipairs(objects) do
482                 local e = v:get_luaentity()
483                 if e and e.name == "signs_lib:text" then
484                         if found then
485                                 v:remove()
486                         else
487                                 set_obj_text(v, text, nil, pos)
488                                 found = true
489                         end
490                 end
491         end
492         if found then
493                 return
494         end
495
496         -- if there is no entity
497         local signnode = minetest.get_node(pos)
498         local signname = signnode.name
499         local def = minetest.registered_items[signname]
500         if not def.entity_info or not def.entity_info.yaw[signnode.param2 + 1] then return end
501         local obj = minetest.add_entity(pos, "signs_lib:text")
502
503         obj:setyaw(def.entity_info.yaw[signnode.param2 + 1])
504         obj:set_properties({
505                 mesh = def.entity_info.mesh,
506         })
507 end
508
509 function signs_lib.receive_fields(pos, formname, fields, sender)
510         if fields and fields.text and fields.ok and signs_lib.can_modify(pos, sender) then
511                 minetest.log("action", S("@1 wrote \"@2\" to sign at @3",
512                         (sender:get_player_name() or ""),
513                         fields.text:gsub('\\', '\\\\'):gsub("\n", "\\n"),
514                         minetest.pos_to_string(pos)
515                 ))
516                 signs_lib.update_sign(pos, fields)
517         end
518 end
519
520 function signs_lib.can_modify(pos, player)
521         local meta = minetest.get_meta(pos)
522         local owner = meta:get_string("owner")
523         local playername = player:get_player_name()
524
525         if minetest.is_protected(pos, playername) then 
526                 minetest.record_protection_violation(pos, playername)
527                 return false
528         end
529
530         if owner == ""
531           or playername == owner
532           or (minetest.check_player_privs(playername, {sign_editor=true}))
533           or (playername == minetest.settings:get("name")) then
534                 return true
535         end
536         minetest.record_protection_violation(pos, playername)
537         return false
538 end
539
540 local signs_text_on_activate = function(self)
541         local pos = self.object:getpos()
542         local meta = minetest.get_meta(pos)
543         local signnode = minetest.get_node(pos)
544         local signname = signnode.name
545         local def = minetest.registered_items[signname]
546         local text = meta:get_string("text")
547         if text and def and def.entity_info then
548                 text = trim_input(text)
549                 set_obj_text(self.object, text, nil, pos)
550                 self.object:set_properties({
551                         mesh = def.entity_info.mesh,
552                 })
553         end
554 end
555
556 minetest.register_entity("signs_lib:text", {
557         collisionbox = { 0, 0, 0, 0, 0, 0 },
558         visual = "mesh",
559         mesh = "signs_lib_standard_wall_sign_entity.obj",
560         textures = {},
561         on_activate = signs_text_on_activate,
562 })
563
564 -- make selection boxes
565 -- sizex/sizey specified in inches because that's what MUTCD uses.
566
567 function signs_lib.make_selection_boxes(sizex, sizey, onpole, xoffs, yoffs, zoffs, fdir)
568
569         local tx = (sizex * 0.0254 ) / 2
570         local ty = (sizey * 0.0254 ) / 2
571         local xo = xoffs and xoffs * 0.0254 or 0
572         local yo = yoffs and yoffs * 0.0254 or 0
573         local zo = zoffs and zoffs * 0.0254 or 0
574
575         if onpole == "_onpole" then
576                 if not fdir then
577                         return {
578                                 type = "wallmounted",
579                                 wall_side =   { -0.5 - 0.3125 + zo, -ty + yo, -tx + xo, -0.4375 - 0.3125 + zo, ty + yo , tx + xo },
580                                 wall_top =    {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5 },
581                                 wall_bottom = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5 },
582                         }
583                 else
584                         return {
585                                 type = "fixed",
586                                 fixed = { -tx + xo, -ty + yo, 0.5 + 0.3125 + zo, tx + xo, ty + yo, 0.4375 + 0.3125 + zo }
587                         }
588                 end
589         else
590                 if not fdir then
591                         return {
592                                 type = "wallmounted",
593                                 wall_side =   { -0.5 + zo, -ty + yo, -tx + xo, -0.4375 + zo, ty + yo, tx + xo },
594                                 wall_top =    { -tx - xo, 0.5 + zo, -ty + yo, tx - xo, 0.4375 + zo, ty + yo},
595                                 wall_bottom = { -tx - xo, -0.5 + zo, -ty + yo, tx - xo, -0.4375 + zo, ty + yo }
596                         }
597                 else
598                         return {
599                                 type = "fixed",
600                                 fixed = { -tx + xo, -ty + yo, 0.5 + zo, tx + xo, ty + yo, 0.4375 + zo}
601                         }
602                 end
603         end
604 end
605
606 function signs_lib.check_for_pole(pos, pointed_thing)
607         local ppos = minetest.get_pointed_thing_position(pointed_thing)
608         local pnode = minetest.get_node(ppos)
609         local pdef = minetest.registered_items[pnode.name]
610
611         print(dump(pos))
612         print(dump(ppos))
613
614         if (signs_lib.allowed_poles[pnode.name]
615                   or (pdef and pdef.drawtype == "fencelike")
616                   or string.find(pnode.name, "default:fence_")
617                   or string.find(pnode.name, "_post")
618                   or string.find(pnode.name, "fencepost")
619                   or (pnode.name == "streets:bigpole" and pnode.param2 < 4)
620                   or (pnode.name == "streets:bigpole" and pnode.param2 > 19 and pnode.param2 < 24)
621                 )
622           and
623                 (pos.x ~= ppos.x or pos.z ~= ppos.z) then
624                 print("signs_lib.check_for_pole returned true")
625                 return true
626         end
627 end
628
629 function signs_lib.after_place_node(pos, placer, itemstack, pointed_thing, locked)
630         local ppos = minetest.get_pointed_thing_position(pointed_thing)
631         local pnode = minetest.get_node(ppos)
632         local pdef = minetest.registered_items[pnode.name]
633         local playername = placer:get_player_name()
634
635         if signs_lib.check_for_pole(pos, pointed_thing) then
636                 local node = minetest.get_node(pos)
637                 minetest.swap_node(pos, {name = itemstack:get_name().."_onpole", param2 = node.param2})
638         end
639         if locked then
640                 local meta = minetest.get_meta(pos)
641                 meta:set_string("owner", playername)
642                 meta:set_string("infotext", S("Locked sign, owned by @1\n", playername))
643         end
644 end
645
646 function signs_lib.register_fence_with_sign()
647         minetest.log("warning", "[signs_lib] ".."Attempt to call no longer used function signs_lib.register_fence_with_sign()")
648 end
649
650 -- restore signs' text after /clearobjects and the like, the next time
651 -- a block is reloaded by the server.
652
653 minetest.register_lbm({
654         nodenames = signs_lib.lbm_restore_nodes,
655         name = "signs_lib:restore_sign_text",
656         label = "Restore sign text",
657         run_at_every_load = true,
658         action = function(pos, node)
659                 signs_lib.update_sign(pos,nil,nil,node)
660         end
661 })
662
663 -- Convert old signs on fenceposts into signs on.. um.. fence posts :P
664
665 minetest.register_lbm({
666         nodenames = signs_lib.old_fenceposts_with_signs,
667         name = "signs_lib:fix_fencepost_signs",
668         label = "Change single-node signs on fences into normal",
669         run_at_every_load = true,
670         action = function(pos, node)
671
672                 local fdir = node.param2 % 8
673                 local signpos = {
674                         x = pos.x + signs_lib.fdir_to_back[fdir+1][1],
675                         y = pos.y,
676                         z = pos.z + signs_lib.fdir_to_back[fdir+1][2]
677                 }
678
679                 if minetest.get_node(signpos).name == "air" then
680                         local new_wmdir = minetest.dir_to_wallmounted(minetest.facedir_to_dir(fdir))
681                         local oldfence =  signs_lib.old_fenceposts[node.name]
682                         local newsign =   signs_lib.old_fenceposts_replacement_signs[node.name]
683
684                         for _, v in ipairs(minetest.get_objects_inside_radius(pos, 0.5)) do
685                                 local e = v:get_luaentity()
686                                 if e and e.name == "signs_lib:text" then
687                                         v:remove()
688                                 end
689                         end
690
691                         local oldmeta = minetest.get_meta(pos):to_table()
692                         minetest.set_node(pos, {name = oldfence})
693                         minetest.set_node(signpos, { name = newsign, param2 = new_wmdir })
694                         local newmeta = minetest.get_meta(signpos)
695                         newmeta:from_table(oldmeta)
696                         signs_lib.update_sign(signpos)
697                 end
698         end
699 })
700
701 signs_lib.block_list = {}
702 signs_lib.totalblocks = 0
703
704 -- Maintain a list of currently-loaded blocks
705 minetest.register_lbm({
706         nodenames = {"group:sign"},
707         name = "signs_lib:update_block_list",
708         label = "Update list of loaded blocks, log only those with signs",
709         run_at_every_load = true,
710         action = function(pos, node)
711                 -- yeah, yeah... I know I'm hashing a block pos, but it's still just a set of coords
712                 local hash = minetest.hash_node_position(vector.floor(vector.divide(pos, core.MAP_BLOCKSIZE)))
713                 if not signs_lib.block_list[hash] then
714                         signs_lib.block_list[hash] = true
715                         signs_lib.totalblocks = signs_lib.totalblocks + 1
716                 end
717         end
718 })
719
720 minetest.register_chatcommand("regen_signs", {
721         params = "",
722         privs = {server = true},
723         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.",
724         func = function(player_name, params)
725                 local allsigns = {}
726                 local totalsigns = 0
727                 for b in pairs(signs_lib.block_list) do
728                         local blockpos = minetest.get_position_from_hash(b)
729                         local pos1 = vector.multiply(blockpos, core.MAP_BLOCKSIZE)
730                         local pos2 = vector.add(pos1, core.MAP_BLOCKSIZE - 1)
731                         if minetest.get_node_or_nil(vector.add(pos1, core.MAP_BLOCKSIZE/2)) then
732                                 local signs_in_block = minetest.find_nodes_in_area(pos1, pos2, {"group:sign"})
733                                 allsigns[#allsigns + 1] = signs_in_block
734                                 totalsigns = totalsigns + #signs_in_block
735                         else
736                                 signs_lib.block_list[b] = nil -- if the block is no longer loaded, remove it from the table
737                                 signs_lib.totalblocks = signs_lib.totalblocks - 1
738                         end
739                 end
740                 if signs_lib.totalblocks < 0 then signs_lib.totalblocks = 0 end
741                 if totalsigns == 0 then
742                         minetest.chat_send_player(player_name, "There are no signs in the currently-loaded terrain.")
743                         signs_lib.block_list = {}
744                         return
745                 end
746
747                 minetest.chat_send_player(player_name, "Found a total of "..totalsigns.." sign nodes across "..signs_lib.totalblocks.." blocks.")
748                 minetest.chat_send_player(player_name, "Regenerating sign entities...")
749
750                 for _, b in pairs(allsigns) do
751                         for _, pos in ipairs(b) do
752                                 local objects = minetest.get_objects_inside_radius(pos, 0.5)
753                                 if #objects > 0 then
754                                         for _, v in ipairs(objects) do
755                                                 v:remove()
756                                         end
757                                 end
758                                 local node = minetest.get_node(pos)
759                                 local def = minetest.registered_items[node.name]
760                                 if def and def.entity_info then
761                                         signs_lib.update_sign(pos)
762                                 end
763                         end
764                 end
765                 minetest.chat_send_player(player_name, "Finished.")
766         end
767 })