]> git.lizzy.rs Git - signs_lib.git/blob - api.lua
d19a643cae274d33154ca81e70d2778a2b205721
[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 = 0
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 signs_lib.standard_wood_groups.attached_node = nil
24
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
28
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)
31
32 signs_lib.default_text_scale = {x=10, y=10}
33
34 signs_lib.standard_yaw = {
35         0,
36         math.pi / -2,
37         math.pi,
38         math.pi / 2,
39 }
40
41 signs_lib.wallmounted_yaw = {
42         nil,
43         nil,
44         math.pi / -2,
45         math.pi / 2,
46         0,
47         math.pi,
48 }
49
50 signs_lib.fdir_to_back = {
51         {  0, -1 },
52         { -1,  0 },
53         {  0,  1 },
54         {  1,  0 },
55 }
56
57 signs_lib.wall_fdir_to_back = {
58         nil,
59         nil,
60         {  0,  1 },
61         {  0, -1 },
62         { -1,  0 },
63         {  1,  0 },
64 }
65
66 signs_lib.rotate_facedir = {
67         [0] = 1,
68         [1] = 6,
69         [2] = 3,
70         [3] = 0,
71         [4] = 2,
72         [5] = 6,
73         [6] = 4
74 }
75
76 signs_lib.rotate_walldir = {
77         [0] = 1,
78         [1] = 5,
79         [2] = 0,
80         [3] = 4,
81         [4] = 2,
82         [5] = 3
83 }
84
85 -- Initialize character texture cache
86 local ctexcache = {}
87
88 -- entity handling
89
90 minetest.register_entity("signs_lib:text", {
91         collisionbox = { 0, 0, 0, 0, 0, 0 },
92         visual = "mesh",
93         mesh = "signs_lib_standard_wall_sign_entity.obj",
94         textures = {},
95         static_save = false
96 })
97
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
102                 v:remove()
103         end
104 end
105
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
111
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)
114         local obj
115
116         if #objects > 0 then
117                 obj = objects[1]
118         else
119                 obj = minetest.add_entity(pos, "signs_lib:text")
120         end
121
122         obj:setyaw(def.entity_info.yaw[node.param2 + 1])
123
124         if not texture then
125                 obj:set_properties({
126                         mesh = def.entity_info.mesh,
127                         visual_size = text_scale,
128                 })
129         else
130                 obj:set_properties({
131                         mesh = def.entity_info.mesh,
132                         visual_size = text_scale,
133                         textures={texture},
134                 })
135         end
136 end
137
138 -- rotation
139
140 function signs_lib.wallmounted_rotate(pos, node, user, mode)
141         if not signs_lib.can_modify(pos, user) then return false end
142
143         if mode ~= screwdriver.ROTATE_FACE or string.match(node.name, "_onpole") then
144                 return false
145         end
146
147         local newparam2 = signs_lib.rotate_walldir[node.param2] or 0
148
149         minetest.swap_node(pos, { name = node.name, param2 = newparam2 })
150         signs_lib.delete_objects(pos)
151         signs_lib.update_sign(pos)
152         return true
153 end
154
155 function signs_lib.facedir_rotate(pos, node, user, mode)
156         if not signs_lib.can_modify(pos, user) then return false end
157
158         if mode ~= screwdriver.ROTATE_FACE or string.match(node.name, "_onpole") then
159                 return false
160         end
161
162         local newparam2 = signs_lib.rotate_facedir[node.param2] or 0
163
164         minetest.swap_node(pos, { name = node.name, param2 = newparam2 })
165         signs_lib.delete_objects(pos)
166         signs_lib.update_sign(pos)
167         return true
168 end
169
170 -- infinite stacks
171
172 if not minetest.settings:get_bool("creative_mode") then
173         signs_lib.expect_infinite_stacks = false
174 else
175         signs_lib.expect_infinite_stacks = true
176 end
177
178 -- CONSTANTS
179
180 -- Path to the textures.
181 local TP = signs_lib.path .. "/textures"
182 -- Font file formatter
183 local CHAR_FILE = "%s_%02x.png"
184 -- Fonts path
185 local CHAR_PATH = TP .. "/" .. CHAR_FILE
186
187 -- Lots of overkill here. KISS advocates, go away, shoo! ;) -- kaeza
188
189 local PNG_HDR = string.char(0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A)
190
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)
195         mode = mode or "r";
196         local f = io.open(name, mode)
197         if f ~= nil then
198                 if (return_handle) then
199                         return f
200                 end
201                 io.close(f) 
202                 return true 
203         else 
204                 return false 
205         end
206 end
207
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)
214         if (not f) then
215                 return 0, 0
216         end
217         f:seek("set", 0x0)
218         local hdr = f:read(string.len(PNG_HDR))
219         if hdr ~= PNG_HDR then
220                 f:close()
221                 return
222         end
223         f:seek("set", 0x13)
224         local ws = f:read(1)
225         f:seek("set", 0x17)
226         local hs = f:read(1)
227         f:close()
228         return ws:byte(), hs:byte()
229 end
230
231 -- 4 rows, max 80 chars per, plus a bit of fudge to
232 -- avoid excess trimming (e.g. due to color codes)
233
234 local MAX_INPUT_CHARS = 400
235
236 -- helper functions to trim sign text input/output
237
238 local function trim_input(text)
239         return text:sub(1, math.min(MAX_INPUT_CHARS, text:len()))
240 end
241
242 local function build_char_db(font_size)
243
244         local cw = {}
245
246         -- To calculate average char width.
247         local total_width = 0
248         local char_count = 0
249
250         for c = 32, 255 do
251                 local w, h = signs_lib.read_image_size(CHAR_PATH:format("signs_lib_font_"..font_size.."px", c))
252                 if w and h then
253                         local ch = string.char(c)
254                         cw[ch] = w
255                         total_width = total_width + w
256                         char_count = char_count + 1
257                 end
258         end
259
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)
263 end
264
265 signs_lib.charwidth15,
266 signs_lib.colorbgw15,
267 signs_lib.lineheight15,
268 signs_lib.avgwidth15 = build_char_db(15)
269
270 signs_lib.charwidth31,
271 signs_lib.colorbgw31,
272 signs_lib.lineheight31,
273 signs_lib.avgwidth31 = build_char_db(31)
274
275 local sign_groups = {choppy=2, dig_immediate=2}
276 local fences_with_sign = { }
277
278 -- some local helper functions
279
280 local math_max = math.max
281
282 local function fill_line(x, y, w, c, font_size, colorbgw)
283         c = c or "0"
284         local tex = { }
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))
287         end
288         return table.concat(tex)
289 end
290
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
296         else
297                 local c = ch:byte()
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)
301                 else
302                         tex = CHAR_FILE:format(font_name, 0x0)
303                 end
304                 ctexcache[font_name..ch] = tex
305                 return tex, exists
306         end
307 end
308
309 local function make_line_texture(line, lineno, pos, line_width, line_height, cwidth_tab, font_size, colorbgw)
310         local width = 0
311         local maxw = 0
312         local font_name = "signs_lib_font_"..font_size.."px"
313
314         local words = { }
315         local node = minetest.get_node(pos)
316         local def = minetest.registered_items[node.name]
317         local default_color = def.default_color or 0
318
319         local cur_color = tonumber(default_color, 16)
320
321         -- We check which chars are available here.
322         for word_i, word in ipairs(line) do
323                 local chars = { }
324                 local ch_offs = 0
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)
342                 })
343                 local word_l = #word
344                 local i = 1
345                 while i <= word_l  do
346                         local c = word:sub(i, i)
347                         if c == "#" then
348                                 local cc = tonumber(word:sub(i+1, i+1), 16)
349                                 if cc then
350                                         i = i + 1
351                                         cur_color = cc
352                                 end
353                         else
354                                 local w = cwidth_tab[c]
355                                 if w then
356                                         width = width + w + 1
357                                         if width >= (line_width - cwidth_tab[" "]) then
358                                                 width = 0
359                                         else
360                                                 maxw = math_max(width, maxw)
361                                         end
362                                         if #chars < MAX_INPUT_CHARS then
363                                                 table.insert(chars, {
364                                                         off = ch_offs,
365                                                         tex = char_tex(font_name, c),
366                                                         col = ("%X"):format(cur_color),
367                                                 })
368                                         end
369                                         ch_offs = ch_offs + w
370                                 end
371                         end
372                         i = i + 1
373                 end
374                 width = width + cwidth_tab[" "] + 1
375                 maxw = math_max(width, maxw)
376                 table.insert(words, { chars=chars, w=ch_offs })
377         end
378
379         -- Okay, we actually build the "line texture" here.
380
381         local texture = { }
382
383         local start_xpos = math.floor((line_width - maxw) / 2) + def.x_offset
384
385         local xpos = start_xpos
386         local ypos = (line_height + def.line_spacing)* lineno + def.y_offset
387
388         cur_color = nil
389
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))
394                         xpos = start_xpos
395                         ypos = ypos + line_height + def.line_spacing
396                         lineno = lineno + 1
397                         if lineno >= def.number_of_lines then break end
398                         table.insert(texture, fill_line(xpos, ypos, maxw, cur_color, font_size, colorbgw))
399                 end
400                 for ch_i, ch in ipairs(word.chars) do
401                         if ch.col ~= cur_color then
402                                 cur_color = ch.col
403                                 table.insert(texture, fill_line(xpos + ch.off, ypos, maxw, cur_color, font_size, colorbgw))
404                         end
405                         table.insert(texture, (":%d,%d=%s"):format(xpos + ch.off, ypos, ch.tex))
406                 end
407                 table.insert(
408                         texture, 
409                         (":%d,%d="):format(xpos + word.w, ypos) .. char_tex(font_name, " ")
410                 )
411                 xpos = xpos + word.w + cwidth_tab[" "]
412                 if xpos >= (line_width + cwidth_tab[" "]) then break end
413         end
414
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))
417
418         return table.concat(texture), lineno
419 end
420
421 local function make_sign_texture(lines, pos)
422         local node = minetest.get_node(pos)
423         local def = minetest.registered_items[node.name]
424
425         local font_size
426         local line_width
427         local line_height
428         local char_width
429         local colorbgw
430
431         if def.font_size and def.font_size == 31 then
432                 font_size = 31
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
437         else
438                 font_size = 15
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
443         end
444
445         local texture = { ("[combine:%dx%d"):format(line_width, (line_height + def.line_spacing) * def.number_of_lines * def.vert_scaling) }
446
447         local lineno = 0
448         for i = 1, #lines do
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)
452                 lineno = ln + 1
453         end
454         table.insert(texture, "^[makealpha:0,0,0")
455         return table.concat(texture, "")
456 end
457
458 function signs_lib.split_lines_and_words(text)
459         if not text then return end
460         local lines = { }
461         for _, line in ipairs(text:split("\n")) do
462                 table.insert(lines, line:split(" "))
463         end
464         return lines
465 end
466
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))
474 end
475
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")
482         else
483                 return name.."_widefont"
484         end
485 end
486
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)
493
494         if minetest.registered_items[wname] then
495                 local state = "off"
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_"..
499                         state..".png;"..
500                         state..";;;false;signs_lib_switch_interm.png]"..
501                         "button_exit[3,3.4;2,1;ok;"..S("Write").."]"
502         else
503                 form = form.."button_exit[2,3.4;2,1;ok;"..S("Write").."]"
504         end
505
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", "")
511         end
512 end
513
514 function signs_lib.destruct_sign(pos)
515         signs_lib.delete_objects(pos)
516 end
517
518 local function make_infotext(text)
519         text = trim_input(text)
520         local lines = signs_lib.split_lines_and_words(text) or {}
521         local lines2 = { }
522         for _, line in ipairs(lines) do
523                 table.insert(lines2, (table.concat(line, " "):gsub("#[0-9a-fA-F]", ""):gsub("##", "#")))
524         end
525         return table.concat(lines2, "\n")
526 end
527
528 function signs_lib.update_sign(pos, fields)
529         local meta = minetest.get_meta(pos)
530
531         local text = fields and fields.text or meta:get_string("text")
532         text = trim_input(text)
533
534         local owner = meta:get_string("owner")
535         ownstr = ""
536         if owner ~= "" then ownstr = S("Locked sign, owned by @1\n", owner) end
537
538         meta:set_string("text", text)
539         meta:set_string("infotext", ownstr..make_infotext(text).." ")
540         signs_lib.set_obj_text(pos, text)
541 end
542
543 function signs_lib.receive_fields(pos, formname, fields, sender)
544
545         if not fields or not signs_lib.can_modify(pos, sender) then return end
546
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)
552                 ))
553                 signs_lib.update_sign(pos, fields)
554         elseif fields.on or fields.off then
555                 local node = minetest.get_node(pos)
556                 local newname
557
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)
562                 end
563                 if newname then
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)
568                         ))
569
570                         minetest.swap_node(pos, {name = newname, param2 = node.param2})
571                         signs_lib.construct_sign(pos)
572                         signs_lib.update_sign(pos, fields)
573                 end
574         end
575 end
576
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()
581
582         if minetest.is_protected(pos, playername) then 
583                 minetest.record_protection_violation(pos, playername)
584                 return false
585         end
586
587         if owner == ""
588           or playername == owner
589           or (minetest.check_player_privs(playername, {sign_editor=true}))
590           or (playername == minetest.settings:get("name")) then
591                 return true
592         end
593         minetest.record_protection_violation(pos, playername)
594         return false
595 end
596
597 -- make selection boxes
598 -- sizex/sizey specified in inches because that's what MUTCD uses.
599
600 function signs_lib.make_selection_boxes(sizex, sizey, foo, xoffs, yoffs, zoffs, is_facedir)
601
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
607
608         if not is_facedir then
609                 return {
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 }
614                 }
615         else
616                 return {
617                         type = "fixed",
618                         fixed = { -tx + xo, -ty + yo, 0.5 + zo, tx + xo, ty + yo, 0.4375 + zo}
619                 }
620         end
621 end
622
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]
627
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)
636                 )
637           and
638                 (pos.x ~= ppos.x or pos.z ~= ppos.z) then
639                 return true
640         end
641 end
642
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
647                 return true
648         end
649 end
650
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()]
655
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]
659
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})
667         end
668         if locked then
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))
672         end
673 end
674
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()")
677 end
678
679 local function register_sign(name, rdef)
680         local def = table.copy(rdef)
681
682         if rdef.entity_info == "standard" then
683                 def.entity_info = {
684                         mesh = "signs_lib_standard_wall_sign_entity.obj",
685                         yaw = signs_lib.wallmounted_yaw
686                 }
687         elseif rdef.entity_info then
688                 def.entity_info = rdef.entity_info
689         end
690
691         def.after_place_node = rdef.after_place_node or signs_lib.after_place_node
692
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)
711                         end
712                 end
713         end
714
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"
723
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
728         else
729                 def.on_rotate = signs_lib.facedir_rotate
730         end
731
732         if rdef.groups then
733                 def.groups = rdef.groups
734         else
735                 def.groups = signs_lib.standard_wood_groups
736         end
737
738         local cbox = signs_lib.make_selection_boxes(35, 25, allow_onpole)
739
740         def.selection_box = rdef.selection_box or cbox
741         def.node_box      = table.copy(rdef.node_box or rdef.selection_box or cbox)
742
743         if def.sunlight_propagates ~= false then
744                 def.sunlight_propagates = true
745         end
746
747         minetest.register_node(":"..name, def)
748         table.insert(signs_lib.lbm_restore_nodes, name)
749
750         if rdef.allow_onpole ~= false then
751
752                 local opdef = table.copy(def)
753
754                 local offset = 0.3125
755                 if opdef.uses_slim_pole_mount then
756                         offset = 0.35
757                 end
758
759                 opdef.selection_box = rdef.onpole_selection_box or opdef.selection_box
760                 opdef.node_box = rdef.onpole_node_box or opdef.selection_box
761
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
765
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
768                 else
769                         opdef.node_box.fixed[3] = def.node_box.fixed[3] + offset
770                         opdef.node_box.fixed[6] = def.node_box.fixed[6] + offset
771
772                         opdef.selection_box.fixed[3] = def.selection_box.fixed[3] + offset
773                         opdef.selection_box.fixed[6] = def.selection_box.fixed[6] + offset
774                 end
775
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
780
781
782                 if opdef.entity_info then
783                         opdef.entity_info.mesh = string.gsub(opdef.entity_info.mesh, ".obj$", "_onpole.obj")
784                 end
785                 minetest.register_node(":"..name.."_onpole", opdef)
786                 table.insert(signs_lib.lbm_restore_nodes, name.."_onpole")
787         end
788
789         if rdef.allow_hanging then
790
791                 local hdef = table.copy(def)
792                 hdef.paramtype2 = "facedir"
793
794                 local hcbox = signs_lib.make_selection_boxes(35, 32, false, 0, 3, -18.5, true)
795
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
798
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")
802                 hdef.on_rotate = nil
803
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
807                 end
808
809                 minetest.register_node(":"..name.."_hanging", hdef)
810                 table.insert(signs_lib.lbm_restore_nodes, name.."_hanging")
811         end
812 end
813
814 --[[
815 The main sign registration function
816 ===================================
817
818 Example minimal recommended def for writable signs:
819
820 signs_lib.register_sign("foo:my_cool_sign", {
821         description = "Wooden cool sign",
822         inventory_image = "signs_lib_sign_cool_inv.png",
823         tiles = {
824                 "signs_lib_sign_cool.png",
825                 "signs_lib_sign_cool_edges.png"
826         },
827         number_of_lines = 2,
828         horiz_scaling = 0.8,
829         vert_scaling = 1,
830         line_spacing = 9,
831         font_size = 31,
832         x_offset = 7,
833         y_offset = 4,
834         chars_per_line = 40,
835         entity_info = "standard"
836 })
837
838 * default def assumes a wallmounted sign with on-pole being allowed.
839
840 *For signs that can't support onpole, include in the def:
841         allow_onpole = false,
842
843 * "standard" entity info implies the standard wood/steel sign model, in
844   wallmounted mode.  For facedir signs using the standard model, use:
845
846         entity_info = {
847                 mesh = "signs_lib_standard_wall_sign_entity.obj",
848                 yaw = signs_lib.standard_yaw
849         },
850
851 ]]--
852
853 function signs_lib.register_sign(name, rdef)
854         register_sign(name, rdef)
855
856         if rdef.allow_widefont then
857
858                 wdef = table.copy(minetest.registered_items[name])
859                 wdef.groups.not_in_creative_inventory = 1
860                 wdef.horiz_scaling = wdef.horiz_scaling / 2
861
862                 register_sign(name.."_widefont", wdef)
863         end
864 end
865
866 -- restore signs' text after /clearobjects and the like, the next time
867 -- a block is reloaded by the server.
868
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)
876         end
877 })
878
879 -- Convert old signs on fenceposts into signs on.. um.. fence posts :P
880
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)
887
888                 local fdir = node.param2 % 8
889                 local signpos = {
890                         x = pos.x + signs_lib.fdir_to_back[fdir+1][1],
891                         y = pos.y,
892                         z = pos.z + signs_lib.fdir_to_back[fdir+1][2]
893                 }
894
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]
899
900                         signs_lib.delete_objects(pos)
901
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)
908                 end
909         end
910 })
911
912 signs_lib.block_list = {}
913 signs_lib.totalblocks = 0
914
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
927                 end
928         end
929 })
930
931 minetest.register_chatcommand("regen_signs", {
932         params = "",
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)
936                 local allsigns = {}
937                 local totalsigns = 0
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
946                         else
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
949                         end
950                 end
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 = {}
955                         return
956                 end
957
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...")
960
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)
968                                 end
969                         end
970                 end
971                 minetest.chat_send_player(player_name, "Finished.")
972         end
973 })