noita_entangled_worlds/files/src/inventory_helper.lua

279 lines
10 KiB
Lua
Raw Normal View History

2024-05-03 23:38:40 +03:00
local np = require("noitapatcher")
local EZWand = dofile_once("mods/quant.ew/files/lib/EZWand.lua")
2024-05-10 18:47:01 +03:00
local pretty = dofile_once("mods/quant.ew/files/lib/pretty_print.lua")
2024-05-03 23:38:40 +03:00
2024-05-02 21:18:55 +03:00
local inventory_helper = {}
local function entity_is_wand(entity_id)
local ability_component = EntityGetFirstComponentIncludingDisabled(entity_id, "AbilityComponent")
if ability_component == nil then return false end
return ComponentGetValue2(ability_component, "use_gun_script") == true
end
2024-05-13 13:07:28 +03:00
function inventory_helper.get_all_inventory_items(player_data)
2024-05-14 15:06:32 +03:00
local items = GameGetAllInventoryItems(player_data.entity) or {}
2024-05-13 13:07:28 +03:00
local result = {}
for _, item in pairs(items) do
table.insert(result, item)
for _, sub_item in pairs(EntityGetAllChildren(item) or {}) do
table.insert(result, sub_item)
end
end
return result
end
2024-05-02 21:18:55 +03:00
function inventory_helper.get_inventory_items(player_data, inventory_name)
local player = player_data.entity
if(not player)then
return {}
end
2024-05-13 13:07:28 +03:00
local inventory = nil
2024-05-02 21:18:55 +03:00
local player_child_entities = EntityGetAllChildren( player )
if ( player_child_entities ~= nil ) then
for i,child_entity in ipairs( player_child_entities ) do
local child_entity_name = EntityGetName( child_entity )
2024-05-13 13:07:28 +03:00
2024-05-02 21:18:55 +03:00
if ( child_entity_name == inventory_name ) then
inventory = child_entity
end
2024-05-13 13:07:28 +03:00
2024-05-02 21:18:55 +03:00
end
end
if(inventory == nil)then
return {}
end
local items = {}
for i, v in ipairs(EntityGetAllChildren(inventory) or {}) do
local item_component = EntityGetFirstComponentIncludingDisabled(v, "ItemComponent")
if(item_component)then
table.insert(items, v)
end
end
return items
end
2024-05-13 13:07:28 +03:00
function inventory_helper.serialize_single_item(item)
local x, y = EntityGetTransform(item)
if(entity_is_wand(item))then
local wand = EZWand(item)
return {true, wand:Serialize(true, true), x, y}
else
return {false, np.SerializeEntity(item), x, y}
end
end
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
2024-05-13 15:18:52 +03:00
item = EZWand(item_data[2], x, y, false).entity_id
-- EntityAddTag(item, "does_physics_update")
2024-05-13 13:07:28 +03:00
else
item = EntityCreateNew()
np.DeserializeEntity(item, item_data[2], x, y)
end
return item
end
2024-05-02 21:18:55 +03:00
function inventory_helper.get_item_data(player_data, fresh)
fresh = fresh or false
local player = player_data.entity
local inventory2Comp = EntityGetFirstComponentIncludingDisabled(player, "Inventory2Component")
local mActiveItem = ComponentGetValue2(inventory2Comp, "mActiveItem")
local wandData = {}
local spellData = {}
for k, item in ipairs(inventory_helper.get_inventory_items(player_data, "inventory_quick") or {}) do
local item_comp = EntityGetFirstComponentIncludingDisabled(item, "ItemComponent")
local slot_x, slot_y = ComponentGetValue2(item_comp, "inventory_slot")
local item_x, item_y = EntityGetTransform(item)
SetRandomSeed(item + slot_x + item_x, slot_y + item_y)
2024-05-13 13:07:28 +03:00
-- GlobalsSetValue(tostring(item) .. "_item", tostring(k))
2024-05-02 21:18:55 +03:00
if(entity_is_wand(item))then
local wand = EZWand(item)
table.insert(wandData,
{
data = wand:Serialize(not fresh, not fresh),
2024-05-03 23:38:40 +03:00
-- id = item_id or (item + Random(1, 10000000)),
2024-05-02 21:18:55 +03:00
slot_x = slot_x,
slot_y = slot_y,
active = (mActiveItem == item),
is_wand = true
})
else
table.insert(wandData,
{
data = np.SerializeEntity(item),
2024-05-03 23:38:40 +03:00
-- id = item_id or (item + Random(1, 10000000)),
2024-05-02 21:18:55 +03:00
slot_x = slot_x,
slot_y = slot_y,
active = (mActiveItem == item)
})
end
end
for k, item in ipairs(inventory_helper.get_inventory_items(player_data, "inventory_full") or {}) do
local item_comp = EntityGetFirstComponentIncludingDisabled(item, "ItemComponent")
local slot_x, slot_y = ComponentGetValue2(item_comp, "inventory_slot")
local item_x, item_y = EntityGetTransform(item)
SetRandomSeed(item + slot_x + item_x, slot_y + item_y)
-- local item_id = entity.GetVariable(item, "arena_entity_id")
2024-05-10 18:47:01 +03:00
-- GlobalsSetValue(tostring(item) .. "_item", tostring(k))
2024-05-02 21:18:55 +03:00
if(entity_is_wand(item))then
local wand = EZWand(item)
table.insert(spellData,
{
data = wand:Serialize(not fresh, not fresh),
-- id = item_id or (item + Random(1, 10000000)),
slot_x = slot_x,
slot_y = slot_y,
active = (mActiveItem == item),
is_wand = true
})
else
table.insert(spellData,
{
data = np.SerializeEntity(item),
-- id = item_id or (item + Random(1, 10000000)),
slot_x = slot_x,
slot_y = slot_y,
active = (mActiveItem == item)
})
end
end
return wandData, spellData
end
2024-05-03 23:38:40 +03:00
local function pickup_item(entity, item)
local item_component = EntityGetFirstComponentIncludingDisabled(item, "ItemComponent")
if item_component then
ComponentSetValue2(item_component, "has_been_picked_by_player", true)
end
local entity_children = EntityGetAllChildren(entity) or {}
for key, child in pairs( entity_children ) do
if EntityGetName( child ) == "inventory_quick" then
EntityAddChild( child, item)
end
end
EntitySetComponentsWithTagEnabled( item, "enabled_in_world", false )
EntitySetComponentsWithTagEnabled( item, "enabled_in_hand", false )
EntitySetComponentsWithTagEnabled( item, "enabled_in_inventory", true )
local wand_children = EntityGetAllChildren(item) or {}
for k, v in ipairs(wand_children)do
EntitySetComponentsWithTagEnabled( item, "enabled_in_world", false )
end
end
function inventory_helper.set_item_data(item_data, player_data)
local player = player_data.entity
2024-05-10 18:47:01 +03:00
if (not EntityGetIsAlive(player)) then
GamePrint("Skip set_item_data, player ".. player_data.name .. " " .. player_data.entity .. " is dead")
2024-05-03 23:38:40 +03:00
return
end
local items = GameGetAllInventoryItems(player) or {}
for i, item_id in ipairs(items) do
GameKillInventoryItem(player, item_id)
EntityKill(item_id)
end
2024-05-10 18:47:01 +03:00
2024-05-03 23:38:40 +03:00
if (item_data ~= nil) then
local active_item_entity = nil
for k, itemInfo in ipairs(item_data) do
local x, y = EntityGetTransform(player)
local item_entity = nil
local item = nil
if(itemInfo.is_wand)then
item = EZWand(itemInfo.data, x, y, GameHasFlagRun("refresh_all_charges"))
else
item = EntityCreateNew()
np.DeserializeEntity(item, itemInfo.data, x, y)
end
if (item == nil) then
return
end
2024-05-13 15:18:52 +03:00
EntityRemoveTag(item, "ew_global_item")
2024-05-03 23:38:40 +03:00
if(itemInfo.is_wand)then
item:PickUp(player)
local itemComp = EntityGetFirstComponentIncludingDisabled(item.entity_id, "ItemComponent")
if (itemComp ~= nil) then
ComponentSetValue2(itemComp, "inventory_slot", itemInfo.slot_x, itemInfo.slot_y)
end
item_entity = item.entity_id
if (itemInfo.active) then
active_item_entity = item.entity_id
end
else
pickup_item(player, item)
local itemComp = EntityGetFirstComponentIncludingDisabled(item, "ItemComponent")
if (itemComp ~= nil) then
ComponentSetValue2(itemComp, "inventory_slot", itemInfo.slot_x, itemInfo.slot_y)
end
item_entity = item
if (itemInfo.active) then
active_item_entity = item
end
end
--print("Deserialized wand #"..tostring(k).." - Active? "..tostring(wandInfo.active))
-- entity.SetVariable(item_entity, "arena_entity_id", itemInfo.id)
2024-05-13 13:07:28 +03:00
-- local lua_comps = EntityGetComponentIncludingDisabled(item_entity, "LuaComponent") or {}
-- local has_pickup_script = false
-- for i, lua_comp in ipairs(lua_comps) do
-- if (ComponentGetValue2(lua_comp, "script_item_picked_up") == "mods/evaisa.arena/files/scripts/gamemode/misc/item_pickup.lua") then
-- has_pickup_script = true
-- end
-- end
2024-05-03 23:38:40 +03:00
2024-05-13 13:07:28 +03:00
-- if (not has_pickup_script) then
-- EntityAddTag(item_entity, "does_physics_update")
-- EntityAddComponent(item_entity, "LuaComponent", {
-- _tags = "enabled_in_world,enabled_in_hand,enabled_in_inventory",
-- -- script_item_picked_up = "mods/evaisa.arena/files/scripts/gamemode/misc/item_pickup.lua",
-- -- script_kick = "mods/evaisa.arena/files/scripts/gamemode/misc/item_kick.lua",
-- -- script_throw_item = "mods/evaisa.arena/files/scripts/gamemode/misc/item_throw.lua",
-- })
-- end
2024-05-03 23:38:40 +03:00
end
if (active_item_entity ~= nil) then
np.SetActiveHeldEntity(player, active_item_entity, false, false)
end
end
end
2024-05-15 16:50:59 +03:00
function inventory_helper.has_inventory_changed(player_data)
local prev_inventory = player_data.prev_inventory_hash
local inventory_hash = 0
for _, item in ipairs(GameGetAllInventoryItems(player_data.entity)) do
local item_comp = EntityGetFirstComponentIncludingDisabled(item, "ItemComponent")
local slot_x, slot_y = ComponentGetValue2(item_comp, "inventory_slot")
inventory_hash = (inventory_hash + (item % 1024 + slot_x + slot_y)) % (math.pow(2, 20) - 1)
end
player_data.prev_inventory_hash = inventory_hash
return inventory_hash ~= prev_inventory
end
2024-05-03 23:38:40 +03:00
return inventory_helper