diff options
Diffstat (limited to 'builtin/game')
-rw-r--r-- | builtin/game/chat.lua (renamed from builtin/game/chatcommands.lua) | 94 | ||||
-rw-r--r-- | builtin/game/falling.lua | 23 | ||||
-rw-r--r-- | builtin/game/features.lua | 3 | ||||
-rw-r--r-- | builtin/game/forceloading.lua | 35 | ||||
-rw-r--r-- | builtin/game/init.lua | 42 | ||||
-rw-r--r-- | builtin/game/item.lua | 15 | ||||
-rw-r--r-- | builtin/game/item_entity.lua | 91 | ||||
-rw-r--r-- | builtin/game/knockback.lua | 46 | ||||
-rw-r--r-- | builtin/game/misc.lua | 2 | ||||
-rw-r--r-- | builtin/game/privileges.lua | 2 | ||||
-rw-r--r-- | builtin/game/register.lua | 39 | ||||
-rw-r--r-- | builtin/game/statbars.lua | 29 |
12 files changed, 324 insertions, 97 deletions
diff --git a/builtin/game/chatcommands.lua b/builtin/game/chat.lua index 60d5d4788..ad703b94c 100644 --- a/builtin/game/chatcommands.lua +++ b/builtin/game/chat.lua @@ -1,4 +1,29 @@ --- Minetest: builtin/game/chatcommands.lua +-- Minetest: builtin/game/chat.lua + +-- +-- Chat message formatter +-- + +-- Implemented in Lua to allow redefinition +function core.format_chat_message(name, message) + local str = core.settings:get("chat_message_format") + local error_str = "Invalid chat message format - missing %s" + local i + + str, i = str:gsub("@name", name, 1) + if i == 0 then + error(error_str:format("@name"), 2) + end + + str, i = str:gsub("@message", message, 1) + if i == 0 then + error(error_str:format("@message"), 2) + end + + str = str:gsub("@timestamp", os.date("%H:%M:%S", os.time()), 1) + + return str +end -- -- Chat command handler @@ -27,9 +52,9 @@ core.register_on_chat_message(function(name, message) local has_privs, missing_privs = core.check_player_privs(name, cmd_def.privs) if has_privs then core.set_last_run_mod(cmd_def.mod_origin) - local success, message = cmd_def.func(name, param) - if message then - core.chat_send_player(name, message) + local _, result = cmd_def.func(name, param) + if result then + core.chat_send_player(name, result) end else core.chat_send_player(name, "You don't have permission" @@ -125,10 +150,10 @@ core.register_chatcommand("haspriv", { if core.check_player_privs(player_name, privs) then table.insert(players_with_priv, player_name) end - end + end return true, "Players online with the \"" .. param .. "\" privilege: " .. table.concat(players_with_priv, ", ") - end + end }) local function handle_grant_command(caller, grantname, grantprivstr) @@ -161,6 +186,7 @@ local function handle_grant_command(caller, grantname, grantprivstr) return false, privs_unknown end for priv, _ in pairs(grantprivs) do + -- call the on_grant callbacks core.run_priv_callbacks(grantname, priv, caller, "grant") end core.set_player_privs(grantname, privs) @@ -233,6 +259,7 @@ core.register_chatcommand("revoke", { end for priv, _ in pairs(revoke_privs) do + -- call the on_revoke callbacks core.run_priv_callbacks(revoke_name, priv, name, "revoke") end @@ -261,11 +288,12 @@ core.register_chatcommand("setpassword", { toname = param:match("^([^ ]+) *$") raw_password = nil end + if not toname then return false, "Name field required" end - local act_str_past = "?" - local act_str_pres = "?" + + local act_str_past, act_str_pres if not raw_password then core.set_player_password(toname, "") act_str_past = "cleared" @@ -277,13 +305,14 @@ core.register_chatcommand("setpassword", { act_str_past = "set" act_str_pres = "sets" end + if toname ~= name then core.chat_send_player(toname, "Your password was " .. act_str_past .. " by " .. name) end - core.log("action", name .. " " .. act_str_pres - .. " password of " .. toname .. ".") + core.log("action", name .. " " .. act_str_pres .. + " password of " .. toname .. ".") return true, "Password of player \"" .. toname .. "\" " .. act_str_past end, @@ -367,35 +396,35 @@ core.register_chatcommand("teleport", { return pos, false end - local teleportee = nil local p = {} p.x, p.y, p.z = string.match(param, "^([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$") p.x = tonumber(p.x) p.y = tonumber(p.y) p.z = tonumber(p.z) if p.x and p.y and p.z then + local lm = 31000 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, "Cannot teleport out of map bounds!" end - teleportee = core.get_player_by_name(name) + local teleportee = core.get_player_by_name(name) if teleportee then teleportee:set_pos(p) return true, "Teleporting to "..core.pos_to_string(p) end end - local teleportee = nil - local p = nil - local target_name = nil - target_name = param:match("^([^ ]+)$") - teleportee = core.get_player_by_name(name) + local target_name = param:match("^([^ ]+)$") + local teleportee = core.get_player_by_name(name) + + p = nil if target_name then local target = core.get_player_by_name(target_name) if target then p = target:get_pos() end end + if teleportee and p then p = find_free_position_near(p) teleportee:set_pos(p) @@ -407,9 +436,9 @@ core.register_chatcommand("teleport", { return false, "You don't have permission to teleport other players (missing bring privilege)" end - local teleportee = nil - local p = {} - local teleportee_name = nil + teleportee = nil + p = {} + local teleportee_name teleportee_name, p.x, p.y, p.z = param:match( "^([^ ]+) +([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$") p.x, p.y, p.z = tonumber(p.x), tonumber(p.y), tonumber(p.z) @@ -422,10 +451,8 @@ core.register_chatcommand("teleport", { .. " to " .. core.pos_to_string(p) end - local teleportee = nil - local p = nil - local teleportee_name = nil - local target_name = nil + teleportee = nil + p = nil teleportee_name, target_name = string.match(param, "^([^ ]+) +([^ ]+)$") if teleportee_name then teleportee = core.get_player_by_name(teleportee_name) @@ -459,7 +486,8 @@ core.register_chatcommand("set", { core.settings:set(setname, setvalue) return true, setname .. " = " .. setvalue end - local setname, setvalue = string.match(param, "([^ ]+) (.+)") + + setname, setvalue = string.match(param, "([^ ]+) (.+)") if setname and setvalue then if not core.settings:get(setname) then return false, "Failed. Use '/set -n <name> <value>' to create a new setting." @@ -467,14 +495,16 @@ core.register_chatcommand("set", { core.settings:set(setname, setvalue) return true, setname .. " = " .. setvalue end - local setname = string.match(param, "([^ ]+)") + + setname = string.match(param, "([^ ]+)") if setname then - local setvalue = core.settings:get(setname) + setvalue = core.settings:get(setname) if not setvalue then setvalue = "<not set>" end return true, setname .. " = " .. setvalue end + return false, "Invalid parameters (see /help set)." end, }) @@ -692,7 +722,7 @@ core.register_chatcommand("pulverize", { end core.log("action", name .. " pulverized \"" .. wielded_item:get_name() .. " " .. wielded_item:get_count() .. "\"") - player:set_wielded_item(nil) + player:set_wielded_item(nil) return true, "An item was pulverized." end, }) @@ -771,7 +801,7 @@ core.register_chatcommand("rollback", { end local target_name, seconds = string.match(param, ":([^ ]+) *(%d*)") if not target_name then - local player_name = nil + local player_name player_name, seconds = string.match(param, "([^ ]+) *(%d*)") if not player_name then return false, "Invalid parameters. See /help rollback" @@ -962,7 +992,7 @@ core.register_chatcommand("clearobjects", { core.register_chatcommand("msg", { params = "<name> <message>", - description = "Send a private message", + description = "Send a direct message to a player", privs = {shout=true}, func = function(name, param) local sendto, message = param:match("^(%S+)%s(.+)$") @@ -973,9 +1003,9 @@ core.register_chatcommand("msg", { return false, "The player " .. sendto .. " is not online." end - core.log("action", "PM from " .. name .. " to " .. sendto + core.log("action", "DM from " .. name .. " to " .. sendto .. ": " .. message) - core.chat_send_player(sendto, "PM from " .. name .. ": " + core.chat_send_player(sendto, "DM from " .. name .. ": " .. message) return true, "Message sent." end, diff --git a/builtin/game/falling.lua b/builtin/game/falling.lua index 62b6973a7..950d6b56f 100644 --- a/builtin/game/falling.lua +++ b/builtin/game/falling.lua @@ -22,7 +22,18 @@ core.register_entity(":__builtin:falling_node", { set_node = function(self, node, meta) self.node = node - self.meta = meta or {} + meta = meta or {} + if type(meta.to_table) == "function" then + meta = meta:to_table() + end + for _, list in pairs(meta.inventory or {}) do + for i, stack in pairs(list) do + if type(stack) == "userdata" then + list[i] = stack:to_string() + end + end + end + self.meta = meta self.object:set_properties({ is_visible = true, textures = {node.name}, @@ -116,7 +127,7 @@ core.register_entity(":__builtin:falling_node", { local meta = core.get_meta(np) meta:from_table(self.meta) end - if def.sounds and def.sounds.place and def.sounds.place.name then + if def.sounds and def.sounds.place then core.sound_play(def.sounds.place, {pos = np}) end end @@ -141,6 +152,11 @@ local function convert_to_falling_node(pos, node) local meta = core.get_meta(pos) local metatable = meta and meta:to_table() or {} + local def = core.registered_nodes[node.name] + if def and def.sounds and def.sounds.fall then + core.sound_play(def.sounds.fall, {pos = pos}) + end + obj:get_luaentity():set_node(node, metatable) core.remove_node(pos) return true @@ -170,6 +186,9 @@ local function drop_attached_node(p) drops = drop_stacks def.preserve_metadata(pos_copy, node_copy, oldmeta, drops) end + if def and def.sounds and def.sounds.fall then + core.sound_play(def.sounds.fall, {pos = p}) + end core.remove_node(p) for _, item in pairs(drops) do local pos = { diff --git a/builtin/game/features.lua b/builtin/game/features.lua index 8e5104867..0af0dc1da 100644 --- a/builtin/game/features.lua +++ b/builtin/game/features.lua @@ -12,6 +12,9 @@ core.features = { no_chat_message_prediction = true, object_use_texture_alpha = true, object_independent_selectionbox = true, + httpfetch_binary_data = true, + formspec_version_element = true, + area_store_persistent_ids = true, } function core.has_feature(arg) diff --git a/builtin/game/forceloading.lua b/builtin/game/forceloading.lua index 7c5537e85..e1e00920c 100644 --- a/builtin/game/forceloading.lua +++ b/builtin/game/forceloading.lua @@ -8,6 +8,9 @@ local blocks_forceloaded local blocks_temploaded = {} local total_forceloaded = 0 +-- true, if the forceloaded blocks got changed (flag for persistence on-disk) +local forceload_blocks_changed = false + local BLOCKSIZE = core.MAP_BLOCKSIZE local function get_blockpos(pos) return { @@ -31,6 +34,9 @@ local function get_relevant_tables(transient) end function core.forceload_block(pos, transient) + -- set changed flag + forceload_blocks_changed = true + local blockpos = get_blockpos(pos) local hash = core.hash_node_position(blockpos) local relevant_table, other_table = get_relevant_tables(transient) @@ -51,6 +57,9 @@ function core.forceload_block(pos, transient) end function core.forceload_free_block(pos, transient) + -- set changed flag + forceload_blocks_changed = true + local blockpos = get_blockpos(pos) local hash = core.hash_node_position(blockpos) local relevant_table, other_table = get_relevant_tables(transient) @@ -95,6 +104,28 @@ core.after(5, function() end end) -core.register_on_shutdown(function() +-- persists the currently forceloaded blocks to disk +local function persist_forceloaded_blocks() write_file(wpath.."/force_loaded.txt", blocks_forceloaded) -end) +end + +-- periodical forceload persistence +local function periodically_persist_forceloaded_blocks() + + -- only persist if the blocks actually changed + if forceload_blocks_changed then + persist_forceloaded_blocks() + + -- reset changed flag + forceload_blocks_changed = false + end + + -- recheck after some time + core.after(10, periodically_persist_forceloaded_blocks) +end + +-- persist periodically +core.after(5, periodically_persist_forceloaded_blocks) + +-- persist on shutdown +core.register_on_shutdown(persist_forceloaded_blocks) diff --git a/builtin/game/init.lua b/builtin/game/init.lua index ab1503dee..1d62be019 100644 --- a/builtin/game/init.lua +++ b/builtin/game/init.lua @@ -1,36 +1,38 @@ local scriptpath = core.get_builtin_path() -local commonpath = scriptpath.."common"..DIR_DELIM -local gamepath = scriptpath.."game"..DIR_DELIM +local commonpath = scriptpath .. "common" .. DIR_DELIM +local gamepath = scriptpath .. "game".. DIR_DELIM -- Shared between builtin files, but -- not exposed to outer context local builtin_shared = {} -dofile(commonpath.."vector.lua") +dofile(commonpath .. "vector.lua") -dofile(gamepath.."constants.lua") -assert(loadfile(gamepath.."item.lua"))(builtin_shared) -dofile(gamepath.."register.lua") +dofile(gamepath .. "constants.lua") +assert(loadfile(gamepath .. "item.lua"))(builtin_shared) +dofile(gamepath .. "register.lua") if core.settings:get_bool("profiler.load") then - profiler = dofile(scriptpath.."profiler"..DIR_DELIM.."init.lua") + profiler = dofile(scriptpath .. "profiler" .. DIR_DELIM .. "init.lua") end dofile(commonpath .. "after.lua") -dofile(gamepath.."item_entity.lua") -dofile(gamepath.."deprecated.lua") -dofile(gamepath.."misc.lua") -dofile(gamepath.."privileges.lua") -dofile(gamepath.."auth.lua") +dofile(gamepath .. "item_entity.lua") +dofile(gamepath .. "deprecated.lua") +dofile(gamepath .. "misc.lua") +dofile(gamepath .. "privileges.lua") +dofile(gamepath .. "auth.lua") dofile(commonpath .. "chatcommands.lua") -dofile(gamepath.."chatcommands.lua") -dofile(gamepath.."static_spawn.lua") -dofile(gamepath.."detached_inventory.lua") -assert(loadfile(gamepath.."falling.lua"))(builtin_shared) -dofile(gamepath.."features.lua") -dofile(gamepath.."voxelarea.lua") -dofile(gamepath.."forceloading.lua") -dofile(gamepath.."statbars.lua") +dofile(gamepath .. "chat.lua") +dofile(commonpath .. "information_formspecs.lua") +dofile(gamepath .. "static_spawn.lua") +dofile(gamepath .. "detached_inventory.lua") +assert(loadfile(gamepath .. "falling.lua"))(builtin_shared) +dofile(gamepath .. "features.lua") +dofile(gamepath .. "voxelarea.lua") +dofile(gamepath .. "forceloading.lua") +dofile(gamepath .. "statbars.lua") +dofile(gamepath .. "knockback.lua") profiler = nil diff --git a/builtin/game/item.lua b/builtin/game/item.lua index ced28771e..8041d557e 100644 --- a/builtin/game/item.lua +++ b/builtin/game/item.lua @@ -206,7 +206,6 @@ function core.get_node_drops(node, toolname) -- Extended drop table local got_items = {} local got_count = 0 - local _, item, tool for _, item in ipairs(drop.items) do local good_rarity = true local good_tool = true @@ -251,11 +250,6 @@ local function user_name(user) return user and user:get_player_name() or "" end -local function is_protected(pos, name) - return core.is_protected(pos, name) and - not minetest.check_player_privs(name, "protection_bypass") -end - -- Returns a logging function. For empty names, does not log. local function make_log(name) return name ~= "" and core.log or function() end @@ -302,7 +296,7 @@ function core.item_place_node(itemstack, placer, pointed_thing, param2, place_to = {x = under.x, y = under.y, z = under.z} end - if is_protected(place_to, playername) then + if core.is_protected(place_to, playername) then log("action", playername .. " tried to place " .. def.name .. " at protected position " @@ -552,7 +546,7 @@ function core.node_dig(pos, node, digger) return end - if is_protected(pos, diggername) then + if core.is_protected(pos, diggername) then log("action", diggername .. " tried to dig " .. node.name .. " at protected position " @@ -619,15 +613,10 @@ function core.node_dig(pos, node, digger) end -- Run script hook - local _, callback 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) - --print("Running " .. tostring(callback) .. - -- " (a " .. origin.name .. " callback in " .. origin.mod .. ")") - else - --print("No data associated with callback") end -- Copy pos and node because callback can modify them diff --git a/builtin/game/item_entity.lua b/builtin/game/item_entity.lua index a330e8723..87fec93ea 100644 --- a/builtin/game/item_entity.lua +++ b/builtin/game/item_entity.lua @@ -35,7 +35,12 @@ core.register_entity(":__builtin:item", { itemstring = "", moving_state = true, slippery_state = false, + physical_state = true, + -- Item expiry age = 0, + -- Pushing item out of solid nodes + force_out = nil, + force_out_start = nil, set_item = function(self, item) local stack = ItemStack(item or self.itemstring) @@ -131,6 +136,24 @@ core.register_entity(":__builtin:item", { return true end, + enable_physics = function(self) + if not self.physical_state then + self.physical_state = true + self.object:set_properties({physical = true}) + self.object:set_velocity({x=0, y=0, z=0}) + self.object:set_acceleration({x=0, y=-gravity, z=0}) + end + end, + + disable_physics = function(self) + if self.physical_state then + self.physical_state = false + self.object:set_properties({physical = false}) + self.object:set_velocity({x=0, y=0, z=0}) + self.object:set_acceleration({x=0, y=0, z=0}) + end + end, + on_step = function(self, dtime) self.age = self.age + dtime if time_to_live > 0 and self.age > time_to_live then @@ -152,6 +175,74 @@ core.register_entity(":__builtin:item", { return end + local is_stuck = false + local snode = core.get_node_or_nil(pos) + if snode then + local sdef = core.registered_nodes[snode.name] or {} + is_stuck = (sdef.walkable == nil or sdef.walkable == true) + and (sdef.collision_box == nil or sdef.collision_box.type == "regular") + and (sdef.node_box == nil or sdef.node_box.type == "regular") + end + + -- Push item out when stuck inside solid node + if is_stuck then + local shootdir + local order = { + {x=1, y=0, z=0}, {x=-1, y=0, z= 0}, + {x=0, y=0, z=1}, {x= 0, y=0, z=-1}, + } + + -- Check which one of the 4 sides is free + for o = 1, #order do + local cnode = core.get_node(vector.add(pos, order[o])).name + local cdef = core.registered_nodes[cnode] or {} + if cnode ~= "ignore" and cdef.walkable == false then + shootdir = order[o] + break + end + end + -- If none of the 4 sides is free, check upwards + if not shootdir then + shootdir = {x=0, y=1, z=0} + local cnode = core.get_node(vector.add(pos, shootdir)).name + if cnode == "ignore" then + shootdir = nil -- Do not push into ignore + end + end + + if shootdir then + -- Set new item moving speed accordingly + local newv = vector.multiply(shootdir, 3) + self:disable_physics() + self.object:set_velocity(newv) + + self.force_out = newv + self.force_out_start = vector.round(pos) + return + end + elseif self.force_out then + -- This code runs after the entity got a push from the above code. + -- It makes sure the entity is entirely outside the solid node + local c = self.object:get_properties().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 + (f.y > 0 and pos.y + c[2] > s.y + 0.5) or + (f.z > 0 and pos.z + c[3] > s.z + 0.5) or + (f.x < 0 and pos.x + c[4] < s.x - 0.5) or + (f.z < 0 and pos.z + c[6] < s.z - 0.5) + if ok then + -- Item was successfully forced out + self.force_out = nil + self:enable_physics() + end + end + + if not self.physical_state then + return -- Don't do anything + end + + -- Slide on slippery nodes local vel = self.object:get_velocity() local def = node and core.registered_nodes[node.name] local is_moving = (def and not def.walkable) or diff --git a/builtin/game/knockback.lua b/builtin/game/knockback.lua new file mode 100644 index 000000000..b5c4cbc5a --- /dev/null +++ b/builtin/game/knockback.lua @@ -0,0 +1,46 @@ +-- can be overriden by mods +function core.calculate_knockback(player, hitter, time_from_last_punch, tool_capabilities, dir, distance, damage) + if damage == 0 or player:get_armor_groups().immortal then + return 0.0 + end + + local m = 8 + -- solve m - m*e^(k*4) = 4 for k + local k = -0.17328 + local res = m - m * math.exp(k * damage) + + if distance < 2.0 then + res = res * 1.1 -- more knockback when closer + elseif distance > 4.0 then + res = res * 0.9 -- less when far away + end + return res +end + +local function vector_absmax(v) + local max, abs = math.max, math.abs + return max(max(abs(v.x), abs(v.y)), abs(v.z)) +end + +core.register_on_punchplayer(function(player, hitter, time_from_last_punch, tool_capabilities, unused_dir, damage) + if player:get_hp() == 0 then + return -- RIP + end + + -- Server::handleCommand_Interact() adds eye offset to one but not the other + -- so the direction is slightly off, calculate it ourselves + local dir = vector.subtract(player:get_pos(), hitter:get_pos()) + local d = vector.length(dir) + if d ~= 0.0 then + dir = vector.divide(dir, d) + end + + local k = core.calculate_knockback(player, hitter, time_from_last_punch, tool_capabilities, dir, d, damage) + + local kdir = vector.multiply(dir, k) + if vector_absmax(kdir) < 1.0 then + return -- barely noticeable, so don't even send + end + + player:add_player_velocity(kdir) +end) diff --git a/builtin/game/misc.lua b/builtin/game/misc.lua index e6d16dde7..02c36ccb1 100644 --- a/builtin/game/misc.lua +++ b/builtin/game/misc.lua @@ -62,7 +62,7 @@ end core.register_on_joinplayer(function(player) local player_name = player:get_player_name() player_list[player_name] = player - if not minetest.is_singleplayer() then + if not core.is_singleplayer() then local status = core.get_server_status(player_name, true) if status and status ~= "" then core.chat_send_player(player_name, status) diff --git a/builtin/game/privileges.lua b/builtin/game/privileges.lua index d77a481ac..c7417d2f4 100644 --- a/builtin/game/privileges.lua +++ b/builtin/game/privileges.lua @@ -18,7 +18,7 @@ function core.register_privilege(name, param) def.description = "(no description)" end end - local def = {} + local def if type(param) == "table" then def = param else diff --git a/builtin/game/register.lua b/builtin/game/register.lua index 3edab0471..bfad6845c 100644 --- a/builtin/game/register.lua +++ b/builtin/game/register.lua @@ -79,6 +79,7 @@ end function core.register_abm(spec) -- Add to core.registered_abms + assert(type(spec.action) == "function", "Required field 'action' of type function") core.registered_abms[#core.registered_abms + 1] = spec spec.mod_origin = core.get_current_modname() or "??" end @@ -86,6 +87,7 @@ end function core.register_lbm(spec) -- Add to core.registered_lbms check_modname_prefix(spec.name) + assert(type(spec.action) == "function", "Required field 'action' of type function") core.registered_lbms[#core.registered_lbms + 1] = spec spec.mod_origin = core.get_current_modname() or "??" end @@ -254,6 +256,18 @@ function core.register_tool(name, tooldef) end -- END Legacy stuff + -- This isn't just legacy, but more of a convenience feature + local toolcaps = tooldef.tool_capabilities + if toolcaps and toolcaps.punch_attack_uses == nil then + for _, cap in pairs(toolcaps.groupcaps or {}) do + local level = (cap.maxlevel or 0) - 1 + if (cap.uses or 0) ~= 0 and level >= 0 then + toolcaps.punch_attack_uses = cap.uses * (3 ^ level) + break + end + end + end + core.register_item(name, tooldef) end @@ -301,7 +315,6 @@ end -- Alias the forbidden item names to "" so they can't be -- created via itemstrings (e.g. /give) -local name for name in pairs(forbidden_item_names) do core.registered_aliases[name] = "" register_alias_raw(name, "") @@ -361,9 +374,9 @@ core.register_node(":ignore", { drop = "", groups = {not_in_creative_inventory=1}, on_place = function(itemstack, placer, pointed_thing) - minetest.chat_send_player( + core.chat_send_player( placer:get_player_name(), - minetest.colorize("#FF0000", + core.colorize("#FF0000", "You can't place 'ignore' nodes!")) return "" end, @@ -372,6 +385,7 @@ core.register_node(":ignore", { -- The hand (bare definition) core.register_item(":", { type = "none", + wield_image = "wieldhand.png", groups = {not_in_creative_inventory=1}, }) @@ -411,10 +425,6 @@ function core.run_callbacks(callbacks, mode, ...) local origin = core.callback_origins[callbacks[i]] if origin then core.set_last_run_mod(origin.mod) - --print("Running " .. tostring(callbacks[i]) .. - -- " (a " .. origin.name .. " callback in " .. origin.mod .. ")") - else - --print("No data associated with callback") end local cb_ret = callbacks[i](...) @@ -514,11 +524,17 @@ local function make_registration_wrap(reg_fn_name, clear_fn_name) end local function make_wrap_deregistration(reg_fn, clear_fn, list) - local unregister = function (unregistered_key) + local unregister = function (key) + if type(key) ~= "string" then + error("key is not a string", 2) + end + if not list[key] then + error("Attempt to unregister non-existent element - '" .. key .. "'", 2) + end local temporary_list = table.copy(list) clear_fn() for k,v in pairs(temporary_list) do - if unregistered_key ~= k then + if key ~= k then reg_fn(v) end end @@ -529,7 +545,7 @@ end core.registered_on_player_hpchanges = { modifiers = { }, loggers = { } } function core.registered_on_player_hpchange(player, hp_change, reason) - local last = false + local last for i = #core.registered_on_player_hpchanges.modifiers, 1, -1 do local func = core.registered_on_player_hpchanges.modifiers[i] hp_change, last = func(player, hp_change, reason) @@ -564,7 +580,8 @@ core.registered_biomes = make_registration_wrap("register_biome", "cle core.registered_ores = make_registration_wrap("register_ore", "clear_registered_ores") core.registered_decorations = make_registration_wrap("register_decoration", "clear_registered_decorations") -core.unregister_biome = make_wrap_deregistration(core.register_biome, core.clear_registered_biomes, core.registered_biomes) +core.unregister_biome = make_wrap_deregistration(core.register_biome, + core.clear_registered_biomes, core.registered_biomes) core.registered_on_chat_messages, core.register_on_chat_message = make_registration() core.registered_globalsteps, core.register_globalstep = make_registration() diff --git a/builtin/game/statbars.lua b/builtin/game/statbars.lua index da924d6f8..46c947b60 100644 --- a/builtin/game/statbars.lua +++ b/builtin/game/statbars.lua @@ -1,8 +1,7 @@ -- cache setting local enable_damage = core.settings:get_bool("enable_damage") -local health_bar_definition = -{ +local health_bar_definition = { hud_elem_type = "statbar", position = { x=0.5, y=1 }, text = "heart.png", @@ -12,8 +11,7 @@ local health_bar_definition = offset = { x=(-10*24)-25, y=-(48+24+16)}, } -local breath_bar_definition = -{ +local breath_bar_definition = { hud_elem_type = "statbar", position = { x=0.5, y=1 }, text = "bubble.png", @@ -30,8 +28,8 @@ local function scaleToDefault(player, field) 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 + math.max(player:get_properties()[field .. "_max"], current)) + return current / max_display * nominal end local function update_builtin_statbars(player) @@ -50,10 +48,11 @@ local function update_builtin_statbars(player) end local hud = hud_ids[name] - if flags.healthbar and enable_damage then + local immortal = player:get_armor_groups().immortal == 1 + if flags.healthbar and enable_damage and not immortal then local number = scaleToDefault(player, "hp") - if hud.id_healthbar == nil then - local hud_def = table.copy(health_bar_definition) + if hud.id_healthbar == nil then + local hud_def = table.copy(health_bar_definition) hud_def.number = number hud.id_healthbar = player:hud_add(hud_def) else @@ -65,11 +64,11 @@ local function update_builtin_statbars(player) end local breath_max = player:get_properties().breath_max - if flags.breathbar and enable_damage and + if flags.breathbar and enable_damage and not immortal and player:get_breath() < breath_max then local number = 2 * scaleToDefault(player, "breath") if hud.id_breathbar == nil then - local hud_def = table.copy(breath_bar_definition) + local hud_def = table.copy(breath_bar_definition) hud_def.number = number hud.id_breathbar = player:hud_add(hud_def) else @@ -116,7 +115,7 @@ local function player_event_handler(player,eventname) end end - if eventname == "hud_changed" then + if eventname == "hud_changed" or eventname == "properties_changed" then update_builtin_statbars(player) return true end @@ -124,14 +123,14 @@ local function player_event_handler(player,eventname) return false end -function core.hud_replace_builtin(name, definition) +function core.hud_replace_builtin(hud_name, definition) if type(definition) ~= "table" or definition.hud_elem_type ~= "statbar" then return false end - if name == "health" then + if hud_name == "health" then health_bar_definition = definition for name, ids in pairs(hud_ids) do @@ -145,7 +144,7 @@ function core.hud_replace_builtin(name, definition) return true end - if name == "breath" then + if hud_name == "breath" then breath_bar_definition = definition for name, ids in pairs(hud_ids) do |