From 18019679d58696cd0daf863fc2be43c2b1b6f0f9 Mon Sep 17 00:00:00 2001 From: Ukendio Date: Wed, 12 Mar 2025 15:29:24 +0100 Subject: [PATCH] Alias for world consttructor --- jecs.luau | 25 ++++++------- test/tests.luau | 96 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 12 deletions(-) diff --git a/jecs.luau b/jecs.luau index 15f290c..a2b2e06 100644 --- a/jecs.luau +++ b/jecs.luau @@ -46,7 +46,7 @@ export type Record = { } type IdRecord = { - columns: { number }, + cache: { number }, counts: { number }, flags: number, size: number, @@ -480,7 +480,7 @@ local function world_target(world: World, entity: i53, relation: i24, index: num nth = nth + count + 1 end - local tr = idr.columns[archetype_id] + local tr = idr.cache[archetype_id] nth = archetype.types[nth + tr] @@ -537,7 +537,7 @@ local function id_record_ensure(world: World, id: number): IdRecord idr = { size = 0, - columns = {}, + cache = {}, counts = {}, flags = flags, hooks = { @@ -562,7 +562,7 @@ local function archetype_append_to_records( local archetype_id = archetype.id local archetype_records = archetype.records local archetype_counts = archetype.counts - local idr_columns = idr.columns + local idr_columns = idr.cache local idr_counts = idr.counts local tr = idr_columns[archetype_id] if not tr then @@ -1063,7 +1063,7 @@ local function archetype_destroy(world: World, archetype: Archetype) for id in records do local idr = component_index[id] - idr.columns[archetype_id] = nil :: any + idr.cache[archetype_id] = nil :: any idr.counts[archetype_id] = nil idr.size -= 1 records[id] = nil :: any @@ -1122,7 +1122,7 @@ do if idr then local flags = idr.flags if bit32.band(flags, ECS_ID_DELETE) ~= 0 then - for archetype_id in idr.columns do + for archetype_id in idr.cache do local idr_archetype = archetypes[archetype_id] local entities = idr_archetype.entities @@ -1134,7 +1134,7 @@ do archetype_destroy(world, idr_archetype) end else - for archetype_id in idr.columns do + for archetype_id in idr.cache do local idr_archetype = archetypes[archetype_id] local entities = idr_archetype.entities local n = #entities @@ -1153,7 +1153,7 @@ do local children local ids local count = 0 - local archetype_ids = idr_t.columns + local archetype_ids = idr_t.cache for archetype_id in archetype_ids do local idr_t_archetype = archetypes[archetype_id] local idr_t_types = idr_t_archetype.types @@ -2107,7 +2107,7 @@ local function world_query(world: World, ...) return q end - for archetype_id in idr.columns do + for archetype_id in idr.cache do local compatibleArchetype = archetypes[archetype_id] if #compatibleArchetype.entities == 0 then continue @@ -2141,9 +2141,9 @@ local function world_each(world: World, id): () -> () return NOOP end - local idr_columns = idr.columns + local idr_cache = idr.cache local archetypes = world.archetypes - local archetype_id = next(idr_columns, nil) :: number + local archetype_id = next(idr_cache, nil) :: number local archetype = archetypes[archetype_id] if not archetype then return NOOP @@ -2155,7 +2155,7 @@ local function world_each(world: World, id): () -> () return function(): any local entity = entities[row] while not entity do - archetype_id = next(idr_columns, archetype_id) :: number + archetype_id = next(idr_cache, archetype_id) :: number if not archetype_id then return end @@ -2488,6 +2488,7 @@ export type World = { return { World = World :: { new: () -> World }, + world = World.new :: () -> World, OnAdd = EcsOnAdd :: Entity<(entity: Entity) -> ()>, OnRemove = EcsOnRemove :: Entity<(entity: Entity) -> ()>, diff --git a/test/tests.luau b/test/tests.luau index 504027f..23f38ba 100644 --- a/test/tests.luau +++ b/test/tests.luau @@ -26,6 +26,50 @@ local N = 2 ^ 8 type World = jecs.World type Entity = jecs.Entity +local c = { + white_underline = function(s: any) + return `\27[1;4m{s}\27[0m` + end, + + white = function(s: any) + return `\27[37;1m{s}\27[0m` + end, + + green = function(s: any) + return `\27[32;1m{s}\27[0m` + end, + + red = function(s: any) + return `\27[31;1m{s}\27[0m` + end, + + yellow = function(s: any) + return `\27[33;1m{s}\27[0m` + end, + + red_highlight = function(s: any) + return `\27[41;1;30m{s}\27[0m` + end, + + green_highlight = function(s: any) + return `\27[42;1;30m{s}\27[0m` + end, + + gray = function(s: any) + return `\27[30;1m{s}\27[0m` + end, +} + +local function pe(e) + local gen = ECS_GENERATION(e) + return c.green(`e{ECS_ID(e)}`)..c.yellow(`v{gen}`) +end + +local function pp(e) + local gen = ECS_GENERATION(e) + return c.green(`e{ECS_ID(e)}`)..c.yellow(`v{jecs.ECS_ENTITY_T_HI(e)}`) +end + local function debug_world_inspect(world: World) local function record(e): jecs.Record return entity_index_try_get_any(world.entity_index, e) :: any @@ -67,10 +111,61 @@ local function debug_world_inspect(world: World) } end +local dwi = debug_world_inspect + local function name(world, e) return world:get(e, jecs.Name) end +TEST("#repro", function() + local world = world_new() + + local function getTargets(relation) + local tgts = {} + local pairwildcard = pair(relation, jecs.Wildcard) + for _, archetype in world:query(pairwildcard):archetypes() do + local tr = archetype.records[pairwildcard] + local count = archetype.counts[pairwildcard] + local types = archetype.types + for _, entity in archetype.entities do + for i = 0, count - 1 do + local tgt = jecs.pair_second(world, types[i + tr]) + table.insert(tgts, tgt) + end + end + end + return tgts + end + + local Attacks = world:component() + local Eats = world:component() + + local function setAttacksAndEats(entity1, entity2) + world:add(entity1, pair(Attacks, entity2)) + world:add(entity1, pair(Eats, entity2)) + end + + local e1 = world:entity() + local e2 = world:entity() + local e3 = world:entity() + print(e1, e2, e3) + setAttacksAndEats(e3, e1) + setAttacksAndEats(e3, e2) + setAttacksAndEats(e1, e2) + print("---------------- delete e2 ---------------") + local d = dwi(world) + for archetype_id in world.component_index[pair(jecs.Wildcard, e2)].cache do + local archetype = world.archetypes[archetype_id].type + testkit.print(archetype) + end + world:delete(e2) + print("-----------------------------") + testkit.print(d.tbl(e1).types) + -- testkit.print(d.tbl(e3).types) + -- testkit.print(getTargets(Attacks)) + -- testkit.print(getTargets(Eats)) +end) + TEST("archetype", function() local archetype_traverse_add = jecs.archetype_traverse_add local archetype_traverse_remove = jecs.archetype_traverse_remove @@ -1199,6 +1294,7 @@ TEST("world:delete", function() world:delete(e) end) + local d = debug_world_inspect(world) for i, friend in friends do CHECK(not world:has(friend, pair(FriendsWith, e))) CHECK(world:has(friend, Health))