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