merge chunk_assign and chunk_insert

This commit is contained in:
BlackMATov
2025-03-20 05:35:39 +07:00
parent 34c00d8c9a
commit 0b3e9fdc48

View File

@@ -1829,8 +1829,6 @@ end
---
local __chunk_set
local __chunk_assign
local __chunk_insert
local __chunk_remove
local __chunk_clear
local __chunk_destroy
@@ -1935,164 +1933,19 @@ end
---
---
---@param chunk evolved.chunk
---@param old_chunk evolved.chunk
---@param fragment evolved.fragment
---@param ... any component arguments
---@return integer set_count
---@nodiscard
__chunk_set = function(chunk, fragment, ...)
if __defer_depth <= 0 then
__error_fmt('batched chunk operations should be deferred')
end
if chunk.__fragment_set[fragment] then
return __chunk_assign(chunk, fragment, ...)
else
return __chunk_insert(chunk, fragment, ...)
end
end
---@param chunk evolved.chunk
---@param fragment evolved.fragment
---@param ... any component arguments
---@return integer assigned_count
---@nodiscard
__chunk_assign = function(chunk, fragment, ...)
if __defer_depth <= 0 then
__error_fmt('batched chunk operations should be deferred')
end
if not chunk.__fragment_set[fragment] then
return 0
end
local chunk_entity_list = chunk.__entity_list
local chunk_entity_count = chunk.__entity_count
if chunk_entity_count == 0 then
return 0
end
local chunk_component_indices = chunk.__component_indices
local chunk_component_storages = chunk.__component_storages
---@type evolved.default?, evolved.construct?
local fragment_default, fragment_construct
---@type evolved.set_hook?, evolved.assign_hook?
local fragment_on_set, fragment_on_assign
do
if chunk.__has_defaults_or_constructs then
fragment_default, fragment_construct = __evolved_get(fragment, __DEFAULT, __CONSTRUCT)
end
if chunk.__has_set_or_assign_hooks then
fragment_on_set, fragment_on_assign = __evolved_get(fragment, __ON_SET, __ON_ASSIGN)
end
end
if fragment_on_set or fragment_on_assign then
local component_index = chunk_component_indices[fragment]
if component_index then
local component_storage = chunk_component_storages[component_index]
if fragment_default ~= nil or fragment_construct then
for place = 1, chunk_entity_count do
local entity = chunk_entity_list[place]
local old_component = component_storage[place]
local new_component = ...
if fragment_construct then new_component = fragment_construct(...) end
if new_component == nil then new_component = fragment_default end
if new_component == nil then new_component = true end
component_storage[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 = ...
if new_component == nil then new_component = true end
for place = 1, chunk_entity_count do
local entity = chunk_entity_list[place]
local old_component = component_storage[place]
component_storage[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 place = 1, chunk_entity_count do
local entity = chunk_entity_list[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 component_index = chunk_component_indices[fragment]
if component_index then
local component_storage = chunk_component_storages[component_index]
if fragment_default ~= nil or fragment_construct then
for place = 1, chunk_entity_count do
local new_component = ...
if fragment_construct then new_component = fragment_construct(...) end
if new_component == nil then new_component = fragment_default end
if new_component == nil then new_component = true end
component_storage[place] = new_component
end
else
local new_component = ...
if new_component == nil then new_component = true end
for place = 1, chunk_entity_count do
component_storage[place] = new_component
end
end
else
-- nothing
end
end
return chunk_entity_count
end
---@param old_chunk evolved.chunk
---@param fragment evolved.fragment
---@param ... any component arguments
---@return integer inserted_count
---@nodiscard
__chunk_insert = function(old_chunk, fragment, ...)
__chunk_set = function(old_chunk, fragment, ...)
if __defer_depth <= 0 then
__error_fmt('batched chunk operations should be deferred')
end
local new_chunk = __chunk_with_fragment(old_chunk, fragment)
if not new_chunk or old_chunk == new_chunk then
if not new_chunk then
return 0
end
@@ -2104,9 +1957,112 @@ __chunk_insert = function(old_chunk, fragment, ...)
end
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
---@type evolved.default?, evolved.construct?
local fragment_default, fragment_construct
---@type evolved.set_hook?, evolved.assign_hook?
local fragment_on_set, fragment_on_assign
do
if old_chunk.__has_defaults_or_constructs then
fragment_default, fragment_construct = __evolved_get(fragment, __DEFAULT, __CONSTRUCT)
end
if old_chunk.__has_set_or_assign_hooks then
fragment_on_set, fragment_on_assign = __evolved_get(fragment, __ON_SET, __ON_ASSIGN)
end
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_default ~= nil or fragment_construct then
for place = 1, old_entity_count do
local entity = old_entity_list[place]
local old_component = old_component_storage[place]
local new_component = ...
if fragment_construct then new_component = fragment_construct(...) end
if new_component == nil then new_component = fragment_default end
if new_component == nil then new_component = true end
old_component_storage[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 = ...
if new_component == nil then new_component = true end
for place = 1, old_entity_count do
local entity = old_entity_list[place]
local old_component = old_component_storage[place]
old_component_storage[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 place = 1, old_entity_count do
local entity = old_entity_list[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_default ~= nil or fragment_construct then
for place = 1, old_entity_count do
local new_component = ...
if fragment_construct then new_component = fragment_construct(...) end
if new_component == nil then new_component = fragment_default end
if new_component == nil then new_component = true end
old_component_storage[place] = new_component
end
else
local new_component = ...
if new_component == nil then new_component = true end
for place = 1, old_entity_count do
old_component_storage[place] = new_component
end
end
else
-- nothing
end
end
else
local new_entity_list = new_chunk.__entity_list
local new_entity_count = new_chunk.__entity_count
@@ -2258,6 +2214,8 @@ __chunk_insert = function(old_chunk, fragment, ...)
end
__structural_changes = __structural_changes + 1
end
return old_entity_count
end
@@ -2755,6 +2713,7 @@ __chunk_multi_set = function(old_chunk, fragments, fragment_count, components)
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]