diff options
| author | Elias Fleckenstein <eliasfleckenstein@web.de> | 2022-05-17 22:12:00 +0200 |
|---|---|---|
| committer | Elias Fleckenstein <eliasfleckenstein@web.de> | 2022-05-17 22:12:00 +0200 |
| commit | 21df26984da91143c15587f5a03c98d68c3adc4e (patch) | |
| tree | aaa707a628ad331f67890023dffe1b4f60dd01d3 /builtin/game | |
| parent | b09fc5de5cdb021f43ad32b7e3f50dc75c0bc622 (diff) | |
| parent | eabf05758e3ba5f6f4bb1b8d1d1f02179b84e410 (diff) | |
| download | dragonfireclient-21df26984da91143c15587f5a03c98d68c3adc4e.tar.xz | |
Merge branch 'master' of https://github.com/minetest/minetest
Diffstat (limited to 'builtin/game')
| -rw-r--r-- | builtin/game/async.lua | 22 | ||||
| -rw-r--r-- | builtin/game/chat.lua | 42 | ||||
| -rw-r--r-- | builtin/game/features.lua | 1 | ||||
| -rw-r--r-- | builtin/game/init.lua | 3 | ||||
| -rw-r--r-- | builtin/game/item.lua | 200 | ||||
| -rw-r--r-- | builtin/game/item_entity.lua | 13 | ||||
| -rw-r--r-- | builtin/game/item_s.lua | 156 | ||||
| -rw-r--r-- | builtin/game/misc.lua | 116 | ||||
| -rw-r--r-- | builtin/game/misc_s.lua | 93 | ||||
| -rw-r--r-- | builtin/game/privileges.lua | 4 | ||||
| -rw-r--r-- | builtin/game/register.lua | 14 | ||||
| -rw-r--r-- | builtin/game/statbars.lua | 68 |
12 files changed, 425 insertions, 307 deletions
diff --git a/builtin/game/async.lua b/builtin/game/async.lua new file mode 100644 index 000000000..469f179d7 --- /dev/null +++ b/builtin/game/async.lua @@ -0,0 +1,22 @@ + +core.async_jobs = {} + +function core.async_event_handler(jobid, retval) + local callback = core.async_jobs[jobid] + assert(type(callback) == "function") + callback(unpack(retval, 1, retval.n)) + core.async_jobs[jobid] = nil +end + +function core.handle_async(func, callback, ...) + assert(type(func) == "function" and type(callback) == "function", + "Invalid minetest.handle_async invocation") + local args = {n = select("#", ...), ...} + local mod_origin = core.get_last_run_mod() + + local jobid = core.do_async_callback(func, args, mod_origin) + core.async_jobs[jobid] = callback + + return true +end + diff --git a/builtin/game/chat.lua b/builtin/game/chat.lua index 99296f782..c4fb6314e 100644 --- a/builtin/game/chat.lua +++ b/builtin/game/chat.lua @@ -310,12 +310,7 @@ local function handle_revoke_command(caller, revokename, revokeprivstr) and revokename == core.settings:get("name") and revokename ~= "" if revokeprivstr == "all" then - revokeprivs = privs - privs = {} - else - for priv, _ in pairs(revokeprivs) do - privs[priv] = nil - end + revokeprivs = table.copy(privs) end local privs_unknown = "" @@ -332,7 +327,10 @@ local function handle_revoke_command(caller, revokename, revokeprivstr) end local def = core.registered_privileges[priv] if not def then - privs_unknown = privs_unknown .. S("Unknown privilege: @1", priv) .. "\n" + -- Old/removed privileges might still be granted to certain players + if not privs[priv] then + privs_unknown = privs_unknown .. S("Unknown privilege: @1", priv) .. "\n" + end elseif is_singleplayer and def.give_to_singleplayer then irrevokable[priv] = true elseif is_admin and def.give_to_admin then @@ -359,19 +357,22 @@ local function handle_revoke_command(caller, revokename, revokeprivstr) end local revokecount = 0 - - core.set_player_privs(revokename, privs) for priv, _ in pairs(revokeprivs) do - -- call the on_revoke callbacks - core.run_priv_callbacks(revokename, priv, caller, "revoke") + privs[priv] = nil revokecount = revokecount + 1 end - local new_privs = core.get_player_privs(revokename) if revokecount == 0 then return false, S("No privileges were revoked.") end + core.set_player_privs(revokename, privs) + for priv, _ in pairs(revokeprivs) do + -- call the on_revoke callbacks + core.run_priv_callbacks(revokename, priv, caller, "revoke") + end + local new_privs = core.get_player_privs(revokename) + core.log("action", caller..' revoked (' ..core.privs_to_string(revokeprivs, ', ') ..') privileges from '..revokename) @@ -524,7 +525,7 @@ end -- Teleports player <name> to <p> if possible local function teleport_to_pos(name, p) - local lm = 31000 + local lm = 31007 -- equals MAX_MAP_GENERATION_LIMIT in C++ if p.x < -lm or p.x > lm or p.y < -lm or p.y > lm or p.z < -lm or p.z > lm then return false, S("Cannot teleport out of map bounds!") @@ -621,6 +622,10 @@ core.register_chatcommand("set", { setname, setvalue = string.match(param, "([^ ]+) (.+)") if setname and setvalue then + if setname:sub(1, 7) == "secure." then + return false, S("Failed. Cannot modify secure settings. " + .. "Edit the settings file manually.") + end if not core.settings:get(setname) then return false, S("Failed. Use '/set -n <name> <value>' " .. "to create a new setting.") @@ -1034,12 +1039,11 @@ core.register_chatcommand("time", { end local hour, minute = param:match("^(%d+):(%d+)$") if not hour then - local new_time = tonumber(param) - if not new_time then - return false, S("Invalid time.") + local new_time = tonumber(param) or -1 + if new_time ~= new_time or new_time < 0 or new_time > 24000 then + return false, S("Invalid time (must be between 0 and 24000).") end - -- Backward compatibility. - core.set_timeofday((new_time % 24000) / 24000) + core.set_timeofday(new_time / 24000) core.log("action", name .. " sets time to " .. new_time) return true, S("Time of day changed.") end @@ -1283,7 +1287,7 @@ local function handle_kill_command(killer, victim) return false, S("@1 is already dead.", victim) end end - if not killer == victim then + if killer ~= victim then core.log("action", string.format("%s killed %s", killer, victim)) end -- Kill victim diff --git a/builtin/game/features.lua b/builtin/game/features.lua index 583ef5092..0d55bb01f 100644 --- a/builtin/game/features.lua +++ b/builtin/game/features.lua @@ -22,6 +22,7 @@ core.features = { degrotate_240_steps = true, abm_min_max_y = true, dynamic_add_media_table = true, + get_sky_as_table = true, } function core.has_feature(arg) diff --git a/builtin/game/init.lua b/builtin/game/init.lua index 9a9966d7e..b9ab97b58 100644 --- a/builtin/game/init.lua +++ b/builtin/game/init.lua @@ -8,6 +8,7 @@ local gamepath = scriptpath .. "game".. DIR_DELIM local builtin_shared = {} dofile(gamepath .. "constants.lua") +dofile(gamepath .. "item_s.lua") assert(loadfile(gamepath .. "item.lua"))(builtin_shared) dofile(gamepath .. "register.lua") @@ -19,6 +20,7 @@ dofile(commonpath .. "after.lua") dofile(commonpath .. "voxelarea.lua") dofile(gamepath .. "item_entity.lua") dofile(gamepath .. "deprecated.lua") +dofile(gamepath .. "misc_s.lua") dofile(gamepath .. "misc.lua") dofile(gamepath .. "privileges.lua") dofile(gamepath .. "auth.lua") @@ -32,5 +34,6 @@ dofile(gamepath .. "features.lua") dofile(gamepath .. "forceloading.lua") dofile(gamepath .. "statbars.lua") dofile(gamepath .. "knockback.lua") +dofile(gamepath .. "async.lua") profiler = nil diff --git a/builtin/game/item.lua b/builtin/game/item.lua index 92818b177..5543e9a3f 100644 --- a/builtin/game/item.lua +++ b/builtin/game/item.lua @@ -15,144 +15,19 @@ end -- Item definition helpers -- -function core.dir_to_facedir(dir, is6d) - --account for y if requested - if is6d and math.abs(dir.y) > math.abs(dir.x) and math.abs(dir.y) > math.abs(dir.z) then - - --from above - if dir.y < 0 then - if math.abs(dir.x) > math.abs(dir.z) then - if dir.x < 0 then - return 19 - else - return 13 - end - else - if dir.z < 0 then - return 10 - else - return 4 - end - end - - --from below - else - if math.abs(dir.x) > math.abs(dir.z) then - if dir.x < 0 then - return 15 - else - return 17 - end - else - if dir.z < 0 then - return 6 - else - return 8 - end - end - end - - --otherwise, place horizontally - elseif math.abs(dir.x) > math.abs(dir.z) then - if dir.x < 0 then - return 3 - else - return 1 - end - else - if dir.z < 0 then - return 2 - else - return 0 - end - end -end - --- Table of possible dirs -local facedir_to_dir = { - vector.new( 0, 0, 1), - vector.new( 1, 0, 0), - vector.new( 0, 0, -1), - vector.new(-1, 0, 0), - vector.new( 0, -1, 0), - vector.new( 0, 1, 0), -} --- Mapping from facedir value to index in facedir_to_dir. -local facedir_to_dir_map = { - [0]=1, 2, 3, 4, - 5, 2, 6, 4, - 6, 2, 5, 4, - 1, 5, 3, 6, - 1, 6, 3, 5, - 1, 4, 3, 2, -} -function core.facedir_to_dir(facedir) - return facedir_to_dir[facedir_to_dir_map[facedir % 32]] -end - -function core.dir_to_wallmounted(dir) - if math.abs(dir.y) > math.max(math.abs(dir.x), math.abs(dir.z)) then - if dir.y < 0 then - return 1 - else - return 0 - end - elseif math.abs(dir.x) > math.abs(dir.z) then - if dir.x < 0 then - return 3 - else - return 2 - end - else - if dir.z < 0 then - return 5 - else - return 4 +function core.get_pointed_thing_position(pointed_thing, above) + if pointed_thing.type == "node" then + if above then + -- The position where a node would be placed + return pointed_thing.above end + -- The position where a node would be dug + return pointed_thing.under + elseif pointed_thing.type == "object" then + return pointed_thing.ref and pointed_thing.ref:get_pos() end end --- table of dirs in wallmounted order -local wallmounted_to_dir = { - [0] = vector.new( 0, 1, 0), - vector.new( 0, -1, 0), - vector.new( 1, 0, 0), - vector.new(-1, 0, 0), - vector.new( 0, 0, 1), - vector.new( 0, 0, -1), -} -function core.wallmounted_to_dir(wallmounted) - return wallmounted_to_dir[wallmounted % 8] -end - -function core.dir_to_yaw(dir) - return -math.atan2(dir.x, dir.z) -end - -function core.yaw_to_dir(yaw) - return vector.new(-math.sin(yaw), 0, math.cos(yaw)) -end - -function core.is_colored_paramtype(ptype) - return (ptype == "color") or (ptype == "colorfacedir") or - (ptype == "colorwallmounted") or (ptype == "colordegrotate") -end - -function core.strip_param2_color(param2, paramtype2) - if not core.is_colored_paramtype(paramtype2) then - return nil - end - if paramtype2 == "colorfacedir" then - param2 = math.floor(param2 / 32) * 32 - elseif paramtype2 == "colorwallmounted" then - param2 = math.floor(param2 / 8) * 8 - elseif paramtype2 == "colordegrotate" then - param2 = math.floor(param2 / 32) * 32 - end - -- paramtype2 == "color" requires no modification. - return param2 -end - local function has_all_groups(tbl, required_groups) if type(required_groups) == "string" then return (tbl[required_groups] or 0) ~= 0 @@ -477,34 +352,41 @@ function core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed return result end end + -- read definition before potentially emptying the stack local def = itemstack:get_definition() - if itemstack:take_item() ~= nil then - user:set_hp(user:get_hp() + hp_change) - - if def and def.sound and def.sound.eat then - core.sound_play(def.sound.eat, { - pos = user:get_pos(), - max_hear_distance = 16 - }, true) - end + if itemstack:take_item():is_empty() then + return itemstack + end + + if def and def.sound and def.sound.eat then + core.sound_play(def.sound.eat, { + pos = user:get_pos(), + max_hear_distance = 16 + }, true) + end - if replace_with_item then - if itemstack:is_empty() then - itemstack:add_item(replace_with_item) + -- Changing hp might kill the player causing mods to do who-knows-what to the + -- inventory, so do this before set_hp(). + if replace_with_item then + if itemstack:is_empty() then + itemstack:add_item(replace_with_item) + else + local inv = user:get_inventory() + -- Check if inv is null, since non-players don't have one + if inv and inv:room_for_item("main", {name=replace_with_item}) then + inv:add_item("main", replace_with_item) else - local inv = user:get_inventory() - -- Check if inv is null, since non-players don't have one - if inv and inv:room_for_item("main", {name=replace_with_item}) then - inv:add_item("main", replace_with_item) - else - local pos = user:get_pos() - pos.y = math.floor(pos.y + 0.5) - core.add_item(pos, replace_with_item) - end + local pos = user:get_pos() + pos.y = math.floor(pos.y + 0.5) + core.add_item(pos, replace_with_item) end end end - return itemstack + user:set_wielded_item(itemstack) + + user:set_hp(user:get_hp() + hp_change) + + return nil -- don't overwrite wield item a second time end function core.item_eat(hp_change, replace_with_item) @@ -585,7 +467,7 @@ function core.node_dig(pos, node, digger) if wielded then local wdef = wielded:get_definition() local tp = wielded:get_tool_capabilities() - local dp = core.get_dig_params(def and def.groups, tp) + local dp = core.get_dig_params(def and def.groups, tp, wielded:get_wear()) if wdef and wdef.after_use then wielded = wdef.after_use(wielded, digger, node, dp) or wielded else @@ -647,9 +529,7 @@ function core.node_dig(pos, node, digger) -- Run script hook for _, callback in ipairs(core.registered_on_dignodes) do local origin = core.callback_origins[callback] - if origin then - core.set_last_run_mod(origin.mod) - end + core.set_last_run_mod(origin.mod) -- Copy pos and node because callback can modify them local pos_copy = vector.new(pos) diff --git a/builtin/game/item_entity.lua b/builtin/game/item_entity.lua index 9b1b23bfd..53f98a7c7 100644 --- a/builtin/game/item_entity.lua +++ b/builtin/game/item_entity.lua @@ -58,17 +58,21 @@ core.register_entity(":__builtin:item", { local glow = def and def.light_source and math.floor(def.light_source / 2 + 0.5) + local size_bias = 1e-3 * math.random() -- small random bias to counter Z-fighting + local c = {-size, -size, -size, size, size, size} self.object:set_properties({ is_visible = true, visual = "wielditem", textures = {itemname}, - visual_size = {x = size, y = size}, - collisionbox = {-size, -size, -size, size, size, size}, + visual_size = {x = size + size_bias, y = size + size_bias}, + collisionbox = c, automatic_rotate = math.pi * 0.5 * 0.2 / size, wield_item = self.itemstring, glow = glow, }) + -- cache for usage in on_step + self._collisionbox = c end, get_staticdata = function(self) @@ -93,6 +97,7 @@ core.register_entity(":__builtin:item", { self.object:set_armor_groups({immortal = 1}) self.object:set_velocity({x = 0, y = 2, z = 0}) self.object:set_acceleration({x = 0, y = -gravity, z = 0}) + self._collisionbox = self.initial_properties.collisionbox self:set_item() end, @@ -163,7 +168,7 @@ core.register_entity(":__builtin:item", { local pos = self.object:get_pos() local node = core.get_node_or_nil({ x = pos.x, - y = pos.y + self.object:get_properties().collisionbox[2] - 0.05, + y = pos.y + self._collisionbox[2] - 0.05, z = pos.z }) -- Delete in 'ignore' nodes @@ -176,7 +181,7 @@ core.register_entity(":__builtin:item", { if self.force_out then -- This code runs after the entity got a push from the is_stuck code. -- It makes sure the entity is entirely outside the solid node - local c = self.object:get_properties().collisionbox + local c = self._collisionbox local s = self.force_out_start local f = self.force_out local ok = (f.x > 0 and pos.x + c[1] > s.x + 0.5) or diff --git a/builtin/game/item_s.lua b/builtin/game/item_s.lua new file mode 100644 index 000000000..a51cd0a1c --- /dev/null +++ b/builtin/game/item_s.lua @@ -0,0 +1,156 @@ +-- Minetest: builtin/item_s.lua +-- The distinction of what goes here is a bit tricky, basically it's everything +-- that does not (directly or indirectly) need access to ServerEnvironment, +-- Server or writable access to IGameDef on the engine side. +-- (The '_s' stands for standalone.) + +-- +-- Item definition helpers +-- + +function core.inventorycube(img1, img2, img3) + img2 = img2 or img1 + img3 = img3 or img1 + return "[inventorycube" + .. "{" .. img1:gsub("%^", "&") + .. "{" .. img2:gsub("%^", "&") + .. "{" .. img3:gsub("%^", "&") +end + +function core.dir_to_facedir(dir, is6d) + --account for y if requested + if is6d and math.abs(dir.y) > math.abs(dir.x) and math.abs(dir.y) > math.abs(dir.z) then + + --from above + if dir.y < 0 then + if math.abs(dir.x) > math.abs(dir.z) then + if dir.x < 0 then + return 19 + else + return 13 + end + else + if dir.z < 0 then + return 10 + else + return 4 + end + end + + --from below + else + if math.abs(dir.x) > math.abs(dir.z) then + if dir.x < 0 then + return 15 + else + return 17 + end + else + if dir.z < 0 then + return 6 + else + return 8 + end + end + end + + --otherwise, place horizontally + elseif math.abs(dir.x) > math.abs(dir.z) then + if dir.x < 0 then + return 3 + else + return 1 + end + else + if dir.z < 0 then + return 2 + else + return 0 + end + end +end + +-- Table of possible dirs +local facedir_to_dir = { + vector.new( 0, 0, 1), + vector.new( 1, 0, 0), + vector.new( 0, 0, -1), + vector.new(-1, 0, 0), + vector.new( 0, -1, 0), + vector.new( 0, 1, 0), +} +-- Mapping from facedir value to index in facedir_to_dir. +local facedir_to_dir_map = { + [0]=1, 2, 3, 4, + 5, 2, 6, 4, + 6, 2, 5, 4, + 1, 5, 3, 6, + 1, 6, 3, 5, + 1, 4, 3, 2, +} +function core.facedir_to_dir(facedir) + return facedir_to_dir[facedir_to_dir_map[facedir % 32]] +end + +function core.dir_to_wallmounted(dir) + if math.abs(dir.y) > math.max(math.abs(dir.x), math.abs(dir.z)) then + if dir.y < 0 then + return 1 + else + return 0 + end + elseif math.abs(dir.x) > math.abs(dir.z) then + if dir.x < 0 then + return 3 + else + return 2 + end + else + if dir.z < 0 then + return 5 + else + return 4 + end + end +end + +-- table of dirs in wallmounted order +local wallmounted_to_dir = { + [0] = vector.new( 0, 1, 0), + vector.new( 0, -1, 0), + vector.new( 1, 0, 0), + vector.new(-1, 0, 0), + vector.new( 0, 0, 1), + vector.new( 0, 0, -1), +} +function core.wallmounted_to_dir(wallmounted) + return wallmounted_to_dir[wallmounted % 8] +end + +function core.dir_to_yaw(dir) + return -math.atan2(dir.x, dir.z) +end + +function core.yaw_to_dir(yaw) + return vector.new(-math.sin(yaw), 0, math.cos(yaw)) +end + +function core.is_colored_paramtype(ptype) + return (ptype == "color") or (ptype == "colorfacedir") or + (ptype == "colorwallmounted") or (ptype == "colordegrotate") +end + +function core.strip_param2_color(param2, paramtype2) + if not core.is_colored_paramtype(paramtype2) then + return nil + end + if paramtype2 == "colorfacedir" then + param2 = math.floor(param2 / 32) * 32 + elseif paramtype2 == "colorwallmounted" then + param2 = math.floor(param2 / 8) * 8 + elseif paramtype2 == "colordegrotate" then + param2 = math.floor(param2 / 32) * 32 + end + -- paramtype2 == "color" requires no modification. + return param2 +end diff --git a/builtin/game/misc.lua b/builtin/game/misc.lua index 63d64817c..997b1894a 100644 --- a/builtin/game/misc.lua +++ b/builtin/game/misc.lua @@ -6,6 +6,16 @@ local S = core.get_translator("__builtin") -- Misc. API functions -- +-- @spec core.kick_player(String, String) :: Boolean +function core.kick_player(player_name, reason) + if type(reason) == "string" then + reason = "Kicked: " .. reason + else + reason = "Kicked." + end + return core.disconnect_player(player_name, reason) +end + function core.check_player_privs(name, ...) if core.is_player(name) then name = name:get_player_name() @@ -111,53 +121,6 @@ function core.get_player_radius_area(player_name, radius) end -function core.hash_node_position(pos) - return (pos.z + 32768) * 65536 * 65536 - + (pos.y + 32768) * 65536 - + pos.x + 32768 -end - - -function core.get_position_from_hash(hash) - local x = (hash % 65536) - 32768 - hash = math.floor(hash / 65536) - local y = (hash % 65536) - 32768 - hash = math.floor(hash / 65536) - local z = (hash % 65536) - 32768 - return vector.new(x, y, z) -end - - -function core.get_item_group(name, group) - if not core.registered_items[name] or not - core.registered_items[name].groups[group] then - return 0 - end - return core.registered_items[name].groups[group] -end - - -function core.get_node_group(name, group) - core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead") - return core.get_item_group(name, group) -end - - -function core.setting_get_pos(name) - local value = core.settings:get(name) - if not value then - return nil - end - return core.string_to_pos(value) -end - - --- See l_env.cpp for the other functions -function core.get_artificial_light(param1) - return math.floor(param1 / 16) -end - - -- To be overriden by protection mods function core.is_protected(pos, name) @@ -240,7 +203,7 @@ end -- HTTP callback interface -function core.http_add_fetch(httpenv) +core.set_http_api_lua(function(httpenv) httpenv.fetch = function(req, callback) local handle = httpenv.fetch_async(req) @@ -256,7 +219,8 @@ function core.http_add_fetch(httpenv) end return httpenv -end +end) +core.set_http_api_lua = nil function core.close_formspec(player_name, formname) @@ -273,40 +237,30 @@ end core.dynamic_media_callbacks = {} --- PNG encoder safety wrapper - -local o_encode_png = core.encode_png -function core.encode_png(width, height, data, compression) - if type(width) ~= "number" then - error("Incorrect type for 'width', expected number, got " .. type(width)) - end - if type(height) ~= "number" then - error("Incorrect type for 'height', expected number, got " .. type(height)) - end - - local expected_byte_count = width * height * 4 +-- Transfer of certain globals into async environment +-- see builtin/async/game.lua for the other side - if type(data) ~= "table" and type(data) ~= "string" then - error("Incorrect type for 'height', expected table or string, got " .. type(height)) +local function copy_filtering(t, seen) + if type(t) == "userdata" or type(t) == "function" then + return true -- don't use nil so presence can still be detected + elseif type(t) ~= "table" then + return t end - - local data_length = type(data) == "table" and #data * 4 or string.len(data) - - if data_length ~= expected_byte_count then - error(string.format( - "Incorrect length of 'data', width and height imply %d bytes but %d were provided", - expected_byte_count, - data_length - )) - end - - if type(data) == "table" then - local dataBuf = {} - for i = 1, #data do - dataBuf[i] = core.colorspec_to_bytes(data[i]) - end - data = table.concat(dataBuf) + local n = {} + seen = seen or {} + seen[t] = n + for k, v in pairs(t) do + local k_ = seen[k] or copy_filtering(k, seen) + local v_ = seen[v] or copy_filtering(v, seen) + n[k_] = v_ end + return n +end - return o_encode_png(width, height, data, compression or 6) +function core.get_globals_to_transfer() + local all = { + registered_items = copy_filtering(core.registered_items), + registered_aliases = core.registered_aliases, + } + return all end diff --git a/builtin/game/misc_s.lua b/builtin/game/misc_s.lua new file mode 100644 index 000000000..67a0ec684 --- /dev/null +++ b/builtin/game/misc_s.lua @@ -0,0 +1,93 @@ +-- Minetest: builtin/misc_s.lua +-- The distinction of what goes here is a bit tricky, basically it's everything +-- that does not (directly or indirectly) need access to ServerEnvironment, +-- Server or writable access to IGameDef on the engine side. +-- (The '_s' stands for standalone.) + +-- +-- Misc. API functions +-- + +function core.hash_node_position(pos) + return (pos.z + 32768) * 65536 * 65536 + + (pos.y + 32768) * 65536 + + pos.x + 32768 +end + + +function core.get_position_from_hash(hash) + local x = (hash % 65536) - 32768 + hash = math.floor(hash / 65536) + local y = (hash % 65536) - 32768 + hash = math.floor(hash / 65536) + local z = (hash % 65536) - 32768 + return vector.new(x, y, z) +end + + +function core.get_item_group(name, group) + if not core.registered_items[name] or not + core.registered_items[name].groups[group] then + return 0 + end + return core.registered_items[name].groups[group] +end + + +function core.get_node_group(name, group) + core.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead") + return core.get_item_group(name, group) +end + + +function core.setting_get_pos(name) + local value = core.settings:get(name) + if not value then + return nil + end + return core.string_to_pos(value) +end + + +-- See l_env.cpp for the other functions +function core.get_artificial_light(param1) + return math.floor(param1 / 16) +end + +-- PNG encoder safety wrapper + +local o_encode_png = core.encode_png +function core.encode_png(width, height, data, compression) + if type(width) ~= "number" then + error("Incorrect type for 'width', expected number, got " .. type(width)) + end + if type(height) ~= "number" then + error("Incorrect type for 'height', expected number, got " .. type(height)) + end + + local expected_byte_count = width * height * 4 + + if type(data) ~= "table" and type(data) ~= "string" then + error("Incorrect type for 'data', expected table or string, got " .. type(data)) + end + + local data_length = type(data) == "table" and #data * 4 or string.len(data) + + if data_length ~= expected_byte_count then + error(string.format( + "Incorrect length of 'data', width and height imply %d bytes but %d were provided", + expected_byte_count, + data_length + )) + end + + if type(data) == "table" then + local dataBuf = {} + for i = 1, #data do + dataBuf[i] = core.colorspec_to_bytes(data[i]) + end + data = table.concat(dataBuf) + end + + return o_encode_png(width, height, data, compression or 6) +end diff --git a/builtin/game/privileges.lua b/builtin/game/privileges.lua index 97681655e..2ff4c093c 100644 --- a/builtin/game/privileges.lua +++ b/builtin/game/privileges.lua @@ -97,10 +97,6 @@ core.register_privilege("rollback", { description = S("Can use the rollback functionality"), give_to_singleplayer = false, }) -core.register_privilege("basic_debug", { - description = S("Can view more debug info that might give a gameplay advantage"), - give_to_singleplayer = false, -}) core.register_privilege("debug", { description = S("Can enable wireframe"), give_to_singleplayer = false, diff --git a/builtin/game/register.lua b/builtin/game/register.lua index 56e40c75c..0be107c36 100644 --- a/builtin/game/register.lua +++ b/builtin/game/register.lua @@ -403,8 +403,14 @@ function core.override_item(name, redefinition) register_item_raw(item) end - -core.callback_origins = {} +do + local default = {mod = "??", name = "??"} + core.callback_origins = setmetatable({}, { + __index = function() + return default + end + }) +end function core.run_callbacks(callbacks, mode, ...) assert(type(callbacks) == "table") @@ -419,9 +425,7 @@ function core.run_callbacks(callbacks, mode, ...) local ret = nil for i = 1, cb_len do local origin = core.callback_origins[callbacks[i]] - if origin then - core.set_last_run_mod(origin.mod) - end + core.set_last_run_mod(origin.mod) local cb_ret = callbacks[i](...) if mode == 0 and i == 1 then diff --git a/builtin/game/statbars.lua b/builtin/game/statbars.lua index db5087a16..cb7ff7b76 100644 --- a/builtin/game/statbars.lua +++ b/builtin/game/statbars.lua @@ -1,39 +1,39 @@ -- cache setting local enable_damage = core.settings:get_bool("enable_damage") -local health_bar_definition = { - hud_elem_type = "statbar", - position = {x = 0.5, y = 1}, - text = "heart.png", - text2 = "heart_gone.png", - number = core.PLAYER_MAX_HP_DEFAULT, - item = core.PLAYER_MAX_HP_DEFAULT, - direction = 0, - size = {x = 24, y = 24}, - offset = {x = (-10 * 24) - 25, y = -(48 + 24 + 16)}, -} - -local breath_bar_definition = { - hud_elem_type = "statbar", - position = {x = 0.5, y = 1}, - text = "bubble.png", - text2 = "bubble_gone.png", - number = core.PLAYER_MAX_BREATH_DEFAULT, - item = core.PLAYER_MAX_BREATH_DEFAULT * 2, - direction = 0, - size = {x = 24, y = 24}, - offset = {x = 25, y= -(48 + 24 + 16)}, +local bar_definitions = { + hp = { + hud_elem_type = "statbar", + position = {x = 0.5, y = 1}, + text = "heart.png", + text2 = "heart_gone.png", + number = core.PLAYER_MAX_HP_DEFAULT, + item = core.PLAYER_MAX_HP_DEFAULT, + direction = 0, + size = {x = 24, y = 24}, + offset = {x = (-10 * 24) - 25, y = -(48 + 24 + 16)}, + }, + breath = { + hud_elem_type = "statbar", + position = {x = 0.5, y = 1}, + text = "bubble.png", + text2 = "bubble_gone.png", + number = core.PLAYER_MAX_BREATH_DEFAULT * 2, + item = core.PLAYER_MAX_BREATH_DEFAULT * 2, + direction = 0, + size = {x = 24, y = 24}, + offset = {x = 25, y= -(48 + 24 + 16)}, + }, } local hud_ids = {} -local function scaleToDefault(player, field) - -- Scale "hp" or "breath" to the default dimensions +local function scaleToHudMax(player, field) + -- Scale "hp" or "breath" to the hud maximum dimensions local current = player["get_" .. field](player) - local nominal = core["PLAYER_MAX_" .. field:upper() .. "_DEFAULT"] - local max_display = math.max(nominal, - math.max(player:get_properties()[field .. "_max"], current)) - return current / max_display * nominal + local nominal = bar_definitions[field].item + local max_display = math.max(player:get_properties()[field .. "_max"], current) + return math.ceil(current / max_display * nominal) end local function update_builtin_statbars(player) @@ -55,9 +55,9 @@ local function update_builtin_statbars(player) local immortal = player:get_armor_groups().immortal == 1 if flags.healthbar and enable_damage and not immortal then - local number = scaleToDefault(player, "hp") + local number = scaleToHudMax(player, "hp") if hud.id_healthbar == nil then - local hud_def = table.copy(health_bar_definition) + local hud_def = table.copy(bar_definitions.hp) hud_def.number = number hud.id_healthbar = player:hud_add(hud_def) else @@ -73,9 +73,9 @@ local function update_builtin_statbars(player) local breath = player:get_breath() local breath_max = player:get_properties().breath_max if show_breathbar and breath <= breath_max then - local number = 2 * scaleToDefault(player, "breath") + local number = scaleToHudMax(player, "breath") if not hud.id_breathbar and breath < breath_max then - local hud_def = table.copy(breath_bar_definition) + local hud_def = table.copy(bar_definitions.breath) hud_def.number = number hud.id_breathbar = player:hud_add(hud_def) elseif hud.id_breathbar then @@ -145,7 +145,7 @@ function core.hud_replace_builtin(hud_name, definition) end if hud_name == "health" then - health_bar_definition = definition + bar_definitions.hp = definition for name, ids in pairs(hud_ids) do local player = core.get_player_by_name(name) @@ -159,7 +159,7 @@ function core.hud_replace_builtin(hud_name, definition) end if hud_name == "breath" then - breath_bar_definition = definition + bar_definitions.breath = definition for name, ids in pairs(hud_ids) do local player = core.get_player_by_name(name) |
