From fd4a79c252fca67b5a89c770a43d82118b61ed69 Mon Sep 17 00:00:00 2001 From: IQuant Date: Sun, 10 Nov 2024 18:21:19 +0300 Subject: [PATCH] Track death messages --- quant.ew/files/core/player_fns.lua | 3 ++ .../local_health/grab_damage_message.lua | 7 +++ .../system/local_health/local_health.lua | 46 +++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 quant.ew/files/system/local_health/grab_damage_message.lua diff --git a/quant.ew/files/core/player_fns.lua b/quant.ew/files/core/player_fns.lua index 1bf1f764..c1863129 100644 --- a/quant.ew/files/core/player_fns.lua +++ b/quant.ew/files/core/player_fns.lua @@ -476,6 +476,9 @@ function player_fns.nickname_of_peer(peer_id) end function player_fns.get_player_data_by_local_entity_id(entity) + if entity == nil then + return nil + end return ctx.player_data_by_local_entity[entity] end diff --git a/quant.ew/files/system/local_health/grab_damage_message.lua b/quant.ew/files/system/local_health/grab_damage_message.lua new file mode 100644 index 00000000..3cc252c8 --- /dev/null +++ b/quant.ew/files/system/local_health/grab_damage_message.lua @@ -0,0 +1,7 @@ +local adjust_damage = dofile_once("mods/quant.ew/files/system/damage/cbs/adjust_damage.lua").adjust_damage + +-- Called on clients when they get damage and redirects it to the host's hp over the network, ignoring any resists. +function damage_received(damage, message, entity_thats_responsible, is_fatal, projectile_thats_responsible) + -- Damage the host + CrossCall("ew_damage_message", message, entity_thats_responsible) +end diff --git a/quant.ew/files/system/local_health/local_health.lua b/quant.ew/files/system/local_health/local_health.lua index 1b3ff6dd..41310310 100644 --- a/quant.ew/files/system/local_health/local_health.lua +++ b/quant.ew/files/system/local_health/local_health.lua @@ -18,6 +18,12 @@ local status_effects = status_effects local module = {} +local last_damage_info = {0, "unknown", 1} + +np.CrossCallAdd("ew_damage_message", function(message, entity_thats_responsible) + last_damage_info = {GameGetFrameNum(), message, entity_thats_responsible} +end) + function module.on_player_died(player_entity) -- This would be a good place to put on death logic -- BUT... player entity is already dead at this point, so it's a bit problematic to do stuff. @@ -163,6 +169,39 @@ local function allow_notplayer_perk(perk_id) return not ignored_perks[perk_id] end +rpc.opts_everywhere() +rpc.opts_reliable() +function rpc.show_death_message(untranslated_message, source_player) + local message = "unknown" + if untranslated_message ~= nil then + message = GameTextGetTranslatedOrNot(untranslated_message) + end + if source_player ~= nil then + message = message .. " from "..source_player + end + + local dead_nickname = ctx.rpc_player_data.name + local full_msg = dead_nickname .. " died: " .. message + GamePrint(full_msg) +end + +local function show_death_message() + local current_frame = GameGetFrameNum() + -- Check if message is recent enough + if current_frame - last_damage_info[1] < 60 then + local message = last_damage_info[2] + local source = nil + local source_ent = last_damage_info[3] + local maybe_player = player_fns.get_player_data_by_local_entity_id(source_ent) + if maybe_player ~= nil then + source = maybe_player.name + end + rpc.show_death_message(message, source) + else + rpc.show_death_message(nil, nil) + end +end + local function player_died() if ctx.my_player.entity == nil then return @@ -171,6 +210,9 @@ local function player_died() util.log("Err: Current player is world state like.") return end + + show_death_message() + rpc.remove_homing(false) -- Serialize inventory, perks, and max_hp, we'll need to copy it over to notplayer. local item_data = inventory_helper.get_item_data(ctx.my_player) @@ -251,6 +293,10 @@ function module.on_local_player_spawn(my_player) local damage_model = EntityGetFirstComponentIncludingDisabled(my_player.entity, "DamageModelComponent") ComponentSetValue2(damage_model, "wait_for_kill_flag_on_death", true) ctx.my_player.status = { is_alive = true } + + util.ensure_component_present(my_player.entity, "LuaComponent", "ew_player_damage", { + script_damage_received = "mods/quant.ew/files/system/local_health/grab_damage_message.lua" + }) end function module.on_world_update()