93 Commits

Author SHA1 Message Date
BlackMATov
ffb0e37b7a update roadmap 2025-09-01 16:10:23 +07:00
BlackMATov
a63b0d4400 Revert "temp remove pairs to merge other changes to dev"
This reverts commit fb6d13ca74.
2025-09-01 15:50:48 +07:00
BlackMATov
fb6d13ca74 temp remove pairs to merge other changes to dev 2025-09-01 15:34:03 +07:00
BlackMATov
8f61a14db6 update pairs todos 2025-09-01 15:09:29 +07:00
BlackMATov
0c016f1b67 return xxx_set debug mode 2025-09-01 01:59:00 +07:00
BlackMATov
26bf586140 primary/secondary functon set doesn't work with pairs as entity now 2025-09-01 01:53:29 +07:00
BlackMATov
46f1516a55 style fixes 2025-09-01 01:10:17 +07:00
BlackMATov
4cd8393546 public api works only with non-pair ids in has/get functions 2025-09-01 00:33:10 +07:00
BlackMATov
22302cee75 primary/secondary iterators for pair entities 2025-08-29 18:19:19 +07:00
BlackMATov
d4a7c7b77c more pair checks 2025-08-29 05:56:23 +07:00
BlackMATov
12beee6eec debug mode for pack/unpack and pair/unpair 2025-08-26 05:42:04 +07:00
BlackMATov
71a7d382c1 more type annots 2025-08-26 04:13:50 +07:00
BlackMATov
f2a8ee5b83 little style fixes 2025-08-24 16:14:26 +07:00
BlackMATov
81bf1d91e9 Revert "temp remove pairs to merge other changes to dev" 2025-08-20 23:27:08 +07:00
BlackMATov
6b4c6f2a9a update changelog 2025-08-20 23:17:12 +07:00
BlackMATov
7eb4bfd62d Merge branch 'feature/temp_remove_pairs' into dev 2025-08-20 23:11:55 +07:00
BlackMATov
1b49f4fcd0 temp remove pairs to merge other changes to dev 2025-08-20 23:04:11 +07:00
BlackMATov
77bc6c298e remove 'options' param from evolved.pack 2025-08-20 22:23:25 +07:00
BlackMATov
008df17ee6 fix readme formatting 2025-08-20 05:41:06 +07:00
BlackMATov
ba3018213e builder wildcard has/remove first impl 2025-08-20 05:16:18 +07:00
BlackMATov
66aec17052 little naming fixes 2025-08-20 02:15:12 +07:00
BlackMATov
91edfa9da9 move example to samples/systems 2025-08-19 05:59:41 +07:00
BlackMATov
9b796b2a8d reduce pair function calls 2025-08-19 04:14:23 +07:00
BlackMATov
dcc5190466 evolved.is_pair/is_wildcard functions 2025-08-19 02:50:12 +07:00
BlackMATov
9e74ddf9c3 mark INTERNAL trait as unique and explicit 2025-08-18 19:45:46 +07:00
BlackMATov
26de93405e find smallest minor chunks for wildcard execute 2025-08-18 19:43:30 +07:00
BlackMATov
be64359177 update roadmap 2025-08-18 05:26:49 +07:00
BlackMATov
3b411cce25 manual param validation, remove some cases 2025-08-18 03:52:53 +07:00
BlackMATov
4a2088e833 wildcard queries and wildcard fuzz test 2025-08-17 21:53:22 +07:00
BlackMATov
e9084f818b alive/empty/has/get function work only with primary fragment in pairs now 2025-08-17 21:45:15 +07:00
BlackMATov
0e6f23d30b rnd gc for fuzz tests 2025-08-15 23:48:05 +07:00
BlackMATov
3f7ab3cf4d additional major/minor chunks for all chunks with pairs 2025-08-15 23:47:09 +07:00
BlackMATov
bd337cefe1 store pair indices in chunk instead full fragments 2025-08-12 06:16:17 +07:00
BlackMATov
2ed3be7a4a unload evolved before fuzz/bench 2025-08-09 02:36:07 +07:00
BlackMATov
a8fda4a22a add min fuzz/bench iters and time 2025-08-07 04:06:42 +07:00
BlackMATov
3e4b0d02c1 store pair majors/minors in main chunk storages 2025-08-05 05:47:14 +07:00
BlackMATov
676aae4402 update chunk flags for pair major chunks 2025-08-03 03:56:51 +07:00
BlackMATov
35c6592418 required fragments work with pairs by primary fragment 2025-08-01 06:46:43 +07:00
BlackMATov
dbca453bbb alive/empty/has/get function work with a primary fragment of pairs 2025-08-01 06:00:29 +07:00
BlackMATov
025a9d4d8c more pair checks 2025-07-30 02:11:08 +07:00
BlackMATov
b6e4bfe608 remove wildcard-set 2025-07-21 22:09:51 +07:00
BlackMATov
90e7bb25ef mark all internal fragments 2025-07-12 00:40:26 +07:00
BlackMATov
d6b16df401 chunk purging with pairs 2025-07-11 22:36:56 +07:00
BlackMATov
d86c85d522 clear pair chunks when pair fragments are destroyed 2025-07-11 18:44:01 +07:00
BlackMATov
f7e4dfb30c remove wildcard get tests 2025-07-08 06:19:18 +07:00
BlackMATov
f4671e5a64 wildcard set 2025-07-08 06:05:14 +07:00
BlackMATov
6603399ee6 evolved.each function documentation 2025-07-07 14:17:11 +07:00
BlackMATov
d91b087c76 style fixes 2025-07-07 13:53:54 +07:00
BlackMATov
041777eb23 update readme 2025-07-07 12:46:17 +07:00
BlackMATov
b8e0345e02 primary/secondary pair iterators 2025-07-07 05:33:45 +07:00
BlackMATov
aa3717d290 update roadmap 2025-07-05 20:04:23 +07:00
BlackMATov
83e09183a2 alive/empty/has for new ids/pairs 2025-07-05 20:04:15 +07:00
BlackMATov
837302c533 chunk_has_all/any for new ids/pairs 2025-07-05 01:43:39 +07:00
BlackMATov
27b134e6c0 new pair flags in chunks, with/without_xxx functions for new ids and pairs 2025-07-04 23:43:46 +07:00
BlackMATov
1c89e3853c new way to pack identifiers and pairs 2025-07-02 00:35:33 +07:00
BlackMATov
5eb8902d5a new evolved.name function 2025-06-30 23:21:46 +07:00
BlackMATov
b5d8ced4c8 universal pack/unpack functions with optional flags 2025-06-20 20:06:42 +07:00
BlackMATov
d24ec1ac8e make the without_fragment function set slightly faster 2025-06-19 08:30:14 +07:00
BlackMATov
8d7435064d additional debug checks for pair/unpair 2025-06-18 22:08:29 +07:00
BlackMATov
3291ad7479 simplify alive function set for now 2025-06-18 21:56:47 +07:00
BlackMATov
71cfdff3b7 proof of concept chunk_without for pair wildcards 2025-06-18 03:09:51 +07:00
BlackMATov
04b36f901b skip paired fragments while gathering required fragments 2025-06-18 01:50:29 +07:00
BlackMATov
063acc778b more effective has function set for pair wildcards 2025-06-18 01:27:45 +07:00
BlackMATov
78ad8bd53e new assoc list helper functions 2025-06-17 23:34:27 +07:00
BlackMATov
14055fbadf set/remove/clear/destroy for pairs 2025-06-16 21:56:22 +07:00
BlackMATov
697a041832 has/get function set for pairs 2025-06-16 21:36:20 +07:00
BlackMATov
9a2a62ec89 empty function set for pairs 2025-06-16 21:31:41 +07:00
BlackMATov
4f33796b97 has function set for pairs 2025-06-16 21:15:52 +07:00
BlackMATov
1e9005e468 alive function set for pairs 2025-06-15 07:19:21 +07:00
BlackMATov
c7402cbb05 simplify validation functions 2025-06-15 07:17:39 +07:00
BlackMATov
5375c0bdea dummy ANY predefined fragment 2025-06-15 07:11:39 +07:00
BlackMATov
44d2572530 basic pairs construction 2025-06-15 07:09:36 +07:00
BlackMATov
56a5fb8265 disable gc for fuzz tests 2025-06-09 19:50:49 +07:00
BlackMATov
21aae4ae86 update ROADMAP 2025-06-09 18:57:07 +07:00
BlackMATov
be22c2c85b update README 2025-06-09 07:37:32 +07:00
BlackMATov
9a5ea20778 v1.1.0 2025-06-06 21:25:08 +07:00
BlackMATov
0112d0747a Merge branch 'feature/requires_fragment' into dev 2025-06-06 21:21:59 +07:00
BlackMATov
c222a49257 fix the cloning with requires behavior for removed required fragments 2025-06-06 21:20:38 +07:00
BlackMATov
fbd9f9f970 additional requries-fragment tests 2025-06-06 18:32:56 +07:00
BlackMATov
d8c9481f13 Merge branch 'dev' into feature/requires_fragment 2025-06-05 14:46:47 +07:00
BlackMATov
47fcc9cc13 additional requries-fragment test 2025-06-05 14:46:16 +07:00
BlackMATov
cb98a4e461 update README 2025-05-31 07:14:03 +07:00
BlackMATov
a559c73c35 update README to reflect the new REQUIRES trait feature 2025-05-30 03:41:42 +07:00
BlackMATov
ba5ee22f5d little improvements REQUIRES fragments 2025-05-30 01:26:56 +07:00
BlackMATov
50afb722d1 proof of concept REQUIRES fragment impl 2025-05-29 17:08:40 +07:00
BlackMATov
8eccd461fd add pack/unpack fuzz test 2025-05-28 23:34:14 +07:00
BlackMATov
c323131d1e add pack/unpack fuzz test 2025-05-28 23:33:57 +07:00
BlackMATov
8038031b51 has_required_fragments chunk flag 2025-05-28 22:44:18 +07:00
BlackMATov
1399820d71 new REQUIRES dummy fragment 2025-05-28 15:06:44 +07:00
BlackMATov
3a64bac8e2 systems can be queries themselves 2025-05-26 17:37:40 +07:00
BlackMATov
5ae0e77f20 update GUIDES 2025-05-25 22:30:45 +07:00
BlackMATov
e78c4e8d6b update README 2025-05-24 18:39:17 +07:00
BlackMATov
9c6e53e610 update README 2025-05-23 19:57:54 +07:00
24 changed files with 6730 additions and 946 deletions

