From 88fc515cff20edb16d627a7973f10575618678e6 Mon Sep 17 00:00:00 2001 From: AFCMS Date: Fri, 26 Mar 2021 17:38:25 +0100 Subject: [PATCH 1/3] make mobs take damage of falling anvils --- mods/ENTITIES/mcl_falling_nodes/init.lua | 71 +++++++++++++++++++++++- 1 file changed, 68 insertions(+), 3 deletions(-) diff --git a/mods/ENTITIES/mcl_falling_nodes/init.lua b/mods/ENTITIES/mcl_falling_nodes/init.lua index 1ffc87b34..8f72cc13d 100644 --- a/mods/ENTITIES/mcl_falling_nodes/init.lua +++ b/mods/ENTITIES/mcl_falling_nodes/init.lua @@ -13,9 +13,8 @@ local deal_falling_damage = function(self, dtime) if minetest.get_item_group(self.node.name, "falling_node_damage") == 0 then return end - -- Cause damage to any player it hits. + -- Cause damage to any entity it hits. -- Algorithm based on MC anvils. - -- TODO: Support smashing other objects, too. local pos = self.object:get_pos() if not self._startpos then -- Fallback @@ -23,6 +22,72 @@ local deal_falling_damage = function(self, dtime) end local objs = minetest.get_objects_inside_radius(pos, 1) for _,v in ipairs(objs) do + if v:is_player() then + local hp = v:get_hp() + local name = v:get_player_name() + if hp ~= 0 then + if not self._hit_players then + self._hit_players = {} + end + local hit = false + for _,v in ipairs(self._hit_players) do + if name == v then + hit = true + end + end + if not hit then + table.insert(self._hit_players, name) + local way = self._startpos.y - pos.y + local damage = (way - 1) * 2 + damage = math.min(40, math.max(0, damage)) + if damage >= 1 then + hp = hp - damage + if hp < 0 then + hp = 0 + end + -- TODO: Reduce damage if wearing a helmet + local msg + if minetest.get_item_group(self.node.name, "anvil") ~= 0 then + msg = S("@1 was smashed by a falling anvil.", v:get_player_name()) + else + msg = S("@1 was smashed by a falling block.", v:get_player_name()) + end + if dmes then + mcl_death_messages.player_damage(v, msg) + end + v:set_hp(hp, { type = "punch", from = "mod" }) + end + end + end + else + local hp = v:get_luaentity().health + if hp and hp ~= 0 then + if not self._hit_mobs then + self._hit_mobs = {} + end + local hit = false + for _,mob in ipairs(self._hit_mobs) do + if v == mob then + hit = true + end + end + if not hit then + table.insert(self._hit_mobs, v) + local way = self._startpos.y - pos.y + local damage = (way - 1) * 2 + damage = math.min(40, math.max(0, damage)) + if damage >= 1 then + hp = hp - damage + if hp < 0 then + hp = 0 + end + v:get_luaentity().health = hp + end + end + end + end + end +--[[ for _,v in ipairs(objs) do local hp = v:get_hp() if v:is_player() and hp ~= 0 then if not self._hit_players then @@ -61,7 +126,7 @@ local deal_falling_damage = function(self, dtime) end end end - end + end ]] end minetest.register_entity(":__builtin:falling_node", { From 041300cde4918b5b0b6ee9fec6f411c01fcd9ede Mon Sep 17 00:00:00 2001 From: AFCMS Date: Fri, 26 Mar 2021 17:39:25 +0100 Subject: [PATCH 2/3] remove unuseful code --- mods/ENTITIES/mcl_falling_nodes/init.lua | 40 ------------------------ 1 file changed, 40 deletions(-) diff --git a/mods/ENTITIES/mcl_falling_nodes/init.lua b/mods/ENTITIES/mcl_falling_nodes/init.lua index 8f72cc13d..f173cbade 100644 --- a/mods/ENTITIES/mcl_falling_nodes/init.lua +++ b/mods/ENTITIES/mcl_falling_nodes/init.lua @@ -87,46 +87,6 @@ local deal_falling_damage = function(self, dtime) end end end ---[[ for _,v in ipairs(objs) do - local hp = v:get_hp() - if v:is_player() and hp ~= 0 then - if not self._hit_players then - self._hit_players = {} - end - local name = v:get_player_name() - local hit = false - for _,v in ipairs(self._hit_players) do - if name == v then - hit = true - end - end - if not hit then - table.insert(self._hit_players, name) - local way = self._startpos.y - pos.y - local damage = (way - 1) * 2 - damage = math.min(40, math.max(0, damage)) - if damage >= 1 then - hp = hp - damage - if hp < 0 then - hp = 0 - end - if v:is_player() then - -- TODO: Reduce damage if wearing a helmet - local msg - if minetest.get_item_group(self.node.name, "anvil") ~= 0 then - msg = S("@1 was smashed by a falling anvil.", v:get_player_name()) - else - msg = S("@1 was smashed by a falling block.", v:get_player_name()) - end - if dmes then - mcl_death_messages.player_damage(v, msg) - end - end - v:set_hp(hp, { type = "punch", from = "mod" }) - end - end - end - end ]] end minetest.register_entity(":__builtin:falling_node", { From 8c002671c0e16f805788be2ea2966c4b6712bd2e Mon Sep 17 00:00:00 2001 From: AFCMS Date: Fri, 26 Mar 2021 18:24:08 +0100 Subject: [PATCH 3/3] make helmet protect from falling anvils --- mods/ENTITIES/mcl_falling_nodes/init.lua | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/mods/ENTITIES/mcl_falling_nodes/init.lua b/mods/ENTITIES/mcl_falling_nodes/init.lua index f173cbade..5b94373d9 100644 --- a/mods/ENTITIES/mcl_falling_nodes/init.lua +++ b/mods/ENTITIES/mcl_falling_nodes/init.lua @@ -1,5 +1,8 @@ local S = minetest.get_translator("mcl_falling_nodes") local dmes = minetest.get_modpath("mcl_death_messages") ~= nil +local has_mcl_armor = minetest.get_modpath("mcl_armor") + +local his_creative_enabled = minetest.is_creative_enabled local get_falling_depth = function(self) if not self._startpos then @@ -45,7 +48,16 @@ local deal_falling_damage = function(self, dtime) if hp < 0 then hp = 0 end - -- TODO: Reduce damage if wearing a helmet + -- Reduce damage if wearing a helmet + local inv = v:get_inventory() + local helmet = inv:get_stack("armor", 2) + if has_mcl_armor and not helmet:is_empty() then + hp = hp/4*3 + if not his_creative_enabled(name) then + helmet:add_wear(65535/helmet:get_definition().groups.mcl_armor_uses) --TODO: be sure damage is exactly like mc (informations are missing in the mc wiki) + inv:set_stack("armor", 2, helmet) + end + end local msg if minetest.get_item_group(self.node.name, "anvil") ~= 0 then msg = S("@1 was smashed by a falling anvil.", v:get_player_name()) @@ -71,6 +83,7 @@ local deal_falling_damage = function(self, dtime) hit = true end end + --TODO: reduce damage for mobs then they will be able to wear armor if not hit then table.insert(self._hit_mobs, v) local way = self._startpos.y - pos.y