misc.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. -- Minetest: builtin/misc.lua
  2. --
  3. -- Misc. API functions
  4. --
  5. local jobs = {}
  6. local time = 0.0
  7. local last = 0.0
  8. core.register_globalstep(function(dtime)
  9. local new = core.get_us_time() / 1000000
  10. if new > last then
  11. time = time + (new - last)
  12. else
  13. -- Overflow, we may lose a little bit of time here but
  14. -- only 1 tick max, potentially running timers slightly
  15. -- too early.
  16. time = time + new
  17. end
  18. last = new
  19. if #jobs < 1 then
  20. return
  21. end
  22. -- Iterate backwards so that we miss any new timers added by
  23. -- a timer callback, and so that we don't skip the next timer
  24. -- in the list if we remove one.
  25. for i = #jobs, 1, -1 do
  26. local job = jobs[i]
  27. if time >= job.expire then
  28. core.set_last_run_mod(job.mod_origin)
  29. job.func(unpack(job.arg))
  30. table.remove(jobs, i)
  31. end
  32. end
  33. end)
  34. function core.after(after, func, ...)
  35. assert(tonumber(time) and type(func) == "function",
  36. "Invalid core.after invocation")
  37. table.insert(jobs, {
  38. func = func,
  39. expire = time + after,
  40. arg = {...},
  41. mod_origin = core.get_last_run_mod()
  42. })
  43. end
  44. function core.check_player_privs(player_or_name, ...)
  45. local name = player_or_name
  46. -- Check if we have been provided with a Player object.
  47. if type(name) ~= "string" then
  48. name = name:get_player_name()
  49. end
  50. local requested_privs = {...}
  51. local player_privs = core.get_player_privs(name)
  52. local missing_privileges = {}
  53. if type(requested_privs[1]) == "table" then
  54. -- We were provided with a table like { privA = true, privB = true }.
  55. for priv, value in pairs(requested_privs[1]) do
  56. if value and not player_privs[priv] then
  57. table.insert(missing_privileges, priv)
  58. end
  59. end
  60. else
  61. -- Only a list, we can process it directly.
  62. for key, priv in pairs(requested_privs) do
  63. if not player_privs[priv] then
  64. table.insert(missing_privileges, priv)
  65. end
  66. end
  67. end
  68. if #missing_privileges > 0 then
  69. return false, missing_privileges
  70. end
  71. return true, ""
  72. end
  73. local player_list = {}
  74. core.register_on_joinplayer(function(player)
  75. player_list[player:get_player_name()] = player
  76. end)
  77. core.register_on_leaveplayer(function(player)
  78. player_list[player:get_player_name()] = nil
  79. end)
  80. function core.get_connected_players()
  81. local temp_table = {}
  82. for index, value in pairs(player_list) do
  83. if value:is_player_connected() then
  84. table.insert(temp_table, value)
  85. end
  86. end
  87. return temp_table
  88. end
  89. -- Returns two position vectors representing a box of `radius` in each
  90. -- direction centered around the player corresponding to `player_name`
  91. function core.get_player_radius_area(player_name, radius)
  92. local player = core.get_player_by_name(player_name)
  93. if player == nil then
  94. return nil
  95. end
  96. local p1 = player:getpos()
  97. local p2 = p1
  98. if radius then
  99. p1 = vector.subtract(p1, radius)
  100. p2 = vector.add(p2, radius)
  101. end
  102. return p1, p2
  103. end
  104. function core.hash_node_position(pos)
  105. return (pos.z+32768)*65536*65536 + (pos.y+32768)*65536 + pos.x+32768
  106. end
  107. function core.get_position_from_hash(hash)
  108. local pos = {}
  109. pos.x = (hash%65536) - 32768
  110. hash = math.floor(hash/65536)
  111. pos.y = (hash%65536) - 32768
  112. hash = math.floor(hash/65536)
  113. pos.z = (hash%65536) - 32768
  114. return pos
  115. end
  116. function core.get_item_group(name, group)
  117. if not core.registered_items[name] or not
  118. core.registered_items[name].groups[group] then
  119. return 0
  120. end
  121. return core.registered_items[name].groups[group]
  122. end
  123. function core.get_node_group(name, group)
  124. core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
  125. return core.get_item_group(name, group)
  126. end
  127. function core.setting_get_pos(name)
  128. local value = core.setting_get(name)
  129. if not value then
  130. return nil
  131. end
  132. return core.string_to_pos(value)
  133. end
  134. -- To be overriden by protection mods
  135. function core.is_protected(pos, name)
  136. return false
  137. end
  138. function core.record_protection_violation(pos, name)
  139. for _, func in pairs(core.registered_on_protection_violation) do
  140. func(pos, name)
  141. end
  142. end
  143. local raillike_ids = {}
  144. local raillike_cur_id = 0
  145. function core.raillike_group(name)
  146. local id = raillike_ids[name]
  147. if not id then
  148. raillike_cur_id = raillike_cur_id + 1
  149. raillike_ids[name] = raillike_cur_id
  150. id = raillike_cur_id
  151. end
  152. return id
  153. end