misc.lua 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. -- Minetest: builtin/misc.lua
  2. local S = core.get_translator("__builtin")
  3. --
  4. -- Misc. API functions
  5. --
  6. function core.check_player_privs(name, ...)
  7. if core.is_player(name) then
  8. name = name:get_player_name()
  9. elseif type(name) ~= "string" then
  10. error("core.check_player_privs expects a player or playername as " ..
  11. "argument.", 2)
  12. end
  13. local requested_privs = {...}
  14. local player_privs = core.get_player_privs(name)
  15. local missing_privileges = {}
  16. if type(requested_privs[1]) == "table" then
  17. -- We were provided with a table like { privA = true, privB = true }.
  18. for priv, value in pairs(requested_privs[1]) do
  19. if value and not player_privs[priv] then
  20. missing_privileges[#missing_privileges + 1] = priv
  21. end
  22. end
  23. else
  24. -- Only a list, we can process it directly.
  25. for key, priv in pairs(requested_privs) do
  26. if not player_privs[priv] then
  27. missing_privileges[#missing_privileges + 1] = priv
  28. end
  29. end
  30. end
  31. if #missing_privileges > 0 then
  32. return false, missing_privileges
  33. end
  34. return true, ""
  35. end
  36. function core.send_join_message(player_name)
  37. if not core.is_singleplayer() then
  38. core.chat_send_all("*** " .. S("@1 joined the game.", player_name))
  39. end
  40. end
  41. function core.send_leave_message(player_name, timed_out)
  42. local announcement = "*** " .. S("@1 left the game.", player_name)
  43. if timed_out then
  44. announcement = "*** " .. S("@1 left the game (timed out).", player_name)
  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. if not core.is_singleplayer() then
  51. local status = core.get_server_status(player_name, true)
  52. if status and status ~= "" then
  53. core.chat_send_player(player_name, status)
  54. end
  55. end
  56. core.send_join_message(player_name)
  57. end)
  58. core.register_on_leaveplayer(function(player, timed_out)
  59. local player_name = player:get_player_name()
  60. core.send_leave_message(player_name, timed_out)
  61. end)
  62. function core.is_player(player)
  63. -- a table being a player is also supported because it quacks sufficiently
  64. -- like a player if it has the is_player function
  65. local t = type(player)
  66. return (t == "userdata" or t == "table") and
  67. type(player.is_player) == "function" and player:is_player()
  68. end
  69. function core.player_exists(name)
  70. return core.get_auth_handler().get_auth(name) ~= nil
  71. end
  72. -- Returns two position vectors representing a box of `radius` in each
  73. -- direction centered around the player corresponding to `player_name`
  74. function core.get_player_radius_area(player_name, radius)
  75. local player = core.get_player_by_name(player_name)
  76. if player == nil then
  77. return nil
  78. end
  79. local p1 = player:get_pos()
  80. local p2 = p1
  81. if radius then
  82. p1 = vector.subtract(p1, radius)
  83. p2 = vector.add(p2, radius)
  84. end
  85. return p1, p2
  86. end
  87. function core.hash_node_position(pos)
  88. return (pos.z + 32768) * 65536 * 65536
  89. + (pos.y + 32768) * 65536
  90. + pos.x + 32768
  91. end
  92. function core.get_position_from_hash(hash)
  93. local x = (hash % 65536) - 32768
  94. hash = math.floor(hash / 65536)
  95. local y = (hash % 65536) - 32768
  96. hash = math.floor(hash / 65536)
  97. local z = (hash % 65536) - 32768
  98. return vector.new(x, y, z)
  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 = vector.new(x, y, 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. -- Used for callback handling with dynamic_add_media
  209. core.dynamic_media_callbacks = {}
  210. -- PNG encoder safety wrapper
  211. local o_encode_png = core.encode_png
  212. function core.encode_png(width, height, data, compression)
  213. if type(width) ~= "number" then
  214. error("Incorrect type for 'width', expected number, got " .. type(width))
  215. end
  216. if type(height) ~= "number" then
  217. error("Incorrect type for 'height', expected number, got " .. type(height))
  218. end
  219. local expected_byte_count = width * height * 4
  220. if type(data) ~= "table" and type(data) ~= "string" then
  221. error("Incorrect type for 'height', expected table or string, got " .. type(height))
  222. end
  223. local data_length = type(data) == "table" and #data * 4 or string.len(data)
  224. if data_length ~= expected_byte_count then
  225. error(string.format(
  226. "Incorrect length of 'data', width and height imply %d bytes but %d were provided",
  227. expected_byte_count,
  228. data_length
  229. ))
  230. end
  231. if type(data) == "table" then
  232. local dataBuf = {}
  233. for i = 1, #data do
  234. dataBuf[i] = core.colorspec_to_bytes(data[i])
  235. end
  236. data = table.concat(dataBuf)
  237. end
  238. return o_encode_png(width, height, data, compression or 6)
  239. end