misc.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. -- Minetest: builtin/misc.lua
  2. --
  3. -- Misc. API functions
  4. --
  5. function core.check_player_privs(name, ...)
  6. if core.is_player(name) then
  7. name = name:get_player_name()
  8. elseif type(name) ~= "string" then
  9. error("core.check_player_privs expects a player or playername as " ..
  10. "argument.", 2)
  11. end
  12. local requested_privs = {...}
  13. local player_privs = core.get_player_privs(name)
  14. local missing_privileges = {}
  15. if type(requested_privs[1]) == "table" then
  16. -- We were provided with a table like { privA = true, privB = true }.
  17. for priv, value in pairs(requested_privs[1]) do
  18. if value and not player_privs[priv] then
  19. missing_privileges[#missing_privileges + 1] = priv
  20. end
  21. end
  22. else
  23. -- Only a list, we can process it directly.
  24. for key, priv in pairs(requested_privs) do
  25. if not player_privs[priv] then
  26. missing_privileges[#missing_privileges + 1] = priv
  27. end
  28. end
  29. end
  30. if #missing_privileges > 0 then
  31. return false, missing_privileges
  32. end
  33. return true, ""
  34. end
  35. local player_list = {}
  36. function core.send_join_message(player_name)
  37. if not minetest.is_singleplayer() then
  38. core.chat_send_all("*** " .. player_name .. " joined the game.")
  39. end
  40. end
  41. function core.send_leave_message(player_name, timed_out)
  42. local announcement = "*** " .. player_name .. " left the game."
  43. if timed_out then
  44. announcement = announcement .. " (timed out)"
  45. end
  46. core.chat_send_all(announcement)
  47. end
  48. core.register_on_joinplayer(function(player)
  49. local player_name = player:get_player_name()
  50. player_list[player_name] = player
  51. core.send_join_message(player_name)
  52. end)
  53. core.register_on_leaveplayer(function(player, timed_out)
  54. local player_name = player:get_player_name()
  55. player_list[player_name] = nil
  56. core.send_leave_message(player_name, timed_out)
  57. end)
  58. function core.get_connected_players()
  59. local temp_table = {}
  60. for index, value in pairs(player_list) do
  61. if value:is_player_connected() then
  62. temp_table[#temp_table + 1] = value
  63. end
  64. end
  65. return temp_table
  66. end
  67. function core.is_player(player)
  68. -- a table being a player is also supported because it quacks sufficiently
  69. -- like a player if it has the is_player function
  70. local t = type(player)
  71. return (t == "userdata" or t == "table") and
  72. type(player.is_player) == "function" and player:is_player()
  73. end
  74. function minetest.player_exists(name)
  75. return minetest.get_auth_handler().get_auth(name) ~= nil
  76. end
  77. -- Returns two position vectors representing a box of `radius` in each
  78. -- direction centered around the player corresponding to `player_name`
  79. function core.get_player_radius_area(player_name, radius)
  80. local player = core.get_player_by_name(player_name)
  81. if player == nil then
  82. return nil
  83. end
  84. local p1 = player:getpos()
  85. local p2 = p1
  86. if radius then
  87. p1 = vector.subtract(p1, radius)
  88. p2 = vector.add(p2, radius)
  89. end
  90. return p1, p2
  91. end
  92. function core.hash_node_position(pos)
  93. return (pos.z+32768)*65536*65536 + (pos.y+32768)*65536 + pos.x+32768
  94. end
  95. function core.get_position_from_hash(hash)
  96. local pos = {}
  97. pos.x = (hash%65536) - 32768
  98. hash = math.floor(hash/65536)
  99. pos.y = (hash%65536) - 32768
  100. hash = math.floor(hash/65536)
  101. pos.z = (hash%65536) - 32768
  102. return pos
  103. end
  104. function core.get_item_group(name, group)
  105. if not core.registered_items[name] or not
  106. core.registered_items[name].groups[group] then
  107. return 0
  108. end
  109. return core.registered_items[name].groups[group]
  110. end
  111. function core.get_node_group(name, group)
  112. core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
  113. return core.get_item_group(name, group)
  114. end
  115. function core.setting_get_pos(name)
  116. local value = core.settings:get(name)
  117. if not value then
  118. return nil
  119. end
  120. return core.string_to_pos(value)
  121. end
  122. -- To be overriden by protection mods
  123. function core.is_protected(pos, name)
  124. return false
  125. end
  126. function core.record_protection_violation(pos, name)
  127. for _, func in pairs(core.registered_on_protection_violation) do
  128. func(pos, name)
  129. end
  130. end
  131. -- Checks if specified volume intersects a protected volume
  132. function core.is_area_protected(minp, maxp, player_name, interval)
  133. -- 'interval' is the largest allowed interval for the 3D lattice of checks.
  134. -- Compute the optimal float step 'd' for each axis so that all corners and
  135. -- borders are checked. 'd' will be smaller or equal to 'interval'.
  136. -- Subtracting 1e-4 ensures that the max co-ordinate will be reached by the
  137. -- for loop (which might otherwise not be the case due to rounding errors).
  138. -- Default to 4
  139. interval = interval or 4
  140. local d = {}
  141. for _, c in pairs({"x", "y", "z"}) do
  142. if minp[c] > maxp[c] then
  143. -- Repair positions: 'minp' > 'maxp'
  144. local tmp = maxp[c]
  145. maxp[c] = minp[c]
  146. minp[c] = tmp
  147. end
  148. if maxp[c] > minp[c] then
  149. d[c] = (maxp[c] - minp[c]) /
  150. math.ceil((maxp[c] - minp[c]) / interval) - 1e-4
  151. else
  152. d[c] = 1 -- Any value larger than 0 to avoid division by zero
  153. end
  154. end
  155. for zf = minp.z, maxp.z, d.z do
  156. local z = math.floor(zf + 0.5)
  157. for yf = minp.y, maxp.y, d.y do
  158. local y = math.floor(yf + 0.5)
  159. for xf = minp.x, maxp.x, d.x do
  160. local x = math.floor(xf + 0.5)
  161. local pos = {x = x, y = y, z = z}
  162. if core.is_protected(pos, player_name) then
  163. return pos
  164. end
  165. end
  166. end
  167. end
  168. return false
  169. end
  170. local raillike_ids = {}
  171. local raillike_cur_id = 0
  172. function core.raillike_group(name)
  173. local id = raillike_ids[name]
  174. if not id then
  175. raillike_cur_id = raillike_cur_id + 1
  176. raillike_ids[name] = raillike_cur_id
  177. id = raillike_cur_id
  178. end
  179. return id
  180. end
  181. -- HTTP callback interface
  182. function core.http_add_fetch(httpenv)
  183. httpenv.fetch = function(req, callback)
  184. local handle = httpenv.fetch_async(req)
  185. local function update_http_status()
  186. local res = httpenv.fetch_async_get(handle)
  187. if res.completed then
  188. callback(res)
  189. else
  190. core.after(0, update_http_status)
  191. end
  192. end
  193. core.after(0, update_http_status)
  194. end
  195. return httpenv
  196. end
  197. function core.close_formspec(player_name, formname)
  198. return minetest.show_formspec(player_name, formname, "")
  199. end
  200. function core.cancel_shutdown_requests()
  201. core.request_shutdown("", false, -1)
  202. end