mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-25 01:20:04 +00:00
Fix column pointers invalidated by upvalues (#88)
* Initial commit * Add types to world:has()
This commit is contained in:
parent
76d1ff91c0
commit
0f8f9348f5
3 changed files with 180 additions and 159 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -53,7 +53,7 @@ WallyPatches
|
||||||
# Misc
|
# Misc
|
||||||
roblox.toml
|
roblox.toml
|
||||||
sourcemap.json
|
sourcemap.json
|
||||||
drafts/*.lua
|
drafts/
|
||||||
|
|
||||||
# Cached Vitepress (docs)
|
# Cached Vitepress (docs)
|
||||||
|
|
||||||
|
|
131
src/init.luau
131
src/init.luau
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
--!optimize 2
|
--!optimize 2
|
||||||
--!native
|
--!native
|
||||||
--!strict
|
--!strict
|
||||||
|
@ -17,6 +16,7 @@ type ArchetypeEdge = {
|
||||||
remove: Archetype,
|
remove: Archetype,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
type Archetype = {
|
type Archetype = {
|
||||||
id: number,
|
id: number,
|
||||||
edges: { [i53]: ArchetypeEdge },
|
edges: { [i53]: ArchetypeEdge },
|
||||||
|
@ -669,7 +669,8 @@ do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local world_has: () -> boolean
|
local world_has: (world: World, entityId: number, ...i53) -> boolean
|
||||||
|
|
||||||
do
|
do
|
||||||
function world_has(world, entity_id, ...)
|
function world_has(world, entity_id, ...)
|
||||||
local id = entity_id
|
local id = entity_id
|
||||||
|
@ -704,8 +705,8 @@ export type Query = typeof({
|
||||||
end,
|
end,
|
||||||
}) & {
|
}) & {
|
||||||
next: Item,
|
next: Item,
|
||||||
replace: (Query, ...any) -> (),
|
without: (Query) -> Query,
|
||||||
without: (Query) -> Query
|
replace: (Query, (...any) -> (...any)) -> ()
|
||||||
}
|
}
|
||||||
|
|
||||||
type CompatibleArchetype = { archetype: Archetype, indices: { number } }
|
type CompatibleArchetype = { archetype: Archetype, indices: { number } }
|
||||||
|
@ -730,29 +731,25 @@ do
|
||||||
|
|
||||||
setmetatable(EmptyQuery, EmptyQuery)
|
setmetatable(EmptyQuery, EmptyQuery)
|
||||||
|
|
||||||
local indices: { { number } }
|
|
||||||
local compatibleArchetypes: { Archetype }
|
|
||||||
local length
|
|
||||||
local components: { number }
|
|
||||||
local queryLength: number
|
|
||||||
local lastArchetype: number
|
local lastArchetype: number
|
||||||
local archetype: Archetype
|
local archetype: Archetype
|
||||||
|
|
||||||
local queryOutput: { any }
|
local queryOutput: { any }
|
||||||
|
local queryLength: number
|
||||||
local entities: {}
|
local entities: { number }
|
||||||
local i: number
|
local i: number
|
||||||
|
|
||||||
local function world_query_next()
|
local compatible_archetypes: { Archetype }
|
||||||
|
local column_indices: { { number} }
|
||||||
|
local ids: { number }
|
||||||
|
|
||||||
|
local function world_query_next(): any
|
||||||
local entityId = entities[i]
|
local entityId = entities[i]
|
||||||
while entityId == nil do
|
while entityId == nil do
|
||||||
lastArchetype += 1
|
lastArchetype += 1
|
||||||
archetype = compatibleArchetypes[lastArchetype]
|
archetype = compatible_archetypes[lastArchetype]
|
||||||
|
|
||||||
if not archetype then
|
if not archetype then
|
||||||
return
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
entities = archetype.entities
|
entities = archetype.entities
|
||||||
i = #entities
|
i = #entities
|
||||||
entityId = entities[i]
|
entityId = entities[i]
|
||||||
|
@ -762,7 +759,7 @@ do
|
||||||
i-=1
|
i-=1
|
||||||
|
|
||||||
local columns = archetype.columns
|
local columns = archetype.columns
|
||||||
local tr = indices[lastArchetype]
|
local tr = column_indices[lastArchetype]
|
||||||
|
|
||||||
if queryLength == 1 then
|
if queryLength == 1 then
|
||||||
return entityId, columns[tr[1]][row]
|
return entityId, columns[tr[1]][row]
|
||||||
|
@ -773,36 +770,56 @@ do
|
||||||
elseif queryLength == 4 then
|
elseif queryLength == 4 then
|
||||||
return entityId, columns[tr[1]][row], columns[tr[2]][row], columns[tr[3]][row], columns[tr[4]][row]
|
return entityId, columns[tr[1]][row], columns[tr[2]][row], columns[tr[3]][row], columns[tr[4]][row]
|
||||||
elseif queryLength == 5 then
|
elseif queryLength == 5 then
|
||||||
return entityId,columns[tr[1]][row], columns[tr[2]][row], columns[tr[3]][row], columns[tr[4]][row],
|
return entityId,
|
||||||
|
columns[tr[1]][row],
|
||||||
|
columns[tr[2]][row],
|
||||||
|
columns[tr[3]][row],
|
||||||
|
columns[tr[4]][row],
|
||||||
columns[tr[5]][row]
|
columns[tr[5]][row]
|
||||||
elseif queryLength == 6 then
|
elseif queryLength == 6 then
|
||||||
return entityId, columns[tr[1]][row], columns[tr[2]][row], columns[tr[3]][row], columns[tr[4]][row],
|
return entityId,
|
||||||
|
columns[tr[1]][row],
|
||||||
|
columns[tr[2]][row],
|
||||||
|
columns[tr[3]][row],
|
||||||
|
columns[tr[4]][row],
|
||||||
columns[tr[5]][row],
|
columns[tr[5]][row],
|
||||||
columns[tr[6]][row]
|
columns[tr[6]][row]
|
||||||
elseif queryLength == 7 then
|
elseif queryLength == 7 then
|
||||||
return entityId, columns[tr[1]][row], columns[tr[2]][row], columns[tr[3]][row], columns[tr[4]][row],
|
return entityId,
|
||||||
|
columns[tr[1]][row],
|
||||||
|
columns[tr[2]][row],
|
||||||
|
columns[tr[3]][row],
|
||||||
|
columns[tr[4]][row],
|
||||||
columns[tr[5]][row],
|
columns[tr[5]][row],
|
||||||
columns[tr[6]][row],
|
columns[tr[6]][row],
|
||||||
columns[tr[7]][row]
|
columns[tr[7]][row]
|
||||||
elseif queryLength == 8 then
|
elseif queryLength == 8 then
|
||||||
return entityId, columns[tr[1]][row], columns[tr[2]][row], columns[tr[3]][row], columns[tr[4]][row],
|
return entityId,
|
||||||
|
columns[tr[1]][row],
|
||||||
|
columns[tr[2]][row],
|
||||||
|
columns[tr[3]][row],
|
||||||
|
columns[tr[4]][row],
|
||||||
columns[tr[5]][row],
|
columns[tr[5]][row],
|
||||||
columns[tr[6]][row],
|
columns[tr[6]][row],
|
||||||
columns[tr[7]][row],
|
columns[tr[7]][row],
|
||||||
columns[tr[8]][row]
|
columns[tr[8]][row]
|
||||||
end
|
end
|
||||||
|
|
||||||
for i in components do
|
for j in ids do
|
||||||
queryOutput[i] = columns[tr[i]][row]
|
queryOutput[j] = columns[tr[j]][row]
|
||||||
end
|
end
|
||||||
|
|
||||||
return entityId, unpack(queryOutput, 1, queryLength)
|
return entityId, unpack(queryOutput, 1, queryLength)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function world_query_without(self, ...): Query
|
local function world_query_iter()
|
||||||
|
return world_query_next
|
||||||
|
end
|
||||||
|
|
||||||
|
local function world_query_without(self, ...)
|
||||||
local withoutComponents = { ... }
|
local withoutComponents = { ... }
|
||||||
for i = #compatibleArchetypes, 1, -1 do
|
for i = #compatible_archetypes, 1, -1 do
|
||||||
local archetype = compatibleArchetypes[i]
|
local archetype = compatible_archetypes[i]
|
||||||
local records = archetype.records
|
local records = archetype.records
|
||||||
local shouldRemove = false
|
local shouldRemove = false
|
||||||
|
|
||||||
|
@ -814,35 +831,29 @@ do
|
||||||
end
|
end
|
||||||
|
|
||||||
if shouldRemove then
|
if shouldRemove then
|
||||||
table.remove(compatibleArchetypes, i)
|
table.remove(compatible_archetypes, i)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if #compatibleArchetypes == 0 then
|
lastArchetype = 1
|
||||||
|
archetype = compatible_archetypes[lastArchetype]
|
||||||
|
|
||||||
|
if not archetype then
|
||||||
return EmptyQuery
|
return EmptyQuery
|
||||||
end
|
end
|
||||||
|
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
local function world_query_iter()
|
|
||||||
lastArchetype = 1
|
|
||||||
archetype = compatibleArchetypes[1]
|
|
||||||
entities = archetype.entities
|
|
||||||
i = #entities
|
|
||||||
|
|
||||||
return world_query_next
|
|
||||||
end
|
|
||||||
|
|
||||||
local function world_query_replace_values(row, columns, ...)
|
local function world_query_replace_values(row, columns, ...)
|
||||||
for i, column in columns do
|
for i, column in columns do
|
||||||
column[row] = select(i, ...)
|
column[row] = select(i, ...)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function world_query_replace(_, fn: any)
|
local function world_query_replace(_, fn: (...any) -> (...any))
|
||||||
for i, archetype in compatibleArchetypes do
|
for i, archetype in compatible_archetypes do
|
||||||
local tr = indices[i]
|
local tr = column_indices[i]
|
||||||
local columns = archetype.columns
|
local columns = archetype.columns
|
||||||
|
|
||||||
for row in archetype.entities do
|
for row in archetype.entities do
|
||||||
|
@ -881,28 +892,30 @@ do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function world_query(world: World, ...: number): Query
|
|
||||||
|
function world_query(world: World, ...: any): Query
|
||||||
-- breaking?
|
-- breaking?
|
||||||
if (...) == nil then
|
if (...) == nil then
|
||||||
error("Missing components")
|
error("Missing components")
|
||||||
end
|
end
|
||||||
|
|
||||||
indices = {}
|
local indices = {}
|
||||||
compatibleArchetypes = {}
|
local compatibleArchetypes = {}
|
||||||
length = 0
|
local length = 0
|
||||||
components = { ... }
|
|
||||||
|
local components = { ... } :: any
|
||||||
|
local archetypes = world.archetypes
|
||||||
|
|
||||||
local archetypes: { Archetype } = world.archetypes :: any
|
|
||||||
local firstArchetypeMap: ArchetypeMap
|
local firstArchetypeMap: ArchetypeMap
|
||||||
local componentIndex = world.componentIndex
|
local componentIndex = world.componentIndex
|
||||||
|
|
||||||
for _, componentId in components do
|
for _, componentId in components do
|
||||||
local map: ArchetypeMap = componentIndex[componentId] :: any
|
local map = componentIndex[componentId]
|
||||||
if not map then
|
if not map then
|
||||||
return EmptyQuery
|
return EmptyQuery
|
||||||
end
|
end
|
||||||
|
|
||||||
if (firstArchetypeMap :: any) == nil or firstArchetypeMap.size > map.size then
|
if firstArchetypeMap == nil or map.size < firstArchetypeMap.size then
|
||||||
firstArchetypeMap = map
|
firstArchetypeMap = map
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -911,7 +924,7 @@ do
|
||||||
local compatibleArchetype = archetypes[id]
|
local compatibleArchetype = archetypes[id]
|
||||||
local archetypeRecords = compatibleArchetype.records
|
local archetypeRecords = compatibleArchetype.records
|
||||||
|
|
||||||
local records: { number } = {}
|
local records = {}
|
||||||
local skip = false
|
local skip = false
|
||||||
|
|
||||||
for i, componentId in components do
|
for i, componentId in components do
|
||||||
|
@ -933,15 +946,19 @@ do
|
||||||
indices[length] = records
|
indices[length] = records
|
||||||
end
|
end
|
||||||
|
|
||||||
|
compatible_archetypes = compatibleArchetypes
|
||||||
|
column_indices = indices
|
||||||
|
ids = components
|
||||||
|
|
||||||
lastArchetype = 1
|
lastArchetype = 1
|
||||||
archetype = compatibleArchetypes[lastArchetype]
|
archetype = compatible_archetypes[lastArchetype]
|
||||||
|
|
||||||
if not archetype then
|
if not archetype then
|
||||||
return EmptyQuery
|
return EmptyQuery
|
||||||
end
|
end
|
||||||
|
|
||||||
queryOutput = {}
|
queryOutput = {}
|
||||||
queryLength = #components
|
queryLength = #ids
|
||||||
|
|
||||||
entities = archetype.entities
|
entities = archetype.entities
|
||||||
i = #entities
|
i = #entities
|
||||||
|
@ -951,9 +968,11 @@ do
|
||||||
next = world_query_next,
|
next = world_query_next,
|
||||||
without = world_query_without,
|
without = world_query_without,
|
||||||
replace = world_query_replace
|
replace = world_query_replace
|
||||||
}
|
} :: any
|
||||||
|
|
||||||
return setmetatable(it, it) :: any
|
setmetatable(it, it)
|
||||||
|
|
||||||
|
return it
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -997,11 +1016,13 @@ export type WorldShim = typeof(setmetatable(
|
||||||
--- Removes a component from the given entity
|
--- Removes a component from the given entity
|
||||||
remove: (WorldShim, id: Entity, component: Entity) -> (),
|
remove: (WorldShim, id: Entity, component: Entity) -> (),
|
||||||
--- Retrieves the value of up to 4 components. These values may be nil.
|
--- Retrieves the value of up to 4 components. These values may be nil.
|
||||||
get: (<A>(WorldShim, id: any, Entity<A>) -> A)
|
get: (<A>(WorldShim, id: Entity, Entity<A>) -> A)
|
||||||
& (<A, B>(WorldShim, id: Entity, Entity<A>, Entity<B>) -> (A, B))
|
& (<A, B>(WorldShim, id: Entity, Entity<A>, Entity<B>) -> (A, B))
|
||||||
& (<A, B, C>(WorldShim, id: Entity, Entity<A>, Entity<B>, Entity<C>) -> (A, B, C))
|
& (<A, B, C>(WorldShim, id: Entity, Entity<A>, Entity<B>, Entity<C>) -> (A, B, C))
|
||||||
& <A, B, C, D>(WorldShim, id: Entity, Entity<A>, Entity<B>, Entity<C>, Entity<D>) -> (A, B, C, D),
|
& <A, B, C, D>(WorldShim, id: Entity, Entity<A>, Entity<B>, Entity<C>, Entity<D>) -> (A, B, C, D),
|
||||||
|
|
||||||
|
has: (WorldShim, Entity, ...Entity) -> boolean,
|
||||||
|
|
||||||
--- Searches the world for entities that match a given query
|
--- Searches the world for entities that match a given query
|
||||||
query: (<A>(WorldShim, Entity<A>) -> QueryShim<A>)
|
query: (<A>(WorldShim, Entity<A>) -> QueryShim<A>)
|
||||||
& (<A, B>(WorldShim, Entity<A>, Entity<B>) -> QueryShim<A, B>)
|
& (<A, B>(WorldShim, Entity<A>, Entity<B>) -> QueryShim<A, B>)
|
||||||
|
|
|
@ -60,10 +60,9 @@ TEST("world", function()
|
||||||
world:clear(e)
|
world:clear(e)
|
||||||
CHECK(world:get(e, A) == nil)
|
CHECK(world:get(e, A) == nil)
|
||||||
CHECK(world:get(e, B) == nil)
|
CHECK(world:get(e, B) == nil)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
do CASE("iterator should not drain the query")
|
do CASE("should drain query while iterating")
|
||||||
local world = jecs.World.new() :: World
|
local world = jecs.World.new() :: World
|
||||||
local A = world:component()
|
local A = world:component()
|
||||||
local B = world:component()
|
local B = world:component()
|
||||||
|
@ -85,7 +84,8 @@ TEST("world", function()
|
||||||
for _ in q do
|
for _ in q do
|
||||||
j+=1
|
j+=1
|
||||||
end
|
end
|
||||||
CHECK(i == j)
|
CHECK(i == 2)
|
||||||
|
CHECK(j == 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
do CASE("should be able to get next results")
|
do CASE("should be able to get next results")
|
||||||
|
|
Loading…
Reference in a new issue