fix shops, fix enemys with wands maybe, fix some more item dupes, dont tether between PW since everyone should be able to get there

This commit is contained in:
bgkillas 2024-11-24 12:45:54 -05:00
parent 161e71fefc
commit 9b87a6cba5
3 changed files with 62 additions and 16 deletions

View file

@ -49,6 +49,10 @@ local HpData = util.make_type({
f32 = {"hp", "max_hp"} f32 = {"hp", "max_hp"}
}) })
local should_wait = {}
local first = true
local FULL_TURN = math.pi * 2 local FULL_TURN = math.pi * 2
local frame = 0 local frame = 0
@ -268,6 +272,9 @@ function enemy_sync.host_upload_entities()
item_sync.make_item_global(item) item_sync.make_item_global(item)
else else
wand = item_sync.get_global_item_id(item) wand = item_sync.get_global_item_id(item)
if not item_sync.is_my_item(wand) then
EntityAddTag(item, "ew_client_item")
end
end end
end end
end end
@ -290,7 +297,8 @@ function enemy_sync.host_upload_entities()
::continue:: ::continue::
end end
rpc.handle_enemy_data(enemy_data_list) rpc.handle_enemy_data(enemy_data_list, first)
first = false
if #dead_entities > 0 then if #dead_entities > 0 then
rpc.handle_death_data(dead_entities) rpc.handle_death_data(dead_entities)
end end
@ -576,7 +584,9 @@ local function sync_enemy(enemy_info_raw, force_no_cull)
if gid ~= nil and (item == nil or item == 0 or not EntityGetIsAlive(item)) then if gid ~= nil and (item == nil or item == 0 or not EntityGetIsAlive(item)) then
local wand = item_sync.find_by_gid(gid) local wand = item_sync.find_by_gid(gid)
if wand ~= nil then if wand ~= nil then
if not item_sync.is_my_item(gid) then
EntityAddTag(wand, "ew_client_item") EntityAddTag(wand, "ew_client_item")
end
local found = false local found = false
for _, child in ipairs(EntityGetAllChildren(enemy_id) or {}) do for _, child in ipairs(EntityGetAllChildren(enemy_id) or {}) do
if EntityGetName(child) == "inventory_quick" then if EntityGetName(child) == "inventory_quick" then
@ -601,7 +611,10 @@ local function sync_enemy(enemy_info_raw, force_no_cull)
EntitySetComponentsWithTagEnabled(wand, "enabled_in_inventory", false) EntitySetComponentsWithTagEnabled(wand, "enabled_in_inventory", false)
np.SetActiveHeldEntity(enemy_id, wand, false, false) np.SetActiveHeldEntity(enemy_id, wand, false, false)
else else
if should_wait[gid] == nil or should_wait[gid] < GameGetFrameNum() then
item_sync.rpc.request_send_again(gid) item_sync.rpc.request_send_again(gid)
should_wait[gid] = GameGetFrameNum() + 15
end
end end
end end
@ -671,7 +684,10 @@ function rpc.handle_death_data(death_data)
end end
end end
function rpc.handle_enemy_data(enemy_data) function rpc.handle_enemy_data(enemy_data, first)
if first then
ctx.entity_by_remote_id = {}
end
frame = GameGetFrameNum() frame = GameGetFrameNum()
for _, enemy_info_raw in ipairs(enemy_data) do for _, enemy_info_raw in ipairs(enemy_data) do
sync_enemy(enemy_info_raw, false) sync_enemy(enemy_info_raw, false)

View file

