Mostly sync shop spells

This commit is contained in:
IQuant 2024-08-30 11:12:10 +03:00
parent 34e162c5f5
commit 57900bf60d
3 changed files with 46 additions and 19 deletions

View file

@ -14,14 +14,14 @@ local pickup_handlers = {}
function item_sync.ensure_notify_component(ent) function item_sync.ensure_notify_component(ent)
local notify = EntityGetFirstComponentIncludingDisabled(ent, "LuaComponent", "ew_notify_component") local notify = EntityGetFirstComponentIncludingDisabled(ent, "LuaComponent", "ew_notify_component")
if notify == nil then if notify == nil then
EntityAddComponent2(ent, "LuaComponent", { EntityAddComponent2(ent, "LuaComponent", {
_tags = "enabled_in_world,enabled_in_hand,enabled_in_inventory,ew_notify_component", _tags = "enabled_in_world,enabled_in_hand,enabled_in_inventory,ew_notify_component",
script_throw_item = "mods/quant.ew/files/resource/cbs/item_notify.lua", script_throw_item = "mods/quant.ew/files/resource/cbs/item_notify.lua",
script_item_picked_up = "mods/quant.ew/files/resource/cbs/item_notify.lua", script_item_picked_up = "mods/quant.ew/files/resource/cbs/item_notify.lua",
-- script_kick = "mods/quant.ew/files/resource/cbs/item_notify.lua", -- script_kick = "mods/quant.ew/files/resource/cbs/item_notify.lua",
}) })
end end
end end
local function mark_in_inventory(my_player) local function mark_in_inventory(my_player)
@ -33,13 +33,15 @@ end
local function allocate_global_id() local function allocate_global_id()
local current = tonumber(GlobalsGetValue("ew_global_item_id", "1")) local current = tonumber(GlobalsGetValue("ew_global_item_id", "1"))
GlobalsSetValue("ew_global_item_id", tostring(current+1)) GlobalsSetValue("ew_global_item_id", tostring(current + 1))
return ctx.my_player.peer_id..":"..current return ctx.my_player.peer_id .. ":" .. current
end end
-- Try to guess if the item is in world. -- Try to guess if the item is in world.
local function is_item_on_ground(item) local function is_item_on_ground(item)
return EntityGetComponent(item, "SimplePhysicsComponent") ~= nil or EntityGetComponent(item, "PhysicsBodyComponent") ~= nil or EntityGetComponent(item, "SpriteParticleEmitterComponent") ~= nil return EntityGetComponent(item, "SimplePhysicsComponent") ~= nil or
EntityGetComponent(item, "PhysicsBodyComponent") ~= nil or
EntityGetComponent(item, "SpriteParticleEmitterComponent") ~= nil
end end
function item_sync.get_global_item_id(item) function item_sync.get_global_item_id(item)
@ -87,7 +89,7 @@ end
function item_sync.host_localize_item(gid, peer_id) function item_sync.host_localize_item(gid, peer_id)
if ctx.item_prevent_localize[gid] then if ctx.item_prevent_localize[gid] then
GamePrint("Item localize for "..gid.." prevented") GamePrint("Item localize for " .. gid .. " prevented")
return return
end end
ctx.item_prevent_localize[gid] = true ctx.item_prevent_localize[gid] = true
@ -120,7 +122,8 @@ function item_sync.make_item_global(item, instant)
return return
end end
item_sync.ensure_notify_component(item) item_sync.ensure_notify_component(item)
local gid_component = EntityGetFirstComponentIncludingDisabled(item, "VariableStorageComponent", "ew_global_item_id") local gid_component = EntityGetFirstComponentIncludingDisabled(item, "VariableStorageComponent",
"ew_global_item_id")
local gid local gid
if gid_component == nil then if gid_component == nil then
gid = allocate_global_id() gid = allocate_global_id()
@ -235,7 +238,7 @@ function item_sync.on_draw_debug_window(imgui)
local x, y = EntityGetTransform(ent) local x, y = EntityGetTransform(ent)
GameCreateSpriteForXFrames("mods/quant.ew/files/resource/debug/marker.png", x, y, true, 0, 0, 1, true) GameCreateSpriteForXFrames("mods/quant.ew/files/resource/debug/marker.png", x, y, true, 0, 0, 1, true)
local gid = item_sync.get_global_item_id(ent) local gid = item_sync.get_global_item_id(ent)
imgui.Text("GID: "..tostring(gid)) imgui.Text("GID: " .. tostring(gid))
local prevented = ctx.item_prevent_localize[gid] local prevented = ctx.item_prevent_localize[gid]
if prevented then if prevented then
imgui.Text("Localize prevented") imgui.Text("Localize prevented")
@ -274,6 +277,7 @@ function rpc.initial_items(item_list)
if item == nil then if item == nil then
local item_new = inventory_helper.deserialize_single_item(item_data) local item_new = inventory_helper.deserialize_single_item(item_data)
add_stuff_to_globalized_item(item_new, item_data.gid) add_stuff_to_globalized_item(item_new, item_data.gid)
GamePrint("spawned initial item " .. item_new)
end end
end end
end end
@ -315,4 +319,4 @@ ctx.cap.item_sync = {
end end
} }
return item_sync return item_sync