5
.luacov Normal file
View File

@@ -0,0 +1,5 @@
modules = {
['evolved'] = 'evolved.lua'
}
reporter = 'html'
reportfile = 'luacov.report.html'

View File

@@ -4,5 +4,12 @@
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true,
"files.trimTrailingWhitespace": true
},
"markdown.extension.toc.levels": "2..6",
"markdown.extension.toc.omittedFromToc": {
"README.md": [
"# Changelog",
"# API Reference"
]
}
}

22
GUIDES.md Normal file
View File

@@ -0,0 +1,22 @@
# Guidelines
## Checklists
### New Version Releasing
1. Ensure all tests pass on CI.
2. Update the version number in `evolved.lua`.
3. Update the **Changelog** section in `README.md`.
4. Create a new rockspec file in `rockspecs`.
5. Commit the changes with a message like `vX.Y.Z`.
6. Push and merge the changes to the `main` branch.
7. Create the release on GitHub.
8. Upload the new package to LuaRocks.
### Adding a New Top-Level Function
1. Insert the new function into the `evolved` table in `evolved.lua`.
2. Create tests for the function in `develop/testing/function_name_tests.lua`.
3. Add the new test to `develop/all.lua`.
4. Document the function in the **Cheat Sheet** and **API Reference** sections of `README.md`.
5. Provide a description in the **Overview** section of `README.md`.
6. Describe the update in the **Changelog** section of `README.md`.

717
README.md
View File

