-
Notifications
You must be signed in to change notification settings - Fork 32
/
luaecs.c
2454 lines (2293 loc) · 59.4 KB
/
luaecs.c
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
#define LUA_LIB
#include <lua.h>
#include <lauxlib.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "luaecs.h"
#include "ecs_group.h"
#include "ecs_internal.h"
#include "ecs_persistence.h"
#include "ecs_template.h"
#include "ecs_capi.h"
#include "ecs_cache.h"
static unsigned int
new_lua_component_id(lua_State *L, struct entity_world *w) {
unsigned int r = w->lua.freelist;
if (r != 0) {
lua_State *tL = w->lua.L;
if (lua_rawgeti(tL, 1, r) != LUA_TNUMBER) {
lua_pop(tL, 1);
return luaL_error(L, "Invalid lua component table[%d]", r);
}
unsigned int next = (unsigned int)lua_tointeger(L, -1);
lua_pop(tL, 1);
w->lua.freelist = next;
return r;
} else {
unsigned int n = ++w->lua.cap;
return n;
}
}
static inline int
mainkey_istag(struct group_iter *iter) {
int mainkey = iter->k[0].id;
return (mainkey >= 0 && iter->world->c[mainkey].stride == STRIDE_TAG);
}
static void
remove_lua_component(struct entity_world *w, unsigned int idx) {
lua_State *L = w->lua.L;
lua_pushinteger(L, w->lua.freelist);
lua_rawseti(L, 1, idx);
w->lua.freelist = idx;
}
static void
set_lua_component(lua_State *L, struct entity_world *w, struct component_pool *c, int index) {
lua_State *tL = w->lua.L;
lua_xmove(L, tL, 1);
unsigned int *lua_index = (unsigned int *)c->buffer;
lua_rawseti(tL, 1, lua_index[index]);
}
static void
new_lua_component(lua_State *L, struct entity_world *w, struct component_pool *c, int index) {
unsigned int idx = new_lua_component_id(L, w);
unsigned int *lua_index = (unsigned int *)c->buffer;
lua_index[index] = idx;
lua_State *tL = w->lua.L;
lua_xmove(L, tL, 1);
lua_rawseti(tL, 1, idx);
}
static void
get_lua_component(lua_State *L, struct entity_world *w, struct component_pool *c, int index) {
if (index < 0) {
lua_pushnil(L);
} else {
lua_State *tL = w->lua.L;
unsigned int *lua_index = (unsigned int *)c->buffer;
lua_rawgeti(tL, 1, lua_index[index]);
lua_xmove(tL, L, 1);
}
}
static void
init_component_pool(struct entity_world *w, int index, int stride, int opt_size) {
struct component_pool *c = &w->c[index];
c->cap = opt_size;
c->n = 0;
c->stride = stride;
c->id = NULL;
c->last_lookup = 0;
if (stride != STRIDE_TAG) {
c->buffer = NULL;
} else {
c->buffer = DUMMY_PTR;
}
}
static void
entity_new_type(lua_State *L, int world_index, int cid, int stride, int opt_size) {
struct entity_world *w = (struct entity_world *)lua_touserdata(L, world_index);
if (opt_size <= 0) {
opt_size = DEFAULT_SIZE;
}
if (cid < 0 || cid >= MAX_COMPONENT || w->c[cid].cap != 0) {
luaL_error(L, "Can't new type %d", cid);
}
init_component_pool(w, cid, stride, opt_size);
}
static int
lnew_type(lua_State *L) {
int cid = luaL_checkinteger(L, 2);
int stride = luaL_checkinteger(L, 3);
int size = luaL_optinteger(L, 4, 0);
entity_new_type(L, 1, cid, stride, size);
return 0;
}
static int
lcount_memory(lua_State *L) {
struct entity_world *w = getW(L);
// count eid
size_t sz = sizeof(*w);
sz += entity_id_memsize(&w->eid);
sz += entity_group_memsize_(&w->group);
int i;
size_t msz = sz;
for (i = 0; i < MAX_COMPONENT; i++) {
struct component_pool *c = &w->c[i];
if (c->id) {
sz += c->cap * sizeof(entity_index_t);
msz += c->n * sizeof(entity_index_t);
}
if (c->buffer != DUMMY_PTR) {
int stride = c->stride;
if (stride == STRIDE_LUA)
stride = sizeof(unsigned int);
sz += c->cap * stride;
msz += c->n * stride;
}
}
lua_pushinteger(L, sz);
lua_pushinteger(L, msz);
return 2;
}
static void
init_buffers(struct component_pool *pool) {
int cap = pool->cap;
int stride = pool->stride;
if (stride <= 0) {
// only id
size_t id_sz = sizeof(entity_index_t) * cap;
pool->id = (entity_index_t *)malloc(id_sz);
if (stride == STRIDE_LUA) {
stride = sizeof(unsigned int);
} else {
return;
}
}
size_t offset = stride * cap;
size_t sz = offset + sizeof(entity_index_t) * cap;
pool->buffer = malloc(sz);
pool->id = (entity_index_t *)((uint8_t *)pool->buffer + offset);
}
static void
move_buffers(struct component_pool *pool, void *buffer, entity_index_t *id) {
memcpy(pool->id, id, pool->n * sizeof(entity_index_t));
int stride = pool->stride;
if (stride <= 0) {
if (stride == STRIDE_LUA) {
stride = sizeof(unsigned int);
} else {
free(id);
return;
}
}
memcpy(pool->buffer, buffer, pool->n * stride);
free(buffer);
}
static void
free_buffers(struct component_pool *c) {
int stride = c->stride;
if (stride <= 0) {
if (stride == STRIDE_LUA) {
stride = sizeof(unsigned int);
} else {
free(c->id);
c->id = NULL;
return;
}
}
free(c->buffer);
c->buffer = NULL;
c->id = NULL;
}
void
ecs_reserve_eid_(struct entity_world *w, int n) {
if (w->eid.cap >= n) {
return;
}
free(w->eid.id);
w->eid.id = (uint64_t *)malloc(n * sizeof(uint64_t));
w->eid.n = 0;
w->eid.cap = n;
}
void
ecs_reserve_component_(struct component_pool *pool, int cid, int cap) {
if (pool->n == 0) {
if (cap > pool->cap) {
free_buffers(pool);
pool->cap = cap;
}
if (pool->id == NULL) {
init_buffers(pool);
}
} else if (cap > pool->cap) {
pool->cap = cap;
void *buffer = pool->buffer;
entity_index_t *id = pool->id;
init_buffers(pool);
move_buffers(pool, buffer, id);
}
}
static void
shrink_component_pool(lua_State *L, struct component_pool *c, int cid) {
if (c->id == NULL)
return;
ecs_reserve_component_(c, cid, c->n);
}
static int
lcollect_memory(lua_State *L) {
struct entity_world *w = getW(L);
int i;
for (i = 0; i < MAX_COMPONENT; i++) {
shrink_component_pool(L, &w->c[i], i);
}
return 0;
}
static inline void
expand_pool(struct component_pool *pool) {
int cap = pool->cap;
if (pool->n == 0) {
if (pool->id == NULL) {
init_buffers(pool);
}
} else if (pool->n >= pool->cap) {
// expand pool
pool->cap = cap * 3 / 2 + 1;
void *buffer = pool->buffer;
entity_index_t *id = pool->id;
init_buffers(pool);
move_buffers(pool, buffer, id);
}
}
static inline int
add_component_id_(struct component_pool *pool, int cid, entity_index_t eid) {
expand_pool(pool);
int index = pool->n;
int cmp;
if (index > 0 && (cmp = ENTITY_INDEX_CMP(pool->id[index-1], eid)) >= 0) {
do {
if (cmp == 0) {
if (pool->stride == STRIDE_TAG)
return index - 1;
return -1;
}
--index;
} while (index > 0 && (cmp = ENTITY_INDEX_CMP(pool->id[index-1], eid)) >= 0);
// move [index, pool->n) -> [index+1, pool->n]
memmove(&pool->id[index+1], &pool->id[index], (pool->n - index) * sizeof(pool->id[0]));
int stride = pool->stride;
if (stride == STRIDE_LUA)
stride = sizeof(unsigned int);
if (stride > 0) {
memmove((uint8_t *)pool->buffer + (index+1) * stride,
(uint8_t *)pool->buffer + index * stride,
(pool->n - index) * stride);
}
}
pool->id[index] = eid;
++pool->n;
return index;
}
int
ecs_add_component_id_(struct entity_world *w, int cid, entity_index_t eid) {
struct component_pool *pool = &w->c[cid];
return add_component_id_(pool, cid, eid);
}
void *
entity_component_temp_(struct entity_world *w, int tag, int cid) {
struct component_pool *t = &w->c[tag];
struct component_pool *pool = &w->c[cid];
if (pool->n == t->n) {
if (entity_new_(w, tag, NULL) < 0)
return NULL;
} else if (pool->n > t->n) {
return NULL;
}
expand_pool(pool);
int index = pool->n++;
pool->id[index] = t->id[index];
return get_ptr(pool, index);
}
entity_index_t
ecs_new_entityid_(struct entity_world *w) {
struct entity_id *e = &w->eid;
uint64_t eid;
int n = entity_id_alloc(e, &eid);
if (n < 0)
return INVALID_ENTITY;
return make_index_(n);
}
static int
lnew_entity(lua_State *L) {
struct entity_world *w = getW(L);
entity_index_t n = ecs_new_entityid_(w);
if (INVALID_ENTITY_INDEX(n)) {
return luaL_error(L, "Too many entities");
}
int index = index_(n);
lua_pushinteger(L, w->eid.id[index]);
lua_pushinteger(L, index);
return 2;
}
static int
binary_search(entity_index_t *a, int from, int to, uint32_t v) {
while (from < to) {
int mid = (from + to) / 2;
uint32_t aa = index_(a[mid]);
if (aa == v)
return mid;
else if (aa < v) {
from = mid + 1;
} else {
to = mid;
}
}
return -1;
}
#define GUESS_RANGE 64
static inline int
search_after(struct component_pool *pool, uint32_t eid, int from_index) {
entity_index_t *a = pool->id;
if (from_index + GUESS_RANGE * 2 >= pool->n) {
return binary_search(a, from_index + 1, pool->n, eid);
}
entity_index_t higher_index = a[from_index + GUESS_RANGE];
uint32_t higher = index_(higher_index);
if (eid > higher) {
return binary_search(a, from_index + GUESS_RANGE + 1, pool->n, eid);
}
return binary_search(a, from_index + 1, from_index + GUESS_RANGE + 1, eid);
}
int
ecs_lookup_component_(struct component_pool *pool, entity_index_t eindex, int guess_index) {
int n = pool->n;
if (n == 0)
return -1;
uint32_t eid = index_(eindex);
if (guess_index < 0 || guess_index >= pool->n)
return binary_search(pool->id, 0, pool->n, eid);
entity_index_t *a = pool->id;
entity_index_t lower_index = a[guess_index];
uint32_t lower = index_(lower_index);
if (eid <= lower) {
if (eid == lower)
return guess_index;
return binary_search(a, 0, guess_index, eid);
}
return search_after(pool, eid, guess_index);
}
static inline int
lookup_component_from(struct component_pool *pool, entity_index_t eindex, int from_index) {
int n = pool->n;
if (from_index >= n)
return -1;
uint32_t eid = index_(eindex);
entity_index_t *a = pool->id;
uint32_t from_id = index_(a[from_index]);
if (eid <= from_id) {
if (eid == from_id)
return from_index;
return -1;
}
return search_after(pool, eid, from_index);
}
static inline void
move_tag(struct component_pool *pool, int from, int to, int delta) {
pool->id[to] = DEC_ENTITY_INDEX(pool->id[from], delta);
}
static inline void
move_item(struct component_pool *pool, int from, int to, int delta) {
pool->id[to] = DEC_ENTITY_INDEX(pool->id[from], delta);
if (from != to) {
int stride = pool->stride;
memcpy((char *)pool->buffer + to * stride, (char *)pool->buffer + from * stride, stride);
}
}
static inline void
move_lua(struct component_pool *pool, int from, int to, int delta) {
pool->id[to] = DEC_ENTITY_INDEX(pool->id[from], delta);
if (from != to) {
int stride = sizeof(unsigned int);
memcpy((char *)pool->buffer + to * stride, (char *)pool->buffer + from * stride, stride);
}
}
static void
remove_entityid(struct entity_world *w, struct component_pool *removed) {
int n = removed->n;
if (n == 0)
return;
int i;
uint32_t last = index_(removed->id[0]);
uint32_t offset = last;
uint64_t *eid = w->eid.id;
for (i=1;i<n;i++) {
uint32_t next = index_(removed->id[i]);
uint32_t t = next - last - 1;
memmove(eid+offset, eid+last+1, t * sizeof(uint64_t));
offset += t;
last = next;
}
uint32_t t = w->eid.n - last - 1;
memmove(eid+offset, eid+last+1, t * sizeof(uint64_t));
w->eid.n -= removed->n;
}
// return the biggset index less than v, or [index] = v:
// c : [ 1 3 5 7 ], v = 4, from = 0, return 2 (1 3)
// c : [ 1 3 5 7 ], v = 3, from = 1, return 1 (1)
static int
less_part(struct component_pool *c, entity_index_t vidx, int from) {
if (from >= c->n)
return c->n;
entity_index_t *id = c->id;
uint32_t v = index_(vidx);
if (v <= index_(id[from]))
return from;
int begin = from;
int end = c->n;
while (begin < end) {
int mid = (begin + end) / 2;
uint32_t p = index_(id[mid]);
if (p < v)
begin = mid + 1;
else
end = mid;
}
return begin;
}
static void
remove_all(lua_State *L, struct entity_world *w, struct component_pool *removed, int cid) {
struct component_pool *pool = &w->c[cid];
if (pool->n == 0)
return;
entity_index_t *removed_id = removed->id;
if (ENTITY_INDEX_CMP(removed_id[0], pool->id[pool->n-1]) > 0) {
// No action, because removed_id[0] is bigger than the biggest index in pool
return;
}
if (ENTITY_INDEX_CMP(pool->id[0], removed_id[removed->n-1]) > 0) {
// No removed components, but the id in pool should -= removed->n
int n = removed->n;
int i;
for (i=0;i<pool->n;i++) {
pool->id[i] = DEC_ENTITY_INDEX(pool->id[i], n);
}
return;
}
int removed_n = less_part(removed, pool->id[0], 0);
int i = 0;
if (removed_n == 0) {
// removed[0] is less than pool[0], find the start point of pool
i = less_part(pool, removed_id[0], 0);
}
int index = i;
int delta = 0;
unsigned int * lua_index;
switch (pool->stride) {
case STRIDE_LUA:
lua_index = (unsigned int *)pool->buffer;
while (i < pool->n && removed_n < removed->n) {
int cmp = ENTITY_INDEX_CMP(pool->id[i], removed_id[removed_n]);
if (cmp == 0) {
// pool[i] should be removed
remove_lua_component(w, lua_index[i]);
++removed_n;
++delta;
++i;
} else if (cmp < 0) {
// pool[i] < current removed
move_lua(pool, i, index, removed_n);
++index;
++i;
} else {
// pool[i] > current removed, find next removed
removed_n = less_part(removed, pool->id[i], removed_n);
}
}
for (;i<pool->n;i++) {
move_lua(pool, i, index, removed_n);
++index;
}
break;
case STRIDE_TAG: {
entity_index_t last = INVALID_ENTITY;
while (i < pool->n && removed_n < removed->n) {
if (ENTITY_INDEX_CMP(pool->id[i], last) == 0) {
// remove duplicate
++i;
} else {
int cmp = ENTITY_INDEX_CMP(pool->id[i], removed_id[removed_n]);
if (cmp == 0) {
// pool[i] should be removed
++delta;
++i;
} else if (cmp < 0) {
// pool[i] < current removed
last = pool->id[i];
move_tag(pool, i, index, removed_n);
++index;
++i;
} else {
// pool[i] > current removed, find next removed
removed_n = less_part(removed, pool->id[i], removed_n);
}
}
}
for (;i<pool->n;i++) {
move_tag(pool, i, index, removed_n);
++index;
}
break; }
default:
while (i < pool->n && removed_n < removed->n) {
int cmp = ENTITY_INDEX_CMP(pool->id[i], removed_id[removed_n]);
if (cmp == 0) {
// pool[i] should be removed
++removed_n;
++delta;
++i;
} else if (cmp < 0) {
// pool[i] < current removed
move_item(pool, i, index, removed_n);
++index;
++i;
} else {
// pool[i] > current removed, find next removed
removed_n = less_part(removed, pool->id[i], removed_n);
}
}
for (;i<pool->n;i++) {
move_item(pool, i, index, removed_n);
++index;
}
break;
}
pool->n -= delta;
}
static int
ladd_component(lua_State *L) {
struct entity_world *w = getW(L);
uint32_t n = luaL_checkinteger(L, 2);
if (n >= MAX_ENTITY)
return luaL_error(L, "Invalid entity index %u", n);
entity_index_t eid = make_index_(n);
int cid = check_cid(L, w, 3);
int index = ecs_add_component_id_(w, cid, eid);
if (index < 0)
return luaL_error(L, "Component %d exist", cid);
struct component_pool *c = &w->c[cid];
if (c->stride == STRIDE_LUA) {
lua_pushnil(L);
new_lua_component(L, w, c, index);
}
lua_pushinteger(L, index + 1);
return 1;
}
static int
lfind_component(lua_State *L) {
struct entity_world *w = getW(L);
uint32_t n = luaL_checkinteger(L, 2);
if (n >= MAX_ENTITY)
return luaL_error(L, "Invalid entity index %u", n);
struct ecs_token t = { n };
int cid = check_cid(L, w, 3);
int index = entity_component_index_(w, t, cid);
if (index < 0)
return luaL_error(L, "Component %d [%d] not found", cid, n);
lua_pushinteger(L, index + 1);
return 1;
}
static int
ladd_temp(lua_State *L) {
struct entity_world *w = getW(L);
int tagid = check_cid(L, w, 2);
int cid = check_cid(L, w, 3);
if (entity_component_temp_(w, tagid, cid) == NULL) {
return luaL_error(L, "Add temp component fail");
}
struct component_pool *c = &w->c[cid];
int index = c->n;
if (c->stride == STRIDE_LUA) {
lua_pushnil(L);
new_lua_component(L, w, c, index - 1);
}
lua_pushinteger(L, index);
return 1;
}
static int
lupdate(lua_State *L) {
struct entity_world *w = getW(L);
int removed_id = luaL_optinteger(L, 2, ENTITY_REMOVED);
struct component_pool *removed = &w->c[removed_id];
int i;
if (removed->n > 0) {
// mark removed
for (i = 0; i < MAX_COMPONENT; i++) {
if (i != removed_id)
remove_all(L, w, removed, i);
}
remove_entityid(w, removed);
removed->n = 0;
}
return 0;
}
static int
lclear_type(lua_State *L) {
struct entity_world *w = getW(L);
int cid = check_cid(L, w, 2);
entity_clear_type_(w, cid);
return 0;
}
static int
lcontext(lua_State *L) {
struct entity_world *w = getW(L);
struct ecs_context *ctx = (struct ecs_context *)lua_newuserdatauv(L, sizeof(struct ecs_context), 1);
lua_pushvalue(L, 1);
lua_setiuservalue(L, -2, 1);
ctx->world = w;
static struct ecs_capi c_api = {
entity_fetch_,
entity_clear_type_,
entity_component_,
entity_component_index_,
entity_component_add_,
entity_component_temp_,
entity_new_,
entity_remove_,
entity_enable_tag_,
entity_disable_tag_,
entity_next_tag_,
entity_get_lua_,
entity_group_enable_,
entity_count_,
entity_index_,
entity_propagate_tag_,
ecs_cache_create,
ecs_cache_release,
ecs_cache_fetch,
ecs_cache_fetch_index,
ecs_cache_sync,
};
ctx->api = &c_api;
return 1;
}
static int
lnew_world(lua_State *L) {
size_t sz = sizeof(struct entity_world);
struct entity_world *w = (struct entity_world *)lua_newuserdatauv(L, sz, 1);
memset(w, 0, sz);
w->lua.L = lua_newthread(L);
lua_newtable(w->lua.L); // table for all lua components
lua_setiuservalue(L, -2, 1);
// removed set
int world_index = lua_gettop(L);
entity_new_type(L, world_index, ENTITY_REMOVED, 0, 0);
luaL_checktype(L, 1, LUA_TTABLE);
lua_pushvalue(L, 1);
lua_setmetatable(L, -2);
return 1;
}
static int
ldeinit_world(lua_State *L) {
struct entity_world *w = lua_touserdata(L, 1);
entity_group_deinit_(&w->group);
entity_id_deinit(&w->eid);
int i;
for (i=0;i<MAX_COMPONENT;i++) {
struct component_pool *c = &w->c[i];
if (c->stride != STRIDE_TAG) {
free(c->buffer);
c->buffer = NULL;
c->id = NULL;
} else {
free(c->id);
c->id = NULL;
}
}
return 0;
}
static int
lreset_world(lua_State *L) {
struct entity_world *w = lua_touserdata(L, 1);
entity_group_deinit_(&w->group);
entity_id_deinit(&w->eid);
memset(&w->group, 0, sizeof(w->group));
memset(&w->eid, 0, sizeof(w->eid));
lua_settop(w->lua.L, 0);
lua_newtable(w->lua.L);
w->lua.freelist = 0;
w->lua.cap = 0;
int i;
for (i=0;i<MAX_COMPONENT;i++) {
struct component_pool *c = &w->c[i];
c->n = 0;
}
return 0;
}
static const int
sizeof_type[TYPE_COUNT] = {
sizeof(int),
sizeof(float),
sizeof(char),
sizeof(int64_t),
sizeof(uint32_t), // DWORD
sizeof(uint16_t), // WORD
sizeof(uint8_t), // BYTE
sizeof(double),
sizeof(void *), // USERDATA
};
static int
check_type(lua_State *L) {
int type = lua_tointeger(L, -1);
if (type < 0 || type >= TYPE_COUNT) {
luaL_error(L, "Invalid field type(%d)", type);
}
lua_pop(L, 1);
return type;
}
static void
get_name(lua_State *L, char * name, int index) {
size_t sz;
const char * s = lua_tolstring(L, index, &sz);
if (sz >= MAX_COMPONENT_NAME)
luaL_error(L, "string '%s' is too long", s);
memcpy(name, s, sz+1);
}
static void
get_field(lua_State *L, int i, struct group_field *f) {
if (lua_geti(L, -1, 1) != LUA_TNUMBER) {
luaL_error(L, "Invalid field %d [1] type", i);
}
f->type = check_type(L);
if (lua_geti(L, -1, 2) != LUA_TSTRING) {
luaL_error(L, "Invalid field %d [2] key", i);
}
get_name(L, f->key, -1);
lua_pop(L, 1);
if (lua_geti(L, -1, 3) != LUA_TNUMBER) {
luaL_error(L, "Invalid field %d [3] offset", i);
}
f->offset = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_pop(L, 1);
}
static inline const char *
field_name(struct group_field *f) {
return f->key[0] ? f->key : "*";
}
static void
write_value(lua_State *L, struct group_field *f, char *buffer) {
int luat = lua_type(L, -1);
char *ptr = buffer + f->offset;
switch (f->type) {
case TYPE_INT:
if (!lua_isinteger(L, -1))
luaL_error(L, "Invalid .%s type %s (int)", field_name(f), lua_typename(L, luat));
*(int *)ptr = lua_tointeger(L, -1);
break;
case TYPE_FLOAT:
if (luat != LUA_TNUMBER)
luaL_error(L, "Invalid .%s type %s (float)", field_name(f), lua_typename(L, luat));
*(float *)ptr = lua_tonumber(L, -1);
break;
case TYPE_BOOL:
if (luat != LUA_TBOOLEAN)
luaL_error(L, "Invalid .%s type %s (bool)", field_name(f), lua_typename(L, luat));
*(unsigned char *)ptr = lua_toboolean(L, -1);
break;
case TYPE_INT64:
if (!lua_isinteger(L, -1))
luaL_error(L, "Invalid .%s type %s (int64)", field_name(f), lua_typename(L, luat));
*(int64_t *)ptr = lua_tointeger(L, -1);
break;
case TYPE_DWORD:
if (!lua_isinteger(L, -1))
luaL_error(L, "Invalid .%s type %s (uint32)", field_name(f), lua_typename(L, luat));
else {
int64_t v = lua_tointeger(L, -1);
if (v < 0 || v > 0xffffffff) {
luaL_error(L, "Invalid .%s (DWORD) %I", field_name(f), v);
}
*(uint32_t *)ptr = v;
}
break;
case TYPE_WORD:
if (!lua_isinteger(L, -1))
luaL_error(L, "Invalid .%s type %s (uint16)", field_name(f), lua_typename(L, luat));
else {
int v = lua_tointeger(L, -1);
if (v < 0 || v > 0xffff) {
luaL_error(L, "Invalid .%s (WORD) %I", field_name(f), v);
}
*(uint16_t *)ptr = v;
}
break;
case TYPE_BYTE:
if (!lua_isinteger(L, -1))
luaL_error(L, "Invalid .%s type %s (uint8)", field_name(f), lua_typename(L, luat));
else {
int v = lua_tointeger(L, -1);
if (v < 0 || v > 255) {
luaL_error(L, "Invalid .%s (BYTE) %I", field_name(f), v);
}
*(uint8_t *)ptr = v;
}
break;
case TYPE_DOUBLE:
if (luat != LUA_TNUMBER)
luaL_error(L, "Invalid .%s type %s (double)", field_name(f), lua_typename(L, luat));
*(double *)ptr = lua_tonumber(L, -1);
break;
case TYPE_USERDATA:
if (luat != LUA_TLIGHTUSERDATA)
luaL_error(L, "Invalid .%s type %s (pointer)", field_name(f), lua_typename(L, luat));
*(void **)ptr = lua_touserdata(L, -1);
break;
}
lua_pop(L, 1);
}
static void
write_value_check(lua_State *L, struct group_field *f, const char *buffer, const char *name) {
struct group_field tmp_f = *f;
tmp_f.offset = 0;
char tmp[sizeof(void *)];
write_value(L, &tmp_f, tmp);
if (memcmp(buffer + f->offset, tmp, sizeof_type[f->type]) != 0) {
if (f->key[0]) {
luaL_error(L, "[%s.%s] changes", name, f->key);
} else {
luaL_error(L, "[%s] changes", name);
}
}
}
static inline void
write_component(lua_State *L, int field_n, struct group_field *f, int index, char *buffer) {
int i;
for (i = 0; i < field_n; i++) {
lua_getfield(L, index, f[i].key);
write_value(L, &f[i], buffer);
}
}
static inline void
write_component_check(lua_State *L, int field_n, struct group_field *f, int index, const char *buffer, const char *name) {
int i;
for (i = 0; i < field_n; i++) {
lua_getfield(L, index, f[i].key);
write_value_check(L, &f[i], buffer, name);
}
}
static void
read_value(lua_State *L, struct group_field *f, const char *buffer) {
const char *ptr = buffer + f->offset;
switch (f->type) {
case TYPE_INT:
lua_pushinteger(L, *(const int *)ptr);
break;
case TYPE_FLOAT:
lua_pushnumber(L, *(const float *)ptr);
break;
case TYPE_BOOL:
lua_pushboolean(L, *ptr);
break;
case TYPE_INT64:
lua_pushinteger(L, *(const int64_t *)ptr);
break;
case TYPE_DWORD:
lua_pushinteger(L, *(const uint32_t *)ptr);
break;
case TYPE_WORD:
lua_pushinteger(L, *(const uint16_t *)ptr);
break;
case TYPE_BYTE:
lua_pushinteger(L, *(const uint8_t *)ptr);
break;
case TYPE_DOUBLE:
lua_pushnumber(L, *(const double *)ptr);
break;
case TYPE_USERDATA:
lua_pushlightuserdata(L, *(void **)ptr);
break;
default:
// never here
luaL_error(L, "Invalid field type %d", f->type);
break;
}
}
static void
read_component(lua_State *L, int field_n, struct group_field *f, int index, const char *buffer) {
int i;
for (i = 0; i < field_n; i++) {
read_value(L, &f[i], buffer);
lua_setfield(L, index, f[i].key);
}
}
static int
get_len(lua_State *L, int index) {
lua_len(L, index);
if (lua_type(L, -1) != LUA_TNUMBER) {
return luaL_error(L, "Invalid table length");
}
int n = lua_tointeger(L, -1);
if (n < 0) {
return luaL_error(L, "Invalid table length %d", n);
}
lua_pop(L, 1);
return n;
}
#define COMPONENT_IN 1
#define COMPONENT_OUT 2
#define COMPONENT_OPTIONAL 4
#define COMPONENT_OBJECT 8
#define COMPONENT_EXIST 0x10
#define COMPONENT_ABSENT 0x20
#define COMPONENT_FILTER (COMPONENT_EXIST | COMPONENT_ABSENT)
static inline int
is_temporary(int attrib) {
if (attrib & COMPONENT_FILTER)
return 0;
return (attrib & COMPONENT_IN) == 0 && (attrib & COMPONENT_OUT) == 0;
}
static int
get_write_component(lua_State *L, int lua_index, const char *name, struct group_field *f, struct component_pool *c) {
switch (lua_getfield(L, lua_index, name)) {
case LUA_TNIL:
lua_pop(L, 1);