WIP item sync: localize

This commit is contained in:
IQuant 2024-05-13 15:18:52 +03:00
parent 70de9a26df
commit f134f48623
6 changed files with 89 additions and 18 deletions

View file

@ -1,10 +1,10 @@
function throw_item()
GamePrint("Item thrown")
-- GamePrint("Item thrown")
GlobalsSetValue("ew_thrown", tostring(GetUpdatedEntityID()))
end
function item_pickup()
GamePrint("Item pickup")
-- GamePrint("Item pickup")
GlobalsSetValue("ew_picked", tostring(GetUpdatedEntityID()))
end

View file

@ -69,7 +69,8 @@ function inventory_helper.deserialize_single_item(item_data)
local item = nil
local x, y = item_data[3], item_data[4]
if item_data[1] then
item = EZWand(item_data[2], x, y, false)
item = EZWand(item_data[2], x, y, false).entity_id
-- EntityAddTag(item, "does_physics_update")
else
item = EntityCreateNew()
np.DeserializeEntity(item, item_data[2], x, y)
@ -207,6 +208,8 @@ function inventory_helper.set_item_data(item_data, player_data)
return
end
EntityRemoveTag(item, "ew_global_item")
if(itemInfo.is_wand)then
item:PickUp(player)
local itemComp = EntityGetFirstComponentIncludingDisabled(item.entity_id, "ItemComponent")

View file

@ -9,9 +9,9 @@ local function mark_in_inventory(my_player)
for _, ent in pairs(items) do
-- GamePrint(tostring(ent))
EntityAddTag(ent, "ew_was_in_inventory")
local notify = EntityGetComponentIncludingDisabled(ent, "LuaComponent", "ew_notify_component")
local notify = EntityGetFirstComponentIncludingDisabled(ent, "LuaComponent", "ew_notify_component")
if notify == nil then
GamePrint("Added lua component")
-- GamePrint("Added lua component")
EntityAddComponent2(ent, "LuaComponent", {
_tags = "enabled_in_world,enabled_in_hand,enabled_in_inventory,ew_notify_component",
script_throw_item = "mods/quant.ew/files/cbs/item_notify.lua",
@ -22,12 +22,61 @@ local function mark_in_inventory(my_player)
end
end
local function allocate_global_id()
local current = tonumber(GlobalsGetValue("ew_global_item_id", "1"))
GlobalsSetValue("ew_global_item_id", tostring(current+1))
return current
end
local function is_item_on_ground(item)
return EntityGetComponent(item, "SimplePhysicsComponent") ~= nil or EntityGetComponent(item, "PhysicsBodyComponent")
end
function item_sync.get_global_item_id(item)
local g_id = EntityGetFirstComponentIncludingDisabled(item, "VariableStorageComponent", "ew_global_item_id")
if g_id == nil then
GamePrint("Item has no g_id")
return 0
end
local ret = ComponentGetValue2(g_id, "value_int")
return ret or 0
end
function item_sync.remove_item_with_id(g_id)
-- GamePrint("Localize, Remove "..g_id)
local global_items = EntityGetWithTag("ew_global_item")
for _, item in ipairs(global_items) do
local i_g_id = item_sync.get_global_item_id(item)
-- GamePrint("Chk "..item.." g_id "..i_g_id)
if i_g_id == g_id then
EntityKill(item)
end
end
end
function item_sync.host_localize_item(item, peer_id)
local g_id = item_sync.get_global_item_id(item)
-- GamePrint("Localize "..g_id)
if peer_id ~= ctx.my_id then
item_sync.remove_item_with_id(g_id)
end
ctx.lib.net.send_localize(peer_id, g_id)
end
function item_sync.make_item_global(item)
local g_id = EntityGetFirstComponentIncludingDisabled(item, "VariableStorageComponent", "ew_global_item_id")
local id = ComponentGetValue2(g_id, "value_int")
if g_id == nil then
id = allocate_global_id()
GamePrint("Allocating id "..id.." for "..item)
EntityAddComponent2(item, "VariableStorageComponent", {
_tags = "enabled_in_world,enabled_in_hand,enabled_in_inventory,ew_global_item_id",
value_int = id,
})
end
GamePrint(item_sync.get_global_item_id(item))
local item_data = inventory_helper.serialize_single_item(item)
item_data.g_id = id
ctx.lib.net.send_make_global(item_data)
end
@ -42,23 +91,17 @@ end
function item_sync.host_upload_items(my_player)
if GameGetFrameNum() % 5 == 4 then
mark_in_inventory(my_player)
-- local x, y = EntityGetTransform(my_player.entity)
-- local ents = EntityGetInRadiusWithTag(x, y, 300, "ew_was_in_inventory")
-- for _, ent in pairs(ents) do
-- if is_item_on_ground(ent) then
-- if not EntityHasTag(ent, "ew_global_item") then
-- EntityAddTag(ent, "ew_global_item")
-- GamePrint(tostring(ent).." "..EntityGetTags(ent))
-- item_sync.make_item_global(ent)
-- end
-- end
-- end
end
local thrown_item = get_global_ent("ew_thrown")
if thrown_item ~= nil then
EntityAddTag(thrown_item, "ew_global_item")
item_sync.make_item_global(thrown_item)
end
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)
end
end
return item_sync

View file

@ -134,4 +134,8 @@ function net.send_make_global(item_data)
net.send("item_global", item_data, true)
end
function net.send_localize(peer_id, item_id)
net.send("item_localize", {peer_id, item_id}, true)
end
return net

View file

@ -5,6 +5,7 @@ local enemy_sync = dofile_once("mods/quant.ew/files/src/enemy_sync.lua")
local world_sync = dofile_once("mods/quant.ew/files/src/world_sync.lua")
local perk_fns = dofile_once("mods/quant.ew/files/src/perk_fns.lua")
local inventory_helper = dofile_once("mods/quant.ew/files/src/inventory_helper.lua")
local item_sync = dofile_once("mods/quant.ew/files/src/item_sync.lua")
local np = require("noitapatcher")
@ -148,7 +149,27 @@ function net_handling.mod.item_global(peer_id, item_data)
if peer_id ~= ctx.host_id then
return
end
inventory_helper.deserialize_single_item(item_data)
local item = inventory_helper.deserialize_single_item(item_data)
EntityAddTag(item, "ew_global_item")
-- GamePrint("Got global item: "..item)
local g_id = EntityGetFirstComponentIncludingDisabled(item, "VariableStorageComponent", "ew_global_item_id")
if g_id == nil then
EntityAddComponent2(item, "VariableStorageComponent", {
_tags = "ew_global_item_id",
value_int = item_data.g_id
})
else
ComponentSetValue2(g_id, "value_int", item_data.g_id)
end
end
function net_handling.mod.item_localize(peer_id, localize_data)
local l_peer_id = localize_data[1]
local item_id = localize_data[2]
-- GamePrint("Localize "..item_id.." to "..l_peer_id)
if l_peer_id ~= ctx.my_id then
item_sync.remove_item_with_id(item_id)
end
end
return net_handling

View file

@ -139,7 +139,7 @@ local function on_world_pre_update_inner()
if my_player == nil then return end
GlobalsSetValue("ew_player_rng", tostring(GameGetFrameNum()))
net.update()
if ctx.is_host and not EntityGetIsAlive(my_player.entity) then