capturetheflag/mods/pvp/kill_assist/init.lua

91 lines
2.2 KiB
Lua
Raw Normal View History

kill_assist = {}
local kill_assists = {}
function kill_assist.clear_assists(player)
if type(player) == "string" then
kill_assists[player] = nil
else
kill_assists = {}
end
end
function kill_assist.add_assist(victim, attacker, damage)
2021-02-10 00:10:53 +00:00
if victim == attacker then return end
if not kill_assists[victim] then
2021-02-10 00:10:53 +00:00
kill_assists[victim] = {
players = {},
hp_offset = 0
}
end
2021-02-10 00:10:53 +00:00
kill_assists[victim].players[attacker] = (kill_assists[victim].players[attacker] or 0) + damage
end
function kill_assist.add_heal_assist(victim, healed_hp)
if not kill_assists[victim] then return end
2021-02-10 00:10:53 +00:00
kill_assists[victim].hp_offset = kill_assists[victim].hp_offset + healed_hp
end
function kill_assist.reward_assists(victim, killer, reward)
2021-02-11 03:14:12 +00:00
local max_hp = minetest.get_player_by_name(victim):get_properties().max_hp or 20
if not kill_assists[victim] then
2021-02-11 04:57:29 +00:00
if victim ~= killer then
kill_assist.add_assist(victim, killer, max_hp)
else
return
end
2021-02-11 03:14:12 +00:00
end
2021-02-10 00:10:53 +00:00
for name, damage in pairs(kill_assists[victim].players) do
2021-02-11 03:14:12 +00:00
if minetest.get_player_by_name(name) then
2021-02-10 00:10:53 +00:00
local help_percent = damage / (max_hp + kill_assists[victim].hp_offset)
local main, match = ctf_stats.player(name)
local color = "0x00FFFF"
2021-02-10 00:10:53 +00:00
if name == killer or help_percent >= 0.33 then
2021-02-10 03:15:36 +00:00
reward = math.max(math.floor((reward * help_percent)*100)/100, 1)
end
match.score = match.score + reward
main.score = main.score + reward
if name == killer then
color = "0x00FF00"
end
hud_score.new(name, {
name = "kill_assist:score",
color = color,
value = reward
})
end
end
ctf_stats.request_save()
kill_assist.clear_assists(victim)
end
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)
end)
ctf.register_on_punchplayer(function(player, hitter, _, _, _, damage)
kill_assist.add_assist(player:get_player_name(), hitter:get_player_name(), damage)
end)
ctf_match.register_on_new_match(function()
kill_assist.clear_assists()
end)
ctf.register_on_new_game(function()
kill_assist.clear_assists()
end)
minetest.register_on_leaveplayer(function(player)
kill_assist.clear_assists(player)
end)