@@ -1,4 +1,4 @@
# evolved.lua (work in progress)
# evolved.lua
> Evolved ECS (Entity-Component-System) for Lua
@@ -19,6 +19,47 @@
[evolved]: https://github.com/BlackMATov/evolved.lua
- [Introduction](#introduction)
- [Performance](#performance)
- [Simplicity](#simplicity)
- [Flexibility](#flexibility)
- [Installation](#installation)
- [Quick Start](#quick-start)
- [Overview](#overview)
- [Identifiers](#identifiers)
- [Entities, Fragments, and Components](#entities-fragments-and-components)
- [Traits](#traits)
- [Singletons](#singletons)
- [Chunks](#chunks)
- [Structural Changes](#structural-changes)
- [Spawning Entities](#spawning-entities)
- [Entity Builders](#entity-builders)
- [Access Operations](#access-operations)
- [Iterating Over Fragments](#iterating-over-fragments)
- [Modifying Operations](#modifying-operations)
- [Debug Mode](#debug-mode)
- [Queries](#queries)
- [Deferred Operations](#deferred-operations)
- [Batch Operations](#batch-operations)
- [Systems](#systems)
- [Predefined Traits](#predefined-traits)
- [Fragment Tags](#fragment-tags)
- [Fragment Hooks](#fragment-hooks)
- [Unique Fragments](#unique-fragments)
- [Explicit Fragments](#explicit-fragments)
- [Shared Components](#shared-components)
- [Fragment Requirements](#fragment-requirements)
- [Destruction Policies](#destruction-policies)
- [Cheat Sheet](#cheat-sheet)
- [Aliases](#aliases)
- [Predefs](#predefs)
- [Core Functions](#core-functions)
- [Relation Functions](#relation-functions)
- [Classes](#classes)
- [Chunk](#chunk)
- [Builder](#builder)
- [License](#license)
## Introduction
`evolved.lua` is a fast and flexible ECS (Entity-Component-System) library for Lua. It is designed to be simple and easy to use, while providing all the features needed to create complex systems with blazing performance. Before we start exploring the library, let's take a look at the main advantages of using `evolved.lua`:
@@ -41,211 +82,21 @@ And yes, the library has some unusual concepts at its core, but once you get the
On the other hand, `evolved.lua` tries to be minimalistic and does not provide features that can be implemented outside the library. I'm trying to find a balance between minimalism and the number of possibilities, which forces me to make flexible decisions in the library's design. I hope you will find this balance acceptable.
## Requirements
- [lua](https://www.lua.org/) **>= 5.1**
- [luajit](https://luajit.org/) **>= 2.0**
## Installation
You can install `evolved.lua` using [luarocks](https://luarocks.org/) with the following command:
`evolved.lua` is a single-file pure Lua library and does not require any external dependencies. It is designed to work with [Lua 5.1](https://www.lua.org/) and later, [LuaJIT](https://luajit.org/), and [Luau](https://luau.org/) (Roblox).
All you need to start using the library is the [evolved.lua](./evolved.lua) source file. You can download it from the [releases](https://github.com/BlackMATov/evolved.lua/releases) page or clone the [repository](https://github.com/BlackMATov/evolved.lua) and copy the file to your project.
If you are using [LuaRocks](https://luarocks.org/), you can install the library using the following command:
```bash
luarocks install evolved.lua
```
Or just clone the [repository](https://github.com/BlackMATov/evolved.lua) and copy the [evolved.lua](evolved.lua) file to your project.
## Quick Start
To start using `evolved.lua`, read the [Overview](#overview) section first. It will give you a basic understanding of how the library works and how to use it. After that, check the full-featured [Example](develop/example.lua), which demonstrates complex usage of the library. Finally, refer to the [Cheat Sheet](#cheat-sheet) for a quick reference of all the functions and classes provided by the library.
Enjoy! :suspect:
## Cheat Sheet
### Aliases
```
id :: implementation-specific
entity :: id
fragment :: id
query :: id
system :: id
component :: any
storage :: component[]
default :: component
duplicate :: {component -> component}
execute :: {chunk, entity[], integer}
prologue :: {}
epilogue :: {}
set_hook :: {entity, fragment, component, component?}
assign_hook :: {entity, fragment, component, component}
insert_hook :: {entity, fragment, component}
remove_hook :: {entity, fragment, component}
each_state :: implementation-specific
execute_state :: implementation-specific
each_iterator :: {each_state? -> fragment?, component?}
execute_iterator :: {execute_state? -> chunk?, entity[]?, integer?}
```
### Predefs
```
TAG :: fragment
NAME :: fragment
UNIQUE :: fragment
EXPLICIT :: fragment
DEFAULT :: fragment
DUPLICATE :: fragment
PREFAB :: fragment
DISABLED :: fragment
INCLUDES :: fragment
EXCLUDES :: fragment
ON_SET :: fragment
ON_ASSIGN :: fragment
ON_INSERT :: fragment
ON_REMOVE :: fragment
GROUP :: fragment
QUERY :: fragment
EXECUTE :: fragment
PROLOGUE :: fragment
EPILOGUE :: fragment
DESTRUCTION_POLICY :: fragment
DESTRUCTION_POLICY_DESTROY_ENTITY :: id
DESTRUCTION_POLICY_REMOVE_FRAGMENT :: id
```
### Functions
```
id :: integer? -> id...
pack :: integer, integer -> id
unpack :: id -> integer, integer
defer :: boolean
commit :: boolean
spawn :: <fragment, component>? -> entity
clone :: entity -> <fragment, component>? -> entity
alive :: entity -> boolean
alive_all :: entity... -> boolean
alive_any :: entity... -> boolean
empty :: entity -> boolean
empty_all :: entity... -> boolean
empty_any :: entity... -> boolean
has :: entity, fragment -> boolean
has_all :: entity, fragment... -> boolean
has_any :: entity, fragment... -> boolean
get :: entity, fragment... -> component...
set :: entity, fragment, component -> ()
remove :: entity, fragment... -> ()
clear :: entity... -> ()
destroy :: entity... -> ()
batch_set :: query, fragment, component -> ()
batch_remove :: query, fragment... -> ()
batch_clear :: query... -> ()
batch_destroy :: query... -> ()
each :: entity -> {each_state? -> fragment?, component?}, each_state?
execute :: query -> {execute_state? -> chunk?, entity[]?, integer?}, execute_state?
process :: system... -> ()
debug_mode :: boolean -> ()
collect_garbage :: ()
```
### Classes
#### Chunk
```
chunk :: fragment, fragment... -> chunk, entity[], integer
chunk_mt:alive :: boolean
chunk_mt:empty :: boolean
chunk_mt:has :: fragment -> boolean
chunk_mt:has_all :: fragment... -> boolean
chunk_mt:has_any :: fragment... -> boolean
chunk_mt:entities :: entity[], integer
chunk_mt:fragments :: fragment[], integer
chunk_mt:components :: fragment... -> storage...
```
#### Builder
```
builder :: builder
builder_mt:spawn :: entity
builder_mt:clone :: entity -> entity
builder_mt:has :: fragment -> boolean
builder_mt:has_all :: fragment... -> boolean
builder_mt:has_any :: fragment... -> boolean
builder_mt:get :: fragment... -> component...
builder_mt:set :: fragment, component -> builder
builder_mt:remove :: fragment... -> builder
builder_mt:clear :: builder
builder_mt:tag :: builder
builder_mt:name :: string -> builder
builder_mt:unique :: builder
builder_mt:explicit :: builder
builder_mt:default :: component -> builder
builder_mt:duplicate :: {component -> component} -> builder
builder_mt:prefab :: builder
builder_mt:disabled :: builder
builder_mt:include :: fragment... -> builder
builder_mt:exclude :: fragment... -> builder
builder_mt:on_set :: {entity, fragment, component, component?} -> builder
builder_mt:on_assign :: {entity, fragment, component, component} -> builder
builder_mt:on_insert :: {entity, fragment, component} -> builder
builder_mt:on_remove :: {entity, fragment} -> builder
builder_mt:group :: system -> builder
builder_mt:query :: query -> builder
builder_mt:execute :: {chunk, entity[], integer} -> builder
builder_mt:prologue :: {} -> builder
builder_mt:epilogue :: {} -> builder
builder_mt:destruction_policy :: id -> builder
```
To get started with `evolved.lua`, read the [Overview](#overview) section to understand the basic concepts and how to use the library. After that, check the [Samples](develop/samples), which demonstrate complex usage of the library. Finally, refer to the [Cheat Sheet](#cheat-sheet) for a quick reference of all the functions and classes provided by the library.
## Overview
@@ -300,11 +151,14 @@ Sometimes (for debugging purposes, for example), it is necessary to extract the
---@param index integer
---@param version integer
---@return evolved.id id
---@nodiscard
function evolved.pack(index, version) end
---@param id evolved.id
---@return integer index
---@return integer version
---@return integer primary
---@return integer secondary
---@return integer options
---@nodiscard
function evolved.unpack(id) end
```
@@ -556,7 +410,7 @@ evolved.set(enemy1, stamina, 42)
#### Entity Builders
Another way to avoid structural changes when spawning entities is to use the [`evolved.builder`](#evolvedbuilder) fluid interface. The [`evolved.builder`](#evolvedbuilder) function returns a builder object that allows you to spawn entities with a specific set of fragments and components without the necessity of setting them one by one with structural changes for each change.
Another way to avoid structural changes when spawning entities is to use the [`evolved.builder`](#evolvedbuilder) fluent interface. The [`evolved.builder`](#evolvedbuilder) function returns a builder object that allows you to spawn entities with a specific set of fragments and components without the necessity of setting them one by one with structural changes for each change.
```lua
local evolved = require 'evolved'
@@ -598,6 +452,35 @@ The [`evolved.alive`](#evolvedalive) function checks whether an entity is alive.
All of these functions can be safely called on non-alive entities and non-alive fragments. Also, they do not cause any structural changes, because they do not modify anything.
### Iterating Over Fragments
Sometimes, you may need to iterate over all fragments attached to an entity. You can use the [`evolved.each`](#evolvedeach) function for this purpose.
```lua
local evolved = require 'evolved'
local health = evolved.builder()
:name('health')
:spawn()
local stamina = evolved.builder()
:name('stamina')
:spawn()
local player = evolved.builder()
:set(health, 100)
:set(stamina, 50)
:spawn()
for fragment, component in evolved.each(player) do
print(string.format('Fragment (%s) has value %d',
evolved.name(fragment), component))
end
```
> [!NOTE]
> [Structural changes](#structural-changes) are not allowed during iteration. If you want to spawn new entities or insert/remove fragments while iterating, defer these operations until the iteration is complete. See the [Deferred Operations](#deferred-operations) section for more details.
### Modifying Operations
The library provides a classic set of functions for modifying entities. These functions are used to insert, override, and remove fragments from entities.
@@ -823,6 +706,28 @@ The [`evolved.process`](#evolvedprocess) function is used to process systems. It
function evolved.process(...) end
```
If you don't specify a query for the system, the system itself will be treated as a query. This means the system can contain `evolved.INCLUDES` and `evolved.EXCLUDES` fragments, and it will be processed according to them. This is useful for creating systems with unique queries that don't need to be reused in other systems.
```lua
local evolved = require 'evolved'
local health = evolved.id()
local system = evolved.builder()
:include(health)
:execute(function(chunk, entity_list, entity_count)
local health_components = chunk:components(health)
for i = 1, entity_count do
health_components[i] = math.max(
health_components[i] - 1,
0)
end
end):spawn()
evolved.process(system)
```
To group systems together, you can use the [`evolved.GROUP`](#evolvedgroup) fragment. Systems with a specified group will be processed when you call the [`evolved.process`](#evolvedprocess) function with this group. For example, you can group all physics systems together and process them in one [`evolved.process`](#evolvedprocess) call.
```lua
@@ -895,7 +800,7 @@ The prologue and epilogue fragments do not require an explicit query. They will
> [!NOTE]
> And one more thing about systems. Execution callbacks are called in the [deferred scope](#deferred-operations), which means that all modifying operations inside the callback will be queued and applied after the system has processed all chunks. But prologue and epilogue callbacks are not called in the deferred scope, so all modifying operations inside them will be applied immediately. This is done to avoid confusion and to make it clear that prologue and epilogue callbacks are not part of the chunk processing.
### Advanced Topics
### Predefined Traits
#### Fragment Tags
@@ -1039,6 +944,35 @@ local enemy2 = evolved.builder()
assert(evolved.get(enemy1, position) ~= evolved.get(enemy2, position))
```
#### Fragment Requirements
Sometimes you want to add additional fragments to an entity when it receives a specific fragment. For example, you might want to add `position` and `velocity` fragments when an entity is given a `physical` fragment. This can be done using the [`evolved.REQUIRES`](#evolvedrequires) fragment trait. This trait expects a list of fragments that will be added to the entity when the fragment is inserted.
```lua
local evolved = require 'evolved'
local position = evolved.builder()
:default(vector2(0, 0))
:duplicate(vector2_duplicate)
:spawn()
local velocity = evolved.builder()
:default(vector2(0, 0))
:duplicate(vector2_duplicate)
:spawn()
local physical = evolved.builder()
:tag()
:require(position, velocity)
:spawn()
local enemy = evolved.builder()
:set(physical)
:spawn()
assert(evolved.has_all(enemy, position, velocity))
```
#### Destruction Policies
Typically, fragments remain alive for the entire lifetime of the program. However, in some cases, you might want to destroy fragments when they are no longer needed. For example, you can use some runtime entities as fragments for other entities. In this case, you might want to destroy such fragments even while they are still attached to other entities. Since entities cannot have destroyed fragments, a destruction policy must be applied to resolve this. By default, the library will remove the destroyed fragment from all entities that have it.
@@ -1086,10 +1020,248 @@ evolved.destroy(world)
assert(not evolved.alive(entity))
```
## Cheat Sheet
### Aliases
```
id :: implementation-specific
pair :: id
entity :: id
fragment :: id
query :: id
system :: id
component :: any
storage :: component[]
default :: component
duplicate :: {component -> component}
execute :: {chunk, entity[], integer}
prologue :: {}
epilogue :: {}
set_hook :: {entity, fragment, component, component?}
assign_hook :: {entity, fragment, component, component}
insert_hook :: {entity, fragment, component}
remove_hook :: {entity, fragment, component}
each_state :: implementation-specific
execute_state :: implementation-specific
primaries_state :: implementation-specific
secondaries_state :: implementation-specific
each_iterator :: {each_state? -> fragment?, component?}
execute_iterator :: {execute_state? -> chunk?, entity[]?, integer?}
primaries_iterator :: {primaries_state? -> fragment?, component?}
secondaries_iterator :: {secondaries_state? -> fragment?, component?}
```
### Predefs
```
ANY :: fragment
TAG :: fragment
NAME :: fragment
UNIQUE :: fragment
EXPLICIT :: fragment
INTERNAL :: fragment
DEFAULT :: fragment
DUPLICATE :: fragment
PREFAB :: fragment
DISABLED :: fragment
INCLUDES :: fragment
EXCLUDES :: fragment
REQUIRES :: fragment
ON_SET :: fragment
ON_ASSIGN :: fragment
ON_INSERT :: fragment
ON_REMOVE :: fragment
GROUP :: fragment
QUERY :: fragment
EXECUTE :: fragment
PROLOGUE :: fragment
EPILOGUE :: fragment
DESTRUCTION_POLICY :: fragment
DESTRUCTION_POLICY_DESTROY_ENTITY :: id
DESTRUCTION_POLICY_REMOVE_FRAGMENT :: id
```
### Core Functions
```
id :: integer? -> id...
name :: id... -> string...
pack :: integer, integer -> id
unpack :: id -> integer, integer, integer
defer :: boolean
commit :: boolean
spawn :: <fragment, component>? -> entity
clone :: entity -> <fragment, component>? -> entity
alive :: entity -> boolean
alive_all :: entity... -> boolean
alive_any :: entity... -> boolean
empty :: entity -> boolean
empty_all :: entity... -> boolean
empty_any :: entity... -> boolean
has :: entity, fragment -> boolean
has_all :: entity, fragment... -> boolean
has_any :: entity, fragment... -> boolean
get :: entity, fragment... -> component...
set :: entity, fragment, component -> ()
remove :: entity, fragment... -> ()
clear :: entity... -> ()
destroy :: entity... -> ()
batch_set :: query, fragment, component -> ()
batch_remove :: query, fragment... -> ()
batch_clear :: query... -> ()
batch_destroy :: query... -> ()
each :: entity -> {each_state? -> fragment?, component?}, each_state?
execute :: query -> {execute_state? -> chunk?, entity[]?, integer?}, execute_state?
process :: system... -> ()
debug_mode :: boolean -> ()
collect_garbage :: ()
```
### Relation Functions
```
pair :: id, id -> pair
unpair :: pair -> id, id
is_pair :: id -> boolean
is_wildcard :: id -> boolean
primary :: entity, fragment, integer? -> fragment?, component?
secondary :: entity, fragment, integer? -> fragment?, component?
primaries :: entity, fragment -> {primaries_state? -> fragment?, component?}, primaries_state?
secondaries :: entity, fragment -> {secondaries_state? -> fragment?, component?}, secondaries_state?
primary_count :: entity, fragment -> integer
secondary_count :: entity, fragment -> integer
```
### Classes
#### Chunk
```
chunk :: fragment, fragment... -> chunk, entity[], integer
chunk_mt:alive :: boolean
chunk_mt:empty :: boolean
chunk_mt:has :: fragment -> boolean
chunk_mt:has_all :: fragment... -> boolean
chunk_mt:has_any :: fragment... -> boolean
chunk_mt:entities :: entity[], integer
chunk_mt:fragments :: fragment[], integer
chunk_mt:components :: fragment... -> storage...
```
#### Builder
```
builder :: builder
builder_mt:spawn :: entity
builder_mt:clone :: entity -> entity
builder_mt:has :: fragment -> boolean
builder_mt:has_all :: fragment... -> boolean
builder_mt:has_any :: fragment... -> boolean
builder_mt:get :: fragment... -> component...
builder_mt:set :: fragment, component -> builder
builder_mt:remove :: fragment... -> builder
builder_mt:clear :: builder
builder_mt:tag :: builder
builder_mt:name :: string -> builder
builder_mt:unique :: builder
builder_mt:explicit :: builder
builder_mt:internal :: builder
builder_mt:default :: component -> builder
builder_mt:duplicate :: {component -> component} -> builder
builder_mt:prefab :: builder
builder_mt:disabled :: builder
builder_mt:include :: fragment... -> builder
builder_mt:exclude :: fragment... -> builder
builder_mt:require :: fragment... -> builder
builder_mt:on_set :: {entity, fragment, component, component?} -> builder
builder_mt:on_assign :: {entity, fragment, component, component} -> builder
builder_mt:on_insert :: {entity, fragment, component} -> builder
builder_mt:on_remove :: {entity, fragment} -> builder
builder_mt:group :: system -> builder
builder_mt:query :: query -> builder
builder_mt:execute :: {chunk, entity[], integer} -> builder
builder_mt:prologue :: {} -> builder
builder_mt:epilogue :: {} -> builder
builder_mt:destruction_policy :: id -> builder
```
## License
`evolved.lua` is licensed under the [MIT License][license]. For more details, see the [LICENSE.md](./LICENSE.md) file in the repository.
# Changelog
## vX.X.X
- Added the new [`evolved.name`](#evolvedname-1) function
- Added the new [`evolved.INTERNAL`](#evolvedinternal) fragment trait
## v1.1.0
- [`Systems`](#systems) can be queries themselves now
- Added the new [`evolved.REQUIRES`](#evolvedrequires) fragment trait
## v1.0.0
- Initial release
# API Reference
## Predefs
### `evolved.ANY`
### `evolved.TAG`
### `evolved.NAME`
@@ -1098,6 +1270,8 @@ assert(not evolved.alive(entity))
### `evolved.EXPLICIT`
### `evolved.INTERNAL`
### `evolved.DEFAULT`
### `evolved.DUPLICATE`
@@ -1110,6 +1284,8 @@ assert(not evolved.alive(entity))
### `evolved.EXCLUDES`
### `evolved.REQUIRES`
### `evolved.ON_SET`
### `evolved.ON_ASSIGN`
@@ -1134,7 +1310,7 @@ assert(not evolved.alive(entity))
### `evolved.DESTRUCTION_POLICY_REMOVE_FRAGMENT`
## Functions
## Core Functions
### `evolved.id`
@@ -1145,6 +1321,15 @@ assert(not evolved.alive(entity))
function evolved.id(count) end
```
### `evolved.name`
```lua
---@param ... evolved.id ids
---@return string... names
---@nodiscard
function evolved.name(...) end
```
### `evolved.pack`
```lua
@@ -1159,8 +1344,9 @@ function evolved.pack(index, version) end
```lua
---@param id evolved.id
---@return integer index
---@return integer version
---@return integer primary
---@return integer secondary
---@return integer options
---@nodiscard
function evolved.unpack(id) end
```
@@ -1392,6 +1578,112 @@ function evolved.debug_mode(yesno) end
function evolved.collect_garbage() end
```
## Relation Functions
### `evolved.pair`
```lua
---@param primary evolved.id
---@param secondary evolved.id
---@return evolved.pair pair
---@nodiscard
function evolved.pair(primary, secondary) end
```
### `evolved.unpair`
```lua
---@param pair evolved.pair
---@return evolved.id primary
---@return evolved.id secondary
---@nodiscard
function evolved.unpair(pair) end
```
### `evolved.is_pair`
```lua
---@param id evolved.id
---@return boolean
---@nodiscard
function evolved.is_pair(id) end
```
### `evolved.is_wildcard`
```lua
---@param id evolved.id
---@return boolean
---@nodiscard
function evolved.is_wildcard(id) end
```
### `evolved.primary`
```lua
---@param entity evolved.entity
---@param secondary evolved.fragment
---@param index? integer
---@return evolved.fragment? primary
---@return evolved.component? component
---@nodiscard
function evolved.primary(entity, secondary, index) end
```
### `evolved.secondary`
```lua
---@param entity evolved.entity
---@param primary evolved.fragment
---@param index? integer
---@return evolved.fragment? secondary
---@return evolved.component? component
---@nodiscard
function evolved.secondary(entity, primary, index) end
```
### `evolved.primaries`
```lua
---@param entity evolved.entity
---@param secondary evolved.fragment
---@return evolved.primaries_iterator iterator
---@return evolved.primaries_state? iterator_state
---@nodiscard
function evolved.primaries(entity, secondary) end
```
### `evolved.secondaries`
```lua
---@param entity evolved.entity
---@param primary evolved.fragment
---@return evolved.secondaries_iterator iterator
---@return evolved.secondaries_state? iterator_state
---@nodiscard
function evolved.secondaries(entity, primary) end
```
### `evolved.primary_count`
```lua
---@param entity evolved.entity
---@param secondary evolved.fragment
---@return integer
---@nodiscard
function evolved.primary_count(entity, secondary) end
```
### `evolved.secondary_count`
```lua
---@param entity evolved.entity
---@param primary evolved.fragment
---@return integer
---@nodiscard
function evolved.secondary_count(entity, primary) end
```
## Classes
### Chunk
@@ -1592,6 +1884,13 @@ function evolved.builder_mt:unique() end
function evolved.builder_mt:explicit() end
```
#### `evolved.builder_mt:internal`
```lua
---@return evolved.builder builder
function evolved.builder_mt:internal() end
```
#### `evolved.builder_mt:default`
```lua
@@ -1638,6 +1937,14 @@ function evolved.builder_mt:include(...) end
function evolved.builder_mt:exclude(...) end
```
### `evolved.builder_mt:require`
```lua
---@param ... evolved.fragment fragments
---@return evolved.builder builder
function evolved.builder_mt:require(...) end
```
#### `evolved.builder_mt:on_set`
```lua
@@ -1717,5 +2024,3 @@ function evolved.builder_mt:epilogue(epilogue) end
---@return evolved.builder builder
function evolved.builder_mt:destruction_policy(destruction_policy) end
```
## [License (MIT)](./LICENSE.md)

View File

@@ -2,11 +2,18 @@
## Backlog
## After first release
- cached queries
- Improve the performance of required fragments by caching first-level required chunks.
- Improve the performance of builders that are used multiple times by caching hint chunks.
- Queries can cache major chunks to avoid finding them every time.
- Add multi-spawn to the builder to spawn multiple entities at once.
- Add a function to shrink storages to free unused memory.
- observers and events
- add INDEX fragment trait
- add REQUIRES fragment trait
- use compact prefix-tree for chunks
- optional ffi component storages
## Thoughts
- We can return deferred status from modifying operations and spawn/clone methods.
- Should we make one builder:build method instead of :spawn and :clone?
- Should we cache the result of without_unique_fragments to clone faster?

View File

@@ -1,6 +1,14 @@
require 'develop.example'
require 'develop.unbench'
require 'develop.samples.relations'
require 'develop.samples.systems'
require 'develop.testing.name_tests'
require 'develop.testing.pairs_tests'
require 'develop.testing.requires_fragment_tests'
require 'develop.testing.system_as_query_tests'
require 'develop.untests'
require 'develop.unbench'
require 'develop.usbench'
local basics = require 'develop.basics'
@@ -12,4 +20,10 @@ basics.describe_fuzz 'develop.fuzzing.batch_destroy_fuzz'
print '----------------------------------------'
basics.describe_fuzz 'develop.fuzzing.explicit_fuzz'
print '----------------------------------------'
basics.describe_fuzz 'develop.fuzzing.pack_unpack_fuzz'
print '----------------------------------------'
basics.describe_fuzz 'develop.fuzzing.requires_fuzz'
print '----------------------------------------'
basics.describe_fuzz 'develop.fuzzing.unique_fuzz'
print '----------------------------------------'
basics.describe_fuzz 'develop.fuzzing.wildcard_fuzz'

View File

@@ -1,5 +1,13 @@
local basics = {}
local MIN_FUZZ_SECS = 0.5
local MIN_BENCH_SECS = 0.1
local MIN_WARMUP_SECS = 0.1
local MIN_FUZZ_ITERS = 100
local MIN_BENCH_ITERS = 100
local MIN_WARMUP_ITERS = 100
local __table_pack = (function()
---@diagnostic disable-next-line: deprecated
return table.pack or function(...)
@@ -23,8 +31,13 @@ end
---@param modname string
function basics.describe_fuzz(modname)
basics.unload('evolved')
print(string.format('| %s ... |', modname))
collectgarbage('collect')
collectgarbage('stop')
do
local iters = 0
@@ -36,7 +49,7 @@ function basics.describe_fuzz(modname)
iters = iters + 1
basics.unload(modname)
require(modname)
until os.clock() - start_s > 0.5
until iters >= MIN_FUZZ_ITERS and os.clock() - start_s >= MIN_FUZZ_SECS
end)
local finish_s = os.clock()
@@ -52,6 +65,9 @@ function basics.describe_fuzz(modname)
print('|-- FUZZ FAIL: ' .. result)
end
end
collectgarbage('restart')
collectgarbage('collect')
end
---@param name string
@@ -59,17 +75,22 @@ end
---@param init? fun(): ...
---@param fini? fun(...): ...
function basics.describe_bench(name, loop, init, fini)
basics.unload('evolved')
print(string.format('| %s ... |', name))
local state = init and __table_pack(init()) or {}
do
local iters = 0
local warmup_s = os.clock()
local success, result = pcall(function()
repeat
iters = iters + 1
loop(__table_unpack(state))
until os.clock() - warmup_s > 0.1
until iters >= MIN_WARMUP_ITERS and os.clock() - warmup_s > MIN_WARMUP_SECS
end)
if not success then
@@ -91,7 +112,7 @@ function basics.describe_bench(name, loop, init, fini)
repeat
iters = iters + 1
loop(__table_unpack(state))
until os.clock() - start_s > 0.1
until iters >= MIN_BENCH_ITERS and os.clock() - start_s > MIN_BENCH_SECS
end)
local finish_s = os.clock()

View File

@@ -117,5 +117,12 @@ end
---
---
if math.random(1, 2) == 1 then
evo.collect_garbage()
end
evo.destroy(__table_unpack(all_entity_list))
evo.collect_garbage()
if math.random(1, 2) == 1 then
evo.collect_garbage()
end

View File

@@ -123,5 +123,12 @@ end
---
---
if math.random(1, 2) == 1 then
evo.collect_garbage()
end
evo.destroy(__table_unpack(all_entity_list))
evo.collect_garbage()
if math.random(1, 2) == 1 then
evo.collect_garbage()
end

View File

@@ -77,5 +77,12 @@ end
---
---
if math.random(1, 2) == 1 then
evo.collect_garbage()
end
evo.destroy(__table_unpack(all_entity_list))
evo.collect_garbage()
if math.random(1, 2) == 1 then
evo.collect_garbage()
end

View File

@@ -0,0 +1,21 @@
local evo = require 'evolved'
evo.debug_mode(true)
---
---
---
---
---
for _ = 1, 1000 do
local initial_primary = math.random(1, 2 ^ 20 - 1)
local initial_secondary = math.random(1, 2 ^ 20 - 1)
local packed_id = evo.pack(initial_primary, initial_secondary)
local unpacked_primary, unpacked_secondary, unpacked_options = evo.unpack(packed_id)
assert(initial_primary == unpacked_primary)
assert(initial_secondary == unpacked_secondary)
assert(0 == unpacked_options)
end

View File

@@ -0,0 +1,131 @@
local evo = require 'evolved'
evo.debug_mode(true)
---
---
---
---
---
local __table_unpack = (function()
---@diagnostic disable-next-line: deprecated
return table.unpack or unpack
end)()
---
---
---
---
---
local all_fragment_list = {} ---@type evolved.fragment[]
for i = 1, math.random(1, 10) do
local fragment = evo.builder()
:default(42)
:spawn()
all_fragment_list[i] = fragment
end
for _, fragment in ipairs(all_fragment_list) do
if math.random(1, 2) == 1 then
for _ = 0, math.random(0, #all_fragment_list) do
local require_list = evo.get(fragment, evo.REQUIRES) or {}
require_list[#require_list + 1] = all_fragment_list[math.random(1, #all_fragment_list)]
evo.set(fragment, evo.REQUIRES, require_list)
end
end
end
local all_entity_list = {} ---@type evolved.entity[]
for i = 1, math.random(1, 10) do
local entity = evo.id()
all_entity_list[i] = entity
for _ = 0, math.random(0, #all_fragment_list) do
local fragment = all_fragment_list[math.random(1, #all_fragment_list)]
if math.random(1, 2) == 1 then
evo.set(entity, fragment, 42)
else
local query = evo.builder()
:include(all_fragment_list[math.random(1, #all_fragment_list)])
:spawn()
evo.batch_set(query, fragment, 42)
evo.destroy(query)
end
end
end
for _ = 1, math.random(1, #all_entity_list) do
local components = {}
for _ = 1, math.random(1, #all_fragment_list) do
local fragment = all_fragment_list[math.random(1, #all_fragment_list)]
components[fragment] = 42
end
all_entity_list[#all_entity_list + 1] = evo.spawn(components)
end
for _ = 1, math.random(1, #all_entity_list) do
local prefab = all_entity_list[math.random(1, #all_entity_list)]
all_entity_list[#all_entity_list + 1] = evo.clone(prefab)
end
---
---
---
---
---
local function collect_required_fragments_for(fragment, req_fragment_set, req_fragment_list)
local fragment_requires = evo.get(fragment, evo.REQUIRES) or {}
for _, required_fragment in ipairs(fragment_requires) do
if not req_fragment_set[required_fragment] then
req_fragment_set[required_fragment] = true
req_fragment_list[#req_fragment_list + 1] = required_fragment
collect_required_fragments_for(required_fragment, req_fragment_set, req_fragment_list)
end
end
end
for _, entity in ipairs(all_entity_list) do
for fragment in evo.each(entity) do
local req_fragment_list = {}
collect_required_fragments_for(fragment, {}, req_fragment_list)
for _, required_fragment in ipairs(req_fragment_list) do
assert(evo.has(entity, required_fragment))
local required_component = evo.get(entity, required_fragment)
assert(required_component == 42)
end
end
end
---
---
---
---
---
if math.random(1, 2) == 1 then
evo.collect_garbage()
end
if math.random(1, 2) == 1 then
evo.destroy(__table_unpack(all_entity_list))
if math.random(1, 2) == 1 then
evo.collect_garbage()
end
evo.destroy(__table_unpack(all_fragment_list))
else
evo.destroy(__table_unpack(all_fragment_list))
if math.random(1, 2) == 1 then
evo.collect_garbage()
end
evo.destroy(__table_unpack(all_entity_list))
end
if math.random(1, 2) == 1 then
evo.collect_garbage()
end

View File

@@ -63,5 +63,12 @@ end
---
---
if math.random(1, 2) == 1 then
evo.collect_garbage()
end
evo.destroy(__table_unpack(all_entity_list))
evo.collect_garbage()
if math.random(1, 2) == 1 then
evo.collect_garbage()
end

View File

@@ -0,0 +1,275 @@
local evo = require 'evolved'
evo.debug_mode(true)
---
---
---
---
---
local __table_unpack = (function()
---@diagnostic disable-next-line: deprecated
return table.unpack or unpack
end)()
---
---
---
---
---
local all_entity_list = {} ---@type evolved.entity[]
local all_fragment_list = {} ---@type evolved.fragment[]
for i = 1, math.random(1, 5) do
local fragment_builder = evo.builder()
if math.random(1, 3) == 1 then
fragment_builder:explicit()
end
if math.random(1, 3) == 1 then
if math.random(1, 2) == 1 then
fragment_builder:destruction_policy(evo.DESTRUCTION_POLICY_DESTROY_ENTITY)
else
fragment_builder:destruction_policy(evo.DESTRUCTION_POLICY_REMOVE_FRAGMENT)
end
end
all_fragment_list[i] = fragment_builder:spawn()
end
for i = 1, math.random(50, 100) do
local entity_builder = evo.builder()
for _ = 0, math.random(0, #all_fragment_list) do
if math.random(1, 2) == 1 then
local fragment = all_fragment_list[math.random(1, #all_fragment_list)]
entity_builder:set(fragment)
else
local primary = all_fragment_list[math.random(1, #all_fragment_list)]
local secondary = all_fragment_list[math.random(1, #all_fragment_list)]
entity_builder:set(evo.pair(primary, secondary))
end
end
all_entity_list[i] = entity_builder:spawn()
end
---
---
---
---
---
for _ = 1, math.random(1, 100) do
local query_builder = evo.builder()
local query_include_set = {} ---@type table<evolved.fragment, integer>
local query_include_list = {} ---@type evolved.entity[]
local query_include_count = 0 ---@type integer
local query_exclude_set = {} ---@type table<evolved.fragment, integer>
local query_exclude_list = {} ---@type evolved.entity[]
local query_exclude_count = 0 ---@type integer
for _ = 1, math.random(0, 2) do
if math.random(1, 2) == 1 then
local fragment = all_fragment_list[math.random(1, #all_fragment_list)]
query_builder:include(fragment)
if not query_include_set[fragment] then
query_include_count = query_include_count + 1
query_include_set[fragment] = query_include_count
query_include_list[query_include_count] = fragment
end
else
local primary = all_fragment_list[math.random(1, #all_fragment_list)]
local secondary = all_fragment_list[math.random(1, #all_fragment_list)]
if math.random(1, 3) == 1 then
primary = evo.ANY
end
if math.random(1, 3) == 1 then
secondary = evo.ANY
end
local pair = evo.pair(primary, secondary)
query_builder:include(pair)
if not query_include_set[pair] then
query_include_count = query_include_count + 1
query_include_set[pair] = query_include_count
query_include_list[query_include_count] = pair
end
end
end
for _ = 1, math.random(0, 2) do
if math.random(1, 2) == 1 then
local fragment = all_fragment_list[math.random(1, #all_fragment_list)]
query_builder:exclude(fragment)
if not query_exclude_set[fragment] then
query_exclude_count = query_exclude_count + 1
query_exclude_set[fragment] = query_exclude_count
query_exclude_list[query_exclude_count] = fragment
end
else
local primary = all_fragment_list[math.random(1, #all_fragment_list)]
local secondary = all_fragment_list[math.random(1, #all_fragment_list)]
if math.random(1, 3) == 1 then
primary = evo.ANY
end
if math.random(1, 3) == 1 then
secondary = evo.ANY
end
local pair = evo.pair(primary, secondary)
query_builder:exclude(pair)
if not query_exclude_set[pair] then
query_exclude_count = query_exclude_count + 1
query_exclude_set[pair] = query_exclude_count
query_exclude_list[query_exclude_count] = pair
end
end
end
local query_entity_set = {} ---@type table<evolved.entity, integer>
local query_entity_count = 0 ---@type integer
do
local query = query_builder:spawn()
for chunk, entity_list, entity_count in evo.execute(query) do
if not chunk:has(evo.INTERNAL) then
for i = 1, entity_count do
local entity = entity_list[i]
assert(not query_entity_set[entity])
query_entity_count = query_entity_count + 1
query_entity_set[entity] = query_entity_count
end
end
end
if query_entity_set[query] then
query_entity_set[query] = nil
query_entity_count = query_entity_count - 1
end
evo.destroy(query)
end
do
local expected_entity_count = 0
for _, entity in ipairs(all_entity_list) do
local is_entity_expected =
not evo.empty(entity) and
evo.has_all(entity, __table_unpack(query_include_list)) and
not evo.has_any(entity, __table_unpack(query_exclude_list))
for fragment in evo.each(entity) do
local is_fragment_explicit = false
if not is_fragment_explicit and evo.is_pair(fragment) then
is_fragment_explicit = evo.has(evo.unpair(fragment), evo.EXPLICIT)
end
if not is_fragment_explicit and not evo.is_pair(fragment) then
is_fragment_explicit = evo.has(fragment, evo.EXPLICIT)
end
if is_fragment_explicit then
local is_fragment_included = false
if not is_fragment_included then
is_fragment_included = query_include_set[fragment] ~= nil
end
if not is_fragment_included and evo.is_pair(fragment) then
local fragment_primary = evo.unpair(fragment)
is_fragment_included = query_include_set[evo.pair(fragment_primary, evo.ANY)] ~= nil
end
if not is_fragment_included and not evo.is_pair(fragment) then
is_fragment_included = query_include_set[evo.pair(fragment, evo.ANY)] ~= nil
end
if not is_fragment_included then
is_entity_expected = false
break
end
end
end
if is_entity_expected then
assert(query_entity_set[entity])
expected_entity_count = expected_entity_count + 1
else
assert(not query_entity_set[entity])
end
end
for _, entity in ipairs(all_fragment_list) do
local is_entity_expected =
not evo.empty(entity) and
evo.has_all(entity, __table_unpack(query_include_list)) and
not evo.has_any(entity, __table_unpack(query_exclude_list))
for fragment in evo.each(entity) do
if evo.has(fragment, evo.EXPLICIT) then
is_entity_expected = is_entity_expected and
(query_include_set[fragment] ~= nil) or
(evo.is_pair(fragment) and query_include_set[evo.pair(fragment, evo.ANY)] ~= nil)
end
end
if is_entity_expected then
assert(query_entity_set[entity])
expected_entity_count = expected_entity_count + 1
else
assert(not query_entity_set[entity])
end
end
assert(query_entity_count == expected_entity_count)
end
end
---
---
---
---
---
if math.random(1, 2) == 1 then
evo.collect_garbage()
end
if math.random(1, 2) == 1 then
evo.destroy(__table_unpack(all_entity_list))
if math.random(1, 2) == 1 then
evo.collect_garbage()
end
evo.destroy(__table_unpack(all_fragment_list))
else
evo.destroy(__table_unpack(all_fragment_list))
if math.random(1, 2) == 1 then
evo.collect_garbage()
end
evo.destroy(__table_unpack(all_entity_list))
end
if math.random(1, 2) == 1 then
evo.collect_garbage()
end

View File

@@ -0,0 +1,79 @@
---@diagnostic disable: unused-local
local evo = require 'evolved'
evo.debug_mode(true)
local fragments = {
planet = evo.builder()
:name('planet')
:tag()
:spawn(),
spaceship = evo.builder()
:name('spaceship')
:tag()
:spawn(),
}
local relations = {
docked_to = evo.builder()
:name('docked_to')
:tag()
:spawn(),
}
local planets = {
mars = evo.builder()
:name('Mars')
:set(fragments.planet)
:spawn(),
venus = evo.builder()
:name('Venus')
:set(fragments.planet)
:spawn(),
}
local spaceships = {
falcon = evo.builder()
:name('Millennium Falcon')
:set(fragments.spaceship)
:set(evo.pair(relations.docked_to, planets.mars))
:spawn(),
enterprise = evo.builder()
:name('USS Enterprise')
:set(fragments.spaceship)
:set(evo.pair(relations.docked_to, planets.venus))
:spawn(),
}
local queries = {
all_docked_spaceships = evo.builder()
:include(fragments.spaceship)
:include(evo.pair(relations.docked_to, evo.ANY))
:spawn(),
docked_spaceships_to_mars = evo.builder()
:include(fragments.spaceship)
:include(evo.pair(relations.docked_to, planets.mars))
:spawn(),
}
print '-= | All Docked Spaceships | =-'
for chunk, entity_list, entity_count in evo.execute(queries.all_docked_spaceships) do
for i = 1, entity_count do
local entity = entity_list[i]
local planet = evo.secondary(entity, relations.docked_to)
print(string.format('%s is docked to %s', evo.name(entity), evo.name(planet)))
end
end
print '-= | Docked Spaceships to Mars | =-'
for chunk, entity_list, entity_count in evo.execute(queries.docked_spaceships_to_mars) do
for i = 1, entity_count do
local entity = entity_list[i]
local planet = evo.secondary(entity, relations.docked_to)
print(string.format('%s is docked to %s', evo.name(entity), evo.name(planet)))
end
end

View File

@@ -0,0 +1,43 @@
local evo = require 'evolved'
do
local id = evo.id()
local index, version, options = evo.unpack(id)
assert(evo.name(id) == string.format('$%d#%d:%d:%d', id, index, version, options))
evo.set(id, evo.NAME, 'hello')
assert(evo.name(id) == 'hello')
evo.set(id, evo.NAME, 'world')
assert(evo.name(id) == 'world')
evo.destroy(id)
assert(evo.name(id) == string.format('$%d#%d:%d:%d', id, index, version, options))
end
do
local id1, id2, id3, id4, id5 = evo.id(5)
evo.set(id1, evo.NAME, 'id1')
evo.set(id2, evo.NAME, 'id2')
evo.set(id3, evo.NAME, 'id3')
evo.set(id4, evo.NAME, 'id4')
evo.set(id5, evo.NAME, 'id5')
do
local id1_n, id3_n, id5_n = evo.name(id1, id3, id5)
assert(id1_n == 'id1')
assert(id3_n == 'id3')
assert(id5_n == 'id5')
end
do
local id1_n, id2_n, id3_n, id4_n, id5_n = evo.name(id1, id2, id3, id4, id5)
assert(id1_n == 'id1')
assert(id2_n == 'id2')
assert(id3_n == 'id3')
assert(id4_n == 'id4')
assert(id5_n == 'id5')
end
end

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,245 @@
local evo = require 'evolved'
do
local f1, f2 = evo.id(2)
evo.set(f1, evo.REQUIRES)
evo.set(f2, evo.REQUIRES, evo.get(f1, evo.REQUIRES))
local f1_rs = evo.get(f1, evo.REQUIRES)
local f2_rs = evo.get(f2, evo.REQUIRES)
assert(f1_rs and f2_rs and #f1_rs == 0 and #f2_rs == 0 and f1_rs ~= f2_rs)
end
do
local f1, f2 = evo.id(2)
local f3 = evo.builder():require(f1):require(f2):spawn()
local f3_rs = evo.get(f3, evo.REQUIRES)
assert(f3_rs and #f3_rs == 2 and f3_rs[1] == f1 and f3_rs[2] == f2)
end
do
local f1, f2 = evo.id(2)
local f3 = evo.builder():require(f1, f2):spawn()
local f3_rs = evo.get(f3, evo.REQUIRES)
assert(f3_rs and #f3_rs == 2 and f3_rs[1] == f1 and f3_rs[2] == f2)
end
do
local f1, f2, f3 = evo.id(3)
evo.set(f1, evo.REQUIRES, { f2 })
evo.set(f3, evo.REQUIRES, { f1, f2 })
do
local e = evo.id()
evo.set(e, f1)
assert(evo.has(e, f2))
assert(evo.get(e, f2) == true)
end
do
local e = evo.builder():set(f1):spawn()
assert(evo.has(e, f2))
assert(evo.get(e, f2) == true)
end
do
local e = evo.spawn { [f1] = true }
assert(evo.has(e, f2))
assert(evo.get(e, f2) == true)
evo.remove(e, f2)
assert(not evo.has(e, f2))
local e2 = evo.clone(e)
assert(evo.has(e2, f2))
assert(evo.get(e2, f2) == true)
local e3 = evo.clone(e, { [f3] = true })
assert(evo.has(e3, f2))
assert(evo.get(e3, f2) == true)
end
do
local f0 = evo.id()
local q0 = evo.builder():include(f0):spawn()
local e1 = evo.builder():set(f0):spawn()
local e2 = evo.builder():set(f0):spawn()
local e3 = evo.builder():set(f0):set(f2, false):spawn()
evo.batch_set(q0, f1)
assert(evo.has(e1, f2) and evo.get(e1, f2) == true)
assert(evo.has(e2, f2) and evo.get(e2, f2) == true)
assert(evo.has(e3, f2) and evo.get(e3, f2) == false)
end
end
do
local f1, f2, f3 = evo.id(3)
evo.set(f1, evo.REQUIRES, { f2 })
evo.set(f2, evo.DEFAULT, 42)
evo.set(f3, evo.REQUIRES, { f1, f2 })
do
local e = evo.id()
evo.set(e, f1)
assert(evo.has(e, f2))
assert(evo.get(e, f2) == 42)
end
do
local e = evo.builder():set(f1):spawn()
assert(evo.has(e, f2))
assert(evo.get(e, f2) == 42)
end
do
local e = evo.spawn { [f1] = true, }
assert(evo.has(e, f2))
assert(evo.get(e, f2) == 42)
evo.remove(e, f2)
assert(not evo.has(e, f2))
local e2 = evo.clone(e)
assert(evo.has(e2, f2))
assert(evo.get(e2, f2) == 42)
local e3 = evo.clone(e, { [f3] = true })
assert(evo.has(e3, f2))
assert(evo.get(e3, f2) == 42)
end
do
local f0 = evo.id()
local q0 = evo.builder():include(f0):spawn()
local e1 = evo.builder():set(f0):spawn()
local e2 = evo.builder():set(f0):spawn()
local e3 = evo.builder():set(f0):set(f2, 21):spawn()
evo.batch_set(q0, f1)
assert(evo.has(e1, f2) and evo.get(e1, f2) == 42)
assert(evo.has(e2, f2) and evo.get(e2, f2) == 42)
assert(evo.has(e3, f2) and evo.get(e3, f2) == 21)
end
end
do
local f1, f2, f3 = evo.id(3)
evo.set(f1, evo.REQUIRES, { f2 })
evo.set(f2, evo.REQUIRES, { f3 })
evo.set(f3, evo.DEFAULT, 42)
evo.set(f3, evo.REQUIRES, { f1, f2 })
do
local e = evo.id()
evo.set(e, f1)
assert(evo.has(e, f2))
assert(evo.get(e, f2) == true)
assert(evo.has(e, f3))
assert(evo.get(e, f3) == 42)
end
do
local e = evo.builder():set(f1):spawn()
assert(evo.has(e, f2))
assert(evo.get(e, f2) == true)
assert(evo.has(e, f3))
assert(evo.get(e, f3) == 42)
end
do
local e = evo.spawn { [f1] = true }
assert(evo.has(e, f2))
assert(evo.get(e, f2) == true)
assert(evo.has(e, f3))
assert(evo.get(e, f3) == 42)
evo.remove(e, f2, f3)
assert(not evo.has(e, f2))
assert(not evo.has(e, f3))
local e2 = evo.clone(e, { [f1] = 21 })
assert(evo.has(e2, f1) and evo.get(e2, f1) == 21)
assert(evo.has(e2, f2) and evo.get(e2, f2) == true)
assert(evo.has(e2, f3) and evo.get(e2, f3) == 42)
local e3 = evo.clone(e, { [f3] = 21 })
assert(evo.has(e3, f1) and evo.get(e3, f1) == true)
assert(evo.has(e3, f2) and evo.get(e3, f2) == true)
assert(evo.has(e3, f3) and evo.get(e3, f3) == 21)
end
end
do
local f1, f2, f3 = evo.id(3)
evo.set(f1, evo.REQUIRES, { f2 })
evo.set(f2, evo.REQUIRES, { f3 })
evo.set(f3, evo.REQUIRES, { f1, f2, f3 })
do
local e = evo.id()
evo.set(e, f1, 42)
assert(evo.has(e, f1) and evo.get(e, f1) == 42)
assert(evo.has(e, f2) and evo.get(e, f2) == true)
assert(evo.has(e, f3) and evo.get(e, f3) == true)
end
do
local e = evo.builder():set(f1, 42):spawn()
assert(evo.has(e, f1) and evo.get(e, f1) == 42)
assert(evo.has(e, f2) and evo.get(e, f2) == true)
assert(evo.has(e, f3) and evo.get(e, f3) == true)
end
do
local e = evo.spawn { [f1] = 42 }
assert(evo.has(e, f1) and evo.get(e, f1) == 42)
assert(evo.has(e, f2) and evo.get(e, f2) == true)
assert(evo.has(e, f3) and evo.get(e, f3) == true)
evo.remove(e, f2, f3)
assert(not evo.has(e, f2))
assert(not evo.has(e, f3))
local e2 = evo.clone(e, { [f1] = 21 })
assert(evo.has(e2, f1) and evo.get(e2, f1) == 21)
assert(evo.has(e2, f2) and evo.get(e2, f2) == true)
assert(evo.has(e2, f3) and evo.get(e2, f3) == true)
local e3 = evo.clone(e, { [f3] = 21 })
assert(evo.has(e3, f1) and evo.get(e3, f1) == 42)
assert(evo.has(e3, f2) and evo.get(e3, f2) == true)
assert(evo.has(e3, f3) and evo.get(e3, f3) == 21)
end
end
do
local f1, f2, f3, f4 = evo.id(4)
do
evo.set(f1, evo.REQUIRES, { f2 })
do
local e = evo.id()
evo.set(e, f1)
assert(evo.has(e, f2) and evo.get(e, f2) == true)
end
evo.set(f1, evo.REQUIRES, { f2, f3 })
do
local e = evo.id()
evo.set(e, f1)
assert(evo.has(e, f2) and evo.get(e, f2) == true)
assert(evo.has(e, f3) and evo.get(e, f3) == true)
end
evo.set(f3, evo.REQUIRES, { f4 })
do
local e = evo.id()
evo.set(e, f1)
assert(evo.has(e, f2) and evo.get(e, f2) == true)
assert(evo.has(e, f3) and evo.get(e, f3) == true)
assert(evo.has(e, f4) and evo.get(e, f4) == true)
end
end
end

View File

@@ -0,0 +1,128 @@
local evo = require 'evolved'
do
local f1, f2, f3 = evo.id(3)
local q1e3 = evo.builder():include(f1):exclude(f3):spawn()
local q2e3 = evo.builder():include(f2):exclude(f3):spawn()
local e1 = evo.builder():set(f1, 1):spawn()
local e12 = evo.builder():set(f1, 11):set(f2, 12):spawn()
local e2 = evo.builder():set(f2, 2):spawn()
local e23 = evo.builder():set(f2, 23):set(f3, 3):spawn()
local c1 = evo.chunk(f1)
local c12 = evo.chunk(f1, f2)
local c2 = evo.chunk(f2)
do
local _, entity_list, entity_count = evo.chunk(f2, f3)
assert(entity_count == 1 and entity_list[1] == e23)
end
do
local entity_sum = 0
local s = evo.builder()
:query(q1e3)
:execute(function(chunk, entity_list, entity_count)
for i = 1, entity_count do
entity_sum = entity_sum + entity_list[i]
end
if chunk == c1 then
assert(entity_count == 1)
assert(entity_list[1] == e1)
elseif chunk == c12 then
assert(entity_count == 1)
assert(entity_list[1] == e12)
else
assert(false, "Unexpected chunk: " .. tostring(chunk))
end
end):spawn()
evo.process(s)
assert(entity_sum == e1 + e12)
end
do
local entity_sum = 0
local s = evo.builder()
:query(q2e3)
:execute(function(chunk, entity_list, entity_count)
for i = 1, entity_count do
entity_sum = entity_sum + entity_list[i]
end
if chunk == c12 then
assert(entity_count == 1)
assert(entity_list[1] == e12)
elseif chunk == c2 then
assert(entity_count == 1)
assert(entity_list[1] == e2)
else
assert(false, "Unexpected chunk: " .. tostring(chunk))
end
end):spawn()
evo.process(s)
assert(entity_sum == e12 + e2)
end
do
local entity_sum = 0
local s = evo.builder()
:include(f1)
:exclude(f3)
:execute(function(chunk, entity_list, entity_count)
for i = 1, entity_count do
entity_sum = entity_sum + entity_list[i]
end
if chunk == c1 then
assert(entity_count == 1)
assert(entity_list[1] == e1)
elseif chunk == c12 then
assert(entity_count == 1)
assert(entity_list[1] == e12)
else
assert(false, "Unexpected chunk: " .. tostring(chunk))
end
end):spawn()
evo.process(s)
assert(entity_sum == e1 + e12)
end
do
local entity_sum = 0
local s = evo.builder()
:include(f2)
:exclude(f3)
:execute(function(chunk, entity_list, entity_count)
for i = 1, entity_count do
entity_sum = entity_sum + entity_list[i]
end
if chunk == c12 then
assert(entity_count == 1)
assert(entity_list[1] == e12)
elseif chunk == c2 then
assert(entity_count == 1)
assert(entity_list[1] == e2)
else
assert(false, "Unexpected chunk: " .. tostring(chunk))
end
end):spawn()
evo.process(s)
assert(entity_sum == e12 + e2)
end
end

View File

@@ -7,6 +7,11 @@ local N = 1000
local B = evo.builder()
local F1, F2, F3, F4, F5 = evo.id(5)
local Q1 = evo.builder():include(F1):spawn()
local R1 = evo.builder():require(F1):spawn()
local R2 = evo.builder():require(F1, F2):spawn()
local R3 = evo.builder():require(F1, F2, F3):spawn()
local R4 = evo.builder():require(F1, F2, F3, F4):spawn()
local R5 = evo.builder():require(F1, F2, F3, F4, F5):spawn()
print '----------------------------------------'
@@ -610,7 +615,6 @@ basics.describe_bench(string.format('create and destroy %d entities with 5 compo
print '----------------------------------------'
basics.describe_bench(string.format('create and destroy %d entities with 1 components / clone', N),
---@param entities evolved.id[]
function(entities)
@@ -690,3 +694,167 @@ basics.describe_bench(string.format('create and destroy %d entities with 5 compo
end, function()
return {}
end)
print '----------------------------------------'
basics.describe_bench(string.format('create and destroy %d entities with 1 requires / spawn', N),
---@param entities evolved.id[]
function(entities)
local spawn = evo.spawn
local components = { [R1] = true }
for i = 1, N do
entities[i] = spawn(components)
end
evo.batch_destroy(Q1)
end, function()
return {}
end)
basics.describe_bench(string.format('create and destroy %d entities with 2 requires / spawn', N),
---@param entities evolved.id[]
function(entities)
local spawn = evo.spawn
local components = { [R2] = true }
for i = 1, N do
entities[i] = spawn(components)
end
evo.batch_destroy(Q1)
end, function()
return {}
end)
basics.describe_bench(string.format('create and destroy %d entities with 3 requires / spawn', N),
---@param entities evolved.id[]
function(entities)
local spawn = evo.spawn
local components = { [R3] = true }
for i = 1, N do
entities[i] = spawn(components)
end
evo.batch_destroy(Q1)
end, function()
return {}
end)
basics.describe_bench(string.format('create and destroy %d entities with 4 requires / spawn', N),
---@param entities evolved.id[]
function(entities)
local spawn = evo.spawn
local components = { [R4] = true }
for i = 1, N do
entities[i] = spawn(components)
end
evo.batch_destroy(Q1)
end, function()
return {}
end)
basics.describe_bench(string.format('create and destroy %d entities with 5 requires / spawn', N),
---@param entities evolved.id[]
function(entities)
local spawn = evo.spawn
local components = { [R5] = true }
for i = 1, N do
entities[i] = spawn(components)
end
evo.batch_destroy(Q1)
end, function()
return {}
end)
print '----------------------------------------'
basics.describe_bench(string.format('create and destroy %d entities with 1 requires / clone', N),
---@param entities evolved.id[]
function(entities)
local clone = evo.clone
local prefab = evo.spawn({ [R1] = true })
for i = 1, N do
entities[i] = clone(prefab)
end
evo.batch_destroy(Q1)
end, function()
return {}
end)
basics.describe_bench(string.format('create and destroy %d entities with 2 requires / clone', N),
---@param entities evolved.id[]
function(entities)
local clone = evo.clone
local prefab = evo.spawn({ [R2] = true })
for i = 1, N do
entities[i] = clone(prefab)
end
evo.batch_destroy(Q1)
end, function()
return {}
end)
basics.describe_bench(string.format('create and destroy %d entities with 3 requires / clone', N),
---@param entities evolved.id[]
function(entities)
local clone = evo.clone
local prefab = evo.spawn({ [R3] = true })
for i = 1, N do
entities[i] = clone(prefab)
end
evo.batch_destroy(Q1)
end, function()
return {}
end)
basics.describe_bench(string.format('create and destroy %d entities with 4 requires / clone', N),
---@param entities evolved.id[]
function(entities)
local clone = evo.clone
local prefab = evo.spawn({ [R4] = true })
for i = 1, N do
entities[i] = clone(prefab)
end
evo.batch_destroy(Q1)
end, function()
return {}
end)
basics.describe_bench(string.format('create and destroy %d entities with 5 requires / clone', N),
---@param entities evolved.id[]
function(entities)
local clone = evo.clone
local prefab = evo.spawn({ [R5] = true })
for i = 1, N do
entities[i] = clone(prefab)
end
evo.batch_destroy(Q1)
end, function()
return {}
end)

View File

@@ -6325,3 +6325,36 @@ do
assert(not chunk and not entity_list and not entity_count)
end
end
do
local function v2(x, y) return { x = x or 0, y = y or 0 } end
local function v2_clone(v) return { x = v.x, y = v.y } end
local v2_default = v2(1, 2)
do
local f = evo.builder():default(v2_default):spawn()
local b = evo.builder()
b:set(f)
evo.remove(f, evo.DEFAULT)
local e = b:spawn()
assert(evo.has(e, f) and evo.get(e, f).x == 1 and evo.get(e, f).y == 2)
assert(evo.get(e, f) == v2_default)
end
do
local f = evo.builder():default(v2_default):duplicate(v2_clone):spawn()
local b = evo.builder()
b:set(f)
evo.remove(f, evo.DEFAULT, evo.DUPLICATE)
local e = b:spawn()
assert(evo.has(e, f) and evo.get(e, f).x == 1 and evo.get(e, f).y == 2)
assert(evo.get(e, f) ~= v2_default)
end
end

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,34 @@
rockspec_format = "3.0"
package = "evolved.lua"
version = "1.1.0-0"
source = {
url = "git://github.com/BlackMATov/evolved.lua",
tag = "v1.1.0",
}
description = {
homepage = "https://github.com/BlackMATov/evolved.lua",
summary = "Evolved ECS (Entity-Component-System) for Lua",
detailed = [[
`evolved.lua` is a fast and flexible ECS (Entity-Component-System) library for Lua.
It is designed to be simple and easy to use, while providing all the features needed to create complex systems with blazing performance.
]],
license = "MIT",
labels = {
"ecs",
"entity",
"entities",
"component",
"components",
"entity-component",
"entity-component-system",
},
}
dependencies = {
"lua >= 5.1",
}
build = {
type = "builtin",
modules = {
evolved = "evolved.lua",
}
}