misc_helpers.lua 20 KB

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