2
0

misc.lua 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. function core.send_join_message(player_name)
  36. if not core.is_singleplayer() then
  37. core.chat_send_all("*** " .. player_name .. " joined the game.")
  38. end
  39. end
  40. function core.send_leave_message(player_name, timed_out)
  41. local announcement = "*** " .. player_name .. " left the game."
  42. if timed_out then
  43. announcement = announcement .. " (timed out)"
  44. end
  45. core.chat_send_all(announcement)
  46. end
  47. core.register_on_joinplayer(function(player)
  48. local player_name = player:get_player_name()
  49. if not core.is_singleplayer() then
  50. local status = core.get_server_status(player_name, true)
  51. if status and status ~= "" then
  52. core.chat_send_player(player_name, status)
  53. end
  54. end
  55. core.send_join_message(player_name)
  56. end)
  57. core.register_on_leaveplayer(function(player, timed_out)
  58. local player_name = player:get_player_name()
  59. core.send_leave_message(player_name, timed_out)
  60. end)
  61. function core.is_player(player)
  62. -- a table being a player is also supported because it quacks sufficiently
  63. -- like a player if it has the is_player function
  64. local t = type(player)
  65. return (t == "userdata" or t == "table") and
  66. type(player.is_player) == "function" and player:is_player()
  67. end
  68. function core.player_exists(name)
  69. return core.get_auth_handler().get_auth(name) ~= nil
  70. end
  71. -- Returns two position vectors representing a box of `radius` in each
  72. -- direction centered around the player corresponding to `player_name`
  73. function core.get_player_radius_area(player_name, radius)
  74. local player = core.get_player_by_name(player_name)
  75. if player == nil then
  76. return nil
  77. end
  78. local p1 = player:get_pos()
  79. local p2 = p1
  80. if radius then
  81. p1 = vector.subtract(p1, radius)
  82. p2 = vector.add(p2, radius)
  83. end
  84. return p1, p2
  85. end
  86. function core.hash_node_position(pos)
  87. return (pos.z + 32768) * 65536 * 65536
  88. + (pos.y + 32768) * 65536
  89. + pos.x + 32768
  90. end
  91. function core.get_position_from_hash(hash)
  92. local pos = {}
  93. pos.x = (hash % 65536) - 32768
  94. hash = math.floor(hash / 65536)
  95. pos.y = (hash % 65536) - 32768
  96. hash = math.floor(hash / 65536)
  97. pos.z = (hash % 65536) - 32768
  98. return pos
  99. end
  100. function core.get_item_group(name, group)
  101. if not core.registered_items[name] or not
  102. core.registered_items[name].groups[group] then
  103. return 0
  104. end
  105. return core.registered_items[name].groups[group]
  106. end
  107. function core.get_node_group(name, group)
  108. core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
  109. return core.get_item_group(name, group)
  110. end
  111. function core.setting_get_pos(name)
  112. local value = core.settings:get(name)
  113. if not value then
  114. return nil
  115. end
  116. return core.string_to_pos(value)
  117. end
  118. -- See l_env.cpp for the other functions
  119. function core.get_artificial_light(param1)
  120. return math.floor(param1 / 16)
  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. -- To be overridden by Creative mods
  132. local creative_mode_cache = core.settings:get_bool("creative_mode")
  133. function core.is_creative_enabled(name)
  134. return creative_mode_cache
  135. end
  136. -- Checks if specified volume intersects a protected volume
  137. function core.is_area_protected(minp, maxp, player_name, interval)
  138. -- 'interval' is the largest allowed interval for the 3D lattice of checks.
  139. -- Compute the optimal float step 'd' for each axis so that all corners and
  140. -- borders are checked. 'd' will be smaller or equal to 'interval'.
  141. -- Subtracting 1e-4 ensures that the max co-ordinate will be reached by the
  142. -- for loop (which might otherwise not be the case due to rounding errors).
  143. -- Default to 4
  144. interval = interval or 4
  145. local d = {}
  146. for _, c in pairs({"x", "y", "z"}) do
  147. if minp[c] > maxp[c] then
  148. -- Repair positions: 'minp' > 'maxp'
  149. local tmp = maxp[c]
  150. maxp[c] = minp[c]
  151. minp[c] = tmp
  152. end
  153. if maxp[c] > minp[c] then
  154. d[c] = (maxp[c] - minp[c]) /
  155. math.ceil((maxp[c] - minp[c]) / interval) - 1e-4
  156. else
  157. d[c] = 1 -- Any value larger than 0 to avoid division by zero
  158. end
  159. end
  160. for zf = minp.z, maxp.z, d.z do
  161. local z = math.floor(zf + 0.5)
  162. for yf = minp.y, maxp.y, d.y do
  163. local y = math.floor(yf + 0.5)
  164. for xf = minp.x, maxp.x, d.x do
  165. local x = math.floor(xf + 0.5)
  166. local pos = {x = x, y = y, z = z}
  167. if core.is_protected(pos, player_name) then
  168. return pos
  169. end
  170. end
  171. end
  172. end
  173. return false
  174. end
  175. local raillike_ids = {}
  176. local raillike_cur_id = 0
  177. function core.raillike_group(name)
  178. local id = raillike_ids[name]
  179. if not id then
  180. raillike_cur_id = raillike_cur_id + 1
  181. raillike_ids[name] = raillike_cur_id
  182. id = raillike_cur_id
  183. end
  184. return id
  185. end
  186. -- HTTP callback interface
  187. function core.http_add_fetch(httpenv)
  188. httpenv.fetch = function(req, callback)
  189. local handle = httpenv.fetch_async(req)
  190. local function update_http_status()
  191. local res = httpenv.fetch_async_get(handle)
  192. if res.completed then
  193. callback(res)
  194. else
  195. core.after(0, update_http_status)
  196. end
  197. end
  198. core.after(0, update_http_status)
  199. end
  200. return httpenv
  201. end
  202. function core.close_formspec(player_name, formname)
  203. return core.show_formspec(player_name, formname, "")
  204. end
  205. function core.cancel_shutdown_requests()
  206. core.request_shutdown("", false, -1)
  207. end
  208. -- Callback handling for dynamic_add_media
  209. local dynamic_add_media_raw = core.dynamic_add_media_raw
  210. core.dynamic_add_media_raw = nil
  211. function core.dynamic_add_media(filepath, callback)
  212. local ret = dynamic_add_media_raw(filepath)
  213. if ret == false then
  214. return ret
  215. end
  216. if callback == nil then
  217. core.log("deprecated", "Calling minetest.dynamic_add_media without "..
  218. "a callback is deprecated and will stop working in future versions.")
  219. else
  220. -- At the moment async loading is not actually implemented, so we
  221. -- immediately call the callback ourselves
  222. for _, name in ipairs(ret) do
  223. callback(name)
  224. end
  225. end
  226. return true
  227. end