2
0

misc.lua 6.3 KB

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