dirty proof of concept multi_spawn impl (without any optimization)

This commit is contained in:
BlackMATov
2025-09-11 06:57:13 +07:00
parent 632232b9b1
commit de7d1a6674
4 changed files with 302 additions and 20 deletions

View File

@@ -1351,10 +1351,10 @@ function evolved.spawn(components) end
### `evolved.multi_spawn`
```lua
---@param count integer
---@param entity_count integer
---@param components? table<evolved.fragment, evolved.component>
---@return evolved.entity[] entity_list
function evolved.multi_spawn(count, components) end
function evolved.multi_spawn(entity_count, components) end
```
### `evolved.clone`
@@ -1369,11 +1369,11 @@ function evolved.clone(prefab, components) end
### `evolved.multi_clone`
```lua
---@param count integer
---@param entity_count integer
---@param prefab evolved.entity
---@param components? table<evolved.fragment, evolved.component>
---@return evolved.entity[] entity_list
function evolved.multi_clone(count, prefab, components) end
function evolved.multi_clone(entity_count, prefab, components) end
```
### `evolved.alive`
@@ -1678,9 +1678,9 @@ function evolved.builder_mt:spawn() end
#### `evolved.builder_mt:multi_spawn`
```lua
---@param count integer
---@param entity_count integer
---@return evolved.entity[] entity_list
function evolved.builder_mt:multi_spawn(count) end
function evolved.builder_mt:multi_spawn(entity_count) end
```
#### `evolved.builder_mt:clone`
@@ -1694,10 +1694,10 @@ function evolved.builder_mt:clone(prefab) end
#### `evolved.builder_mt:multi_clone`
```lua
---@param count integer
---@param entity_count integer
---@param prefab evolved.entity
---@return evolved.entity[] entity_list
function evolved.builder_mt:multi_clone(count, prefab) end
function evolved.builder_mt:multi_clone(entity_count, prefab) end
```
#### `evolved.builder_mt:has`

View File

@@ -1,5 +1,6 @@
require 'develop.samples.systems'
require 'develop.testing.multi_spawn_tests'
require 'develop.testing.name_tests'
require 'develop.testing.requires_fragment_tests'
require 'develop.testing.system_as_query_tests'

View File

