is_alive, is_empty, has, has_all, has_any for chunks

This commit is contained in:
BlackMATov
2025-03-17 16:44:16 +07:00
parent 9aa7758fc2
commit 6c4f9a0dbe
5 changed files with 387 additions and 261 deletions

View File

@@ -60,18 +60,19 @@ unpack :: id -> integer, integer
defer :: boolean
commit :: boolean
is_alive :: entity -> boolean
is_alive_all :: entity... -> boolean
is_alive_any :: entity... -> boolean
is_alive :: chunk | entity -> boolean
is_alive_all :: chunk | entity... -> boolean
is_alive_any :: chunk | entity... -> boolean
is_empty :: entity -> boolean
is_empty_all :: entity... -> boolean
is_empty_any :: entity... -> boolean
is_empty :: chunk | entity -> boolean
is_empty_all :: chunk | entity... -> boolean
is_empty_any :: chunk | entity... -> boolean
has :: chunk | entity, fragment -> boolean
has_all :: chunk | entity, fragment... -> boolean
has_any :: chunk | entity, fragment... -> boolean
get :: entity, fragment... -> component...
has :: entity, fragment -> boolean
has_all :: entity, fragment... -> boolean
has_any :: entity, fragment... -> boolean
set :: entity, fragment, any... -> boolean, boolean
assign :: entity, fragment, any... -> boolean, boolean

View File

@@ -2,8 +2,6 @@
## Backlog
- add has, has_all, has_any for chunks
## After first release
- add system groups

View File

@@ -1,4 +1,4 @@
require 'develop.example'
require 'develop.unbench'
-- require 'develop.unbench'
require 'develop.untests'
require 'develop.usbench'
-- require 'develop.usbench'

View File

