Support wildcards in records

This commit is contained in:
Ukendio 2024-05-12 16:55:06 +02:00
parent afdda32c85
commit 18381ff5e6

View file

@ -236,22 +236,14 @@ local function hash(arr): string | number
return table.concat(arr, "_") return table.concat(arr, "_")
end end
local function createArchetypeRecords(componentIndex: ComponentIndex, to: Archetype, _from: Archetype?) local function createArchetypeRecord(componentIndex, id, componentId, i)
local destinationIds = to.types local archetypesMap = componentIndex[componentId]
local records = to.records
local id = to.id
for i, destinationId in destinationIds do if not archetypesMap then
local archetypesMap = componentIndex[destinationId] archetypesMap = {size = 0, sparse = {}}
componentIndex[componentId] = archetypesMap
if not archetypesMap then
archetypesMap = {size = 0, sparse = {}}
componentIndex[destinationId] = archetypesMap
end
archetypesMap.sparse[id] = i
records[destinationId] = i
end end
archetypesMap.sparse[id] = i
end end
local function archetypeOf(world: World, types: {i24}, prev: Archetype?): Archetype local function archetypeOf(world: World, types: {i24}, prev: Archetype?): Archetype
@ -261,10 +253,26 @@ local function archetypeOf(world: World, types: {i24}, prev: Archetype?): Archet
world.nextArchetypeId = id world.nextArchetypeId = id
local length = #types local length = #types
local columns = {} local columns = table.create(length)
for index, componentId in types do local records = {}
table.insert(columns, {}) local componentIndex = world.componentIndex
local entityIndex = world.entityIndex
for i, componentId in types do
createArchetypeRecord(componentIndex, id, componentId, i)
records[componentId] = i
columns[i] = {}
if ECS_IS_PAIR(componentId) then
local first = ecs_get_source(entityIndex, componentId)
local second = ecs_get_target(entityIndex, componentId)
local firstPair = ECS_PAIR(first, WILDCARD)
local secondPair = ECS_PAIR(WILDCARD, second)
createArchetypeRecord(componentIndex, id, firstPair, i)
createArchetypeRecord(componentIndex, id, secondPair, i)
records[firstPair] = i
records[secondPair] = i
end
end end
local archetype = { local archetype = {
@ -272,15 +280,12 @@ local function archetypeOf(world: World, types: {i24}, prev: Archetype?): Archet
edges = {}; edges = {};
entities = {}; entities = {};
id = id; id = id;
records = {}; records = records;
type = ty; type = ty;
types = types; types = types;
} }
world.archetypeIndex[ty] = archetype world.archetypeIndex[ty] = archetype
world.archetypes[id] = archetype world.archetypes[id] = archetype
if length > 0 then
createArchetypeRecords(world.componentIndex, archetype, prev)
end
return archetype return archetype
end end
@ -404,7 +409,6 @@ local function findInsert(types: {i53}, toAdd: i53)
end end
local function findArchetypeWith(world: World, node: Archetype, componentId: i53) local function findArchetypeWith(world: World, node: Archetype, componentId: i53)
local entityIndex = world.entityIndex
local types = node.types local types = node.types
-- Component IDs are added incrementally, so inserting and sorting -- Component IDs are added incrementally, so inserting and sorting
-- them each time would be expensive. Instead this insertion sort can find the insertion -- them each time would be expensive. Instead this insertion sort can find the insertion
@ -418,22 +422,6 @@ local function findArchetypeWith(world: World, node: Archetype, componentId: i53
return node return node
end end
table.insert(destinationType, at, componentId) table.insert(destinationType, at, componentId)
if ECS_IS_PAIR(componentId) then
local source = ECS_PAIR(
ecs_get_source(entityIndex, componentId), WILDCARD)
local sourceAt = findInsert(destinationType, source)
if sourceAt ~= -1 then
table.insert(destinationType, sourceAt, source)
end
local target = ECS_PAIR(
WILDCARD, ecs_get_target(entityIndex, componentId))
local targetAt = findInsert(destinationType, target)
if targetAt ~= -1 then
table.insert(destinationType, targetAt, target)
end
end
return ensureArchetype(world, destinationType, node) return ensureArchetype(world, destinationType, node)
end end