diff --git a/jecs/CHANGELOG.md b/jecs/CHANGELOG.md index 5773969..8fdd827 100644 --- a/jecs/CHANGELOG.md +++ b/jecs/CHANGELOG.md @@ -11,8 +11,6 @@ The format is based on [Keep a Changelog][kac], and this project adheres to ## [Unreleased] - `[world]`: - - Added `world:range` to restrict entity range - - Changed `world:entity` to accept the overload to create an entity at the desired index - 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 diff --git a/jecs/addons/observers.luau b/jecs/addons/observers.luau index 5f3f1f5..a7a8191 100644 --- a/jecs/addons/observers.luau +++ b/jecs/addons/observers.luau @@ -24,7 +24,7 @@ local function observers_new(world, description) end local entity_index = world.entity_index :: any - local function emplaced(entity, id, value) + local function emplaced(entity: jecs.Entity) local r = jecs.entity_index_try_get_fast( entity_index, entity :: any) @@ -45,106 +45,6 @@ local function observers_new(world, description) end end - -local function join(world, component) - local sparse_array = {} - local dense_array = {} - local values = {} - local max_id = 0 - - world:added(component, function(entity, id, value) - max_id += 1 - sparse_array[entity] = max_id - dense_array[max_id] = entity - values[max_id] = value - end) - - world:removed(component, function(entity, id) - local e_swap = dense_array[max_id] - local v_swap = values[max_id] - - local dense = sparse_array[entity] - dense_array[dense] = e_swap - values[dense] = v_swap - - sparse_array[entity] = nil - dense_array[max_id] = nil - values[max_id] = nil - end) - - world:changed(component, function(entity, id, value) - values[sparse_array[entity]] = value - end) - - return function() - local i = max_id - return function(): ...any - i -= 1 - if i == 0 then - return nil - end - local e = dense_array[i] - return e, values[i] - end - end -end - -local function query_changed(world, component) - assert(jecs.IS_PAIR(component) == false) - local callerid = debug.info(2, "sl") - - local tracker = world.trackers[callerid] - if not tracker then - local records = {} - local connections = {} - tracker = { - records = records, - connections = connections - } - world.trackers[callerid] = tracker - - table.insert(connections, world:added(component, function(entity, id, v) - tracker[entity] = { - new = v - } - end)) - table.insert(connections, world:changed(component, function(entity, id, v) - local record = tracker[entity] - record.old = record.new - record.new = v - end)) - - table.insert(connections, world:removed(component, function(entity, id) - local record = tracker[entity] - record.old = record.new - record.new = nil - end)) - end - - local entity = nil - local record = nil - return function() - entity, record = next(tracker, entity) - if entity == nil then - return - end - return entity, record - end -end - -local function spy_on_world_delete(world) - local world_delete = world.delete - world.delete = function(world, entity) - world_delete(world, entity) - for _, tracker in world.trackers do - tracker.records[entity] = nil - for _, connection in tracker.connections do - connection() - end - end - end -end - local function monitors_new(world, description) local query = description.query local callback = description.callback @@ -193,11 +93,10 @@ local function monitors_new(world, description) end local function observers_add(world: jecs.World & { [string]: any }): PatchedWorld - type Signal = { [jecs.Entity]: { (...any) -> () } } local signals = { - added = {} :: Signal, - emplaced = {} :: Signal, - removed = {} :: Signal + added = {}, + emplaced = {}, + removed = {} } world.added = function(_, component, fn) @@ -207,21 +106,13 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl if not listeners then listeners = {} signals.added[component] = listeners - local function on_add(entity: number, id: number, value: any) for _, listener in listeners :: any do listener(entity, id, value) end end - world:set(component, jecs.OnAdd, on_add) - end + world:set(component, jecs.OnAdd, on_add) end table.insert(listeners, fn) - return function() - local n = #listeners - local i = table.find(listeners, fn) - listeners[i] = listeners[n] - listeners[n] = nil - end end world.changed = function(_, component, fn) @@ -239,12 +130,6 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl world:set(component, jecs.OnChange, on_change) end table.insert(listeners, fn) - return function() - local n = #listeners - local i = table.find(listeners, fn) - listeners[i] = listeners[n] - listeners[n] = nil - end end world.removed = function(_, component, fn) @@ -262,12 +147,6 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl world:set(component, jecs.OnRemove, on_remove) end table.insert(listeners, fn) - return function() - local n = #listeners - local i = table.find(listeners, fn) - listeners[i] = listeners[n] - listeners[n] = nil - end end world.signals = signals @@ -276,8 +155,6 @@ local function observers_add(world: jecs.World & { [string]: any }): PatchedWorl world.monitor = monitors_new - world.trackers = {} - return world :: PatchedWorld end diff --git a/jecs/build.txt b/jecs/build.txt index e3fde5a..5224a55 100644 --- a/jecs/build.txt +++ b/jecs/build.txt @@ -1,2 +1,2 @@ -modified = ["addons/observers.luau", "CHANGELOG.md", "jecs.luau"] -version = "0.5.5-nightly.20250426T001101Z" +modified = ["addons/observers.luau", "jecs.luau"] +version = "0.5.5-nightly.20250423T001100Z" diff --git a/jecs/jecs.luau b/jecs/jecs.luau index 94415af..3f297da 100644 --- a/jecs/jecs.luau +++ b/jecs/jecs.luau @@ -62,8 +62,6 @@ 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 = { @@ -127,12 +125,6 @@ local ECS_INTERNAL_ERROR = [[ 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 @@ -193,10 +185,6 @@ 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 @@ -261,10 +249,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(entity_index: ecs_entity_index_t, entity: i53): i53? - local r = entity_index_try_get_any(entity_index, entity) +local function entity_index_get_alive(index: ecs_entity_index_t, entity: i53): i53? + local r = entity_index_try_get_any(index, entity) if r then - return entity_index.dense_array[r.dense] + return index.dense_array[r.dense] end return nil end @@ -292,15 +280,11 @@ 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] @@ -308,14 +292,11 @@ 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 - sparse_array[id] = { dense = alive_count } :: ecs_record_t + entity_index.sparse_array[id] = { dense = alive_count } :: ecs_record_t return id end @@ -602,10 +583,10 @@ local function id_record_ensure(world: ecs_world_t, id: number): ecs_id_record_t 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( + 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( + assert(target and entity_index_is_alive( entity_index, target), ECS_INTERNAL_ERROR) end @@ -738,101 +719,8 @@ local function archetype_create(world: ecs_world_t, id_types: { i24 }, ty, prev: return archetype end -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 dense == 0 then - dense = index - end - local any = dense_array[dense] - if any == entity then - if alive_count > dense then - r.dense = dense - return entity - end - local e_swap = dense_array[alive_count] - local r_swap = sparse_array[alive_count] - r_swap.dense = dense - r.dense = alive_count - dense_array[alive_count] = entity - dense_array[dense] = e_swap - return entity - end - - -- assert(any ~= 0) should never happen - - local e_swap = dense_array[alive_count] - local r_swap = sparse_array[alive_count] - - if dense <= alive_count then - alive_count += 1 - entity_index.alive_count = alive_count - end - - r_swap.dense = dense - r.dense = alive_count - dense_array[alive_count] = any - dense_array[dense] = e_swap - return any - 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) +local function world_entity(world: ecs_world_t): i53 + return entity_index_new_id(world.entity_index) end local function world_parent(world: ecs_world_t, entity: i53) @@ -2423,8 +2311,6 @@ export type EntityIndex = { sparse_array: Map, alive_count: number, max_id: number, - range_begin: number?, - range_end: number? } local World = {} @@ -2446,7 +2332,6 @@ World.contains = world_contains World.cleanup = world_cleanup World.each = world_each World.children = world_children -World.range = world_range local function world_new() local entity_index = { @@ -2485,7 +2370,16 @@ local function world_new() for i = EcsRest + 1, ecs_max_tag_id do -- Initialize built-in components entity_index_new_id(entity_index) - print("hm...", i) + 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 world_add(self, EcsName, EcsComponent) @@ -2510,16 +2404,6 @@ local function world_new() world_add(self, EcsChildOf, ECS_PAIR(EcsOnDeleteTarget, EcsDelete)) - 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 @@ -2570,9 +2454,6 @@ 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. @@ -2677,8 +2558,6 @@ return { ECS_META_RESET = ECS_META_RESET, IS_PAIR = (ECS_IS_PAIR :: any) :: (pair: Pair) -> boolean, - ECS_PAIR_FIRST = ECS_PAIR_FIRST, - ECS_PAIR_SECOND = ECS_PAIR_SECOND, 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, diff --git a/jecs/pesde-rbx.toml b/jecs/pesde-rbx.toml index e575978..bede318 100644 --- a/jecs/pesde-rbx.toml +++ b/jecs/pesde-rbx.toml @@ -3,7 +3,7 @@ includes = ["init.luau", "pesde.toml", "README.md", "CHANGELOG.md", "LICENSE", " license = "MIT" name = "marked/jecs_nightly" repository = "https://git.devmarked.win/marked/jecs-nightly" -version = "0.5.5-nightly.20250426T001101Z" +version = "0.5.5-nightly.20250423T001100Z" [indices] default = "https://github.com/pesde-pkg/index" diff --git a/jecs/pesde.toml b/jecs/pesde.toml index cba3853..21ecf1a 100644 --- a/jecs/pesde.toml +++ b/jecs/pesde.toml @@ -3,7 +3,7 @@ includes = ["init.luau", "pesde.toml", "README.md", "CHANGELOG.md", "LICENSE", " license = "MIT" name = "marked/jecs_nightly" repository = "https://git.devmarked.win/marked/jecs-nightly" -version = "0.5.5-nightly.20250426T001101Z" +version = "0.5.5-nightly.20250423T001100Z" [indices] default = "https://github.com/pesde-pkg/index" diff --git a/jecs/test.txt b/jecs/test.txt index 67f4934..c2e1b5f 100644 --- a/jecs/test.txt +++ b/jecs/test.txt @@ -1,2 +1,2 @@ passed = true -timestamp = "20250426T001103Z" +timestamp = "20250425T001100Z" diff --git a/jecs/test_fulllog.txt b/jecs/test_fulllog.txt index 89c1324..60d18b7 100644 --- a/jecs/test_fulllog.txt +++ b/jecs/test_fulllog.txt @@ -1,8 +1,6 @@ -hm... 271 -7.7 us  3 kB│ delete children of entity -9.3 us  1 kB│ remove friends of entity -349 ns  0  B│ simple deletion of entity -*created e1000v0 +7.3 us  2 kB│ delete children of entity +8.8 us  1 kB│ remove friends of entity +346 ns  0  B│ simple deletion of entity world:add() PASS│ idempotent PASS│ archetype move @@ -39,11 +37,7 @@ hm... 271 world:each() PASS│  -world:range() -PASS│  - world:entity() -PASS│ desired id PASS│ unique IDs PASS│ generations PASS│ pairs @@ -119,5 +113,5 @@ hm... 271 PASS│ #2 PASS│ #3 -72/72 test cases passed in 31.076 ms. +70/70 test cases passed in 32.159 ms. 0 fails diff --git a/jecs/wally.toml b/jecs/wally.toml index d23c40e..edcbfe1 100644 --- a/jecs/wally.toml +++ b/jecs/wally.toml @@ -5,4 +5,4 @@ license = "MIT" name = "mark-marks/jecs-nightly" realm = "shared" registry = "https://github.com/UpliftGames/wally-index" -version = "0.5.5-nightly.20250426T001101Z" +version = "0.5.5-nightly.20250423T001100Z"