@@ -0,0 +1,194 @@
local evo = require 'evolved'
do
local entity_list
do
entity_list = evo.multi_spawn(0)
assert(entity_list and #entity_list == 0)
entity_list = evo.multi_spawn(0, {})
assert(entity_list and #entity_list == 0)
end
do
entity_list = evo.multi_spawn(-1)
assert(entity_list and #entity_list == 0)
entity_list = evo.multi_spawn(-1, {})
assert(entity_list and #entity_list == 0)
end
do
entity_list = evo.builder():multi_spawn(0)
assert(entity_list and #entity_list == 0)
end
do
entity_list = evo.builder():multi_spawn(-1)
assert(entity_list and #entity_list == 0)
end
end
do
local entity_list
do
entity_list = evo.multi_spawn(1)
assert(entity_list and #entity_list == 1)
assert(entity_list[1] and evo.empty(entity_list[1]))
assert(not entity_list[2])
entity_list = evo.multi_spawn(1, {})
assert(entity_list and #entity_list == 1)
assert(entity_list[1] and evo.empty(entity_list[1]))
assert(not entity_list[2])
end
do
entity_list = evo.multi_spawn(2)
assert(entity_list and #entity_list == 2)
assert(entity_list[1] and evo.empty(entity_list[1]))
assert(entity_list[2] and evo.empty(entity_list[2]))
assert(not entity_list[3])
entity_list = evo.multi_spawn(2, {})
assert(entity_list and #entity_list == 2)
assert(entity_list[1] and evo.empty(entity_list[1]))
assert(entity_list[2] and evo.empty(entity_list[2]))
assert(not entity_list[3])
end
do
entity_list = evo.builder():multi_spawn(1)
assert(entity_list and #entity_list == 1)
assert(entity_list[1] and evo.empty(entity_list[1]))
assert(not entity_list[2])
end
do
entity_list = evo.builder():multi_spawn(2)
assert(entity_list and #entity_list == 2)
assert(entity_list[1] and evo.empty(entity_list[1]))
assert(entity_list[2] and evo.empty(entity_list[2]))
assert(not entity_list[3])
end
end
do
local entity_list
local prefab = evo.id()
do
entity_list = evo.multi_clone(0, prefab)
assert(entity_list and #entity_list == 0)
entity_list = evo.multi_clone(0, prefab, {})
assert(entity_list and #entity_list == 0)
end
do
entity_list = evo.multi_clone(-1, prefab)
assert(entity_list and #entity_list == 0)
entity_list = evo.multi_clone(-1, prefab, {})
assert(entity_list and #entity_list == 0)
end
do
entity_list = evo.builder():multi_clone(0, prefab)
assert(entity_list and #entity_list == 0)
end
do
entity_list = evo.builder():multi_clone(-1, prefab)
assert(entity_list and #entity_list == 0)
end
end
do
local entity_list
local prefab = evo.id()
do
entity_list = evo.multi_clone(1, prefab)
assert(entity_list and #entity_list == 1)
assert(entity_list[1] and evo.empty(entity_list[1]))
assert(not entity_list[2])
entity_list = evo.multi_clone(1, prefab, {})
assert(entity_list and #entity_list == 1)
assert(entity_list[1] and evo.empty(entity_list[1]))
assert(not entity_list[2])
end
do
entity_list = evo.multi_clone(2, prefab)
assert(entity_list and #entity_list == 2)
assert(entity_list[1] and evo.empty(entity_list[1]))
assert(entity_list[2] and evo.empty(entity_list[2]))
assert(not entity_list[3])
entity_list = evo.multi_clone(2, prefab, {})
assert(entity_list and #entity_list == 2)
assert(entity_list[1] and evo.empty(entity_list[1]))
assert(entity_list[2] and evo.empty(entity_list[2]))
assert(not entity_list[3])
end
do
entity_list = evo.builder():multi_clone(1, prefab)
assert(entity_list and #entity_list == 1)
assert(entity_list[1] and evo.empty(entity_list[1]))
assert(not entity_list[2])
end
do
entity_list = evo.builder():multi_clone(2, prefab)
assert(entity_list and #entity_list == 2)
assert(entity_list[1] and evo.empty(entity_list[1]))
assert(entity_list[2] and evo.empty(entity_list[2]))
assert(not entity_list[3])
end
end
do
local f1, f2 = evo.id(2)
do
local entity_list
entity_list = evo.multi_spawn(2, { [f1] = true, [f2] = 123 })
assert(entity_list and #entity_list == 2)
assert(entity_list[1] and evo.get(entity_list[1], f1) == true and evo.get(entity_list[1], f2) == 123)
assert(entity_list[2] and evo.get(entity_list[2], f1) == true and evo.get(entity_list[2], f2) == 123)
entity_list = evo.multi_spawn(2, { [f1] = false, [f2] = 456 })
assert(entity_list and #entity_list == 2)
assert(entity_list[1] and evo.get(entity_list[1], f1) == false and evo.get(entity_list[1], f2) == 456)
assert(entity_list[2] and evo.get(entity_list[2], f1) == false and evo.get(entity_list[2], f2) == 456)
end
do
local prefab = evo.builder():set(f1, true):set(f2, 123):spawn()
local entity_list
entity_list = evo.multi_clone(2, prefab)
assert(entity_list and #entity_list == 2)
assert(entity_list[1] and evo.get(entity_list[1], f1) == true and evo.get(entity_list[1], f2) == 123)
assert(entity_list[2] and evo.get(entity_list[2], f1) == true and evo.get(entity_list[2], f2) == 123)
entity_list = evo.multi_clone(2, prefab, {})
assert(entity_list and #entity_list == 2)
assert(entity_list[1] and evo.get(entity_list[1], f1) == true and evo.get(entity_list[1], f2) == 123)
assert(entity_list[2] and evo.get(entity_list[2], f1) == true and evo.get(entity_list[2], f2) == 123)
entity_list = evo.multi_clone(2, prefab, { [f1] = false, [f2] = 456 })
assert(entity_list and #entity_list == 2)
assert(entity_list[1] and evo.get(entity_list[1], f1) == false and evo.get(entity_list[1], f2) == 456)
assert(entity_list[2] and evo.get(entity_list[2], f1) == false and evo.get(entity_list[2], f2) == 456)
end
end

View File

@@ -4053,11 +4053,52 @@ function __evolved_spawn(components)
return entity
end
---@param count integer
---@param entity_count integer
---@param components? table<evolved.fragment, evolved.component>
---@return evolved.entity[] entity_list
function __evolved_multi_spawn(count, components)
__error_fmt('not implemented yet')
function __evolved_multi_spawn(entity_count, components)
entity_count = entity_count or 1
if entity_count <= 0 then
return {}
end
if not components then
components = __safe_tbls.__EMPTY_COMPONENT_MAP
end
if __debug_mode then
for fragment in __lua_next, components do
if not __evolved_alive(fragment) then
__error_fmt('the fragment (%s) is not alive and cannot be used',
__id_name(fragment))
end
end
end
local entity_list = __lua_table_new(entity_count, 0)
for entity_index = 1, entity_count do
entity_list[entity_index] = __acquire_id()
end
if __defer_depth > 0 then
for entity_index = 1, entity_count do
local entity = entity_list[entity_index]
__defer_spawn_entity(entity, components)
end
else
__evolved_defer()
do
for entity_index = 1, entity_count do
local entity = entity_list[entity_index]
__spawn_entity(entity, components)
end
end
__evolved_commit()
end
return entity_list
end
---@param prefab evolved.entity
@@ -4097,12 +4138,58 @@ function __evolved_clone(prefab, components)
return entity
end
---@param count integer
---@param entity_count integer
---@param prefab evolved.entity
---@param components? table<evolved.fragment, evolved.component>
---@return evolved.entity[] entity_list
function __evolved_multi_clone(count, prefab, components)
__error_fmt('not implemented yet')
function __evolved_multi_clone(entity_count, prefab, components)
entity_count = entity_count or 1
if entity_count <= 0 then
return {}
end
if not components then
components = __safe_tbls.__EMPTY_COMPONENT_MAP
end
if __debug_mode then
if not __evolved_alive(prefab) then
__error_fmt('the prefab (%s) is not alive and cannot be used',
__id_name(prefab))
end
for fragment in __lua_next, components do
if not __evolved_alive(fragment) then
__error_fmt('the fragment (%s) is not alive and cannot be used',
__id_name(fragment))
end
end
end
local entity_list = __lua_table_new(entity_count, 0)
for entity_index = 1, entity_count do
entity_list[entity_index] = __acquire_id()
end
if __defer_depth > 0 then
for entity_index = 1, entity_count do
local entity = entity_list[entity_index]
__defer_clone_entity(entity, prefab, components)
end
else
__evolved_defer()
do
for entity_index = 1, entity_count do
local entity = entity_list[entity_index]
__clone_entity(entity, prefab, components)
end
end
__evolved_commit()
end
return entity_list
end
---@param entity evolved.entity
@@ -5405,10 +5492,10 @@ function __builder_mt:spawn()
return __evolved_spawn(self.__components)
end
---@param count integer
---@param entity_count integer
---@return evolved.entity[] entity_list
function __builder_mt:multi_spawn(count)
__error_fmt('not implemented yet')
function __builder_mt:multi_spawn(entity_count)
return __evolved_multi_spawn(entity_count, self.__components)
end
---@param prefab evolved.entity
@@ -5417,11 +5504,11 @@ function __builder_mt:clone(prefab)
return __evolved_clone(prefab, self.__components)
end
---@param count integer
---@param entity_count integer
---@param prefab evolved.entity
---@return evolved.entity[] entity_list
function __builder_mt:multi_clone(count, prefab)
__error_fmt('not implemented yet')
function __builder_mt:multi_clone(entity_count, prefab)
return __evolved_multi_clone(entity_count, prefab, self.__components)
end
---@param fragment evolved.fragment