From e89b91fd18dae23c268d7eb827fd577bb686f72c Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Mon, 21 Apr 2025 00:31:06 +0700 Subject: [PATCH] remove multi chunk_or_entity api --- README.md | 55 ++- develop/example.lua | 6 +- develop/fuzzing/batch_destroy_fuzz.lua | 4 +- develop/fuzzing/destroy_fuzz.lua | 4 +- develop/untests.lua | 405 +++++++--------- develop/usbench.lua | 24 +- evolved.lua | 619 +++++++++++++------------ 7 files changed, 540 insertions(+), 577 deletions(-) diff --git a/README.md b/README.md index 91efb42..9eaaac5 100644 --- a/README.md +++ b/README.md @@ -66,17 +66,17 @@ unpack :: id -> integer, integer defer :: boolean commit :: boolean -is_alive :: chunk | entity -> boolean -is_alive_all :: chunk | entity... -> boolean -is_alive_any :: chunk | entity... -> boolean +is_alive :: entity -> boolean +is_alive_all :: entity... -> boolean +is_alive_any :: entity... -> boolean -is_empty :: chunk | entity -> boolean -is_empty_all :: chunk | entity... -> boolean -is_empty_any :: chunk | entity... -> boolean +is_empty :: entity -> boolean +is_empty_all :: entity... -> boolean +is_empty_any :: entity... -> boolean -has :: chunk | entity, fragment -> boolean -has_all :: chunk | entity, fragment... -> boolean -has_any :: chunk | entity, fragment... -> boolean +has :: entity, fragment -> boolean +has_all :: entity, fragment... -> boolean +has_any :: entity, fragment... -> boolean get :: entity, fragment... -> component... @@ -90,12 +90,6 @@ batch_remove :: query, fragment... -> () batch_clear :: query... -> () batch_destroy :: query... -> () -chunk :: fragment, fragment... -> chunk, entity[], integer - -entities :: chunk -> entity[], integer -fragments :: chunk -> fragment[], integer -components :: chunk, fragment... -> component[]... - each :: entity -> {each_state? -> fragment?, component?}, each_state? execute :: query -> {execute_state? -> chunk?, entity[]?, integer?}, execute_state? @@ -109,36 +103,67 @@ debug_mode :: boolean -> () collect_garbage :: () ``` +## Chunk + +``` +chunk :: fragment, fragment... -> chunk, entity[], integer + +chunk:is_alive :: boolean +chunk:is_empty :: boolean + +chunk:has :: fragment -> boolean +chunk:has_all :: fragment... -> boolean +chunk:has_any :: fragment... -> boolean + +chunk:entities :: entity[], integer +chunk:fragments :: fragment[], integer +chunk:components :: fragment... -> component[]... +``` + ## Builder ``` builder :: builder + builder:has :: fragment -> boolean builder:has_all :: fragment... -> boolean builder:has_any :: fragment... -> boolean + builder:get :: fragment... -> component... + builder:set :: fragment, component -> builder builder:remove :: fragment... -> builder builder:clear :: builder + builder:tag :: builder builder:name :: string -> builder + builder:prefab :: entity -> builder builder:single :: component -> builder + builder:default :: component -> builder builder:duplicate :: {component -> component} -> builder + builder:include :: fragment... -> builder builder:exclude :: fragment... -> builder + builder:on_set :: {entity, fragment, component, component?} -> builder builder:on_assign :: {entity, fragment, component, component} -> builder builder:on_insert :: {entity, fragment, component} -> builder builder:on_remove :: {entity, fragment} -> builder + builder:group :: system -> builder + builder:query :: query -> builder builder:execute :: {chunk, entity[], integer} -> builder + builder:prologue :: {} -> builder builder:epilogue :: {} -> builder + builder:disabled :: builder + builder:destroy_policy :: id -> builder + builder:build :: boolean -> entity ``` diff --git a/develop/example.lua b/develop/example.lua index 22cc32a..f5aec9c 100644 --- a/develop/example.lua +++ b/develop/example.lua @@ -62,7 +62,7 @@ local integrate_forces_system = evo.builder() evo.get(singles.physics_gravity, singles.physics_gravity) ---@type evolved.vector2[], evolved.vector2[] - local forces, velocities = evo.components(chunk, + local forces, velocities = chunk:components( fragments.force, fragments.velocity) for i = 1, entity_count do @@ -82,7 +82,7 @@ local integrate_velocities_system = evo.builder() evo.get(singles.delta_time, singles.delta_time) ---@type evolved.vector2[], evolved.vector2[], evolved.vector2[] - local forces, positions, velocities = evo.components(chunk, + local forces, positions, velocities = chunk:components( fragments.force, fragments.position, fragments.velocity) for i = 1, entity_count do @@ -101,7 +101,7 @@ local graphics_system = evo.builder() :query(queries.physics_bodies) :execute(function(chunk, entities, entity_count) ---@type evolved.vector2[] - local positions = evo.components(chunk, + local positions = chunk:components( fragments.position) for i = 1, entity_count do diff --git a/develop/fuzzing/batch_destroy_fuzz.lua b/develop/fuzzing/batch_destroy_fuzz.lua index b8b4968..7ac0238 100644 --- a/develop/fuzzing/batch_destroy_fuzz.lua +++ b/develop/fuzzing/batch_destroy_fuzz.lua @@ -101,8 +101,8 @@ end local all_chunk_query = evo.builder():build() for chunk in evo.execute(all_chunk_query) do - assert(not evo.has_any(chunk, __table_unpack(should_be_destroyed_entity_list))) - for _, fragment in ipairs(evo.fragments(chunk)) do + assert(not chunk:has_any(__table_unpack(should_be_destroyed_entity_list))) + for _, fragment in ipairs(chunk:fragments()) do assert(not evo.has_all(fragment, __table_unpack(destroying_include_list))) end end diff --git a/develop/fuzzing/destroy_fuzz.lua b/develop/fuzzing/destroy_fuzz.lua index e9b4707..069cbca 100644 --- a/develop/fuzzing/destroy_fuzz.lua +++ b/develop/fuzzing/destroy_fuzz.lua @@ -105,8 +105,8 @@ end local all_chunk_query = evo.builder():build() for chunk in evo.execute(all_chunk_query) do - assert(not evo.has_any(chunk, __table_unpack(destroying_entity_list))) - assert(not evo.has_any(chunk, __table_unpack(should_be_destroyed_entity_list))) + assert(not chunk:has_any(__table_unpack(destroying_entity_list))) + assert(not chunk:has_any(__table_unpack(should_be_destroyed_entity_list))) end for _, destroying_entity in ipairs(destroying_entity_list) do diff --git a/develop/untests.lua b/develop/untests.lua index 8dd5742..2fb1158 100644 --- a/develop/untests.lua +++ b/develop/untests.lua @@ -594,7 +594,7 @@ do do local chunk, entities = evo.chunk(f1) assert(entities and entities[1] == e1) - assert(chunk and evo.components(chunk, f1)[1] == 41) + assert(chunk and chunk:components(f1)[1] == 41) end do @@ -606,8 +606,8 @@ do assert(chunk == evo.chunk(f2, f1)) assert(chunk == evo.chunk(f2, f1, f2, f1)) assert(entities and entities[1] == e2 and entities[2] == e2b) - assert(chunk and evo.components(chunk, f1)[1] == 42 and evo.components(chunk, f2)[1] == 43) - assert(chunk and evo.components(chunk, f1)[2] == 44 and evo.components(chunk, f2)[2] == 45) + assert(chunk and chunk:components(f1)[1] == 42 and chunk:components(f2)[1] == 43) + assert(chunk and chunk:components(f1)[2] == 44 and chunk:components(f2)[2] == 45) end do @@ -1863,9 +1863,9 @@ do assert(chunk_entities[1] == e3 and chunk_entities[2] == e1) - assert(#evo.components(chunk, f1) == 0) - assert(#evo.components(chunk, f2) == 0) - assert(evo.components(chunk, f3)[1] == 43 and evo.components(chunk, f3)[2] == 50) + assert(#chunk:components(f1) == 0) + assert(#chunk:components(f2) == 0) + assert(chunk:components(f3)[1] == 43 and chunk:components(f3)[2] == 50) end end end @@ -1904,9 +1904,9 @@ do assert(chunk and chunk_entities) assert(chunk_entities[1] == e1) - assert(#evo.components(chunk, f1) == 0) - assert(#evo.components(chunk, f2) == 0) - assert(#evo.components(chunk, f3) == 0) + assert(#chunk:components(f1) == 0) + assert(#chunk:components(f2) == 0) + assert(#chunk:components(f3) == 0) end do @@ -1914,9 +1914,9 @@ do assert(chunk and chunk_entities) assert(chunk_entities[1] == e3) - assert(#evo.components(chunk, f1) == 0) - assert(#evo.components(chunk, f2) == 0) - assert(evo.components(chunk, f3)[1] == 43) + assert(#chunk:components(f1) == 0) + assert(#chunk:components(f2) == 0) + assert(chunk:components(f3)[1] == 43) end end end @@ -1959,9 +1959,9 @@ do assert(chunk and chunk_entities) assert(next(chunk_entities) == nil) - assert(#evo.components(chunk, f1) == 0) - assert(#evo.components(chunk, f2) == 0) - assert(#evo.components(chunk, f3) == 0) + assert(#chunk:components(f1) == 0) + assert(#chunk:components(f2) == 0) + assert(#chunk:components(f3) == 0) end end end @@ -2004,9 +2004,9 @@ do assert(chunk and chunk_entities) assert(next(chunk_entities) == nil) - assert(#evo.components(chunk, f1) == 0) - assert(#evo.components(chunk, f2) == 0) - assert(#evo.components(chunk, f3) == 0) + assert(#chunk:components(f1) == 0) + assert(#chunk:components(f2) == 0) + assert(#chunk:components(f3) == 0) end end end @@ -3476,20 +3476,20 @@ do assert(c and es and #es == 1 and es[1] == e) do - local c1, c2 = evo.components(c, f1, f2) + local c1, c2 = c:components(f1, f2) assert(c1 and #c1 == 1 and c1[1] == 1) assert(c2 and #c2 == 1 and c2[1] == 2) end do - local c1, c2, c3 = evo.components(c, f1, f2, f3) + local c1, c2, c3 = c:components(f1, f2, f3) assert(c1 and #c1 == 1 and c1[1] == 1) assert(c2 and #c2 == 1 and c2[1] == 2) assert(c3 and #c3 == 1 and c3[1] == 3) end do - local c1, c2, c3, c4 = evo.components(c, f1, f2, f3, f4) + local c1, c2, c3, c4 = c:components(f1, f2, f3, f4) assert(c1 and #c1 == 1 and c1[1] == 1) assert(c2 and #c2 == 1 and c2[1] == 2) assert(c3 and #c3 == 1 and c3[1] == 3) @@ -3497,7 +3497,7 @@ do end do - local c1, c2, c3, c4, c5 = evo.components(c, f1, f2, f3, f4, f5) + local c1, c2, c3, c4, c5 = c:components(f1, f2, f3, f4, f5) assert(c1 and #c1 == 1 and c1[1] == 1) assert(c2 and #c2 == 1 and c2[1] == 2) assert(c3 and #c3 == 1 and c3[1] == 3) @@ -3951,7 +3951,7 @@ do local c1, c1_es = evo.chunk(f1) assert(c1 and c1_es and #c1_es == 2) assert(c1_es[1] == e1a and c1_es[2] == e1b) - assert(evo.components(c1, f1)[1] == 1 and evo.components(c1, f1)[2] == 11) + assert(c1:components(f1)[1] == 1 and c1:components(f1)[2] == 11) end evo.batch_set(q1, f2, 2) @@ -3963,8 +3963,8 @@ do local c12, c12_es = evo.chunk(f1, f2) assert(c12 and c12_es and #c12_es == 2) assert(c12_es[1] == e1a and c12_es[2] == e1b) - assert(evo.components(c12, f1)[1] == 1 and evo.components(c12, f1)[2] == 11) - assert(evo.components(c12, f2)[1] == 2 and evo.components(c12, f2)[2] == 2) + assert(c12:components(f1)[1] == 1 and c12:components(f1)[2] == 11) + assert(c12:components(f2)[1] == 2 and c12:components(f2)[2] == 2) end local e1c = evo.builder():set(f1, 111):build() @@ -3974,7 +3974,7 @@ do local c1, c1_es = evo.chunk(f1) assert(c1 and c1_es and #c1_es == 2) assert(c1_es[1] == e1c and c1_es[2] == e1d) - assert(evo.components(c1, f1)[1] == 111 and evo.components(c1, f1)[2] == 1111) + assert(c1:components(f1)[1] == 111 and c1:components(f1)[2] == 1111) end evo.set(q1, evo.EXCLUDES, { f2 }) @@ -3988,10 +3988,10 @@ do assert(c12 and c12_es and #c12_es == 4) assert(c12_es[1] == e1a and c12_es[2] == e1b) assert(c12_es[3] == e1c and c12_es[4] == e1d) - assert(evo.components(c12, f1)[1] == 1 and evo.components(c12, f1)[2] == 11) - assert(evo.components(c12, f1)[3] == 111 and evo.components(c12, f1)[4] == 1111) - assert(evo.components(c12, f2)[1] == 2 and evo.components(c12, f2)[2] == 2) - assert(evo.components(c12, f2)[3] == 22 and evo.components(c12, f2)[4] == 22) + assert(c12:components(f1)[1] == 1 and c12:components(f1)[2] == 11) + assert(c12:components(f1)[3] == 111 and c12:components(f1)[4] == 1111) + assert(c12:components(f2)[1] == 2 and c12:components(f2)[2] == 2) + assert(c12:components(f2)[3] == 22 and c12:components(f2)[4] == 22) end end @@ -4007,9 +4007,9 @@ do local c123, c123_es = evo.chunk(f1, f2, f3) assert(c123 and c123_es and #c123_es == 2) assert(c123_es[1] == e123a and c123_es[2] == e123b) - assert(evo.components(c123, f1)[1] == 1 and evo.components(c123, f1)[2] == 11) - assert(evo.components(c123, f2)[1] == 2 and evo.components(c123, f2)[2] == 22) - assert(evo.components(c123, f3)[1] == 3 and evo.components(c123, f3)[2] == 33) + assert(c123:components(f1)[1] == 1 and c123:components(f1)[2] == 11) + assert(c123:components(f2)[1] == 2 and c123:components(f2)[2] == 22) + assert(c123:components(f3)[1] == 3 and c123:components(f3)[2] == 33) end evo.batch_remove(q1, f2) @@ -4018,9 +4018,9 @@ do local c13, c13_es = evo.chunk(f3, f1) assert(c13 and c13_es and #c13_es == 2) assert(c13_es[1] == e123a and c13_es[2] == e123b) - assert(evo.components(c13, f1)[1] == 1 and evo.components(c13, f1)[2] == 11) - assert(evo.components(c13, f2)[1] == nil and evo.components(c13, f2)[2] == nil) - assert(evo.components(c13, f3)[1] == 3 and evo.components(c13, f3)[2] == 33) + assert(c13:components(f1)[1] == 1 and c13:components(f1)[2] == 11) + assert(c13:components(f2)[1] == nil and c13:components(f2)[2] == nil) + assert(c13:components(f3)[1] == 3 and c13:components(f3)[2] == 33) end local e3a = evo.builder():set(f3, 3):build() @@ -4030,7 +4030,7 @@ do local c3, c3_es = evo.chunk(f3) assert(c3 and c3_es and #c3_es == 2) assert(c3_es[1] == e3a and c3_es[2] == e3b) - assert(evo.components(c3, f3)[1] == 3 and evo.components(c3, f3)[2] == 33) + assert(c3:components(f3)[1] == 3 and c3:components(f3)[2] == 33) end evo.batch_remove(q1, f1) @@ -4040,12 +4040,12 @@ do assert(c3 and c3_es and #c3_es == 4) assert(c3_es[1] == e3a and c3_es[2] == e3b) assert(c3_es[3] == e123a and c3_es[4] == e123b) - assert(evo.components(c3, f1)[1] == nil and evo.components(c3, f1)[2] == nil) - assert(evo.components(c3, f1)[3] == nil and evo.components(c3, f1)[4] == nil) - assert(evo.components(c3, f2)[1] == nil and evo.components(c3, f2)[2] == nil) - assert(evo.components(c3, f2)[3] == nil and evo.components(c3, f2)[4] == nil) - assert(evo.components(c3, f3)[1] == 3 and evo.components(c3, f3)[2] == 33) - assert(evo.components(c3, f3)[3] == 3 and evo.components(c3, f3)[4] == 33) + assert(c3:components(f1)[1] == nil and c3:components(f1)[2] == nil) + assert(c3:components(f1)[3] == nil and c3:components(f1)[4] == nil) + assert(c3:components(f2)[1] == nil and c3:components(f2)[2] == nil) + assert(c3:components(f2)[3] == nil and c3:components(f2)[4] == nil) + assert(c3:components(f3)[1] == 3 and c3:components(f3)[2] == 33) + assert(c3:components(f3)[3] == 3 and c3:components(f3)[4] == 33) end end @@ -4168,14 +4168,14 @@ do local e12b = evo.builder():set(f1, 5):set(f2, 6):build() do - local c1_es, c1_ec = evo.entities(c1) + local c1_es, c1_ec = c1:entities() assert(c1_es and #c1_es == 2 and c1_ec == 2) assert(c1_es[1] == e1a and c1_es[2] == e1b) - local c2_es, c2_ec = evo.entities(c2) + local c2_es, c2_ec = c2:entities() assert(c2_es and #c2_es == 0 and c2_ec == 0) - local c12_es, c12_ec = evo.entities(c12) + local c12_es, c12_ec = c12:entities() assert(c12_es and #c12_es == 2 and c12_ec == 2) assert(c12_es[1] == e12a and c12_es[2] == e12b) end @@ -4186,14 +4186,14 @@ do evo.set(e1b, f2, 8) do - local c1_es, c1_ec = evo.entities(c1) + local c1_es, c1_ec = c1:entities() assert(c1_es and #c1_es == 0 and c1_ec == 0) - local c2_es, c2_ec = evo.entities(c2) + local c2_es, c2_ec = c2:entities() assert(c2_es and #c2_es == 2 and c2_ec == 2) assert(c2_es[1] == e12a and c2_es[2] == e12b) - local c12_es, c12_ec = evo.entities(c12) + local c12_es, c12_ec = c12:entities() assert(c12_es and #c12_es == 2 and c12_ec == 2) assert(c12_es[1] == e1a and c12_es[2] == e1b) end @@ -4209,7 +4209,7 @@ do assert(evo.is_alive(f2)) assert(evo.is_empty(f2)) - local c1_es, c1_ec = evo.entities(c1) + local c1_es, c1_ec = c1:entities() assert(c1_es and #c1_es == 0 and c1_ec == 0) end end @@ -4220,7 +4220,7 @@ do evo.set(f1, f1) evo.destroy(f1) do - local c1_es, c1_ec = evo.entities(c1) + local c1_es, c1_ec = c1:entities() assert(c1_es and #c1_es == 0 and c1_ec == 0) end end @@ -4235,26 +4235,26 @@ do evo.set(f2, f1) evo.set(f2, f2) do - local c1_es, c1_ec = evo.entities(c1) + local c1_es, c1_ec = c1:entities() assert(c1_es and #c1_es == 0 and c1_ec == 0) - local c2_es, c2_ec = evo.entities(c2) + local c2_es, c2_ec = c2:entities() assert(c2_es and #c2_es == 0 and c2_ec == 0) - local c12_es, c12_ec = evo.entities(c12) + local c12_es, c12_ec = c12:entities() assert(c12_es and #c12_es == 1 and c12_ec == 1) assert(c12_es[1] == f2) end evo.destroy(f1) do - local c1_es, c1_ec = evo.entities(c1) + local c1_es, c1_ec = c1:entities() assert(c1_es and #c1_es == 0 and c1_ec == 0) - local c2_es, c2_ec = evo.entities(c2) + local c2_es, c2_ec = c2:entities() assert(c2_es and #c2_es == 1 and c2_ec == 1) assert(c2_es[1] == f2) - local c12_es, c12_ec = evo.entities(c12) + local c12_es, c12_ec = c12:entities() assert(c12_es and #c12_es == 0 and c12_ec == 0) end end @@ -4269,25 +4269,25 @@ do evo.set(f2, f1) evo.set(f2, f2) do - local c1_es, c1_ec = evo.entities(c1) + local c1_es, c1_ec = c1:entities() assert(c1_es and #c1_es == 0 and c1_ec == 0) - local c2_es, c2_ec = evo.entities(c2) + local c2_es, c2_ec = c2:entities() assert(c2_es and #c2_es == 0 and c2_ec == 0) - local c12_es, c12_ec = evo.entities(c12) + local c12_es, c12_ec = c12:entities() assert(c12_es and #c12_es == 1 and c12_ec == 1) assert(c12_es[1] == f2) end evo.destroy(f1) do - local c1_es, c1_ec = evo.entities(c1) + local c1_es, c1_ec = c1:entities() assert(c1_es and #c1_es == 0 and c1_ec == 0) - local c2_es, c2_ec = evo.entities(c2) + local c2_es, c2_ec = c2:entities() assert(c2_es and #c2_es == 0 and c2_ec == 0) - local c12_es, c12_ec = evo.entities(c12) + local c12_es, c12_ec = c12:entities() assert(c12_es and #c12_es == 0 and c12_ec == 0) end end @@ -4299,20 +4299,20 @@ do evo.set(f2, f1) evo.set(f3, f2) do - local c1_es, c1_ec = evo.entities(c1) + local c1_es, c1_ec = c1:entities() assert(c1_es and #c1_es == 1 and c1_ec == 1) assert(c1_es[1] == f2) - local c2_es, c2_ec = evo.entities(c2) + local c2_es, c2_ec = c2:entities() assert(c2_es and #c2_es == 1 and c2_ec == 1) assert(c2_es[1] == f3) end evo.destroy(f1) do - local c1_es, c1_ec = evo.entities(c1) + local c1_es, c1_ec = c1:entities() assert(c1_es and #c1_es == 0 and c1_ec == 0) - local c2_es, c2_ec = evo.entities(c2) + local c2_es, c2_ec = c2:entities() assert(c2_es and #c2_es == 1 and c2_ec == 1) assert(c2_es[1] == f3) end @@ -4326,20 +4326,20 @@ do evo.set(f2, f1) evo.set(f3, f2) do - local c1_es, c1_ec = evo.entities(c1) + local c1_es, c1_ec = c1:entities() assert(c1_es and #c1_es == 1 and c1_ec == 1) assert(c1_es[1] == f2) - local c2_es, c2_ec = evo.entities(c2) + local c2_es, c2_ec = c2:entities() assert(c2_es and #c2_es == 1 and c2_ec == 1) assert(c2_es[1] == f3) end evo.destroy(f1) do - local c1_es, c1_ec = evo.entities(c1) + local c1_es, c1_ec = c1:entities() assert(c1_es and #c1_es == 0 and c1_ec == 0) - local c2_es, c2_ec = evo.entities(c2) + local c2_es, c2_ec = c2:entities() assert(c2_es and #c2_es == 1 and c2_ec == 1) assert(c2_es[1] == f3) end @@ -4353,20 +4353,20 @@ do evo.set(f2, f1) evo.set(f3, f2) do - local c1_es, c1_ec = evo.entities(c1) + local c1_es, c1_ec = c1:entities() assert(c1_es and #c1_es == 1 and c1_ec == 1) assert(c1_es[1] == f2) - local c2_es, c2_ec = evo.entities(c2) + local c2_es, c2_ec = c2:entities() assert(c2_es and #c2_es == 1 and c2_ec == 1) assert(c2_es[1] == f3) end evo.destroy(f1) do - local c1_es, c1_ec = evo.entities(c1) + local c1_es, c1_ec = c1:entities() assert(c1_es and #c1_es == 0 and c1_ec == 0) - local c2_es, c2_ec = evo.entities(c2) + local c2_es, c2_ec = c2:entities() assert(c2_es and #c2_es == 0 and c2_ec == 0) end end @@ -4393,15 +4393,15 @@ do evo.batch_destroy(qt) do - local c4_es, c4_ec = evo.entities(c4) + local c4_es, c4_ec = c4:entities() assert(c4_es and #c4_es == 3 and c4_ec == 3) assert(c4_es[1] == e24 and c4_es[2] == e14 and c4_es[3] == e124) end - assert(#evo.entities(c14) == 0) - assert(#evo.entities(c24) == 0) - assert(#evo.entities(c124) == 0) - assert(#evo.entities(c234) == 0) + assert(#c14:entities() == 0) + assert(#c24:entities() == 0) + assert(#c124:entities() == 0) + assert(#c234:entities() == 0) assert(evo.is_alive(e14) and not evo.is_empty(e14)) assert(evo.is_alive(e24) and not evo.is_empty(e24)) @@ -4430,7 +4430,7 @@ do assert(not evo.is_alive(f1)) assert(remove_count == 1) - local c1_es, c1_ec = evo.entities(c1) + local c1_es, c1_ec = c1:entities() assert(c1_es and #c1_es == 0 and c1_ec == 0) end end @@ -4456,7 +4456,7 @@ do assert(not evo.is_alive(f1)) assert(remove_count == 1) - local c1_es, c1_ec = evo.entities(c1) + local c1_es, c1_ec = c1:entities() assert(c1_es and #c1_es == 0 and c1_ec == 0) end end @@ -4489,27 +4489,27 @@ do end do - local c1_es, c1_ec = evo.entities(c1) + local c1_es, c1_ec = c1:entities() assert(c1 and c1_es and c1_ec) assert(c1_ec == 4 and #c1_es == 4) assert(c1_es[1] == e1a and c1_es[2] == e1b and c1_es[3] == e12a and c1_es[4] == e12b) end do - local c12_es, c12_ec = evo.entities(c12) + local c12_es, c12_ec = c12:entities() assert(c12 and c12_es and c12_ec) assert(c12_ec == 0 and #c12_es == 0) end do - local c13_es, c13_ec = evo.entities(c13) + local c13_es, c13_ec = c13:entities() assert(c13 and c13_es and c13_ec) assert(c13_ec == 2 and #c13_es == 2) assert(c13_es[1] == e123a and c13_es[2] == e123b) end do - local c123_es, c123_ec = evo.entities(c123) + local c123_es, c123_ec = c123:entities() assert(c123 and c123_es and c123_ec) assert(c123_ec == 0 and #c123_es == 0) end @@ -4632,12 +4632,12 @@ do assert(c1 and c23) - assert(#evo.fragments(c1) == 1) - assert(evo.fragments(c1)[1] == f1) + assert(#c1:fragments() == 1) + assert(c1:fragments()[1] == f1) - assert(#evo.fragments(c23) == 2) - assert(evo.fragments(c23)[1] == f2) - assert(evo.fragments(c23)[2] == f3) + assert(#c23:fragments() == 2) + assert(c23:fragments()[1] == f2) + assert(c23:fragments()[2] == f3) end do @@ -4773,10 +4773,10 @@ do local e12 = evo.builder():set(f1, 2):set(f2, 3):build() do - assert(evo.is_alive(old_c1)) + assert(old_c1:is_alive()) assert(old_c1 == evo.chunk(f1)) - local old_c1_es, old_c1_ec = evo.entities(old_c1) + local old_c1_es, old_c1_ec = old_c1:entities() assert(old_c1_es and old_c1_ec) assert(#old_c1_es == 1 and old_c1_ec == 1) assert(old_c1_es[1] == e1) @@ -4785,10 +4785,10 @@ do do local new_c12 = assert(evo.chunk(f1, f2)) - assert(not evo.is_alive(old_c12)) + assert(not old_c12:is_alive()) assert(old_c12 ~= new_c12) - local new_c12_es, new_c12_ec = evo.entities(new_c12) + local new_c12_es, new_c12_ec = new_c12:entities() assert(new_c12_es and new_c12_ec) assert(#new_c12_es == 1 and new_c12_ec == 1) assert(new_c12_es[1] == e12) @@ -4802,12 +4802,12 @@ do do local new_c1 = assert(evo.chunk(f1)) - assert(not evo.is_alive(old_c1)) + assert(not old_c1:is_alive()) assert(old_c1 ~= new_c1) local new_c12 = assert(evo.chunk(f1, f2)) - assert(not evo.is_alive(old_c12)) + assert(not old_c12:is_alive()) assert(old_c12 ~= new_c12) end end @@ -4820,12 +4820,12 @@ do evo.collect_garbage() - assert(evo.is_alive(old_c1)) + assert(old_c1:is_alive()) assert(old_c1 == evo.chunk(f1)) assert(evo.commit()) - assert(not evo.is_alive(old_c1)) + assert(not old_c1:is_alive()) assert(old_c1 ~= evo.chunk(f1)) end @@ -5278,16 +5278,16 @@ do local f1 = evo.id() local c1 = assert(evo.chunk(f1)) - assert(evo.is_alive(c1) and evo.is_empty(c1)) + assert(c1:is_alive() and c1:is_empty()) local e1 = evo.spawn_at(c1) - assert(evo.is_alive(c1) and not evo.is_empty(c1)) + assert(c1:is_alive() and not c1:is_empty()) evo.destroy(e1) - assert(evo.is_alive(c1) and evo.is_empty(c1)) + assert(c1:is_alive() and c1:is_empty()) evo.collect_garbage() - assert(not evo.is_alive(c1) and evo.is_empty(c1)) + assert(not c1:is_alive() and c1:is_empty()) end do @@ -5296,123 +5296,51 @@ do local c1 = assert(evo.chunk(f1)) local c12 = assert(evo.chunk(f1, f2)) - assert(evo.is_alive(c1)) - assert(evo.is_alive(c12)) - assert(evo.is_alive_all()) - assert(evo.is_alive_all(c1)) - assert(evo.is_alive_all(c1, c12)) - assert(not evo.is_alive_any()) - assert(evo.is_alive_any(c1)) - assert(evo.is_alive_any(c1, c12)) + assert(c1:is_alive()) + assert(c12:is_alive()) - assert(evo.is_empty(c1)) - assert(evo.is_empty(c12)) - assert(evo.is_empty_all()) - assert(evo.is_empty_all(c1)) - assert(evo.is_empty_all(c1, c12)) - assert(not evo.is_empty_any()) - assert(evo.is_empty_any(c1)) - assert(evo.is_empty_any(c1, c12)) + assert(c1:is_empty()) + assert(c12:is_empty()) local e12 = evo.spawn_at(c12) - assert(evo.is_alive(c1)) - assert(evo.is_alive(c12)) - assert(evo.is_alive_all()) - assert(evo.is_alive_all(c1)) - assert(evo.is_alive_all(c1, c12)) - assert(not evo.is_alive_any()) - assert(evo.is_alive_any(c1)) - assert(evo.is_alive_any(c1, c12)) + assert(c1:is_alive()) + assert(c12:is_alive()) - assert(evo.is_empty(c1)) - assert(not evo.is_empty(c12)) - assert(evo.is_empty_all()) - assert(evo.is_empty_all(c1)) - assert(not evo.is_empty_all(c1, c12)) - assert(not evo.is_empty_any()) - assert(evo.is_empty_any(c1)) - assert(evo.is_empty_any(c1, c12)) + assert(c1:is_empty()) + assert(not c12:is_empty()) evo.remove(e12, f2) - assert(evo.is_alive(c1)) - assert(evo.is_alive(c12)) - assert(evo.is_alive_all()) - assert(evo.is_alive_all(c1)) - assert(evo.is_alive_all(c1, c12)) - assert(not evo.is_alive_any()) - assert(evo.is_alive_any(c1)) - assert(evo.is_alive_any(c1, c12)) + assert(c1:is_alive()) + assert(c12:is_alive()) - assert(not evo.is_empty(c1)) - assert(evo.is_empty(c12)) - assert(evo.is_empty_all()) - assert(not evo.is_empty_all(c1)) - assert(not evo.is_empty_all(c1, c12)) - assert(not evo.is_empty_any()) - assert(not evo.is_empty_any(c1)) - assert(evo.is_empty_any(c1, c12)) + assert(not c1:is_empty()) + assert(c12:is_empty()) evo.collect_garbage() - assert(evo.is_alive(c1)) - assert(not evo.is_alive(c12)) - assert(evo.is_alive_all()) - assert(evo.is_alive_all(c1)) - assert(not evo.is_alive_all(c1, c12)) - assert(not evo.is_alive_any()) - assert(evo.is_alive_any(c1)) - assert(evo.is_alive_any(c1, c12)) + assert(c1:is_alive()) + assert(not c12:is_alive()) - assert(not evo.is_empty(c1)) - assert(evo.is_empty(c12)) - assert(evo.is_empty_all()) - assert(not evo.is_empty_all(c1)) - assert(not evo.is_empty_all(c1, c12)) - assert(not evo.is_empty_any()) - assert(not evo.is_empty_any(c1)) - assert(evo.is_empty_any(c1, c12)) + assert(not c1:is_empty()) + assert(c12:is_empty()) evo.remove(e12, f1) - assert(evo.is_alive(c1)) - assert(not evo.is_alive(c12)) - assert(evo.is_alive_all()) - assert(evo.is_alive_all(c1)) - assert(not evo.is_alive_all(c1, c12)) - assert(not evo.is_alive_any()) - assert(evo.is_alive_any(c1)) - assert(evo.is_alive_any(c1, c12)) + assert(c1:is_alive()) + assert(not c12:is_alive()) - assert(evo.is_empty(c1)) - assert(evo.is_empty(c12)) - assert(evo.is_empty_all()) - assert(evo.is_empty_all(c1)) - assert(evo.is_empty_all(c1, c12)) - assert(not evo.is_empty_any()) - assert(evo.is_empty_any(c1)) - assert(evo.is_empty_any(c1, c12)) + assert(c1:is_empty()) + assert(c12:is_empty()) evo.collect_garbage() - assert(not evo.is_alive(c1)) - assert(not evo.is_alive(c12)) - assert(evo.is_alive_all()) - assert(not evo.is_alive_all(c1)) - assert(not evo.is_alive_all(c1, c12)) - assert(not evo.is_alive_any()) - assert(not evo.is_alive_any(c1)) - assert(not evo.is_alive_any(c1, c12)) + assert(not c1:is_alive()) + assert(not c12:is_alive()) - assert(evo.is_empty(c1)) - assert(evo.is_empty(c12)) - assert(evo.is_empty_all()) - assert(evo.is_empty_all(c1)) - assert(evo.is_empty_all(c1, c12)) - assert(not evo.is_empty_any()) - assert(evo.is_empty_any(c1)) - assert(evo.is_empty_any(c1, c12)) + assert(c1:is_empty()) + assert(c12:is_empty()) end do @@ -5420,46 +5348,47 @@ do local c1 = assert(evo.chunk(f1)) local c12 = assert(evo.chunk(f1, f2)) - assert(evo.has(c1, f1)) - assert(not evo.has(c1, f2)) - assert(evo.has(c12, f1)) - assert(evo.has(c12, f2)) + assert(c1:has(f1)) + assert(not c1:has(f2)) + assert(c12:has(f1)) + assert(c12:has(f2)) - assert(evo.has_all(c1)) - assert(evo.has_all(c1, f1)) - assert(not evo.has_all(c1, f1, f2)) - assert(evo.has_all(c12)) - assert(evo.has_all(c12, f1)) - assert(evo.has_all(c12, f1, f2)) + assert(c1:has_all()) + assert(c1:has_all(f1)) + assert(not c1:has_all(f1, f2)) + assert(c12:has_all()) + assert(c12:has_all(f1)) + assert(c12:has_all(f1, f2)) - assert(not evo.has_any(c1)) - assert(evo.has_any(c1, f1)) - assert(evo.has_any(c1, f1, f2)) - assert(not evo.has_any(c12)) - assert(evo.has_any(c12, f1)) - assert(evo.has_any(c12, f1, f2)) + assert(not c1:has_any()) + assert(c1:has_any(f1)) + assert(c1:has_any(f1, f2)) + assert(not c12:has_any()) + assert(c12:has_any(f1)) + assert(c12:has_any(f1, f2)) evo.collect_garbage() - assert(not evo.is_alive_any(c1, c12)) + assert(not c1:is_alive()) + assert(not c12:is_alive()) - assert(evo.has(c1, f1)) - assert(not evo.has(c1, f2)) - assert(evo.has(c12, f1)) - assert(evo.has(c12, f2)) + assert(c1:has(f1)) + assert(not c1:has(f2)) + assert(c12:has(f1)) + assert(c12:has(f2)) - assert(evo.has_all(c1)) - assert(evo.has_all(c1, f1)) - assert(not evo.has_all(c1, f1, f2)) - assert(evo.has_all(c12)) - assert(evo.has_all(c12, f1)) - assert(evo.has_all(c12, f1, f2)) + assert(c1:has_all()) + assert(c1:has_all(f1)) + assert(not c1:has_all(f1, f2)) + assert(c12:has_all()) + assert(c12:has_all(f1)) + assert(c12:has_all(f1, f2)) - assert(not evo.has_any(c1)) - assert(evo.has_any(c1, f1)) - assert(evo.has_any(c1, f1, f2)) - assert(not evo.has_any(c12)) - assert(evo.has_any(c12, f1)) - assert(evo.has_any(c12, f1, f2)) + assert(not c1:has_any()) + assert(c1:has_any(f1)) + assert(c1:has_any(f1, f2)) + assert(not c12:has_any()) + assert(c12:has_any(f1)) + assert(c12:has_any(f1, f2)) end do @@ -5491,14 +5420,14 @@ do local e1 = evo.spawn_at(c1, { f1 }, { 42 }) assert(evo.commit()) - assert(evo.is_alive(c1)) + assert(c1:is_alive()) assert(evo.get(e1, f1) == 42) assert(evo.defer()) evo.collect_garbage() assert(evo.commit()) - assert(evo.is_alive(c1)) + assert(c1:is_alive()) evo.destroy(e1) assert(not evo.is_alive(e1)) @@ -5507,7 +5436,7 @@ do evo.collect_garbage() assert(evo.commit()) - assert(not evo.is_alive(c1)) + assert(not c1:is_alive()) end do @@ -5519,14 +5448,14 @@ do local e1 = evo.spawn_with({ f1 }, { 42 }) assert(evo.commit()) - assert(evo.is_alive(c1)) + assert(c1:is_alive()) assert(evo.get(e1, f1) == 42) assert(evo.defer()) evo.collect_garbage() assert(evo.commit()) - assert(evo.is_alive(c1)) + assert(c1:is_alive()) evo.destroy(e1) assert(not evo.is_alive(e1)) @@ -5535,7 +5464,7 @@ do evo.collect_garbage() assert(evo.commit()) - assert(not evo.is_alive(c1)) + assert(not c1:is_alive()) end do diff --git a/develop/usbench.lua b/develop/usbench.lua index 293ce8a..d2ebe04 100644 --- a/develop/usbench.lua +++ b/develop/usbench.lua @@ -41,7 +41,7 @@ basics.describe_bench(string.format('Evolved Entity Cycle (Defer): %d entities', evo.defer() do for chunk, entities in evo.execute(A) do - local as = evo.components(chunk, a) + local as = chunk:components(a) for i = 1, #entities do evo.set(evo.id(), b, as[i]) end @@ -70,7 +70,7 @@ basics.describe_bench(string.format('Evolved Entity Cycle (Manual): %d entities' local to_create = {} for chunk, entities in evo.execute(A) do - local as = evo.components(chunk, a) + local as = chunk:components(a) for i = 1, #entities do to_create[#to_create + 1] = as[i] end @@ -144,21 +144,21 @@ basics.describe_bench(string.format('Evolved Simple Iteration: %d entities', N), ---@param CE evolved.query function(a, b, c, d, e, AB, CD, CE) for chunk, entities in evo.execute(AB) do - local as, bs = evo.components(chunk, a, b) + local as, bs = chunk:components(a, b) for i = 1, #entities do as[i], bs[i] = bs[i], as[i] end end for chunk, entities in evo.execute(CD) do - local cs, ds = evo.components(chunk, c, d) + local cs, ds = chunk:components(c, d) for i = 1, #entities do cs[i], ds[i] = ds[i], cs[i] end end for chunk, entities in evo.execute(CE) do - local cs, es = evo.components(chunk, c, e) + local cs, es = chunk:components(c, e) for i = 1, #entities do cs[i], es[i] = es[i], cs[i] end @@ -240,35 +240,35 @@ basics.describe_bench(string.format('Evolved Packed Iteration: %d entities', N), ---@param E evolved.query function(a, b, c, d, e, A, B, C, D, E) for chunk, entities in evo.execute(A) do - local as = evo.components(chunk, a) + local as = chunk:components(a) for i = 1, #entities do as[i] = as[i] * 2 end end for chunk, entities in evo.execute(B) do - local bs = evo.components(chunk, b) + local bs = chunk:components(b) for i = 1, #entities do bs[i] = bs[i] * 2 end end for chunk, entities in evo.execute(C) do - local cs = evo.components(chunk, c) + local cs = chunk:components(c) for i = 1, #entities do cs[i] = cs[i] * 2 end end for chunk, entities in evo.execute(D) do - local ds = evo.components(chunk, d) + local ds = chunk:components(d) for i = 1, #entities do ds[i] = ds[i] * 2 end end for chunk, entities in evo.execute(E) do - local es = evo.components(chunk, e) + local es = chunk:components(e) for i = 1, #entities do es[i] = es[i] * 2 end @@ -335,14 +335,14 @@ basics.describe_bench(string.format('Evolved Fragmented Iteration: %d entities', ---@param Last evolved.query function(data, last, Data, Last) for chunk, entities in evo.execute(Data) do - local ds = evo.components(chunk, data) + local ds = chunk:components(data) for i = 1, #entities do ds[i] = ds[i] * 2 end end for chunk, entities in evo.execute(Last) do - local ls = evo.components(chunk, last) + local ls = chunk:components(last) for i = 1, #entities do ls[i] = ls[i] * 2 end diff --git a/evolved.lua b/evolved.lua index 99b7d4f..27e3652 100644 --- a/evolved.lua +++ b/evolved.lua @@ -49,29 +49,6 @@ local evolved = { ---@alias evolved.insert_hook fun(entity: evolved.entity, fragment: evolved.fragment, new_component: evolved.component) ---@alias evolved.remove_hook fun(entity: evolved.entity, fragment: evolved.fragment, component: evolved.component) ----@class (exact) evolved.chunk ----@field package __parent? evolved.chunk ----@field package __child_set table ----@field package __child_list evolved.chunk[] ----@field package __child_count integer ----@field package __entity_list evolved.entity[] ----@field package __entity_count integer ----@field package __fragment evolved.fragment ----@field package __fragment_set table ----@field package __fragment_list evolved.fragment[] ----@field package __fragment_count integer ----@field package __component_count integer ----@field package __component_indices table ----@field package __component_storages evolved.storage[] ----@field package __component_fragments evolved.fragment[] ----@field package __with_fragment_edges table ----@field package __without_fragment_edges table ----@field package __unreachable_or_collected boolean ----@field package __has_setup_hooks boolean ----@field package __has_assign_hooks boolean ----@field package __has_insert_hooks boolean ----@field package __has_remove_hooks boolean - ---@class (exact) evolved.each_state ---@field package [1] integer structural_changes ---@field package [2] evolved.chunk entity_chunk @@ -130,7 +107,6 @@ local __lua_pcall = pcall local __lua_select = select local __lua_setmetatable = setmetatable local __lua_table_sort = table.sort -local __lua_type = type ---@type fun(narray: integer, nhash: integer): table local __lua_table_new = (function() @@ -735,12 +711,6 @@ local __evolved_batch_remove local __evolved_batch_clear local __evolved_batch_destroy -local __evolved_chunk - -local __evolved_entities -local __evolved_fragments -local __evolved_components - local __evolved_each local __evolved_execute @@ -753,6 +723,7 @@ local __evolved_spawn_with local __evolved_debug_mode local __evolved_collect_garbage +local __evolved_chunk local __evolved_builder --- @@ -855,7 +826,28 @@ end local __debug_fns = {} ----@type metatable +---@class evolved.chunk +---@field package __parent? evolved.chunk +---@field package __child_set table +---@field package __child_list evolved.chunk[] +---@field package __child_count integer +---@field package __entity_list evolved.entity[] +---@field package __entity_count integer +---@field package __fragment evolved.fragment +---@field package __fragment_set table +---@field package __fragment_list evolved.fragment[] +---@field package __fragment_count integer +---@field package __component_count integer +---@field package __component_indices table +---@field package __component_storages evolved.storage[] +---@field package __component_fragments evolved.fragment[] +---@field package __with_fragment_edges table +---@field package __without_fragment_edges table +---@field package __unreachable_or_collected boolean +---@field package __has_setup_hooks boolean +---@field package __has_assign_hooks boolean +---@field package __has_insert_hooks boolean +---@field package __has_remove_hooks boolean __debug_fns.chunk_mt = {} __debug_fns.chunk_mt.__index = __debug_fns.chunk_mt @@ -879,6 +871,24 @@ __debug_fns.chunk_component_storages_mt.__index = __debug_fns.chunk_component_st __debug_fns.chunk_component_fragments_mt = {} __debug_fns.chunk_component_fragments_mt.__index = __debug_fns.chunk_component_fragments_mt +---@class evolved.builder +---@field package __prefab? evolved.entity +---@field package __single? evolved.component +---@field package __fragment_set table +---@field package __fragment_list evolved.fragment[] +---@field package __component_list evolved.component[] +---@field package __component_count integer +__debug_fns.builder_mt = {} +__debug_fns.builder_mt.__index = __debug_fns.builder_mt + +---@type metatable +__debug_fns.builder_fragment_set_mt = {} +__debug_fns.builder_fragment_set_mt.__index = __debug_fns.builder_fragment_set_mt + +---@type metatable +__debug_fns.builder_fragment_list_mt = {} +__debug_fns.builder_fragment_list_mt.__index = __debug_fns.builder_fragment_list_mt + --- --- --- @@ -962,6 +972,47 @@ end --- --- +---@param self evolved.builder +function __debug_fns.builder_mt.__tostring(self) + local items = {} ---@type string[] + + for fragment_index, fragment in ipairs(self.__fragment_list) do + items[fragment_index] = __id_name(fragment) + end + + return string.format('<%s>', table.concat(items, ', ')) +end + +---@param self table +function __debug_fns.builder_fragment_set_mt.__tostring(self) + local items = {} ---@type string[] + + for fragment, fragment_index in pairs(self) do + items[fragment_index] = string.format('(%s -> %d)', + __id_name(fragment), fragment_index) + end + + return string.format('{%s}', table.concat(items, ', ')) +end + +---@param self evolved.fragment[] +function __debug_fns.builder_fragment_list_mt.__tostring(self) + local items = {} ---@type string[] + + for fragment_index, fragment in ipairs(self) do + items[fragment_index] = string.format('(%d -> %s)', + fragment_index, __id_name(fragment)) + end + + return string.format('[%s]', table.concat(items, ', ')) +end + +--- +--- +--- +--- +--- + ---@param chunk evolved.chunk function __debug_fns.validate_chunk(chunk) if chunk.__unreachable_or_collected then @@ -3788,21 +3839,15 @@ function __evolved_commit() return true end ----@param chunk_or_entity evolved.chunk | evolved.entity +---@param entity evolved.entity ---@return boolean ---@nodiscard -function __evolved_is_alive(chunk_or_entity) - if __lua_type(chunk_or_entity) ~= 'number' then - local chunk = chunk_or_entity --[[@as evolved.chunk]] - return not chunk.__unreachable_or_collected - else - local entity = chunk_or_entity --[[@as evolved.entity]] - local entity_index = entity % 0x100000 - return __freelist_ids[entity_index] == entity - end +function __evolved_is_alive(entity) + local entity_index = entity % 0x100000 + return __freelist_ids[entity_index] == entity end ----@param ... evolved.chunk | evolved.entity chunks_or_entities +---@param ... evolved.entity entities ---@return boolean ---@nodiscard function __evolved_is_alive_all(...) @@ -3815,27 +3860,18 @@ function __evolved_is_alive_all(...) local freelist_ids = __freelist_ids for argument_index = 1, argument_count do - ---@type evolved.chunk | evolved.entity - local chunk_or_entity = __lua_select(argument_index, ...) - - if __lua_type(chunk_or_entity) ~= 'number' then - local chunk = chunk_or_entity --[[@as evolved.chunk]] - if chunk.__unreachable_or_collected then - return false - end - else - local entity = chunk_or_entity --[[@as evolved.entity]] - local entity_index = entity % 0x100000 - if freelist_ids[entity_index] ~= entity then - return false - end + ---@type evolved.entity + local entity = __lua_select(argument_index, ...) + local entity_index = entity % 0x100000 + if freelist_ids[entity_index] ~= entity then + return false end end return true end ----@param ... evolved.chunk | evolved.entity chunks_or_entities +---@param ... evolved.entity entities ---@return boolean ---@nodiscard function __evolved_is_alive_any(...) @@ -3848,41 +3884,26 @@ function __evolved_is_alive_any(...) local freelist_ids = __freelist_ids for argument_index = 1, argument_count do - ---@type evolved.chunk | evolved.entity - local chunk_or_entity = __lua_select(argument_index, ...) - - if __lua_type(chunk_or_entity) ~= 'number' then - local chunk = chunk_or_entity --[[@as evolved.chunk]] - if not chunk.__unreachable_or_collected then - return true - end - else - local entity = chunk_or_entity --[[@as evolved.entity]] - local entity_index = entity % 0x100000 - if freelist_ids[entity_index] == entity then - return true - end + ---@type evolved.entity + local entity = __lua_select(argument_index, ...) + local entity_index = entity % 0x100000 + if freelist_ids[entity_index] == entity then + return true end end return false end ----@param chunk_or_entity evolved.chunk | evolved.entity +---@param entity evolved.entity ---@return boolean ---@nodiscard -function __evolved_is_empty(chunk_or_entity) - if __lua_type(chunk_or_entity) ~= 'number' then - local chunk = chunk_or_entity --[[@as evolved.chunk]] - return chunk.__unreachable_or_collected or chunk.__entity_count == 0 - else - local entity = chunk_or_entity --[[@as evolved.entity]] - local entity_index = entity % 0x100000 - return __freelist_ids[entity_index] ~= entity or not __entity_chunks[entity_index] - end +function __evolved_is_empty(entity) + local entity_index = entity % 0x100000 + return __freelist_ids[entity_index] ~= entity or not __entity_chunks[entity_index] end ----@param ... evolved.chunk | evolved.entity chunks_or_entities +---@param ... evolved.entity entities ---@return boolean ---@nodiscard function __evolved_is_empty_all(...) @@ -3895,27 +3916,18 @@ function __evolved_is_empty_all(...) local freelist_ids = __freelist_ids for argument_index = 1, argument_count do - ---@type evolved.chunk | evolved.entity - local chunk_or_entity = __lua_select(argument_index, ...) - - if __lua_type(chunk_or_entity) ~= 'number' then - local chunk = chunk_or_entity --[[@as evolved.chunk]] - if not chunk.__unreachable_or_collected and chunk.__entity_count > 0 then - return false - end - else - local entity = chunk_or_entity --[[@as evolved.entity]] - local entity_index = entity % 0x100000 - if freelist_ids[entity_index] == entity and __entity_chunks[entity_index] then - return false - end + ---@type evolved.entity + local entity = __lua_select(argument_index, ...) + local entity_index = entity % 0x100000 + if freelist_ids[entity_index] == entity and __entity_chunks[entity_index] then + return false end end return true end ----@param ... evolved.chunk | evolved.entity chunks_or_entities +---@param ... evolved.entity entities ---@return boolean ---@nodiscard function __evolved_is_empty_any(...) @@ -3928,105 +3940,75 @@ function __evolved_is_empty_any(...) local freelist_ids = __freelist_ids for argument_index = 1, argument_count do - ---@type evolved.chunk | evolved.entity - local chunk_or_entity = __lua_select(argument_index, ...) - - if __lua_type(chunk_or_entity) ~= 'number' then - local chunk = chunk_or_entity --[[@as evolved.chunk]] - if chunk.__unreachable_or_collected or chunk.__entity_count == 0 then - return true - end - else - local entity = chunk_or_entity --[[@as evolved.entity]] - local entity_index = entity % 0x100000 - if freelist_ids[entity_index] ~= entity or not __entity_chunks[entity_index] then - return true - end + ---@type evolved.entity + local entity = __lua_select(argument_index, ...) + local entity_index = entity % 0x100000 + if freelist_ids[entity_index] ~= entity or not __entity_chunks[entity_index] then + return true end end return false end ----@param chunk_or_entity evolved.chunk | evolved.entity +---@param entity evolved.entity ---@param fragment evolved.fragment ---@return boolean ---@nodiscard -function __evolved_has(chunk_or_entity, fragment) - if __lua_type(chunk_or_entity) ~= 'number' then - local chunk = chunk_or_entity --[[@as evolved.chunk]] - return __chunk_has_fragment(chunk, fragment) - else - local entity = chunk_or_entity --[[@as evolved.entity]] +function __evolved_has(entity, fragment) + local entity_index = entity % 0x100000 - local entity_index = entity % 0x100000 - - if __freelist_ids[entity_index] ~= entity then - return false - end - - local chunk = __entity_chunks[entity_index] - - if not chunk then - return false - end - - return __chunk_has_fragment(chunk, fragment) + if __freelist_ids[entity_index] ~= entity then + return false end + + local chunk = __entity_chunks[entity_index] + + if not chunk then + return false + end + + return __chunk_has_fragment(chunk, fragment) end ----@param chunk_or_entity evolved.chunk | evolved.entity +---@param entity evolved.entity ---@param ... evolved.fragment fragments ---@return boolean ---@nodiscard -function __evolved_has_all(chunk_or_entity, ...) - if __lua_type(chunk_or_entity) ~= 'number' then - local chunk = chunk_or_entity --[[@as evolved.chunk]] - return __chunk_has_all_fragments(chunk, ...) - else - local entity = chunk_or_entity --[[@as evolved.entity]] +function __evolved_has_all(entity, ...) + local entity_index = entity % 0x100000 - local entity_index = entity % 0x100000 - - if __freelist_ids[entity_index] ~= entity then - return __lua_select('#', ...) == 0 - end - - local chunk = __entity_chunks[entity_index] - - if not chunk then - return __lua_select('#', ...) == 0 - end - - return __chunk_has_all_fragments(chunk, ...) + if __freelist_ids[entity_index] ~= entity then + return __lua_select('#', ...) == 0 end + + local chunk = __entity_chunks[entity_index] + + if not chunk then + return __lua_select('#', ...) == 0 + end + + return __chunk_has_all_fragments(chunk, ...) end ----@param chunk_or_entity evolved.chunk | evolved.entity +---@param entity evolved.entity ---@param ... evolved.fragment fragments ---@return boolean ---@nodiscard -function __evolved_has_any(chunk_or_entity, ...) - if __lua_type(chunk_or_entity) ~= 'number' then - local chunk = chunk_or_entity --[[@as evolved.chunk]] - return __chunk_has_any_fragments(chunk, ...) - else - local entity = chunk_or_entity --[[@as evolved.entity]] +function __evolved_has_any(entity, ...) + local entity_index = entity % 0x100000 - local entity_index = entity % 0x100000 - - if __freelist_ids[entity_index] ~= entity then - return false - end - - local chunk = __entity_chunks[entity_index] - - if not chunk then - return false - end - - return __chunk_has_any_fragments(chunk, ...) + if __freelist_ids[entity_index] ~= entity then + return false end + + local chunk = __entity_chunks[entity_index] + + if not chunk then + return false + end + + return __chunk_has_any_fragments(chunk, ...) end ---@param entity evolved.entity @@ -4650,95 +4632,6 @@ end --- --- ----@param head_fragment evolved.fragment ----@param ... evolved.fragment tail_fragments ----@return evolved.chunk chunk ----@return evolved.entity[] entity_list ----@return integer entity_count ----@nodiscard -function __evolved_chunk(head_fragment, ...) - local chunk = __chunk_fragments(head_fragment, ...) - return chunk, chunk.__entity_list, chunk.__entity_count -end - ----@param chunk evolved.chunk ----@return evolved.entity[] entity_list ----@return integer entity_count ----@nodiscard -function __evolved_entities(chunk) - return chunk.__entity_list, chunk.__entity_count -end - ----@param chunk evolved.chunk ----@return evolved.fragment[] fragments ----@return integer fragment_count ----@nodiscard -function __evolved_fragments(chunk) - return chunk.__fragment_list, chunk.__fragment_count -end - ----@param chunk evolved.chunk ----@param ... evolved.fragment fragments ----@return evolved.storage ... storages ----@nodiscard -function __evolved_components(chunk, ...) - local fragment_count = __lua_select('#', ...) - - if fragment_count == 0 then - return - end - - local indices = chunk.__component_indices - local storages = chunk.__component_storages - - local empty_component_storage = __safe_tbls.__EMPTY_COMPONENT_STORAGE - - if fragment_count == 1 then - local f1 = ... - local i1 = indices[f1] - return - i1 and storages[i1] or empty_component_storage - end - - if fragment_count == 2 then - local f1, f2 = ... - local i1, i2 = indices[f1], indices[f2] - return - i1 and storages[i1] or empty_component_storage, - i2 and storages[i2] or empty_component_storage - end - - if fragment_count == 3 then - local f1, f2, f3 = ... - local i1, i2, i3 = indices[f1], indices[f2], indices[f3] - return - i1 and storages[i1] or empty_component_storage, - i2 and storages[i2] or empty_component_storage, - i3 and storages[i3] or empty_component_storage - end - - if fragment_count == 4 then - local f1, f2, f3, f4 = ... - local i1, i2, i3, i4 = indices[f1], indices[f2], indices[f3], indices[f4] - return - i1 and storages[i1] or empty_component_storage, - i2 and storages[i2] or empty_component_storage, - i3 and storages[i3] or empty_component_storage, - i4 and storages[i4] or empty_component_storage - end - - do - local f1, f2, f3, f4 = ... - local i1, i2, i3, i4 = indices[f1], indices[f2], indices[f3], indices[f4] - return - i1 and storages[i1] or empty_component_storage, - i2 and storages[i2] or empty_component_storage, - i3 and storages[i3] or empty_component_storage, - i4 and storages[i4] or empty_component_storage, - __evolved_components(chunk, __lua_select(5, ...)) - end -end - ---@param entity evolved.entity ---@return evolved.each_iterator iterator ---@return evolved.each_state? iterator_state @@ -5067,15 +4960,130 @@ end --- --- ----@class evolved.builder ----@field package __prefab? evolved.entity ----@field package __single? evolved.component ----@field package __fragment_set table ----@field package __fragment_list evolved.fragment[] ----@field package __component_list evolved.component[] ----@field package __component_count integer -local __evolved_builder_mt = {} -__evolved_builder_mt.__index = __evolved_builder_mt +---@param fragment evolved.fragment +---@param ... evolved.fragment fragments +---@return evolved.chunk chunk +---@return evolved.entity[] entity_list +---@return integer entity_count +---@nodiscard +function __evolved_chunk(fragment, ...) + local chunk = __chunk_fragments(fragment, ...) + return chunk, chunk.__entity_list, chunk.__entity_count +end + +--- +--- +--- +--- +--- + +---@return boolean +---@nodiscard +function __debug_fns.chunk_mt:is_alive() + return not self.__unreachable_or_collected +end + +---@return boolean +---@nodiscard +function __debug_fns.chunk_mt:is_empty() + return self.__unreachable_or_collected or self.__entity_count == 0 +end + +---@param fragment evolved.fragment +---@return boolean +---@nodiscard +function __debug_fns.chunk_mt:has(fragment) + return __chunk_has_fragment(self, fragment) +end + +---@param ... evolved.fragment fragments +---@return boolean +---@nodiscard +function __debug_fns.chunk_mt:has_all(...) + return __chunk_has_all_fragments(self, ...) +end + +---@param ... evolved.fragment fragments +---@return boolean +---@nodiscard +function __debug_fns.chunk_mt:has_any(...) + return __chunk_has_any_fragments(self, ...) +end + +---@return evolved.entity[] entity_list +---@return integer entity_count +---@nodiscard +function __debug_fns.chunk_mt:entities() + return self.__entity_list, self.__entity_count +end + +---@return evolved.fragment[] fragment_list +---@return integer fragment_count +---@nodiscard +function __debug_fns.chunk_mt:fragments() + return self.__fragment_list, self.__fragment_count +end + +---@param ... evolved.fragment fragments +---@return evolved.storage ... storages +---@nodiscard +function __debug_fns.chunk_mt:components(...) + local fragment_count = __lua_select('#', ...) + + if fragment_count == 0 then + return + end + + local indices = self.__component_indices + local storages = self.__component_storages + + local empty_component_storage = __safe_tbls.__EMPTY_COMPONENT_STORAGE + + if fragment_count == 1 then + local f1 = ... + local i1 = indices[f1] + return + i1 and storages[i1] or empty_component_storage + end + + if fragment_count == 2 then + local f1, f2 = ... + local i1, i2 = indices[f1], indices[f2] + return + i1 and storages[i1] or empty_component_storage, + i2 and storages[i2] or empty_component_storage + end + + if fragment_count == 3 then + local f1, f2, f3 = ... + local i1, i2, i3 = indices[f1], indices[f2], indices[f3] + return + i1 and storages[i1] or empty_component_storage, + i2 and storages[i2] or empty_component_storage, + i3 and storages[i3] or empty_component_storage + end + + if fragment_count == 4 then + local f1, f2, f3, f4 = ... + local i1, i2, i3, i4 = indices[f1], indices[f2], indices[f3], indices[f4] + return + i1 and storages[i1] or empty_component_storage, + i2 and storages[i2] or empty_component_storage, + i3 and storages[i3] or empty_component_storage, + i4 and storages[i4] or empty_component_storage + end + + do + local f1, f2, f3, f4 = ... + local i1, i2, i3, i4 = indices[f1], indices[f2], indices[f3], indices[f4] + return + i1 and storages[i1] or empty_component_storage, + i2 and storages[i2] or empty_component_storage, + i3 and storages[i3] or empty_component_storage, + i4 and storages[i4] or empty_component_storage, + self:components(__lua_select(5, ...)) + end +end --- --- @@ -5087,17 +5095,23 @@ __evolved_builder_mt.__index = __evolved_builder_mt ---@nodiscard function __evolved_builder() return __lua_setmetatable({ - __fragment_set = {}, - __fragment_list = {}, + __fragment_set = __lua_setmetatable({}, __debug_fns.builder_fragment_set_mt), + __fragment_list = __lua_setmetatable({}, __debug_fns.builder_fragment_list_mt), __component_list = {}, __component_count = 0, - }, __evolved_builder_mt) + }, __debug_fns.builder_mt) end +--- +--- +--- +--- +--- + ---@param fragment evolved.fragment ---@return boolean ---@nodiscard -function __evolved_builder_mt:has(fragment) +function __debug_fns.builder_mt:has(fragment) local component_index = self.__fragment_set[fragment] if not component_index then @@ -5118,7 +5132,7 @@ end ---@param ... evolved.fragment fragments ---@return boolean ---@nodiscard -function __evolved_builder_mt:has_all(...) +function __debug_fns.builder_mt:has_all(...) local fragment_count = select("#", ...) if fragment_count == 0 then @@ -5131,7 +5145,7 @@ end ---@param ... evolved.fragment fragments ---@return boolean ---@nodiscard -function __evolved_builder_mt:has_any(...) +function __debug_fns.builder_mt:has_any(...) local fragment_count = select("#", ...) if fragment_count == 0 then @@ -5144,7 +5158,7 @@ end ---@param ... evolved.fragment fragments ---@return evolved.component ... components ---@nodiscard -function __evolved_builder_mt:get(...) +function __debug_fns.builder_mt:get(...) local fragment_count = select("#", ...) if fragment_count == 0 then @@ -5173,7 +5187,7 @@ end ---@param fragment evolved.fragment ---@param component evolved.component ---@return evolved.builder builder -function __evolved_builder_mt:set(fragment, component) +function __debug_fns.builder_mt:set(fragment, component) if __debug_mode then __debug_fns.validate_fragment(fragment) end @@ -5222,7 +5236,7 @@ end ---@param ... evolved.fragment fragments ---@return evolved.builder builder -function __evolved_builder_mt:remove(...) +function __debug_fns.builder_mt:remove(...) local fragment_count = select("#", ...) if fragment_count == 0 then @@ -5263,7 +5277,7 @@ function __evolved_builder_mt:remove(...) end ---@return evolved.builder builder -function __evolved_builder_mt:clear() +function __debug_fns.builder_mt:clear() self.__prefab = nil self.__single = nil self.__component_count = 0 @@ -5271,45 +5285,45 @@ function __evolved_builder_mt:clear() end ---@return evolved.builder builder -function __evolved_builder_mt:tag() +function __debug_fns.builder_mt:tag() return self:set(__TAG) end ---@param name string ---@return evolved.builder builder -function __evolved_builder_mt:name(name) +function __debug_fns.builder_mt:name(name) return self:set(__NAME, name) end ---@param prefab evolved.entity ---@return evolved.builder builder -function __evolved_builder_mt:prefab(prefab) +function __debug_fns.builder_mt:prefab(prefab) self.__prefab = prefab return self end ---@param single evolved.component ---@return evolved.builder builder -function __evolved_builder_mt:single(single) +function __debug_fns.builder_mt:single(single) self.__single = single == nil and true or single return self end ---@param default evolved.component ---@return evolved.builder builder -function __evolved_builder_mt:default(default) +function __debug_fns.builder_mt:default(default) return self:set(__DEFAULT, default) end ---@param duplicate evolved.duplicate ---@return evolved.builder builder -function __evolved_builder_mt:duplicate(duplicate) +function __debug_fns.builder_mt:duplicate(duplicate) return self:set(__DUPLICATE, duplicate) end ---@param ... evolved.fragment fragments ---@return evolved.builder builder -function __evolved_builder_mt:include(...) +function __debug_fns.builder_mt:include(...) local argument_count = __lua_select('#', ...) if argument_count == 0 then @@ -5334,7 +5348,7 @@ end ---@param ... evolved.fragment fragments ---@return evolved.builder builder -function __evolved_builder_mt:exclude(...) +function __debug_fns.builder_mt:exclude(...) local argument_count = __lua_select('#', ...) if argument_count == 0 then @@ -5359,72 +5373,72 @@ end ---@param on_set evolved.set_hook ---@return evolved.builder builder -function __evolved_builder_mt:on_set(on_set) +function __debug_fns.builder_mt:on_set(on_set) return self:set(__ON_SET, on_set) end ---@param on_assign evolved.assign_hook ---@return evolved.builder builder -function __evolved_builder_mt:on_assign(on_assign) +function __debug_fns.builder_mt:on_assign(on_assign) return self:set(__ON_ASSIGN, on_assign) end ---@param on_insert evolved.insert_hook ---@return evolved.builder builder -function __evolved_builder_mt:on_insert(on_insert) +function __debug_fns.builder_mt:on_insert(on_insert) return self:set(__ON_INSERT, on_insert) end ---@param on_remove evolved.remove_hook ---@return evolved.builder builder -function __evolved_builder_mt:on_remove(on_remove) +function __debug_fns.builder_mt:on_remove(on_remove) return self:set(__ON_REMOVE, on_remove) end ---@param group evolved.system ---@return evolved.builder builder -function __evolved_builder_mt:group(group) +function __debug_fns.builder_mt:group(group) return self:set(__GROUP, group) end ---@param query evolved.query ---@return evolved.builder builder -function __evolved_builder_mt:query(query) +function __debug_fns.builder_mt:query(query) return self:set(__QUERY, query) end ---@param execute evolved.execute ---@return evolved.builder builder -function __evolved_builder_mt:execute(execute) +function __debug_fns.builder_mt:execute(execute) return self:set(__EXECUTE, execute) end ---@param prologue evolved.prologue ---@return evolved.builder builder -function __evolved_builder_mt:prologue(prologue) +function __debug_fns.builder_mt:prologue(prologue) return self:set(__PROLOGUE, prologue) end ---@param epilogue evolved.epilogue ---@return evolved.builder builder -function __evolved_builder_mt:epilogue(epilogue) +function __debug_fns.builder_mt:epilogue(epilogue) return self:set(__EPILOGUE, epilogue) end ---@return evolved.builder builder -function __evolved_builder_mt:disabled() +function __debug_fns.builder_mt:disabled() return self:set(__DISABLED) end ---@param destroy_policy evolved.id ---@return evolved.builder builder -function __evolved_builder_mt:destroy_policy(destroy_policy) +function __debug_fns.builder_mt:destroy_policy(destroy_policy) return self:set(__DESTROY_POLICY, destroy_policy) end ---@param no_clear? boolean ---@return evolved.entity entity -function __evolved_builder_mt:build(no_clear) +function __debug_fns.builder_mt:build(no_clear) local prefab = self.__prefab local single = self.__single local fragment_list = self.__fragment_list @@ -5843,12 +5857,6 @@ evolved.batch_remove = __evolved_batch_remove evolved.batch_clear = __evolved_batch_clear evolved.batch_destroy = __evolved_batch_destroy -evolved.chunk = __evolved_chunk - -evolved.entities = __evolved_entities -evolved.fragments = __evolved_fragments -evolved.components = __evolved_components - evolved.each = __evolved_each evolved.execute = __evolved_execute @@ -5861,6 +5869,7 @@ evolved.spawn_with = __evolved_spawn_with evolved.debug_mode = __evolved_debug_mode evolved.collect_garbage = __evolved_collect_garbage +evolved.chunk = __evolved_chunk evolved.builder = __evolved_builder evolved.collect_garbage()