From 623dd73fe4347129e8eba3836168c72fc5d2df71 Mon Sep 17 00:00:00 2001 From: bgkillas Date: Sat, 23 Nov 2024 13:03:27 -0500 Subject: [PATCH] make throwing look slightly less ugly, fix an edge case that causes items to never appear until restarting --- quant.ew/files/core/inventory_helper.lua | 11 ++- quant.ew/files/core/util.lua | 2 - quant.ew/files/resource/cbs/throw_item.lua | 5 + quant.ew/files/system/item_sync.lua | 103 +++++++++++---------- 4 files changed, 68 insertions(+), 53 deletions(-) create mode 100644 quant.ew/files/resource/cbs/throw_item.lua diff --git a/quant.ew/files/core/inventory_helper.lua b/quant.ew/files/core/inventory_helper.lua index 9f008bd1..e446fb62 100644 --- a/quant.ew/files/core/inventory_helper.lua +++ b/quant.ew/files/core/inventory_helper.lua @@ -355,9 +355,6 @@ end function inventory_helper.set_item_data(item_data, player_data) local player = player_data.entity if player == nil or not EntityGetIsAlive(player) then - if player ~= nil then - GamePrint("Skip set_item_data, player ".. player_data.name .. " " .. player_data.entity .. " is dead") - end return end @@ -419,7 +416,13 @@ function inventory_helper.set_item_data(item_data, player_data) if (itemInfo.active) then active_item_entity = item_entity end - + EntityAddComponent(item_entity, "LuaComponent", { + script_throw_item = "mods/quant.ew/files/resource/cbs/throw_item.lua", + }) + local notify = EntityGetFirstComponentIncludingDisabled(item_entity, "LuaComponent", "ew_notify_component") + if notify ~= nil then + EntityRemoveComponent(item_entity, notify) + end --print("Deserialized wand #"..tostring(k).." - Active? "..tostring(wandInfo.active)) -- entity.SetVariable(item_entity, "arena_entity_id", itemInfo.id) diff --git a/quant.ew/files/core/util.lua b/quant.ew/files/core/util.lua index 41c3985b..db4e3945 100644 --- a/quant.ew/files/core/util.lua +++ b/quant.ew/files/core/util.lua @@ -401,7 +401,6 @@ function util.get_phys_info(entity, kill) if phys_component ~= nil and phys_component ~= 0 then local ret, info = pcall(serialize_phys_component, phys_component) if not ret and kill then - GamePrint("Physics component has no body, deleting entity") EntityKill(entity) return nil end @@ -415,7 +414,6 @@ function util.get_phys_info(entity, kill) if initialized then local ret, info = pcall(serialize_phys_component, phys_component) if not ret and kill then - GamePrint("Physics component has no body, deleting entity") EntityKill(entity) return nil end diff --git a/quant.ew/files/resource/cbs/throw_item.lua b/quant.ew/files/resource/cbs/throw_item.lua new file mode 100644 index 00000000..b8ed6579 --- /dev/null +++ b/quant.ew/files/resource/cbs/throw_item.lua @@ -0,0 +1,5 @@ +function throw_item() + local ent = GetUpdatedEntityID() + EntityRemoveComponent(ent, GetUpdatedComponentID()) + EntityKill(ent) +end \ No newline at end of file diff --git a/quant.ew/files/system/item_sync.lua b/quant.ew/files/system/item_sync.lua index 38facf40..640c814c 100644 --- a/quant.ew/files/system/item_sync.lua +++ b/quant.ew/files/system/item_sync.lua @@ -167,54 +167,56 @@ end local wait_for_gid = {} +local function make_global(item, give_authority_to) + if not EntityGetIsAlive(item) then + print("Thrown item vanished before we could send it") + return + end + item_sync.ensure_notify_component(item) + local gid_component = EntityGetFirstComponentIncludingDisabled(item, "VariableStorageComponent", + "ew_global_item_id") + local gid + if gid_component == nil then + gid = allocate_global_id() + if give_authority_to ~= nil then + gid = give_authority_to..":"..gid + end + EntityAddComponent2(item, "VariableStorageComponent", { + _tags = "enabled_in_world,enabled_in_hand,enabled_in_inventory,ew_global_item_id", + value_string = gid, + }) + else + gid = ComponentGetValue2(gid_component, "value_string") + end + --local vel = EntityGetFirstComponentIncludingDisabled(item, "VelocityComponent") + --if vel then + -- local vx, vy = ComponentGetValue2(vel, "mVelocity") + --end + + local item_data = inventory_helper.serialize_single_item(item) + item_data.gid = gid + + local _, _, 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 + function item_sync.make_item_global(item, instant, give_authority_to) EntityAddTag(item, "ew_global_item") - async(function() - if not instant then + if instant then + make_global(item, give_authority_to) + else + async(function() wait(1) -- Wait 1 frame so that game sets proper velocity. - end - if not EntityGetIsAlive(item) then - print("Thrown item vanished before we could send it") - return - end - item_sync.ensure_notify_component(item) - local gid_component = EntityGetFirstComponentIncludingDisabled(item, "VariableStorageComponent", - "ew_global_item_id") - local gid - if gid_component == nil then - gid = allocate_global_id() - if give_authority_to ~= nil then - gid = give_authority_to..":"..gid - end - EntityAddComponent2(item, "VariableStorageComponent", { - _tags = "enabled_in_world,enabled_in_hand,enabled_in_inventory,ew_global_item_id", - value_string = gid, - }) - else - gid = ComponentGetValue2(gid_component, "value_string") - end - --local vel = EntityGetFirstComponentIncludingDisabled(item, "VelocityComponent") - --if vel then - -- local vx, vy = ComponentGetValue2(vel, "mVelocity") - --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 - - if give_authority_to ~= nil then - local itemcom = EntityGetFirstComponentIncludingDisabled(item, "ItemComponent") - end - - ctx.item_prevent_localize[gid] = false - rpc.item_globalize(item_data) - end) + make_global(item, give_authority_to) + end) + end end local function get_global_ent(key) @@ -356,7 +358,7 @@ function item_sync.on_world_update_client() mark_in_inventory(my_player) end local thrown_item = get_global_ent("ew_thrown") - if thrown_item ~= nil and not EntityHasTag(thrown_item, "ew_client_item") then + if thrown_item ~= nil then item_sync.make_item_global(thrown_item) end @@ -399,6 +401,13 @@ function item_sync.on_world_update() elseif GameGetFrameNum() % 5 == 3 then send_item_positions(false) end + if GameGetFrameNum() % 30 == 23 then + for gid, num in pairs(wait_for_gid) do + if num < GameGetFrameNum() then + wait_for_gid[gid] = nil + end + end + end end function item_sync.on_should_send_updates() @@ -566,7 +575,7 @@ function rpc.update_positions(position_data, all) util.log("Requesting again "..gid) if wait_for_gid[gid] == nil then rpc.request_send_again(gid) - wait_for_gid[gid] = true + wait_for_gid[gid] = GameGetFrameNum() + 120 end end end