From ba1917bc6d95301ad85324741f1dca22218aa1d0 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Sun, 16 Mar 2025 06:49:02 +0700 Subject: [PATCH] is_alive/empty for multiple entities --- README.md | 4 +- develop/all.lua | 4 +- develop/untests.lua | 58 +++++++++++++++++++++ evolved.lua | 123 +++++++++++++++++++++++++++++++++++++++----- 4 files changed, 171 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 52744ac..ef79422 100644 --- a/README.md +++ b/README.md @@ -60,8 +60,8 @@ unpack :: id -> integer, integer defer :: boolean commit :: boolean -is_alive :: entity -> boolean -is_empty :: entity -> boolean +is_alive :: entity... -> boolean +is_empty :: entity... -> boolean get :: entity, fragment... -> component... has :: entity, fragment -> boolean diff --git a/develop/all.lua b/develop/all.lua index 7d7536f..49a0913 100644 --- a/develop/all.lua +++ b/develop/all.lua @@ -1,4 +1,4 @@ require 'develop.example' --- require 'develop.unbench' +require 'develop.unbench' require 'develop.untests' --- require 'develop.usbench' +require 'develop.usbench' diff --git a/develop/untests.lua b/develop/untests.lua index 1c067b7..2734574 100644 --- a/develop/untests.lua +++ b/develop/untests.lua @@ -7643,3 +7643,61 @@ do assert(not evo.is_alive(e1) and evo.is_empty(e1)) assert(not evo.is_alive(e2) and evo.is_empty(e2)) end + +do + local a1, a2, a3, a4, a5 = evo.id(5) + + assert(evo.is_alive()) + assert(evo.is_alive(a1)) + assert(evo.is_alive(a1, a2)) + assert(evo.is_alive(a1, a2, a3)) + assert(evo.is_alive(a1, a2, a3, a4)) + assert(evo.is_alive(a1, a2, a3, a4, a5)) + + local d1, d2 = evo.id(2) + assert(evo.destroy(d1, d2)) + + assert(not evo.is_alive(d1)) + assert(not evo.is_alive(d1, d2)) + assert(not evo.is_alive(d1, a1)) + assert(not evo.is_alive(a1, d1)) + assert(not evo.is_alive(d1, d2, a1)) + assert(not evo.is_alive(d1, a1, a2)) + assert(not evo.is_alive(d1, a1, a2, d2, a3)) + assert(not evo.is_alive(d1, a1, a2, d2, a3, d1)) +end + +do + local e1, e2, e3, e4, e5 = evo.id(5) + + assert(evo.is_empty()) + assert(evo.is_empty(e1)) + assert(evo.is_empty(e1, e2)) + assert(evo.is_empty(e1, e2, e3)) + assert(evo.is_empty(e1, e2, e3, e4)) + assert(evo.is_empty(e1, e2, e3, e4, e5)) + + local d1, d2 = evo.id(2) + assert(evo.destroy(d1, d2)) + + assert(evo.is_empty(d1)) + assert(evo.is_empty(d1, d2)) + assert(evo.is_empty(d1, e1)) + assert(evo.is_empty(e1, d1)) + assert(evo.is_empty(d1, d2, e1)) + assert(evo.is_empty(d1, e1, e2)) + assert(evo.is_empty(d1, e1, e2, d2, e3)) + assert(evo.is_empty(d1, e1, e2, d2, e3, d1)) + + local f1, f2 = evo.id(2) + assert(evo.insert(f1, f1) and evo.insert(f2, f2)) + + assert(not evo.is_empty(f1)) + assert(not evo.is_empty(f1, f2)) + assert(not evo.is_empty(f1, e1)) + assert(not evo.is_empty(e1, f1)) + assert(not evo.is_empty(f1, f2, e1)) + assert(not evo.is_empty(f1, e1, e2)) + assert(not evo.is_empty(f1, e1, e2, f2, e3)) + assert(not evo.is_empty(f1, e1, e2, f2, e3, f1)) +end diff --git a/evolved.lua b/evolved.lua index c7a73f7..57a1c0c 100644 --- a/evolved.lua +++ b/evolved.lua @@ -4865,26 +4865,121 @@ __evolved_commit = function() return __commit() end ----@param entity evolved.entity +---@param ... evolved.entity entities ---@return boolean ---@nodiscard -__evolved_is_alive = function(entity) - local entity_index = entity % 0x100000 +__evolved_is_alive = function(...) + local entity_count = __lua_select('#', ...) - return __freelist_ids[entity_index] == entity -end - ----@param entity evolved.entity ----@return boolean ----@nodiscard -__evolved_is_empty = function(entity) - local entity_index = entity % 0x100000 - - if __freelist_ids[entity_index] ~= entity then + if entity_count == 0 then return true end - return not __entity_chunks[entity_index] + local ids = __freelist_ids + + if entity_count == 1 then + local e1 = ... + local i1 = e1 % 0x100000 + return + (ids[i1] == e1) + 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(__lua_select(5, ...)) + end +end + +---@param ... evolved.entity entities +---@return boolean +---@nodiscard +__evolved_is_empty = function(...) + local entity_count = __lua_select('#', ...) + + if entity_count == 0 then + return true + end + + local ids = __freelist_ids + local ecs = __entity_chunks + + if entity_count == 1 then + local e1 = ... + local i1 = e1 % 0x100000 + return + (ids[i1] ~= e1 or not ecs[i1]) + 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(__lua_select(5, ...)) + end end ---@param entity evolved.entity