jecs.luau Coverage

Total Execution Hits: 1006447

Function Coverage Overview: 84.54%

Function Coverage:

FunctionHits
1
ECS_COMBINE:14265683
ECS_IS_PAIR:1471729
ECS_GENERATION_INC:15165684
ECS_ENTITY_T_LO:166162390
ECS_GENERATION:1705
ECS_ENTITY_T_HI:1740
ECS_PAIR:178133381
ECS_PAIR_FIRST:185648
ECS_PAIR_SECOND:189722
entity_index_try_get_any:193133207
entity_index_try_get:20666607
entity_index_try_get_fast:22025535
entity_index_is_alive:230922
entity_index_get_alive:234842
ecs_get_alive:2429
entity_index_new_id:26586562
ecs_pair_first:2864
ecs_pair_second:2915
query_match:29610
find_observers:3191518
archetype_move:3281822
archetype_append:38822380
new_entity:39820558
entity_move:4091822
hash:423569
fetch:4271809
world_get:438734
world_has_one_inline:469892
world_has:48580
world_target:5071440
ECS_ID_IS_WILDCARD:5390
id_record_ensure:5451509
archetype_append_to_records:6081507
archetype_create:633635
world_entity:69866852
world_parent:7021
archetype_ensure:706580
find_insert:720551
find_archetype_with:732551
find_archetype_without:75033
archetype_init_edge:767582
archetype_ensure_edge:77822971
init_edge_for_add:792549
init_edge_for_remove:80933
create_edge_for_add:832551
create_edge_for_remove:84333
archetype_traverse_add:85422092
archetype_traverse_remove:870297
world_add:88719316
world_set:9192775
world_component:971121
world_remove:983298
archetype_fast_delete_last:100876
archetype_fast_delete:101658
archetype_delete:1025134
world_clear:10646
archetype_disconnect_edge:116450
archetype_remove_edge:117523
archetype_clear_edges:118036
archetype_destroy:121337
world_cleanup:12491
world_delete:127065684
world_contains:1446145
NOOP:14500
query_iter_init:146126
world_query_iter_next:1527556
world_query_iter_next:15537
world_query_iter_next:15800
world_query_iter_next:16082
world_query_iter_next:16370
world_query_iter_next:16670
world_query_iter_next:16980
world_query_iter_next:17300
world_query_iter_next:17642
query_iter:179820
query_without:18066
query_with:18361
query_archetypes:18705
query_cached:18746
on_create_callback:19244
on_delete_callback:19281
cached_query_iter:194111
world_query_iter_next:200111
world_query_iter_next:20278
world_query_iter_next:20540
world_query_iter_next:20820
world_query_iter_next:21110
world_query_iter_next:21410
world_query_iter_next:21720
world_query_iter_next:22040
world_query_iter_next:22380
world_query:229533
world_each:23554
:237212
world_children:23892
world_new:243973

