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