From 753a73882aed2ee071f2525d611ea7c4a5d0a5f3 Mon Sep 17 00:00:00 2001 From: marked Date: Sun, 11 May 2025 02:11:44 +0200 Subject: [PATCH] Sync to upstream Jecs 0.6.0 (#11) Reviewed-on: https://git.devmarked.win/marked/jecs-pesde/pulls/11 --- .luaurc | 1 + CHANGELOG.md | 63 ++-- jecs.luau | 868 +++++++++++++++++++++++++++++-------------------- pesde-rbx.toml | 2 +- pesde.lock | 2 +- pesde.toml | 2 +- 6 files changed, 554 insertions(+), 384 deletions(-) diff --git a/.luaurc b/.luaurc index d1ae244..f856eba 100644 --- a/.luaurc +++ b/.luaurc @@ -4,6 +4,7 @@ "testkit": "tools/testkit", "mirror": "mirror", "tools": "tools", + "addons": "addons" }, "languageMode": "strict" } diff --git a/CHANGELOG.md b/CHANGELOG.md index 52f87cc..fa1d870 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,30 +10,53 @@ The format is based on [Keep a Changelog][kac], and this project adheres to ## [Unreleased] +## [0.6.0] - 2025-05-10 + - `[world]`: - - 16% faster `world:get` - - `world:has` no longer typechecks components after the 8th one. -- `[typescript]` + - Added `world:range` to restrict entity range + - Changed `world:entity` to accept the overload to create an entity at the desired id + - Changed `world:clear` to also look through the component record for the cleared `ID` + - Removes the cleared ID from every entity that has it + - Changed entity ID layouts by putting the index in the lower bits, which should make every world function 1-5 nanoseconds faster + - Fixed `world:delete` not removing every pair with an unalive target + - Specifically happened when you had at least two pairs of different relations with multiple targets each +- `[hooks]`: + - Replaced `OnSet` with `OnChange` + - The former was used to detect emplace/move actions. Now the behaviour for `OnChange` is that it will run only when the value has changed + - Changed `OnAdd` to specifically run after the data has been set for non-zero-sized components. Also returns the value that the component was set to + - This should allow a more lenient window for modifying data + - Changed `OnRemove` to lazily lookup which archetype the entity will move to + - Can now have interior structural changes within `OnRemove` hooks + - Optimized `world:has` for both single component and multiple component presence. + - This comes at the cost that it cannot check the component presence for more than 4 components at a time. If this is important, consider calling to this function multiple times. - - Fixed Entity type to default to `undefined | unknown` instead of just `undefined` +## [0.5.0] - 2024-12-26 +- `[world]`: + - Fixed `world:target` not giving adjacent pairs + - Added `world:each` to find entities with a specific Tag + - Added `world:children` to find children of entity - `[query]`: - - Fixed bug where `world:clear` did not invoke `jecs.OnRemove` hooks - - Changed `query.__iter` to drain on iteration - - It will initialize once wherever you left iteration off at last time - - Changed `query:iter` to restart the iterator - - Removed `query:drain` and `query:next` - - If you want to get individual results outside of a for-loop, you need to call `query:iter` to initialize the iterator and then call the iterator function manually - ```lua - local it = world:query(A, B, C):iter() - local entity, a, b, c = it() - entity, a, b, c = it() -- get next results - ``` -- `[world` - - Fixed a bug with `world:clear` not invoking `jecs.OnRemove` hooks -- `[typescript]`: - - Changed pair to accept generics - - Improved handling of Tags + - Added `query:cached` + - Adds query cache that updates itself when an archetype matching the query gets created or deleted. +- `[luau]`: + - Changed how entities' types are inferred with user-defined type functions + - Changed `Pair` to return `Second` if `First` is a `Tag`; otherwise, returns `First`. + +## [0.4.0] - 2024-11-17 + +- `[world]`: + - Added recycling to `world:entity` + - If you see much larger entity ids, that is because its generation has been incremented +- `[query]`: + - Removed `query:drain` + - The default behaviour is simply to drain the iterator + - Removed `query:next` + - Just call the iterator function returned by `query:iter` directly if you want to get the next results + - Removed `query:replace` +- `[luau]`: + - Fixed `query:archetypes` not taking `self` + - Changed so that the `jecs.Pair` type now returns the first element's type so you won't need to typecast anymore. ## [0.3.2] - 2024-10-01 diff --git a/jecs.luau b/jecs.luau index 69a4ad7..f705f0e 100644 --- a/jecs.luau +++ b/jecs.luau @@ -13,22 +13,6 @@ type Column = { any } type Map = { [K]: V } -type ecs_graph_edge_t = { - from: ecs_archetype_t, - to: ecs_archetype_t?, - id: number, - prev: ecs_graph_edge_t?, - next: ecs_graph_edge_t?, -} - -type ecs_graph_edges_t = Map - -type ecs_graph_node_t = { - add: ecs_graph_edges_t, - remove: ecs_graph_edges_t, - refs: ecs_graph_edge_t, -} - type ecs_archetype_t = { id: number, types: Ty, @@ -37,7 +21,7 @@ type ecs_archetype_t = { columns: { Column }, records: { [i53]: number }, counts: { [i53]: number }, -} & ecs_graph_node_t +} export type Archetype = { id: number, @@ -61,9 +45,9 @@ type ecs_id_record_t = { flags: number, size: number, hooks: { - on_add: ((entity: i53) -> ())?, - on_set: ((entity: i53, data: any) -> ())?, - on_remove: ((entity: i53) -> ())?, + on_add: ((entity: i53, id: i53, data: any?) -> ())?, + on_change: ((entity: i53, id: i53, data: any) -> ())?, + on_remove: ((entity: i53, id: i53) -> ())?, }, } @@ -78,6 +62,8 @@ type ecs_entity_index_t = { sparse_array: Map, alive_count: number, max_id: number, + range_begin: number?, + range_end: number? } type ecs_query_data_t = { @@ -97,6 +83,7 @@ type ecs_observer_t = { type ecs_observable_t = Map> type ecs_world_t = { + archetype_edges: Map>, entity_index: ecs_entity_index_t, component_index: ecs_id_index_t, archetypes: ecs_archetypes_t, @@ -111,7 +98,7 @@ local HI_COMPONENT_ID = _G.__JECS_HI_COMPONENT_ID or 256 -- stylua: ignore start local EcsOnAdd = HI_COMPONENT_ID + 1 local EcsOnRemove = HI_COMPONENT_ID + 2 -local EcsOnSet = HI_COMPONENT_ID + 3 +local EcsOnChange = HI_COMPONENT_ID + 3 local EcsWildcard = HI_COMPONENT_ID + 4 local EcsChildOf = HI_COMPONENT_ID + 5 local EcsComponent = HI_COMPONENT_ID + 6 @@ -124,23 +111,60 @@ local EcsOnArchetypeCreate = HI_COMPONENT_ID + 12 local EcsOnArchetypeDelete = HI_COMPONENT_ID + 13 local EcsRest = HI_COMPONENT_ID + 14 -local ECS_ID_DELETE = 0b0000_0001 -local ECS_ID_IS_TAG = 0b0000_0010 -local ECS_ID_HAS_ON_ADD = 0b0000_0100 -local ECS_ID_HAS_ON_SET = 0b0000_1000 -local ECS_ID_HAS_ON_REMOVE = 0b0001_0000 -local ECS_ID_MASK = 0b0000_0000 +local ECS_ID_DELETE = 0b01 +local ECS_ID_IS_TAG = 0b10 +local ECS_ID_MASK = 0b00 local ECS_ENTITY_MASK = bit32.lshift(1, 24) local ECS_GENERATION_MASK = bit32.lshift(1, 16) -local NULL_ARRAY = table.freeze({}) +local NULL_ARRAY = table.freeze({}) :: Column +local NULL = newproxy(false) + local ECS_INTERNAL_ERROR = [[ This is an internal error, please file a bug report via the following link: https://github.com/Ukendio/jecs/issues/new?template=BUG-REPORT.md ]] +local function ecs_assert(condition, msg: string?) + if not condition then + error(msg) + end +end + +local ecs_metadata: Map> = {} +local ecs_max_component_id = 0 +local ecs_max_tag_id = EcsRest + +local function ECS_COMPONENT() + ecs_max_component_id += 1 + if ecs_max_component_id > HI_COMPONENT_ID then + error("Too many components") + end + return ecs_max_component_id +end + +local function ECS_TAG() + ecs_max_tag_id += 1 + return ecs_max_tag_id +end + +local function ECS_META(id: i53, ty: i53, value: any?) + local bundle = ecs_metadata[id] + if bundle == nil then + bundle = {} + ecs_metadata[id] = bundle + end + bundle[ty] = if value == nil then NULL else value +end + +local function ECS_META_RESET() + ecs_metadata = {} + ecs_max_component_id = 0 + ecs_max_tag_id = EcsRest +end + local function ECS_COMBINE(id: number, generation: number): i53 return id + (generation * ECS_ENTITY_MASK) end @@ -169,6 +193,10 @@ local function ECS_ENTITY_T_LO(e: i53): i24 return e % ECS_ENTITY_MASK end +local function ECS_ID(e: i53) + return e % ECS_ENTITY_MASK +end + local function ECS_GENERATION(e: i53) return e // ECS_ENTITY_MASK end @@ -233,10 +261,10 @@ local function entity_index_is_alive(entity_index: ecs_entity_index_t, entity: i return entity_index_try_get(entity_index, entity) ~= nil end -local function entity_index_get_alive(index: ecs_entity_index_t, entity: i53): i53? - local r = entity_index_try_get_any(index, entity) +local function entity_index_get_alive(entity_index: ecs_entity_index_t, entity: i53): i53? + local r = entity_index_try_get_any(entity_index, entity) if r then - return index.dense_array[r.dense] + return entity_index.dense_array[r.dense] end return nil end @@ -264,11 +292,15 @@ local function ecs_get_alive(world, entity) return current end +local ECS_INTERNAL_ERROR_INCOMPATIBLE_ENTITY = "Entity is outside range" + local function entity_index_new_id(entity_index: ecs_entity_index_t): i53 local dense_array = entity_index.dense_array local alive_count = entity_index.alive_count + local sparse_array = entity_index.sparse_array local max_id = entity_index.max_id - if alive_count ~= max_id then + + if alive_count < max_id then alive_count += 1 entity_index.alive_count = alive_count local id = dense_array[alive_count] @@ -276,11 +308,14 @@ local function entity_index_new_id(entity_index: ecs_entity_index_t): i53 end local id = max_id + 1 + local range_end = entity_index.range_end + ecs_assert(range_end == nil or id < range_end, ECS_INTERNAL_ERROR_INCOMPATIBLE_ENTITY) + entity_index.max_id = id alive_count += 1 entity_index.alive_count = alive_count dense_array[alive_count] = id - entity_index.sparse_array[id] = { dense = alive_count } :: ecs_record_t + sparse_array[id] = { dense = alive_count } :: ecs_record_t return id end @@ -484,7 +519,17 @@ local function world_has_one_inline(world: ecs_world_t, entity: i53, id: i53): b return records[id] ~= nil end -local function world_has(world: ecs_world_t, entity: i53, ...: i53): boolean +local function ecs_is_tag(world: ecs_world_t, entity: i53): boolean + local idr = world.component_index[entity] + if idr then + return bit32.band(idr.flags, ECS_ID_IS_TAG) ~= 0 + end + return not world_has_one_inline(world, entity, EcsComponent) +end + +local function world_has(world: ecs_world_t, entity: i53, + a: i53, b: i53?, c: i53?, d: i53?, e: i53?): boolean + local record = entity_index_try_get_fast(world.entity_index, entity) if not record then return false @@ -497,13 +542,11 @@ local function world_has(world: ecs_world_t, entity: i53, ...: i53): boolean local records = archetype.records - for i = 1, select("#", ...) do - if not records[select(i, ...)] then - return false - end - end - - return true + return records[a] ~= nil and + (b == nil or records[b] ~= nil) and + (c == nil or records[c] ~= nil) and + (d == nil or records[d] ~= nil) and + (e == nil or error("args exceeded")) end local function world_target(world: ecs_world_t, entity: i53, relation: i24, index: number?): i24? @@ -547,63 +590,64 @@ end local function id_record_ensure(world: ecs_world_t, id: number): ecs_id_record_t local component_index = world.component_index local entity_index = world.entity_index - local idr: ecs_id_record_t = component_index[id] + local idr: ecs_id_record_t? = component_index[id] - if not idr then - local flags = ECS_ID_MASK - local relation = id - local target = 0 - local is_pair = ECS_IS_PAIR(id) - if is_pair then - relation = entity_index_get_alive(entity_index, ECS_PAIR_FIRST(id)) :: i53 - assert(relation and entity_index_is_alive( - entity_index, relation), ECS_INTERNAL_ERROR) - target = entity_index_get_alive(entity_index, ECS_PAIR_SECOND(id)) :: i53 - assert(target and entity_index_is_alive( - entity_index, target), ECS_INTERNAL_ERROR) - end - - local cleanup_policy = world_target(world, relation, EcsOnDelete, 0) - local cleanup_policy_target = world_target(world, relation, EcsOnDeleteTarget, 0) - - local has_delete = false - - if cleanup_policy == EcsDelete or cleanup_policy_target == EcsDelete then - has_delete = true - end - - local on_add, on_set, on_remove = world_get(world, relation, EcsOnAdd, EcsOnSet, EcsOnRemove) - - local is_tag = not world_has_one_inline(world, relation, EcsComponent) - - if is_tag and is_pair then - is_tag = not world_has_one_inline(world, target, EcsComponent) - end - - flags = bit32.bor( - flags, - if on_add then ECS_ID_HAS_ON_ADD else 0, - if on_remove then ECS_ID_HAS_ON_REMOVE else 0, - if on_set then ECS_ID_HAS_ON_SET else 0, - if has_delete then ECS_ID_DELETE else 0, - if is_tag then ECS_ID_IS_TAG else 0 - ) - - idr = { - size = 0, - cache = {}, - counts = {}, - flags = flags, - hooks = { - on_add = on_add, - on_set = on_set, - on_remove = on_remove, - }, - } - - component_index[id] = idr + if idr then + return idr end + local flags = ECS_ID_MASK + local relation = id + local target = 0 + local is_pair = ECS_IS_PAIR(id) + if is_pair then + relation = entity_index_get_alive(entity_index, ECS_PAIR_FIRST(id)) :: i53 + ecs_assert(relation and entity_index_is_alive( + entity_index, relation), ECS_INTERNAL_ERROR) + target = entity_index_get_alive(entity_index, ECS_PAIR_SECOND(id)) :: i53 + ecs_assert(target and entity_index_is_alive( + entity_index, target), ECS_INTERNAL_ERROR) + end + + local cleanup_policy = world_target(world, relation, EcsOnDelete, 0) + local cleanup_policy_target = world_target(world, relation, EcsOnDeleteTarget, 0) + + local has_delete = false + + if cleanup_policy == EcsDelete or cleanup_policy_target == EcsDelete then + has_delete = true + end + + local on_add, on_change, on_remove = world_get(world, + relation, EcsOnAdd, EcsOnChange, EcsOnRemove) + + local is_tag = not world_has_one_inline(world, + relation, EcsComponent) + + if is_tag and is_pair then + is_tag = not world_has_one_inline(world, target, EcsComponent) + end + + flags = bit32.bor( + flags, + if has_delete then ECS_ID_DELETE else 0, + if is_tag then ECS_ID_IS_TAG else 0 + ) + + idr = { + size = 0, + cache = {}, + counts = {}, + flags = flags, + hooks = { + on_add = on_add, + on_change = on_change, + on_remove = on_remove, + }, + } :: ecs_id_record_t + + component_index[id] = idr + return idr end @@ -640,7 +684,7 @@ local function archetype_create(world: ecs_world_t, id_types: { i24 }, ty, prev: local columns = (table.create(length) :: any) :: { Column } local records: { number } = {} - local counts: {number} = {} + local counts: { number } = {} local archetype: ecs_archetype_t = { columns = columns, @@ -650,10 +694,6 @@ local function archetype_create(world: ecs_world_t, id_types: { i24 }, ty, prev: counts = counts, type = ty, types = id_types, - - add = {}, - remove = {}, - refs = {} :: ecs_graph_edge_t, } for i, component_id in id_types do @@ -693,12 +733,98 @@ local function archetype_create(world: ecs_world_t, id_types: { i24 }, ty, prev: world.archetype_index[ty] = archetype world.archetypes[archetype_id] = archetype + world.archetype_edges[archetype.id] = {} :: Map return archetype end -local function world_entity(world: ecs_world_t): i53 - return entity_index_new_id(world.entity_index) +local function world_range(world: ecs_world_t, range_begin: number, range_end: number?) + local entity_index = world.entity_index + + entity_index.range_begin = range_begin + entity_index.range_end = range_end + + local max_id = entity_index.max_id + + if range_begin > max_id then + local dense_array = entity_index.dense_array + local sparse_array = entity_index.sparse_array + + for i = max_id + 1, range_begin do + dense_array[i] = i + sparse_array[i] = { + dense = 0 + } :: ecs_record_t + end + entity_index.max_id = range_begin - 1 + entity_index.alive_count = range_begin - 1 + end +end + +local function world_entity(world: ecs_world_t, entity: i53?): i53 + local entity_index = world.entity_index + if entity then + local index = ECS_ID(entity) + local max_id = entity_index.max_id + local sparse_array = entity_index.sparse_array + local dense_array = entity_index.dense_array + local alive_count = entity_index.alive_count + local r = sparse_array[index] + if r then + local dense = r.dense + + if not dense or r.dense == 0 then + r.dense = index + dense = index + end + + local any = dense_array[dense] + if dense <= alive_count then + if any ~= entity then + error("Entity ID is already in use with a different generation") + else + return entity + end + end + + local e_swap = dense_array[dense] + local r_swap = entity_index_try_get_any(entity_index, e_swap) :: ecs_record_t + alive_count += 1 + entity_index.alive_count = alive_count + r_swap.dense = dense + r.dense = alive_count + dense_array[dense] = e_swap + dense_array[alive_count] = entity + + return entity + else + for i = max_id + 1, index do + sparse_array[i] = { dense = i } :: ecs_record_t + dense_array[i] = i + end + entity_index.max_id = index + + local e_swap = dense_array[alive_count] + local r_swap = sparse_array[alive_count] + r_swap.dense = index + + alive_count += 1 + entity_index.alive_count = alive_count + + r = sparse_array[index] + + r.dense = alive_count + + sparse_array[index] = r + + dense_array[index] = e_swap + dense_array[alive_count] = entity + + + return entity + end + end + return entity_index_new_id(entity_index, entity) end local function world_parent(world: ecs_world_t, entity: i53) @@ -722,6 +848,7 @@ end local function find_insert(id_types: { i53 }, toAdd: i53): number for i, id in id_types do if id == toAdd then + error("Duplicate component id") return -1 end if id > toAdd then @@ -731,24 +858,6 @@ local function find_insert(id_types: { i53 }, toAdd: i53): number return #id_types + 1 end -local function find_archetype_with(world: ecs_world_t, node: ecs_archetype_t, id: i53): ecs_archetype_t - local id_types = node.types - -- Component IDs are added incrementally, so inserting and sorting - -- them each time would be expensive. Instead this insertion sort can find the insertion - -- point in the types array. - - local dst = table.clone(node.types) :: { i53 } - local at = find_insert(id_types, id) - if at == -1 then - -- If it finds a duplicate, it just means it is the same archetype so it can return it - -- directly instead of needing to hash types for a lookup to the archetype. - return node - end - table.insert(dst, at, id) - - return archetype_ensure(world, dst) -end - local function find_archetype_without( world: ecs_world_t, node: ecs_archetype_t, @@ -756,9 +865,6 @@ local function find_archetype_without( ): ecs_archetype_t local id_types = node.types local at = table.find(id_types, id) - if at == nil then - return node - end local dst = table.clone(id_types) table.remove(dst, at) @@ -766,124 +872,65 @@ local function find_archetype_without( return archetype_ensure(world, dst) end -local function archetype_init_edge( - archetype: ecs_archetype_t, - edge: ecs_graph_edge_t, - id: i53, - to: ecs_archetype_t -) - edge.from = archetype - edge.to = to - edge.id = id -end - -local function archetype_ensure_edge( - world: ecs_world_t, - edges: ecs_graph_edges_t, - id: i53 -): ecs_graph_edge_t - local edge = edges[id] - if not edge then - edge = {} :: ecs_graph_edge_t - edges[id] = edge - end - - return edge -end - -local function init_edge_for_add(world, archetype: ecs_archetype_t, edge: ecs_graph_edge_t, id, to: ecs_archetype_t) - archetype_init_edge(archetype, edge, id, to) - archetype_ensure_edge(world, archetype.add, id) - if archetype ~= to then - local to_refs = to.refs - local next_edge = to_refs.next - - to_refs.next = edge - edge.prev = to_refs - edge.next = next_edge - - if next_edge then - next_edge.prev = edge - end - end -end - -local function init_edge_for_remove( - world: ecs_world_t, - archetype: ecs_archetype_t, - edge: ecs_graph_edge_t, - id: number, - to: ecs_archetype_t -) - archetype_init_edge(archetype, edge, id, to) - archetype_ensure_edge(world, archetype.remove, id) - if archetype ~= to then - local to_refs = to.refs - local prev_edge = to_refs.prev - - to_refs.prev = edge - edge.next = to_refs - edge.prev = prev_edge - - if prev_edge then - prev_edge.next = edge - end - end -end - -local function create_edge_for_add( - world: ecs_world_t, - node: ecs_archetype_t, - edge: ecs_graph_edge_t, - id: i53 -): ecs_archetype_t - local to = find_archetype_with(world, node, id) - init_edge_for_add(world, node, edge, id, to) - return to -end local function create_edge_for_remove( world: ecs_world_t, node: ecs_archetype_t, - edge: ecs_graph_edge_t, + edge: Map, id: i53 ): ecs_archetype_t local to = find_archetype_without(world, node, id) - init_edge_for_remove(world, node, edge, id, to) + local edges = world.archetype_edges + local archetype_id = node.id + edges[archetype_id][id] = to + edges[to.id][id] = node return to end -local function archetype_traverse_add( - world: ecs_world_t, - id: i53, - from: ecs_archetype_t -): ecs_archetype_t - from = from or world.ROOT_ARCHETYPE - local edge = archetype_ensure_edge(world, from.add, id) - - local to = edge.to - if not to then - to = create_edge_for_add(world, from, edge, id) - end - - return to :: ecs_archetype_t -end - local function archetype_traverse_remove( world: ecs_world_t, id: i53, from: ecs_archetype_t ): ecs_archetype_t - from = from or world.ROOT_ARCHETYPE + local edges = world.archetype_edges + local edge = edges[from.id] - local edge = archetype_ensure_edge(world, from.remove, id) - - local to = edge.to - if not to then - to = create_edge_for_remove(world, from, edge, id) + local to: ecs_archetype_t = edge[id] + if to == nil then + to = find_archetype_without(world, from, id) + edge[id] = to + edges[to.id][id] = from end - return to :: ecs_archetype_t + return to +end + +local function find_archetype_with(world, id, from): ecs_archetype_t + local id_types = from.types + + local at = find_insert(id_types, id) + local dst = table.clone(id_types) :: { i53 } + table.insert(dst, at, id) + + return archetype_ensure(world, dst) +end + +local function archetype_traverse_add(world, id, from: ecs_archetype_t): ecs_archetype_t + from = from or world.ROOT_ARCHETYPE + if from.records[id] then + return from + end + local edges = world.archetype_edges + local edge = edges[from.id] + + local to = edge[id] + if not to then + to = find_archetype_with(world, id, from) + edge[id] = to + edges[to.id][id] = from + end + + return to end local function world_add( @@ -914,7 +961,7 @@ local function world_add( local on_add = idr.hooks.on_add if on_add then - on_add(entity) + on_add(entity, id) end end @@ -931,14 +978,15 @@ local function world_set(world: ecs_world_t, entity: i53, id: i53, data: unknown local idr_hooks = idr.hooks if from == to then - -- If the archetypes are the same it can avoid moving the entity - -- and just set the data directly. - local tr = to.records[id] + local tr = (to :: ecs_archetype_t).records[id] local column = from.columns[tr] column[record.row] = data - local on_set = idr_hooks.on_set - if on_set then - on_set(entity, data) + + -- If the archetypes are the same it can avoid moving the entity + -- and just set the data directly. + local on_change = idr_hooks.on_change + if on_change then + on_change(entity, id, data) end return @@ -961,12 +1009,7 @@ local function world_set(world: ecs_world_t, entity: i53, id: i53, data: unknown local on_add = idr_hooks.on_add if on_add then - on_add(entity) - end - - local on_set = idr_hooks.on_set - if on_set then - on_set(entity, data) + on_add(entity, id, data) end end @@ -998,7 +1041,7 @@ local function world_remove(world: ecs_world_t, entity: i53, id: i53) local idr = world.component_index[id] local on_remove = idr.hooks.on_remove if on_remove then - on_remove(entity) + on_remove(entity, id) end local to = archetype_traverse_remove(world, id, record.archetype) @@ -1050,7 +1093,7 @@ local function archetype_delete(world: ecs_world_t, archetype: ecs_archetype_t, local idr = component_index[id] local on_remove = idr.hooks.on_remove if on_remove then - on_remove(delete) + on_remove(delete, id) end end @@ -1064,21 +1107,20 @@ local function archetype_delete(world: ecs_world_t, archetype: ecs_archetype_t, end local function world_clear(world: ecs_world_t, entity: i53) - --TODO: use sparse_get (stashed) - local record = entity_index_try_get(world.entity_index, entity) - if not record then - return - end + local entity_index = world.entity_index + local component_index = world.component_index + local archetypes = world.archetypes + local tgt = ECS_PAIR(EcsWildcard, entity) + local idr_t = component_index[tgt] + local idr = component_index[entity] + local rel = ECS_PAIR(entity, EcsWildcard) + local idr_r = component_index[rel] - local archetype = record.archetype - local row = record.row - - local idr = world.component_index[entity] if idr then local count = 0 local queue = {} for archetype_id in idr.cache do - local idr_archetype = world.archetypes[archetype_id] + local idr_archetype = archetypes[archetype_id] local entities = idr_archetype.entities local n = #entities count += n @@ -1089,63 +1131,79 @@ local function world_clear(world: ecs_world_t, entity: i53) end end - if archetype then - -- In the future should have a destruct mode for - -- deleting archetypes themselves. Maybe requires recycling - archetype_delete(world, archetype, row) + if idr_t then + local queue: { i53 } + local ids: Map + + local count = 0 + 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 + local entities = idr_t_archetype.entities + local removal_queued = false + + for _, id in idr_t_types do + if not ECS_IS_PAIR(id) then + continue + end + local object = entity_index_get_alive( + entity_index, ECS_PAIR_SECOND(id)) + if object ~= entity then + continue + end + if not ids then + ids = {} :: { [i53]: boolean } + end + ids[id] = true + removal_queued = true + end + + if not removal_queued then + continue + end + + if not queue then + queue = {} :: { i53 } + end + + local n = #entities + table.move(entities, 1, n, count + 1, queue) + count += n + end + + for id in ids do + for _, child in queue do + world_remove(world, child, id) + end + end end - record.archetype = nil :: any - record.row = nil :: any -end + if idr_r then + local count = 0 + local archetype_ids = idr_r.cache + local ids = {} + local queue = {} + for archetype_id in archetype_ids do + local idr_r_archetype = archetypes[archetype_id] + local entities = idr_r_archetype.entities + local tr = idr_r_archetype.records[rel] + local tr_count = idr_r_archetype.counts[rel] + local types = idr_r_archetype.types + for i = tr, tr + tr_count - 1 do + ids[types[i]] = true + end + local n = #entities + table.move(entities, 1, n, count + 1, queue) + count += n + end -local function archetype_disconnect_edge(edge: ecs_graph_edge_t) - local edge_next = edge.next - local edge_prev = edge.prev - if edge_next then - edge_next.prev = edge_prev + for _, e in queue do + for id in ids do + world_remove(world, e, id) + end + end end - if edge_prev then - edge_prev.next = edge_next - end -end - -local function archetype_remove_edge(edges: ecs_graph_edges_t, id: i53, edge: ecs_graph_edge_t) - archetype_disconnect_edge(edge) - edges[id] = nil :: any -end - -local function archetype_clear_edges(archetype: ecs_archetype_t) - local add: ecs_graph_edges_t = archetype.add - local remove: ecs_graph_edges_t = archetype.remove - local node_refs = archetype.refs - for id, edge in add do - archetype_disconnect_edge(edge) - add[id] = nil :: any - end - for id, edge in remove do - archetype_disconnect_edge(edge) - remove[id] = nil :: any - end - - local cur = node_refs.next - while cur do - local edge = cur :: ecs_graph_edge_t - local next_edge = edge.next - archetype_remove_edge(edge.from.add, edge.id, edge) - cur = next_edge - end - - cur = node_refs.prev - while cur do - local edge: ecs_graph_edge_t = cur - local next_edge = edge.prev - archetype_remove_edge(edge.from.remove, edge.id, edge) - cur = next_edge - end - - node_refs.next = nil - node_refs.prev = nil end local function archetype_destroy(world: ecs_world_t, archetype: ecs_archetype_t) @@ -1154,7 +1212,12 @@ local function archetype_destroy(world: ecs_world_t, archetype: ecs_archetype_t) end local component_index = world.component_index - archetype_clear_edges(archetype) + local archetype_edges = world.archetype_edges + + for id, edge in archetype_edges[archetype.id] do + archetype_edges[edge.id][id] = nil + end + local archetype_id = archetype.id world.archetypes[archetype_id] = nil :: any world.archetype_index[archetype.type] = nil :: any @@ -1225,8 +1288,11 @@ local function world_delete(world: ecs_world_t, entity: i53) local component_index = world.component_index local archetypes = world.archetypes local tgt = ECS_PAIR(EcsWildcard, delete) + local rel = ECS_PAIR(delete, EcsWildcard) + local idr_t = component_index[tgt] local idr = component_index[delete] + local idr_r = component_index[rel] if idr then local flags = idr.flags @@ -1256,11 +1322,10 @@ local function world_delete(world: ecs_world_t, entity: i53) end end - local dense_array = entity_index.dense_array - if idr_t then - local children - local ids + local children: { i53 } + local ids: Map + local count = 0 local archetype_ids = idr_t.cache for archetype_id in archetype_ids do @@ -1288,8 +1353,8 @@ local function world_delete(world: ecs_world_t, entity: i53) end break else - if not ids then - ids = {} + if not ids then + ids = {} :: { [i53]: boolean } end ids[id] = true removal_queued = true @@ -1300,7 +1365,7 @@ local function world_delete(world: ecs_world_t, entity: i53) continue end if not children then - children = {} + children = {} :: { i53 } end local n = #entities table.move(entities, 1, n, count + 1, children) @@ -1308,8 +1373,8 @@ local function world_delete(world: ecs_world_t, entity: i53) end if ids then - for id in ids do - for _, child in children do + for _, child in children do + for id in ids do world_remove(world, child, id) end end @@ -1320,19 +1385,68 @@ local function world_delete(world: ecs_world_t, entity: i53) end end - local index_of_deleted_entity = record.dense - local index_of_last_alive_entity = entity_index.alive_count - entity_index.alive_count = index_of_last_alive_entity - 1 + if idr_r then + local archetype_ids = idr_r.cache + local flags = idr_r.flags + if (bit32.band(flags, ECS_ID_DELETE) :: number) ~= 0 then + for archetype_id in archetype_ids do + local idr_r_archetype = archetypes[archetype_id] + local entities = idr_r_archetype.entities + local n = #entities + for i = n, 1, -1 do + world_delete(world, entities[i]) + end + archetype_destroy(world, idr_r_archetype) + end + else + local children = {} + local count = 0 + local ids = {} + for archetype_id in archetype_ids do + local idr_r_archetype = archetypes[archetype_id] + local entities = idr_r_archetype.entities + local tr = idr_r_archetype.records[rel] + local tr_count = idr_r_archetype.counts[rel] + local types = idr_r_archetype.types + for i = tr, tr_count do + ids[types[tr]] = true + end + local n = #entities + table.move(entities, 1, n, count + 1, children) + count += n + end - local last_alive_entity = dense_array[index_of_last_alive_entity] - local r_swap = entity_index_try_get_any(entity_index, last_alive_entity) :: ecs_record_t - r_swap.dense = index_of_deleted_entity + for _, child in children do + for id in ids do + world_remove(world, child, id) + end + end + + for archetype_id in archetype_ids do + archetype_destroy(world, archetypes[archetype_id]) + end + end + end + + local dense_array = entity_index.dense_array + local dense = record.dense + local i_swap = entity_index.alive_count + entity_index.alive_count = i_swap - 1 + + local e_swap = dense_array[i_swap] + local r_swap = entity_index_try_get_any(entity_index, e_swap) :: ecs_record_t + + r_swap.dense = dense record.archetype = nil :: any record.row = nil :: any - record.dense = index_of_last_alive_entity + record.dense = i_swap - dense_array[index_of_deleted_entity] = last_alive_entity - dense_array[index_of_last_alive_entity] = ECS_GENERATION_INC(entity) + dense_array[dense] = e_swap + dense_array[i_swap] = ECS_GENERATION_INC(entity) +end + +local function world_exists(world: ecs_world_t, entity): boolean + return entity_index_try_get_any(world.entity_index, entity) ~= nil end local function world_contains(world: ecs_world_t, entity): boolean @@ -1350,8 +1464,6 @@ export type QueryInner = { world: World, } - - local function query_iter_init(query: ecs_query_data_t): () -> (number, ...any) local world_query_iter_next @@ -1795,7 +1907,7 @@ local function query_cached(query: ecs_query_data_t) local observable = world.observable :: ecs_observable_t local on_create_action = observable[EcsOnArchetypeCreate] if not on_create_action then - on_create_action = {} + on_create_action = {} :: Map observable[EcsOnArchetypeCreate] = on_create_action end local query_cache_on_create = on_create_action[A] @@ -1806,7 +1918,7 @@ local function query_cached(query: ecs_query_data_t) local on_delete_action = observable[EcsOnArchetypeDelete] if not on_delete_action then - on_delete_action = {} + on_delete_action = {} :: Map observable[EcsOnArchetypeDelete] = on_delete_action end local query_cache_on_delete = on_delete_action[A] @@ -2209,12 +2321,12 @@ local function world_query(world: ecs_world_t, ...) return q end - if idr == nil or map.size < idr.size then + if idr == nil or (map.size :: number) < (idr.size :: number) then idr = map end end - if not idr then + if idr == nil then return q end @@ -2295,9 +2407,9 @@ export type ComponentRecord = { flags: number, size: number, hooks: { - on_add: ((entity: Entity) -> ())?, - on_set: ((entity: Entity, data: any) -> ())?, - on_remove: ((entity: Entity) -> ())?, + on_add: ((entity: Entity, id: Entity, value: T) -> ())?, + on_change: ((entity: Entity, id: Entity, value: T) -> ())?, + on_remove: ((entity: Entity, id: Entity) -> ())?, }, } export type ComponentIndex = Map @@ -2308,6 +2420,8 @@ export type EntityIndex = { sparse_array: Map, alive_count: number, max_id: number, + range_begin: number?, + range_end: number? } local World = {} @@ -2326,9 +2440,11 @@ World.has = world_has World.target = world_target World.parent = world_parent World.contains = world_contains +World.exists = world_exists World.cleanup = world_cleanup World.each = world_each World.children = world_children +World.range = world_range local function world_new() local entity_index = { @@ -2338,6 +2454,8 @@ local function world_new() max_id = 0, } :: ecs_entity_index_t local self = setmetatable({ + archetype_edges = {}, + archetype_index = {} :: { [string]: Archetype }, archetypes = {} :: Archetypes, component_index = {} :: ComponentIndex, @@ -2345,7 +2463,7 @@ local function world_new() ROOT_ARCHETYPE = (nil :: any) :: Archetype, max_archetype_id = 0, - max_component_id = 0, + max_component_id = ecs_max_component_id, observable = {} :: Observable, }, World) :: any @@ -2363,7 +2481,7 @@ local function world_new() end world_add(self, EcsName, EcsComponent) - world_add(self, EcsOnSet, EcsComponent) + world_add(self, EcsOnChange, EcsComponent) world_add(self, EcsOnAdd, EcsComponent) world_add(self, EcsOnRemove, EcsComponent) world_add(self, EcsWildcard, EcsComponent) @@ -2371,7 +2489,7 @@ local function world_new() world_set(self, EcsOnAdd, EcsName, "jecs.OnAdd") world_set(self, EcsOnRemove, EcsName, "jecs.OnRemove") - world_set(self, EcsOnSet, EcsName, "jecs.OnSet") + world_set(self, EcsOnChange, EcsName, "jecs.OnChange") world_set(self, EcsWildcard, EcsName, "jecs.Wildcard") world_set(self, EcsChildOf, EcsName, "jecs.ChildOf") world_set(self, EcsComponent, EcsName, "jecs.Component") @@ -2384,27 +2502,44 @@ local function world_new() world_add(self, EcsChildOf, ECS_PAIR(EcsOnDeleteTarget, EcsDelete)) + for i = EcsRest + 1, ecs_max_tag_id do + entity_index_new_id(entity_index) + end + + for i, bundle in ecs_metadata do + for ty, value in bundle do + if value == NULL then + world_add(self, i, ty) + else + world_set(self, i, ty, value) + end + end + end + return self end World.new = world_new -export type Entity = { __T: T } -export type Id = { __T: T } +export type Entity = { __T: T } +export type Id = { __T: T } export type Pair = Id

type ecs_id_t = Id | Pair | Pair<"Tag", T> export type Item = (self: Query) -> (Entity, T...) export type Iter = (query: Query) -> () -> (Entity, T...) -export type Query = typeof(setmetatable({}, { - __iter = (nil :: any) :: Iter, -})) & { - iter: Iter, - with: (self: Query, ...Id) -> Query, - without: (self: Query, ...Id) -> Query, - archetypes: (self: Query) -> { Archetype }, - cached: (self: Query) -> Query, -} +export type Query = typeof(setmetatable( + {} :: { + iter: Iter, + with: (self: Query, ...Id) -> Query, + without: (self: Query, ...Id) -> Query, + archetypes: (self: Query) -> { Archetype }, + cached: (self: Query) -> Query, + }, + {} :: { + __iter: Iter + } +)) export type Observer = { callback: (archetype: Archetype) -> (), @@ -2431,6 +2566,9 @@ export type World = { observable: any, + --- Enforce a check on entities to be created within desired range + range: (self: World, range_begin: number, range_end: number?) -> (), + --- Creates a new entity entity: (self: World, id: Entity?) -> Entity, --- Creates a new entity located in the first 256 ids. @@ -2438,20 +2576,20 @@ export type World = { component: (self: World) -> Entity, --- Gets the target of an relationship. For example, when a user calls --- `world:target(id, ChildOf(parent), 0)`, you will obtain the parent entity. - target: (self: World, id: Entity, relation: Id, index: number?) -> Entity?, + target: (self: World, id: Entity, relation: Id, index: number?) -> Entity?, --- Deletes an entity and all it's related components and relationships. delete: (self: World, id: Entity) -> (), --- Adds a component to the entity with no value - add: (self: World, id: Entity, component: Id) -> (), + add: (self: World, id: Entity, component: Id) -> (), --- Assigns a value to a component on the given entity set: (self: World, id: Entity, component: Id, data: T) -> (), cleanup: (self: World) -> (), -- Clears an entity from the world - clear: (self: World, id: Entity) -> (), + clear: (self: World, id: Id) -> (), --- Removes a component from the given entity - remove: (self: World, id: Entity, component: Id) -> (), + remove: (self: World, id: Entity, component: Id) -> (), --- Retrieves the value of up to 4 components. These values may be nil. get: ((self: World, id: Entity, Id) -> A?) & ((self: World, id: Entity, Id, Id) -> (A?, B?)) @@ -2459,7 +2597,10 @@ export type World = { & (self: World, id: Entity, Id, Id, Id, Id) -> (A?, B?, C?, D?), --- Returns whether the entity has the ID. - has: (self: World, entity: Entity, ...Id) -> boolean, + has: ((World, Entity, A) -> boolean) + & ((World, Entity, A, B) -> boolean) + & ((World, Entity, A, B, C) -> boolean) + & (World, Entity, A, B, C, D) -> boolean, --- Get parent (target of ChildOf relationship) for entity. If there is no ChildOf relationship pair, it will return nil. parent:(self: World, entity: Entity) -> Entity, @@ -2467,9 +2608,12 @@ export type World = { --- Checks if the world contains the given entity contains:(self: World, entity: Entity) -> boolean, - each: (self: World, id: Id) -> () -> Entity, + --- Checks if the entity exists + exists: (self: World, entity: Entity) -> boolean, - children: (self: World, id: Id) -> () -> Entity, + each: (self: World, id: Id) -> () -> Entity, + + children: (self: World, id: Id) -> () -> Entity, --- Searches the world for entities that match a given query query: ((World, Id) -> Query) @@ -2497,14 +2641,19 @@ export type World = { -- return first -- end -- end +-- return { World = World :: { new: () -> World }, - world = World.new :: () -> World, + world = world_new :: () -> World, + component = (ECS_COMPONENT :: any) :: () -> Entity, + tag = (ECS_TAG :: any) :: () -> Entity, + meta = (ECS_META :: any) :: (id: Entity, id: Id, value: T) -> Entity, + is_tag = (ecs_is_tag :: any) :: (World, Id) -> boolean, OnAdd = EcsOnAdd :: Entity<(entity: Entity) -> ()>, OnRemove = EcsOnRemove :: Entity<(entity: Entity) -> ()>, - OnSet = EcsOnSet :: Entity<(entity: Entity, data: any) -> ()>, + OnChange = EcsOnChange :: Entity<(entity: Entity, data: any) -> ()>, ChildOf = EcsChildOf :: Entity, Component = EcsComponent :: Entity, Wildcard = EcsWildcard :: Entity, @@ -2523,12 +2672,14 @@ return { ECS_GENERATION_INC = ECS_GENERATION_INC, ECS_GENERATION = ECS_GENERATION, ECS_ID_IS_WILDCARD = ECS_ID_IS_WILDCARD, - ECS_ID_DELETE = ECS_ID_DELETE, + ECS_META_RESET = ECS_META_RESET, - IS_PAIR = ECS_IS_PAIR, - pair_first = ecs_pair_first, - pair_second = ecs_pair_second, + IS_PAIR = (ECS_IS_PAIR :: any) :: (pair: Pair) -> boolean, + ECS_PAIR_FIRST = ECS_PAIR_FIRST :: (pair: Pair) -> Id

, + ECS_PAIR_SECOND = ECS_PAIR_SECOND :: (pair: Pair) -> Id, + pair_first = (ecs_pair_first :: any) :: (world: World, pair: Pair) -> Id

, + pair_second = (ecs_pair_second :: any) :: (world: World, pair: Pair) -> Id, entity_index_get_alive = entity_index_get_alive, archetype_append_to_records = archetype_append_to_records, @@ -2538,11 +2689,6 @@ return { find_insert = find_insert, find_archetype_with = find_archetype_with, find_archetype_without = find_archetype_without, - archetype_init_edge = archetype_init_edge, - archetype_ensure_edge = archetype_ensure_edge, - init_edge_for_add = init_edge_for_add, - init_edge_for_remove = init_edge_for_remove, - create_edge_for_add = create_edge_for_add, create_edge_for_remove = create_edge_for_remove, archetype_traverse_add = archetype_traverse_add, archetype_traverse_remove = archetype_traverse_remove, diff --git a/pesde-rbx.toml b/pesde-rbx.toml index 62662ad..d0520fc 100644 --- a/pesde-rbx.toml +++ b/pesde-rbx.toml @@ -11,7 +11,7 @@ includes = [ license = "MIT" name = "marked/jecs" repository = "https://git.devmarked.win/marked/jecs-pesde" -version = "0.6.0-rc.1" +version = "0.6.0" [indices] default = "https://github.com/pesde-pkg/index" diff --git a/pesde.lock b/pesde.lock index 30c36fb..91f9895 100644 --- a/pesde.lock +++ b/pesde.lock @@ -2,5 +2,5 @@ # It is not intended for manual editing. format = 1 name = "marked/jecs" -version = "0.6.0-rc.1" +version = "0.6.0" target = "luau" diff --git a/pesde.toml b/pesde.toml index e533800..ecf59dc 100644 --- a/pesde.toml +++ b/pesde.toml @@ -11,7 +11,7 @@ includes = [ license = "MIT" name = "marked/jecs" repository = "https://git.devmarked.win/marked/jecs-pesde" -version = "0.6.0-rc.1" +version = "0.6.0" [indices] default = "https://github.com/pesde-pkg/index"