@ -61,7 +61,10 @@ util.add_cross_call("ew_item_death_notify", function(enemy_id, responsible_id)
else else
responsible = responsible_id responsible = responsible_id
end end
table.insert(dead_entities, {item_sync.get_global_item_id(enemy_id), responsible}) local gid = item_sync.get_global_item_id(enemy_id)
if gid ~= nil then
table.insert(dead_entities, {gid, responsible})
end
end) end)
function item_sync.ensure_notify_component(ent) function item_sync.ensure_notify_component(ent)
@ -125,6 +128,7 @@ function item_sync.find_by_gid(gid)
local candidate local candidate
for _, item in ipairs(EntityGetWithTag("ew_global_item") or {}) do for _, item in ipairs(EntityGetWithTag("ew_global_item") or {}) do
local i_gid = item_sync.get_global_item_id(item) local i_gid = item_sync.get_global_item_id(item)
if i_gid ~= nil then
find_by_gid_cache[i_gid] = item find_by_gid_cache[i_gid] = item
if i_gid == gid then if i_gid == gid then
if is_item_on_ground(item) then if is_item_on_ground(item) then
@ -134,6 +138,7 @@ function item_sync.find_by_gid(gid)
end end
end end
end end
end
return candidate return candidate
end end
@ -260,6 +265,10 @@ local function is_my_item(gid)
return string.sub(gid, 1, 16) == ctx.my_id return string.sub(gid, 1, 16) == ctx.my_id
end end
function item_sync.is_my_item(gid)
is_my_item(gid)
end
function item_sync.take_authority(gid) function item_sync.take_authority(gid)
if not is_my_item(gid) then if not is_my_item(gid) then
local new_id = allocate_global_id() local new_id = allocate_global_id()
@ -272,6 +281,15 @@ rpc.opts_reliable()
function rpc.give_authority_to(gid, new_id) function rpc.give_authority_to(gid, new_id)
local item = item_sync.find_by_gid(gid) local item = item_sync.find_by_gid(gid)
find_by_gid_cache[gid] = nil find_by_gid_cache[gid] = nil
if table.contains(pending_remove, gid) then
for i, id in ipairs(pending_remove) do
if id == gid then
table.remove(pending_remove, i)
break
end
end
table.insert(pending_remove, new_id)
end
if item ~= nil then if item ~= nil then
find_by_gid_cache[new_id] = item find_by_gid_cache[new_id] = item
local var = EntityGetFirstComponentIncludingDisabled(item, "VariableStorageComponent", "ew_global_item_id") local var = EntityGetFirstComponentIncludingDisabled(item, "VariableStorageComponent", "ew_global_item_id")
@ -424,8 +442,10 @@ function item_sync.on_world_update_host()
local picked_item = get_global_ent("ew_picked") local picked_item = get_global_ent("ew_picked")
if picked_item ~= nil and EntityHasTag(picked_item, "ew_global_item") then if picked_item ~= nil and EntityHasTag(picked_item, "ew_global_item") then
local gid = item_sync.get_global_item_id(picked_item) local gid = item_sync.get_global_item_id(picked_item)
if gid ~= nil then
item_sync.host_localize_item(gid, ctx.my_id) item_sync.host_localize_item(gid, ctx.my_id)
end end
end
remove_client_items_from_world() remove_client_items_from_world()
end end
@ -435,15 +455,17 @@ function item_sync.on_world_update_client()
mark_in_inventory(my_player) mark_in_inventory(my_player)
end end
local thrown_item = get_global_ent("ew_thrown") local thrown_item = get_global_ent("ew_thrown")
if thrown_item ~= nil then if thrown_item ~= nil and is_my_item(item_sync.get_global_item_id(thrown_item)) then
item_sync.make_item_global(thrown_item) item_sync.make_item_global(thrown_item)
end end
local picked_item = get_global_ent("ew_picked") local picked_item = get_global_ent("ew_picked")
if picked_item ~= nil and EntityHasTag(picked_item, "ew_global_item") then if picked_item ~= nil and EntityHasTag(picked_item, "ew_global_item") then
local gid = item_sync.get_global_item_id(picked_item) local gid = item_sync.get_global_item_id(picked_item)
if gid ~= nil then
rpc.item_localize_req(gid) rpc.item_localize_req(gid)
end end
end
remove_client_items_from_world() remove_client_items_from_world()
end end
@ -495,10 +517,12 @@ function item_sync.on_should_send_updates()
if is_item_on_ground(item) and not EntityHasTag(item, "mimic_potion") then if is_item_on_ground(item) and not EntityHasTag(item, "mimic_potion") then
local item_data = inventory_helper.serialize_single_item(item) local item_data = inventory_helper.serialize_single_item(item)
local gid = item_sync.get_global_item_id(item) local gid = item_sync.get_global_item_id(item)
if gid ~= nil then
item_data.gid = gid item_data.gid = gid
table.insert(item_list, item_data) table.insert(item_list, item_data)
end end
end end
end
rpc.initial_items(item_list) rpc.initial_items(item_list)
end end

View file

@ -1,3 +1,4 @@
dofile_once("data/scripts/lib/utilities.lua")
local ctx = dofile_once("mods/quant.ew/files/core/ctx.lua") local ctx = dofile_once("mods/quant.ew/files/core/ctx.lua")
local player_fns = dofile_once("mods/quant.ew/files/core/player_fns.lua") local player_fns = dofile_once("mods/quant.ew/files/core/player_fns.lua")
local rpc = net.new_rpc_namespace() local rpc = net.new_rpc_namespace()
@ -252,6 +253,11 @@ function module.on_world_update()
end end
return return
end end
local host_pw = check_parallel_pos(x1)
local my_pw = check_parallel_pos(x2)
if host_pw ~= my_pw then
return
end
local dx = x1-x2 local dx = x1-x2
local dy = y1-y2 local dy = y1-y2
local dist_sq = dx*dx + dy*dy local dist_sq = dx*dx + dy*dy