mirror of
https://github.com/BlackMATov/evolved.lua.git
synced 2025-12-13 19:48:00 +07:00
alive/empty/has/get function work with a primary fragment of pairs
This commit is contained in:
@@ -78,13 +78,13 @@ do
|
||||
evo.set(p1, s1)
|
||||
evo.set(s1, p1)
|
||||
evo.set(p2, s2)
|
||||
assert(evo.empty(evo.pair(p1, s1)))
|
||||
assert(evo.empty(evo.pair(p2, s2)))
|
||||
assert(evo.empty_all(evo.pair(p1, s1), evo.pair(p2, s2)))
|
||||
assert(evo.empty_any(evo.pair(p1, s1), evo.pair(p2, s2)))
|
||||
assert(not evo.empty(evo.pair(p1, s1)))
|
||||
assert(not evo.empty(evo.pair(p2, s2)))
|
||||
assert(not evo.empty_all(evo.pair(p1, s1), evo.pair(p2, s2)))
|
||||
assert(not evo.empty_any(evo.pair(p1, s1), evo.pair(p2, s2)))
|
||||
assert(not evo.empty_all(evo.pair(p1, s1), evo.pair(p2, s2), p1))
|
||||
assert(evo.empty_any(evo.pair(p1, s1), evo.pair(p2, s2), p1))
|
||||
assert(evo.empty_all(evo.pair(p1, s1), evo.pair(p2, s2), s2))
|
||||
assert(not evo.empty_any(evo.pair(p1, s1), evo.pair(p2, s2), p1))
|
||||
assert(not evo.empty_all(evo.pair(p1, s1), evo.pair(p2, s2), s2))
|
||||
assert(evo.empty_any(evo.pair(p1, s1), evo.pair(p2, s2), s2))
|
||||
end
|
||||
|
||||
@@ -93,11 +93,11 @@ do
|
||||
evo.set(p1, s1)
|
||||
evo.set(s1, p1)
|
||||
assert(not evo.has(evo.pair(p1, s1), p1))
|
||||
assert(not evo.has(evo.pair(p1, s1), s1))
|
||||
assert(evo.has(evo.pair(p1, s1), s1))
|
||||
assert(not evo.has_all(evo.pair(p1, s1), p1, s1))
|
||||
assert(not evo.has_any(evo.pair(p1, s1), p1, s1))
|
||||
assert(evo.has_any(evo.pair(p1, s1), p1, s1))
|
||||
assert(evo.get(evo.pair(p1, s1), p1) == nil)
|
||||
assert(evo.get(evo.pair(p1, s1), s1) == nil)
|
||||
assert(evo.get(evo.pair(p1, s1), s1) == true)
|
||||
end
|
||||
|
||||
do
|
||||
@@ -1066,6 +1066,135 @@ do
|
||||
assert(f_p1s1_chunk:entities()[1] == e1)
|
||||
end
|
||||
|
||||
do
|
||||
local f, p, s = evo.id(3)
|
||||
evo.set(p, evo.DEFAULT, 42)
|
||||
|
||||
do
|
||||
local e = evo.id()
|
||||
evo.set(e, f)
|
||||
evo.set(e, evo.pair(p, s))
|
||||
assert(evo.has(e, f) and evo.get(e, f) == true)
|
||||
assert(evo.has(e, evo.pair(p, s)) and evo.get(e, evo.pair(p, s)) == 42)
|
||||
end
|
||||
|
||||
do
|
||||
local e = evo.builder():set(f):set(evo.pair(p, s)):spawn()
|
||||
assert(evo.has(e, f) and evo.get(e, f) == true)
|
||||
assert(evo.has(e, evo.pair(p, s)) and evo.get(e, evo.pair(p, s)) == 42)
|
||||
end
|
||||
|
||||
do
|
||||
local e = evo.builder():set(f, 84):set(evo.pair(p, s), 21):spawn()
|
||||
evo.set(e, f)
|
||||
evo.set(e, evo.pair(p, s))
|
||||
assert(evo.has(e, f) and evo.get(e, f) == true)
|
||||
assert(evo.has(e, evo.pair(p, s)) and evo.get(e, evo.pair(p, s)) == 42)
|
||||
end
|
||||
end
|
||||
|
||||
do
|
||||
do
|
||||
local f, p, s = evo.id(3)
|
||||
assert(evo.empty(evo.pair(p, s)))
|
||||
|
||||
evo.set(p, f)
|
||||
assert(not evo.empty(evo.pair(p, s)))
|
||||
|
||||
evo.destroy(p)
|
||||
assert(evo.empty(evo.pair(p, s)))
|
||||
end
|
||||
|
||||
do
|
||||
local f, p, s = evo.id(3)
|
||||
assert(evo.empty(evo.pair(p, s)))
|
||||
|
||||
evo.set(p, f)
|
||||
assert(not evo.empty(evo.pair(p, s)))
|
||||
|
||||
evo.destroy(s)
|
||||
assert(evo.empty(evo.pair(p, s)))
|
||||
end
|
||||
|
||||
do
|
||||
local f, p, s = evo.id(3)
|
||||
assert(not evo.has(p, f))
|
||||
assert(not evo.has(evo.pair(p, s), f))
|
||||
|
||||
evo.set(p, f, 42)
|
||||
assert(evo.has(p, f))
|
||||
assert(evo.has(evo.pair(p, s), f))
|
||||
assert(not evo.has(evo.pair(s, p), f))
|
||||
assert(evo.get(p, f) == 42)
|
||||
assert(evo.get(evo.pair(p, s), f) == 42)
|
||||
assert(evo.get(evo.pair(s, p), f) == nil)
|
||||
end
|
||||
end
|
||||
|
||||
do
|
||||
local p, s = evo.id(3)
|
||||
|
||||
local set_count = 0
|
||||
local insert_count = 0
|
||||
local remove_count = 0
|
||||
|
||||
evo.set(p, evo.ON_SET, function(e, f, nc, oc)
|
||||
set_count = set_count + 1
|
||||
assert(f == p or f == evo.pair(p, s))
|
||||
assert(nc == 21 or nc == 42)
|
||||
assert(oc == nil or oc == 21)
|
||||
assert(evo.has(e, f))
|
||||
assert(evo.get(e, f) == nc)
|
||||
end)
|
||||
|
||||
evo.set(p, evo.ON_INSERT, function(e, f, nc)
|
||||
insert_count = insert_count + 1
|
||||
assert(f == p or f == evo.pair(p, s))
|
||||
assert(nc == 21 or nc == 42)
|
||||
assert(evo.has(e, f))
|
||||
assert(evo.get(e, f) == nc)
|
||||
end)
|
||||
|
||||
evo.set(p, evo.ON_REMOVE, function(e, f, oc)
|
||||
remove_count = remove_count + 1
|
||||
assert(f == p or f == evo.pair(p, s))
|
||||
assert(oc == 21 or oc == 42)
|
||||
assert(not evo.has(e, f))
|
||||
end)
|
||||
|
||||
do
|
||||
set_count, insert_count, remove_count = 0, 0, 0
|
||||
local e = evo.id()
|
||||
evo.set(e, p, 21)
|
||||
evo.set(e, evo.pair(p, s), 42)
|
||||
assert(set_count == 2)
|
||||
assert(insert_count == 2)
|
||||
assert(remove_count == 0)
|
||||
evo.remove(e, p)
|
||||
assert(set_count == 2)
|
||||
assert(insert_count == 2)
|
||||
assert(remove_count == 1)
|
||||
evo.remove(e, evo.pair(p, s))
|
||||
assert(set_count == 2)
|
||||
assert(insert_count == 2)
|
||||
assert(remove_count == 2)
|
||||
end
|
||||
|
||||
do
|
||||
set_count, insert_count, remove_count = 0, 0, 0
|
||||
local e = evo.id()
|
||||
evo.set(e, p, 21)
|
||||
evo.set(e, evo.pair(p, s), 42)
|
||||
assert(set_count == 2)
|
||||
assert(insert_count == 2)
|
||||
assert(remove_count == 0)
|
||||
evo.destroy(e)
|
||||
assert(set_count == 2)
|
||||
assert(insert_count == 2)
|
||||
assert(remove_count == 2)
|
||||
end
|
||||
end
|
||||
|
||||
-- TODO:
|
||||
-- How should required fragments work with pairs?
|
||||
-- How can we set defaults for paired fragments?
|
||||
|
||||
289
evolved.lua
289
evolved.lua
@@ -930,14 +930,14 @@ local __component_storage
|
||||
---@return string
|
||||
---@nodiscard
|
||||
function __id_name(id)
|
||||
---@type string?
|
||||
local id_name = __evolved_get(id, __NAME)
|
||||
if not __is_pair(id) then
|
||||
---@type string?
|
||||
local id_name = __evolved_get(id, __NAME)
|
||||
|
||||
if id_name then
|
||||
return id_name
|
||||
end
|
||||
|
||||
if __is_pair(id) then
|
||||
if id_name then
|
||||
return id_name
|
||||
end
|
||||
else
|
||||
local id_primary_index, id_secondary_index = __evolved_unpack(id)
|
||||
|
||||
local id_primary = __freelist_ids[id_primary_index] --[[@as evolved.id?]]
|
||||
@@ -4739,22 +4739,27 @@ end
|
||||
function __evolved_alive(entity)
|
||||
if not __is_pair(entity) then
|
||||
local entity_index = entity % 2 ^ 20
|
||||
return __freelist_ids[entity_index] == entity
|
||||
|
||||
if __freelist_ids[entity_index] ~= entity then
|
||||
return false
|
||||
end
|
||||
|
||||
return true
|
||||
else
|
||||
local primary_index, secondary_index = __evolved_unpack(entity)
|
||||
|
||||
local primary = __freelist_ids[primary_index] --[[@as evolved.id?]]
|
||||
if not primary or primary % 2 ^ 20 ~= primary_index then
|
||||
return false
|
||||
end
|
||||
|
||||
local secondary = __freelist_ids[secondary_index] --[[@as evolved.id?]]
|
||||
if not secondary or secondary % 2 ^ 20 ~= secondary_index then
|
||||
return false
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
local primary_index, secondary_index = __evolved_unpack(entity)
|
||||
|
||||
local primary = __freelist_ids[primary_index]
|
||||
if not primary or primary % 2 ^ 20 ~= primary_index then
|
||||
return false
|
||||
end
|
||||
|
||||
local secondary = __freelist_ids[secondary_index]
|
||||
if not secondary or secondary % 2 ^ 20 ~= secondary_index then
|
||||
return false
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
---@param ... evolved.entity entities
|
||||
@@ -4803,18 +4808,29 @@ end
|
||||
---@return boolean
|
||||
---@nodiscard
|
||||
function __evolved_empty(entity)
|
||||
if __is_pair(entity) then
|
||||
-- pairs are always empty
|
||||
return true
|
||||
if not __is_pair(entity) then
|
||||
local entity_index = entity % 2 ^ 20
|
||||
|
||||
if __freelist_ids[entity_index] ~= entity then
|
||||
return true
|
||||
end
|
||||
|
||||
return not __entity_chunks[entity_index]
|
||||
else
|
||||
local primary_index, secondary_index = __evolved_unpack(entity)
|
||||
|
||||
local primary = __freelist_ids[primary_index] --[[@as evolved.id?]]
|
||||
if not primary or primary % 2 ^ 20 ~= primary_index then
|
||||
return true
|
||||
end
|
||||
|
||||
local secondary = __freelist_ids[secondary_index] --[[@as evolved.id?]]
|
||||
if not secondary or secondary % 2 ^ 20 ~= secondary_index then
|
||||
return true
|
||||
end
|
||||
|
||||
return not __entity_chunks[primary_index]
|
||||
end
|
||||
|
||||
local entity_index = entity % 2 ^ 20
|
||||
|
||||
if __freelist_ids[entity_index] ~= entity then
|
||||
return true
|
||||
end
|
||||
|
||||
return not __entity_chunks[entity_index]
|
||||
end
|
||||
|
||||
---@param ... evolved.entity entities
|
||||
@@ -4864,24 +4880,41 @@ end
|
||||
---@return boolean
|
||||
---@nodiscard
|
||||
function __evolved_has(entity, fragment)
|
||||
if __is_pair(entity) then
|
||||
-- pairs are always empty
|
||||
return false
|
||||
if not __is_pair(entity) then
|
||||
local entity_index = entity % 2 ^ 20
|
||||
|
||||
if __freelist_ids[entity_index] ~= entity then
|
||||
return false
|
||||
end
|
||||
|
||||
local entity_chunk = __entity_chunks[entity_index]
|
||||
|
||||
if not entity_chunk then
|
||||
return false
|
||||
end
|
||||
|
||||
return __chunk_has_fragment(entity_chunk, fragment)
|
||||
else
|
||||
local primary_index, secondary_index = __evolved_unpack(entity)
|
||||
|
||||
local primary = __freelist_ids[primary_index] --[[@as evolved.id?]]
|
||||
if not primary or primary % 2 ^ 20 ~= primary_index then
|
||||
return false
|
||||
end
|
||||
|
||||
local secondary = __freelist_ids[secondary_index] --[[@as evolved.id?]]
|
||||
if not secondary or secondary % 2 ^ 20 ~= secondary_index then
|
||||
return false
|
||||
end
|
||||
|
||||
local primary_chunk = __entity_chunks[primary_index]
|
||||
|
||||
if not primary_chunk then
|
||||
return false
|
||||
end
|
||||
|
||||
return __chunk_has_fragment(primary_chunk, fragment)
|
||||
end
|
||||
|
||||
local entity_index = entity % 2 ^ 20
|
||||
|
||||
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
|
||||
@@ -4889,24 +4922,41 @@ end
|
||||
---@return boolean
|
||||
---@nodiscard
|
||||
function __evolved_has_all(entity, ...)
|
||||
if __is_pair(entity) then
|
||||
-- pairs are always empty
|
||||
return __lua_select('#', ...) == 0
|
||||
if not __is_pair(entity) then
|
||||
local entity_index = entity % 2 ^ 20
|
||||
|
||||
if __freelist_ids[entity_index] ~= entity then
|
||||
return __lua_select('#', ...) == 0
|
||||
end
|
||||
|
||||
local entity_chunk = __entity_chunks[entity_index]
|
||||
|
||||
if not entity_chunk then
|
||||
return __lua_select('#', ...) == 0
|
||||
end
|
||||
|
||||
return __chunk_has_all_fragments(entity_chunk, ...)
|
||||
else
|
||||
local primary_index, secondary_index = __evolved_unpack(entity)
|
||||
|
||||
local primary = __freelist_ids[primary_index] --[[@as evolved.id?]]
|
||||
if not primary or primary % 2 ^ 20 ~= primary_index then
|
||||
return __lua_select('#', ...) == 0
|
||||
end
|
||||
|
||||
local secondary = __freelist_ids[secondary_index] --[[@as evolved.id?]]
|
||||
if not secondary or secondary % 2 ^ 20 ~= secondary_index then
|
||||
return __lua_select('#', ...) == 0
|
||||
end
|
||||
|
||||
local primary_chunk = __entity_chunks[primary_index]
|
||||
|
||||
if not primary_chunk then
|
||||
return __lua_select('#', ...) == 0
|
||||
end
|
||||
|
||||
return __chunk_has_all_fragments(primary_chunk, ...)
|
||||
end
|
||||
|
||||
local entity_index = entity % 2 ^ 20
|
||||
|
||||
if __freelist_ids[entity_index] ~= 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 entity evolved.entity
|
||||
@@ -4914,24 +4964,41 @@ end
|
||||
---@return boolean
|
||||
---@nodiscard
|
||||
function __evolved_has_any(entity, ...)
|
||||
if __is_pair(entity) then
|
||||
-- pairs are always empty
|
||||
return false
|
||||
if not __is_pair(entity) then
|
||||
local entity_index = entity % 2 ^ 20
|
||||
|
||||
if __freelist_ids[entity_index] ~= entity then
|
||||
return false
|
||||
end
|
||||
|
||||
local entity_chunk = __entity_chunks[entity_index]
|
||||
|
||||
if not entity_chunk then
|
||||
return false
|
||||
end
|
||||
|
||||
return __chunk_has_any_fragments(entity_chunk, ...)
|
||||
else
|
||||
local primary_index, secondary_index = __evolved_unpack(entity)
|
||||
|
||||
local primary = __freelist_ids[primary_index] --[[@as evolved.id?]]
|
||||
if not primary or primary % 2 ^ 20 ~= primary_index then
|
||||
return false
|
||||
end
|
||||
|
||||
local secondary = __freelist_ids[secondary_index] --[[@as evolved.id?]]
|
||||
if not secondary or secondary % 2 ^ 20 ~= secondary_index then
|
||||
return false
|
||||
end
|
||||
|
||||
local primary_chunk = __entity_chunks[primary_index]
|
||||
|
||||
if not primary_chunk then
|
||||
return false
|
||||
end
|
||||
|
||||
return __chunk_has_any_fragments(primary_chunk, ...)
|
||||
end
|
||||
|
||||
local entity_index = entity % 2 ^ 20
|
||||
|
||||
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
|
||||
@@ -4939,25 +5006,43 @@ end
|
||||
---@return evolved.component ... components
|
||||
---@nodiscard
|
||||
function __evolved_get(entity, ...)
|
||||
if __is_pair(entity) then
|
||||
-- pairs are always empty
|
||||
return
|
||||
if not __is_pair(entity) then
|
||||
local entity_index = entity % 2 ^ 20
|
||||
|
||||
if __freelist_ids[entity_index] ~= entity then
|
||||
return
|
||||
end
|
||||
|
||||
local entity_chunk = __entity_chunks[entity_index]
|
||||
|
||||
if not entity_chunk then
|
||||
return
|
||||
end
|
||||
|
||||
local entity_place = __entity_places[entity_index]
|
||||
return __chunk_get_components(entity_chunk, entity_place, ...)
|
||||
else
|
||||
local primary_index, secondary_index = __evolved_unpack(entity)
|
||||
|
||||
local primary = __freelist_ids[primary_index] --[[@as evolved.id?]]
|
||||
if not primary or primary % 2 ^ 20 ~= primary_index then
|
||||
return
|
||||
end
|
||||
|
||||
local secondary = __freelist_ids[secondary_index] --[[@as evolved.id?]]
|
||||
if not secondary or secondary % 2 ^ 20 ~= secondary_index then
|
||||
return
|
||||
end
|
||||
|
||||
local primary_chunk = __entity_chunks[primary_index]
|
||||
|
||||
if not primary_chunk then
|
||||
return
|
||||
end
|
||||
|
||||
local primary_place = __entity_places[primary_index]
|
||||
return __chunk_get_components(primary_chunk, primary_place, ...)
|
||||
end
|
||||
|
||||
local entity_index = entity % 2 ^ 20
|
||||
|
||||
if __freelist_ids[entity_index] ~= entity then
|
||||
return
|
||||
end
|
||||
|
||||
local chunk = __entity_chunks[entity_index]
|
||||
|
||||
if not chunk then
|
||||
return
|
||||
end
|
||||
|
||||
local place = __entity_places[entity_index]
|
||||
return __chunk_get_components(chunk, place, ...)
|
||||
end
|
||||
|
||||
---@param entity evolved.entity
|
||||
|
||||
Reference in New Issue
Block a user