From e391b1552fd505e145a62feb383f37bb4b18634f Mon Sep 17 00:00:00 2001 From: bgkillas Date: Sun, 17 Nov 2024 22:09:57 -0500 Subject: [PATCH] sync items positions and stuffs --- .../files/resource/cbs/item_death_notify.lua | 3 + quant.ew/files/system/enemy_sync.lua | 2 +- quant.ew/files/system/item_sync.lua | 100 ++++++++++++++++-- 3 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 quant.ew/files/resource/cbs/item_death_notify.lua diff --git a/quant.ew/files/resource/cbs/item_death_notify.lua b/quant.ew/files/resource/cbs/item_death_notify.lua new file mode 100644 index 00000000..668e9d70 --- /dev/null +++ b/quant.ew/files/resource/cbs/item_death_notify.lua @@ -0,0 +1,3 @@ +function death( damage_type_bit_field, damage_message, entity_thats_responsible, drop_items ) + CrossCall("ew_item_death_notify", GetUpdatedEntityID(), entity_thats_responsible) +end \ No newline at end of file diff --git a/quant.ew/files/system/enemy_sync.lua b/quant.ew/files/system/enemy_sync.lua index 7403c087..e2a94bbf 100644 --- a/quant.ew/files/system/enemy_sync.lua +++ b/quant.ew/files/system/enemy_sync.lua @@ -164,13 +164,13 @@ function enemy_sync.host_upload_entities() if ai_component ~= 0 and ai_component ~= nil then ComponentSetValue2(ai_component, "max_distance_to_cam_to_start_hunting", math.pow(2, 29)) end - local hp, max_hp, has_hp = util.get_ent_health(enemy_id) local phys_info = util.get_phys_info(enemy_id, true) if phys_info == nil then goto continue end + local hp, max_hp, has_hp = util.get_ent_health(enemy_id) if has_hp then util.ensure_component_present(enemy_id, "LuaComponent", "ew_death_notify", { script_death = "mods/quant.ew/files/resource/cbs/death_notify.lua" diff --git a/quant.ew/files/system/item_sync.lua b/quant.ew/files/system/item_sync.lua index 8d13e05a..579d368c 100644 --- a/quant.ew/files/system/item_sync.lua +++ b/quant.ew/files/system/item_sync.lua @@ -12,6 +12,19 @@ local item_sync = {} local pending_remove = {} local pickup_handlers = {} +local dead_entities = {} + +util.add_cross_call("ew_item_death_notify", function(enemy_id, responsible_id) + local player_data = player_fns.get_player_data_by_local_entity_id(responsible_id) + local responsible + if player_data ~= nil then + responsible = player_data.peer_id + else + responsible = responsible_id + end + table.insert(dead_entities, {item_sync.get_global_item_id(enemy_id), responsible}) +end) + function item_sync.ensure_notify_component(ent) local notify = EntityGetFirstComponentIncludingDisabled(ent, "LuaComponent", "ew_notify_component") if notify == nil then @@ -146,7 +159,7 @@ function item_sync.make_item_global(item, instant, give_authority_to) end item_sync.ensure_notify_component(item) local gid_component = EntityGetFirstComponentIncludingDisabled(item, "VariableStorageComponent", - "ew_global_item_id") + "ew_global_item_id") local gid if gid_component == nil then gid = allocate_global_id() @@ -166,6 +179,13 @@ function item_sync.make_item_global(item, instant, give_authority_to) --end local item_data = inventory_helper.serialize_single_item(item) item_data.gid = gid + local hp, max_hp, has_hp = util.get_ent_health(item) + if has_hp then + util.ensure_component_present(item, "LuaComponent", "ew_item_death_notify", { + script_death = "mods/quant.ew/files/resource/cbs/item_death_notify.lua" + }) + end + ctx.item_prevent_localize[gid] = false rpc.item_globalize(item_data) end) @@ -194,6 +214,61 @@ local function is_my_item(gid) return string.sub(gid, 1, 16) == ctx.my_id end +rpc.opts_reliable() +function rpc.handle_death_data(death_data) + for _, remote_data in ipairs(death_data) do + local remote_id = remote_data[1] + --[[if confirmed_kills[remote_id] then + GamePrint("Remote id has been killed already..?") + goto continue + end + confirmed_kills[remote_id] = true]] + local responsible_entity = 0 + local peer_data = player_fns.peer_get_player_data(remote_data[2], true) + if peer_data ~= nil then + responsible_entity = peer_data.entity + elseif ctx.entity_by_remote_id[remote_data[2]] ~= nil then + responsible_entity = ctx.entity_by_remote_id[remote_data[2]] + end + + --if unsynced_enemys[remote_id] ~= nil then + --sync_enemy(unsynced_enemys[remote_id], true) + --end + local enemy_id = item_sync.find_by_gid(remote_id) + if enemy_id ~= nil and EntityGetIsAlive(enemy_id) then + local immortal = EntityGetFirstComponentIncludingDisabled(enemy_id, "LuaComponent", "ew_immortal") + if immortal ~= 0 then + EntityRemoveComponent(enemy_id, immortal) + end + local protection_component_id = GameGetGameEffect(enemy_id, "PROTECTION_ALL") + if protection_component_id ~= 0 then + EntitySetComponentIsEnabled(enemy_id, protection_component_id, false) + end + + local damage_component = EntityGetFirstComponentIncludingDisabled(enemy_id, "DamageModelComponent") + if damage_component and damage_component ~= 0 then + ComponentSetValue2(damage_component, "wait_for_kill_flag_on_death", false) + end + + -- Enable explosion back + local expl_component = EntityGetFirstComponent(enemy_id, "ExplodeOnDamageComponent") + if expl_component ~= nil and expl_component ~= 0 then + ComponentSetValue2(expl_component, "explode_on_death_percent", 1) + end + + local current_hp = util.get_ent_health(enemy_id) + local dmg = current_hp + if dmg > 0 then + EntityInflictDamage(enemy_id, dmg+0.1, "DAMAGE_CURSE", "", "NONE", 0, 0, responsible_entity) + end + + EntityInflictDamage(enemy_id, 1000000000, "DAMAGE_CURSE", "", "NONE", 0, 0, responsible_entity) -- Just to be sure + EntityKill(enemy_id) + end + ::continue:: + end +end + local function send_item_positions() local position_data = {} for _, item in ipairs(EntityGetWithTag("ew_global_item")) do @@ -210,10 +285,17 @@ local function send_item_positions() ComponentSetValue2(costcom, "stealable", true) end end - position_data[gid] = {x, y, cost} + local phys_info = util.get_phys_info(item, true) + if phys_info ~= nil then + position_data[gid] = {x, y, phys_info, cost} + end end end rpc.update_positions(position_data) + if #dead_entities > 0 then + rpc.handle_death_data(dead_entities) + end + dead_entities = {} end function item_sync.on_world_update_host() @@ -265,9 +347,6 @@ function item_sync.on_world_update() item_sync.remove_item_with_id_now(gid) end end - if GameGetFrameNum() % 60 == 31 then - send_item_positions() - end if GameGetFrameNum() % 5 == 2 then for _, ent in ipairs(EntityGetWithTag("mimic_potion")) do if ctx.is_host and not EntityHasTag(ent, "polymorphed_player") then @@ -277,6 +356,9 @@ function item_sync.on_world_update() end end end + if GameGetFrameNum() % 5 == 3 then + send_item_positions() + end end function item_sync.on_should_send_updates() @@ -366,6 +448,7 @@ function rpc.item_globalize(item_data) ComponentSetValue2(com, "value_int", GameGetFrameNum()) end end + EntityAddComponent2(item, "LuaComponent", {_tags="ew_immortal", script_damage_about_to_be_received = "mods/quant.ew/files/resource/cbs/immortal.lua"}) end rpc.opts_reliable() @@ -394,11 +477,14 @@ function rpc.update_positions(position_data) local cx, cy = GameGetCameraPos() for gid, el in pairs(position_data) do local x, y = el[1], el[2] - local price = el[3] + local phys_info = el[3] + local price = el[4] if math.abs(x - cx) < LIMIT and math.abs(y - cy) < LIMIT then local item = item_sync.find_by_gid(gid) if item ~= nil then - EntitySetTransform(item, x, y) + if not util.set_phys_info(item, phys_info) then + EntitySetTransform(item, x, y) + end local costcom = EntityGetFirstComponentIncludingDisabled(item, "ItemCostComponent") if costcom ~= nil then if price == 0 then