-
-
Notifications
You must be signed in to change notification settings - Fork 586
Expand file tree
/
Copy pathentity.c
More file actions
5340 lines (4646 loc) · 162 KB
/
entity.c
File metadata and controls
5340 lines (4646 loc) · 162 KB
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
/**
* @file entity.c
* @brief Entity API.
*
* This file contains the implementation for the entity API, which includes
* creating/deleting entities, adding/removing/setting components, instantiating
* prefabs, and several other APIs for retrieving entity data.
*
* The file also contains the implementation of the command buffer, which is
* located here so it can call functions private to the compilation unit.
*/
#include "private_api.h"
#include <ctype.h>
#ifdef FLECS_QUERY_DSL
#include "addons/query_dsl/query_dsl.h"
#endif
static
const ecs_entity_t* flecs_bulk_new(
ecs_world_t *world,
ecs_table_t *table,
const ecs_entity_t *entities,
ecs_type_t *component_ids,
int32_t count,
void **c_info,
bool move,
int32_t *row_out,
ecs_table_diff_t *diff);
typedef struct {
const ecs_type_info_t *ti;
void *ptr;
} flecs_component_ptr_t;
static
flecs_component_ptr_t flecs_table_get_component(
ecs_table_t *table,
int32_t column_index,
int32_t row)
{
ecs_check(column_index < table->column_count, ECS_NOT_A_COMPONENT, NULL);
ecs_column_t *column = &table->data.columns[column_index];
return (flecs_component_ptr_t){
.ti = column->ti,
.ptr = ECS_ELEM(column->data, column->ti->size, row)
};
error:
return (flecs_component_ptr_t){0};
}
static
flecs_component_ptr_t flecs_get_component_ptr(
ecs_table_t *table,
int32_t row,
ecs_id_record_t *idr)
{
ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL);
if (!idr) {
return (flecs_component_ptr_t){0};
}
if (idr->flags & EcsIdIsSparse) {
ecs_entity_t entity = ecs_table_entities(table)[row];
return (flecs_component_ptr_t){
.ti = idr->type_info,
.ptr = flecs_sparse_get_any(idr->sparse, 0, entity)
};
}
ecs_table_record_t *tr = flecs_id_record_get_table(idr, table);
if (!tr || (tr->column == -1)) {
return (flecs_component_ptr_t){0};
}
return flecs_table_get_component(table, tr->column, row);
}
static
void* flecs_get_component(
ecs_table_t *table,
int32_t row,
ecs_id_record_t *idr)
{
return flecs_get_component_ptr(table, row, idr).ptr;
}
void* flecs_get_base_component(
const ecs_world_t *world,
ecs_table_t *table,
ecs_id_t id,
ecs_id_record_t *idr,
int32_t recur_depth)
{
ecs_check(recur_depth < ECS_MAX_RECURSION, ECS_INVALID_PARAMETER,
"cycle detected in IsA relationship");
/* Table (and thus entity) does not have component, look for base */
if (!(table->flags & EcsTableHasIsA)) {
return NULL;
}
if (!(idr->flags & EcsIdOnInstantiateInherit)) {
return NULL;
}
/* Exclude Name */
if (id == ecs_pair(ecs_id(EcsIdentifier), EcsName)) {
return NULL;
}
/* Table should always be in the table index for (IsA, *), otherwise the
* HasBase flag should not have been set */
ecs_table_record_t *tr_isa = flecs_id_record_get_table(
world->idr_isa_wildcard, table);
ecs_check(tr_isa != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_type_t type = table->type;
ecs_id_t *ids = type.array;
int32_t i = tr_isa->index, end = tr_isa->count + tr_isa->index;
void *ptr = NULL;
do {
ecs_id_t pair = ids[i ++];
ecs_entity_t base = ecs_pair_second(world, pair);
ecs_record_t *r = flecs_entities_get(world, base);
ecs_assert(r != NULL, ECS_INTERNAL_ERROR, NULL);
table = r->table;
if (!table) {
continue;
}
const ecs_table_record_t *tr = flecs_id_record_get_table(idr, table);
if (!tr) {
ptr = flecs_get_base_component(world, table, id, idr,
recur_depth + 1);
} else {
if (idr->flags & EcsIdIsSparse) {
return flecs_sparse_get_any(idr->sparse, 0, base);
} else {
int32_t row = ECS_RECORD_TO_ROW(r->row);
return flecs_table_get_component(table, tr->column, row).ptr;
}
}
} while (!ptr && (i < end));
return ptr;
error:
return NULL;
}
static
void flecs_instantiate_slot(
ecs_world_t *world,
ecs_entity_t base,
ecs_entity_t instance,
ecs_entity_t slot_of,
ecs_entity_t slot,
ecs_entity_t child)
{
if (base == slot_of) {
/* Instance inherits from slot_of, add slot to instance */
ecs_add_pair(world, instance, slot, child);
} else {
/* Slot is registered for other prefab, travel hierarchy
* upwards to find instance that inherits from slot_of */
ecs_entity_t parent = instance;
int32_t depth = 0;
do {
if (ecs_has_pair(world, parent, EcsIsA, slot_of)) {
const char *name = ecs_get_name(world, slot);
if (name == NULL) {
char *slot_of_str = ecs_get_path(world, slot_of);
ecs_throw(ECS_INVALID_OPERATION, "prefab '%s' has unnamed "
"slot (slots must be named)", slot_of_str);
ecs_os_free(slot_of_str);
return;
}
/* The 'slot' variable is currently pointing to a child (or
* grandchild) of the current base. Find the original slot by
* looking it up under the prefab it was registered. */
if (depth == 0) {
/* If the current instance is an instance of slot_of, just
* lookup the slot by name, which is faster than having to
* create a relative path. */
slot = ecs_lookup_child(world, slot_of, name);
} else {
/* If the slot is more than one level away from the slot_of
* parent, use a relative path to find the slot */
char *path = ecs_get_path_w_sep(world, parent, child, ".",
NULL);
slot = ecs_lookup_path_w_sep(world, slot_of, path, ".",
NULL, false);
ecs_os_free(path);
}
if (slot == 0) {
char *slot_of_str = ecs_get_path(world, slot_of);
char *slot_str = ecs_get_path(world, slot);
ecs_throw(ECS_INVALID_OPERATION,
"'%s' is not in hierarchy for slot '%s'",
slot_of_str, slot_str);
ecs_os_free(slot_of_str);
ecs_os_free(slot_str);
}
ecs_add_pair(world, parent, slot, child);
break;
}
depth ++;
} while ((parent = ecs_get_target(world, parent, EcsChildOf, 0)));
if (parent == 0) {
char *slot_of_str = ecs_get_path(world, slot_of);
char *slot_str = ecs_get_path(world, slot);
ecs_throw(ECS_INVALID_OPERATION,
"'%s' is not in hierarchy for slot '%s'",
slot_of_str, slot_str);
ecs_os_free(slot_of_str);
ecs_os_free(slot_str);
}
}
error:
return;
}
static
ecs_table_t* flecs_find_table_add(
ecs_world_t *world,
ecs_table_t *table,
ecs_id_t id,
ecs_table_diff_builder_t *diff)
{
ecs_table_diff_t temp_diff = ECS_TABLE_DIFF_INIT;
table = flecs_table_traverse_add(world, table, &id, &temp_diff);
ecs_check(table != NULL, ECS_INVALID_PARAMETER, NULL);
flecs_table_diff_build_append_table(world, diff, &temp_diff);
return table;
error:
return NULL;
}
static
ecs_table_t* flecs_find_table_remove(
ecs_world_t *world,
ecs_table_t *table,
ecs_id_t id,
ecs_table_diff_builder_t *diff)
{
ecs_table_diff_t temp_diff = ECS_TABLE_DIFF_INIT;
table = flecs_table_traverse_remove(world, table, &id, &temp_diff);
ecs_check(table != NULL, ECS_INVALID_PARAMETER, NULL);
flecs_table_diff_build_append_table(world, diff, &temp_diff);
return table;
error:
return NULL;
}
static
int32_t flecs_child_type_insert(
ecs_type_t *type,
void **component_data,
ecs_id_t id)
{
int32_t i, count = type->count;
for (i = 0; i < count; i ++) {
ecs_id_t cur = type->array[i];
if (cur == id) {
/* Id is already part of type */
return -1;
}
if (cur > id) {
/* A larger id was found so id can't be part of the type. */
break;
}
}
/* Assumes that the array has enough memory to store the new element. */
int32_t to_move = type->count - i;
if (to_move) {
ecs_os_memmove(&type->array[i + 1],
&type->array[i], to_move * ECS_SIZEOF(ecs_id_t));
ecs_os_memmove(&component_data[i + 1],
&component_data[i], to_move * ECS_SIZEOF(void*));
}
component_data[i] = NULL;
type->array[i] = id;
type->count ++;
return i;
}
static
void flecs_instantiate_children(
ecs_world_t *world,
ecs_entity_t base,
ecs_table_t *table,
int32_t row,
int32_t count,
ecs_table_t *child_table,
const ecs_instantiate_ctx_t *ctx)
{
if (!ecs_table_count(child_table)) {
return;
}
ecs_type_t type = child_table->type;
ecs_data_t *child_data = &child_table->data;
ecs_entity_t slot_of = 0;
ecs_entity_t *ids = type.array;
int32_t type_count = type.count;
/* Instantiate child table for each instance */
/* Create component array for creating the table */
ecs_table_diff_t diff = { .added = {0}};
diff.added.array = ecs_os_alloca_n(ecs_entity_t, type_count + 1);
void **component_data = ecs_os_alloca_n(void*, type_count + 1);
/* Copy in component identifiers. Find the base index in the component
* array, since we'll need this to replace the base with the instance id */
int j, i, childof_base_index = -1;
for (i = 0; i < type_count; i ++) {
ecs_id_t id = ids[i];
/* If id has DontInherit flag don't inherit it, except for the name
* and ChildOf pairs. The name is preserved so applications can lookup
* the instantiated children by name. The ChildOf pair is replaced later
* with the instance parent. */
if ((id != ecs_pair(ecs_id(EcsIdentifier), EcsName)) &&
ECS_PAIR_FIRST(id) != EcsChildOf)
{
ecs_table_record_t *tr = &child_table->_->records[i];
ecs_id_record_t *idr = (ecs_id_record_t*)tr->hdr.cache;
if (idr->flags & EcsIdOnInstantiateDontInherit) {
continue;
}
}
/* If child is a slot, keep track of which parent to add it to, but
* don't add slot relationship to child of instance. If this is a child
* of a prefab, keep the SlotOf relationship intact. */
if (!(table->flags & EcsTableIsPrefab)) {
if (ECS_IS_PAIR(id) && ECS_PAIR_FIRST(id) == EcsSlotOf) {
ecs_assert(slot_of == 0, ECS_INTERNAL_ERROR, NULL);
slot_of = ecs_pair_second(world, id);
continue;
}
}
/* Keep track of the element that creates the ChildOf relationship with
* the prefab parent. We need to replace this element to make sure the
* created children point to the instance and not the prefab */
if (ECS_HAS_RELATION(id, EcsChildOf) &&
(ECS_PAIR_SECOND(id) == (uint32_t)base)) {
childof_base_index = diff.added.count;
}
/* If this is a pure override, make sure we have a concrete version of the
* component. This relies on the fact that overrides always come after
* concrete components in the table type so we can check the components
* that have already been added to the child table type. */
if (ECS_HAS_ID_FLAG(id, AUTO_OVERRIDE)) {
ecs_id_t concreteId = id & ~ECS_AUTO_OVERRIDE;
flecs_child_type_insert(&diff.added, component_data, concreteId);
continue;
}
int32_t storage_index = ecs_table_type_to_column_index(child_table, i);
if (storage_index != -1) {
component_data[diff.added.count] =
child_data->columns[storage_index].data;
} else {
component_data[diff.added.count] = NULL;
}
diff.added.array[diff.added.count] = id;
diff.added.count ++;
diff.added_flags |= flecs_id_flags_get(world, id);
}
/* Table must contain children of base */
ecs_assert(childof_base_index != -1, ECS_INTERNAL_ERROR, NULL);
/* If children are added to a prefab, make sure they are prefabs too */
if (table->flags & EcsTableIsPrefab) {
if (flecs_child_type_insert(
&diff.added, component_data, EcsPrefab) != -1)
{
childof_base_index ++;
}
}
/* Instantiate the prefab child table for each new instance */
const ecs_entity_t *instances = ecs_table_entities(table);
int32_t child_count = ecs_table_count(child_table);
ecs_entity_t *child_ids = flecs_walloc_n(world, ecs_entity_t, child_count);
for (i = row; i < count + row; i ++) {
ecs_entity_t instance = instances[i];
ecs_table_t *i_table = NULL;
/* Replace ChildOf element in the component array with instance id */
diff.added.array[childof_base_index] = ecs_pair(EcsChildOf, instance);
/* Find or create table */
i_table = flecs_table_find_or_create(world, &diff.added);
ecs_assert(i_table != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_assert(i_table->type.count == diff.added.count,
ECS_INTERNAL_ERROR, NULL);
/* The instance is trying to instantiate from a base that is also
* its parent. This would cause the hierarchy to instantiate itself
* which would cause infinite recursion. */
const ecs_entity_t *children = ecs_table_entities(child_table);
#ifdef FLECS_DEBUG
for (j = 0; j < child_count; j ++) {
ecs_entity_t child = children[j];
ecs_check(child != instance, ECS_INVALID_PARAMETER,
"cycle detected in IsA relationship");
}
#else
/* Bit of boilerplate to ensure that we don't get warnings about the
* error label not being used. */
ecs_check(true, ECS_INVALID_OPERATION, NULL);
#endif
/* Attempt to reserve ids for children that have the same offset from
* the instance as from the base prefab. This ensures stable ids for
* instance children, even across networked applications. */
ecs_instantiate_ctx_t ctx_cur = {base, instance};
if (ctx) {
ctx_cur = *ctx;
}
for (j = 0; j < child_count; j ++) {
if ((uint32_t)children[j] < (uint32_t)ctx_cur.root_prefab) {
/* Child id is smaller than root prefab id, can't use offset */
child_ids[j] = ecs_new(world);
continue;
}
/* Get prefab offset, ignore lifecycle generation count */
ecs_entity_t prefab_offset =
(uint32_t)children[j] - (uint32_t)ctx_cur.root_prefab;
ecs_assert(prefab_offset != 0, ECS_INTERNAL_ERROR, NULL);
/* First check if any entity with the desired id exists */
ecs_entity_t instance_child = (uint32_t)ctx_cur.root_instance + prefab_offset;
ecs_entity_t alive_id = flecs_entities_get_alive(world, instance_child);
if (alive_id && flecs_entities_is_alive(world, alive_id)) {
/* Alive entity with requested id exists, can't use offset id */
child_ids[j] = ecs_new(world);
continue;
}
/* Id is not in use. Make it alive & match the generation of the instance. */
instance_child = ctx_cur.root_instance + prefab_offset;
flecs_entities_make_alive(world, instance_child);
flecs_entities_ensure(world, instance_child);
ecs_assert(ecs_is_alive(world, instance_child), ECS_INTERNAL_ERROR, NULL);
child_ids[j] = instance_child;
}
/* Create children */
int32_t child_row;
const ecs_entity_t *i_children = flecs_bulk_new(world, i_table, child_ids,
&diff.added, child_count, component_data, false, &child_row, &diff);
/* If children are slots, add slot relationships to parent */
if (slot_of) {
for (j = 0; j < child_count; j ++) {
ecs_entity_t child = children[j];
ecs_entity_t i_child = i_children[j];
flecs_instantiate_slot(world, base, instance, slot_of,
child, i_child);
}
}
/* If prefab child table has children itself, recursively instantiate */
for (j = 0; j < child_count; j ++) {
ecs_entity_t child = children[j];
flecs_instantiate(world, child, i_table, child_row + j, 1, &ctx_cur);
}
}
flecs_wfree_n(world, ecs_entity_t, child_count, child_ids);
error:
return;
}
void flecs_instantiate(
ecs_world_t *world,
ecs_entity_t base,
ecs_table_t *table,
int32_t row,
int32_t count,
const ecs_instantiate_ctx_t *ctx)
{
ecs_record_t *record = flecs_entities_get_any(world, base);
ecs_table_t *base_table = record->table;
if (!base_table) {
return;
}
/* If prefab has union relationships, also set them on instance */
if (base_table->flags & EcsTableHasUnion) {
const ecs_entity_t *entities = ecs_table_entities(table);
ecs_id_record_t *union_idr = flecs_id_record_get(world,
ecs_pair(EcsWildcard, EcsUnion));
ecs_assert(union_idr != NULL, ECS_INTERNAL_ERROR, NULL);
const ecs_table_record_t *tr = flecs_id_record_get_table(
union_idr, base_table);
ecs_assert(tr != NULL, ECS_INTERNAL_ERROR, NULL);
int32_t i = 0, j, union_count = 0;
do {
ecs_id_t id = base_table->type.array[i];
if (ECS_PAIR_SECOND(id) == EcsUnion) {
ecs_entity_t rel = ECS_PAIR_FIRST(id);
ecs_entity_t tgt = ecs_get_target(world, base, rel, 0);
ecs_assert(tgt != 0, ECS_INTERNAL_ERROR, NULL);
ecs_id_record_t *idr =
(ecs_id_record_t*)base_table->_->records[i].hdr.cache;
for (j = row; j < (row + count); j ++) {
flecs_switch_set(idr->sparse, (uint32_t)entities[j], tgt);
}
union_count ++;
}
i ++;
} while (union_count < tr->count);
}
if (!(base_table->flags & EcsTableIsPrefab)) {
/* Don't instantiate children from base entities that aren't prefabs */
return;
}
ecs_id_record_t *idr = flecs_id_record_get(world, ecs_childof(base));
ecs_table_cache_iter_t it;
if (idr && flecs_table_cache_all_iter((ecs_table_cache_t*)idr, &it)) {
ecs_os_perf_trace_push("flecs.instantiate");
const ecs_table_record_t *tr;
while ((tr = flecs_table_cache_next(&it, ecs_table_record_t))) {
flecs_instantiate_children(
world, base, table, row, count, tr->hdr.table, ctx);
}
ecs_os_perf_trace_pop("flecs.instantiate");
}
}
static
void flecs_sparse_on_add(
ecs_world_t *world,
ecs_table_t *table,
int32_t row,
int32_t count,
const ecs_type_t *added,
bool construct)
{
int32_t i, j;
for (i = 0; i < added->count; i ++) {
ecs_id_t id = added->array[i];
ecs_id_record_t *idr = flecs_id_record_get(world, id);
if (idr && idr->flags & EcsIdIsSparse) {
const ecs_type_info_t *ti = idr->type_info;
ecs_xtor_t ctor = ti->hooks.ctor;
ecs_iter_action_t on_add = ti->hooks.on_add;
const ecs_entity_t *entities = ecs_table_entities(table);
for (j = 0; j < count; j ++) {
ecs_entity_t e = entities[row + j];
void *ptr = flecs_sparse_ensure(idr->sparse, 0, e);
ecs_assert(ptr != NULL, ECS_INTERNAL_ERROR, NULL);
if (construct && ctor) {
ctor(ptr, 1, ti);
}
if (on_add) {
const ecs_table_record_t *tr =
flecs_id_record_get_table(idr, table);
flecs_invoke_hook(world, table, tr, count, row,
&entities[row + j],id, ti, EcsOnAdd, on_add);
}
}
}
}
}
static
void flecs_sparse_on_remove(
ecs_world_t *world,
ecs_table_t *table,
int32_t row,
int32_t count,
const ecs_type_t *removed)
{
int32_t i, j;
for (i = 0; i < removed->count; i ++) {
ecs_id_t id = removed->array[i];
ecs_id_record_t *idr = flecs_id_record_get(world, id);
if (idr && idr->flags & EcsIdIsSparse) {
const ecs_type_info_t *ti = idr->type_info;
const ecs_table_record_t *tr =
flecs_id_record_get_table(idr, table);
ecs_xtor_t dtor = ti->hooks.dtor;
ecs_iter_action_t on_remove = ti->hooks.on_remove;
const ecs_entity_t *entities = ecs_table_entities(table);
for (j = 0; j < count; j ++) {
ecs_entity_t e = entities[row + j];
if (on_remove) {
flecs_invoke_hook(world, table, tr, count, row,
&entities[row + j], id, ti, EcsOnRemove, on_remove);
}
void *ptr = flecs_sparse_remove_fast(idr->sparse, 0, e);
ecs_assert(ptr != NULL, ECS_INTERNAL_ERROR, NULL);
if (dtor) {
dtor(ptr, 1, ti);
}
}
}
}
}
static
void flecs_union_on_add(
ecs_world_t *world,
ecs_table_t *table,
int32_t row,
int32_t count,
const ecs_type_t *added)
{
int32_t i, j;
for (i = 0; i < added->count; i ++) {
ecs_id_t id = added->array[i];
if (ECS_IS_PAIR(id)) {
ecs_id_t wc = ecs_pair(ECS_PAIR_FIRST(id), EcsUnion);
ecs_id_record_t *idr = flecs_id_record_get(world, wc);
if (idr && idr->flags & EcsIdIsUnion) {
const ecs_entity_t *entities = ecs_table_entities(table);
for (j = 0; j < count; j ++) {
ecs_entity_t e = entities[row + j];
flecs_switch_set(
idr->sparse, (uint32_t)e, ecs_pair_second(world, id));
}
}
}
}
}
static
void flecs_union_on_remove(
ecs_world_t *world,
ecs_table_t *table,
int32_t row,
int32_t count,
const ecs_type_t *removed)
{
int32_t i, j;
for (i = 0; i < removed->count; i ++) {
ecs_id_t id = removed->array[i];
if (ECS_IS_PAIR(id)) {
ecs_id_t wc = ecs_pair(ECS_PAIR_FIRST(id), EcsUnion);
ecs_id_record_t *idr = flecs_id_record_get(world, wc);
if (idr && idr->flags & EcsIdIsUnion) {
const ecs_entity_t *entities = ecs_table_entities(table);
for (j = 0; j < count; j ++) {
ecs_entity_t e = entities[row + j];
flecs_switch_reset(idr->sparse, (uint32_t)e);
}
}
}
}
}
static
void flecs_notify_on_add(
ecs_world_t *world,
ecs_table_t *table,
ecs_table_t *other_table,
int32_t row,
int32_t count,
const ecs_table_diff_t *diff,
ecs_flags32_t flags,
ecs_flags64_t set_mask,
bool construct,
bool sparse)
{
ecs_assert(diff != NULL, ECS_INTERNAL_ERROR, NULL);
const ecs_type_t *added = &diff->added;
if (added->count) {
ecs_flags32_t diff_flags =
diff->added_flags|(table->flags & EcsTableHasTraversable);
if (!diff_flags) {
return;
}
if (sparse && (diff_flags & EcsTableHasSparse)) {
flecs_sparse_on_add(world, table, row, count, added, construct);
}
if (diff_flags & EcsTableHasUnion) {
flecs_union_on_add(world, table, row, count, added);
}
if (diff_flags & (EcsTableHasOnAdd|EcsTableHasTraversable)) {
flecs_emit(world, world, set_mask, &(ecs_event_desc_t){
.event = EcsOnAdd,
.ids = added,
.table = table,
.other_table = other_table,
.offset = row,
.count = count,
.observable = world,
.flags = flags
});
}
}
}
void flecs_notify_on_remove(
ecs_world_t *world,
ecs_table_t *table,
ecs_table_t *other_table,
int32_t row,
int32_t count,
const ecs_table_diff_t *diff)
{
ecs_assert(diff != NULL, ECS_INTERNAL_ERROR, NULL);
const ecs_type_t *removed = &diff->removed;
ecs_assert(count != 0, ECS_INTERNAL_ERROR, NULL);
if (removed->count) {
ecs_flags32_t diff_flags =
diff->removed_flags|(table->flags & EcsTableHasTraversable);
if (!diff_flags) {
return;
}
if (diff_flags & EcsTableHasUnion) {
flecs_union_on_remove(world, table, row, count, removed);
}
if (diff_flags & (EcsTableHasOnRemove|EcsTableHasTraversable)) {
flecs_emit(world, world, 0, &(ecs_event_desc_t) {
.event = EcsOnRemove,
.ids = removed,
.table = table,
.other_table = other_table,
.offset = row,
.count = count,
.observable = world
});
}
if (diff_flags & EcsTableHasSparse) {
flecs_sparse_on_remove(world, table, row, count, removed);
}
}
}
static
void flecs_update_name_index(
ecs_world_t *world,
ecs_table_t *src,
ecs_table_t *dst,
int32_t offset,
int32_t count)
{
ecs_assert(src != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_assert(dst != NULL, ECS_INTERNAL_ERROR, NULL);
if (!(dst->flags & EcsTableHasName)) {
/* If destination table doesn't have a name, we don't need to update the
* name index. Even if the src table had a name, the on_remove hook for
* EcsIdentifier will remove the entity from the index. */
return;
}
ecs_hashmap_t *src_index = src->_->name_index;
ecs_hashmap_t *dst_index = dst->_->name_index;
if ((src_index == dst_index) || (!src_index && !dst_index)) {
/* If the name index didn't change, the entity still has the same parent
* so nothing needs to be done. */
return;
}
EcsIdentifier *names = ecs_table_get_pair(world,
dst, EcsIdentifier, EcsName, offset);
ecs_assert(names != NULL, ECS_INTERNAL_ERROR, NULL);
int32_t i;
const ecs_entity_t *entities = &ecs_table_entities(dst)[offset];
for (i = 0; i < count; i ++) {
ecs_entity_t e = entities[i];
EcsIdentifier *name = &names[i];
uint64_t index_hash = name->index_hash;
if (index_hash) {
flecs_name_index_remove(src_index, e, index_hash);
}
const char *name_str = name->value;
if (name_str) {
ecs_assert(name->hash != 0, ECS_INTERNAL_ERROR, NULL);
flecs_name_index_ensure(
dst_index, e, name_str, name->length, name->hash);
name->index = dst_index;
}
}
}
static
ecs_record_t* flecs_new_entity(
ecs_world_t *world,
ecs_entity_t entity,
ecs_record_t *record,
ecs_table_t *table,
ecs_table_diff_t *diff,
bool ctor,
ecs_flags32_t evt_flags)
{
ecs_assert(record != NULL, ECS_INTERNAL_ERROR, NULL);
int32_t row = flecs_table_append(world, table, entity, ctor, true);
record->table = table;
record->row = ECS_ROW_TO_RECORD(row, record->row & ECS_ROW_FLAGS_MASK);
ecs_assert(ecs_table_count(table) > row, ECS_INTERNAL_ERROR, NULL);
flecs_notify_on_add(
world, table, NULL, row, 1, diff, evt_flags, 0, ctor, true);
ecs_assert(table == record->table, ECS_INTERNAL_ERROR, NULL);
return record;
}
static int commit_indent = 0;
static
void flecs_move_entity(
ecs_world_t *world,
ecs_entity_t entity,
ecs_record_t *record,
ecs_table_t *dst_table,
ecs_table_diff_t *diff,
bool ctor,
ecs_flags32_t evt_flags)
{
ecs_table_t *src_table = record->table;
int32_t src_row = ECS_RECORD_TO_ROW(record->row);
ecs_assert(src_table != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_assert(src_table != dst_table, ECS_INTERNAL_ERROR, NULL);
ecs_assert(src_table->type.count > 0, ECS_INTERNAL_ERROR, NULL);
ecs_assert(src_row >= 0, ECS_INTERNAL_ERROR, NULL);
ecs_assert(ecs_table_count(src_table) > src_row, ECS_INTERNAL_ERROR, NULL);
ecs_check(ecs_is_alive(world, entity), ECS_INVALID_PARAMETER, NULL);
ecs_assert(record != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_assert(record == flecs_entities_get(world, entity),
ECS_INTERNAL_ERROR, NULL);
ecs_assert(record->table == src_table, ECS_INTERNAL_ERROR, NULL);
/* Append new row to destination table */
int32_t dst_row = flecs_table_append(world, dst_table, entity,
false, false);
/* Invoke remove actions for removed components */
flecs_notify_on_remove(world, src_table, dst_table, src_row, 1, diff);
/* Copy entity & components from src_table to dst_table */
flecs_table_move(world, entity, entity, dst_table, dst_row,
src_table, src_row, ctor);
ecs_assert(record->table == src_table, ECS_INTERNAL_ERROR, NULL);
/* Update entity index & delete old data after running remove actions */
record->table = dst_table;
record->row = ECS_ROW_TO_RECORD(dst_row, record->row & ECS_ROW_FLAGS_MASK);
flecs_table_delete(world, src_table, src_row, false);
flecs_notify_on_add(world, dst_table, src_table, dst_row, 1, diff,
evt_flags, 0, ctor, true);
flecs_update_name_index(world, src_table, dst_table, dst_row, 1);
ecs_assert(record->table == dst_table, ECS_INTERNAL_ERROR, NULL);
error:
return;
}
static
void flecs_delete_entity(
ecs_world_t *world,
ecs_record_t *record,
ecs_table_diff_t *diff)
{
ecs_table_t *table = record->table;
int32_t row = ECS_RECORD_TO_ROW(record->row);
/* Invoke remove actions before deleting */
flecs_notify_on_remove(world, table, NULL, row, 1, diff);
flecs_table_delete(world, table, row, true);
}
/* Updating component monitors is a relatively expensive operation that only
* happens for entities that are monitored. The approach balances the amount of
* processing between the operation on the entity vs the amount of work that
* needs to be done to rematch queries, as a simple brute force approach does
* not scale when there are many tables / queries. Therefore we need to do a bit
* of bookkeeping that is more intelligent than simply flipping a flag */
static
void flecs_update_component_monitor_w_array(
ecs_world_t *world,
ecs_type_t *ids)
{
if (!ids) {
return;
}
int i;
for (i = 0; i < ids->count; i ++) {
ecs_entity_t id = ids->array[i];
if (ECS_HAS_ID_FLAG(id, PAIR)) {
flecs_monitor_mark_dirty(world,
ecs_pair(ECS_PAIR_FIRST(id), EcsWildcard));
}
flecs_monitor_mark_dirty(world, id);
}
}
static
void flecs_update_component_monitors(
ecs_world_t *world,
ecs_type_t *added,
ecs_type_t *removed)
{
flecs_update_component_monitor_w_array(world, added);
flecs_update_component_monitor_w_array(world, removed);
}
static
void flecs_commit(
ecs_world_t *world,
ecs_entity_t entity,
ecs_record_t *record,
ecs_table_t *dst_table,
ecs_table_diff_t *diff,
bool construct,
ecs_flags32_t evt_flags)
{
ecs_assert(!(world->flags & EcsWorldReadonly), ECS_INTERNAL_ERROR, NULL);
flecs_journal_begin(world, EcsJournalMove, entity,
&diff->added, &diff->removed);
ecs_table_t *src_table = NULL;
int is_trav = 0;
if (record) {
src_table = record->table;
is_trav = (record->row & EcsEntityIsTraversable) != 0;
}
if (src_table == dst_table) {
/* If source and destination table are the same no action is needed *
* However, if a component was added in the process of traversing a
* table, this suggests that a union relationship could have changed. */
if (src_table && src_table->flags & EcsTableHasUnion) {
diff->added_flags |= EcsIdIsUnion;
flecs_notify_on_add(world, src_table, src_table,
ECS_RECORD_TO_ROW(record->row), 1, diff, evt_flags, 0,
construct, true);
}
flecs_journal_end();
return;
}
commit_indent += 2;
ecs_os_perf_trace_push("flecs.commit");
if (src_table) {
ecs_assert(dst_table != NULL, ECS_INTERNAL_ERROR, NULL);
flecs_table_traversable_add(dst_table, is_trav);
if (dst_table->type.count) {
flecs_move_entity(world, entity, record, dst_table, diff,
construct, evt_flags);
} else {
flecs_delete_entity(world, record, diff);