Source Code:

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
LineHitsCode
1N/A--!optimize 2
2N/A--!native
3N/A--!strict
4N/A--draft 4
5N/A
60type i53 = number
70type i24 = number
8N/A
90type Ty = { i53 }
100type ArchetypeId = number
11N/A
120type Column = { any }
13N/A
140type Map = { [K]: V }
15N/A
160type ecs_graph_edge_t = {
170from: ecs_archetype_t,
180to: ecs_archetype_t?,
190id: number,
200prev: ecs_graph_edge_t?,
210next: ecs_graph_edge_t?,
220}
23N/A
240type ecs_graph_edges_t = Map
25N/A
260type ecs_graph_node_t = {
270add: ecs_graph_edges_t,
280remove: ecs_graph_edges_t,
290refs: ecs_graph_edge_t,
300}
31N/A
320type ecs_archetype_t = {
330id: number,
340types: Ty,
350type: string,
360entities: { number },
370columns: { Column },
380records: { [i53]: number },
390counts: { [i53]: number },
400} & ecs_graph_node_t
41N/A
420export type Archetype = {
430id: number,
440types: Ty,
450type: string,
460entities: { number },
470columns: { Column },
480records: { [Id]: number },
490counts: { [Id]: number },
500}
51N/A
520type ecs_record_t = {
530archetype: ecs_archetype_t,
540row: number,
550dense: i24,
560}
57N/A
580type ecs_id_record_t = {
590cache: { number },
600counts: { number },
610flags: number,
620size: number,
630hooks: {
640on_add: ((entity: i53) -> ())?,
650on_set: ((entity: i53, data: any) -> ())?,
660on_remove: ((entity: i53) -> ())?,
670},
680}
69N/A
700type ecs_id_index_t = Map
71N/A
720type ecs_archetypes_map_t = { [string]: ecs_archetype_t }
73N/A
740type ecs_archetypes_t = { ecs_archetype_t }
75N/A
760type ecs_entity_index_t = {
770dense_array: Map,
780sparse_array: Map,
790alive_count: number,
800max_id: number,
810}
82N/A
830type ecs_query_data_t = {
840compatible_archetypes: { ecs_archetype_t },
850ids: { i53 },
860filter_with: { i53 },
870filter_without: { i53 },
880next: () -> (number, ...any),
890world: ecs_world_t,
900}
91N/A
920type ecs_observer_t = {
930callback: (archetype: ecs_archetype_t) -> (),
940query: ecs_query_data_t,
950}
96N/A
970type ecs_observable_t = Map>
98N/A
990type ecs_world_t = {
1000entity_index: ecs_entity_index_t,
1010component_index: ecs_id_index_t,
1020archetypes: ecs_archetypes_t,
1030archetype_index: ecs_archetypes_map_t,
1040max_archetype_id: number,
1050max_component_id: number,
1060ROOT_ARCHETYPE: ecs_archetype_t,
1070observable: Map>,
1081}
109N/A
1101local HI_COMPONENT_ID = _G.__JECS_HI_COMPONENT_ID or 256
111N/A-- stylua: ignore start
1121local EcsOnAdd = HI_COMPONENT_ID + 1
1131local EcsOnRemove = HI_COMPONENT_ID + 2
1141local EcsOnSet = HI_COMPONENT_ID + 3
1151local EcsWildcard = HI_COMPONENT_ID + 4
1161local EcsChildOf = HI_COMPONENT_ID + 5
1171local EcsComponent = HI_COMPONENT_ID + 6
1181local EcsOnDelete = HI_COMPONENT_ID + 7
1191local EcsOnDeleteTarget = HI_COMPONENT_ID + 8
1201local EcsDelete = HI_COMPONENT_ID + 9
1211local EcsRemove = HI_COMPONENT_ID + 10
1221local EcsName = HI_COMPONENT_ID + 11
1231local EcsOnArchetypeCreate = HI_COMPONENT_ID + 12
1240local EcsOnArchetypeDelete = HI_COMPONENT_ID + 13
1251local EcsRest = HI_COMPONENT_ID + 14
126N/A
1271local ECS_ID_DELETE = 0b0000_0001
1281local ECS_ID_IS_TAG = 0b0000_0010
1291local ECS_ID_HAS_ON_ADD = 0b0000_0100
1301local ECS_ID_HAS_ON_SET = 0b0000_1000
1310local ECS_ID_HAS_ON_REMOVE = 0b0001_0000
1321local ECS_ID_MASK = 0b0000_0000
133N/A
1340local ECS_ENTITY_MASK = bit32.lshift(1, 24)
1351local ECS_GENERATION_MASK = bit32.lshift(1, 16)
136N/A
1370local NULL_ARRAY = table.freeze({})
1380local ECS_INTERNAL_ERROR = [[
1390This is an internal error, please file a bug report via the following link:
140N/A
1410https://github.com/Ukendio/jecs/issues/new?template=BUG-REPORT.md
1421]]
143N/A
1440local function ECS_COMBINE(id: number, generation: number): i53
1451return id + (generation * ECS_ENTITY_MASK)
146N/Aend
1471local ECS_PAIR_OFFSET = 2^48
148N/A
1490local function ECS_IS_PAIR(e: number): boolean
1500return e > ECS_PAIR_OFFSET
151N/Aend
152N/A
15365540local function ECS_GENERATION_INC(e: i53): i53
15465540if e > ECS_ENTITY_MASK then
1550local id = e % ECS_ENTITY_MASK
15665540local generation = e // ECS_ENTITY_MASK
157N/A
1581local next_gen = generation + 1
1590if next_gen >= ECS_GENERATION_MASK then
1600return id
161N/Aend
162N/A
163144return ECS_COMBINE(id, next_gen)
164N/Aend
1650return ECS_COMBINE(e, 1)
166N/Aend
167N/A
1680local function ECS_ENTITY_T_LO(e: i53): i24
1690return e % ECS_ENTITY_MASK
170N/Aend
171N/A
1720local function ECS_GENERATION(e: i53)
1730return e // ECS_ENTITY_MASK
174N/Aend
175N/A
1760local function ECS_ENTITY_T_HI(e: i53): i24
1770return e // ECS_ENTITY_MASK
178N/Aend
179N/A
180133381local function ECS_PAIR(pred: i53, obj: i53): i53
1810pred %= ECS_ENTITY_MASK
182133381obj %= ECS_ENTITY_MASK
183N/A
1840return obj + (pred * ECS_ENTITY_MASK) + ECS_PAIR_OFFSET
185N/Aend
186N/A
1870local function ECS_PAIR_FIRST(e: i53): i24
1880return (e - ECS_PAIR_OFFSET) // ECS_ENTITY_MASK
189N/Aend
190N/A
1910local function ECS_PAIR_SECOND(e: i53): i24
1920return (e - ECS_PAIR_OFFSET) % ECS_ENTITY_MASK
193N/Aend
194N/A
1950local function entity_index_try_get_any(
1960entity_index: ecs_entity_index_t,
197133207entity: number
1980): ecs_record_t?
199133207local r = entity_index.sparse_array[ECS_ENTITY_T_LO(entity)]
200N/A
2010if not r or r.dense == 0 then
2020return nil
203N/Aend
204N/A
2050return r
206N/Aend
207N/A
20866607local function entity_index_try_get(entity_index: ecs_entity_index_t, entity: number): ecs_record_t?
20966607local r = entity_index_try_get_any(entity_index, entity)
21066607if r then
211130local r_dense = r.dense
2120if r_dense > entity_index.alive_count then
21366477return nil
214N/Aend
2150if entity_index.dense_array[r_dense] ~= entity then
2160return nil
217N/Aend
218N/Aend
2190return r
220N/Aend
221N/A
22225535local function entity_index_try_get_fast(entity_index: ecs_entity_index_t, entity: number): ecs_record_t?
22325243local r = entity_index.sparse_array[ECS_ENTITY_T_LO(entity)]
22424if r then
2250if entity_index.dense_array[r.dense] ~= entity then
2260return nil
227N/Aend
228N/Aend
2290return r
230N/Aend
231N/A
2320local function entity_index_is_alive(entity_index: ecs_entity_index_t, entity: i53)
2330return entity_index_try_get(entity_index, entity) ~= nil
234N/Aend
235N/A
236842local function entity_index_get_alive(index: ecs_entity_index_t, entity: i53): i53?
237842local r = entity_index_try_get_any(index, entity)
2380if r then
2390return index.dense_array[r.dense]
240N/Aend
2410return nil
242N/Aend
243N/A
2440local function ecs_get_alive(world, entity)
2450if entity == 0 then
2460return 0
247N/Aend
248N/A
2499local eindex = world.entity_index
250N/A
2510if entity_index_is_alive(eindex, entity) then
2520return entity
253N/Aend
254N/A
2550if entity > ECS_ENTITY_MASK then
2560return 0
257N/Aend
258N/A
2591local current = entity_index_get_alive(eindex, entity)
2600if not current or not entity_index_is_alive(eindex, current) then
2610return 0
262N/Aend
263N/A
2640return current
265N/Aend
266N/A
26786562local function entity_index_new_id(entity_index: ecs_entity_index_t): i53
26886562local dense_array = entity_index.dense_array
26986562local alive_count = entity_index.alive_count
27065559local max_id = entity_index.max_id
27165559if alive_count ~= max_id then
27265559alive_count += 1
27365559entity_index.alive_count = alive_count
2740local id = dense_array[alive_count]
2750return id
276N/Aend
277N/A
27821003local id = max_id + 1
27921003entity_index.max_id = id
28021003alive_count += 1
28121003entity_index.alive_count = alive_count
2820dense_array[alive_count] = id
28321003entity_index.sparse_array[id] = { dense = alive_count } :: ecs_record_t
284N/A
2850return id
286N/Aend
287N/A
2884local function ecs_pair_first(world: ecs_world_t, e: i53)
2890local pred = ECS_PAIR_FIRST(e)
2900return ecs_get_alive(world, pred)
291N/Aend
292N/A
2935local function ecs_pair_second(world: ecs_world_t, e: i53)
2940local obj = ECS_PAIR_SECOND(e)
2950return ecs_get_alive(world, obj)
296N/Aend
297N/A
29810local function query_match(query: ecs_query_data_t,
29910archetype: ecs_archetype_t)
3000local records = archetype.records
30110local with = query.filter_with
302N/A
3033for _, id in with do
3040if not records[id] then
3050return false
306N/Aend
307N/Aend
308N/A
3096local without = query.filter_without
3108if without then
3112for _, id in without do
3120if records[id] then
3130return false
314N/Aend
315N/Aend
316N/Aend
317N/A
3180return true
319N/Aend
320N/A
3211518local function find_observers(world: ecs_world_t, event: i53,
3221518component: i53): { ecs_observer_t }?
3231499local cache = world.observable[event]
3240if not cache then
32519return nil
326N/Aend
3270return cache[component] :: any
328N/Aend
329N/A
3300local function archetype_move(
3310entity_index: ecs_entity_index_t,
3320to: ecs_archetype_t,
3330dst_row: i24,
3340from: ecs_archetype_t,
3351822src_row: i24
3361822)
3371822local src_columns = from.columns
3381822local dst_columns = to.columns
3390local dst_entities = to.entities
3401822local src_entities = from.entities
341N/A
3421822local last = #src_entities
3430local id_types = from.types
3441822local records = to.records
345N/A
3460for i, column in src_columns do
3470if column == NULL_ARRAY then
3480continue
349N/Aend
350N/A-- Retrieves the new column index from the source archetype's record from each component
351N/A-- We have to do this because the columns are tightly packed and indexes may not correspond to each other.
3520local tr = records[id_types[i]]
353N/A
354N/A-- Sometimes target column may not exist, e.g. when you remove a component.
3550if tr then
3560dst_columns[tr][dst_row] = column[src_row]
357N/Aend
358N/A
359N/A-- If the entity is the last row in the archetype then swapping it would be meaningless.
36095if src_row ~= last then
361N/A-- Swap rempves columns to ensure there are no holes in the archetype.
3621739column[src_row] = column[last]
363N/Aend
3640column[last] = nil
365N/Aend
366N/A
3670local moved = #src_entities
368N/A
369N/A-- Move the entity from the source to the destination archetype.
370N/A-- Because we have swapped columns we now have to update the records
371N/A-- corresponding to the entities' rows that were swapped.
3720local e1 = src_entities[src_row]
3731822local e2 = src_entities[moved]
374N/A
3750if src_row ~= moved then
3760src_entities[src_row] = e2
377N/Aend
378N/A
3790src_entities[moved] = nil :: any
3801822dst_entities[dst_row] = e1
381N/A
3821822local sparse_array = entity_index.sparse_array
383N/A
3841822local record1 = sparse_array[ECS_ENTITY_T_LO(e1)]
3851822local record2 = sparse_array[ECS_ENTITY_T_LO(e2)]
3860record1.row = dst_row
3870record2.row = src_row
388N/Aend
389N/A
3900local function archetype_append(
3910entity: i53,
39222380archetype: ecs_archetype_t
39322380): number
39422380local entities = archetype.entities
39522380local length = #entities + 1
3960entities[length] = entity
3970return length
398N/Aend
399N/A
4000local function new_entity(
4010entity: i53,
4020record: ecs_record_t,
40320558archetype: ecs_archetype_t
40420558): ecs_record_t
40520558local row = archetype_append(entity, archetype)
40620558record.archetype = archetype
4070record.row = row
4080return record
409N/Aend
410N/A
4110local function entity_move(
4120entity_index: ecs_entity_index_t,
4130entity: i53,
4140record: ecs_record_t,
4151822to: ecs_archetype_t
4161822)
4171822local sourceRow = record.row
4181822local from = record.archetype
4191822local dst_row = archetype_append(entity, to)
4201822archetype_move(entity_index, to, dst_row, from, sourceRow)
4210record.archetype = to
4220record.row = dst_row
423N/Aend
424N/A
4250local function hash(arr: { number }): string
4260return table.concat(arr, "_")
427N/Aend
428N/A
4291809local function fetch(id: i53, records: { number },
4300columns: { Column }, row: number): any
4311809local tr = records[id]
432N/A
4330if not tr then
4340return nil
435N/Aend
436N/A
4370return columns[tr][row]
438N/Aend
439N/A
440734local function world_get(world: ecs_world_t, entity: i53,
441734a: i53, b: i53?, c: i53?, d: i53?, e: i53?): ...any
44275local record = entity_index_try_get_fast(world.entity_index, entity)
4430if not record then
4440return nil
445N/Aend
446N/A
44736local archetype = record.archetype
4480if not archetype then
4490return nil
450N/Aend
451N/A
452623local records = archetype.records
4530local columns = archetype.columns
454623local row = record.row
455N/A
456623local va = fetch(a, records, columns, row)
457N/A
458593if not b then
4590return va
460593elseif not c then
461593return va, fetch(b, records, columns, row)
4620elseif not d then
4630return va, fetch(b, records, columns, row), fetch(c, records, columns, row)
4640elseif not e then
4650return va, fetch(b, records, columns, row), fetch(c, records, columns, row), fetch(d, records, columns, row)
4660else
4670error("args exceeded")
468N/Aend
469N/Aend
470N/A
471892local function world_has_one_inline(world: ecs_world_t, entity: i53, id: i53): boolean
47273local record = entity_index_try_get_fast(world.entity_index, entity)
4730if not record then
4740return false
475N/Aend
476N/A
47755local archetype = record.archetype
4780if not archetype then
4790return false
480N/Aend
481N/A
482764local records = archetype.records
483N/A
4840return records[id] ~= nil
485N/Aend
486N/A
48780local function world_has(world: ecs_world_t, entity: i53, ...: i53): boolean
48822local record = entity_index_try_get_fast(world.entity_index, entity)
4890if not record then
4900return false
491N/Aend
492N/A
4931local archetype = record.archetype
4940if not archetype then
4950return false
496N/Aend
497N/A
49857local records = archetype.records
499N/A
50025for i = 1, select("#", ...) do
5010if not records[select(i, ...)] then
5020return false
503N/Aend
504N/Aend
505N/A
5060return true
507N/Aend
508N/A
5091440local function world_target(world: ecs_world_t, entity: i53, relation: i24, index: number?): i24?
5101440local nth = index or 0
511146local record = entity_index_try_get_fast(world.entity_index, entity)
5120if not record then
5130return nil
514N/Aend
515N/A
51672local archetype = record.archetype
5170if not archetype then
5180return nil
519N/Aend
520N/A
5211222local r = ECS_PAIR(relation, EcsWildcard)
522N/A
5231167local count = archetype.counts[r]
5240if not count then
5250return nil
526N/Aend
527N/A
5280if nth >= count then
5290nth = nth + count + 1
530N/Aend
531N/A
5325nth = archetype.types[nth + archetype.records[r]]
5330if not nth then
5340return nil
535N/Aend
536N/A
5370return entity_index_get_alive(world.entity_index,
5380ECS_PAIR_SECOND(nth))
539N/Aend
540N/A
5410local function ECS_ID_IS_WILDCARD(e: i53): boolean
5420local first = ECS_ENTITY_T_HI(e)
5430local second = ECS_ENTITY_T_LO(e)
5440return first == EcsWildcard or second == EcsWildcard
545N/Aend
546N/A
5471509local function id_record_ensure(world: ecs_world_t, id: number): ecs_id_record_t
5481509local component_index = world.component_index
5490local entity_index = world.entity_index
5501509local idr: ecs_id_record_t = component_index[id]
551N/A
552704if not idr then
553704local flags = ECS_ID_MASK
554704local relation = id
555704local target = 0
556384local is_pair = ECS_IS_PAIR(id)
557384if is_pair then
558384relation = entity_index_get_alive(entity_index, ECS_PAIR_FIRST(id)) :: i53
559383assert(relation and entity_index_is_alive(
560383entity_index, relation), ECS_INTERNAL_ERROR)
561383target = entity_index_get_alive(entity_index, ECS_PAIR_SECOND(id)) :: i53
5620assert(target and entity_index_is_alive(
5630entity_index, target), ECS_INTERNAL_ERROR)
564N/Aend
565N/A
5660local cleanup_policy = world_target(world, relation, EcsOnDelete, 0)
567702local cleanup_policy_target = world_target(world, relation, EcsOnDeleteTarget, 0)
568N/A
569702local has_delete = false
570N/A
5710if cleanup_policy == EcsDelete or cleanup_policy_target == EcsDelete then
5720has_delete = true
573N/Aend
574N/A
575702local on_add, on_set, on_remove = world_get(world, relation, EcsOnAdd, EcsOnSet, EcsOnRemove)
576N/A
577702local is_tag = not world_has_one_inline(world, relation, EcsComponent)
578N/A
5790if is_tag and is_pair then
5800is_tag = not world_has_one_inline(world, target, EcsComponent)
581N/Aend
582N/A
583702flags = bit32.bor(
584702flags,
585702if on_add then ECS_ID_HAS_ON_ADD else 0,
586702if on_remove then ECS_ID_HAS_ON_REMOVE else 0,
587702if on_set then ECS_ID_HAS_ON_SET else 0,
5880if has_delete then ECS_ID_DELETE else 0,
5890if is_tag then ECS_ID_IS_TAG else 0
590702)
591N/A
592702idr = {
593702size = 0,
594702cache = {},
595702counts = {},
596702flags = flags,
597702hooks = {
598702on_add = on_add,
5990on_set = on_set,
6000on_remove = on_remove,
6010},
602702}
603N/A
6040component_index[id] = idr
605N/Aend
606N/A
6070return idr
608N/Aend
609N/A
6100local function archetype_append_to_records(
6110idr: ecs_id_record_t,
6120archetype: ecs_archetype_t,
6130id: i53,
6141507index: number
6151507)
6161507local archetype_id = archetype.id
6171507local archetype_records = archetype.records
6181507local archetype_counts = archetype.counts
6191507local idr_columns = idr.cache
6201507local idr_counts = idr.counts
6211405local tr = idr_columns[archetype_id]
6221405if not tr then
6230idr_columns[archetype_id] = index
6241405idr_counts[archetype_id] = 1
625N/A
6260archetype_records[id] = index
627102archetype_counts[id] = 1
628102else
629102local max_count = idr_counts[archetype_id] + 1
6300idr_counts[archetype_id] = max_count
6310archetype_counts[id] = max_count
632N/Aend
633N/Aend
634N/A
635635local function archetype_create(world: ecs_world_t, id_types: { i24 }, ty, prev: i53?): ecs_archetype_t
6360local archetype_id = (world.max_archetype_id :: number) + 1
637635world.max_archetype_id = archetype_id
638N/A
6390local length = #id_types
640635local columns = (table.create(length) :: any) :: { Column }
641N/A
6420local records: { number } = {}
643635local counts: {number} = {}
644N/A
645635local archetype: ecs_archetype_t = {
646635columns = columns,
647635entities = {},
648635id = archetype_id,
649635records = records,
650635counts = counts,
6510type = ty,
652635types = id_types,
653N/A
654635add = {},
6550remove = {},
6560refs = {} :: ecs_graph_edge_t,
657635}
658N/A
659987for i, component_id in id_types do
6600local idr = id_record_ensure(world, component_id)
661987archetype_append_to_records(idr, archetype, component_id, i)
662N/A
663260if ECS_IS_PAIR(component_id) then
664260local relation = ECS_PAIR_FIRST(component_id)
665260local object = ECS_PAIR_SECOND(component_id)
666260local r = ECS_PAIR(relation, EcsWildcard)
6670local idr_r = id_record_ensure(world, r)
668260archetype_append_to_records(idr_r, archetype, r, i)
669N/A
670260local t = ECS_PAIR(EcsWildcard, object)
6710local idr_t = id_record_ensure(world, t)
6720archetype_append_to_records(idr_t, archetype, t, i)
673N/Aend
674N/A
6750if bit32.band(idr.flags, ECS_ID_IS_TAG) == 0 then
676393columns[i] = {}
6770else
6780columns[i] = NULL_ARRAY
679N/Aend
680N/Aend
681N/A
6821405for id in records do
6830local observer_list = find_observers(world, EcsOnArchetypeCreate, id)
6840if not observer_list then
6858continue
686N/Aend
6874for _, observer in observer_list do
6880if query_match(observer.query, archetype) then
6890observer.callback(archetype)
690N/Aend
691N/Aend
692N/Aend
693N/A
6940world.archetype_index[ty] = archetype
695633world.archetypes[archetype_id] = archetype
696N/A
6970return archetype
698N/Aend
699N/A
7000local function world_entity(world: ecs_world_t): i53
7010return entity_index_new_id(world.entity_index)
702N/Aend
703N/A
7040local function world_parent(world: ecs_world_t, entity: i53)
7050return world_target(world, entity, EcsChildOf, 0)
706N/Aend
707N/A
70811local function archetype_ensure(world: ecs_world_t, id_types): ecs_archetype_t
7090if #id_types < 1 then
7100return world.ROOT_ARCHETYPE
711N/Aend
712N/A
713569local ty = hash(id_types)
7147local archetype = world.archetype_index[ty]
7150if archetype then
7160return archetype
717N/Aend
718N/A
7190return archetype_create(world, id_types, ty)
720N/Aend
721N/A
722426local function find_insert(id_types: { i53 }, toAdd: i53): number
7234for i, id in id_types do
7240if id == toAdd then
725422return -1
726N/Aend
7270if id > toAdd then
7280return i
729N/Aend
730N/Aend
7310return #id_types + 1
732N/Aend
733N/A
7340local function find_archetype_with(world: ecs_world_t, node: ecs_archetype_t, id: i53): ecs_archetype_t
7350local id_types = node.types
736N/A-- Component IDs are added incrementally, so inserting and sorting
737N/A-- them each time would be expensive. Instead this insertion sort can find the insertion
738N/A-- point in the types array.
739N/A
740551local dst = table.clone(node.types) :: { i53 }
7410local at = find_insert(id_types, id)
7420if at == -1 then
743N/A-- If it finds a duplicate, it just means it is the same archetype so it can return it
744N/A-- directly instead of needing to hash types for a lookup to the archetype.
745547return node
746N/Aend
747547table.insert(dst, at, id)
748N/A
7490return archetype_ensure(world, dst)
750N/Aend
751N/A
7520local function find_archetype_without(
7530world: ecs_world_t,
7540node: ecs_archetype_t,
75533id: i53
75633): ecs_archetype_t
75733local id_types = node.types
7580local at = table.find(id_types, id)
7590if at == nil then
7600return node
761N/Aend
762N/A
7630local dst = table.clone(id_types)
76433table.remove(dst, at)
765N/A
7660return archetype_ensure(world, dst)
767N/Aend
768N/A
7690local function archetype_init_edge(
7700archetype: ecs_archetype_t,
7710edge: ecs_graph_edge_t,
7720id: i53,
773582to: ecs_archetype_t
774582)
775582edge.from = archetype
7760edge.to = to
7770edge.id = id
778N/Aend
779N/A
7800local function archetype_ensure_edge(
7810world: ecs_world_t,
7820edges: ecs_graph_edges_t,
78322971id: i53
78422971): ecs_graph_edge_t
785584local edge = edges[id]
786584if not edge then
7870edge = {} :: ecs_graph_edge_t
7880edges[id] = edge
789N/Aend
790N/A
7910return edge
792N/Aend
793N/A
794549local function init_edge_for_add(world, archetype: ecs_archetype_t, edge: ecs_graph_edge_t, id, to: ecs_archetype_t)
795549archetype_init_edge(archetype, edge, id, to)
796545archetype_ensure_edge(world, archetype.add, id)
797545if archetype ~= to then
7980local to_refs = to.refs
799545local next_edge = to_refs.next
800N/A
801545to_refs.next = edge
8020edge.prev = to_refs
803545edge.next = next_edge
804N/A
8050if next_edge then
8060next_edge.prev = edge
807N/Aend
808N/Aend
809N/Aend
810N/A
8110local function init_edge_for_remove(
8120world: ecs_world_t,
8130archetype: ecs_archetype_t,
8140edge: ecs_graph_edge_t,
8150id: number,
81633to: ecs_archetype_t
81733)
81833archetype_init_edge(archetype, edge, id, to)
81933archetype_ensure_edge(world, archetype.remove, id)
82033if archetype ~= to then
8210local to_refs = to.refs
82233local prev_edge = to_refs.prev
823N/A
82433to_refs.prev = edge
8250edge.next = to_refs
82633edge.prev = prev_edge
827N/A
8280if prev_edge then
8290prev_edge.next = edge
830N/Aend
831N/Aend
832N/Aend
833N/A
8340local function create_edge_for_add(
8350world: ecs_world_t,
8360node: ecs_archetype_t,
8370edge: ecs_graph_edge_t,
838551id: i53
839549): ecs_archetype_t
840549local to = find_archetype_with(world, node, id)
8410init_edge_for_add(world, node, edge, id, to)
8420return to
843N/Aend
844N/A
8450local function create_edge_for_remove(
8460world: ecs_world_t,
8470node: ecs_archetype_t,
8480edge: ecs_graph_edge_t,
84933id: i53
85033): ecs_archetype_t
85133local to = find_archetype_without(world, node, id)
8520init_edge_for_remove(world, node, edge, id, to)
8530return to
854N/Aend
855N/A
8560local function archetype_traverse_add(
8570world: ecs_world_t,
8580id: i53,
85922092from: ecs_archetype_t
86022092): ecs_archetype_t
8610from = from or world.ROOT_ARCHETYPE
86222092local edge = archetype_ensure_edge(world, from.add, id)
863N/A
864551local to = edge.to
8650if not to then
8660to = create_edge_for_add(world, from, edge, id)
867N/Aend
868N/A
8690return to :: ecs_archetype_t
870N/Aend
871N/A
8720local function archetype_traverse_remove(
8730world: ecs_world_t,
8740id: i53,
875297from: ecs_archetype_t
8760): ecs_archetype_t
877297from = from or world.ROOT_ARCHETYPE
878N/A
879297local edge = archetype_ensure_edge(world, from.remove, id)
880N/A
88133local to = edge.to
8820if not to then
8830to = create_edge_for_remove(world, from, edge, id)
884N/Aend
885N/A
8860return to :: ecs_archetype_t
887N/Aend
888N/A
8890local function world_add(
8900world: ecs_world_t,
8910entity: i53,
89219316id: i53
89319316): ()
89419316local entity_index = world.entity_index
8950local record = entity_index_try_get_fast(entity_index, entity)
8960if not record then
8970return
898N/Aend
899N/A
90019315local from = record.archetype
9013local to = archetype_traverse_add(world, id, from)
9020if from == to then
90319312return
904N/Aend
9050if from then
90619170entity_move(entity_index, entity, record, to)
90719170else
9080if #to.types > 0 then
9090new_entity(entity, record, to)
910N/Aend
911N/Aend
912N/A
9130local idr = world.component_index[id]
91419312local on_add = idr.hooks.on_add
915N/A
9160if on_add then
9170on_add(entity)
918N/Aend
919N/Aend
920N/A
9212775local function world_set(world: ecs_world_t, entity: i53, id: i53, data: unknown): ()
9222775local entity_index = world.entity_index
9230local record = entity_index_try_get_fast(entity_index, entity)
9240if not record then
9250return
926N/Aend
927N/A
9282774local from: ecs_archetype_t = record.archetype
9292774local to: ecs_archetype_t = archetype_traverse_add(world, id, from)
9300local idr = world.component_index[id]
9312774local idr_hooks = idr.hooks
932N/A
9330if from == to then
934N/A-- If the archetypes are the same it can avoid moving the entity
935N/A-- and just set the data directly.
9362local tr = to.records[id]
9372local column = from.columns[tr]
9382column[record.row] = data
9390local on_set = idr_hooks.on_set
9400if on_set then
9410on_set(entity, data)
942N/Aend
943N/A
9440return
945N/Aend
946N/A
9471384if from then
948N/A-- If there was a previous archetype, then the entity needs to move the archetype
9491388entity_move(entity_index, entity, record, to)
9500else
9511388if #to.types > 0 then
952N/A-- When there is no previous archetype it should create the archetype
9530new_entity(entity, record, to)
954N/Aend
955N/Aend
956N/A
9570local tr = to.records[id]
9582772local column = to.columns[tr]
959N/A
9602765column[record.row] = data
961N/A
9620local on_add = idr_hooks.on_add
9630if on_add then
9640on_add(entity)
965N/Aend
966N/A
9671local on_set = idr_hooks.on_set
9680if on_set then
9690on_set(entity, data)
970N/Aend
971N/Aend
972N/A
973121local function world_component(world: World): i53
9740local id = (world.max_component_id :: number) + 1
9750if id > HI_COMPONENT_ID then
976N/A-- IDs are partitioned into ranges because component IDs are not nominal,
977N/A-- so it needs to error when IDs intersect into the entity range.
978121error("Too many components, consider using world:entity() instead to create components.")
979N/Aend
980121world.max_component_id = id
981N/A
9820return id
983N/Aend
984N/A
985298local function world_remove(world: ecs_world_t, entity: i53, id: i53)
986298local entity_index = world.entity_index
9870local record = entity_index_try_get_fast(entity_index, entity)
9880if not record then
989298return
990N/Aend
991298local from = record.archetype
992N/A
9930if not from then
9940return
995N/Aend
996N/A
997296if from.records[id] then
998296local idr = world.component_index[id]
9993local on_remove = idr.hooks.on_remove
10000if on_remove then
10010on_remove(entity)
1002N/Aend
1003N/A
1004296local to = archetype_traverse_remove(world, id, record.archetype)
1005N/A
10060entity_move(entity_index, entity, record, to)
1007N/Aend
1008N/Aend
1009N/A
1010153local function archetype_fast_delete_last(columns: { Column }, column_count: number, types: { i53 }, entity: i53)
1011135for i, column in columns do
10120if column ~= NULL_ARRAY then
10130column[column_count] = nil
1014N/Aend
1015N/Aend
1016N/Aend
1017N/A
1018109local function archetype_fast_delete(columns: { Column }, column_count: number, row, types, entity)
1019103for i, column in columns do
1020103if column ~= NULL_ARRAY then
10210column[row] = column[column_count]
10220column[column_count] = nil
1023N/Aend
1024N/Aend
1025N/Aend
1026N/A
1027134local function archetype_delete(world: ecs_world_t, archetype: ecs_archetype_t, row: number)
1028134local entity_index = world.entity_index
1029134local component_index = world.component_index
1030134local columns = archetype.columns
1031134local id_types = archetype.types
1032134local entities = archetype.entities
1033134local column_count = #entities
10340local last = #entities
1035134local move = entities[last]
1036N/A-- We assume first that the entity is the last in the archetype
1037134local delete = move
1038N/A
103958if row ~= last then
104058local record_to_move = entity_index_try_get_any(entity_index, move)
10410if record_to_move then
10420record_to_move.row = row
1043N/Aend
1044N/A
10450delete = entities[row]
10460entities[row] = move
1047N/Aend
1048N/A
1049262for _, id in id_types do
1050262local idr = component_index[id]
10513local on_remove = idr.hooks.on_remove
10520if on_remove then
10530on_remove(delete)
1054N/Aend
1055N/Aend
1056N/A
1057134entities[last] = nil :: any
1058N/A
10590if row == last then
106058archetype_fast_delete_last(columns, column_count, id_types, delete)
10610else
10620archetype_fast_delete(columns, column_count, row, id_types, delete)
1063N/Aend
1064N/Aend
1065N/A
10666local function world_clear(world: ecs_world_t, entity: i53)
10676local entity_index = world.entity_index
10686local component_index = world.component_index
10696local archetypes = world.archetypes
10706local tgt = ECS_PAIR(EcsWildcard, entity)
10716local idr_t = component_index[tgt]
10726local idr = component_index[entity]
10730local rel = ECS_PAIR(entity, EcsWildcard)
10746local idr_r = component_index[rel]
1075N/A
10764if idr then
10774local count = 0
107811local queue = {}
107911for archetype_id in idr.cache do
108011local idr_archetype = archetypes[archetype_id]
108111local entities = idr_archetype.entities
108211local n = #entities
10830count += n
10844table.move(entities, 1, n, #queue + 1, queue)
1085N/Aend
10860for _, e in queue do
10870world_remove(world, e, entity)
1088N/Aend
1089N/Aend
1090N/A
10910if idr_t then
10920local queue
10930local ids
1094N/A
10950local count = 0
10960local archetype_ids = idr_t.cache
10970for archetype_id in archetype_ids do
10980local idr_t_archetype = archetypes[archetype_id]
10990local idr_t_types = idr_t_archetype.types
11000local entities = idr_t_archetype.entities
11010local removal_queued = false
1102N/A
11030for _, id in idr_t_types do
11040if not ECS_IS_PAIR(id) then
11050continue
1106N/Aend
11070local object = entity_index_get_alive(
11080entity_index, ECS_PAIR_SECOND(id))
11090if object ~= entity then
11100continue
1111N/Aend
11120if not ids then
11130ids = {}
1114N/Aend
11150ids[id] = true
11160removal_queued = true
1117N/Aend
1118N/A
11190if not removal_queued then
11200continue
1121N/Aend
1122N/A
11230if not queue then
11240queue = {}
1125N/Aend
1126N/A
11270local n = #entities
11280table.move(entities, 1, n, count + 1, queue)
11290count += n
1130N/Aend
1131N/A
11320for id in ids do
11330for _, child in queue do
11340world_remove(world, child, id)
1135N/Aend
1136N/Aend
1137N/Aend
1138N/A
11391if idr_r then
11401local count = 0
11411local archetype_ids = idr_r.cache
11421local ids = {}
11432local queue = {}
11442for archetype_id in archetype_ids do
11452local idr_r_archetype = archetypes[archetype_id]
11462local entities = idr_r_archetype.entities
11472local tr = idr_r_archetype.records[rel]
11482local tr_count = idr_r_archetype.counts[rel]
11492local types = idr_r_archetype.types
11500for i = tr, tr + tr_count - 1 do
11512ids[types[i]] = true
1152N/Aend
11532local n = #entities
11540table.move(entities, 1, n, count + 1, queue)
11550count += n
1156N/Aend
1157N/A
11583for _, e in queue do
11590for id in ids do
11600world_remove(world, e, id)
1161N/Aend
1162N/Aend
1163N/Aend
1164N/Aend
1165N/A
116650local function archetype_disconnect_edge(edge: ecs_graph_edge_t)
116750local edge_next = edge.next
116819local edge_prev = edge.prev
11690if edge_next then
117050edge_next.prev = edge_prev
1171N/Aend
11720if edge_prev then
11730edge_prev.next = edge_next
1174N/Aend
1175N/Aend
1176N/A
117723local function archetype_remove_edge(edges: ecs_graph_edges_t, id: i53, edge: ecs_graph_edge_t)
11780archetype_disconnect_edge(edge)
11790edges[id] = nil :: any
1180N/Aend
1181N/A
118236local function archetype_clear_edges(archetype: ecs_archetype_t)
118336local add: ecs_graph_edges_t = archetype.add
118436local remove: ecs_graph_edges_t = archetype.remove
11859local node_refs = archetype.refs
11869for id, edge in add do
11870archetype_disconnect_edge(edge)
118836add[id] = nil :: any
1189N/Aend
119018for id, edge in remove do
11910archetype_disconnect_edge(edge)
11920remove[id] = nil :: any
1193N/Aend
1194N/A
119522local cur = node_refs.next
119622while cur do
119722local edge = cur :: ecs_graph_edge_t
119822local next_edge = edge.next
11990archetype_remove_edge(edge.from.add, edge.id, edge)
12000cur = next_edge
1201N/Aend
1202N/A
12031cur = node_refs.prev
12041while cur do
12051local edge: ecs_graph_edge_t = cur
12061local next_edge = edge.prev
12070archetype_remove_edge(edge.from.remove, edge.id, edge)
12080cur = next_edge
1209N/Aend
1210N/A
12110node_refs.next = nil
12120node_refs.prev = nil
1213N/Aend
1214N/A
12151local function archetype_destroy(world: ecs_world_t, archetype: ecs_archetype_t)
12160if archetype == world.ROOT_ARCHETYPE then
12170return
1218N/Aend
1219N/A
122036local component_index = world.component_index
122136archetype_clear_edges(archetype)
122236local archetype_id = archetype.id
122336world.archetypes[archetype_id] = nil :: any
12240world.archetype_index[archetype.type] = nil :: any
122536local records = archetype.records
1226N/A
1227113for id in records do
12280local observer_list = find_observers(world, EcsOnArchetypeDelete, id)
12290if not observer_list then
12302continue
1231N/Aend
12321for _, observer in observer_list do
12330if query_match(observer.query, archetype) then
12340observer.callback(archetype)
1235N/Aend
1236N/Aend
1237N/Aend
1238N/A
1239113for id in records do
1240113local idr = component_index[id]
1241113idr.cache[archetype_id] = nil :: any
1242113idr.counts[archetype_id] = nil
1243113idr.size -= 1
12440records[id] = nil :: any
12450if idr.size == 0 then
12460component_index[id] = nil :: any
1247N/Aend
1248N/Aend
1249N/Aend
1250N/A
12510local function world_cleanup(world: ecs_world_t)
12521local archetypes = world.archetypes
1253N/A
12544for _, archetype in archetypes do
12550if #archetype.entities == 0 then
12560archetype_destroy(world, archetype)
1257N/Aend
1258N/Aend
1259N/A
12600local new_archetypes = table.create(#archetypes) :: { ecs_archetype_t }
12611local new_archetype_map = {}
1262N/A
12636for index, archetype in archetypes do
12640new_archetypes[index] = archetype
12650new_archetype_map[archetype.type] = archetype
1266N/Aend
1267N/A
12680world.archetypes = new_archetypes
12690world.archetype_index = new_archetype_map
1270N/Aend
1271N/A
127265684local function world_delete(world: ecs_world_t, entity: i53)
127365684local entity_index = world.entity_index
12741local record = entity_index_try_get(entity_index, entity)
12750if not record then
12760return
1277N/Aend
1278N/A
12790local archetype = record.archetype
128065683local row = record.row
1281N/A
12820if archetype then
1283N/A-- In the future should have a destruct mode for
1284N/A-- deleting archetypes themselves. Maybe requires recycling
12850archetype_delete(world, archetype, row)
1286N/Aend
1287N/A
128865683local delete = entity
128965683local component_index = world.component_index
129065683local archetypes = world.archetypes
12910local tgt = ECS_PAIR(EcsWildcard, delete)
129265683local rel = ECS_PAIR(delete, EcsWildcard)
1293N/A
129465683local idr_t = component_index[tgt]
12950local idr = component_index[delete]
129665683local idr_r = component_index[rel]
1297N/A
12988if idr then
12991local flags = idr.flags
13001if bit32.band(flags, ECS_ID_DELETE) ~= 0 then
13010for archetype_id in idr.cache do
13021local idr_archetype = archetypes[archetype_id]
1303N/A
13041local entities = idr_archetype.entities
13052local n = #entities
13060for i = n, 1, -1 do
13070world_delete(world, entities[i])
1308N/Aend
1309N/A
13100archetype_destroy(world, idr_archetype)
1311N/Aend
131212else
131312for archetype_id in idr.cache do
131412local idr_archetype = archetypes[archetype_id]
131512local entities = idr_archetype.entities
131610local n = #entities
13170for i = n, 1, -1 do
13180world_remove(world, entities[i], delete)
1319N/Aend
1320N/A
13210archetype_destroy(world, idr_archetype)
1322N/Aend
1323N/Aend
1324N/Aend
1325N/A
132613if idr_t then
13270local children
132813local ids
1329N/A
133013local count = 0
133118local archetype_ids = idr_t.cache
133218for archetype_id in archetype_ids do
133318local idr_t_archetype = archetypes[archetype_id]
133418local idr_t_types = idr_t_archetype.types
13350local entities = idr_t_archetype.entities
133618local removal_queued = false
1337N/A
13380for _, id in idr_t_types do
13390if not ECS_IS_PAIR(id) then
134024continue
1341N/Aend
134224local object = entity_index_get_alive(
13430entity_index, ECS_PAIR_SECOND(id))
13440if object ~= delete then
134520continue
1346N/Aend
134720local id_record = component_index[id]
134820local flags = id_record.flags
13498local flags_delete_mask: number = bit32.band(flags, ECS_ID_DELETE)
135015if flags_delete_mask ~= 0 then
135115for i = #entities, 1, -1 do
13520local child = entities[i]
13538world_delete(world, child)
1354N/Aend
135512break
13566else
13570if not ids then
135812ids = {}
1359N/Aend
13600ids[id] = true
13610removal_queued = true
1362N/Aend
1363N/Aend
1364N/A
13650if not removal_queued then
136610continue
1367N/Aend
13680if not children then
136910children = {}
1370N/Aend
137110local n = #entities
13720table.move(entities, 1, n, count + 1, children)
13730count += n
1374N/Aend
1375N/A
137617if ids then
137719for _, child in children do
13780for id in ids do
13790world_remove(world, child, id)
1380N/Aend
1381N/Aend
1382N/Aend
1383N/A
13840for archetype_id in archetype_ids do
13850archetype_destroy(world, archetypes[archetype_id])
1386N/Aend
1387N/Aend
1388N/A
13890if idr_r then
13900local archetype_ids = idr_r.cache
13910local flags = idr_r.flags
13920if bit32.band(flags, ECS_ID_DELETE) ~= 0 then
13930for archetype_id in archetype_ids do
13940local idr_r_archetype = archetypes[archetype_id]
13950local entities = idr_r_archetype.entities
13960local n = #entities
13970for i = n, 1, -1 do
13980world_delete(world, entities[i])
1399N/Aend
14000archetype_destroy(world, idr_r_archetype)
1401N/Aend
14020else
14030local children = {}
14040local count = 0
14050local ids = {}
14060for archetype_id in archetype_ids do
14070local idr_r_archetype = archetypes[archetype_id]
14080local entities = idr_r_archetype.entities
14090local tr = idr_r_archetype.records[rel]
14100local tr_count = idr_r_archetype.counts[rel]
14110local types = idr_r_archetype.types
14120for i = tr, tr_count - 1 do
14130ids[types[tr]] = true
1414N/Aend
14150local n = #entities
14160table.move(entities, 1, n, count + 1, children)
14170count += n
1418N/Aend
1419N/A
14200for _, child in children do
14210for id in ids do
14220world_remove(world, child, id)
1423N/Aend
1424N/Aend
1425N/A
14260for archetype_id in archetype_ids do
14270archetype_destroy(world, archetypes[archetype_id])
1428N/Aend
1429N/Aend
1430N/Aend
1431N/A
143265683local dense_array = entity_index.dense_array
143365683local index_of_deleted_entity = record.dense
14340local index_of_last_alive_entity = entity_index.alive_count
143565683entity_index.alive_count = index_of_last_alive_entity - 1
1436N/A
143765683local last_alive_entity = dense_array[index_of_last_alive_entity]
143865683local r_swap = entity_index_try_get_any(entity_index, last_alive_entity) :: ecs_record_t
143965683r_swap.dense = index_of_deleted_entity
144065683record.archetype = nil :: any
14410record.row = nil :: any
144265683record.dense = index_of_last_alive_entity
1443N/A
14440dense_array[index_of_deleted_entity] = last_alive_entity
14450dense_array[index_of_last_alive_entity] = ECS_GENERATION_INC(entity)
1446N/Aend
1447N/A
14480local function world_contains(world: ecs_world_t, entity): boolean
14490return entity_index_is_alive(world.entity_index, entity)
1450N/Aend
1451N/A
14520local function NOOP() end
1453N/A
14540export type QueryInner = {
14550compatible_archetypes: { Archetype },
14560ids: { i53 },
14570filter_with: { i53 },
14580filter_without: { i53 },
14590next: () -> (number, ...any),
14600world: World,
14611}
1462N/A
14630local function query_iter_init(query: ecs_query_data_t): () -> (number, ...any)
146426local world_query_iter_next
1465N/A
146626local compatible_archetypes = query.compatible_archetypes
146726local lastArchetype = 1
14684local archetype = compatible_archetypes[1]
14690if not archetype then
147022return NOOP :: () -> (number, ...any)
1471N/Aend
147222local columns = archetype.columns
147322local entities = archetype.entities
14740local i = #entities
147522local records = archetype.records
1476N/A
147722local ids = query.ids
147822local A, B, C, D, E, F, G, H, I = unpack(ids)
14790local a: Column, b: Column, c: Column, d: Column
148022local e: Column, f: Column, g: Column, h: Column
1481N/A
14825if not B then
14833a = columns[records[A]]
14843elseif not C then
14852a = columns[records[A]]
14860b = columns[records[B]]
14870elseif not D then
14880a = columns[records[A]]
14892b = columns[records[B]]
14901c = columns[records[C]]
14911elseif not E then
14921a = columns[records[A]]
14931b = columns[records[B]]
14941c = columns[records[C]]
14950d = columns[records[D]]
14960elseif not F then
14970a = columns[records[A]]
14980b = columns[records[B]]
14990c = columns[records[C]]
15001d = columns[records[D]]
15010e = columns[records[E]]
15020elseif not G then
15030a = columns[records[A]]
15040b = columns[records[B]]
15050c = columns[records[C]]
15060d = columns[records[D]]
15071e = columns[records[E]]
15080f = columns[records[F]]
15090elseif not H then
15100a = columns[records[A]]
15110b = columns[records[B]]
15120c = columns[records[C]]
15130d = columns[records[D]]
15140e = columns[records[E]]
15151f = columns[records[F]]
15160g = columns[records[G]]
15170elseif not I then
15180a = columns[records[A]]
15190b = columns[records[B]]
15200c = columns[records[C]]
15210d = columns[records[D]]
15220e = columns[records[E]]
15230f = columns[records[F]]
15240g = columns[records[G]]
15250h = columns[records[H]]
1526N/Aend
1527N/A
1528556if not B then
1529556function world_query_iter_next(): any
153026local entity = entities[i]
153126while entity == nil do
153226lastArchetype += 1
153318archetype = compatible_archetypes[lastArchetype]
15340if not archetype then
15350return nil
1536N/Aend
1537N/A
15388entities = archetype.entities
15390i = #entities
15400if i == 0 then
15418continue
1542N/Aend
15438entity = entities[i]
15448columns = archetype.columns
15450records = archetype.records
15460a = columns[records[A]]
1547N/Aend
1548N/A
15490local row = i
1550538i -= 1
1551N/A
15525return entity, a[row]
1553N/Aend
15547elseif not C then
15557function world_query_iter_next(): any
15563local entity = entities[i]
15573while entity == nil do
15583lastArchetype += 1
15593archetype = compatible_archetypes[lastArchetype]
15600if not archetype then
15610return nil
1562N/Aend
1563N/A
15640entities = archetype.entities
15650i = #entities
15660if i == 0 then
15670continue
1568N/Aend
15690entity = entities[i]
15700columns = archetype.columns
15710records = archetype.records
15720a = columns[records[A]]
15730b = columns[records[B]]
1574N/Aend
1575N/A
15760local row = i
15774i -= 1
1578N/A
15792return entity, a[row], b[row]
1580N/Aend
15810elseif not D then
15820function world_query_iter_next(): any
15830local entity = entities[i]
15840while entity == nil do
15850lastArchetype += 1
15860archetype = compatible_archetypes[lastArchetype]
15870if not archetype then
15880return nil
1589N/Aend
1590N/A
15910entities = archetype.entities
15920i = #entities
15930if i == 0 then
15940continue
1595N/Aend
15960entity = entities[i]
15970columns = archetype.columns
15980records = archetype.records
15990a = columns[records[A]]
16000b = columns[records[B]]
16010c = columns[records[C]]
1602N/Aend
1603N/A
16040local row = i
16050i -= 1
1606N/A
16072return entity, a[row], b[row], c[row]
1608N/Aend
16092elseif not E then
16102function world_query_iter_next(): any
16111local entity = entities[i]
16121while entity == nil do
16131lastArchetype += 1
16141archetype = compatible_archetypes[lastArchetype]
16150if not archetype then
16160return nil
1617N/Aend
1618N/A
16190entities = archetype.entities
16200i = #entities
16210if i == 0 then
16220continue
1623N/Aend
16240entity = entities[i]
16250columns = archetype.columns
16260records = archetype.records
16270a = columns[records[A]]
16280b = columns[records[B]]
16290c = columns[records[C]]
16300d = columns[records[D]]
1631N/Aend
1632N/A
16330local row = i
16341i -= 1
1635N/A
16361return entity, a[row], b[row], c[row], d[row]
1637N/Aend
16380elseif not F then
16390function world_query_iter_next(): any
16400local entity = entities[i]
16410while entity == nil do
16420lastArchetype += 1
16430archetype = compatible_archetypes[lastArchetype]
16440if not archetype then
16450return nil
1646N/Aend
1647N/A
16480entities = archetype.entities
16490i = #entities
16500if i == 0 then
16510continue
1652N/Aend
16530entity = entities[i]
16540columns = archetype.columns
16550records = archetype.records
16560a = columns[records[A]]
16570b = columns[records[B]]
16580c = columns[records[C]]
16590d = columns[records[D]]
16600e = columns[records[E]]
1661N/Aend
1662N/A
16630local row = i
16640i -= 1
1665N/A
16661return entity, a[row], b[row], c[row], d[row], e[row]
1667N/Aend
16680elseif not G then
16690function world_query_iter_next(): any
16700local entity = entities[i]
16710while entity == nil do
16720lastArchetype += 1
16730archetype = compatible_archetypes[lastArchetype]
16740if not archetype then
16750return nil
1676N/Aend
1677N/A
16780entities = archetype.entities
16790i = #entities
16800if i == 0 then
16810continue
1682N/Aend
16830entity = entities[i]
16840columns = archetype.columns
16850records = archetype.records
16860a = columns[records[A]]
16870b = columns[records[B]]
16880c = columns[records[C]]
16890d = columns[records[D]]
16900e = columns[records[E]]
16910f = columns[records[F]]
1692N/Aend
1693N/A
16940local row = i
16950i -= 1
1696N/A
16971return entity, a[row], b[row], c[row], d[row], e[row], f[row]
1698N/Aend
16990elseif not H then
17000function world_query_iter_next(): any
17010local entity = entities[i]
17020while entity == nil do
17030lastArchetype += 1
17040archetype = compatible_archetypes[lastArchetype]
17050if not archetype then
17060return nil
1707N/Aend
1708N/A
17090entities = archetype.entities
17100i = #entities
17110if i == 0 then
17120continue
1713N/Aend
17140entity = entities[i]
17150columns = archetype.columns
17160records = archetype.records
17170a = columns[records[A]]
17180b = columns[records[B]]
17190c = columns[records[C]]
17200d = columns[records[D]]
17210e = columns[records[E]]
17220f = columns[records[F]]
17230g = columns[records[G]]
1724N/Aend
1725N/A
17260local row = i
17270i -= 1
1728N/A
17291return entity, a[row], b[row], c[row], d[row], e[row], f[row], g[row]
1730N/Aend
17310elseif not I then
17320function world_query_iter_next(): any
17330local entity = entities[i]
17340while entity == nil do
17350lastArchetype += 1
17360archetype = compatible_archetypes[lastArchetype]
17370if not archetype then
17380return nil
1739N/Aend
1740N/A
17410entities = archetype.entities
17420i = #entities
17430if i == 0 then
17440continue
1745N/Aend
17460entity = entities[i]
17470columns = archetype.columns
17480records = archetype.records
17490a = columns[records[A]]
17500b = columns[records[B]]
17510c = columns[records[C]]
17520d = columns[records[D]]
17530e = columns[records[E]]
17540f = columns[records[F]]
17550g = columns[records[G]]
17560h = columns[records[H]]
1757N/Aend
1758N/A
17590local row = i
17600i -= 1
1761N/A
17620return entity, a[row], b[row], c[row], d[row], e[row], f[row], g[row], h[row]
1763N/Aend
17641else
17652local output = {}
17662function world_query_iter_next(): any
17671local entity = entities[i]
17681while entity == nil do
17691lastArchetype += 1
17701archetype = compatible_archetypes[lastArchetype]
17710if not archetype then
17720return nil
1773N/Aend
1774N/A
17750entities = archetype.entities
17760i = #entities
17770if i == 0 then
17780continue
1779N/Aend
17800entity = entities[i]
17810columns = archetype.columns
17820records = archetype.records
1783N/Aend
1784N/A
17850local row = i
17861i -= 1
1787N/A
17880for j, id in ids do
17890output[j] = columns[records[id]][row]
1790N/Aend
1791N/A
17920return entity, unpack(output)
1793N/Aend
1794N/Aend
1795N/A
17960query.next = world_query_iter_next
17970return world_query_iter_next
1798N/Aend
1799N/A
180020local function query_iter(query): () -> (number, ...any)
180119local query_next = query.next
18020if not query_next then
180320query_next = query_iter_init(query)
1804N/Aend
18050return query_next
1806N/Aend
1807N/A
18086local function query_without(query: ecs_query_data_t, ...: i53)
18096local without = { ... }
18106query.filter_without = without
18113local compatible_archetypes = query.compatible_archetypes
18123for i = #compatible_archetypes, 1, -1 do
18133local archetype = compatible_archetypes[i]
18140local records = archetype.records
18153local matches = true
1816N/A
18172for _, id in without do
18182if records[id] then
18190matches = false
18200break
1821N/Aend
1822N/Aend
1823N/A
18240if matches then
18250continue
1826N/Aend
1827N/A
18280local last = #compatible_archetypes
18290if last ~= i then
18302compatible_archetypes[i] = compatible_archetypes[last]
1831N/Aend
18320compatible_archetypes[last] = nil :: any
1833N/Aend
1834N/A
18350return query :: any
1836N/Aend
1837N/A
18381local function query_with(query: ecs_query_data_t, ...: i53)
18391local compatible_archetypes = query.compatible_archetypes
18400local with = { ... }
18411query.filter_with = with
1842N/A
18430for i = #compatible_archetypes, 1, -1 do
18440local archetype = compatible_archetypes[i]
18450local records = archetype.records
18460local matches = true
1847N/A
18480for _, id in with do
18490if not records[id] then
18500matches = false
18510break
1852N/Aend
1853N/Aend
1854N/A
18550if matches then
18560continue
1857N/Aend
1858N/A
18590local last = #compatible_archetypes
18600if last ~= i then
18610compatible_archetypes[i] = compatible_archetypes[last]
1862N/Aend
18630compatible_archetypes[last] = nil :: any
1864N/Aend
1865N/A
18660return query :: any
1867N/Aend
1868N/A
1869N/A-- Meant for directly iterating over archetypes to minimize
1870N/A-- function call overhead. Should not be used unless iterating over
1871N/A-- hundreds of thousands of entities in bulk.
18720local function query_archetypes(query)
18730return query.compatible_archetypes
1874N/Aend
1875N/A
18766local function query_cached(query: ecs_query_data_t)
18776local with = query.filter_with
18781local ids = query.ids
18790if with then
18805table.move(ids, 1, #ids, #with + 1, with)
18810else
18820query.filter_with = ids
1883N/Aend
1884N/A
18850local compatible_archetypes = query.compatible_archetypes
18866local lastArchetype = 1
1887N/A
18886local A, B, C, D, E, F, G, H, I = unpack(ids)
18890local a: Column, b: Column, c: Column, d: Column
18906local e: Column, f: Column, g: Column, h: Column
1891N/A
18926local world_query_iter_next
18936local columns: { Column }
18946local entities: { number }
18956local i: number
18966local archetype: ecs_archetype_t
18970local records: { number }
18986local archetypes = query.compatible_archetypes
1899N/A
19000local world = query.world :: { observable: ecs_observable_t }
1901N/A-- Only need one observer for EcsArchetypeCreate and EcsArchetypeDelete respectively
1902N/A-- because the event will be emitted for all components of that Archetype.
19036local observable = world.observable :: ecs_observable_t
19046local on_create_action = observable[EcsOnArchetypeCreate]
19056if not on_create_action then
19060on_create_action = {}
19076observable[EcsOnArchetypeCreate] = on_create_action
1908N/Aend
19096local query_cache_on_create = on_create_action[A]
19106if not query_cache_on_create then
19110query_cache_on_create = {}
19120on_create_action[A] = query_cache_on_create
1913N/Aend
1914N/A
19156local on_delete_action = observable[EcsOnArchetypeDelete]
19166if not on_delete_action then
19170on_delete_action = {}
19186observable[EcsOnArchetypeDelete] = on_delete_action
1919N/Aend
19206local query_cache_on_delete = on_delete_action[A]
19216if not query_cache_on_delete then
19220query_cache_on_delete = {}
19230on_delete_action[A] = query_cache_on_delete
1924N/Aend
1925N/A
19260local function on_create_callback(archetype)
19270table.insert(archetypes, archetype)
1928N/Aend
1929N/A
19301local function on_delete_callback(archetype)
19311local i = table.find(archetypes, archetype) :: number
19321local n = #archetypes
19330archetypes[i] = archetypes[n]
19340archetypes[n] = nil
1935N/Aend
1936N/A
19370local observer_for_create = { query = query, callback = on_create_callback }
19386local observer_for_delete = { query = query, callback = on_delete_callback }
1939N/A
19400table.insert(query_cache_on_create, observer_for_create)
19416table.insert(query_cache_on_delete, observer_for_delete)
1942N/A
194311local function cached_query_iter()
194411lastArchetype = 1
19451archetype = compatible_archetypes[lastArchetype]
19460if not archetype then
194710return NOOP
1948N/Aend
194910entities = archetype.entities
195010i = #entities
195110records = archetype.records
19526columns = archetype.columns
19534if not B then
19544a = columns[records[A]]
19554elseif not C then
19560a = columns[records[A]]
19570b = columns[records[B]]
19580elseif not D then
19590a = columns[records[A]]
19600b = columns[records[B]]
19610c = columns[records[C]]
19620elseif not E then
19630a = columns[records[A]]
19640b = columns[records[B]]
19650c = columns[records[C]]
19660d = columns[records[D]]
19670elseif not F then
19680a = columns[records[A]]
19690b = columns[records[B]]
19700c = columns[records[C]]
19710d = columns[records[D]]
19720e = columns[records[E]]
19730elseif not G then
19740a = columns[records[A]]
19750b = columns[records[B]]
19760c = columns[records[C]]
19770d = columns[records[D]]
19780e = columns[records[E]]
19790f = columns[records[F]]
19800elseif not H then
19810a = columns[records[A]]
19820b = columns[records[B]]
19830c = columns[records[C]]
19840d = columns[records[D]]
19850e = columns[records[E]]
19860f = columns[records[F]]
19870g = columns[records[G]]
19880elseif not I then
19890a = columns[records[A]]
19900b = columns[records[B]]
19910c = columns[records[C]]
19920d = columns[records[D]]
19930e = columns[records[E]]
19940f = columns[records[F]]
19950g = columns[records[G]]
19960h = columns[records[H]]
1997N/Aend
1998N/A
19990return world_query_iter_next
2000N/Aend
2001N/A
200211if not B then
200311function world_query_iter_next(): any
20046local entity = entities[i]
20056while entity == nil do
20066lastArchetype += 1
20076archetype = compatible_archetypes[lastArchetype]
20080if not archetype then
20090return nil
2010N/Aend
2011N/A
20120entities = archetype.entities
20130i = #entities
20140if i == 0 then
20150continue
2016N/Aend
20170entity = entities[i]
20180columns = archetype.columns
20190records = archetype.records
20200a = columns[records[A]]
2021N/Aend
2022N/A
20230local row = i
20245i -= 1
2025N/A
20261return entity, a[row]
2027N/Aend
20288elseif not C then
20298function world_query_iter_next(): any
20304local entity = entities[i]
20314while entity == nil do
20324lastArchetype += 1
20334archetype = compatible_archetypes[lastArchetype]
20340if not archetype then
20350return nil
2036N/Aend
2037N/A
20380entities = archetype.entities
20390i = #entities
20400if i == 0 then
20410continue
2042N/Aend
20430entity = entities[i]
20440columns = archetype.columns
20450records = archetype.records
20460a = columns[records[A]]
20470b = columns[records[B]]
2048N/Aend
2049N/A
20500local row = i
20514i -= 1
2052N/A
20530return entity, a[row], b[row]
2054N/Aend
20550elseif not D then
20560function world_query_iter_next(): any
20570local entity = entities[i]
20580while entity == nil do
20590lastArchetype += 1
20600archetype = compatible_archetypes[lastArchetype]
20610if not archetype then
20620return nil
2063N/Aend
2064N/A
20650entities = archetype.entities
20660i = #entities
20670if i == 0 then
20680continue
2069N/Aend
20700entity = entities[i]
20710columns = archetype.columns
20720records = archetype.records
20730a = columns[records[A]]
20740b = columns[records[B]]
20750c = columns[records[C]]
2076N/Aend
2077N/A
20780local row = i
20790i -= 1
2080N/A
20810return entity, a[row], b[row], c[row]
2082N/Aend
20830elseif not E then
20840function world_query_iter_next(): any
20850local entity = entities[i]
20860while entity == nil do
20870lastArchetype += 1
20880archetype = compatible_archetypes[lastArchetype]
20890if not archetype then
20900return nil
2091N/Aend
2092N/A
20930entities = archetype.entities
20940i = #entities
20950if i == 0 then
20960continue
2097N/Aend
20980entity = entities[i]
20990columns = archetype.columns
21000records = archetype.records
21010a = columns[records[A]]
21020b = columns[records[B]]
21030c = columns[records[C]]
21040d = columns[records[D]]
2105N/Aend
2106N/A
21070local row = i
21080i -= 1
2109N/A
21100return entity, a[row], b[row], c[row], d[row]
2111N/Aend
21120elseif not F then
21130function world_query_iter_next(): any
21140local entity = entities[i]
21150while entity == nil do
21160lastArchetype += 1
21170archetype = compatible_archetypes[lastArchetype]
21180if not archetype then
21190return nil
2120N/Aend
2121N/A
21220entities = archetype.entities
21230i = #entities
21240if i == 0 then
21250continue
2126N/Aend
21270entity = entities[i]
21280columns = archetype.columns
21290records = archetype.records
21300a = columns[records[A]]
21310b = columns[records[B]]
21320c = columns[records[C]]
21330d = columns[records[D]]
21340e = columns[records[E]]
2135N/Aend
2136N/A
21370local row = i
21380i -= 1
2139N/A
21400return entity, a[row], b[row], c[row], d[row], e[row]
2141N/Aend
21420elseif not G then
21430function world_query_iter_next(): any
21440local entity = entities[i]
21450while entity == nil do
21460lastArchetype += 1
21470archetype = compatible_archetypes[lastArchetype]
21480if not archetype then
21490return nil
2150N/Aend
2151N/A
21520entities = archetype.entities
21530i = #entities
21540if i == 0 then
21550continue
2156N/Aend
21570entity = entities[i]
21580columns = archetype.columns
21590records = archetype.records
21600a = columns[records[A]]
21610b = columns[records[B]]
21620c = columns[records[C]]
21630d = columns[records[D]]
21640e = columns[records[E]]
21650f = columns[records[F]]
2166N/Aend
2167N/A
21680local row = i
21690i -= 1
2170N/A
21710return entity, a[row], b[row], c[row], d[row], e[row], f[row]
2172N/Aend
21730elseif not H then
21740function world_query_iter_next(): any
21750local entity = entities[i]
21760while entity == nil do
21770lastArchetype += 1
21780archetype = compatible_archetypes[lastArchetype]
21790if not archetype then
21800return nil
2181N/Aend
2182N/A
21830entities = archetype.entities
21840i = #entities
21850if i == 0 then
21860continue
2187N/Aend
21880entity = entities[i]
21890columns = archetype.columns
21900records = archetype.records
21910a = columns[records[A]]
21920b = columns[records[B]]
21930c = columns[records[C]]
21940d = columns[records[D]]
21950e = columns[records[E]]
21960f = columns[records[F]]
21970g = columns[records[G]]
2198N/Aend
2199N/A
22000local row = i
22010i -= 1
2202N/A
22030return entity, a[row], b[row], c[row], d[row], e[row], f[row], g[row]
2204N/Aend
22050elseif not I then
22060function world_query_iter_next(): any
22070local entity = entities[i]
22080while entity == nil do
22090lastArchetype += 1
22100archetype = compatible_archetypes[lastArchetype]
22110if not archetype then
22120return nil
2213N/Aend
2214N/A
22150entities = archetype.entities
22160i = #entities
22170if i == 0 then
22180continue
2219N/Aend
22200entity = entities[i]
22210columns = archetype.columns
22220records = archetype.records
22230a = columns[records[A]]
22240b = columns[records[B]]
22250c = columns[records[C]]
22260d = columns[records[D]]
22270e = columns[records[E]]
22280f = columns[records[F]]
22290g = columns[records[G]]
22300h = columns[records[H]]
2231N/Aend
2232N/A
22330local row = i
22340i -= 1
2235N/A
22360return entity, a[row], b[row], c[row], d[row], e[row], f[row], g[row], h[row]
2237N/Aend
22380else
22390local queryOutput = {}
22400function world_query_iter_next(): any
22410local entity = entities[i]
22420while entity == nil do
22430lastArchetype += 1
22440archetype = compatible_archetypes[lastArchetype]
22450if not archetype then
22460return nil
2247N/Aend
2248N/A
22490entities = archetype.entities
22500i = #entities
22510if i == 0 then
22520continue
2253N/Aend
22540entity = entities[i]
22550columns = archetype.columns
22560records = archetype.records
2257N/Aend
2258N/A
22590local row = i
22600i -= 1
2261N/A
22620if not F then
22630return entity, a[row], b[row], c[row], d[row], e[row]
22640elseif not G then
22650return entity, a[row], b[row], c[row], d[row], e[row], f[row]
22660elseif not H then
22670return entity, a[row], b[row], c[row], d[row], e[row], f[row], g[row]
22680elseif not I then
22690return entity, a[row], b[row], c[row], d[row], e[row], f[row], g[row], h[row]
2270N/Aend
2271N/A
22720for j, id in ids do
22730queryOutput[j] = columns[records[id]][row]
2274N/Aend
2275N/A
22760return entity, unpack(queryOutput)
2277N/Aend
2278N/Aend
2279N/A
22806local cached_query = query :: any
22816cached_query.archetypes = query_archetypes
22826cached_query.__iter = cached_query_iter
22836cached_query.iter = cached_query_iter
22840setmetatable(cached_query, cached_query)
22850return cached_query
2286N/Aend
2287N/A
22881local Query = {}
22891Query.__index = Query
22901Query.__iter = query_iter
22911Query.iter = query_iter_init
22921Query.without = query_without
22931Query.with = query_with
22940Query.archetypes = query_archetypes
22951Query.cached = query_cached
2296N/A
229733local function world_query(world: ecs_world_t, ...)
22980local compatible_archetypes = {}
229933local length = 0
2300N/A
230133local ids = { ... }
2302N/A
230333local archetypes = world.archetypes
2304N/A
23050local idr: ecs_id_record_t?
230633local component_index = world.component_index
2307N/A
230833local q = setmetatable({
230933ids = ids,
231033compatible_archetypes = compatible_archetypes,
23110world = world,
231233}, Query)
2313N/A
231447for _, id in ids do
23157local map = component_index[id]
23160if not map then
23170return q
2318N/Aend
2319N/A
23200if idr == nil or map.size < idr.size then
23210idr = map
2322N/Aend
2323N/Aend
2324N/A
23250if not idr then
23260return q
2327N/Aend
2328N/A
232948for archetype_id in idr.cache do
23300local compatibleArchetype = archetypes[archetype_id]
23310if #compatibleArchetype.entities == 0 then
233234continue
2333N/Aend
233434local records = compatibleArchetype.records
2335N/A
233634local skip = false
2337N/A
233847for i, id in ids do
23390local tr = records[id]
23400if not tr then
23410skip = true
23420break
2343N/Aend
2344N/Aend
2345N/A
23460if skip then
23470continue
2348N/Aend
2349N/A
23500length += 1
23510compatible_archetypes[length] = compatibleArchetype
2352N/Aend
2353N/A
23540return q
2355N/Aend
2356N/A
23574local function world_each(world: ecs_world_t, id: i53): () -> ()
23580local idr = world.component_index[id]
23590if not idr then
23600return NOOP
2361N/Aend
2362N/A
23634local idr_cache = idr.cache
23644local archetypes = world.archetypes
23654local archetype_id = next(idr_cache, nil) :: number
23660local archetype = archetypes[archetype_id]
23670if not archetype then
23680return NOOP
2369N/Aend
2370N/A
23710local entities = archetype.entities
23724local row = #entities
2373N/A
237412return function(): any
237511local entity = entities[row]
237611while not entity do
23774archetype_id = next(idr_cache, archetype_id) :: number
23780if not archetype_id then
23797return
2380N/Aend
23817archetype = archetypes[archetype_id]
23827entities = archetype.entities
23830row = #entities
23848entity = entities[row]
2385N/Aend
23860row -= 1
23870return entity
2388N/Aend
2389N/Aend
2390N/A
23910local function world_children(world: ecs_world_t, parent: i53)
23920return world_each(world, ECS_PAIR(EcsChildOf, parent))
2393N/Aend
2394N/A
23950export type Record = {
23960archetype: Archetype,
23970row: number,
23980dense: i24,
23990}
24000export type ComponentRecord = {
24010cache: { [Id]: number },
24020counts: { [Id]: number },
24030flags: number,
24040size: number,
24050hooks: {
24060on_add: ((entity: Entity) -> ())?,
24070on_set: ((entity: Entity, data: any) -> ())?,
24080on_remove: ((entity: Entity) -> ())?,
24090},
24100}
24110export type ComponentIndex = Map
24120export type Archetypes = { [Id]: Archetype }
2413N/A
24140export type EntityIndex = {
24150dense_array: Map,
24160sparse_array: Map,
24170alive_count: number,
24180max_id: number,
24191}
2420N/A
24210local World = {}
24221World.__index = World
2423N/A
24241World.entity = world_entity
24251World.query = world_query
24261World.remove = world_remove
24271World.clear = world_clear
24281World.delete = world_delete
24291World.component = world_component
24301World.add = world_add
24311World.set = world_set
24321World.get = world_get
24331World.has = world_has
24341World.target = world_target
24351World.parent = world_parent
24361World.contains = world_contains
24371World.cleanup = world_cleanup
24380World.each = world_each
24391World.children = world_children
2440N/A
244173local function world_new()
244273local entity_index = {
244373dense_array = {},
244473sparse_array = {},
24450alive_count = 0,
244673max_id = 0,
244773} :: ecs_entity_index_t
244873local self = setmetatable({
244973archetype_index = {} :: { [string]: Archetype },
245073archetypes = {} :: Archetypes,
245173component_index = {} :: ComponentIndex,
24520entity_index = entity_index,
245373ROOT_ARCHETYPE = (nil :: any) :: Archetype,
2454N/A
24550max_archetype_id = 0,
245673max_component_id = 0,
2457N/A
24580observable = {} :: Observable,
245973}, World) :: any
2460N/A
246173self.ROOT_ARCHETYPE = archetype_create(self, {}, "")
2462N/A
246318688for i = 1, HI_COMPONENT_ID do
24640local e = entity_index_new_id(entity_index)
24650world_add(self, e, EcsComponent)
2466N/Aend
2467N/A
24681022for i = HI_COMPONENT_ID + 1, EcsRest do
2469N/A-- Initialize built-in components
24700entity_index_new_id(entity_index)
2471N/Aend
2472N/A
247373world_add(self, EcsName, EcsComponent)
247473world_add(self, EcsOnSet, EcsComponent)
247573world_add(self, EcsOnAdd, EcsComponent)
247673world_add(self, EcsOnRemove, EcsComponent)
24770world_add(self, EcsWildcard, EcsComponent)
247873world_add(self, EcsRest, EcsComponent)
2479N/A
248073world_set(self, EcsOnAdd, EcsName, "jecs.OnAdd")
248173world_set(self, EcsOnRemove, EcsName, "jecs.OnRemove")
248273world_set(self, EcsOnSet, EcsName, "jecs.OnSet")
248373world_set(self, EcsWildcard, EcsName, "jecs.Wildcard")
248473world_set(self, EcsChildOf, EcsName, "jecs.ChildOf")
248573world_set(self, EcsComponent, EcsName, "jecs.Component")
248673world_set(self, EcsOnDelete, EcsName, "jecs.OnDelete")
248773world_set(self, EcsOnDeleteTarget, EcsName, "jecs.OnDeleteTarget")
248873world_set(self, EcsDelete, EcsName, "jecs.Delete")
248973world_set(self, EcsRemove, EcsName, "jecs.Remove")
24900world_set(self, EcsName, EcsName, "jecs.Name")
249173world_set(self, EcsRest, EcsRest, "jecs.Rest")
2492N/A
249373world_add(self, EcsChildOf, ECS_PAIR(EcsOnDeleteTarget, EcsDelete))
2494N/A
24950return self
2496N/Aend
2497N/A
24980World.new = world_new
2499N/A
25000export type Entity = { __T: T }
25010export type Id = { __T: T }
25020export type Pair = Id

25030type ecs_id_t = Id | Pair | Pair<"Tag", T>
25040export type Item = (self: Query) -> (Entity, T...)
25050export type Iter = (query: Query) -> () -> (Entity, T...)
2506N/A
25070export type Query = typeof(setmetatable({}, {
25080__iter = (nil :: any) :: Iter,
25090})) & {
25100iter: Iter,
25110with: (self: Query, ...Id) -> Query,
25120without: (self: Query, ...Id) -> Query,
25130archetypes: (self: Query) -> { Archetype },
25140cached: (self: Query) -> Query,
25150}
2516N/A
25170export type Observer = {
25180callback: (archetype: Archetype) -> (),
25190query: QueryInner,
25200}
2521N/A
25220export type Observable = {
25230[Id]: {
25240[Id]: {
25250{ Observer }
25260}
25270}
25280}
2529N/A
25300export type World = {
25310archetype_index: { [string]: Archetype },
25320archetypes: Archetypes,
25330component_index: ComponentIndex,
25340entity_index: EntityIndex,
25350ROOT_ARCHETYPE: Archetype,
2536N/A
25370max_component_id: number,
25380max_archetype_id: number,
2539N/A
25400observable: any,
2541N/A
2542N/A--- Creates a new entity
25430entity: (self: World, id: Entity?) -> Entity,
2544N/A--- Creates a new entity located in the first 256 ids.
2545N/A--- These should be used for static components for fast access.
25460component: (self: World) -> Entity,
2547N/A--- Gets the target of an relationship. For example, when a user calls
2548N/A--- `world:target(id, ChildOf(parent), 0)`, you will obtain the parent entity.
25490target: (self: World, id: Entity, relation: Id, index: number?) -> Entity?,
2550N/A--- Deletes an entity and all it's related components and relationships.
25510delete: (self: World, id: Entity) -> (),
2552N/A
2553N/A--- Adds a component to the entity with no value
25540add: (self: World, id: Entity, component: Id) -> (),
2555N/A--- Assigns a value to a component on the given entity
25560set: (self: World, id: Entity, component: Id, data: T) -> (),
2557N/A
25580cleanup: (self: World) -> (),
2559N/A-- Clears an entity from the world
25600clear: (self: World, id: Entity) -> (),
2561N/A--- Removes a component from the given entity
25620remove: (self: World, id: Entity, component: Id) -> (),
2563N/A--- Retrieves the value of up to 4 components. These values may be nil.
25640get: ((self: World, id: Entity, Id) -> A?)
25650& ((self: World, id: Entity, Id, Id) -> (A?, B?))
25660& ((self: World, id: Entity, Id, Id, Id) -> (A?, B?, C?))
25670& (self: World, id: Entity, Id, Id, Id, Id) -> (A?, B?, C?, D?),
2568N/A
2569N/A--- Returns whether the entity has the ID.
25700has: (self: World, entity: Entity, ...Id) -> boolean,
2571N/A
2572N/A--- Get parent (target of ChildOf relationship) for entity. If there is no ChildOf relationship pair, it will return nil.
25730parent:(self: World, entity: Entity) -> Entity,
2574N/A
2575N/A--- Checks if the world contains the given entity
25760contains:(self: World, entity: Entity) -> boolean,
2577N/A
25780each: (self: World, id: Id) -> () -> Entity,
2579N/A
25800children: (self: World, id: Id) -> () -> Entity,
2581N/A
2582N/A--- Searches the world for entities that match a given query
25830query: ((World, Id) -> Query)
25840& ((World, Id, Id) -> Query)
25850& ((World, Id, Id, Id) -> Query)
25860& ((World, Id, Id, Id, Id) -> Query)
25870& ((World, Id, Id, Id, Id, Id) -> Query)
25880& ((World, Id, Id, Id, Id, Id, Id) -> Query)
25890& ((World, Id, Id, Id, Id, Id, Id, Id) -> Query)
25900& ((World, Id, Id, Id, Id, Id, Id, Id, Id, ...Id) -> Query)
25910}
2592N/A-- type function ecs_id_t(entity)
2593N/A-- local ty = entity:components()[2]
2594N/A-- local __T = ty:readproperty(types.singleton("__T"))
2595N/A-- if not __T then
2596N/A-- return ty:readproperty(types.singleton("__jecs_pair_value"))
2597N/A-- end
2598N/A-- return __T
2599N/A-- end
2600N/A
2601N/A-- type function ecs_pair_t(first, second)
2602N/A-- if ecs_id_t(first):is("nil") then
2603N/A-- return second
2604N/A-- else
2605N/A-- return first
2606N/A-- end
2607N/A-- end
2608N/A
26091return {
26100World = World :: { new: () -> World },
26111world = World.new :: () -> World,
2612N/A
26131OnAdd = EcsOnAdd :: Entity<(entity: Entity) -> ()>,
26141OnRemove = EcsOnRemove :: Entity<(entity: Entity) -> ()>,
26151OnSet = EcsOnSet :: Entity<(entity: Entity, data: any) -> ()>,
26161ChildOf = EcsChildOf :: Entity,
26171Component = EcsComponent :: Entity,
26181Wildcard = EcsWildcard :: Entity,
26191w = EcsWildcard :: Entity,
26201OnDelete = EcsOnDelete :: Entity,
26211OnDeleteTarget = EcsOnDeleteTarget :: Entity,
26221Delete = EcsDelete :: Entity,
26231Remove = EcsRemove :: Entity,
26240Name = EcsName :: Entity,
26251Rest = EcsRest :: Entity,
2626N/A
26270pair = (ECS_PAIR :: any) :: (first: Id

, second: Id) -> Pair,

2628N/A
2629N/A-- Inwards facing API for testing
26301ECS_ID = ECS_ENTITY_T_LO,
26311ECS_GENERATION_INC = ECS_GENERATION_INC,
26320ECS_GENERATION = ECS_GENERATION,
26331ECS_ID_IS_WILDCARD = ECS_ID_IS_WILDCARD,
2634N/A
26351ECS_ID_DELETE = ECS_ID_DELETE,
2636N/A
26371IS_PAIR = ECS_IS_PAIR,
26381pair_first = ecs_pair_first,
26390pair_second = ecs_pair_second,
26401entity_index_get_alive = entity_index_get_alive,
2641N/A
26421archetype_append_to_records = archetype_append_to_records,
26431id_record_ensure = id_record_ensure,
26441archetype_create = archetype_create,
26451archetype_ensure = archetype_ensure,
26461find_insert = find_insert,
26471find_archetype_with = find_archetype_with,
26481find_archetype_without = find_archetype_without,
26491archetype_init_edge = archetype_init_edge,
26501archetype_ensure_edge = archetype_ensure_edge,
26511init_edge_for_add = init_edge_for_add,
26521init_edge_for_remove = init_edge_for_remove,
26531create_edge_for_add = create_edge_for_add,
26541create_edge_for_remove = create_edge_for_remove,
26550archetype_traverse_add = archetype_traverse_add,
26561archetype_traverse_remove = archetype_traverse_remove,
2657N/A
26581entity_move = entity_move,
2659N/A
26601entity_index_try_get = entity_index_try_get,
26611entity_index_try_get_any = entity_index_try_get_any,
26621entity_index_try_get_fast = entity_index_try_get_fast,
26630entity_index_is_alive = entity_index_is_alive,
26641entity_index_new_id = entity_index_new_id,
2665N/A
26661query_iter = query_iter,
26671query_iter_init = query_iter_init,
26681query_with = query_with,
26691query_without = query_without,
26700query_archetypes = query_archetypes,
26711query_match = query_match,
2672N/A
26730find_observers = find_observers,
26740}