57 Commits

Author SHA1 Message Date
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
18 changed files with 4497 additions and 697 deletions

5
.luacov Normal file
View File

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

221
README.md
View File

@@ -35,6 +35,7 @@
- [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)
@@ -53,6 +54,7 @@
- [Aliases](#aliases)
- [Predefs](#predefs)
- [Functions](#functions)
- [Relations](#relations)
- [Classes](#classes)
- [Chunk](#chunk)
- [Builder](#builder)
@@ -94,7 +96,7 @@ luarocks install evolved.lua
## Quick Start
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 [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.
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
@@ -146,14 +148,18 @@ function evolved.alive_any(...) end
Sometimes (for debugging purposes, for example), it is necessary to extract the index and version from an identifier or to pack them back into an identifier. The [`evolved.pack`](#evolvedpack) and [`evolved.unpack`](#evolvedunpack) functions can be used for this purpose.
```lua
---@param index integer
---@param version integer
---@param primary integer
---@param secondary integer
---@param options? integer
---@return evolved.id id
function evolved.pack(index, version) end
---@nodiscard
function evolved.pack(primary, secondary, options) 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
```
@@ -405,7 +411,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'
@@ -447,6 +453,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.
@@ -1015,19 +1050,26 @@ 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
@@ -1061,9 +1103,10 @@ DESTRUCTION_POLICY_REMOVE_FRAGMENT :: id
```
id :: integer? -> id...
name :: id... -> string...
pack :: integer, integer -> id
unpack :: id -> integer, integer
pack :: integer, integer, integer? -> id
unpack :: id -> integer, integer, integer
defer :: boolean
commit :: boolean
@@ -1104,6 +1147,25 @@ debug_mode :: boolean -> ()
collect_garbage :: ()
```
### Relations
```
pair :: id, id -> id
unpair :: id -> 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
@@ -1146,6 +1208,7 @@ 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
@@ -1192,6 +1255,8 @@ builder_mt:destruction_policy :: id -> builder
## Predefs
### `evolved.ANY`
### `evolved.TAG`
### `evolved.NAME`
@@ -1200,6 +1265,8 @@ builder_mt:destruction_policy :: id -> builder
### `evolved.EXPLICIT`
### `evolved.INTERNAL`
### `evolved.DEFAULT`
### `evolved.DUPLICATE`
@@ -1238,7 +1305,7 @@ builder_mt:destruction_policy :: id -> builder
### `evolved.DESTRUCTION_POLICY_REMOVE_FRAGMENT`
## Functions
## Core Functions
### `evolved.id`
@@ -1249,22 +1316,33 @@ builder_mt:destruction_policy :: id -> builder
function evolved.id(count) end
```
### `evolved.name`
```lua
---@param ... evolved.id ids
---@return string... names
---@nodiscard
function evolved.name(...) end
```
### `evolved.pack`
```lua
---@param index integer
---@param version integer
---@param primary integer
---@param secondary integer
---@param options? integer
---@return evolved.id id
---@nodiscard
function evolved.pack(index, version) end
function evolved.pack(primary, secondary, options) end
```
### `evolved.unpack`
```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
```
@@ -1496,6 +1574,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.id pair
---@nodiscard
function evolved.pair(primary, secondary) end
```
### `evolved.unpair`
```lua
---@param pair evolved.id
---@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
@@ -1696,6 +1880,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

View File

@@ -2,10 +2,19 @@
## 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
- use compact prefix-tree for chunks
- optional ffi component storages
- add EXCLUSIVE fragment trait
## 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,9 +1,13 @@
require 'develop.example'
require 'develop.untests'
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'
@@ -21,3 +25,5 @@ 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

@@ -9,12 +9,39 @@ evo.debug_mode(true)
---
for _ = 1, 1000 do
local initial_index = math.random(1, 0xFFFFF)
local initial_version = math.random(1, 0xFFFFF)
local initial_primary = math.random(1, 2 ^ 20 - 1)
local initial_secondary = math.random(1, 2 ^ 20 - 1)
local packed_id = evo.pack(initial_index, initial_version)
local unpacked_index, unpacked_version = evo.unpack(packed_id)
local packed_id = evo.pack(initial_primary, initial_secondary)
local unpacked_primary, unpacked_secondary, unpacked_options = evo.unpack(packed_id)
assert(initial_index == unpacked_index)
assert(initial_version == unpacked_version)
assert(initial_primary == unpacked_primary)
assert(initial_secondary == unpacked_secondary)
assert(0 == unpacked_options)
end
for _ = 1, 1000 do
local initial_primary = math.random(1, 2 ^ 20 - 1)
local initial_secondary = math.random(1, 2 ^ 20 - 1)
local initial_options = math.random(1, 2 ^ 12 - 1)
local packed_id = evo.pack(initial_primary, initial_secondary, initial_options)
local unpacked_primary, unpacked_secondary, unpacked_options = evo.unpack(packed_id)
assert(initial_primary == unpacked_primary)
assert(initial_secondary == unpacked_secondary)
assert(initial_options == unpacked_options)
end
for _ = 1, 1000 do
local initial_primary = math.random(1, 2 ^ 31 - 1)
local initial_secondary = math.random(1, 2 ^ 31 - 1)
local initial_options = math.random(1, 2 ^ 31 - 1)
local packed_id = evo.pack(initial_primary, initial_secondary, initial_options)
local unpacked_primary, unpacked_secondary, unpacked_options = evo.unpack(packed_id)
assert(initial_primary % 2 ^ 20 == unpacked_primary)
assert(initial_secondary % 2 ^ 20 == unpacked_secondary)
assert(initial_options % 2 ^ 12 == unpacked_options)
end

View File

@@ -108,6 +108,24 @@ end
---
---
evo.destroy(__table_unpack(all_entity_list))
evo.destroy(__table_unpack(all_fragment_list))
evo.collect_garbage()
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,254 @@
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
if evo.has(fragment, evo.EXPLICIT) then
local is_fragment_included =
query_include_set[fragment] ~= nil or
query_include_set[evo.pair(fragment, evo.ANY)] ~= nil
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

@@ -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