View file

@ -10,4 +10,15 @@ function EntityLoadCameraBound(ent, x, y)
if pre_result ~= false then if pre_result ~= false then
return pre_result return pre_result
end end
end end
local old_CreateItemActionEntity = CreateItemActionEntity
function CreateItemActionEntity(action_id, x, y)
if not CrossCall("ew_action_spawn_hook_pre") then
return 0
end
local eid = old_CreateItemActionEntity(action_id, x, y)
CrossCall("ew_action_spawn_hook", eid)
return eid
end

View file

@ -4,7 +4,8 @@ local np = require("noitapatcher")
local module = {} local module = {}
ModLuaFileAppend("data/scripts/director_helpers.lua", "mods/quant.ew/files/system/spawn_hooks/append/director_helpers.lua") ModLuaFileAppend("data/scripts/director_helpers.lua",
"mods/quant.ew/files/system/spawn_hooks/append/director_helpers.lua")
ModLuaFileAppend("data/scripts/item_spawnlists.lua", "mods/quant.ew/files/system/spawn_hooks/append/item_spawnlist.lua") ModLuaFileAppend("data/scripts/item_spawnlists.lua", "mods/quant.ew/files/system/spawn_hooks/append/item_spawnlist.lua")
local exclude = {} local exclude = {}
@ -30,6 +31,9 @@ local function is_sync_item(ent_path)
if string.sub(ent_path, 1, #start) == start then if string.sub(ent_path, 1, #start) == start then
return true return true
end end
if ent_path == "data/entities/misc/custom_cards/action.xml" then
return true
end
return false return false
end end
@ -51,6 +55,14 @@ np.CrossCallAdd("ew_spawn_hook_pre", function(ent_path, x, y)
end end
end) end)
np.CrossCallAdd("ew_action_spawn_hook_pre", function()
return (not ctx.proxy_opt.item_dedup) or ctx.is_host
end)
np.CrossCallAdd("ew_action_spawn_hook", function(eid)
ctx.cap.item_sync.globalize(eid, false)
end)
-- Called after entity was loaded. -- Called after entity was loaded.
-- Might be useless in some cases, as entity was already despawned/serialized due to CameraBoundComponent. -- Might be useless in some cases, as entity was already despawned/serialized due to CameraBoundComponent.
np.CrossCallAdd("ew_spawn_hook_post", function(ent_path, ent) np.CrossCallAdd("ew_spawn_hook_post", function(ent_path, ent)
@ -64,7 +76,7 @@ function module.entity_is_synced(ent_path)
return entity_is_enemy_cache[ent_path] return entity_is_enemy_cache[ent_path]
end end
print("Checking if this is an enemy: "..ent_path) print("Checking if this is an enemy: " .. ent_path)
local tags = util.load_ents_tags(ent_path) local tags = util.load_ents_tags(ent_path)
@ -74,4 +86,4 @@ function module.entity_is_synced(ent_path)
return res return res
end end
return module return module