-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathjecs.luau
2370 lines (2016 loc) · 59.9 KB
/
jecs.luau
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--!optimize 2
--!native
--!strict
--draft 4
type i53 = number
type i24 = number
type Ty = { i53 }
type ArchetypeId = number
type Column = { any }
type Map<K, V> = { [K]: V }
type GraphEdge = {
from: Archetype,
to: Archetype?,
id: number,
prev: GraphEdge?,
next: GraphEdge?,
}
type GraphEdges = Map<i53, GraphEdge>
type GraphNode = {
add: GraphEdges,
remove: GraphEdges,
refs: GraphEdge,
}
export type Archetype = {
id: number,
types: Ty,
type: string,
entities: { number },
columns: { Column },
records: { ArchetypeRecord },
} & GraphNode
export type Record = {
archetype: Archetype,
row: number,
dense: i24,
}
type ArchetypeRecord = {
count: number,
column: number,
}
type IdRecord = {
cache: { ArchetypeRecord },
flags: number,
size: number,
hooks: {
on_add: ((entity: i53) -> ())?,
on_set: ((entity: i53, data: any) -> ())?,
on_remove: ((entity: i53) -> ())?,
},
}
type ComponentIndex = Map<i53, IdRecord>
type Archetypes = { [ArchetypeId]: Archetype }
type ArchetypeDiff = {
added: Ty,
removed: Ty,
}
type EntityIndex = {
dense_array: Map<i24, i53>,
sparse_array: Map<i53, Record>,
alive_count: number,
max_id: number,
}
local HI_COMPONENT_ID = _G.__JECS_HI_COMPONENT_ID or 256
-- stylua: ignore start
local EcsOnAdd = HI_COMPONENT_ID + 1
local EcsOnRemove = HI_COMPONENT_ID + 2
local EcsOnSet = HI_COMPONENT_ID + 3
local EcsWildcard = HI_COMPONENT_ID + 4
local EcsChildOf = HI_COMPONENT_ID + 5
local EcsComponent = HI_COMPONENT_ID + 6
local EcsOnDelete = HI_COMPONENT_ID + 7
local EcsOnDeleteTarget = HI_COMPONENT_ID + 8
local EcsDelete = HI_COMPONENT_ID + 9
local EcsRemove = HI_COMPONENT_ID + 10
local EcsName = HI_COMPONENT_ID + 11
local EcsOnArchetypeCreate = HI_COMPONENT_ID + 12
local EcsOnArchetypeDelete = HI_COMPONENT_ID + 13
local EcsRest = HI_COMPONENT_ID + 14
local ECS_PAIR_FLAG = 0x8
local ECS_ID_FLAGS_MASK = 0x10
local ECS_ENTITY_MASK = bit32.lshift(1, 24)
local ECS_GENERATION_MASK = bit32.lshift(1, 16)
local ECS_ID_DELETE = 0b0000_0001
local ECS_ID_IS_TAG = 0b0000_0010
local ECS_ID_HAS_ON_ADD = 0b0000_0100
local ECS_ID_HAS_ON_SET = 0b0000_1000
local ECS_ID_HAS_ON_REMOVE = 0b0001_0000
local ECS_ID_MASK = 0b0000_0000
-- stylua: ignore end
local NULL_ARRAY = table.freeze({}) :: Column
local function FLAGS_ADD(is_pair: boolean): number
local flags = 0x0
if is_pair then
flags = bit32.bor(flags, ECS_PAIR_FLAG) -- HIGHEST bit in the ID.
end
if false then
flags = bit32.bor(flags, 0x4) -- Set the second flag to true
end
if false then
flags = bit32.bor(flags, 0x2) -- Set the third flag to true
end
if false then
flags = bit32.bor(flags, 0x1) -- LAST BIT in the ID.
end
return flags
end
local function ECS_COMBINE(source: number, target: number): i53
return (source * 268435456) + (target * ECS_ID_FLAGS_MASK)
end
local function ECS_IS_PAIR(e: number): boolean
return if e > ECS_ENTITY_MASK then (e % ECS_ID_FLAGS_MASK) // ECS_PAIR_FLAG ~= 0 else false
end
-- HIGH 24 bits LOW 24 bits
local function ECS_GENERATION(e: i53): i24
return if e > ECS_ENTITY_MASK then (e // ECS_ID_FLAGS_MASK) % ECS_GENERATION_MASK else 0
end
local function ECS_GENERATION_INC(e: i53)
if e > ECS_ENTITY_MASK then
local flags = e // ECS_ID_FLAGS_MASK
local id = flags // ECS_ENTITY_MASK
local generation = flags % ECS_GENERATION_MASK
local next_gen = generation + 1
if next_gen > ECS_GENERATION_MASK then
return id
end
return ECS_COMBINE(id, next_gen)
end
return ECS_COMBINE(e, 1)
end
-- FIRST gets the high ID
local function ECS_ENTITY_T_HI(e: i53): i24
return if e > ECS_ENTITY_MASK then (e // ECS_ID_FLAGS_MASK) % ECS_ENTITY_MASK else e
end
-- SECOND
local function ECS_ENTITY_T_LO(e: i53): i24
return if e > ECS_ENTITY_MASK then (e // ECS_ID_FLAGS_MASK) // ECS_ENTITY_MASK else e
end
local function _STRIP_GENERATION(e: i53): i24
return ECS_ENTITY_T_LO(e)
end
local function ECS_PAIR(pred: i53, obj: i53): i53
return ECS_COMBINE(ECS_ENTITY_T_LO(pred), ECS_ENTITY_T_LO(obj)) + FLAGS_ADD(--[[isPair]] true) :: i53
end
local function entity_index_try_get_any(entity_index: EntityIndex, entity: number): Record?
local r = entity_index.sparse_array[ECS_ENTITY_T_LO(entity)]
if not r then
return nil
end
if not r or r.dense == 0 then
return nil
end
return r
end
local function entity_index_try_get(entity_index: EntityIndex, entity: number): Record?
local r = entity_index_try_get_any(entity_index, entity)
if r then
local r_dense = r.dense
if r_dense > entity_index.alive_count then
return nil
end
if entity_index.dense_array[r_dense] ~= entity then
return nil
end
end
return r
end
local function entity_index_try_get_fast(entity_index: EntityIndex, entity: number): Record?
local r = entity_index.sparse_array[ECS_ENTITY_T_LO(entity)]
if r then
if entity_index.dense_array[r.dense] ~= entity then
return nil
end
end
return r
end
local function entity_index_get_alive(index: EntityIndex, e: i24): i53
local r = entity_index_try_get_any(index, e)
if r then
return index.dense_array[r.dense]
end
return 0
end
local function entity_index_is_alive(entity_index: EntityIndex, entity: number)
return entity_index_try_get(entity_index, entity) ~= nil
end
local function entity_index_new_id(entity_index: EntityIndex): i53
local dense_array = entity_index.dense_array
local alive_count = entity_index.alive_count
if alive_count ~= #dense_array then
alive_count += 1
entity_index.alive_count = alive_count
local id = dense_array[alive_count]
return id
end
local id = entity_index.max_id + 1
entity_index.max_id = id
alive_count += 1
entity_index.alive_count = alive_count
dense_array[alive_count] = id
entity_index.sparse_array[id] = { dense = alive_count } :: Record
return id
end
-- ECS_PAIR_FIRST, gets the relationship target / obj / HIGH bits
local function ecs_pair_first(world, e)
return entity_index_get_alive(world.entity_index, ECS_ENTITY_T_LO(e))
end
-- ECS_PAIR_SECOND gets the relationship / pred / LOW bits
local function ecs_pair_second(world, e)
return entity_index_get_alive(world.entity_index, ECS_ENTITY_T_HI(e))
end
local function query_match(query, archetype: Archetype)
local records = archetype.records
local with = query.filter_with
for _, id in with do
if not records[id] then
return false
end
end
local without = query.filter_without
if without then
for _, id in without do
if records[id] then
return false
end
end
end
return true
end
local function find_observers(world: World, event, component): { Observer }?
local cache = world.observerable[event]
if not cache then
return nil
end
return cache[component] :: any
end
local function archetype_move(entity_index: EntityIndex, to: Archetype, dst_row: i24, from: Archetype, src_row: i24)
local src_columns = from.columns
local dst_columns = to.columns
local dst_entities = to.entities
local src_entities = from.entities
local last = #src_entities
local id_types = from.types
local records = to.records
for i, column in src_columns do
if column == NULL_ARRAY then
continue
end
-- Retrieves the new column index from the source archetype's record from each component
-- We have to do this because the columns are tightly packed and indexes may not correspond to each other.
local tr = records[id_types[i]]
-- Sometimes target column may not exist, e.g. when you remove a component.
if tr then
dst_columns[tr.column][dst_row] = column[src_row]
end
-- If the entity is the last row in the archetype then swapping it would be meaningless.
if src_row ~= last then
-- Swap rempves columns to ensure there are no holes in the archetype.
column[src_row] = column[last]
end
column[last] = nil
end
local moved = #src_entities
-- Move the entity from the source to the destination archetype.
-- Because we have swapped columns we now have to update the records
-- corresponding to the entities' rows that were swapped.
local e1 = src_entities[src_row]
local e2 = src_entities[moved]
if src_row ~= moved then
src_entities[src_row] = e2
end
src_entities[moved] = nil :: any
dst_entities[dst_row] = e1
local sparse_array = entity_index.sparse_array
local record1 = sparse_array[ECS_ENTITY_T_LO(e1)]
local record2 = sparse_array[ECS_ENTITY_T_LO(e2)]
record1.row = dst_row
record2.row = src_row
end
local function archetype_append(entity: number, archetype: Archetype): number
local entities = archetype.entities
local length = #entities + 1
entities[length] = entity
return length
end
local function new_entity(entityId: i53, record: Record, archetype: Archetype): Record
local row = archetype_append(entityId, archetype)
record.archetype = archetype
record.row = row
return record
end
local function entity_move(entity_index: EntityIndex, entityId: i53, record: Record, to: Archetype)
local sourceRow = record.row
local from = record.archetype
local dst_row = archetype_append(entityId, to)
archetype_move(entity_index, to, dst_row, from, sourceRow)
record.archetype = to
record.row = dst_row
end
local function hash(arr: { number }): string
return table.concat(arr, "_")
end
local function fetch(id, records: { ArchetypeRecord }, columns: { Column }, row: number): any
local tr = records[id]
if not tr then
return nil
end
return columns[tr.column][row]
end
local function world_get(world: World, entity: i53, a: i53, b: i53?, c: i53?, d: i53?, e: i53?): ...any
local record = entity_index_try_get_fast(world.entity_index, entity)
if not record then
return nil
end
local archetype = record.archetype
if not archetype then
return nil
end
local records = archetype.records
local columns = archetype.columns
local row = record.row
local va = fetch(a, records, columns, row)
if not b then
return va
elseif not c then
return va, fetch(b, records, columns, row)
elseif not d then
return va, fetch(b, records, columns, row), fetch(c, records, columns, row)
elseif not e then
return va, fetch(b, records, columns, row), fetch(c, records, columns, row), fetch(d, records, columns, row)
else
error("args exceeded")
end
end
local function world_get_one_inline(world: World, entity: i53, id: i53): any
local record = entity_index_try_get_fast(world.entity_index, entity)
if not record then
return nil
end
local archetype = record.archetype
if not archetype then
return nil
end
local tr = archetype.records[id]
if not tr then
return nil
end
return archetype.columns[tr.column][record.row]
end
local function world_has_one_inline(world: World, entity: number, id: i53): boolean
local record = entity_index_try_get_fast(world.entity_index, entity)
if not record then
return false
end
local archetype = record.archetype
if not archetype then
return false
end
local records = archetype.records
return records[id] ~= nil
end
local function world_has(world: World, entity: number, ...: i53): boolean
local record = entity_index_try_get_fast(world.entity_index, entity)
if not record then
return false
end
local archetype = record.archetype
if not archetype then
return false
end
local records = archetype.records
for i = 1, select("#", ...) do
if not records[select(i, ...)] then
return false
end
end
return true
end
local function world_target(world: World, entity: i53, relation: i24, index: number?): i24?
local nth = index or 0
local record = entity_index_try_get_fast(world.entity_index, entity)
if not record then
return nil
end
local archetype = record.archetype
if not archetype then
return nil
end
local idr = world.componentIndex[ECS_PAIR(relation, EcsWildcard)]
if not idr then
return nil
end
local tr = idr.cache[archetype.id]
if not tr then
return nil
end
local count = tr.count
if nth >= count then
nth = nth + count + 1
end
nth = archetype.types[nth + tr.column]
if not nth then
return nil
end
return ecs_pair_second(world, nth)
end
local function ECS_ID_IS_WILDCARD(e: i53): boolean
local first = ECS_ENTITY_T_HI(e)
local second = ECS_ENTITY_T_LO(e)
return first == EcsWildcard or second == EcsWildcard
end
local function id_record_ensure(world: World, id: number): IdRecord
local componentIndex = world.componentIndex
local idr = componentIndex[id]
if not idr then
local flags = ECS_ID_MASK
local relation = id
local is_pair = ECS_IS_PAIR(id)
if is_pair then
relation = ecs_pair_first(world, id)
end
local cleanup_policy = world_target(world, relation, EcsOnDelete, 0)
local cleanup_policy_target = world_target(world, relation, EcsOnDeleteTarget, 0)
local has_delete = false
if cleanup_policy == EcsDelete or cleanup_policy_target == EcsDelete then
has_delete = true
end
local on_add, on_set, on_remove = world_get(world, relation, EcsOnAdd, EcsOnSet, EcsOnRemove)
local is_tag = not world_has_one_inline(world, relation, EcsComponent)
if is_tag and is_pair then
is_tag = not world_has_one_inline(world, ecs_pair_second(world, id), EcsComponent)
end
flags = bit32.bor(
flags,
if on_add then ECS_ID_HAS_ON_ADD else 0,
if on_remove then ECS_ID_HAS_ON_REMOVE else 0,
if on_set then ECS_ID_HAS_ON_SET else 0,
if has_delete then ECS_ID_DELETE else 0,
if is_tag then ECS_ID_IS_TAG else 0
)
idr = {
size = 0,
cache = {},
flags = flags,
hooks = {
on_add = on_add,
on_set = on_set,
on_remove = on_remove,
},
} :: IdRecord
componentIndex[id] = idr
end
return idr
end
local function archetype_append_to_records(
idr: IdRecord,
archetype_id: number,
records: Map<i53, ArchetypeRecord>,
id: number,
index: number
)
local tr = idr.cache[archetype_id]
if not tr then
tr = { column = index, count = 1 }
idr.cache[archetype_id] = tr
idr.size += 1
records[id] = tr
else
tr.count += 1
end
end
local function archetype_create(world: World, id_types: { i24 }, ty, prev: i53?): Archetype
local archetype_id = (world.nextArchetypeId :: number) + 1
world.nextArchetypeId = archetype_id
local length = #id_types
local columns = (table.create(length) :: any) :: { Column }
local records: { ArchetypeRecord } = {}
local archetype: Archetype = {
columns = columns,
entities = {},
id = archetype_id,
records = records,
type = ty,
types = id_types,
add = {},
remove = {},
refs = {} :: GraphEdge,
}
for i, componentId in id_types do
local idr = id_record_ensure(world, componentId)
archetype_append_to_records(idr, archetype_id, records, componentId, i)
if ECS_IS_PAIR(componentId) then
local relation = ecs_pair_first(world, componentId)
local object = ecs_pair_second(world, componentId)
local r = ECS_PAIR(relation, EcsWildcard)
local idr_r = id_record_ensure(world, r)
archetype_append_to_records(idr_r, archetype_id, records, r, i)
local t = ECS_PAIR(EcsWildcard, object)
local idr_t = id_record_ensure(world, t)
archetype_append_to_records(idr_t, archetype_id, records, t, i)
end
if bit32.band(idr.flags, ECS_ID_IS_TAG) == 0 then
columns[i] = {}
else
columns[i] = NULL_ARRAY
end
end
for id in records do
local observer_list = find_observers(world, EcsOnArchetypeCreate, id)
if not observer_list then
continue
end
for _, observer in observer_list do
if query_match(observer.query, archetype) then
observer.callback(archetype)
end
end
end
world.archetypeIndex[ty] = archetype
world.archetypes[archetype_id] = archetype
return archetype
end
local function world_entity(world: World): i53
return entity_index_new_id(world.entity_index)
end
local function world_parent(world: World, entity: i53)
return world_target(world, entity, EcsChildOf, 0)
end
local function archetype_ensure(world: World, id_types): Archetype
if #id_types < 1 then
return world.ROOT_ARCHETYPE
end
local ty = hash(id_types)
local archetype = world.archetypeIndex[ty]
if archetype then
return archetype
end
return archetype_create(world, id_types, ty)
end
local function find_insert(id_types: { i53 }, toAdd: i53): number
for i, id in id_types do
if id == toAdd then
return -1
end
if id > toAdd then
return i
end
end
return #id_types + 1
end
local function find_archetype_with(world: World, node: Archetype, id: i53): Archetype
local id_types = node.types
-- Component IDs are added incrementally, so inserting and sorting
-- them each time would be expensive. Instead this insertion sort can find the insertion
-- point in the types array.
local dst = table.clone(node.types) :: { i53 }
local at = find_insert(id_types, id)
if at == -1 then
-- If it finds a duplicate, it just means it is the same archetype so it can return it
-- directly instead of needing to hash types for a lookup to the archetype.
return node
end
table.insert(dst, at, id)
return archetype_ensure(world, dst)
end
local function find_archetype_without(world: World, node: Archetype, id: i53): Archetype
local id_types = node.types
local at = table.find(id_types, id)
if at == nil then
return node
end
local dst = table.clone(id_types)
table.remove(dst, at)
return archetype_ensure(world, dst)
end
local function archetype_init_edge(archetype: Archetype, edge: GraphEdge, id: i53, to: Archetype)
edge.from = archetype
edge.to = to
edge.id = id
end
local function archetype_ensure_edge(world, edges: GraphEdges, id): GraphEdge
local edge = edges[id]
if not edge then
edge = {} :: GraphEdge
edges[id] = edge
end
return edge
end
local function init_edge_for_add(world, archetype: Archetype, edge: GraphEdge, id, to: Archetype)
archetype_init_edge(archetype, edge, id, to)
archetype_ensure_edge(world, archetype.add, id)
if archetype ~= to then
local to_refs = to.refs
local next_edge = to_refs.next
to_refs.next = edge
edge.prev = to_refs
edge.next = next_edge
if next_edge then
next_edge.prev = edge
end
end
end
local function init_edge_for_remove(world: World, archetype: Archetype, edge: GraphEdge, id: number, to: Archetype)
archetype_init_edge(archetype, edge, id, to)
archetype_ensure_edge(world, archetype.remove, id)
if archetype ~= to then
local to_refs = to.refs
local prev_edge = to_refs.prev
to_refs.prev = edge
edge.next = to_refs
edge.prev = prev_edge
if prev_edge then
prev_edge.next = edge
end
end
end
local function create_edge_for_add(world: World, node: Archetype, edge: GraphEdge, id: i53): Archetype
local to = find_archetype_with(world, node, id)
init_edge_for_add(world, node, edge, id, to)
return to
end
local function create_edge_for_remove(world: World, node: Archetype, edge: GraphEdge, id: i53): Archetype
local to = find_archetype_without(world, node, id)
init_edge_for_remove(world, node, edge, id, to)
return to
end
local function archetype_traverse_add(world: World, id: i53, from: Archetype): Archetype
from = from or world.ROOT_ARCHETYPE
local edge = archetype_ensure_edge(world, from.add, id)
local to = edge.to
if not to then
to = create_edge_for_add(world, from, edge, id)
end
return to :: Archetype
end
local function archetype_traverse_remove(world: World, id: i53, from: Archetype): Archetype
from = from or world.ROOT_ARCHETYPE
local edge = archetype_ensure_edge(world, from.remove, id)
local to = edge.to
if not to then
to = create_edge_for_remove(world, from, edge, id)
end
return to :: Archetype
end
local function invoke_hook(action, entity, data)
action(entity, data)
end
local function world_add(world: World, entity: i53, id: i53): ()
local entity_index = world.entity_index
local record = entity_index_try_get_fast(entity_index, entity)
if not record then
return
end
local from = record.archetype
local to = archetype_traverse_add(world, id, from)
if from == to then
return
end
if from then
entity_move(entity_index, entity, record, to)
else
if #to.types > 0 then
new_entity(entity, record, to)
end
end
local idr = world.componentIndex[id]
local on_add = idr.hooks.on_add
if on_add then
on_add(entity)
end
end
local function world_set(world: World, entity: i53, id: i53, data: unknown): ()
local entity_index = world.entity_index
local record = entity_index_try_get_fast(entity_index, entity)
if not record then
return
end
local from: Archetype = record.archetype
local to: Archetype = archetype_traverse_add(world, id, from)
local idr = world.componentIndex[id]
local flags = idr.flags
local is_tag = bit32.band(flags, ECS_ID_IS_TAG) ~= 0
local idr_hooks = idr.hooks
if from == to then
if is_tag then
return
end
-- If the archetypes are the same it can avoid moving the entity
-- and just set the data directly.
local tr = to.records[id]
local column = from.columns[tr.column]
column[record.row] = data
local on_set = idr_hooks.on_set
if on_set then
on_set(entity, data)
end
return
end
if from then
-- If there was a previous archetype, then the entity needs to move the archetype
entity_move(entity_index, entity, record, to)
else
if #to.types > 0 then
-- When there is no previous archetype it should create the archetype
new_entity(entity, record, to)
end
end
local on_add = idr_hooks.on_add
if on_add then
on_add(entity)
end
if is_tag then
return
end
local tr = to.records[id]
local column = to.columns[tr.column]
column[record.row] = data
local on_set = idr_hooks.on_set
if on_set then
invoke_hook(on_set, entity, data)
end
end
local function world_component(world: World): i53
local componentId = (world.nextComponentId :: number) + 1
if componentId > HI_COMPONENT_ID then
-- IDs are partitioned into ranges because component IDs are not nominal,
-- so it needs to error when IDs intersect into the entity range.
error("Too many components, consider using world:entity() instead to create components.")
end
world.nextComponentId = componentId
return componentId
end
local function world_remove(world: World, entity: i53, id: i53)
local entity_index = world.entity_index
local record = entity_index_try_get_fast(entity_index, entity)
if not record then
return
end
local from = record.archetype
if not from then
return
end
local to = archetype_traverse_remove(world, id, from)
if from and not (from == to) then
local idr = world.componentIndex[id]
local on_remove = idr.hooks.on_remove
if on_remove then
on_remove(entity)
end
entity_move(entity_index, entity, record, to)
end
end
local function archetype_fast_delete_last(columns: { Column }, column_count: number, types: { i53 }, entity: i53)
for i, column in columns do
if column ~= NULL_ARRAY then
column[column_count] = nil
end
end
end
local function archetype_fast_delete(columns: { Column }, column_count: number, row, types, entity)
for i, column in columns do
if column ~= NULL_ARRAY then
column[row] = column[column_count]
column[column_count] = nil
end
end
end
local function archetype_delete(world: World, archetype: Archetype, row: number, destruct: boolean?)
local entityIndex = world.entity_index
local columns = archetype.columns
local id_types = archetype.types
local entities = archetype.entities
local column_count = #entities
local last = #entities
local move = entities[last]
local delete = entities[row]
entities[row] = move
entities[last] = nil :: any
if row ~= last then
-- TODO: should be "entity_index_sparse_get(entityIndex, move)"
local record_to_move = entity_index_try_get_any(entityIndex, move)
if record_to_move then
record_to_move.row = row
end
end
-- TODO: if last == 0 then deactivate table
for _, id in id_types do
local on_remove: (entity: i53) -> () = world_get_one_inline(world, id, EcsOnRemove)
if on_remove then
on_remove(delete)
end
end
if row == last then
archetype_fast_delete_last(columns, column_count, id_types, delete)
else
archetype_fast_delete(columns, column_count, row, id_types, delete)
end
end
local function world_clear(world: World, entity: i53)
--TODO: use sparse_get (stashed)
local record = entity_index_try_get(world.entity_index, entity)
if not record then
return
end
local archetype = record.archetype
local row = record.row
if archetype then
-- In the future should have a destruct mode for
-- deleting archetypes themselves. Maybe requires recycling
archetype_delete(world, archetype, row)
end
record.archetype = nil :: any
record.row = nil :: any
end
local function archetype_disconnect_edge(edge: GraphEdge)
local edge_next = edge.next
local edge_prev = edge.prev
if edge_next then
edge_next.prev = edge_prev
end
if edge_prev then