remove private chunk multi api impl

This commit is contained in:
BlackMATov
2025-04-19 22:49:51 +07:00
parent 265323a8b3
commit dce83d1a1f
-522
View File
@@ -2132,9 +2132,6 @@ local __chunk_set
local __chunk_remove
local __chunk_clear
local __chunk_multi_set
local __chunk_multi_remove
---
---
---
@@ -2885,525 +2882,6 @@ function __chunk_clear(chunk)
__structural_changes = __structural_changes + 1
end
---@param old_chunk evolved.chunk
---@param fragments evolved.fragment[]
---@param fragment_count integer
---@param components evolved.component[]
function __chunk_multi_set(old_chunk, fragments, fragment_count, components)
if __defer_depth <= 0 then
__error_fmt('batched chunk operations should be deferred')
end
if fragment_count == 0 then
return
end
local new_chunk = __chunk_with_fragment_list(old_chunk, fragments, fragment_count)
if not new_chunk then
return
end
local old_entity_list = old_chunk.__entity_list
local old_entity_count = old_chunk.__entity_count
if old_entity_count == 0 then
return
end
local old_fragment_set = old_chunk.__fragment_set
local old_component_count = old_chunk.__component_count
local old_component_indices = old_chunk.__component_indices
local old_component_storages = old_chunk.__component_storages
local old_component_fragments = old_chunk.__component_fragments
if old_chunk == new_chunk then
local old_chunk_has_setup_hooks = old_chunk.__has_setup_hooks
local old_chunk_has_assign_hooks = old_chunk.__has_assign_hooks
for i = 1, fragment_count do
local fragment = fragments[i]
---@type evolved.default?, evolved.duplicate?, evolved.set_hook?, evolved.assign_hook?
local fragment_default, fragment_duplicate, fragment_on_set, fragment_on_assign
if old_chunk_has_setup_hooks or old_chunk_has_assign_hooks then
fragment_default, fragment_duplicate, fragment_on_set, fragment_on_assign =
__evolved_get(fragment, __DEFAULT, __DUPLICATE, __ON_SET, __ON_ASSIGN)
end
if fragment_on_set or fragment_on_assign then
local old_component_index = old_component_indices[fragment]
if old_component_index then
local old_component_storage = old_component_storages[old_component_index]
if fragment_duplicate then
for old_place = 1, old_entity_count do
local entity = old_entity_list[old_place]
local new_component = components[i]
if new_component == nil then new_component = fragment_default end
if new_component ~= nil then new_component = fragment_duplicate(new_component) end
if new_component == nil then new_component = true end
local old_component = old_component_storage[old_place]
old_component_storage[old_place] = new_component
if fragment_on_set then
__defer_call_hook(fragment_on_set, entity, fragment, new_component, old_component)
end
if fragment_on_assign then
__defer_call_hook(fragment_on_assign, entity, fragment, new_component, old_component)
end
end
else
local new_component = components[i]
if new_component == nil then new_component = fragment_default end
if new_component == nil then new_component = true end
for old_place = 1, old_entity_count do
local entity = old_entity_list[old_place]
local old_component = old_component_storage[old_place]
old_component_storage[old_place] = new_component
if fragment_on_set then
__defer_call_hook(fragment_on_set, entity, fragment, new_component, old_component)
end
if fragment_on_assign then
__defer_call_hook(fragment_on_assign, entity, fragment, new_component, old_component)
end
end
end
else
for old_place = 1, old_entity_count do
local entity = old_entity_list[old_place]
if fragment_on_set then
__defer_call_hook(fragment_on_set, entity, fragment)
end
if fragment_on_assign then
__defer_call_hook(fragment_on_assign, entity, fragment)
end
end
end
else
local old_component_index = old_component_indices[fragment]
if old_component_index then
local old_component_storage = old_component_storages[old_component_index]
if fragment_duplicate then
for old_place = 1, old_entity_count do
local new_component = components[i]
if new_component == nil then new_component = fragment_default end
if new_component ~= nil then new_component = fragment_duplicate(new_component) end
if new_component == nil then new_component = true end
old_component_storage[old_place] = new_component
end
else
local new_component = components[i]
if new_component == nil then new_component = fragment_default end
if new_component == nil then new_component = true end
for old_place = 1, old_entity_count do
old_component_storage[old_place] = new_component
end
end
else
-- nothing
end
end
end
else
local new_entity_list = new_chunk.__entity_list
local new_entity_count = new_chunk.__entity_count
local new_component_indices = new_chunk.__component_indices
local new_component_storages = new_chunk.__component_storages
local new_chunk_has_setup_hooks = new_chunk.__has_setup_hooks
local new_chunk_has_assign_hooks = new_chunk.__has_assign_hooks
local new_chunk_has_insert_hooks = new_chunk.__has_insert_hooks
if new_entity_count == 0 then
old_chunk.__entity_list, new_chunk.__entity_list =
new_entity_list, old_entity_list
old_entity_list, new_entity_list =
new_entity_list, old_entity_list
for old_ci = 1, old_component_count do
local old_f = old_component_fragments[old_ci]
local new_ci = new_component_indices[old_f]
old_component_storages[old_ci], new_component_storages[new_ci] =
new_component_storages[new_ci], old_component_storages[old_ci]
end
new_chunk.__entity_count = old_entity_count
else
__lua_table_move(
old_entity_list, 1, old_entity_count,
new_entity_count + 1, new_entity_list)
for old_ci = 1, old_component_count do
local old_f = old_component_fragments[old_ci]
local old_cs = old_component_storages[old_ci]
local new_ci = new_component_indices[old_f]
local new_cs = new_component_storages[new_ci]
__lua_table_move(old_cs, 1, old_entity_count, new_entity_count + 1, new_cs)
end
new_chunk.__entity_count = new_entity_count + old_entity_count
end
do
local entity_chunks = __entity_chunks
local entity_places = __entity_places
for new_place = new_entity_count + 1, new_entity_count + old_entity_count do
local entity = new_entity_list[new_place]
local entity_index = entity % 0x100000
entity_chunks[entity_index] = new_chunk
entity_places[entity_index] = new_place
end
__detach_all_entities(old_chunk)
end
---@type table<evolved.fragment, boolean>
local inserted_set = __acquire_table(__table_pool_tag.fragment_set)
for i = 1, fragment_count do
local fragment = fragments[i]
---@type evolved.default?, evolved.duplicate?, evolved.set_hook?, evolved.assign_hook?, evolved.insert_hook?
local fragment_default, fragment_duplicate, fragment_on_set, fragment_on_assign, fragment_on_insert
if new_chunk_has_setup_hooks or new_chunk_has_assign_hooks or new_chunk_has_insert_hooks then
fragment_default, fragment_duplicate, fragment_on_set, fragment_on_assign, fragment_on_insert =
__evolved_get(fragment, __DEFAULT, __DUPLICATE, __ON_SET, __ON_ASSIGN, __ON_INSERT)
end
if inserted_set[fragment] or old_fragment_set[fragment] then
if fragment_on_set or fragment_on_assign then
local new_component_index = new_component_indices[fragment]
if new_component_index then
local new_component_storage = new_component_storages[new_component_index]
if fragment_duplicate then
for new_place = new_entity_count + 1, new_entity_count + old_entity_count do
local entity = new_entity_list[new_place]
local new_component = components[i]
if new_component == nil then new_component = fragment_default end
if new_component ~= nil then new_component = fragment_duplicate(new_component) end
if new_component == nil then new_component = true end
local old_component = new_component_storage[new_place]
new_component_storage[new_place] = new_component
if fragment_on_set then
__defer_call_hook(fragment_on_set, entity, fragment, new_component, old_component)
end
if fragment_on_assign then
__defer_call_hook(fragment_on_assign, entity, fragment, new_component, old_component)
end
end
else
local new_component = components[i]
if new_component == nil then new_component = fragment_default end
if new_component == nil then new_component = true end
for new_place = new_entity_count + 1, new_entity_count + old_entity_count do
local entity = new_entity_list[new_place]
local old_component = new_component_storage[new_place]
new_component_storage[new_place] = new_component
if fragment_on_set then
__defer_call_hook(fragment_on_set, entity, fragment, new_component, old_component)
end
if fragment_on_assign then
__defer_call_hook(fragment_on_assign, entity, fragment, new_component, old_component)
end
end
end
else
for new_place = new_entity_count + 1, new_entity_count + old_entity_count do
local entity = new_entity_list[new_place]
if fragment_on_set then
__defer_call_hook(fragment_on_set, entity, fragment)
end
if fragment_on_assign then
__defer_call_hook(fragment_on_assign, entity, fragment)
end
end
end
else
local new_component_index = new_component_indices[fragment]
if new_component_index then
local new_component_storage = new_component_storages[new_component_index]
if fragment_duplicate then
for new_place = new_entity_count + 1, new_entity_count + old_entity_count do
local new_component = components[i]
if new_component == nil then new_component = fragment_default end
if new_component ~= nil then new_component = fragment_duplicate(new_component) end
if new_component == nil then new_component = true end
new_component_storage[new_place] = new_component
end
else
local new_component = components[i]
if new_component == nil then new_component = fragment_default end
if new_component == nil then new_component = true end
for new_place = new_entity_count + 1, new_entity_count + old_entity_count do
new_component_storage[new_place] = new_component
end
end
else
-- nothing
end
end
else
inserted_set[fragment] = true
if fragment_on_set or fragment_on_insert then
local new_component_index = new_component_indices[fragment]
if new_component_index then
local new_component_storage = new_component_storages[new_component_index]
if fragment_duplicate then
for new_place = new_entity_count + 1, new_entity_count + old_entity_count do
local entity = new_entity_list[new_place]
local new_component = components[i]
if new_component == nil then new_component = fragment_default end
if new_component ~= nil then new_component = fragment_duplicate(new_component) end
if new_component == nil then new_component = true end
new_component_storage[new_place] = new_component
if fragment_on_set then
__defer_call_hook(fragment_on_set, entity, fragment, new_component)
end
if fragment_on_insert then
__defer_call_hook(fragment_on_insert, entity, fragment, new_component)
end
end
else
local new_component = components[i]
if new_component == nil then new_component = fragment_default end
if new_component == nil then new_component = true end
for new_place = new_entity_count + 1, new_entity_count + old_entity_count do
local entity = new_entity_list[new_place]
new_component_storage[new_place] = new_component
if fragment_on_set then
__defer_call_hook(fragment_on_set, entity, fragment, new_component)
end
if fragment_on_insert then
__defer_call_hook(fragment_on_insert, entity, fragment, new_component)
end
end
end
else
for new_place = new_entity_count + 1, new_entity_count + old_entity_count do
local entity = new_entity_list[new_place]
if fragment_on_set then
__defer_call_hook(fragment_on_set, entity, fragment)
end
if fragment_on_insert then
__defer_call_hook(fragment_on_insert, entity, fragment)
end
end
end
else
local new_component_index = new_component_indices[fragment]
if new_component_index then
local new_component_storage = new_component_storages[new_component_index]
if fragment_duplicate then
for new_place = new_entity_count + 1, new_entity_count + old_entity_count do
local new_component = components[i]
if new_component == nil then new_component = fragment_default end
if new_component ~= nil then new_component = fragment_duplicate(new_component) end
if new_component == nil then new_component = true end
new_component_storage[new_place] = new_component
end
else
local new_component = components[i]
if new_component == nil then new_component = fragment_default end
if new_component == nil then new_component = true end
for new_place = new_entity_count + 1, new_entity_count + old_entity_count do
new_component_storage[new_place] = new_component
end
end
else
-- nothing
end
end
end
end
__release_table(__table_pool_tag.fragment_set, inserted_set)
__structural_changes = __structural_changes + 1
end
end
---@param old_chunk evolved.chunk
---@param fragments evolved.fragment[]
---@param fragment_count integer
function __chunk_multi_remove(old_chunk, fragments, fragment_count)
if __defer_depth <= 0 then
__error_fmt('batched chunk operations should be deferred')
end
if fragment_count == 0 then
return
end
local new_chunk = __chunk_without_fragment_list(old_chunk, fragments, fragment_count)
if old_chunk == new_chunk then
return
end
local old_entity_list = old_chunk.__entity_list
local old_entity_count = old_chunk.__entity_count
if old_entity_count == 0 then
return
end
local old_fragment_set = old_chunk.__fragment_set
local old_component_indices = old_chunk.__component_indices
local old_component_storages = old_chunk.__component_storages
if old_chunk.__has_remove_hooks then
---@type table<evolved.fragment, boolean>
local removed_set = __acquire_table(__table_pool_tag.fragment_set)
for i = 1, fragment_count do
local fragment = fragments[i]
if not removed_set[fragment] and old_fragment_set[fragment] then
removed_set[fragment] = true
---@type evolved.remove_hook?
local fragment_on_remove = __evolved_get(fragment, __ON_REMOVE)
if fragment_on_remove then
local old_component_index = old_component_indices[fragment]
if old_component_index then
local old_component_storage = old_component_storages[old_component_index]
for old_place = 1, old_entity_count do
local entity = old_entity_list[old_place]
local old_component = old_component_storage[old_place]
__defer_call_hook(fragment_on_remove, entity, fragment, old_component)
end
else
for old_place = 1, old_entity_count do
local entity = old_entity_list[old_place]
__defer_call_hook(fragment_on_remove, entity, fragment)
end
end
end
end
end
__release_table(__table_pool_tag.fragment_set, removed_set)
end
if new_chunk then
local new_entity_list = new_chunk.__entity_list
local new_entity_count = new_chunk.__entity_count
local new_component_count = new_chunk.__component_count
local new_component_storages = new_chunk.__component_storages
local new_component_fragments = new_chunk.__component_fragments
if new_entity_count == 0 then
old_chunk.__entity_list, new_chunk.__entity_list =
new_entity_list, old_entity_list
old_entity_list, new_entity_list =
new_entity_list, old_entity_list
for new_ci = 1, new_component_count do
local new_f = new_component_fragments[new_ci]
local old_ci = old_component_indices[new_f]
old_component_storages[old_ci], new_component_storages[new_ci] =
new_component_storages[new_ci], old_component_storages[old_ci]
end
new_chunk.__entity_count = old_entity_count
else
__lua_table_move(
old_entity_list, 1, old_entity_count,
new_entity_count + 1, new_entity_list)
for new_ci = 1, new_component_count do
local new_f = new_component_fragments[new_ci]
local new_cs = new_component_storages[new_ci]
local old_ci = old_component_indices[new_f]
local old_cs = old_component_storages[old_ci]
__lua_table_move(old_cs, 1, old_entity_count, new_entity_count + 1, new_cs)
end
new_chunk.__entity_count = new_entity_count + old_entity_count
end
do
local entity_chunks = __entity_chunks
local entity_places = __entity_places
for new_place = new_entity_count + 1, new_entity_count + old_entity_count do
local entity = new_entity_list[new_place]
local entity_index = entity % 0x100000
entity_chunks[entity_index] = new_chunk
entity_places[entity_index] = new_place
end
__detach_all_entities(old_chunk)
end
else
local entity_chunks = __entity_chunks
local entity_places = __entity_places
for old_place = 1, old_entity_count do
local entity = old_entity_list[old_place]
local entity_index = entity % 0x100000
entity_chunks[entity_index] = nil
entity_places[entity_index] = nil
end
__detach_all_entities(old_chunk)
end
__structural_changes = __structural_changes + 1
end
---
---
---