mirror of
https://github.com/IntQuant/noita_entangled_worlds.git
synced 2025-10-19 07:03:16 +00:00
WIP item sync: make global
This commit is contained in:
parent
5fbe9ec267
commit
70de9a26df
7 changed files with 154 additions and 25 deletions
13
files/cbs/item_notify.lua
Normal file
13
files/cbs/item_notify.lua
Normal file
|
@ -0,0 +1,13 @@
|
|||
function throw_item()
|
||||
GamePrint("Item thrown")
|
||||
GlobalsSetValue("ew_thrown", tostring(GetUpdatedEntityID()))
|
||||
end
|
||||
|
||||
function item_pickup()
|
||||
GamePrint("Item pickup")
|
||||
GlobalsSetValue("ew_picked", tostring(GetUpdatedEntityID()))
|
||||
end
|
||||
|
||||
function kick()
|
||||
-- GamePrint("Item kicked")
|
||||
end
|
|
@ -1,5 +1,6 @@
|
|||
local ctx = {
|
||||
ready = false
|
||||
ready = false,
|
||||
lib = {},
|
||||
}
|
||||
|
||||
ctx.init = function()
|
||||
|
|
|
@ -10,22 +10,34 @@ local function entity_is_wand(entity_id)
|
|||
return ComponentGetValue2(ability_component, "use_gun_script") == true
|
||||
end
|
||||
|
||||
function inventory_helper.get_all_inventory_items(player_data)
|
||||
local items = GameGetAllInventoryItems(player_data.entity)
|
||||
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
|
||||
|
||||
function inventory_helper.get_inventory_items(player_data, inventory_name)
|
||||
local player = player_data.entity
|
||||
if(not player)then
|
||||
return {}
|
||||
end
|
||||
local inventory = nil
|
||||
local inventory = nil
|
||||
|
||||
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 )
|
||||
|
||||
|
||||
if ( child_entity_name == inventory_name ) then
|
||||
inventory = child_entity
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -43,6 +55,28 @@ function inventory_helper.get_inventory_items(player_data, inventory_name)
|
|||
return items
|
||||
end
|
||||
|
||||
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
|
||||
item = EZWand(item_data[2], x, y, false)
|
||||
else
|
||||
item = EntityCreateNew()
|
||||
np.DeserializeEntity(item, item_data[2], x, y)
|
||||
end
|
||||
return item
|
||||
end
|
||||
|
||||
function inventory_helper.get_item_data(player_data, fresh)
|
||||
fresh = fresh or false
|
||||
|
||||
|
@ -57,10 +91,7 @@ function inventory_helper.get_item_data(player_data, fresh)
|
|||
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")
|
||||
|
||||
GlobalsSetValue(tostring(item) .. "_item", tostring(k))
|
||||
-- GlobalsSetValue(tostring(item) .. "_item", tostring(k))
|
||||
if(entity_is_wand(item))then
|
||||
local wand = EZWand(item)
|
||||
table.insert(wandData,
|
||||
|
@ -202,23 +233,23 @@ function inventory_helper.set_item_data(item_data, player_data)
|
|||
|
||||
-- entity.SetVariable(item_entity, "arena_entity_id", itemInfo.id)
|
||||
|
||||
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
|
||||
-- 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
|
||||
|
||||
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
|
||||
-- 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
|
||||
end
|
||||
|
||||
if (active_item_entity ~= nil) then
|
||||
|
|
64
files/src/item_sync.lua
Normal file
64
files/src/item_sync.lua
Normal file
|
@ -0,0 +1,64 @@
|
|||
local inventory_helper = dofile_once("mods/quant.ew/files/src/inventory_helper.lua")
|
||||
local ctx = dofile_once("mods/quant.ew/files/src/ctx.lua")
|
||||
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 = EntityGetComponentIncludingDisabled(ent, "LuaComponent", "ew_notify_component")
|
||||
if notify == nil then
|
||||
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",
|
||||
script_item_picked_up = "mods/quant.ew/files/cbs/item_notify.lua",
|
||||
-- script_kick = "mods/quant.ew/files/cbs/item_notify.lua",
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function is_item_on_ground(item)
|
||||
return EntityGetComponent(item, "SimplePhysicsComponent") ~= nil or EntityGetComponent(item, "PhysicsBodyComponent")
|
||||
end
|
||||
|
||||
function item_sync.make_item_global(item)
|
||||
local item_data = inventory_helper.serialize_single_item(item)
|
||||
ctx.lib.net.send_make_global(item_data)
|
||||
end
|
||||
|
||||
local function get_global_ent(key)
|
||||
local val = tonumber(GlobalsGetValue(key, "0"))
|
||||
GlobalsSetValue(key, "0")
|
||||
if val ~= 0 then
|
||||
return val
|
||||
end
|
||||
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
|
||||
end
|
||||
|
||||
return item_sync
|
|
@ -8,6 +8,8 @@ local reactor = pollnet.Reactor()
|
|||
local net_handling = dofile_once("mods/quant.ew/files/src/net_handling.lua")
|
||||
local net = {}
|
||||
|
||||
ctx.lib.net = net
|
||||
|
||||
function net.update()
|
||||
reactor:update()
|
||||
end
|
||||
|
@ -128,4 +130,8 @@ function net.send_fire(fire_info)
|
|||
net.send("fire", fire_info, true)
|
||||
end
|
||||
|
||||
function net.send_make_global(item_data)
|
||||
net.send("item_global", item_data, true)
|
||||
end
|
||||
|
||||
return net
|
|
@ -4,6 +4,7 @@ local util = dofile_once("mods/quant.ew/files/src/util.lua")
|
|||
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 np = require("noitapatcher")
|
||||
|
||||
|
@ -143,4 +144,11 @@ function net_handling.mod.fire(peer_id, fire_data)
|
|||
end
|
||||
end
|
||||
|
||||
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)
|
||||
end
|
||||
|
||||
return net_handling
|
8
init.lua
8
init.lua
|
@ -16,6 +16,7 @@ local pretty = dofile_once("mods/quant.ew/files/lib/pretty_print.lua")
|
|||
local perk_fns = dofile_once("mods/quant.ew/files/src/perk_fns.lua")
|
||||
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 item_sync = dofile_once("mods/quant.ew/files/src/item_sync.lua")
|
||||
|
||||
ModLuaFileAppend("data/scripts/gun/gun.lua", "mods/quant.ew/files/append/gun.lua")
|
||||
ModLuaFileAppend("data/scripts/gun/gun_actions.lua", "mods/quant.ew/files/append/action_fix.lua")
|
||||
|
@ -131,6 +132,7 @@ function OnPlayerSpawned( player_entity ) -- This runs when player entity has be
|
|||
local x, y = EntityGetFirstHitboxCenter(player_entity)
|
||||
perk_spawn(x, y, "LASER_AIM", true)
|
||||
perk_spawn(x-50, y, "GLASS_CANNON", true)
|
||||
perk_spawn(x-25, y, "EDIT_WANDS_EVERYWHERE", true)
|
||||
end
|
||||
|
||||
local function on_world_pre_update_inner()
|
||||
|
@ -153,7 +155,11 @@ local function on_world_pre_update_inner()
|
|||
EntityInflictDamage(my_player.entity, 10000000, "DAMAGE_CURSE", "Out of shared health", "NONE", 0, 0, GameGetWorldStateEntity())
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
if ctx.is_host then
|
||||
item_sync.host_upload_items(my_player)
|
||||
end
|
||||
|
||||
-- Player sync
|
||||
if GameGetFrameNum() % 1 == 0 then
|
||||
local input_data = player_fns.serialize_inputs(my_player)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue