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