diff --git a/CHANGELOG.md b/CHANGELOG.md index 5773969..12cd1ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,226 +1,169 @@ -# Jecs Changelog +# Changelog -All notable changes to this project will be documented in this file. +## Unreleased -The format is based on [Keep a Changelog][kac], and this project adheres to -[Semantic Versioning][semver]. - -[kac]: https://keepachangelog.com/en/1.1.0/ -[semver]: https://semver.org/spec/v2.0.0.html - -## [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 - - 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. - -## [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]`: - - 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 - -- `[world]`: - - Changed `world:cleanup` to traverse a header type for graph edges. (Edit) - - Fixed a regression that occurred when you call `world:set` following a `world:remove` using the same component - - Remove explicit error in JECS_DEBUG for `world:target` when not applying an index parameter -- `[typescript]` : - - Fixed `world.set` with NoInfer - -## [0.3.1] - 2024-10-01 - -- `[world]`: - - Added an index parameter to `world:target` - - Added a way to change the components limit via `_G.JECS_HI_COMPONENT_ID` - - Set it to whatever number you want but try to make it as close to the number of components you will use as possible - - Make sure to set this before importing jecs or else it will not work - - Added debug mode, enable via setting `_G.JECS_DEBUG` to true - - Make sure to set this before importing jecs or else it will not work - - Added `world:cleanup` which is called to cleanup empty archetypes manually - - Changed `world:delete` to delete archetypes that are dependent on the passed entity - - Changed `world:delete` to delete entity's children before the entity to prevent cycles -- `[query]`: - - Fixed the iterator to not drain by default -- `[typescript]` - - Fixed entry point of the package.json file to be `src` rather than `src/init` - - Fixed `query.next` returning a query object whereas it would be expected to return a tuple containing the entity and the corresponding component values - - Exported `query.archetypes` - - Changed `pair` to return a number instead of an entity - - Preventing direct usage of a pair as an entity while still allowing it to be used as a component - - Exported built-in components `ChildOf` and `Name` - - Exported `world.parent` - -## [0.2.10] - 2024-09-07 - -- `[world]`: - - Improved performance for hooks - - Changed `world:set` to be idempotent when setting tags -- `[traits]`: - - Added cleanup condition `jecs.OnDelete` for when the entity or component is deleted - - Added cleanup action `jecs.Remove` which removes instances of the specified (component) id from all entities - - This is the default cleanup action - - Added component trait `jecs.Tag` which allows for zero-cost components used as tags - - Setting data to a component with this trait will do nothing -- `[luau]`: - - Exported `world:contains()` - - Exported `query:drain()` - - Exported `Query` - - Improved types for the hook `OnAdd`, `OnSet`, `OnRemove` - - Changed functions to accept any ID including pairs in type parameters - - Applies to `world:add()`, `world:set()`, `world:remove()`, `world:get()`, `world:has()` and `world:query()` - - New exported type `Id = Entity | Pair` - - Changed `world:contains()` to return a `boolean` instead of an entity which may or may not exist - - Fixed `world:has()` to take the correct parameters - -## [0.2.2] - 2024-07-07 +## 0.6.0 ### Added - -- Added `query:replace(function(...T) return ...U end)` for replacing components in place - - Method is fast pathed to replace the data to the components for each corresponding entity +- `World:range` to restrict entity range to allow for e.g. reserving ids `1000..5000` for clients and everything above that (5000+) for entities from the server. This makes it possible to receive ids from a server that don't have to be mapped to local ids. +- `jecs.component`, `jecs.tag` and `jecs.meta` for preregistering ids and their metadata before a world +- Overload to `World:entity` to create an entity at the desired id. ### Changed - -- Iterator now goes backwards instead to prevent common cases of iterator invalidation - -## [0.2.1] - 2024-07-06 - -### Added - -- Added `jecs.Component` built-in component which will be added to ids created with `world:component()`. - - Used to find every component id with `query(jecs.Component) - -## [0.2.0] - 2024-07-03 - -### Added - -- Added `world:parent(entity)` and `jecs.ChildOf` respectively as first class citizen for building parent-child relationships. - - Give a parent to an entity with `world:add($source, pair(ChildOf, $target))` - - Use `world:parent(entity)` to find the target of the relationship -- Added user-facing Luau types - -### Changed - -- Improved iteration speeds 20-40% by manually indexing rather than using `next()` :scream: - -## [0.1.1] - 2024-05-19 - -### Added - -- Added `world:clear(entity)` for removing the components to the corresponding entity -- Added Typescript Types - -## [0.1.0] - 2024-05-13 - -### Changed - -- Optimized iterator - -## [0.1.0-rc.6] - 2024-05-13 - -### Added - -- Added a `jecs.Wildcard` term - - it lets you query any partially matched pairs - -## [0.1.0-rc.5] - 2024-05-10 - -### Added - -- Added Entity relationships for creating logical connections between entities -- Added `world:__iter method` which allows for iteration over the whole world to get every entity - - used for reconciling whole worlds such as via replication, saving/loading, etc -- Added `world:add(entity, component)` which adds a component to the entity - - it is an idempotent function, so calling it twice and in any order should be fine +- `World:clear` to remove the `ID` from every entity instead of the previous behaviour of removing all of the components on the entity. You should prefer deleting the entity instead for the previous behaviour. +- Entity ID layouts by putting the index in the lower bits, which should make every world function 1–5 nanoseconds faster. +- Hooks now pass the full component ID which is useful for pairs when you need both the relation and the target. +- Replaced `OnSet` with `OnChange`, which now only runs when the component ID was previously present on the entity. +- `OnAdd` now runs after the value has been set and also passes the component ID and the value. +- `OnRemove` now lazily looks up which archetype the entity will move to. This is meant to support interior structural changes within every hook. +- Optimized `world:has` for both single 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 this function multiple times. ### 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. -- Fixed component overriding when in disorder - - Previously setting the components in different order results in it overriding component data because it incorrectly mapped the index of the column. So it took the index from the source archetype rather than the destination archetype - -## [0.0.0-prototype.rc.3] - 2024-05-01 +## 0.5.0 ### Added - -- Added observers -- Added an arm to query `query:without()` for chaining invariants. +- `World:each` to find entities with a specific Tag. +- `World:children` to find children of an entity. +- `Query:cached` to add a query cache that updates itself when an archetype matching the query gets created or deleted. ### Changed +- Inference of entities' types using user-defined type functions. +- `Pair` to return `Second` if `First` is a `Tag`; otherwise, returns `First`. -- Separates ranges for components and entity IDs. +### Fixed +- `World:target` not giving adjacent pairs. - - IDs created with `world:component()` will promote array lookups rather than map lookups in the `component_index` which is a significant boost +## 0.4.0 -- No longer caches the column pointers directly and instead the column indices which stay persistent even when data is reallocated during swap-removals - - This was an issue with the iterator being invalidated when you move an entity to a different archetype. +### Added +- Recycling support to `world:entity` so reused entity IDs now increment generation. -### Fixedhttps://github.com/Ukendio/jecs/releases/tag/v0.0.0-prototype.rc.3 - -- Fixed a bug where changing an existing component would be slow because it was always appending changing the row of the entity record - - The fix dramatically improves times where it is basically down to just the speed of setting a field in a table - -## [0.0.0-prototype.rc.2] - 2024-04-26 +### Removed +- `Query:drain` +- `Query:next` +- `Query:replace` ### Changed +- `jecs.Pair` type in Luau now returns the first element's type to avoid manual typecasting. -- Optimized the creation of the query - - It will now finds the smallest archetype map to iterate over -- Optimized the query iterator +### Fixed +- `Query:archetypes` now correctly takes `self`. - - It will now populates iterator with columns for faster indexing +## 0.3.2 - 2024-10-01 -- Renamed the insertion method from world:add to world:set to better reflect what it does. +### Changed +- `World:cleanup` to traverse a header type for graph edges. -## [0.0.0-prototype.rc.2] - 2024-04-23 +### Fixed +- Regression when calling `World:set` after `World:remove` on the same component. +- Removed explicit error in `JECS_DEBUG` for `World:target` missing index. +- `World.set` type inference with `NoInfer` in TypeScript. -- Initial release +## 0.3.1 - 2024-10-01 -[unreleased]: https://github.com/ukendio/jecs/compare/v0.0.0.0-prototype.rc.2...HEAD -[0.2.2]: https://github.com/ukendio/jecs/releases/tag/v0.2.2 -[0.2.1]: https://github.com/ukendio/jecs/releases/tag/v0.2.1 -[0.2.0]: https://github.com/ukendio/jecs/releases/tag/v0.2.0 -[0.1.1]: https://github.com/ukendio/jecs/releases/tag/v0.1.1 -[0.1.0]: https://github.com/ukendio/jecs/releases/tag/v0.1.0 -[0.1.0-rc.6]: https://github.com/ukendio/jecs/releases/tag/v0.1.0-rc.6 -[0.1.0-rc.5]: https://github.com/ukendio/jecs/releases/tag/v0.1.0-rc.5 -[0.0.0-prototype-rc.3]: https://github.com/ukendio/jecs/releases/tag/v0.0.0-prototype.rc.3 -[0.0.0-prototype.rc.2]: https://github.com/ukendio/jecs/releases/tag/v0.0.0-prototype.rc.2 -[0.0.0-prototype-rc.1]: https://github.com/ukendio/jecs/releases/tag/v0.0.0-prototype.rc.1 +### Added +- Index parameter to `World:target`. +- Global config `_G.JECS_HI_COMPONENT_ID` to change component ID limit (must be set before importing JECS). +- Debug mode via `_G.JECS_DEBUG` (must be set before importing JECS). +- `world:cleanup` to manually clean up empty archetypes. + +### Changed +- `world:delete` now also deletes dependent archetypes and child entities. + +### Fixed +- `Query` iterator to not drain by default. +- TypeScript package entry to `src` instead of `src/init`. +- `Query.next` now returns expected result tuple in typescript. +- `pair` returns a number instead of entity to prevent misuse. +- Exported built-in components `ChildOf`, `Name`, and `world.parent`. + +## 0.2.10 + +### Added +- Trait `jecs.Tag` for zero-cost tag components. +- Cleanup conditions: `jecs.OnDelete`, `jecs.Remove`. + +### Changed +- `world:set` is now idempotent when setting tags. + +### Fixed +- Improved performance for hooks. +- Exported types and functions: `world:contains()`, `query:drain()`, `Query`. +- Hook types: `OnAdd`, `OnSet`, `OnRemove`. +- ID flexibility for `add`, `set`, `remove`, `get`, `has`, `query`. +- `world:contains()` now returns `boolean`. +- `world:has()` parameters now correct. + +## 0.2.2 + +### Added +- `query:replace(fn)` for in-place replacement of component values. + +### Changed +- Iterator now goes backwards to avoid invalidation during iteration. + +## 0.2.1 + +### Added +- Built-in `jecs.Component` used to find all component IDs. + +## 0.2.0 + +### Added +- `world:parent(entity)` and `jecs.ChildOf` for parent-child relationships. + +### Changed +- Iteration performance improved by 20–40% through manual indexing. + +## 0.1.1 + +### Added +- `world:clear(entity)` for removing all components from an entity. +- TypeScript types. + +## 0.1.0 + +### Changed +- Optimized iterator. + +## 0.1.0-rc.6 + +### Added +- `jecs.Wildcard` term to query partially matched pairs. + +## 0.1.0-rc.5 + +### Added +- Entity relationships. +- `world:__iter()` for full world iteration. +- `world:add(entity, component)` (idempotent). + +### Fixed +- Component overriding when set out of order. + +## 0.0.0-prototype.rc.3 + +### Added +- Observers. +- `query:without()` for invariant queries. + +### Changed +- Separate ID ranges for entities and components. +- Avoid caching pointers; cache stable column indices instead. + +### Fixed +- Slow component updates due to unnecessary row changes. + +## 0.0.0-prototype.rc.2 - 2024-04-26 + +### Changed +- Query now uses smallest archetype map. +- Optimized query iterator. +- Renamed `world:add` to `world:set`. + +## 0.0.0-prototype.rc.1 + +### Added +- Initial release. diff --git a/addons/observers.luau b/addons/observers.luau index ef85d85..0175060 100644 --- a/addons/observers.luau +++ b/addons/observers.luau @@ -1,16 +1,16 @@ local jecs = require("@jecs") -type Observer = { +type Observer = { callback: (jecs.Entity) -> (), - query: jecs.Query, + query: jecs.Query<...jecs.Id>, } export type PatchedWorld = jecs.World & { - added: (PatchedWorld, jecs.Id, (e: jecs.Entity, id: jecs.Id, value: T) -> ()) -> () -> (), - removed: (PatchedWorld, jecs.Id, (e: jecs.Entity, id: jecs.Id) -> ()) -> () -> (), - changed: (PatchedWorld, jecs.Id, (e: jecs.Entity, id: jecs.Id, value: T) -> ()) -> () -> (), - observer: (PatchedWorld, Observer) -> (), - monitor: (PatchedWorld, Observer) -> (), + added: (PatchedWorld, jecs.Id, (jecs.Entity, jecs.Id, T) -> ()) -> () -> (), + removed: (PatchedWorld, jecs.Id, (jecs.Entity, jecs.Id) -> ()) -> () -> (), + changed: (PatchedWorld, jecs.Id, (jecs.Entity, jecs.Id, T) -> ()) -> () -> (), + observer: (PatchedWorld, Observer) -> (), + monitor: (PatchedWorld, Observer) -> (), } local function observers_new(world, description) diff --git a/demo/src/ReplicatedStorage/systems/receive_replication.luau b/demo/src/ReplicatedStorage/systems/receive_replication.luau index 7574f34..68376f8 100644 --- a/demo/src/ReplicatedStorage/systems/receive_replication.luau +++ b/demo/src/ReplicatedStorage/systems/receive_replication.luau @@ -5,15 +5,19 @@ local collect = require("../collect") local client_ids = {} local function ecs_map_get(world: types.World, id: types.Entity) - local deserialised_id = 0 - - if not world:exists(id) or not world:contains(id) then - deserialised_id = world:entity(id) + local deserialised_id = client_ids[id] + if not deserialised_id then + if world:has(id, jecs.Name) then + deserialised_id = world:entity(id) + else + if world:exists(id) then + deserialised_id = world:entity() + else + deserialised_id = world:entity(id) + end + end client_ids[id] = deserialised_id - else - deserialised_id = client_ids[id] end - return deserialised_id end diff --git a/jecs.d.ts b/jecs.d.ts index 7e53388..01239fd 100644 --- a/jecs.d.ts +++ b/jecs.d.ts @@ -108,6 +108,13 @@ export class World { */ constructor(); + /** + * Enforces a check for entities to be created within a desired range. + * @param range_begin The starting point + * @param range_end The end point (optional) + */ + range(range_begin: number, range_end?: number): void; + /** * Creates a new entity. * @returns An entity (Tag) with no data. @@ -130,19 +137,6 @@ export class World { */ target(entity: Entity, relation: Entity, index?: number): Entity | undefined; - /** - * Cleans up the world by removing empty archetypes and rebuilding the archetype collections. - * This helps maintain memory efficiency by removing unused archetype definitions. - */ - cleanup(): void; - - /** - * Clears all components and relationships from the given entity, but - * does not delete the entity from the world. - * @param entity The entity to clear. - */ - clear(entity: Entity): void; - /** * Deletes an entity (and its components/relationships) from the world entirely. * @param entity The entity to delete. @@ -164,6 +158,19 @@ export class World { */ set>(entity: Entity, component: E, value: InferComponent): void; + /** + * Cleans up the world by removing empty archetypes and rebuilding the archetype collections. + * This helps maintain memory efficiency by removing unused archetype definitions. + */ + cleanup(): void; + + /** + * Clears all components and relationships from the given entity, but + * does not delete the entity from the world. + * @param entity The entity to clear. + */ + clear(entity: Entity): void; + /** * Removes a component from the given entity. * @param entity The target entity. @@ -191,12 +198,6 @@ export class World { */ has(entity: Entity, ...components: Id[]): boolean; - /** - * Checks if an entity exists in the world. - * @param entity The entity to verify. - */ - contains(entity: Entity): boolean; - /** * Gets the parent (the target of a `ChildOf` relationship) for an entity, * if such a relationship exists. @@ -205,11 +206,17 @@ export class World { parent(entity: Entity): Entity | undefined; /** - * Searches the world for entities that match specified components. - * @param components The list of components to query. - * @returns A Query object to iterate over results. + * Checks if an entity exists in the world. + * @param entity The entity to verify. */ - query(...components: T): Query>; + contains(entity: Entity): boolean; + + /** + * Checks if an entity with the given ID is currently alive, ignoring its generation. + * @param entity The entity to verify. + * @returns boolean true if any entity with the given ID exists (ignoring generation), false otherwise + */ + exists(entity: Entity): boolean; /** * Returns an iterator that yields all entities that have the specified component or relationship. @@ -225,8 +232,24 @@ export class World { * @returns An iterator function that yields child entities */ children(parent: Entity): IterableFunction; + + /** + * Searches the world for entities that match specified components. + * @param components The list of components to query. + * @returns A Query object to iterate over results. + */ + query(...components: T): Query>; } +export function component(): Entity; + +export function tag(): Entity; + +// note: original types had id: Entity, id: Id, which does not work with TS. +export function meta(e: Entity, id: Id, value?: T): Entity + +export function is_tag(world: World, id: Id): boolean; + /** * Creates a composite key (pair) * @param pred The first entity (predicate) diff --git a/jecs.luau b/jecs.luau index 35cf16a..7f47472 100644 --- a/jecs.luau +++ b/jecs.luau @@ -780,7 +780,11 @@ local function world_entity(world: ecs_world_t, entity: i53?): i53 local any = dense_array[dense] if dense <= alive_count then - return any + 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] @@ -790,9 +794,9 @@ local function world_entity(world: ecs_world_t, entity: i53?): i53 r_swap.dense = dense r.dense = alive_count dense_array[dense] = e_swap - dense_array[alive_count] = any + dense_array[alive_count] = entity - return any + return entity else for i = max_id + 1, index do sparse_array[i] = { dense = i } :: ecs_record_t @@ -2646,10 +2650,10 @@ return { 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) -> ()>, - OnChange = EcsOnChange :: Entity<(entity: Entity, data: any) -> ()>, + + OnAdd = EcsOnAdd :: Entity<(entity: Entity, id: Id, data: T) -> ()>, + OnRemove = EcsOnRemove :: Entity<(entity: Entity, id: Id) -> ()>, + OnChange = EcsOnChange :: Entity<(entity: Entity, id: Id, data: T) -> ()>, ChildOf = EcsChildOf :: Entity, Component = EcsComponent :: Entity, Wildcard = EcsWildcard :: Entity, diff --git a/package.json b/package.json index 069ad9e..f3cf774 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@rbxts/jecs", - "version": "0.6.0-rc.1", + "version": "0.6.0", "description": "Stupidly fast Entity Component System", "main": "jecs.luau", "repository": { diff --git a/test/addons/observers.luau b/test/addons/observers.luau index 1d5a99e..b255625 100644 --- a/test/addons/observers.luau +++ b/test/addons/observers.luau @@ -23,7 +23,6 @@ TEST("addons/observers", function() world:set(world:entity(), A, true) CHECK(count == 3) - print(count) end do CASE "Ensure ordering between signals and observers" diff --git a/test/tests.luau b/test/tests.luau index 61c089d..9ce5023 100644 --- a/test/tests.luau +++ b/test/tests.luau @@ -672,7 +672,7 @@ TEST("world:range()", function() local e2v1 = world:entity(399) CHECK(world:contains(e2v1)) CHECK(ECS_ID(e2v1) == 399) - CHECK(ECS_GENERATION(e2v1) == 1) + CHECK(ECS_GENERATION(e2v1) == 0) end do CASE "over range start" @@ -685,12 +685,12 @@ TEST("world:range()", function() local e2v1 = world:entity(405) CHECK(world:contains(e2v1)) CHECK(ECS_ID(e2v1) == 405) - CHECK(ECS_GENERATION(e2v1) == 1) + CHECK(ECS_GENERATION(e2v1) == 0) - do - local _e2v1 = world:entity(405) - CHECK(_e2v1 == e2v1) - end + world:delete(e2v1) + local e2v2 = world:entity(e2v1) + CHECK(ECS_ID(e2v2) == 405) + CHECK(ECS_GENERATION(e2v2) == 0) end end) diff --git a/wally.toml b/wally.toml index af3bb5d..745cab1 100644 --- a/wally.toml +++ b/wally.toml @@ -1,6 +1,6 @@ [package] name = "ukendio/jecs" -version = "0.6.0-rc.1" +version = "0.6.0" registry = "https://github.com/UpliftGames/wally-index" realm = "shared" license = "MIT"