misc.lua 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. function core.hash_node_position(pos)
  97. return (pos.z + 32768) * 65536 * 65536
  98. + (pos.y + 32768) * 65536
  99. + pos.x + 32768
  100. end
  101. function core.get_position_from_hash(hash)
  102. local x = (hash % 65536) - 32768
  103. hash = math.floor(hash / 65536)
  104. local y = (hash % 65536) - 32768
  105. hash = math.floor(hash / 65536)
  106. local z = (hash % 65536) - 32768
  107. return vector.new(x, y, z)
  108. end
  109. function core.get_item_group(name, group)
  110. if not core.registered_items[name] or not
  111. core.registered_items[name].groups[group] then
  112. return 0
  113. end
  114. return core.registered_items[name].groups[group]
  115. end
  116. function core.get_node_group(name, group)
  117. core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
  118. return core.get_item_group(name, group)
  119. end
  120. function core.setting_get_pos(name)
  121. local value = core.settings:get(name)
  122. if not value then
  123. return nil
  124. end
  125. return core.string_to_pos(value)
  126. end
  127. -- See l_env.cpp for the other functions
  128. function core.get_artificial_light(param1)
  129. return math.floor(param1 / 16)
  130. end
  131. -- To be overriden by protection mods
  132. function core.is_protected(pos, name)
  133. return false
  134. end
  135. function core.record_protection_violation(pos, name)
  136. for _, func in pairs(core.registered_on_protection_violation) do
  137. func(pos, name)
  138. end
  139. end
  140. -- To be overridden by Creative mods
  141. local creative_mode_cache = core.settings:get_bool("creative_mode")
  142. function core.is_creative_enabled(name)
  143. return creative_mode_cache
  144. end
  145. -- Checks if specified volume intersects a protected volume
  146. function core.is_area_protected(minp, maxp, player_name, interval)
  147. -- 'interval' is the largest allowed interval for the 3D lattice of checks.
  148. -- Compute the optimal float step 'd' for each axis so that all corners and
  149. -- borders are checked. 'd' will be smaller or equal to 'interval'.
  150. -- Subtracting 1e-4 ensures that the max co-ordinate will be reached by the
  151. -- for loop (which might otherwise not be the case due to rounding errors).
  152. -- Default to 4
  153. interval = interval or 4
  154. local d = {}
  155. for _, c in pairs({"x", "y", "z"}) do
  156. if minp[c] > maxp[c] then
  157. -- Repair positions: 'minp' > 'maxp'
  158. local tmp = maxp[c]
  159. maxp[c] = minp[c]
  160. minp[c] = tmp
  161. end
  162. if maxp[c] > minp[c] then
  163. d[c] = (maxp[c] - minp[c]) /
  164. math.ceil((maxp[c] - minp[c]) / interval) - 1e-4
  165. else
  166. d[c] = 1 -- Any value larger than 0 to avoid division by zero
  167. end
  168. end
  169. for zf = minp.z, maxp.z, d.z do
  170. local z = math.floor(zf + 0.5)
  171. for yf = minp.y, maxp.y, d.y do
  172. local y = math.floor(yf + 0.5)
  173. for xf = minp.x, maxp.x, d.x do
  174. local x = math.floor(xf + 0.5)
  175. local pos = vector.new(x, y, z)
  176. if core.is_protected(pos, player_name) then
  177. return pos
  178. end
  179. end
  180. end
  181. end
  182. return false
  183. end
  184. local raillike_ids = {}
  185. local raillike_cur_id = 0
  186. function core.raillike_group(name)
  187. local id = raillike_ids[name]
  188. if not id then
  189. raillike_cur_id = raillike_cur_id + 1
  190. raillike_ids[name] = raillike_cur_id
  191. id = raillike_cur_id
  192. end
  193. return id
  194. end
  195. -- HTTP callback interface
  196. core.set_http_api_lua(function(httpenv)
  197. httpenv.fetch = function(req, callback)
  198. local handle = httpenv.fetch_async(req)
  199. local function update_http_status()
  200. local res = httpenv.fetch_async_get(handle)
  201. if res.completed then
  202. callback(res)
  203. else
  204. core.after(0, update_http_status)
  205. end
  206. end
  207. core.after(0, update_http_status)
  208. end
  209. return httpenv
  210. end)
  211. core.set_http_api_lua = nil
  212. function core.close_formspec(player_name, formname)
  213. return core.show_formspec(player_name, formname, "")
  214. end
  215. function core.cancel_shutdown_requests()
  216. core.request_shutdown("", false, -1)
  217. end
  218. -- Used for callback handling with dynamic_add_media
  219. core.dynamic_media_callbacks = {}
  220. -- PNG encoder safety wrapper
  221. local o_encode_png = core.encode_png
  222. function core.encode_png(width, height, data, compression)
  223. if type(width) ~= "number" then
  224. error("Incorrect type for 'width', expected number, got " .. type(width))
  225. end
  226. if type(height) ~= "number" then
  227. error("Incorrect type for 'height', expected number, got " .. type(height))
  228. end
  229. local expected_byte_count = width * height * 4
  230. if type(data) ~= "table" and type(data) ~= "string" then
  231. error("Incorrect type for 'data', expected table or string, got " .. type(data))
  232. end
  233. local data_length = type(data) == "table" and #data * 4 or string.len(data)
  234. if data_length ~= expected_byte_count then
  235. error(string.format(
  236. "Incorrect length of 'data', width and height imply %d bytes but %d were provided",
  237. expected_byte_count,
  238. data_length
  239. ))
  240. end
  241. if type(data) == "table" then
  242. local dataBuf = {}
  243. for i = 1, #data do
  244. dataBuf[i] = core.colorspec_to_bytes(data[i])
  245. end
  246. data = table.concat(dataBuf)
  247. end
  248. return o_encode_png(width, height, data, compression or 6)
  249. end