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