misc.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. -- Minetest: builtin/misc.lua
  2. --
  3. -- Misc. API functions
  4. --
  5. minetest.timers_to_add = {}
  6. minetest.timers = {}
  7. minetest.register_globalstep(function(dtime)
  8. for _, timer in ipairs(minetest.timers_to_add) do
  9. table.insert(minetest.timers, timer)
  10. end
  11. minetest.timers_to_add = {}
  12. for index, timer in ipairs(minetest.timers) do
  13. timer.time = timer.time - dtime
  14. if timer.time <= 0 then
  15. timer.func(timer.param)
  16. table.remove(minetest.timers,index)
  17. end
  18. end
  19. end)
  20. function minetest.after(time, func, param)
  21. table.insert(minetest.timers_to_add, {time=time, func=func, param=param})
  22. end
  23. function minetest.check_player_privs(name, privs)
  24. local player_privs = minetest.get_player_privs(name)
  25. local missing_privileges = {}
  26. for priv, val in pairs(privs) do
  27. if val then
  28. if not player_privs[priv] then
  29. table.insert(missing_privileges, priv)
  30. end
  31. end
  32. end
  33. if #missing_privileges > 0 then
  34. return false, missing_privileges
  35. end
  36. return true, ""
  37. end
  38. function minetest.get_connected_players()
  39. -- This could be optimized a bit, but leave that for later
  40. local list = {}
  41. for _, obj in pairs(minetest.env:get_objects_inside_radius({x=0,y=0,z=0}, 1000000)) do
  42. if obj:is_player() then
  43. table.insert(list, obj)
  44. end
  45. end
  46. return list
  47. end
  48. function minetest.hash_node_position(pos)
  49. return (pos.z+32768)*65536*65536 + (pos.y+32768)*65536 + pos.x+32768
  50. end
  51. function minetest.get_item_group(name, group)
  52. if not minetest.registered_items[name] or not
  53. minetest.registered_items[name].groups[group] then
  54. return 0
  55. end
  56. return minetest.registered_items[name].groups[group]
  57. end
  58. function minetest.get_node_group(name, group)
  59. return minetest.get_item_group(name, group)
  60. end
  61. function minetest.string_to_pos(value)
  62. local p = {}
  63. p.x, p.y, p.z = string.match(value, "^([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
  64. if p.x and p.y and p.z then
  65. p.x = tonumber(p.x)
  66. p.y = tonumber(p.y)
  67. p.z = tonumber(p.z)
  68. return p
  69. end
  70. local p = {}
  71. p.x, p.y, p.z = string.match(value, "^%( *([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+) *%)$")
  72. if p.x and p.y and p.z then
  73. p.x = tonumber(p.x)
  74. p.y = tonumber(p.y)
  75. p.z = tonumber(p.z)
  76. return p
  77. end
  78. return nil
  79. end
  80. assert(minetest.string_to_pos("10.0, 5, -2").x == 10)
  81. assert(minetest.string_to_pos("( 10.0, 5, -2)").z == -2)
  82. assert(minetest.string_to_pos("asd, 5, -2)") == nil)
  83. function minetest.setting_get_pos(name)
  84. local value = minetest.setting_get(name)
  85. if not value then
  86. return nil
  87. end
  88. return minetest.string_to_pos(value)
  89. end