From 84dc7da461c8c08481c072ad72631b5dd02c5414 Mon Sep 17 00:00:00 2001 From: savilli <78875209+savilli@users.noreply.github.com> Date: Fri, 3 Sep 2021 17:22:20 +0300 Subject: [PATCH] Rework melee damage (#878) --- mods/ctf/ctf/teams.lua | 46 ++++++++++++++++---------------- mods/ctf/ctf_classes/classes.lua | 2 +- mods/ctf/ctf_classes/init.lua | 13 +++++++++ mods/ctf/ctf_classes/melee.lua | 31 --------------------- mods/ctf/ctf_events/init.lua | 2 +- mods/ctf/ctf_metrics/init.lua | 2 +- mods/pvp/anticoward/init.lua | 4 +-- mods/pvp/kill_assist/init.lua | 2 +- mods/pvp/medkits/init.lua | 9 +++---- 9 files changed, 45 insertions(+), 66 deletions(-) diff --git a/mods/ctf/ctf/teams.lua b/mods/ctf/ctf/teams.lua index b62469e..a81efbd 100644 --- a/mods/ctf/ctf/teams.lua +++ b/mods/ctf/ctf/teams.lua @@ -450,6 +450,10 @@ function ctf.can_attack(player, hitter, time_from_last_punch, tool_capabilities, return true end +function ctf.get_damage_modifier(player, tool_capabilities) + return 0 +end + ctf.registered_on_attack = {} function ctf.register_on_attack(func) if ctf._mt_loaded then @@ -458,15 +462,8 @@ function ctf.register_on_attack(func) table.insert(ctf.registered_on_attack, func) end -local dead_players = {} -minetest.register_on_respawnplayer(function(player) - dead_players[player:get_player_name()] = nil -end) -minetest.register_on_joinplayer(function(player) - dead_players[player:get_player_name()] = nil -end) minetest.register_on_punchplayer(function(player, hitter, - time_from_last_punch, tool_capabilities, dir, damage, ...) + time_from_last_punch, tool_capabilities, dir, orig_damage, ...) if player and hitter then local pname = player:get_player_name() local hname = hitter:get_player_name() @@ -474,10 +471,6 @@ minetest.register_on_punchplayer(function(player, hitter, local to = ctf.player(pname) local from = ctf.player(hname) - if dead_players[pname] then - return - end - if to.team == from.team and to.team ~= "" and to.team ~= nil and to.name ~= from.name then hud_event.new(hname, { @@ -491,31 +484,38 @@ minetest.register_on_punchplayer(function(player, hitter, end if ctf.can_attack(player, hitter, time_from_last_punch, tool_capabilities, - dir, damage, ...) == false + dir, orig_damage, ...) == false then return true end local hp = player:get_hp() - if hp == 0 then - return false + if hp <= 0 then + return true end - if hp - damage <= 0 then - dead_players[pname] = true - local wielded = hitter:get_wielded_item() - for i = 1, #ctf.registered_on_killedplayer do - ctf.registered_on_killedplayer[i](pname, hname, - wielded, tool_capabilities) - end - return false + if tool_capabilities and tool_capabilities.damage_groups and tool_capabilities.damage_groups.fleshy then + local modifier = ctf.get_damage_modifier(hitter, tool_capabilities) + tool_capabilities.damage_groups.fleshy = math.max(1, tool_capabilities.damage_groups.fleshy + modifier) end + local damage = minetest.get_hit_params(player:get_armor_groups(), tool_capabilities, time_from_last_punch).hp + damage = math.min(damage, hp) + for i = 1, #ctf.registered_on_attack do ctf.registered_on_attack[i]( player, hitter, time_from_last_punch, tool_capabilities, dir, damage, ... ) end + + if hp <= damage then + for i = 1, #ctf.registered_on_killedplayer do + ctf.registered_on_killedplayer[i](pname, hname, tool_capabilities) + end + end + + player:set_hp(hp - damage) + return true end end) diff --git a/mods/ctf/ctf_classes/classes.lua b/mods/ctf/ctf_classes/classes.lua index bdbff8a..fbc69fb 100644 --- a/mods/ctf/ctf_classes/classes.lua +++ b/mods/ctf/ctf_classes/classes.lua @@ -8,7 +8,7 @@ ctf_classes.register("knight", { properties = { max_hp = 30, speed = 0.90, - melee_bonus = 1, + sword_modifier = 1, initial_stuff = { "ctf_classes:sword_bronze", diff --git a/mods/ctf/ctf_classes/init.lua b/mods/ctf/ctf_classes/init.lua index f126c32..ed946bf 100644 --- a/mods/ctf/ctf_classes/init.lua +++ b/mods/ctf/ctf_classes/init.lua @@ -69,3 +69,16 @@ ctf_classes.register_on_changed(function(player, old, new) ctf.chat_send_team(ctf.player(pname).team, minetest.colorize("#ABCDEF", pname .. " is now a " .. new.description)) end) + +local old_get_damage_modifier = ctf.get_damage_modifier +function ctf.get_damage_modifier(player, tool_capabilities) + local modifier = 0 + if tool_capabilities.damage_groups.sword then + local class = ctf_classes.get(player) + if class.properties.sword_modifier then + modifier = class.properties.sword_modifier + end + end + + return modifier + old_get_damage_modifier(player, tool_capabilities) +end diff --git a/mods/ctf/ctf_classes/melee.lua b/mods/ctf/ctf_classes/melee.lua index 8cd894c..1dba648 100644 --- a/mods/ctf/ctf_classes/melee.lua +++ b/mods/ctf/ctf_classes/melee.lua @@ -1,33 +1,3 @@ -minetest.register_on_player_hpchange(function(player, hp_change, reason) - if reason.type ~= "punch" or not reason.object or not reason.object:is_player() then - return hp_change - end - - local class = ctf_classes.get(reason.object) - - if class.properties.melee_bonus and reason.object:get_wielded_item():get_name():find("sword") then - local change = hp_change - class.properties.melee_bonus - - if player:get_hp() + change <= 0 and player:get_hp() + hp_change > 0 then - local wielded_item = reason.object:get_wielded_item() - - for i = 1, #ctf.registered_on_killedplayer do - ctf.registered_on_killedplayer[i]( - player:get_player_name(), - reason.object:get_player_name(), - wielded_item, - wielded_item:get_tool_capabilities() - ) - end - end - - return change - end - - return hp_change -end, true) - - local sword_special_timer = {} local SWORD_SPECIAL_COOLDOWN = 20 local function sword_special_timer_func(pname, timeleft) @@ -68,7 +38,6 @@ minetest.register_tool("ctf_classes:sword_bronze", { end local pteam = ctf.player(pname).team - if not pteam then -- can be nil during map change return end diff --git a/mods/ctf/ctf_events/init.lua b/mods/ctf/ctf_events/init.lua index 18bf340..b8b96fe 100644 --- a/mods/ctf/ctf_events/init.lua +++ b/mods/ctf/ctf_events/init.lua @@ -120,7 +120,7 @@ function ctf_events.update_all() end end -ctf.register_on_killedplayer(function(victim, killer, stack, tool_caps) +ctf.register_on_killedplayer(function(victim, killer, tool_caps) local victim_color = ctf_colors.get_color(ctf.player(victim)) local killer_color = ctf_colors.get_color(ctf.player(killer)) diff --git a/mods/ctf/ctf_metrics/init.lua b/mods/ctf/ctf_metrics/init.lua index 6210ea3..aa555bf 100644 --- a/mods/ctf/ctf_metrics/init.lua +++ b/mods/ctf/ctf_metrics/init.lua @@ -15,7 +15,7 @@ end -- Kills -- local kill_counter = counter("ctf_kills", "Total kills") -ctf.register_on_killedplayer(function(victim, killer, type) +ctf.register_on_killedplayer(function(victim, killer) kill_counter:increment() end) diff --git a/mods/pvp/anticoward/init.lua b/mods/pvp/anticoward/init.lua index de93a2a..9f851de 100644 --- a/mods/pvp/anticoward/init.lua +++ b/mods/pvp/anticoward/init.lua @@ -38,12 +38,11 @@ ctf.register_on_attack(function(player, hitter, potential_cowards[pname].timer = 0 potential_cowards[pname].puncher = hname - potential_cowards[pname].wielded_item = hitter:get_wielded_item() potential_cowards[pname].toolcaps = tool_capabilities end end) -ctf.register_on_killedplayer(function(victim, killer, _, toolcaps) +ctf.register_on_killedplayer(function(victim, killer, toolcaps) if toolcaps.damage_groups.combat_log or toolcaps.damage_groups.suicide then return end @@ -72,7 +71,6 @@ function handle_leave_or_die(pname, leave) ctf.registered_on_killedplayer[i]( pname, hname, - potential_cowards[pname].wielded_item, potential_cowards[pname].toolcaps ) end diff --git a/mods/pvp/kill_assist/init.lua b/mods/pvp/kill_assist/init.lua index b813500..5315586 100644 --- a/mods/pvp/kill_assist/init.lua +++ b/mods/pvp/kill_assist/init.lua @@ -69,7 +69,7 @@ function kill_assist.reward_assists(victim, killer, reward) kill_assist.clear_assists(victim) end -ctf.register_on_killedplayer(function(victim, killer, _, toolcaps) +ctf.register_on_killedplayer(function(victim, killer, toolcaps) local reward = ctf_stats.calculateKillReward(victim, killer, toolcaps) reward = math.floor(reward * 100) / 100 kill_assist.reward_assists(victim, killer, reward) diff --git a/mods/pvp/medkits/init.lua b/mods/pvp/medkits/init.lua index 5848a0b..9a71e87 100644 --- a/mods/pvp/medkits/init.lua +++ b/mods/pvp/medkits/init.lua @@ -133,22 +133,21 @@ end) -- If player takes damage while healing, -- stop regen and revert back to original state -minetest.register_on_player_hpchange(function(player, hp, reason) +minetest.register_on_player_hpchange(function(player, hp_change, reason) local name = player:get_player_name() - if hp < 0 then + if hp_change < 0 then if players[name] then player:hud_remove(players[name].hud) players[name] = nil -- Don't use stop_healing(), it uses set_hp() and won't allocate deaths or score properly end - if reason and reason.type == "punch" then + if reason.type == "punch" then local hitter = reason.object if hitter and players[hitter:get_player_name()] then stop_healing(hitter, "attack") end end end - return hp -end, true) +end) minetest.register_on_leaveplayer(function(player) players[player:get_player_name()] = nil