sync items positions and stuffs

This commit is contained in:
bgkillas 2024-11-17 22:09:57 -05:00
parent f1347df61e
commit e391b1552f
3 changed files with 97 additions and 8 deletions

View file

@ -0,0 +1,3 @@
function death( damage_type_bit_field, damage_message, entity_thats_responsible, drop_items )
CrossCall("ew_item_death_notify", GetUpdatedEntityID(), entity_thats_responsible)
end

View file

@ -164,13 +164,13 @@ function enemy_sync.host_upload_entities()
if ai_component ~= 0 and ai_component ~= nil then if ai_component ~= 0 and ai_component ~= nil then
ComponentSetValue2(ai_component, "max_distance_to_cam_to_start_hunting", math.pow(2, 29)) ComponentSetValue2(ai_component, "max_distance_to_cam_to_start_hunting", math.pow(2, 29))
end end
local hp, max_hp, has_hp = util.get_ent_health(enemy_id)
local phys_info = util.get_phys_info(enemy_id, true) local phys_info = util.get_phys_info(enemy_id, true)
if phys_info == nil then if phys_info == nil then
goto continue goto continue
end end
local hp, max_hp, has_hp = util.get_ent_health(enemy_id)
if has_hp then if has_hp then
util.ensure_component_present(enemy_id, "LuaComponent", "ew_death_notify", { util.ensure_component_present(enemy_id, "LuaComponent", "ew_death_notify", {
script_death = "mods/quant.ew/files/resource/cbs/death_notify.lua" script_death = "mods/quant.ew/files/resource/cbs/death_notify.lua"

View file

@ -12,6 +12,19 @@ local item_sync = {}
local pending_remove = {} local pending_remove = {}
local pickup_handlers = {} local pickup_handlers = {}
local dead_entities = {}
util.add_cross_call("ew_item_death_notify", function(enemy_id, responsible_id)
local player_data = player_fns.get_player_data_by_local_entity_id(responsible_id)
local responsible
if player_data ~= nil then
responsible = player_data.peer_id
else
responsible = responsible_id
end
table.insert(dead_entities, {item_sync.get_global_item_id(enemy_id), responsible})
end)
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
@ -146,7 +159,7 @@ function item_sync.make_item_global(item, instant, give_authority_to)
end end
item_sync.ensure_notify_component(item) item_sync.ensure_notify_component(item)
local gid_component = EntityGetFirstComponentIncludingDisabled(item, "VariableStorageComponent", local gid_component = EntityGetFirstComponentIncludingDisabled(item, "VariableStorageComponent",
"ew_global_item_id") "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()
@ -166,6 +179,13 @@ function item_sync.make_item_global(item, instant, give_authority_to)
--end --end
local item_data = inventory_helper.serialize_single_item(item) local item_data = inventory_helper.serialize_single_item(item)
item_data.gid = gid item_data.gid = gid
local hp, max_hp, has_hp = util.get_ent_health(item)
if has_hp then
util.ensure_component_present(item, "LuaComponent", "ew_item_death_notify", {
script_death = "mods/quant.ew/files/resource/cbs/item_death_notify.lua"
})
end
ctx.item_prevent_localize[gid] = false ctx.item_prevent_localize[gid] = false
rpc.item_globalize(item_data) rpc.item_globalize(item_data)
end) end)
@ -194,6 +214,61 @@ 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
rpc.opts_reliable()
function rpc.handle_death_data(death_data)
for _, remote_data in ipairs(death_data) do
local remote_id = remote_data[1]
--[[if confirmed_kills[remote_id] then
GamePrint("Remote id has been killed already..?")
goto continue
end
confirmed_kills[remote_id] = true]]
local responsible_entity = 0
local peer_data = player_fns.peer_get_player_data(remote_data[2], true)
if peer_data ~= nil then
responsible_entity = peer_data.entity
elseif ctx.entity_by_remote_id[remote_data[2]] ~= nil then
responsible_entity = ctx.entity_by_remote_id[remote_data[2]]
end
--if unsynced_enemys[remote_id] ~= nil then
--sync_enemy(unsynced_enemys[remote_id], true)
--end
local enemy_id = item_sync.find_by_gid(remote_id)
if enemy_id ~= nil and EntityGetIsAlive(enemy_id) then
local immortal = EntityGetFirstComponentIncludingDisabled(enemy_id, "LuaComponent", "ew_immortal")
if immortal ~= 0 then
EntityRemoveComponent(enemy_id, immortal)
end
local protection_component_id = GameGetGameEffect(enemy_id, "PROTECTION_ALL")
if protection_component_id ~= 0 then
EntitySetComponentIsEnabled(enemy_id, protection_component_id, false)
end
local damage_component = EntityGetFirstComponentIncludingDisabled(enemy_id, "DamageModelComponent")
if damage_component and damage_component ~= 0 then
ComponentSetValue2(damage_component, "wait_for_kill_flag_on_death", false)
end
-- Enable explosion back
local expl_component = EntityGetFirstComponent(enemy_id, "ExplodeOnDamageComponent")
if expl_component ~= nil and expl_component ~= 0 then
ComponentSetValue2(expl_component, "explode_on_death_percent", 1)
end
local current_hp = util.get_ent_health(enemy_id)
local dmg = current_hp
if dmg > 0 then
EntityInflictDamage(enemy_id, dmg+0.1, "DAMAGE_CURSE", "", "NONE", 0, 0, responsible_entity)
end
EntityInflictDamage(enemy_id, 1000000000, "DAMAGE_CURSE", "", "NONE", 0, 0, responsible_entity) -- Just to be sure
EntityKill(enemy_id)
end
::continue::
end
end
local function send_item_positions() local function send_item_positions()
local position_data = {} local position_data = {}
for _, item in ipairs(EntityGetWithTag("ew_global_item")) do for _, item in ipairs(EntityGetWithTag("ew_global_item")) do
@ -210,10 +285,17 @@ local function send_item_positions()
ComponentSetValue2(costcom, "stealable", true) ComponentSetValue2(costcom, "stealable", true)
end end
end end
position_data[gid] = {x, y, cost} local phys_info = util.get_phys_info(item, true)
if phys_info ~= nil then
position_data[gid] = {x, y, phys_info, cost}
end
end end
end end
rpc.update_positions(position_data) rpc.update_positions(position_data)
if #dead_entities > 0 then
rpc.handle_death_data(dead_entities)
end
dead_entities = {}
end end
function item_sync.on_world_update_host() function item_sync.on_world_update_host()
@ -265,9 +347,6 @@ function item_sync.on_world_update()
item_sync.remove_item_with_id_now(gid) item_sync.remove_item_with_id_now(gid)
end end
end end
if GameGetFrameNum() % 60 == 31 then
send_item_positions()
end
if GameGetFrameNum() % 5 == 2 then if GameGetFrameNum() % 5 == 2 then
for _, ent in ipairs(EntityGetWithTag("mimic_potion")) do for _, ent in ipairs(EntityGetWithTag("mimic_potion")) do
if ctx.is_host and not EntityHasTag(ent, "polymorphed_player") then if ctx.is_host and not EntityHasTag(ent, "polymorphed_player") then
@ -277,6 +356,9 @@ function item_sync.on_world_update()
end end
end end
end end
if GameGetFrameNum() % 5 == 3 then
send_item_positions()
end
end end
function item_sync.on_should_send_updates() function item_sync.on_should_send_updates()
@ -366,6 +448,7 @@ function rpc.item_globalize(item_data)
ComponentSetValue2(com, "value_int", GameGetFrameNum()) ComponentSetValue2(com, "value_int", GameGetFrameNum())
end end
end end
EntityAddComponent2(item, "LuaComponent", {_tags="ew_immortal", script_damage_about_to_be_received = "mods/quant.ew/files/resource/cbs/immortal.lua"})
end end
rpc.opts_reliable() rpc.opts_reliable()
@ -394,11 +477,14 @@ function rpc.update_positions(position_data)
local cx, cy = GameGetCameraPos() local cx, cy = GameGetCameraPos()
for gid, el in pairs(position_data) do for gid, el in pairs(position_data) do
local x, y = el[1], el[2] local x, y = el[1], el[2]
local price = el[3] local phys_info = el[3]
local price = el[4]
if math.abs(x - cx) < LIMIT and math.abs(y - cy) < LIMIT then if math.abs(x - cx) < LIMIT and math.abs(y - cy) < LIMIT then
local item = item_sync.find_by_gid(gid) local item = item_sync.find_by_gid(gid)
if item ~= nil then if item ~= nil then
EntitySetTransform(item, x, y) if not util.set_phys_info(item, phys_info) then
EntitySetTransform(item, x, y)
end
local costcom = EntityGetFirstComponentIncludingDisabled(item, "ItemCostComponent") local costcom = EntityGetFirstComponentIncludingDisabled(item, "ItemCostComponent")
if costcom ~= nil then if costcom ~= nil then
if price == 0 then if price == 0 then