@@ -7316,6 +7316,7 @@ do
local e12 = evo.entity():set(f1, 2):set(f2, 3):build()
do
assert(evo.is_alive(old_c1))
assert(old_c1 == evo.chunk(f1))
local old_c1_es, old_c1_ec = evo.entities(old_c1)
@@ -7326,6 +7327,8 @@ do
do
local new_c12 = assert(evo.chunk(f1, f2))
assert(not evo.is_alive(old_c12))
assert(old_c12 ~= new_c12)
local new_c12_es, new_c12_ec = evo.entities(new_c12)
@@ -7341,24 +7344,31 @@ do
do
local new_c1 = assert(evo.chunk(f1))
assert(not evo.is_alive(old_c1))
assert(old_c1 ~= new_c1)
local new_c12 = assert(evo.chunk(f1, f2))
assert(not evo.is_alive(old_c12))
assert(old_c12 ~= new_c12)
end
end
do
local f1 = evo.id()
local old_c1 = evo.chunk(f1)
local old_c1 = assert(evo.chunk(f1))
assert(evo.defer())
assert(not evo.collect_garbage())
assert(evo.is_alive(old_c1))
assert(old_c1 == evo.chunk(f1))
assert(evo.commit())
assert(not evo.is_alive(old_c1))
assert(old_c1 ~= evo.chunk(f1))
end
@@ -7805,3 +7815,191 @@ do
assert(not evo.has_any(e5, f7, f7))
assert(not evo.has_any(e5, f7, f7, f6))
end
do
local f1 = evo.id()
local c1 = assert(evo.chunk(f1))
assert(evo.is_alive(c1) and evo.is_empty(c1))
local e1 = evo.spawn_at(c1)
assert(evo.is_alive(c1) and not evo.is_empty(c1))
assert(evo.destroy(e1))
assert(evo.is_alive(c1) and evo.is_empty(c1))
assert(evo.collect_garbage())
assert(not evo.is_alive(c1) and evo.is_empty(c1))
end
do
local f1, f2 = evo.id(2)
local c1 = assert(evo.chunk(f1))
local c12 = assert(evo.chunk(f1, f2))
assert(evo.is_alive(c1))
assert(evo.is_alive(c12))
assert(evo.is_alive_all())
assert(evo.is_alive_all(c1))
assert(evo.is_alive_all(c1, c12))
assert(not evo.is_alive_any())
assert(evo.is_alive_any(c1))
assert(evo.is_alive_any(c1, c12))
assert(evo.is_empty(c1))
assert(evo.is_empty(c12))
assert(evo.is_empty_all())
assert(evo.is_empty_all(c1))
assert(evo.is_empty_all(c1, c12))
assert(not evo.is_empty_any())
assert(evo.is_empty_any(c1))
assert(evo.is_empty_any(c1, c12))
local e12 = evo.spawn_at(c12)
assert(evo.is_alive(c1))
assert(evo.is_alive(c12))
assert(evo.is_alive_all())
assert(evo.is_alive_all(c1))
assert(evo.is_alive_all(c1, c12))
assert(not evo.is_alive_any())
assert(evo.is_alive_any(c1))
assert(evo.is_alive_any(c1, c12))
assert(evo.is_empty(c1))
assert(not evo.is_empty(c12))
assert(evo.is_empty_all())
assert(evo.is_empty_all(c1))
assert(not evo.is_empty_all(c1, c12))
assert(not evo.is_empty_any())
assert(evo.is_empty_any(c1))
assert(evo.is_empty_any(c1, c12))
assert(evo.remove(e12, f2))
assert(evo.is_alive(c1))
assert(evo.is_alive(c12))
assert(evo.is_alive_all())
assert(evo.is_alive_all(c1))
assert(evo.is_alive_all(c1, c12))
assert(not evo.is_alive_any())
assert(evo.is_alive_any(c1))
assert(evo.is_alive_any(c1, c12))
assert(not evo.is_empty(c1))
assert(evo.is_empty(c12))
assert(evo.is_empty_all())
assert(not evo.is_empty_all(c1))
assert(not evo.is_empty_all(c1, c12))
assert(not evo.is_empty_any())
assert(not evo.is_empty_any(c1))
assert(evo.is_empty_any(c1, c12))
assert(evo.collect_garbage())
assert(evo.is_alive(c1))
assert(not evo.is_alive(c12))
assert(evo.is_alive_all())
assert(evo.is_alive_all(c1))
assert(not evo.is_alive_all(c1, c12))
assert(not evo.is_alive_any())
assert(evo.is_alive_any(c1))
assert(evo.is_alive_any(c1, c12))
assert(not evo.is_empty(c1))
assert(evo.is_empty(c12))
assert(evo.is_empty_all())
assert(not evo.is_empty_all(c1))
assert(not evo.is_empty_all(c1, c12))
assert(not evo.is_empty_any())
assert(not evo.is_empty_any(c1))
assert(evo.is_empty_any(c1, c12))
assert(evo.remove(e12, f1))
assert(evo.is_alive(c1))
assert(not evo.is_alive(c12))
assert(evo.is_alive_all())
assert(evo.is_alive_all(c1))
assert(not evo.is_alive_all(c1, c12))
assert(not evo.is_alive_any())
assert(evo.is_alive_any(c1))
assert(evo.is_alive_any(c1, c12))
assert(evo.is_empty(c1))
assert(evo.is_empty(c12))
assert(evo.is_empty_all())
assert(evo.is_empty_all(c1))
assert(evo.is_empty_all(c1, c12))
assert(not evo.is_empty_any())
assert(evo.is_empty_any(c1))
assert(evo.is_empty_any(c1, c12))
assert(evo.collect_garbage())
assert(not evo.is_alive(c1))
assert(not evo.is_alive(c12))
assert(evo.is_alive_all())
assert(not evo.is_alive_all(c1))
assert(not evo.is_alive_all(c1, c12))
assert(not evo.is_alive_any())
assert(not evo.is_alive_any(c1))
assert(not evo.is_alive_any(c1, c12))
assert(evo.is_empty(c1))
assert(evo.is_empty(c12))
assert(evo.is_empty_all())
assert(evo.is_empty_all(c1))
assert(evo.is_empty_all(c1, c12))
assert(not evo.is_empty_any())
assert(evo.is_empty_any(c1))
assert(evo.is_empty_any(c1, c12))
end
do
local f1, f2 = evo.id(2)
local c1 = assert(evo.chunk(f1))
local c12 = assert(evo.chunk(f1, f2))
assert(evo.has(c1, f1))
assert(not evo.has(c1, f2))
assert(evo.has(c12, f1))
assert(evo.has(c12, f2))
assert(evo.has_all(c1))
assert(evo.has_all(c1, f1))
assert(not evo.has_all(c1, f1, f2))
assert(evo.has_all(c12))
assert(evo.has_all(c12, f1))
assert(evo.has_all(c12, f1, f2))
assert(not evo.has_any(c1))
assert(evo.has_any(c1, f1))
assert(evo.has_any(c1, f1, f2))
assert(not evo.has_any(c12))
assert(evo.has_any(c12, f1))
assert(evo.has_any(c12, f1, f2))
assert(evo.collect_garbage())
assert(not evo.is_alive_any(c1, c12))
assert(evo.has(c1, f1))
assert(not evo.has(c1, f2))
assert(evo.has(c12, f1))
assert(evo.has(c12, f2))
assert(evo.has_all(c1))
assert(evo.has_all(c1, f1))
assert(not evo.has_all(c1, f1, f2))
assert(evo.has_all(c12))
assert(evo.has_all(c12, f1))
assert(evo.has_all(c12, f1, f2))
assert(not evo.has_any(c1))
assert(evo.has_any(c1, f1))
assert(evo.has_any(c1, f1, f2))
assert(not evo.has_any(c12))
assert(evo.has_any(c12, f1))
assert(evo.has_any(c12, f1, f2))
end

