diff --git a/README.md b/README.md index 08a2d11..88d74fe 100644 --- a/README.md +++ b/README.md @@ -78,8 +78,7 @@ has :: chunk | entity, fragment -> boolean has_all :: chunk | entity, fragment... -> boolean has_any :: chunk | entity, fragment... -> boolean -get :: entity, fragment -> component -get_all :: entity, fragment... -> component... +get :: entity, fragment... -> component... set :: entity, fragment, component -> () remove :: entity, fragment... -> () @@ -123,8 +122,7 @@ builder :: builder builder:has :: fragment -> boolean builder:has_all :: fragment... -> boolean builder:has_any :: fragment... -> boolean -builder:get :: fragment -> component -builder:get_all :: fragment... -> component... +builder:get :: fragment... -> component... builder:set :: fragment, component -> builder builder:remove :: fragment -> builder builder:clear :: builder diff --git a/develop/untests.lua b/develop/untests.lua index 00b3244..def7296 100644 --- a/develop/untests.lua +++ b/develop/untests.lua @@ -117,7 +117,7 @@ do assert(evo.get(e, f1) == nil) assert(evo.get(e, f2) == nil) - local c1, c2 = evo.get_all(e, f1, f2) + local c1, c2 = evo.get(e, f1, f2) assert(c1 == nil and c2 == nil) end @@ -134,7 +134,7 @@ do assert(evo.get(e, f1) == 41) assert(evo.get(e, f2) == nil) - local c1, c2 = evo.get_all(e, f1, f2) + local c1, c2 = evo.get(e, f1, f2) assert(c1 == 41 and c2 == nil) end @@ -151,7 +151,7 @@ do assert(evo.get(e, f1) == 41) assert(evo.get(e, f2) == 42) - local c1, c2 = evo.get_all(e, f1, f2) + local c1, c2 = evo.get(e, f1, f2) assert(c1 == 41 and c2 == 42) end end @@ -166,7 +166,7 @@ do do assert(evo.has_all(e, f1, f2)) - local c1, c2 = evo.get_all(e, f1, f2) + local c1, c2 = evo.get(e, f1, f2) assert(c1 == 41 and c2 == 42) end end @@ -184,7 +184,7 @@ do assert(not evo.has(e, f1)) assert(evo.has(e, f2)) - local c1, c2 = evo.get_all(e, f1, f2) + local c1, c2 = evo.get(e, f1, f2) assert(c1 == nil and c2 == 42) end @@ -198,7 +198,7 @@ do assert(evo.has(e, f1)) assert(not evo.has(e, f2)) - local c1, c2 = evo.get_all(e, f1, f2) + local c1, c2 = evo.get(e, f1, f2) assert(c1 == 41 and c2 == nil) end @@ -211,7 +211,7 @@ do assert(not evo.has_any(e, f1, f2)) - local c1, c2 = evo.get_all(e, f1, f2) + local c1, c2 = evo.get(e, f1, f2) assert(c1 == nil and c2 == nil) end end @@ -3066,7 +3066,7 @@ do do local q = qb:build() - local includes, excludes = evo.get_all(q, evo.INCLUDES, evo.EXCLUDES) + local includes, excludes = evo.get(q, evo.INCLUDES, evo.EXCLUDES) assert(includes == nil) assert(excludes == nil) end @@ -3074,7 +3074,7 @@ do do local q = qb:include(f1):build() - local includes, excludes = evo.get_all(q, evo.INCLUDES, evo.EXCLUDES) + local includes, excludes = evo.get(q, evo.INCLUDES, evo.EXCLUDES) assert(#includes == 1 and includes[1] == f1) assert(excludes == nil) end @@ -3082,7 +3082,7 @@ do do local q = qb:include(f1, f2):build() - local includes, excludes = evo.get_all(q, evo.INCLUDES, evo.EXCLUDES) + local includes, excludes = evo.get(q, evo.INCLUDES, evo.EXCLUDES) assert(#includes == 2 and includes[1] == f1 and includes[2] == f2) assert(excludes == nil) end @@ -3090,7 +3090,7 @@ do do local q = qb:include(f1):include(f2):build() - local includes, excludes = evo.get_all(q, evo.INCLUDES, evo.EXCLUDES) + local includes, excludes = evo.get(q, evo.INCLUDES, evo.EXCLUDES) assert(#includes == 2 and includes[1] == f1 and includes[2] == f2) assert(excludes == nil) end @@ -3098,7 +3098,7 @@ do do local q = qb:exclude(f1):build() - local includes, excludes = evo.get_all(q, evo.INCLUDES, evo.EXCLUDES) + local includes, excludes = evo.get(q, evo.INCLUDES, evo.EXCLUDES) assert(includes == nil) assert(#excludes == 1 and excludes[1] == f1) end @@ -3106,7 +3106,7 @@ do do local q = qb:exclude(f1, f2):build() - local includes, excludes = evo.get_all(q, evo.INCLUDES, evo.EXCLUDES) + local includes, excludes = evo.get(q, evo.INCLUDES, evo.EXCLUDES) assert(includes == nil) assert(#excludes == 2 and excludes[1] == f1 and excludes[2] == f2) end @@ -3114,7 +3114,7 @@ do do local q = qb:exclude(f1):exclude(f2):build() - local includes, excludes = evo.get_all(q, evo.INCLUDES, evo.EXCLUDES) + local includes, excludes = evo.get(q, evo.INCLUDES, evo.EXCLUDES) assert(includes == nil) assert(#excludes == 2 and excludes[1] == f1 and excludes[2] == f2) end @@ -3125,7 +3125,7 @@ do local q = qb:build() - local includes, excludes = evo.get_all(q, evo.INCLUDES, evo.EXCLUDES) + local includes, excludes = evo.get(q, evo.INCLUDES, evo.EXCLUDES) assert(#includes == 1 and includes[1] == f1) assert(#excludes == 1 and excludes[1] == f2) end @@ -4730,31 +4730,31 @@ do assert(c1 == 11) end do - local c1, c2 = evo.get_all(e, f1, f2) + local c1, c2 = evo.get(e, f1, f2) assert(c1 == 11 and c2 == 22) end do - local c2, c1 = evo.get_all(e, f2, f1) + local c2, c1 = evo.get(e, f2, f1) assert(c1 == 11 and c2 == 22) end do - local c1, c2, c3 = evo.get_all(e, f1, f2, f3) + local c1, c2, c3 = evo.get(e, f1, f2, f3) assert(c1 == 11 and c2 == 22 and c3 == 33) end do - local c3, c2, c1 = evo.get_all(e, f3, f2, f1) + local c3, c2, c1 = evo.get(e, f3, f2, f1) assert(c1 == 11 and c2 == 22 and c3 == 33) end do - local c1, c2, c3, c4 = evo.get_all(e, f1, f2, f3, f4) + local c1, c2, c3, c4 = evo.get(e, f1, f2, f3, f4) assert(c1 == 11 and c2 == 22 and c3 == 33 and c4 == 44) end do - local c1, c2, c3, c4, c5 = evo.get_all(e, f1, f2, f3, f4, f5) + local c1, c2, c3, c4, c5 = evo.get(e, f1, f2, f3, f4, f5) assert(c1 == 11 and c2 == 22 and c3 == 33 and c4 == 44 and c5 == 55) end do - local c5, c4, c3, c2, c1 = evo.get_all(e, f5, f4, f3, f2, f1) + local c5, c4, c3, c2, c1 = evo.get(e, f5, f4, f3, f2, f1) assert(c1 == 11 and c2 == 22 and c3 == 33 and c4 == 44 and c5 == 55) end end @@ -8701,71 +8701,91 @@ do end do - local f1, f2, f3, f4, f5 = evo.id(5) + local f0, f1, f2, f3, f4, f5 = evo.id(6) do local b = evo.builder() do - assert(b:get_all() == nil) + assert(b:get() == nil) end do - local c1 = b:get_all(f1) + local c1 = b:get(f1) assert(c1 == nil) end do - local c1, c2 = b:get_all(f1, f2) + local c1, c2 = b:get(f1, f2) assert(c1 == nil and c2 == nil) end do - local c1, c2, c3 = b:get_all(f1, f2, f3) + local c1, c2, c3 = b:get(f1, f2, f3) assert(c1 == nil and c2 == nil and c3 == nil) end do - local c1, c2, c3, c4 = b:get_all(f1, f2, f3, f4) + local c1, c2, c3, c4 = b:get(f1, f2, f3, f4) assert(c1 == nil and c2 == nil and c3 == nil and c4 == nil) end do - local c1, c2, c3, c4, c5 = b:get_all(f1, f2, f3, f4, f5) + local c1, c2, c3, c4, c5 = b:get(f1, f2, f3, f4, f5) assert(c1 == nil and c2 == nil and c3 == nil and c4 == nil and c5 == nil) end + + do + local c0, c1, c2, c3, c4, c5 = b:get(f0, f1, f2, f3, f4, f5) + assert(c0 == nil and c1 == nil and c2 == nil and c3 == nil and c4 == nil and c5 == nil) + end end do local b = evo.builder():set(f1, 11):set(f2, 22):set(f3, 33):set(f4, 44):set(f5, 55) do - assert(b:get_all() == nil) + assert(b:get() == nil) end do - local c1 = b:get_all(f1) + local c1 = b:get(f1) assert(c1 == 11) end do - local c1, c2 = b:get_all(f1, f2) + local c1, c2 = b:get(f1, f2) assert(c1 == 11 and c2 == 22) end do - local c1, c2, c3 = b:get_all(f1, f2, f3) + local c1, c2, c3 = b:get(f1, f2, f3) assert(c1 == 11 and c2 == 22 and c3 == 33) end do - local c1, c2, c3, c4 = b:get_all(f1, f2, f3, f4) + local c1, c2, c3, c4 = b:get(f1, f2, f3, f4) assert(c1 == 11 and c2 == 22 and c3 == 33 and c4 == 44) end do - local c1, c2, c3, c4, c5 = b:get_all(f1, f2, f3, f4, f5) + local c1, c2, c3, c4, c5 = b:get(f1, f2, f3, f4, f5) assert(c1 == 11 and c2 == 22 and c3 == 33 and c4 == 44 and c5 == 55) end + + do + local c0, c1, c2, c3, c4, c5 = b:get(f0, f1, f2, f3, f4, f5) + assert(c0 == nil and c1 == 11 and c2 == 22 and c3 == 33 and c4 == 44 and c5 == 55) + end + + do + local c1, c0, c2, c3, c4, c5 = b:get(f1, f0, f2, f3, f4, f5) + assert(c0 == nil and c1 == 11 and c2 == 22 and c3 == 33 and c4 == 44 and c5 == 55) + end + + do + local c5, c4, c3, c2, c1, c0 = b:get(f5, f4, f3, f2, f1, f0) + assert(c0 == nil and c1 == 11 and c2 == 22 and c3 == 33 and c4 == 44 and c5 == 55) + end end end diff --git a/evolved.lua b/evolved.lua index 5ea9911..bacb122 100644 --- a/evolved.lua +++ b/evolved.lua @@ -724,7 +724,6 @@ local __evolved_has_all local __evolved_has_any local __evolved_get -local __evolved_get_all local __evolved_set local __evolved_remove @@ -1537,16 +1536,6 @@ local function __chunk_has_any_fragment_list(chunk, fragment_list, fragment_coun return false end ----@param chunk evolved.chunk ----@param place integer ----@param fragment evolved.fragment ----@return evolved.component ----@nodiscard -local function __chunk_get_component(chunk, place, fragment) - local component_index = chunk.__component_indices[fragment] - return component_index and chunk.__component_storages[component_index][place] -end - ---@param chunk evolved.chunk ---@param place integer ---@param ... evolved.fragment fragments @@ -1735,7 +1724,7 @@ local function __spawn_entity_at(entity, chunk, fragment_list, fragment_count, c ---@type evolved.default?, evolved.duplicate? local fragment_default, fragment_duplicate = - __evolved_get_all(fragment, __DEFAULT, __DUPLICATE) + __evolved_get(fragment, __DEFAULT, __DUPLICATE) local new_component = fragment_default @@ -1769,7 +1758,7 @@ local function __spawn_entity_at(entity, chunk, fragment_list, fragment_count, c if component_index then ---@type evolved.default?, evolved.duplicate? local fragment_default, fragment_duplicate = - __evolved_get_all(fragment, __DEFAULT, __DUPLICATE) + __evolved_get(fragment, __DEFAULT, __DUPLICATE) local new_component = component_list[i] @@ -1818,7 +1807,7 @@ local function __spawn_entity_at(entity, chunk, fragment_list, fragment_count, c ---@type evolved.set_hook?, evolved.insert_hook? local fragment_on_set, fragment_on_insert = - __evolved_get_all(fragment, __ON_SET, __ON_INSERT) + __evolved_get(fragment, __ON_SET, __ON_INSERT) local component_index = chunk_component_indices[fragment] @@ -1947,7 +1936,7 @@ local function __spawn_entity_as(entity, prefab, fragment_list, fragment_count, if component_index then ---@type evolved.default?, evolved.duplicate? local fragment_default, fragment_duplicate = - __evolved_get_all(fragment, __DEFAULT, __DUPLICATE) + __evolved_get(fragment, __DEFAULT, __DUPLICATE) local new_component = component_list[i] @@ -1996,7 +1985,7 @@ local function __spawn_entity_as(entity, prefab, fragment_list, fragment_count, ---@type evolved.set_hook?, evolved.insert_hook? local fragment_on_set, fragment_on_insert = - __evolved_get_all(fragment, __ON_SET, __ON_INSERT) + __evolved_get(fragment, __ON_SET, __ON_INSERT) local component_index = chunk_component_indices[fragment] @@ -2067,7 +2056,7 @@ local function __spawn_entity_with(entity, chunk, fragment_list, fragment_count, if component_index then ---@type evolved.default?, evolved.duplicate? local fragment_default, fragment_duplicate = - __evolved_get_all(fragment, __DEFAULT, __DUPLICATE) + __evolved_get(fragment, __DEFAULT, __DUPLICATE) local new_component = component_list[i] @@ -2116,7 +2105,7 @@ local function __spawn_entity_with(entity, chunk, fragment_list, fragment_count, ---@type evolved.set_hook?, evolved.insert_hook? local fragment_on_set, fragment_on_insert = - __evolved_get_all(fragment, __ON_SET, __ON_INSERT) + __evolved_get(fragment, __ON_SET, __ON_INSERT) local component_index = chunk_component_indices[fragment] @@ -2469,7 +2458,7 @@ function __chunk_set(old_chunk, fragment, component) 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_all(fragment, __DEFAULT, __DUPLICATE, __ON_SET, __ON_ASSIGN) + __evolved_get(fragment, __DEFAULT, __DUPLICATE, __ON_SET, __ON_ASSIGN) end if fragment_on_set or fragment_on_assign then @@ -2572,7 +2561,7 @@ function __chunk_set(old_chunk, fragment, component) if new_chunk_has_setup_hooks or new_chunk_has_insert_hooks then fragment_default, fragment_duplicate, fragment_on_set, fragment_on_insert = - __evolved_get_all(fragment, __DEFAULT, __DUPLICATE, __ON_SET, __ON_INSERT) + __evolved_get(fragment, __DEFAULT, __DUPLICATE, __ON_SET, __ON_INSERT) end if new_entity_count == 0 then @@ -2952,7 +2941,7 @@ function __chunk_multi_set(old_chunk, fragments, fragment_count, components) 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_all(fragment, __DEFAULT, __DUPLICATE, __ON_SET, __ON_ASSIGN) + __evolved_get(fragment, __DEFAULT, __DUPLICATE, __ON_SET, __ON_ASSIGN) end if fragment_on_set or fragment_on_assign then @@ -3108,7 +3097,7 @@ function __chunk_multi_set(old_chunk, fragments, fragment_count, components) 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_all(fragment, __DEFAULT, __DUPLICATE, __ON_SET, __ON_ASSIGN, __ON_INSERT) + __evolved_get(fragment, __DEFAULT, __DUPLICATE, __ON_SET, __ON_ASSIGN, __ON_INSERT) end if inserted_set[fragment] or old_fragment_set[fragment] then @@ -3435,7 +3424,7 @@ end ---@param system evolved.system local function __system_process(system) - local query, execute, prologue, epilogue = __evolved_get_all(system, + local query, execute, prologue, epilogue = __evolved_get(system, __QUERY, __EXECUTE, __PROLOGUE, __EPILOGUE) if prologue then @@ -4728,32 +4717,11 @@ function __evolved_has_any(chunk_or_entity, ...) end end ----@param entity evolved.entity ----@param fragment evolved.fragment ----@return evolved.component ----@nodiscard -function __evolved_get(entity, fragment) - local entity_index = entity % 0x100000 - - if __freelist_ids[entity_index] ~= entity then - return - end - - local chunk = __entity_chunks[entity_index] - - if not chunk then - return - end - - local place = __entity_places[entity_index] - return __chunk_get_component(chunk, place, fragment) -end - ---@param entity evolved.entity ---@param ... evolved.fragment fragments ---@return evolved.component ... components ---@nodiscard -function __evolved_get_all(entity, ...) +function __evolved_get(entity, ...) local entity_index = entity % 0x100000 if __freelist_ids[entity_index] ~= entity then @@ -4812,7 +4780,7 @@ function __evolved_set(entity, fragment, component) 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_all(fragment, __DEFAULT, __DUPLICATE, __ON_SET, __ON_ASSIGN) + __evolved_get(fragment, __DEFAULT, __DUPLICATE, __ON_SET, __ON_ASSIGN) end local old_component_index = old_component_indices[fragment] @@ -4859,7 +4827,7 @@ function __evolved_set(entity, fragment, component) if new_chunk_has_setup_hooks or new_chunk_has_insert_hooks then fragment_default, fragment_duplicate, fragment_on_set, fragment_on_insert = - __evolved_get_all(fragment, __DEFAULT, __DUPLICATE, __ON_SET, __ON_INSERT) + __evolved_get(fragment, __DEFAULT, __DUPLICATE, __ON_SET, __ON_INSERT) end local new_place = new_entity_count + 1 @@ -5215,7 +5183,7 @@ function __evolved_multi_set(entity, fragments, components) 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_all(fragment, __DEFAULT, __DUPLICATE, __ON_SET, __ON_ASSIGN) + __evolved_get(fragment, __DEFAULT, __DUPLICATE, __ON_SET, __ON_ASSIGN) end local old_component_index = old_component_indices[fragment] @@ -5304,7 +5272,7 @@ function __evolved_multi_set(entity, fragments, components) 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_all(fragment, __DEFAULT, __DUPLICATE, __ON_SET, __ON_ASSIGN, __ON_INSERT) + __evolved_get(fragment, __DEFAULT, __DUPLICATE, __ON_SET, __ON_ASSIGN, __ON_INSERT) end if inserted_set[fragment] or old_fragment_set[fragment] then @@ -6332,31 +6300,10 @@ function __evolved_builder_mt:has_any(...) end end ----@param fragment evolved.fragment ----@return evolved.component ----@nodiscard -function __evolved_builder_mt:get(fragment) - local component_index = self.__fragment_set[fragment] - - if not component_index then - return - end - - if component_index > self.__component_count then - return - end - - if fragment ~= self.__fragment_list[component_index] then - return - end - - return self.__component_list[component_index] -end - ---@param ... evolved.fragment fragments ---@return evolved.component ... components ---@nodiscard -function __evolved_builder_mt:get_all(...) +function __evolved_builder_mt:get(...) local fragment_count = select("#", ...) if fragment_count == 0 then @@ -6364,33 +6311,24 @@ function __evolved_builder_mt:get_all(...) end local get = self.get - local get_all = self.get_all - if fragment_count == 1 then - local f1 = ... - return get(self, f1) + local fragment = ... + + local component_index = self.__fragment_set[fragment] + + if not component_index then + return nil, get(self, __lua_select(2, ...)) end - if fragment_count == 2 then - local f1, f2 = ... - return get(self, f1), get(self, f2) + if component_index > self.__component_count then + return nil, get(self, __lua_select(2, ...)) end - if fragment_count == 3 then - local f1, f2, f3 = ... - return get(self, f1), get(self, f2), get(self, f3) + if fragment ~= self.__fragment_list[component_index] then + return nil, get(self, __lua_select(2, ...)) end - if fragment_count == 4 then - local f1, f2, f3, f4 = ... - return get(self, f1), get(self, f2), get(self, f3), get(self, f4) - end - - do - local f1, f2, f3, f4 = ... - return get(self, f1), get(self, f2), get(self, f3), get(self, f4), - get_all(self, __lua_select(5, ...)) - end + return self.__component_list[component_index], get(self, __lua_select(2, ...)) end ---@param fragment evolved.fragment @@ -6404,7 +6342,7 @@ function __evolved_builder_mt:set(fragment, component) do ---@type evolved.default?, evolved.duplicate? local fragment_default, fragment_duplicate = - __evolved_get_all(fragment, __DEFAULT, __DUPLICATE) + __evolved_get(fragment, __DEFAULT, __DUPLICATE) if component == nil then component = fragment_default @@ -6795,7 +6733,7 @@ local function __update_chunk_tags_trace(chunk, fragment) ---@type evolved.default?, evolved.duplicate? local fragment_default, fragment_duplicate = - __evolved_get_all(fragment, __DEFAULT, __DUPLICATE) + __evolved_get(fragment, __DEFAULT, __DUPLICATE) if fragment_duplicate then for place = 1, chunk.__entity_count do @@ -7050,7 +6988,6 @@ evolved.is_empty_all = __evolved_is_empty_all evolved.is_empty_any = __evolved_is_empty_any evolved.get = __evolved_get -evolved.get_all = __evolved_get_all evolved.has = __evolved_has evolved.has_all = __evolved_has_all