WIP item sync: localize request

This commit is contained in:
IQuant 2024-05-13 16:19:28 +03:00
parent f134f48623
commit 488a828a18
4 changed files with 42 additions and 11 deletions

View file

@ -4,12 +4,8 @@ local util = dofile_once("mods/quant.ew/files/src/util.lua")
local item_sync = {}
local function mark_in_inventory(my_player)
local items = inventory_helper.get_all_inventory_items(my_player)
for _, ent in pairs(items) do
-- GamePrint(tostring(ent))
EntityAddTag(ent, "ew_was_in_inventory")
local notify = EntityGetFirstComponentIncludingDisabled(ent, "LuaComponent", "ew_notify_component")
function item_sync.ensure_notify_component(ent)
local notify = EntityGetFirstComponentIncludingDisabled(ent, "LuaComponent", "ew_notify_component")
if notify == nil then
-- GamePrint("Added lua component")
EntityAddComponent2(ent, "LuaComponent", {
@ -19,6 +15,14 @@ local function mark_in_inventory(my_player)
-- script_kick = "mods/quant.ew/files/cbs/item_notify.lua",
})
end
end
local function mark_in_inventory(my_player)
local items = inventory_helper.get_all_inventory_items(my_player)
for _, ent in pairs(items) do
-- GamePrint(tostring(ent))
-- EntityAddTag(ent, "ew_was_in_inventory")
item_sync.ensure_notify_component(ent)
end
end
@ -54,13 +58,12 @@ function item_sync.remove_item_with_id(g_id)
end
end
function item_sync.host_localize_item(item, peer_id)
local g_id = item_sync.get_global_item_id(item)
function item_sync.host_localize_item(gid, peer_id)
-- GamePrint("Localize "..g_id)
if peer_id ~= ctx.my_id then
item_sync.remove_item_with_id(g_id)
item_sync.remove_item_with_id(gid)
end
ctx.lib.net.send_localize(peer_id, g_id)
ctx.lib.net.send_localize(peer_id, gid)
end
function item_sync.make_item_global(item)
@ -100,7 +103,17 @@ function item_sync.host_upload_items(my_player)
local picked_item = get_global_ent("ew_picked")
if picked_item ~= nil and EntityHasTag(picked_item, "ew_global_item") then
GamePrint("Picked up "..picked_item)
item_sync.host_localize_item(picked_item, ctx.my_id)
local g_id = item_sync.get_global_item_id(picked_item)
item_sync.host_localize_item(g_id, ctx.my_id)
end
end
function item_sync.client_tick(my_player)
local picked_item = get_global_ent("ew_picked")
if picked_item ~= nil and EntityHasTag(picked_item, "ew_global_item") then
GamePrint("Picked up "..picked_item)
local gid = item_sync.get_global_item_id(picked_item)
ctx.lib.net.send_localize_request(gid)
end
end

View file

@ -138,4 +138,9 @@ function net.send_localize(peer_id, item_id)
net.send("item_localize", {peer_id, item_id}, true)
end
function net.send_localize_request(item_id)
GamePrint("sent localize req")
net.send("item_localize_req", item_id, true)
end
return net

View file

@ -151,6 +151,7 @@ function net_handling.mod.item_global(peer_id, item_data)
end
local item = inventory_helper.deserialize_single_item(item_data)
EntityAddTag(item, "ew_global_item")
item_sync.ensure_notify_component(item)
-- GamePrint("Got global item: "..item)
local g_id = EntityGetFirstComponentIncludingDisabled(item, "VariableStorageComponent", "ew_global_item_id")
if g_id == nil then
@ -172,4 +173,13 @@ function net_handling.mod.item_localize(peer_id, localize_data)
end
end
function net_handling.mod.item_localize_req(peer_id, gid)
if not ctx.is_host then
return
end
-- TODO check for race condition from several clients
GamePrint("localize req "..peer_id.." gid "..gid)
item_sync.host_localize_item(gid, peer_id)
end
return net_handling

View file

@ -156,8 +156,11 @@ local function on_world_pre_update_inner()
end
end
-- Item sync
if ctx.is_host then
item_sync.host_upload_items(my_player)
else
item_sync.client_tick(my_player)
end
-- Player sync