From edb4a4ccde7f9e6e4c96fbe8e49d8af9c1f6d792 Mon Sep 17 00:00:00 2001 From: bgkillas Date: Mon, 18 Nov 2024 07:18:35 -0500 Subject: [PATCH] sync chests --- quant.ew/files/resource/cbs/chest_sync.lua | 12 +++++ quant.ew/files/system/item_sync.lua | 44 +++++++++++++++++++ .../files/system/spawn_hooks/spawn_hooks.lua | 2 - 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 quant.ew/files/resource/cbs/chest_sync.lua diff --git a/quant.ew/files/resource/cbs/chest_sync.lua b/quant.ew/files/resource/cbs/chest_sync.lua new file mode 100644 index 00000000..0e910db0 --- /dev/null +++ b/quant.ew/files/resource/cbs/chest_sync.lua @@ -0,0 +1,12 @@ +local old = on_open +function on_open(entity_item, dont) + if dont == nil then + CrossCall("ew_chest_opened", entity_item) + end + old(entity_item) +end + +function init(entity_id) + on_open(entity_id, true) + EntityKill(entity_id) +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 8adc2314..112d1b0b 100644 --- a/quant.ew/files/system/item_sync.lua +++ b/quant.ew/files/system/item_sync.lua @@ -3,6 +3,10 @@ local inventory_helper = dofile_once("mods/quant.ew/files/core/inventory_helper. local ctx = dofile_once("mods/quant.ew/files/core/ctx.lua") local net = dofile_once("mods/quant.ew/files/core/net.lua") +ModLuaFileAppend("data/scripts/items/utility_box.lua", "mods/quant.ew/files/resource/cbs/chest_sync.lua") +ModLuaFileAppend("data/scripts/items/chest_random.lua", "mods/quant.ew/files/resource/cbs/chest_sync.lua") +ModLuaFileAppend("data/scripts/items/chest_random_super.lua", "mods/quant.ew/files/resource/cbs/chest_sync.lua") + dofile_once("data/scripts/lib/coroutines.lua") local rpc = net.new_rpc_namespace() @@ -14,6 +18,37 @@ local pickup_handlers = {} local dead_entities = {} +function rpc.open_chest(gid) + local ent = item_sync.find_by_gid(gid) + if ent ~= nil then + local name = EntityGetFilename(ent) + local file + if name == "data/entities/items/pickup/utility_box.xml" then + file = "data/scripts/items/utility_box.lua" + end + if name == "data/entities/items/pickup/chest_random_super.xml" then + file = "data/scripts/items/chest_random_super.lua" + end + if name == "data/entities/items/pickup/chest_random.xml" then + file = "data/scripts/items/chest_random.lua" + end + if file ~= nil then + EntityAddComponent2(ent, "LuaComponent", { + script_source_file = file, + execute_on_added = true, + call_init_function = true, + }) + end + end +end + +util.add_cross_call("ew_chest_opened", function(chest_id) + local gid = item_sync.get_global_item_id(chest_id) + if gid ~= "unknown" then + rpc.open_chest(gid) + end +end) + 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 @@ -177,8 +212,10 @@ function item_sync.make_item_global(item, instant, give_authority_to) --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", { @@ -186,6 +223,13 @@ function item_sync.make_item_global(item, instant, give_authority_to) }) end + local name = EntityGetName(item) + if name == "$item_utility_box" + or name == "$item_chest_treasure" + or name == "$item_chest_treasure_super" then + + end + ctx.item_prevent_localize[gid] = false rpc.item_globalize(item_data) end) diff --git a/quant.ew/files/system/spawn_hooks/spawn_hooks.lua b/quant.ew/files/system/spawn_hooks/spawn_hooks.lua index 63efa827..63fa3574 100644 --- a/quant.ew/files/system/spawn_hooks/spawn_hooks.lua +++ b/quant.ew/files/system/spawn_hooks/spawn_hooks.lua @@ -17,8 +17,6 @@ exclude["data/entities/items/pickup/heart_evil.xml"] = true exclude["data/entities/items/pickup/heart_fullhp.xml"] = true exclude["data/entities/items/pickup/heart_fullhp_temple.xml"] = true exclude["data/entities/items/pickup/perk_reroll.xml"] = true -exclude["data/entities/items/pickup/chest_random.xml"] = true -exclude["data/entities/items/pickup/chest_random_super.xml"] = true -- This entity needs to be synced by item_sync local function is_sync_item(ent_path)