]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/common/misc_helpers.lua
Merge branch 'master' of https://github.com/minetest/minetest
[dragonfireclient.git] / builtin / common / misc_helpers.lua
1 -- Minetest: builtin/misc_helpers.lua
2
3 --------------------------------------------------------------------------------
4 -- Localize functions to avoid table lookups (better performance).
5 local string_sub, string_find = string.sub, string.find
6
7 --------------------------------------------------------------------------------
8 local function basic_dump(o)
9         local tp = type(o)
10         if tp == "number" then
11                 return tostring(o)
12         elseif tp == "string" then
13                 return string.format("%q", o)
14         elseif tp == "boolean" then
15                 return tostring(o)
16         elseif tp == "nil" then
17                 return "nil"
18         -- Uncomment for full function dumping support.
19         -- Not currently enabled because bytecode isn't very human-readable and
20         -- dump's output is intended for humans.
21         --elseif tp == "function" then
22         --      return string.format("loadstring(%q)", string.dump(o))
23         elseif tp == "userdata" then
24                 return tostring(o)
25         else
26                 return string.format("<%s>", tp)
27         end
28 end
29
30 local keywords = {
31         ["and"] = true,
32         ["break"] = true,
33         ["do"] = true,
34         ["else"] = true,
35         ["elseif"] = true,
36         ["end"] = true,
37         ["false"] = true,
38         ["for"] = true,
39         ["function"] = true,
40         ["goto"] = true,  -- Lua 5.2
41         ["if"] = true,
42         ["in"] = true,
43         ["local"] = true,
44         ["nil"] = true,
45         ["not"] = true,
46         ["or"] = true,
47         ["repeat"] = true,
48         ["return"] = true,
49         ["then"] = true,
50         ["true"] = true,
51         ["until"] = true,
52         ["while"] = true,
53 }
54 local function is_valid_identifier(str)
55         if not str:find("^[a-zA-Z_][a-zA-Z0-9_]*$") or keywords[str] then
56                 return false
57         end
58         return true
59 end
60
61 --------------------------------------------------------------------------------
62 -- Dumps values in a line-per-value format.
63 -- For example, {test = {"Testing..."}} becomes:
64 --   _["test"] = {}
65 --   _["test"][1] = "Testing..."
66 -- This handles tables as keys and circular references properly.
67 -- It also handles multiple references well, writing the table only once.
68 -- The dumped argument is internal-only.
69
70 function dump2(o, name, dumped)
71         name = name or "_"
72         -- "dumped" is used to keep track of serialized tables to handle
73         -- multiple references and circular tables properly.
74         -- It only contains tables as keys.  The value is the name that
75         -- the table has in the dump, eg:
76         -- {x = {"y"}} -> dumped[{"y"}] = '_["x"]'
77         dumped = dumped or {}
78         if type(o) ~= "table" then
79                 return string.format("%s = %s\n", name, basic_dump(o))
80         end
81         if dumped[o] then
82                 return string.format("%s = %s\n", name, dumped[o])
83         end
84         dumped[o] = name
85         -- This contains a list of strings to be concatenated later (because
86         -- Lua is slow at individual concatenation).
87         local t = {}
88         for k, v in pairs(o) do
89                 local keyStr
90                 if type(k) == "table" then
91                         if dumped[k] then
92                                 keyStr = dumped[k]
93                         else
94                                 -- Key tables don't have a name, so use one of
95                                 -- the form _G["table: 0xFFFFFFF"]
96                                 keyStr = string.format("_G[%q]", tostring(k))
97                                 -- Dump key table
98                                 t[#t + 1] = dump2(k, keyStr, dumped)
99                         end
100                 else
101                         keyStr = basic_dump(k)
102                 end
103                 local vname = string.format("%s[%s]", name, keyStr)
104                 t[#t + 1] = dump2(v, vname, dumped)
105         end
106         return string.format("%s = {}\n%s", name, table.concat(t))
107 end
108
109 --------------------------------------------------------------------------------
110 -- This dumps values in a one-statement format.
111 -- For example, {test = {"Testing..."}} becomes:
112 -- [[{
113 --      test = {
114 --              "Testing..."
115 --      }
116 -- }]]
117 -- This supports tables as keys, but not circular references.
118 -- It performs poorly with multiple references as it writes out the full
119 -- table each time.
120 -- The indent field specifies a indentation string, it defaults to a tab.
121 -- Use the empty string to disable indentation.
122 -- The dumped and level arguments are internal-only.
123
124 function dump(o, indent, nested, level)
125         local t = type(o)
126         if not level and t == "userdata" then
127                 -- when userdata (e.g. player) is passed directly, print its metatable:
128                 return "userdata metatable: " .. dump(getmetatable(o))
129         end
130         if t ~= "table" then
131                 return basic_dump(o)
132         end
133
134         -- Contains table -> true/nil of currently nested tables
135         nested = nested or {}
136         if nested[o] then
137                 return "<circular reference>"
138         end
139         nested[o] = true
140         indent = indent or "\t"
141         level = level or 1
142
143         local ret = {}
144         local dumped_indexes = {}
145         for i, v in ipairs(o) do
146                 ret[#ret + 1] = dump(v, indent, nested, level + 1)
147                 dumped_indexes[i] = true
148         end
149         for k, v in pairs(o) do
150                 if not dumped_indexes[k] then
151                         if type(k) ~= "string" or not is_valid_identifier(k) then
152                                 k = "["..dump(k, indent, nested, level + 1).."]"
153                         end
154                         v = dump(v, indent, nested, level + 1)
155                         ret[#ret + 1] = k.." = "..v
156                 end
157         end
158         nested[o] = nil
159         if indent ~= "" then
160                 local indent_str = "\n"..string.rep(indent, level)
161                 local end_indent_str = "\n"..string.rep(indent, level - 1)
162                 return string.format("{%s%s%s}",
163                                 indent_str,
164                                 table.concat(ret, ","..indent_str),
165                                 end_indent_str)
166         end
167         return "{"..table.concat(ret, ", ").."}"
168 end
169
170 --------------------------------------------------------------------------------
171 function string.split(str, delim, include_empty, max_splits, sep_is_pattern)
172         delim = delim or ","
173         max_splits = max_splits or -2
174         local items = {}
175         local pos, len = 1, #str
176         local plain = not sep_is_pattern
177         max_splits = max_splits + 1
178         repeat
179                 local np, npe = string_find(str, delim, pos, plain)
180                 np, npe = (np or (len+1)), (npe or (len+1))
181                 if (not np) or (max_splits == 1) then
182                         np = len + 1
183                         npe = np
184                 end
185                 local s = string_sub(str, pos, np - 1)
186                 if include_empty or (s ~= "") then
187                         max_splits = max_splits - 1
188                         items[#items + 1] = s
189                 end
190                 pos = npe + 1
191         until (max_splits == 0) or (pos > (len + 1))
192         return items
193 end
194
195 --------------------------------------------------------------------------------
196 function table.indexof(list, val)
197         for i, v in ipairs(list) do
198                 if v == val then
199                         return i
200                 end
201         end
202         return -1
203 end
204
205 --------------------------------------------------------------------------------
206 function string:trim()
207         return (self:gsub("^%s*(.-)%s*$", "%1"))
208 end
209
210 --------------------------------------------------------------------------------
211 function math.hypot(x, y)
212         return math.sqrt(x * x + y * y)
213 end
214
215 --------------------------------------------------------------------------------
216 function math.sign(x, tolerance)
217         tolerance = tolerance or 0
218         if x > tolerance then
219                 return 1
220         elseif x < -tolerance then
221                 return -1
222         end
223         return 0
224 end
225
226 --------------------------------------------------------------------------------
227 function math.factorial(x)
228         assert(x % 1 == 0 and x >= 0, "factorial expects a non-negative integer")
229         if x >= 171 then
230                 -- 171! is greater than the biggest double, no need to calculate
231                 return math.huge
232         end
233         local v = 1
234         for k = 2, x do
235                 v = v * k
236         end
237         return v
238 end
239
240
241 function math.round(x)
242         if x >= 0 then
243                 return math.floor(x + 0.5)
244         end
245         return math.ceil(x - 0.5)
246 end
247
248
249 function core.formspec_escape(text)
250         if text ~= nil then
251                 text = string.gsub(text,"\\","\\\\")
252                 text = string.gsub(text,"%]","\\]")
253                 text = string.gsub(text,"%[","\\[")
254                 text = string.gsub(text,";","\\;")
255                 text = string.gsub(text,",","\\,")
256         end
257         return text
258 end
259
260
261 function core.wrap_text(text, max_length, as_table)
262         local result = {}
263         local line = {}
264         if #text <= max_length then
265                 return as_table and {text} or text
266         end
267
268         for word in text:gmatch('%S+') do
269                 local cur_length = #table.concat(line, ' ')
270                 if cur_length > 0 and cur_length + #word + 1 >= max_length then
271                         -- word wouldn't fit on current line, move to next line
272                         table.insert(result, table.concat(line, ' '))
273                         line = {}
274                 end
275                 table.insert(line, word)
276         end
277
278         table.insert(result, table.concat(line, ' '))
279         return as_table and result or table.concat(result, '\n')
280 end
281
282 --------------------------------------------------------------------------------
283
284 if INIT == "game" then
285         local dirs1 = {9, 18, 7, 12}
286         local dirs2 = {20, 23, 22, 21}
287
288         function core.rotate_and_place(itemstack, placer, pointed_thing,
289                         infinitestacks, orient_flags, prevent_after_place)
290                 orient_flags = orient_flags or {}
291
292                 local unode = core.get_node_or_nil(pointed_thing.under)
293                 if not unode then
294                         return
295                 end
296                 local undef = core.registered_nodes[unode.name]
297                 local sneaking = placer and placer:get_player_control().sneak
298                 if undef and undef.on_rightclick and not sneaking then
299                         return undef.on_rightclick(pointed_thing.under, unode, placer,
300                                         itemstack, pointed_thing)
301                 end
302                 local fdir = placer and core.dir_to_facedir(placer:get_look_dir()) or 0
303
304                 local above = pointed_thing.above
305                 local under = pointed_thing.under
306                 local iswall = (above.y == under.y)
307                 local isceiling = not iswall and (above.y < under.y)
308
309                 if undef and undef.buildable_to then
310                         iswall = false
311                 end
312
313                 if orient_flags.force_floor then
314                         iswall = false
315                         isceiling = false
316                 elseif orient_flags.force_ceiling then
317                         iswall = false
318                         isceiling = true
319                 elseif orient_flags.force_wall then
320                         iswall = true
321                         isceiling = false
322                 elseif orient_flags.invert_wall then
323                         iswall = not iswall
324                 end
325
326                 local param2 = fdir
327                 if iswall then
328                         param2 = dirs1[fdir + 1]
329                 elseif isceiling then
330                         if orient_flags.force_facedir then
331                                 param2 = 20
332                         else
333                                 param2 = dirs2[fdir + 1]
334                         end
335                 else -- place right side up
336                         if orient_flags.force_facedir then
337                                 param2 = 0
338                         end
339                 end
340
341                 local old_itemstack = ItemStack(itemstack)
342                 local new_itemstack = core.item_place_node(itemstack, placer,
343                                 pointed_thing, param2, prevent_after_place)
344                 return infinitestacks and old_itemstack or new_itemstack
345         end
346
347
348 --------------------------------------------------------------------------------
349 --Wrapper for rotate_and_place() to check for sneak and assume Creative mode
350 --implies infinite stacks when performing a 6d rotation.
351 --------------------------------------------------------------------------------
352         core.rotate_node = function(itemstack, placer, pointed_thing)
353                 local name = placer and placer:get_player_name() or ""
354                 local invert_wall = placer and placer:get_player_control().sneak or false
355                 return core.rotate_and_place(itemstack, placer, pointed_thing,
356                         core.is_creative_enabled(name),
357                         {invert_wall = invert_wall}, true)
358         end
359 end
360
361 --------------------------------------------------------------------------------
362 function core.explode_table_event(evt)
363         if evt ~= nil then
364                 local parts = evt:split(":")
365                 if #parts == 3 then
366                         local t = parts[1]:trim()
367                         local r = tonumber(parts[2]:trim())
368                         local c = tonumber(parts[3]:trim())
369                         if type(r) == "number" and type(c) == "number"
370                                         and t ~= "INV" then
371                                 return {type=t, row=r, column=c}
372                         end
373                 end
374         end
375         return {type="INV", row=0, column=0}
376 end
377
378 --------------------------------------------------------------------------------
379 function core.explode_textlist_event(evt)
380         if evt ~= nil then
381                 local parts = evt:split(":")
382                 if #parts == 2 then
383                         local t = parts[1]:trim()
384                         local r = tonumber(parts[2]:trim())
385                         if type(r) == "number" and t ~= "INV" then
386                                 return {type=t, index=r}
387                         end
388                 end
389         end
390         return {type="INV", index=0}
391 end
392
393 --------------------------------------------------------------------------------
394 function core.explode_scrollbar_event(evt)
395         local retval = core.explode_textlist_event(evt)
396
397         retval.value = retval.index
398         retval.index = nil
399
400         return retval
401 end
402
403 --------------------------------------------------------------------------------
404 function core.rgba(r, g, b, a)
405         return a and string.format("#%02X%02X%02X%02X", r, g, b, a) or
406                         string.format("#%02X%02X%02X", r, g, b)
407 end
408
409 --------------------------------------------------------------------------------
410 function core.pos_to_string(pos, decimal_places)
411         local x = pos.x
412         local y = pos.y
413         local z = pos.z
414         if decimal_places ~= nil then
415                 x = string.format("%." .. decimal_places .. "f", x)
416                 y = string.format("%." .. decimal_places .. "f", y)
417                 z = string.format("%." .. decimal_places .. "f", z)
418         end
419         return "(" .. x .. "," .. y .. "," .. z .. ")"
420 end
421
422 --------------------------------------------------------------------------------
423 function core.string_to_pos(value)
424         if value == nil then
425                 return nil
426         end
427
428         local x, y, z = string.match(value, "^([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
429         if x and y and z then
430                 x = tonumber(x)
431                 y = tonumber(y)
432                 z = tonumber(z)
433                 return vector.new(x, y, z)
434         end
435         x, y, z = string.match(value, "^%( *([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+) *%)$")
436         if x and y and z then
437                 x = tonumber(x)
438                 y = tonumber(y)
439                 z = tonumber(z)
440                 return vector.new(x, y, z)
441         end
442         return nil
443 end
444
445
446 --------------------------------------------------------------------------------
447 function core.string_to_area(value)
448         local p1, p2 = unpack(value:split(") ("))
449         if p1 == nil or p2 == nil then
450                 return nil
451         end
452
453         p1 = core.string_to_pos(p1 .. ")")
454         p2 = core.string_to_pos("(" .. p2)
455         if p1 == nil or p2 == nil then
456                 return nil
457         end
458
459         return p1, p2
460 end
461
462 local function test_string_to_area()
463         local p1, p2 = core.string_to_area("(10.0, 5, -2) (  30.2,   4, -12.53)")
464         assert(p1.x == 10.0 and p1.y == 5 and p1.z == -2)
465         assert(p2.x == 30.2 and p2.y == 4 and p2.z == -12.53)
466
467         p1, p2 = core.string_to_area("(10.0, 5, -2  30.2,   4, -12.53")
468         assert(p1 == nil and p2 == nil)
469
470         p1, p2 = core.string_to_area("(10.0, 5,) -2  fgdf2,   4, -12.53")
471         assert(p1 == nil and p2 == nil)
472 end
473
474 test_string_to_area()
475
476 --------------------------------------------------------------------------------
477 function table.copy(t, seen)
478         local n = {}
479         seen = seen or {}
480         seen[t] = n
481         for k, v in pairs(t) do
482                 n[(type(k) == "table" and (seen[k] or table.copy(k, seen))) or k] =
483                         (type(v) == "table" and (seen[v] or table.copy(v, seen))) or v
484         end
485         return n
486 end
487
488
489 function table.insert_all(t, other)
490         for i=1, #other do
491                 t[#t + 1] = other[i]
492         end
493         return t
494 end
495
496
497 function table.key_value_swap(t)
498         local ti = {}
499         for k,v in pairs(t) do
500                 ti[v] = k
501         end
502         return ti
503 end
504
505
506 function table.shuffle(t, from, to, random)
507         from = from or 1
508         to = to or #t
509         random = random or math.random
510         local n = to - from + 1
511         while n > 1 do
512                 local r = from + n-1
513                 local l = from + random(0, n-1)
514                 t[l], t[r] = t[r], t[l]
515                 n = n-1
516         end
517 end
518
519
520 function table.combine(t, other)
521         other = other or {}
522         for k, v in pairs(other) do
523                 if type(v) == "table" and type(t[k]) == "table" then
524                         table.combine(t[k], v)
525                 else
526                         t[k] = v
527                 end
528         end
529 end
530
531 --------------------------------------------------------------------------------
532 -- mainmenu only functions
533 --------------------------------------------------------------------------------
534 if INIT == "mainmenu" then
535         function core.get_game(index)
536                 local games = core.get_games()
537
538                 if index > 0 and index <= #games then
539                         return games[index]
540                 end
541
542                 return nil
543         end
544 end
545
546 if core.gettext then -- for client and mainmenu
547         function fgettext_ne(text, ...)
548                 text = core.gettext(text)
549                 local arg = {n=select('#', ...), ...}
550                 if arg.n >= 1 then
551                         -- Insert positional parameters ($1, $2, ...)
552                         local result = ''
553                         local pos = 1
554                         while pos <= text:len() do
555                                 local newpos = text:find('[$]', pos)
556                                 if newpos == nil then
557                                         result = result .. text:sub(pos)
558                                         pos = text:len() + 1
559                                 else
560                                         local paramindex =
561                                                 tonumber(text:sub(newpos+1, newpos+1))
562                                         result = result .. text:sub(pos, newpos-1)
563                                                 .. tostring(arg[paramindex])
564                                         pos = newpos + 2
565                                 end
566                         end
567                         text = result
568                 end
569                 return text
570         end
571
572         function fgettext(text, ...)
573                 return core.formspec_escape(fgettext_ne(text, ...))
574         end
575 end
576
577 local ESCAPE_CHAR = string.char(0x1b)
578
579 function core.get_color_escape_sequence(color)
580         return ESCAPE_CHAR .. "(c@" .. color .. ")"
581 end
582
583 function core.get_background_escape_sequence(color)
584         return ESCAPE_CHAR .. "(b@" .. color .. ")"
585 end
586
587 function core.colorize(color, message)
588         local lines = tostring(message):split("\n", true)
589         local color_code = core.get_color_escape_sequence(color)
590
591         for i, line in ipairs(lines) do
592                 lines[i] = color_code .. line
593         end
594
595         return table.concat(lines, "\n") .. core.get_color_escape_sequence("#ffffff")
596 end
597
598 local function rgb_to_hex(rgb)
599         local hexadecimal = "#"
600
601         for key, value in pairs(rgb) do
602                 local hex = ""
603
604                 while(value > 0)do
605                         local index = math.fmod(value, 16) + 1
606                         value = math.floor(value / 16)
607                         hex = string.sub("0123456789ABCDEF", index, index) .. hex
608                 end
609
610                 if(string.len(hex) == 0)then
611                         hex = "00"
612                 elseif(string.len(hex) == 1)then
613                         hex = "0" .. hex
614                 end
615
616                 hexadecimal = hexadecimal .. hex
617         end
618
619         return hexadecimal
620 end
621
622 local function color_from_hue(hue)
623         local h = hue / 60
624         local c = 255
625         local x = (1 - math.abs(h % 2 - 1)) * 255
626
627         local i = math.floor(h)
628         if i == 0 then
629                 return rgb_to_hex({c, x, 0})
630         elseif i == 1 then
631                 return rgb_to_hex({x, c, 0})
632         elseif i == 2 then
633                 return rgb_to_hex({0, c, x})
634         elseif i == 3 then
635                 return rgb_to_hex({0, x, c})
636         elseif i == 4 then
637                 return rgb_to_hex({x, 0, c})
638         else
639                 return rgb_to_hex({c, 0, x})
640         end
641 end
642
643 function core.rainbow(input)
644         local step = 360 / input:len()
645         local hue = 0
646         local output = ""
647         for i = 1, input:len() do
648                 local char = input:sub(i, i)
649                 if char:match("%s") then
650                         output = output .. char
651                 else
652                         output = output  .. core.get_color_escape_sequence(color_from_hue(hue)) .. char
653                 end
654                 hue = hue + step
655         end
656         return output
657 end
658
659 function core.strip_foreground_colors(str)
660         return (str:gsub(ESCAPE_CHAR .. "%(c@[^)]+%)", ""))
661 end
662
663 function core.strip_background_colors(str)
664         return (str:gsub(ESCAPE_CHAR .. "%(b@[^)]+%)", ""))
665 end
666
667 function core.strip_colors(str)
668         return (str:gsub(ESCAPE_CHAR .. "%([bc]@[^)]+%)", ""))
669 end
670
671 function core.translate(textdomain, str, ...)
672         local start_seq
673         if textdomain == "" then
674                 start_seq = ESCAPE_CHAR .. "T"
675         else
676                 start_seq = ESCAPE_CHAR .. "(T@" .. textdomain .. ")"
677         end
678         local arg = {n=select('#', ...), ...}
679         local end_seq = ESCAPE_CHAR .. "E"
680         local arg_index = 1
681         local translated = str:gsub("@(.)", function(matched)
682                 local c = string.byte(matched)
683                 if string.byte("1") <= c and c <= string.byte("9") then
684                         local a = c - string.byte("0")
685                         if a ~= arg_index then
686                                 error("Escape sequences in string given to core.translate " ..
687                                         "are not in the correct order: got @" .. matched ..
688                                         "but expected @" .. tostring(arg_index))
689                         end
690                         if a > arg.n then
691                                 error("Not enough arguments provided to core.translate")
692                         end
693                         arg_index = arg_index + 1
694                         return ESCAPE_CHAR .. "F" .. arg[a] .. ESCAPE_CHAR .. "E"
695                 elseif matched == "n" then
696                         return "\n"
697                 else
698                         return matched
699                 end
700         end)
701         if arg_index < arg.n + 1 then
702                 error("Too many arguments provided to core.translate")
703         end
704         return start_seq .. translated .. end_seq
705 end
706
707 function core.get_translator(textdomain)
708         return function(str, ...) return core.translate(textdomain or "", str, ...) end
709 end
710
711 function core.get_pointed_thing_position(pointed_thing, above)
712         if pointed_thing.type == "node" then
713                 if above then
714                         -- The position where a node would be placed
715                         return pointed_thing.above
716                 end
717                 -- The position where a node would be dug
718                 return pointed_thing.under
719         elseif pointed_thing.type == "object" then
720                 return pointed_thing.ref and pointed_thing.ref:get_pos()
721         end
722 end
723
724 --------------------------------------------------------------------------------
725 -- Returns the exact coordinate of a pointed surface
726 --------------------------------------------------------------------------------
727 function core.pointed_thing_to_face_pos(placer, pointed_thing)
728         -- Avoid crash in some situations when player is inside a node, causing
729         -- 'above' to equal 'under'.
730         if vector.equals(pointed_thing.above, pointed_thing.under) then
731                 return pointed_thing.under
732         end
733
734         local eye_height = placer:get_properties().eye_height
735         local eye_offset_first = placer:get_eye_offset()
736         local node_pos = pointed_thing.under
737         local camera_pos = placer:get_pos()
738         local pos_off = vector.multiply(
739                         vector.subtract(pointed_thing.above, node_pos), 0.5)
740         local look_dir = placer:get_look_dir()
741         local offset, nc
742         local oc = {}
743
744         for c, v in pairs(pos_off) do
745                 if nc or v == 0 then
746                         oc[#oc + 1] = c
747                 else
748                         offset = v
749                         nc = c
750                 end
751         end
752
753         local fine_pos = {[nc] = node_pos[nc] + offset}
754         camera_pos.y = camera_pos.y + eye_height + eye_offset_first.y / 10
755         local f = (node_pos[nc] + offset - camera_pos[nc]) / look_dir[nc]
756
757         for i = 1, #oc do
758                 fine_pos[oc[i]] = camera_pos[oc[i]] + look_dir[oc[i]] * f
759         end
760         return fine_pos
761 end
762
763 function core.string_to_privs(str, delim)
764         assert(type(str) == "string")
765         delim = delim or ','
766         local privs = {}
767         for _, priv in pairs(string.split(str, delim)) do
768                 privs[priv:trim()] = true
769         end
770         return privs
771 end
772
773 function core.privs_to_string(privs, delim)
774         assert(type(privs) == "table")
775         delim = delim or ','
776         local list = {}
777         for priv, bool in pairs(privs) do
778                 if bool then
779                         list[#list + 1] = priv
780                 end
781         end
782         return table.concat(list, delim)
783 end
784
785 function core.is_nan(number)
786         return number ~= number
787 end
788
789 function core.inventorycube(img1, img2, img3)
790         img2 = img2 or img1
791         img3 = img3 or img1
792         return "[inventorycube"
793                         .. "{" .. img1:gsub("%^", "&")
794                         .. "{" .. img2:gsub("%^", "&")
795                         .. "{" .. img3:gsub("%^", "&")
796 end