]> git.lizzy.rs Git - signs_lib.git/blob - init.lua
Fix crash if sign is "unknown block"
[signs_lib.git] / init.lua
1 -- This mod provides the visible text on signs library used by Home Decor
2 -- and perhaps other mods at some point in the future.  Forked from thexyz's/
3 -- PilzAdam's original text-on-signs mod and rewritten by Vanessa Ezekowitz
4 -- and Diego Martinez
5
6 -- textpos = {
7 --              { delta = {entity position for 0° yaw}, exact yaw expression }
8 --              { delta = {entity position for 180° yaw}, exact yaw expression }
9 --              { delta = {entity position for 270° yaw}, exact yaw expression }
10 --              { delta = {entity position for 90° yaw}, exact yaw expression }
11 -- }
12 -- Made colored metal signs optionals
13 local enable_colored_metal_signs = true
14
15 -- CWz's keyword interact mod uses this setting.
16 local current_keyword = minetest.settings:get("interact_keyword") or "iaccept"
17
18 signs_lib = {}
19 signs_lib.path = minetest.get_modpath(minetest.get_current_modname())
20 screwdriver = screwdriver or {}
21
22 -- Load support for intllib.
23 local S, NS = dofile(signs_lib.path .. "/intllib.lua")
24 signs_lib.gettext = S
25
26 -- text encoding
27 dofile(signs_lib.path .. "/encoding.lua");
28
29 signs_lib.wallmounted_rotate = function(pos, node, user, mode, new_param2)
30         if mode ~= screwdriver.ROTATE_AXIS then return false end
31         minetest.swap_node(pos, {name = node.name, param2 = (node.param2 + 1) % 6})
32         for _, v in ipairs(minetest.get_objects_inside_radius(pos, 0.5)) do
33                 local e = v:get_luaentity()
34                 if e and e.name == "signs:text" then
35                         v:remove()
36                 end
37         end
38         signs_lib.update_sign(pos)
39         return true
40 end
41
42 signs_lib.modpath = minetest.get_modpath("signs_lib")
43
44 local DEFAULT_TEXT_SCALE = {x=0.8, y=0.5}
45
46 signs_lib.regular_wall_sign_model = {
47         nodebox = {
48                 type = "wallmounted",
49                 wall_side =   { -0.5,    -0.25,   -0.4375, -0.4375,  0.375,  0.4375 },
50                 wall_bottom = { -0.4375, -0.5,    -0.25,    0.4375, -0.4375, 0.375 },
51                 wall_top =    { -0.4375,  0.4375, -0.375,   0.4375,  0.5,    0.25 }
52         },
53         textpos = {
54                 nil,
55                 nil,
56                 {delta = { x =  0.41, y = 0.07, z =  0    }, yaw = math.pi / -2},
57                 {delta = { x = -0.41, y = 0.07, z =  0    }, yaw = math.pi / 2},
58                 {delta = { x =  0,    y = 0.07, z =  0.41 }, yaw = 0},
59                 {delta = { x =  0,    y = 0.07, z = -0.41 }, yaw = math.pi},
60         }
61 }
62
63 signs_lib.metal_wall_sign_model = {
64         nodebox = {
65                 type = "fixed",
66                 fixed = {-0.4375, -0.25, 0.4375, 0.4375, 0.375, 0.5}
67         },
68         textpos = {
69                 {delta = { x =  0,     y = 0.07, z =  0.41 }, yaw = 0},
70                 {delta = { x =  0.41,  y = 0.07, z =  0    }, yaw = math.pi / -2},
71                 {delta = { x =  0,     y = 0.07, z = -0.41 }, yaw = math.pi},
72                 {delta = { x = -0.41,  y = 0.07, z =  0    }, yaw = math.pi / 2},
73         }
74 }
75
76 signs_lib.yard_sign_model = {
77         nodebox = {
78                 type = "fixed",
79                 fixed = {
80                                 {-0.4375, -0.25, -0.0625, 0.4375, 0.375, 0},
81                                 {-0.0625, -0.5, -0.0625, 0.0625, -0.1875, 0},
82                 }
83         },
84         textpos = {
85                 {delta = { x =  0,    y = 0.07, z = -0.08 }, yaw = 0},
86                 {delta = { x = -0.08, y = 0.07, z =  0    }, yaw = math.pi / -2},
87                 {delta = { x =  0,    y = 0.07, z =  0.08 }, yaw = math.pi},
88                 {delta = { x =  0.08, y = 0.07, z =  0    }, yaw = math.pi / 2},
89         }
90 }
91
92 signs_lib.hanging_sign_model = {
93         nodebox = {
94                 type = "fixed",
95                 fixed = {
96                                 {-0.4375, -0.3125, -0.0625, 0.4375, 0.3125, 0},
97                                 {-0.4375, 0.25, -0.03125, 0.4375, 0.5, -0.03125},
98                 }
99         },
100         textpos = {
101                 {delta = { x =  0,    y = -0.02, z = -0.08 }, yaw = 0},
102                 {delta = { x = -0.08, y = -0.02, z =  0    }, yaw = math.pi / -2},
103                 {delta = { x =  0,    y = -0.02, z =  0.08 }, yaw = math.pi},
104                 {delta = { x =  0.08, y = -0.02, z =  0    }, yaw = math.pi / 2},
105         }
106 }
107
108 signs_lib.sign_post_model = {
109         nodebox = {
110                 type = "fixed",
111                 fixed = {
112                                 {-0.4375, -0.25, -0.1875, 0.4375, 0.375, -0.125},
113                                 {-0.125, -0.5, -0.125, 0.125, 0.5, 0.125},
114                 }
115         },
116         textpos = {
117                 {delta = { x = 0,    y = 0.07, z = -0.2 }, yaw = 0},
118                 {delta = { x = -0.2, y = 0.07, z = 0    }, yaw = math.pi / -2},
119                 {delta = { x = 0,    y = 0.07, z = 0.2  }, yaw = math.pi},
120                 {delta = { x = 0.2,  y = 0.07, z = 0    }, yaw = math.pi / 2},
121         }
122 }
123
124 -- the list of standard sign nodes
125
126 signs_lib.sign_node_list = {
127         "default:sign_wall_wood",
128         "default:sign_wall_steel",
129         "signs:sign_yard",
130         "signs:sign_hanging",
131         "signs:sign_wall_green",
132         "signs:sign_wall_yellow",
133         "signs:sign_wall_red",
134         "signs:sign_wall_white_red",
135         "signs:sign_wall_white_black",
136         "signs:sign_wall_orange",
137         "signs:sign_wall_blue",
138         "signs:sign_wall_brown",
139         "locked_sign:sign_wall_locked"
140 }
141
142 local default_sign, default_sign_image
143
144 -- Default sign was renamed in 0.4.14. Support both & old versions.
145 if minetest.registered_nodes["default:sign_wall_wood"] then
146         default_sign = "default:sign_wall_wood"
147         default_sign_image = "default_sign_wood.png"
148 else
149         default_sign = "default:sign_wall"
150         default_sign_image = "default_sign_wall.png"
151 end
152
153 default_sign_metal = "default:sign_wall_steel"
154 default_sign_metal_image = "default_sign_steel.png"
155
156 --table copy
157
158 function signs_lib.table_copy(t)
159     local nt = { };
160     for k, v in pairs(t) do
161         if type(v) == "table" then
162             nt[k] = signs_lib.table_copy(v)
163         else
164             nt[k] = v
165         end
166     end
167     return nt
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 -- Font name.
188 local font_name = "hdf"
189
190 -- Lots of overkill here. KISS advocates, go away, shoo! ;) -- kaeza
191
192 local PNG_HDR = string.char(0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A)
193
194 -- check if a file does exist
195 -- to avoid reopening file after checking again
196 -- pass TRUE as second argument
197 function file_exists(name, return_handle, mode)
198         mode = mode or "r";
199         local f = io.open(name, mode)
200         if f ~= nil then
201                 if (return_handle) then
202                         return f
203                 end
204                 io.close(f) 
205                 return true 
206         else 
207                 return false 
208         end
209 end
210
211 -- Read the image size from a PNG file.
212 -- Returns image_w, image_h.
213 -- Only the LSB is read from each field!
214 local function read_image_size(filename)
215         local f = file_exists(filename, true, "rb")
216         -- file might not exist (don't crash the game)
217         if (not f) then
218                 return 0, 0
219         end
220         f:seek("set", 0x0)
221         local hdr = f:read(string.len(PNG_HDR))
222         if hdr ~= PNG_HDR then
223                 f:close()
224                 return
225         end
226         f:seek("set", 0x13)
227         local ws = f:read(1)
228         f:seek("set", 0x17)
229         local hs = f:read(1)
230         f:close()
231         return ws:byte(), hs:byte()
232 end
233
234 -- Set by build_char_db()
235 local LINE_HEIGHT
236 local SIGN_WIDTH
237 local COLORBGW, COLORBGH
238
239 -- Size of the canvas, in characters.
240 -- Please note that CHARS_PER_LINE is multiplied by the average character
241 -- width to get the total width of the canvas, so for proportional fonts,
242 -- either more or fewer characters may fit on a line.
243 local CHARS_PER_LINE = 30
244 local NUMBER_OF_LINES = 6
245
246 -- 6 rows, max 80 chars per, plus a bit of fudge to
247 -- avoid excess trimming (e.g. due to color codes)
248
249 local MAX_INPUT_CHARS = 600
250
251 -- This holds the individual character widths.
252 -- Indexed by the actual character (e.g. charwidth["A"])
253 local charwidth
254
255 -- helper functions to trim sign text input/output
256
257 local function trim_input(text)
258         return text:sub(1, math.min(MAX_INPUT_CHARS, text:len()))
259 end
260
261 local function build_char_db()
262
263         charwidth = { }
264
265         -- To calculate average char width.
266         local total_width = 0
267         local char_count = 0
268
269         for c = 32, 255 do
270                 local w, h = read_image_size(CHAR_PATH:format(font_name, c))
271                 if w and h then
272                         local ch = string.char(c)
273                         charwidth[ch] = w
274                         total_width = total_width + w
275                         char_count = char_count + 1
276                 end
277         end
278
279         COLORBGW, COLORBGH = read_image_size(TP.."/slc_n.png")
280         assert(COLORBGW and COLORBGH, "error reading bg dimensions")
281         LINE_HEIGHT = COLORBGH
282
283         -- XXX: Is there a better way to calc this?
284         SIGN_WIDTH = math.floor((total_width / char_count) * CHARS_PER_LINE)
285
286 end
287
288 local sign_groups = {choppy=2, dig_immediate=2}
289
290 local fences_with_sign = { }
291
292 -- some local helper functions
293
294 local function split_lines_and_words_old(text)
295         local lines = { }
296         local line = { }
297         if not text then return end
298         for word in text:gmatch("%S+") do
299                 if word == "|" then
300                         table.insert(lines, line)
301                         if #lines >= NUMBER_OF_LINES then break end
302                         line = { }
303                 elseif word == "\\|" then
304                         table.insert(line, "|")
305                 else
306                         table.insert(line, word)
307                 end
308         end
309         table.insert(lines, line)
310         return lines
311 end
312
313 local function split_lines_and_words(text)
314         if not text then return end
315         text = string.gsub(text, "@KEYWORD", current_keyword)
316         local lines = { }
317         for _, line in ipairs(text:split("\n")) do
318                 table.insert(lines, line:split(" "))
319         end
320         return lines
321 end
322
323 local math_max = math.max
324
325 local function fill_line(x, y, w, c)
326         c = c or "0"
327         local tex = { }
328         for xx = 0, math.max(0, w), COLORBGW do
329                 table.insert(tex, (":%d,%d=slc_%s.png"):format(x + xx, y, c))
330         end
331         return table.concat(tex)
332 end
333
334 -- make char texture file name
335 -- if texture file does not exist use fallback texture instead
336 local function char_tex(font_name, ch)
337         local c = ch:byte()
338         local exists, tex = file_exists(CHAR_PATH:format(font_name, c))
339         if exists and c ~= 14 then
340                 tex = CHAR_FILE:format(font_name, c)
341         else
342                 tex = CHAR_FILE:format(font_name, 0x0)
343         end
344         return tex, exists
345 end
346
347 local function make_line_texture(line, lineno, pos)
348
349         local width = 0
350         local maxw = 0
351
352         local words = { }
353         local n = minetest.registered_nodes[minetest.get_node(pos).name]
354         local default_color = n.default_color or 0
355
356         local cur_color = tonumber(default_color, 16)
357
358         -- We check which chars are available here.
359         for word_i, word in ipairs(line) do
360                 local chars = { }
361                 local ch_offs = 0
362                 local word_l = #word
363                 local i = 1
364                 while i <= word_l  do
365                         local c = word:sub(i, i)
366                         if c == "#" then
367                                 local cc = tonumber(word:sub(i+1, i+1), 16)
368                                 if cc then
369                                         i = i + 1
370                                         cur_color = cc
371                                 end
372                         else
373                                 local w = charwidth[c]
374                                 if w then
375                                         width = width + w + 1
376                                         if width >= (SIGN_WIDTH - charwidth[" "]) then
377                                                 width = 0
378                                         else
379                                                 maxw = math_max(width, maxw)
380                                         end
381                                         if #chars < MAX_INPUT_CHARS then
382                                                 table.insert(chars, {
383                                                         off = ch_offs,
384                                                         tex = char_tex(font_name, c),
385                                                         col = ("%X"):format(cur_color),
386                                                 })
387                                         end
388                                         ch_offs = ch_offs + w
389                                 end
390                         end
391                         i = i + 1
392                 end
393                 width = width + charwidth[" "] + 1
394                 maxw = math_max(width, maxw)
395                 table.insert(words, { chars=chars, w=ch_offs })
396         end
397
398         -- Okay, we actually build the "line texture" here.
399
400         local texture = { }
401
402         local start_xpos = math.floor((SIGN_WIDTH - maxw) / 2)
403
404         local xpos = start_xpos
405         local ypos = (LINE_HEIGHT * lineno)
406
407         cur_color = nil
408
409         for word_i, word in ipairs(words) do
410                 local xoffs = (xpos - start_xpos)
411                 if (xoffs > 0) and ((xoffs + word.w) > maxw) then
412                         table.insert(texture, fill_line(xpos, ypos, maxw, "n"))
413                         xpos = start_xpos
414                         ypos = ypos + LINE_HEIGHT
415                         lineno = lineno + 1
416                         if lineno >= NUMBER_OF_LINES then break end
417                         table.insert(texture, fill_line(xpos, ypos, maxw, cur_color))
418                 end
419                 for ch_i, ch in ipairs(word.chars) do
420                         if ch.col ~= cur_color then
421                                 cur_color = ch.col
422                                 table.insert(texture, fill_line(xpos + ch.off, ypos, maxw, cur_color))
423                         end
424                         table.insert(texture, (":%d,%d=%s"):format(xpos + ch.off, ypos, ch.tex))
425                 end
426                 table.insert(
427                         texture, 
428                         (":%d,%d="):format(xpos + word.w, ypos) .. char_tex(font_name, " ")
429                 )
430                 xpos = xpos + word.w + charwidth[" "]
431                 if xpos >= (SIGN_WIDTH + charwidth[" "]) then break end
432         end
433
434         table.insert(texture, fill_line(xpos, ypos, maxw, "n"))
435         table.insert(texture, fill_line(start_xpos, ypos + LINE_HEIGHT, maxw, "n"))
436
437         return table.concat(texture), lineno
438 end
439
440 local function make_sign_texture(lines, pos)
441         local texture = { ("[combine:%dx%d"):format(SIGN_WIDTH, LINE_HEIGHT * NUMBER_OF_LINES) }
442         local lineno = 0
443         for i = 1, #lines do
444                 if lineno >= NUMBER_OF_LINES then break end
445                 local linetex, ln = make_line_texture(lines[i], lineno, pos)
446                 table.insert(texture, linetex)
447                 lineno = ln + 1
448         end
449         table.insert(texture, "^[makealpha:0,0,0")
450         return table.concat(texture, "")
451 end
452
453 local function set_obj_text(obj, text, new, pos)
454         local split = new and split_lines_and_words or split_lines_and_words_old
455         local text_ansi = Utf8ToAnsi(text)
456         local n = minetest.registered_nodes[minetest.get_node(pos).name]
457         local text_scale = (n and n.text_scale) or DEFAULT_TEXT_SCALE
458         obj:set_properties({
459                 textures={make_sign_texture(split(text_ansi), pos)},
460                 visual_size = text_scale,
461         })
462 end
463
464 signs_lib.construct_sign = function(pos, locked)
465     local meta = minetest.get_meta(pos)
466         meta:set_string(
467                 "formspec",
468                 "size[6,4]"..
469                 "textarea[0,-0.3;6.5,3;text;;${text}]"..
470                 "button_exit[2,3.4;2,1;ok;"..S("Write").."]"..
471                 "background[-0.5,-0.5;7,5;bg_signs_lib.jpg]")
472         meta:set_string("infotext", "")
473 end
474
475 signs_lib.destruct_sign = function(pos)
476     local objects = minetest.get_objects_inside_radius(pos, 0.5)
477     for _, v in ipairs(objects) do
478                 local e = v:get_luaentity()
479         if e and e.name == "signs:text" then
480             v:remove()
481         end
482     end
483 end
484
485 local function make_infotext(text)
486         text = trim_input(text)
487         local lines = split_lines_and_words(text) or {}
488         local lines2 = { }
489         for _, line in ipairs(lines) do
490                 table.insert(lines2, (table.concat(line, " "):gsub("#[0-9a-fA-F]", ""):gsub("##", "#")))
491         end
492         return table.concat(lines2, "\n")
493 end
494
495 signs_lib.update_sign = function(pos, fields, owner)
496
497         -- First, check if the interact keyword from CWz's mod is being set,
498         -- or has been changed since the last restart...
499
500         local meta = minetest.get_meta(pos)
501         local stored_text = meta:get_string("text") or ""
502         current_keyword = rawget(_G, "mki_interact_keyword") or current_keyword
503
504         if fields then -- ...we're editing the sign.
505                 if fields.text and string.find(dump(fields.text), "@KEYWORD") then
506                         meta:set_string("keyword", current_keyword)
507                 else
508                         meta:set_string("keyword", nil)
509                 end
510         elseif string.find(dump(stored_text), "@KEYWORD") then -- we need to check if the password is being set/changed
511
512                 local stored_keyword = meta:get_string("keyword")
513                 if stored_keyword and stored_keyword ~= "" and stored_keyword ~= current_keyword then
514                         signs_lib.destruct_sign(pos)
515                         meta:set_string("keyword", current_keyword)
516                         local ownstr = ""
517                         if owner then ownstr = S("Locked sign, owned by @1\n", owner) end
518                         meta:set_string("infotext", ownstr..string.gsub(make_infotext(stored_text), "@KEYWORD", current_keyword).." ")
519                 end
520         end
521
522         local new
523
524         if fields then
525
526                 fields.text = trim_input(fields.text)
527
528                 local ownstr = ""
529                 if owner then ownstr = S("Locked sign, owned by @1\n", owner) end
530
531                 meta:set_string("infotext", ownstr..string.gsub(make_infotext(fields.text), "@KEYWORD", current_keyword).." ")
532                 meta:set_string("text", fields.text)
533                 
534                 meta:set_int("__signslib_new_format", 1)
535                 new = true
536         else
537                 new = (meta:get_int("__signslib_new_format") ~= 0)
538         end
539         local text = meta:get_string("text")
540         if text == nil then return end
541         local objects = minetest.get_objects_inside_radius(pos, 0.5)
542         local found
543         for _, v in ipairs(objects) do
544                 local e = v:get_luaentity()
545                 if e and e.name == "signs:text" then
546                         if found then
547                                 v:remove()
548                         else
549                                 set_obj_text(v, text, new, pos)
550                                 found = true
551                         end
552                 end
553         end
554         if found then
555                 return
556         end
557
558         -- if there is no entity
559         local sign_info
560         local signnode = minetest.get_node(pos)
561         local signname = signnode.name
562         local textpos = minetest.registered_nodes[signname].textpos
563         if textpos then
564                 sign_info = textpos[minetest.get_node(pos).param2 + 1]
565         elseif signnode.name == "signs:sign_yard" then
566                 sign_info = signs_lib.yard_sign_model.textpos[minetest.get_node(pos).param2 + 1]
567         elseif signnode.name == "signs:sign_hanging" then
568                 sign_info = signs_lib.hanging_sign_model.textpos[minetest.get_node(pos).param2 + 1]
569         elseif string.find(signnode.name, "sign_wall") then
570                 if signnode.name == default_sign
571                   or signnode.name == default_sign_metal
572                   or signnode.name == "locked_sign:sign_wall_locked" then
573                         sign_info = signs_lib.regular_wall_sign_model.textpos[minetest.get_node(pos).param2 + 1]
574                 else
575                         sign_info = signs_lib.metal_wall_sign_model.textpos[minetest.get_node(pos).param2 + 1]
576                 end
577         else -- ...it must be a sign on a fence post.
578                 sign_info = signs_lib.sign_post_model.textpos[minetest.get_node(pos).param2 + 1]
579         end
580         if sign_info == nil then
581                 return
582         end
583         local text = minetest.add_entity({x = pos.x + sign_info.delta.x,
584                                                                                 y = pos.y + sign_info.delta.y,
585                                                                                 z = pos.z + sign_info.delta.z}, "signs:text")
586         text:setyaw(sign_info.yaw)
587 end
588
589 -- What kind of sign do we need to place, anyway?
590
591 function signs_lib.determine_sign_type(itemstack, placer, pointed_thing, locked)
592         local name
593         name = minetest.get_node(pointed_thing.under).name
594         if fences_with_sign[name] then
595                 if minetest.is_protected(pointed_thing.under, placer:get_player_name()) then
596                         minetest.record_protection_violation(pointed_thing.under,
597                                 placer:get_player_name())
598                         return itemstack
599                 end
600         else
601                 name = minetest.get_node(pointed_thing.above).name
602                 local def = minetest.registered_nodes[name]
603                 if not def.buildable_to then
604                         return itemstack
605                 end
606                 if minetest.is_protected(pointed_thing.above, placer:get_player_name()) then
607                         minetest.record_protection_violation(pointed_thing.above,
608                                 placer:get_player_name())
609                         return itemstack
610                 end
611         end
612
613         local node=minetest.get_node(pointed_thing.under)
614
615         if minetest.registered_nodes[node.name] and
616            minetest.registered_nodes[node.name].on_rightclick and
617            not placer:get_player_control().sneak then
618                 return minetest.registered_nodes[node.name].on_rightclick(pointed_thing.under, node, placer, itemstack, pointed_thing)
619         else
620                 local above = pointed_thing.above
621                 local under = pointed_thing.under
622                 local dir = {x = under.x - above.x,
623                                          y = under.y - above.y,
624                                          z = under.z - above.z}
625
626                 local wdir = minetest.dir_to_wallmounted(dir)
627
628                 local placer_pos = placer:getpos()
629                 if placer_pos then
630                         dir = {
631                                 x = above.x - placer_pos.x,
632                                 y = above.y - placer_pos.y,
633                                 z = above.z - placer_pos.z
634                         }
635                 end
636
637                 local fdir = minetest.dir_to_facedir(dir)
638                 local pt_name = minetest.get_node(under).name
639                 local signname = itemstack:get_name()
640
641                 if fences_with_sign[pt_name] and signname == default_sign then
642                         minetest.add_node(under, {name = fences_with_sign[pt_name], param2 = fdir})
643                 elseif wdir == 0 and signname == default_sign then
644                         minetest.add_node(above, {name = "signs:sign_hanging", param2 = fdir})
645                 elseif wdir == 1 and signname == default_sign then
646                         minetest.add_node(above, {name = "signs:sign_yard", param2 = fdir})
647                 elseif signname == default_sign_metal then
648                         minetest.add_node(above, {name = signname, param2 = wdir })
649                 elseif signname ~= default_sign
650                   and signname ~= default_sign_metal
651                   and signname ~= "locked_sign:sign_wall_locked" then -- it's a signs_lib colored metal wall sign.
652                         minetest.add_node(above, {name = signname, param2 = fdir})
653                 else -- it must be a default or locked wooden wall sign
654                         minetest.add_node(above, {name = signname, param2 = wdir }) -- note it's wallmounted here!
655                         if locked then
656                                 local meta = minetest.get_meta(above)
657                                 local owner = placer:get_player_name()
658                                 meta:set_string("owner", owner)
659                         end
660                 end
661
662                 if not signs_lib.expect_infinite_stacks then
663                         itemstack:take_item()
664                 end
665                 return itemstack
666         end
667 end
668
669 function signs_lib.receive_fields(pos, formname, fields, sender, lock)
670         if minetest.is_protected(pos, sender:get_player_name()) then
671                 minetest.record_protection_violation(pos,
672                         sender:get_player_name())
673                 return
674         end
675         local lockstr = lock and S("locked ") or ""
676         if fields and fields.text and fields.ok then
677                 minetest.log("action", S("@1 wrote \"@2\" to @3sign at @4",
678                         (sender:get_player_name() or ""),
679                         fields.text:gsub('\\', '\\\\'):gsub("\n", "\\n"),
680                         lockstr,
681                         minetest.pos_to_string(pos)
682                 ))
683                 if lock then
684                         signs_lib.update_sign(pos, fields, sender:get_player_name())
685                 else
686                         signs_lib.update_sign(pos, fields)
687                 end
688         end
689 end
690
691 minetest.register_node(":"..default_sign, {
692         description = S("Sign"),
693         inventory_image = default_sign_image,
694         wield_image = default_sign_image,
695         node_placement_prediction = "",
696         sunlight_propagates = true,
697         paramtype = "light",
698         paramtype2 = "wallmounted",
699         drawtype = "nodebox",
700         node_box = signs_lib.regular_wall_sign_model.nodebox,
701         tiles = {"signs_wall_sign.png"},
702         groups = sign_groups,
703
704         on_place = function(itemstack, placer, pointed_thing)
705                 return signs_lib.determine_sign_type(itemstack, placer, pointed_thing)
706         end,
707         on_construct = function(pos)
708                 signs_lib.construct_sign(pos)
709         end,
710         on_destruct = function(pos)
711                 signs_lib.destruct_sign(pos)
712         end,
713         on_receive_fields = function(pos, formname, fields, sender)
714                 signs_lib.receive_fields(pos, formname, fields, sender)
715         end,
716         on_punch = function(pos, node, puncher)
717                 signs_lib.update_sign(pos)
718         end,
719         on_rotate = signs_lib.wallmounted_rotate
720 })
721
722 minetest.register_node(":signs:sign_yard", {
723     paramtype = "light",
724         sunlight_propagates = true,
725     paramtype2 = "facedir",
726     drawtype = "nodebox",
727     node_box = signs_lib.yard_sign_model.nodebox,
728         selection_box = {
729                 type = "fixed",
730                 fixed = {-0.4375, -0.5, -0.0625, 0.4375, 0.375, 0}
731         },
732     tiles = {"signs_top.png", "signs_bottom.png", "signs_side.png", "signs_side.png", "signs_back.png", "signs_front.png"},
733     groups = {choppy=2, dig_immediate=2},
734     drop = default_sign,
735
736     on_construct = function(pos)
737         signs_lib.construct_sign(pos)
738     end,
739     on_destruct = function(pos)
740         signs_lib.destruct_sign(pos)
741     end,
742         on_receive_fields = function(pos, formname, fields, sender)
743                 signs_lib.receive_fields(pos, formname, fields, sender)
744         end,
745         on_punch = function(pos, node, puncher)
746                 signs_lib.update_sign(pos)
747         end,
748 })
749
750 minetest.register_node(":signs:sign_hanging", {
751     paramtype = "light",
752         sunlight_propagates = true,
753     paramtype2 = "facedir",
754     drawtype = "nodebox",
755     node_box = signs_lib.hanging_sign_model.nodebox,
756     selection_box = {
757                 type = "fixed",
758                 fixed = {-0.45, -0.275, -0.049, 0.45, 0.5, 0.049}
759         },
760     tiles = {
761                 "signs_hanging_top.png",
762                 "signs_hanging_bottom.png",
763                 "signs_hanging_side.png",
764                 "signs_hanging_side.png",
765                 "signs_hanging_back.png",
766                 "signs_hanging_front.png"
767         },
768     groups = {choppy=2, dig_immediate=2},
769     drop = default_sign,
770
771     on_construct = function(pos)
772         signs_lib.construct_sign(pos)
773     end,
774     on_destruct = function(pos)
775         signs_lib.destruct_sign(pos)
776     end,
777         on_receive_fields = function(pos, formname, fields, sender)
778                 signs_lib.receive_fields(pos, formname, fields, sender)
779         end,
780         on_punch = function(pos, node, puncher)
781                 signs_lib.update_sign(pos)
782         end,
783 })
784
785 minetest.register_node(":signs:sign_post", {
786     paramtype = "light",
787         sunlight_propagates = true,
788     paramtype2 = "facedir",
789     drawtype = "nodebox",
790     node_box = signs_lib.sign_post_model.nodebox,
791     tiles = {
792                 "signs_post_top.png",
793                 "signs_post_bottom.png",
794                 "signs_post_side.png",
795                 "signs_post_side.png",
796                 "signs_post_back.png",
797                 "signs_post_front.png",
798         },
799     groups = {choppy=2, dig_immediate=2},
800     drop = {
801                 max_items = 2,
802                 items = {
803                         { items = { default_sign }},
804                         { items = { "default:fence_wood" }},
805                 },
806     },
807 })
808
809 -- Locked wall sign
810
811 minetest.register_privilege("sign_editor", S("Can edit all locked signs"))
812
813 minetest.register_node(":locked_sign:sign_wall_locked", {
814         description = S("Locked Sign"),
815         inventory_image = "signs_locked_inv.png",
816         wield_image = "signs_locked_inv.png",
817         node_placement_prediction = "",
818         sunlight_propagates = true,
819         paramtype = "light",
820         paramtype2 = "wallmounted",
821         drawtype = "nodebox",
822         node_box = signs_lib.regular_wall_sign_model.nodebox,
823         tiles = { "signs_wall_sign_locked.png" },
824         groups = sign_groups,
825         on_place = function(itemstack, placer, pointed_thing)
826                 return signs_lib.determine_sign_type(itemstack, placer, pointed_thing, true)
827         end,
828         on_construct = function(pos)
829                 signs_lib.construct_sign(pos, true)
830         end,
831         on_destruct = function(pos)
832                 signs_lib.destruct_sign(pos)
833         end,
834         on_receive_fields = function(pos, formname, fields, sender)
835                 local meta = minetest.get_meta(pos)
836                 local owner = meta:get_string("owner")
837                 local pname = sender:get_player_name() or ""
838                 if pname ~= owner and pname ~= minetest.settings:get("name")
839                   and not minetest.check_player_privs(pname, {sign_editor=true}) then
840                         return
841                 end
842                 signs_lib.receive_fields(pos, formname, fields, sender, true)
843         end,
844         on_punch = function(pos, node, puncher)
845                 signs_lib.update_sign(pos)
846         end,
847         can_dig = function(pos, player)
848                 local meta = minetest.get_meta(pos)
849                 local owner = meta:get_string("owner")
850                 local pname = player:get_player_name()
851                 return pname == owner or pname == minetest.settings:get("name")
852                         or minetest.check_player_privs(pname, {sign_editor=true})
853         end,
854         on_rotate = signs_lib.wallmounted_rotate
855 })
856
857 -- default metal sign, if defined
858
859 if minetest.registered_nodes["default:sign_wall_steel"] then
860         minetest.register_node(":"..default_sign_metal, {
861                 description = S("Sign"),
862                 inventory_image = default_sign_metal_image,
863                 wield_image = default_sign_metal_image,
864                 node_placement_prediction = "",
865                 sunlight_propagates = true,
866                 paramtype = "light",
867                 paramtype2 = "wallmounted",
868                 drawtype = "nodebox",
869                 node_box = signs_lib.regular_wall_sign_model.nodebox,
870                 tiles = {"signs_wall_sign_metal.png"},
871                 groups = sign_groups,
872
873                 on_place = function(itemstack, placer, pointed_thing)
874                         return signs_lib.determine_sign_type(itemstack, placer, pointed_thing)
875                 end,
876                 on_construct = function(pos)
877                         signs_lib.construct_sign(pos)
878                 end,
879                 on_destruct = function(pos)
880                         signs_lib.destruct_sign(pos)
881                 end,
882                 on_receive_fields = function(pos, formname, fields, sender)
883                         signs_lib.receive_fields(pos, formname, fields, sender)
884                 end,
885                 on_punch = function(pos, node, puncher)
886                         signs_lib.update_sign(pos)
887                 end,
888                 on_rotate = signs_lib.wallmounted_rotate
889         })
890 end
891
892 -- metal, colored signs
893 if enable_colored_metal_signs then
894         -- array : color, translated color, default text color
895         local sign_colors = {
896                 {"green",        S("green"),       "f"},
897                 {"yellow",       S("yellow"),      "0"},
898                 {"red",          S("red"),         "f"},
899                 {"white_red",    S("white_red"),   "4"},
900                 {"white_black",  S("white_black"), "0"},
901                 {"orange",       S("orange"),      "0"},
902                 {"blue",         S("blue"),        "f"},
903                 {"brown",        S("brown"),       "f"},
904         }
905
906         for i, color in ipairs(sign_colors) do
907                 minetest.register_node(":signs:sign_wall_"..color[1], {
908                         description = S("Sign (@1, metal)", color[2]),
909                         inventory_image = "signs_"..color[1].."_inv.png",
910                         wield_image = "signs_"..color[1].."_inv.png",
911                         node_placement_prediction = "",
912                         paramtype = "light",
913                         sunlight_propagates = true,
914                         paramtype2 = "facedir",
915                         drawtype = "nodebox",
916                         node_box = signs_lib.metal_wall_sign_model.nodebox,
917                         tiles = {
918                                 "signs_metal_tb.png",
919                                 "signs_metal_tb.png",
920                                 "signs_metal_sides.png",
921                                 "signs_metal_sides.png",
922                                 "signs_metal_back.png",
923                                 "signs_"..color[1].."_front.png"
924                         },
925                         default_color = color[3],
926                         groups = sign_groups,
927                         on_place = function(itemstack, placer, pointed_thing)
928                                 return signs_lib.determine_sign_type(itemstack, placer, pointed_thing)
929                         end,
930                         on_construct = function(pos)
931                                 signs_lib.construct_sign(pos)
932                         end,
933                         on_destruct = function(pos)
934                                 signs_lib.destruct_sign(pos)
935                         end,
936                         on_receive_fields = function(pos, formname, fields, sender)
937                                 signs_lib.receive_fields(pos, formname, fields, sender)
938                         end,
939                         on_punch = function(pos, node, puncher)
940                                 signs_lib.update_sign(pos)
941                         end,
942                 })
943         end
944 end
945
946 local signs_text_on_activate
947
948 signs_text_on_activate = function(self)
949         local pos = self.object:getpos()
950         local meta = minetest.get_meta(pos)
951         local text = meta:get_string("text")
952         local new = (meta:get_int("__signslib_new_format") ~= 0)
953         if text and minetest.registered_nodes[minetest.get_node(pos).name] then
954                 text = trim_input(text)
955                 set_obj_text(self.object, text, new, pos)
956         end
957 end
958
959 minetest.register_entity(":signs:text", {
960     collisionbox = { 0, 0, 0, 0, 0, 0 },
961     visual = "upright_sprite",
962     textures = {},
963
964         on_activate = signs_text_on_activate,
965 })
966
967 -- And the good stuff here! :-)
968
969 function signs_lib.register_fence_with_sign(fencename, fencewithsignname)
970     local def = minetest.registered_nodes[fencename]
971     local def_sign = minetest.registered_nodes[fencewithsignname]
972     if not (def and def_sign) then
973         minetest.log("warning", "[signs_lib] "..S("Attempt to register unknown node as fence"))
974         return
975     end
976     def = signs_lib.table_copy(def)
977     def_sign = signs_lib.table_copy(def_sign)
978     fences_with_sign[fencename] = fencewithsignname
979
980     def_sign.on_place = function(itemstack, placer, pointed_thing, ...)
981                 local node_above = minetest.get_node_or_nil(pointed_thing.above)
982                 local node_under = minetest.get_node_or_nil(pointed_thing.under)
983                 local def_above = node_above and minetest.registered_nodes[node_above.name]
984                 local def_under = node_under and minetest.registered_nodes[node_under.name]
985                 local fdir = minetest.dir_to_facedir(placer:get_look_dir())
986                 local playername = placer:get_player_name()
987
988                 if minetest.is_protected(pointed_thing.under, playername) then
989                         minetest.record_protection_violation(pointed_thing.under, playername)
990                         return itemstack
991                 end
992
993                 if minetest.is_protected(pointed_thing.above, playername) then
994                         minetest.record_protection_violation(pointed_thing.above, playername)
995                         return itemstack
996                 end
997
998                 if def_under and def_under.on_rightclick then
999                         return def_under.on_rightclick(pointed_thing.under, node_under, placer, itemstack, pointed_thing) or itemstack
1000                 elseif def_under and def_under.buildable_to then
1001                         minetest.add_node(pointed_thing.under, {name = fencename, param2 = fdir})
1002                         if not signs_lib.expect_infinite_stacks then
1003                                 itemstack:take_item()
1004                         end
1005                         placer:set_wielded_item(itemstack)
1006                 elseif def_above and def_above.buildable_to then
1007                         minetest.add_node(pointed_thing.above, {name = fencename, param2 = fdir})
1008                         if not signs_lib.expect_infinite_stacks then
1009                                 itemstack:take_item()
1010                         end
1011                         placer:set_wielded_item(itemstack)
1012                 end
1013                 return itemstack
1014         end
1015         def_sign.on_construct = function(pos, ...)
1016                 signs_lib.construct_sign(pos)
1017         end
1018         def_sign.on_destruct = function(pos, ...)
1019                 signs_lib.destruct_sign(pos)
1020         end
1021         def_sign.on_receive_fields = function(pos, formname, fields, sender)
1022                 signs_lib.receive_fields(pos, formname, fields, sender)
1023         end
1024         def_sign.on_punch = function(pos, node, puncher, ...)
1025                 signs_lib.update_sign(pos)
1026         end
1027         local fencename = fencename
1028         def_sign.after_dig_node = function(pos, node, ...)
1029             node.name = fencename
1030             minetest.add_node(pos, node)
1031         end
1032     def_sign.drop = default_sign
1033         minetest.register_node(":"..fencename, def)
1034         minetest.register_node(":"..fencewithsignname, def_sign)
1035         table.insert(signs_lib.sign_node_list, fencewithsignname)
1036         minetest.log("verbose", S("Registered @1 and @2", fencename, fencewithsignname))
1037 end
1038
1039 build_char_db()
1040
1041 minetest.register_alias("homedecor:fence_wood_with_sign", "signs:sign_post")
1042 minetest.register_alias("sign_wall_locked", "locked_sign:sign_wall_locked")
1043
1044 signs_lib.register_fence_with_sign("default:fence_wood", "signs:sign_post")
1045
1046 -- restore signs' text after /clearobjects and the like, the next time
1047 -- a block is reloaded by the server.
1048
1049 minetest.register_lbm({
1050         nodenames = signs_lib.sign_node_list,
1051         name = "signs_lib:restore_sign_text",
1052         label = "Restore sign text",
1053         run_at_every_load = true,
1054         action = function(pos, node)
1055                 signs_lib.update_sign(pos)
1056         end
1057 })
1058
1059 -- locked sign
1060
1061 minetest.register_craft({
1062         output = "locked_sign:sign_wall_locked",
1063         recipe = {
1064                 {default_sign},
1065                 {"default:steel_ingot"},
1066     },
1067 })
1068
1069 -- craft recipes for the metal signs
1070 if enable_colored_metal_signs then
1071
1072         minetest.register_craft( {
1073                 output = "signs:sign_wall_green",
1074                 recipe = {
1075                                 { "dye:dark_green", "dye:white", "dye:dark_green" },
1076                                 { "", default_sign_metal, "" }
1077                 },
1078         })
1079
1080         minetest.register_craft( {
1081                 output = "signs:sign_wall_green 2",
1082                 recipe = {
1083                                 { "dye:dark_green", "dye:white", "dye:dark_green" },
1084                                 { "steel:sheet_metal", "steel:sheet_metal", "steel:sheet_metal" }
1085                 },
1086         })
1087
1088         minetest.register_craft( {
1089                 output = "signs:sign_wall_yellow",
1090                 recipe = {
1091                                 { "dye:yellow", "dye:black", "dye:yellow" },
1092                                 { "", default_sign_metal, "" }
1093                 },
1094         })
1095
1096         minetest.register_craft( {
1097                 output = "signs:sign_wall_yellow 2",
1098                 recipe = {
1099                                 { "dye:yellow", "dye:black", "dye:yellow" },
1100                                 { "steel:sheet_metal", "steel:sheet_metal", "steel:sheet_metal" }
1101                 },
1102         })
1103
1104         minetest.register_craft( {
1105                 output = "signs:sign_wall_red",
1106                 recipe = {
1107                                 { "dye:red", "dye:white", "dye:red" },
1108                                 { "", default_sign_metal, "" }
1109                 },
1110         })
1111
1112         minetest.register_craft( {
1113                 output = "signs:sign_wall_red 2",
1114                 recipe = {
1115                                 { "dye:red", "dye:white", "dye:red" },
1116                                 { "steel:sheet_metal", "steel:sheet_metal", "steel:sheet_metal" }
1117                 },
1118         })
1119
1120         minetest.register_craft( {
1121                 output = "signs:sign_wall_white_red",
1122                 recipe = {
1123                                 { "dye:white", "dye:red", "dye:white" },
1124                                 { "", default_sign_metal, "" }
1125                 },
1126         })
1127
1128         minetest.register_craft( {
1129                 output = "signs:sign_wall_white_red 2",
1130                 recipe = {
1131                                 { "dye:white", "dye:red", "dye:white" },
1132                                 { "steel:sheet_metal", "steel:sheet_metal", "steel:sheet_metal" }
1133                 },
1134         })
1135
1136         minetest.register_craft( {
1137                 output = "signs:sign_wall_white_black",
1138                 recipe = {
1139                                 { "dye:white", "dye:black", "dye:white" },
1140                                 { "", default_sign_metal, "" }
1141                 },
1142         })
1143
1144         minetest.register_craft( {
1145                 output = "signs:sign_wall_white_black 2",
1146                 recipe = {
1147                                 { "dye:white", "dye:black", "dye:white" },
1148                                 { "steel:sheet_metal", "steel:sheet_metal", "steel:sheet_metal" }
1149                 },
1150         })
1151
1152         minetest.register_craft( {
1153                 output = "signs:sign_wall_orange",
1154                 recipe = {
1155                                 { "dye:orange", "dye:black", "dye:orange" },
1156                                 { "", default_sign_metal, "" }
1157                 },
1158         })
1159
1160         minetest.register_craft( {
1161                 output = "signs:sign_wall_orange 2",
1162                 recipe = {
1163                                 { "dye:orange", "dye:black", "dye:orange" },
1164                                 { "steel:sheet_metal", "steel:sheet_metal", "steel:sheet_metal" }
1165                 },
1166         })
1167
1168         minetest.register_craft( {
1169                 output = "signs:sign_wall_blue",
1170                 recipe = {
1171                                 { "dye:blue", "dye:white", "dye:blue" },
1172                                 { "", default_sign_metal, "" }
1173                 },
1174         })
1175
1176         minetest.register_craft( {
1177                 output = "signs:sign_wall_blue 2",
1178                 recipe = {
1179                                 { "dye:blue", "dye:white", "dye:blue" },
1180                                 { "steel:sheet_metal", "steel:sheet_metal", "steel:sheet_metal" }
1181                 },
1182         })
1183
1184         minetest.register_craft( {
1185                 output = "signs:sign_wall_brown",
1186                 recipe = {
1187                                 { "dye:brown", "dye:white", "dye:brown" },
1188                                 { "", default_sign_metal, "" }
1189                 },
1190         })
1191
1192         minetest.register_craft( {
1193                 output = "signs:sign_wall_brown 2",
1194                 recipe = {
1195                                 { "dye:brown", "dye:white", "dye:brown" },
1196                                 { "steel:sheet_metal", "steel:sheet_metal", "steel:sheet_metal" }
1197                 },
1198         })
1199 end
1200
1201 if minetest.settings:get("log_mods") then
1202         minetest.log("action", S("[MOD] signs loaded"))
1203 end