View File

@@ -130,6 +130,7 @@ local __lua_pcall = pcall
local __lua_select = select
local __lua_table_sort = table.sort
local __lua_table_unpack = table.unpack or unpack
local __lua_type = type
local __lua_table_move = (function()
---@param a1 table
@@ -4908,255 +4909,243 @@ __evolved_commit = function()
return __commit()
end
---@param entity evolved.entity
---@param chunk_or_entity evolved.chunk | evolved.entity
---@return boolean
---@nodiscard
__evolved_is_alive = function(entity)
local entity_index = entity % 0x100000
return __freelist_ids[entity_index] == entity
__evolved_is_alive = function(chunk_or_entity)
if __lua_type(chunk_or_entity) ~= 'number' then
---@cast chunk_or_entity -evolved.entity
return not chunk_or_entity.__unreachable_or_collected
end
---@cast chunk_or_entity -evolved.chunk
local entity_index = chunk_or_entity % 0x100000
return __freelist_ids[entity_index] == chunk_or_entity
end
---@param ... evolved.entity entities
---@param ... evolved.chunk | evolved.entity chunks_or_entities
---@return boolean
---@nodiscard
__evolved_is_alive_all = function(...)
local entity_count = __lua_select('#', ...)
local argument_count = __lua_select('#', ...)
if entity_count == 0 then
if argument_count == 0 then
return true
end
local ids = __freelist_ids
local freelist_ids = __freelist_ids
if entity_count == 1 then
local e1 = ...
local i1 = e1 % 0x100000
return
(ids[i1] == e1)
for argument_index = 1, argument_count do
---@type evolved.chunk | evolved.entity
local chunk_or_entity = __lua_select(argument_index, ...)
if __lua_type(chunk_or_entity) ~= 'number' then
---@cast chunk_or_entity -evolved.entity
if chunk_or_entity.__unreachable_or_collected then
return false
end
else
---@cast chunk_or_entity -evolved.chunk
local entity_index = chunk_or_entity % 0x100000
if freelist_ids[entity_index] ~= chunk_or_entity then
return false
end
end
end
if entity_count == 2 then
local e1, e2 = ...
local i1, i2 = e1 % 0x100000, e2 % 0x100000
return
(ids[i1] == e1) and
(ids[i2] == e2)
end
if entity_count == 3 then
local e1, e2, e3 = ...
local i1, i2, i3 = e1 % 0x100000, e2 % 0x100000, e3 % 0x100000
return
(ids[i1] == e1) and
(ids[i2] == e2) and
(ids[i3] == e3)
end
if entity_count == 4 then
local e1, e2, e3, e4 = ...
local i1, i2, i3, i4 = e1 % 0x100000, e2 % 0x100000, e3 % 0x100000, e4 % 0x100000
return
(ids[i1] == e1) and
(ids[i2] == e2) and
(ids[i3] == e3) and
(ids[i4] == e4)
end
do
local e1, e2, e3, e4 = ...
local i1, i2, i3, i4 = e1 % 0x100000, e2 % 0x100000, e3 % 0x100000, e4 % 0x100000
return
(ids[i1] == e1) and
(ids[i2] == e2) and
(ids[i3] == e3) and
(ids[i4] == e4) and
__evolved_is_alive_all(__lua_select(5, ...))
end
return true
end
---@param ... evolved.entity entities
---@param ... evolved.chunk | evolved.entity chunks_or_entities
---@return boolean
---@nodiscard
__evolved_is_alive_any = function(...)
local entity_count = __lua_select('#', ...)
local argument_count = __lua_select('#', ...)
if entity_count == 0 then
if argument_count == 0 then
return false
end
local ids = __freelist_ids
local freelist_ids = __freelist_ids
if entity_count == 1 then
local e1 = ...
local i1 = e1 % 0x100000
return
(ids[i1] == e1)
for argument_index = 1, argument_count do
---@type evolved.chunk | evolved.entity
local chunk_or_entity = __lua_select(argument_index, ...)
if __lua_type(chunk_or_entity) ~= 'number' then
---@cast chunk_or_entity -evolved.entity
if not chunk_or_entity.__unreachable_or_collected then
return true
end
else
---@cast chunk_or_entity -evolved.chunk
local entity_index = chunk_or_entity % 0x100000
if freelist_ids[entity_index] == chunk_or_entity then
return true
end
end
end
if entity_count == 2 then
local e1, e2 = ...
local i1, i2 = e1 % 0x100000, e2 % 0x100000
return
(ids[i1] == e1) or
(ids[i2] == e2)
end
if entity_count == 3 then
local e1, e2, e3 = ...
local i1, i2, i3 = e1 % 0x100000, e2 % 0x100000, e3 % 0x100000
return
(ids[i1] == e1) or
(ids[i2] == e2) or
(ids[i3] == e3)
end
if entity_count == 4 then
local e1, e2, e3, e4 = ...
local i1, i2, i3, i4 = e1 % 0x100000, e2 % 0x100000, e3 % 0x100000, e4 % 0x100000
return
(ids[i1] == e1) or
(ids[i2] == e2) or
(ids[i3] == e3) or
(ids[i4] == e4)
end
do
local e1, e2, e3, e4 = ...
local i1, i2, i3, i4 = e1 % 0x100000, e2 % 0x100000, e3 % 0x100000, e4 % 0x100000
return
(ids[i1] == e1) or
(ids[i2] == e2) or
(ids[i3] == e3) or
(ids[i4] == e4) or
__evolved_is_alive_any(__lua_select(5, ...))
end
return false
end
---@param entity evolved.entity
---@param chunk_or_entity evolved.chunk | evolved.entity
---@return boolean
---@nodiscard
__evolved_is_empty = function(entity)
local entity_index = entity % 0x100000
return __freelist_ids[entity_index] ~= entity
__evolved_is_empty = function(chunk_or_entity)
if __lua_type(chunk_or_entity) ~= 'number' then
---@cast chunk_or_entity -evolved.entity
return chunk_or_entity.__entity_count == 0
end
---@cast chunk_or_entity -evolved.chunk
local entity_index = chunk_or_entity % 0x100000
return __freelist_ids[entity_index] ~= chunk_or_entity
or not __entity_chunks[entity_index]
end
---@param ... evolved.entity entities
---@param ... evolved.chunk | evolved.entity chunks_or_entities
---@return boolean
---@nodiscard
__evolved_is_empty_all = function(...)
local entity_count = __lua_select('#', ...)
local argument_count = __lua_select('#', ...)
if entity_count == 0 then
if argument_count == 0 then
return true
end
local ids = __freelist_ids
local ecs = __entity_chunks
local freelist_ids = __freelist_ids
if entity_count == 1 then
local e1 = ...
local i1 = e1 % 0x100000
return
(ids[i1] ~= e1 or not ecs[i1])
for argument_index = 1, argument_count do
---@type evolved.chunk | evolved.entity
local chunk_or_entity = __lua_select(argument_index, ...)
if __lua_type(chunk_or_entity) ~= 'number' then
---@cast chunk_or_entity -evolved.entity
if chunk_or_entity.__entity_count > 0 then
return false
end
else
---@cast chunk_or_entity -evolved.chunk
local entity_index = chunk_or_entity % 0x100000
if freelist_ids[entity_index] == chunk_or_entity and __entity_chunks[entity_index] then
return false
end
end
end
if entity_count == 2 then
local e1, e2 = ...
local i1, i2 = e1 % 0x100000, e2 % 0x100000
return
(ids[i1] ~= e1 or not ecs[i1]) and
(ids[i2] ~= e2 or not ecs[i2])
end
if entity_count == 3 then
local e1, e2, e3 = ...
local i1, i2, i3 = e1 % 0x100000, e2 % 0x100000, e3 % 0x100000
return
(ids[i1] ~= e1 or not ecs[i1]) and
(ids[i2] ~= e2 or not ecs[i2]) and
(ids[i3] ~= e3 or not ecs[i3])
end
if entity_count == 4 then
local e1, e2, e3, e4 = ...
local i1, i2, i3, i4 = e1 % 0x100000, e2 % 0x100000, e3 % 0x100000, e4 % 0x100000
return
(ids[i1] ~= e1 or not ecs[i1]) and
(ids[i2] ~= e2 or not ecs[i2]) and
(ids[i3] ~= e3 or not ecs[i3]) and
(ids[i4] ~= e4 or not ecs[i4])
end
do
local e1, e2, e3, e4 = ...
local i1, i2, i3, i4 = e1 % 0x100000, e2 % 0x100000, e3 % 0x100000, e4 % 0x100000
return
(ids[i1] ~= e1 or not ecs[i1]) and
(ids[i2] ~= e2 or not ecs[i2]) and
(ids[i3] ~= e3 or not ecs[i3]) and
(ids[i4] ~= e4 or not ecs[i4]) and
__evolved_is_empty_all(__lua_select(5, ...))
end
return true
end
---@param ... evolved.entity entities
---@param ... evolved.chunk | evolved.entity chunks_or_entities
---@return boolean
---@nodiscard
__evolved_is_empty_any = function(...)
local entity_count = __lua_select('#', ...)
local argument_count = __lua_select('#', ...)
if entity_count == 0 then
if argument_count == 0 then
return false
end
local ids = __freelist_ids
local ecs = __entity_chunks
local freelist_ids = __freelist_ids
if entity_count == 1 then
local e1 = ...
local i1 = e1 % 0x100000
return
(ids[i1] ~= e1 or not ecs[i1])
for argument_index = 1, argument_count do
---@type evolved.chunk | evolved.entity
local chunk_or_entity = __lua_select(argument_index, ...)
if __lua_type(chunk_or_entity) ~= 'number' then
---@cast chunk_or_entity -evolved.entity
if chunk_or_entity.__entity_count == 0 then
return true
end
else
---@cast chunk_or_entity -evolved.chunk
local entity_index = chunk_or_entity % 0x100000
if freelist_ids[entity_index] ~= chunk_or_entity or not __entity_chunks[entity_index] then
return true
end
end
end
if entity_count == 2 then
local e1, e2 = ...
local i1, i2 = e1 % 0x100000, e2 % 0x100000
return
(ids[i1] ~= e1 or not ecs[i1]) or
(ids[i2] ~= e2 or not ecs[i2])
return false
end
---@param chunk_or_entity evolved.chunk | evolved.entity
---@param fragment evolved.fragment
---@return boolean
---@nodiscard
__evolved_has = function(chunk_or_entity, fragment)
if __lua_type(chunk_or_entity) ~= 'number' then
---@cast chunk_or_entity -evolved.entity
return __chunk_has_fragment(chunk_or_entity, fragment)
end
if entity_count == 3 then
local e1, e2, e3 = ...
local i1, i2, i3 = e1 % 0x100000, e2 % 0x100000, e3 % 0x100000
return
(ids[i1] ~= e1 or not ecs[i1]) or
(ids[i2] ~= e2 or not ecs[i2]) or
(ids[i3] ~= e3 or not ecs[i3])
---@cast chunk_or_entity -evolved.chunk
local entity_index = chunk_or_entity % 0x100000
if __freelist_ids[entity_index] ~= chunk_or_entity then
return false
end
if entity_count == 4 then
local e1, e2, e3, e4 = ...
local i1, i2, i3, i4 = e1 % 0x100000, e2 % 0x100000, e3 % 0x100000, e4 % 0x100000
return
(ids[i1] ~= e1 or not ecs[i1]) or
(ids[i2] ~= e2 or not ecs[i2]) or
(ids[i3] ~= e3 or not ecs[i3]) or
(ids[i4] ~= e4 or not ecs[i4])
local chunk = __entity_chunks[entity_index]
if not chunk then
return false
end
do
local e1, e2, e3, e4 = ...
local i1, i2, i3, i4 = e1 % 0x100000, e2 % 0x100000, e3 % 0x100000, e4 % 0x100000
return
(ids[i1] ~= e1 or not ecs[i1]) or
(ids[i2] ~= e2 or not ecs[i2]) or
(ids[i3] ~= e3 or not ecs[i3]) or
(ids[i4] ~= e4 or not ecs[i4]) or
__evolved_is_empty_any(__lua_select(5, ...))
return __chunk_has_fragment(chunk, fragment)
end
---@param chunk_or_entity evolved.chunk | evolved.entity
---@param ... evolved.fragment fragments
---@return boolean
---@nodiscard
__evolved_has_all = function(chunk_or_entity, ...)
if __lua_type(chunk_or_entity) ~= 'number' then
---@cast chunk_or_entity -evolved.entity
return __chunk_has_all_fragments(chunk_or_entity, ...)
end
---@cast chunk_or_entity -evolved.chunk
local entity_index = chunk_or_entity % 0x100000
if __freelist_ids[entity_index] ~= chunk_or_entity then
return __lua_select('#', ...) == 0
end
local chunk = __entity_chunks[entity_index]
if not chunk then
return __lua_select('#', ...) == 0
end
return __chunk_has_all_fragments(chunk, ...)
end
---@param chunk_or_entity evolved.chunk | evolved.entity
---@param ... evolved.fragment fragments
---@return boolean
---@nodiscard
__evolved_has_any = function(chunk_or_entity, ...)
if __lua_type(chunk_or_entity) ~= 'number' then
---@cast chunk_or_entity -evolved.entity
return __chunk_has_any_fragments(chunk_or_entity, ...)
end
---@cast chunk_or_entity -evolved.chunk
local entity_index = chunk_or_entity % 0x100000
if __freelist_ids[entity_index] ~= chunk_or_entity then
return false
end
local chunk = __entity_chunks[entity_index]
if not chunk then
return false
end
return __chunk_has_any_fragments(chunk, ...)
end
---@param entity evolved.entity
@@ -5180,66 +5169,6 @@ __evolved_get = function(entity, ...)
return __chunk_get_components(chunk, place, ...)
end
---@param entity evolved.entity
---@param fragment evolved.fragment
---@return boolean
---@nodiscard
__evolved_has = function(entity, fragment)
local entity_index = entity % 0x100000
if __freelist_ids[entity_index] ~= entity then
return false
end
local chunk = __entity_chunks[entity_index]
if not chunk then
return false
end
return __chunk_has_fragment(chunk, fragment)
end
---@param entity evolved.entity
---@param ... evolved.fragment fragments
---@return boolean
---@nodiscard
__evolved_has_all = function(entity, ...)
local entity_index = entity % 0x100000
if __freelist_ids[entity_index] ~= entity then
return false
end
local chunk = __entity_chunks[entity_index]
if not chunk then
return __lua_select('#', ...) == 0
end
return __chunk_has_all_fragments(chunk, ...)
end
---@param entity evolved.entity
---@param ... evolved.fragment fragments
---@return boolean
---@nodiscard
__evolved_has_any = function(entity, ...)
local entity_index = entity % 0x100000
if __freelist_ids[entity_index] ~= entity then
return false
end
local chunk = __entity_chunks[entity_index]
if not chunk then
return false
end
return __chunk_has_any_fragments(chunk, ...)
end
---@param entity evolved.entity
---@param fragment evolved.fragment
---@param ... any component arguments