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