-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
arch.go
502 lines (419 loc) · 13.6 KB
/
arch.go
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
package ecs
import (
"fmt"
)
// This is the identifier for entities in the world
//cod:struct
type Id uint32
type archetypeId uint32
type componentSlice[T any] struct {
comp []T
}
// Note: This will panic if you write past the buffer by more than 1
func (s *componentSlice[T]) Write(index int, val T) {
if index == len(s.comp) {
// Case: index causes a single append (new element added)
s.comp = append(s.comp, val)
} else {
// Case: index is inside the length
// Edge: (Causes Panic): Index is greater than 1 plus length
s.comp[index] = val
}
}
// TODO: Rename, this is kind of like an archetype header
type lookupList struct {
index *internalMap[Id,int] // A mapping from entity ids to array indices
id []Id // An array of every id in the arch list (essentially a reverse mapping from index to Id)
holes []int // List of indexes that have ben deleted
mask archetypeMask
}
// Adds ourselves to the last available hole, else appends
// Returns the index
func (l *lookupList) addToEasiestHole(id Id) int {
if len(l.holes) > 0 {
lastHoleIndex := len(l.holes)-1
index := l.holes[lastHoleIndex]
l.id[index] = id
l.index.Put(id, index)
l.holes = l.holes[:lastHoleIndex]
return index
} else {
// Because the Id hasn't been added to this arch, we need to append it to the end
l.id = append(l.id, id)
index := len(l.id) - 1
l.index.Put(id, index)
return index
}
}
type storage interface {
ReadToEntity(*Entity, archetypeId, int) bool
ReadToRawEntity(*RawEntity, archetypeId, int) bool
Delete(archetypeId, int)
print(int)
}
type componentSliceStorage[T any] struct {
slice map[archetypeId]*componentSlice[T]
}
func (ss *componentSliceStorage[T]) ReadToEntity(entity *Entity, archId archetypeId, index int) bool {
cSlice, ok := ss.slice[archId]
if !ok {
return false
}
entity.Add(C(cSlice.comp[index]))
return true
}
func (ss *componentSliceStorage[T]) ReadToRawEntity(entity *RawEntity, archId archetypeId, index int) bool {
cSlice, ok := ss.slice[archId]
if !ok {
return false
}
entity.Add(&cSlice.comp[index])
return true
}
// Delete is somewhat special because it deletes the index of the archId for the componentSlice
// but then plugs the hole by pushing the last element of the componentSlice into index
func (ss *componentSliceStorage[T]) Delete(archId archetypeId, index int) {
cSlice, ok := ss.slice[archId]
if !ok {
return
}
lastVal := cSlice.comp[len(cSlice.comp)-1]
cSlice.comp[index] = lastVal
cSlice.comp = cSlice.comp[:len(cSlice.comp)-1]
}
func (s *componentSliceStorage[T]) print(amount int) {
for archId, compSlice := range s.slice {
fmt.Printf("archId(%d) - %v\n", archId, *compSlice)
}
}
// Provides generic storage for all archetypes
type archEngine struct {
generation int
// archCounter archetypeId
lookup []*lookupList // Indexed by archetypeId
compSliceStorage []storage // Indexed by componentId
dcr *componentRegistry
// TODO - using this makes things not thread safe inside the engine
archCount map[archetypeId]int
}
func newArchEngine() *archEngine {
return &archEngine{
generation: 1, // Start at 1 so that anyone with the default int value will always realize they are in the wrong generation
lookup: make([]*lookupList, 0, DefaultAllocation),
compSliceStorage: make([]storage, maxComponentId + 1),
dcr: newComponentRegistry(),
archCount: make(map[archetypeId]int),
}
}
func (e *archEngine) newArchetypeId(archMask archetypeMask) archetypeId {
e.generation++ // Increment the generation
archId := archetypeId(len(e.lookup))
e.lookup = append(e.lookup,
&lookupList{
index: newMap[Id,int](0),
id: make([]Id, 0, DefaultAllocation),
holes: make([]int, 0, DefaultAllocation),
mask: archMask,
},
)
return archId
}
func (e *archEngine) getGeneration() int {
return e.generation
}
// func (e *archEngine) Print(amount int) {
// fmt.Println("--- archEngine ---")
// max := amount
// for archId, lookup := range e.lookup {
// fmt.Printf("archId(%d) - lookup(%v)\n", archId, lookup)
// max--; if max <= 0 { break }
// }
// for name, storage := range e.compSliceStorage {
// fmt.Printf("name(%s) -\n", name)
// storage.print(amount)
// max--; if max <= 0 { break }
// }
// e.dcr.print()
// }
func (e *archEngine) count(anything ...any) int {
comps := make([]componentId, len(anything))
for i, c := range anything {
comps[i] = name(c)
}
archIds := make([]archetypeId, 0)
archIds = e.FilterList(archIds, comps)
total := 0
for _, archId := range archIds {
lookup := e.lookup[archId]
if lookup == nil {
panic(fmt.Sprintf("Couldnt find archId in archEngine lookup table: %d", archId))
}
// Each id represents an entity that holds the requested component(s)
// Each hole represents a deleted entity that used to hold the requested component(s)
total = total + len(lookup.id) - len(lookup.holes)
}
return total
}
func (e *archEngine) getArchetypeId(comp ...Component) archetypeId {
return e.dcr.getArchetypeId(e, comp...)
}
// TODO - map might be slower than just having an array. I could probably do a big bitmask and then just do a logical OR
func (e *archEngine) FilterList(archIds []archetypeId, comp []componentId) []archetypeId {
// TODO: could I maybe do something more optimal with archetypeMask?
// New way: With archSets that are just slices
// Logic: Go thorugh and keep track of how many times we see each archetype. Then only keep the archetypes that we've seen an amount of times equal to the number of components. If we have 5 components and see 5 for a specific archId, it means that each component has that archId
// TODO: this may be more efficient to use a slice?
// Clearing Optimization: https://go.dev/doc/go1.11#performance-compiler
for k := range e.archCount {
delete(e.archCount, k)
}
for _, compId := range comp {
for _, archId := range e.dcr.archSet[compId] {
e.archCount[archId] = e.archCount[archId] + 1
}
}
numComponents := len(comp)
archIds = archIds[:0]
for archId, count := range e.archCount {
if count >= numComponents {
archIds = append(archIds, archId)
// // TODO: How tight do I want my tolerances?
// if count > numComponents {
// panic("AAAA")
// }
}
}
return archIds
}
func getStorage[T any](e *archEngine) *componentSliceStorage[T] {
var val T
n := name(val)
return getStorageByCompId[T](e, n)
}
// Note: This will panic if the wrong compId doesn't match the generic type
func getStorageByCompId[T any](e *archEngine, compId componentId) *componentSliceStorage[T] {
ss := e.compSliceStorage[compId]
if ss == nil {
ss = &componentSliceStorage[T]{
slice: make(map[archetypeId]*componentSlice[T], DefaultAllocation),
}
e.compSliceStorage[compId] = ss
}
storage := ss.(*componentSliceStorage[T])
return storage
}
func (e *archEngine) getOrAddLookupIndex(archId archetypeId, id Id) int {
lookup := e.lookup[archId]
// Check if we want to cleanup holes
// TODO: This is a defragmentation operation. I'm not really sure how to compute heuristically that we should repack our slices. Too big it causes a stall, too small it causes unecessary repacks. maybe make it percentage based on holes per total entities. Maybe repack one at a time. Currently this should only trigger if we delete more than 1024 of the same archetype
if len(lookup.holes) >= 1024 {
e.CleanupHoles(archId)
}
index, ok := lookup.index.Get(id)
if !ok {
// Because the Id hasn't been added to this arch, we need to add it
index = lookup.addToEasiestHole(id)
}
return index
}
// Writes all of the components to the archetype
func (e *archEngine) write(archId archetypeId, id Id, comp ...Component) {
// Add to lookup list
index := e.getOrAddLookupIndex(archId, id)
// Loop through all components and add them to individual component slices
for i := range comp {
comp[i].write(e, archId, index)
}
}
func writeArch[T any](e *archEngine, archId archetypeId, index int, store *componentSliceStorage[T], val T) {
// Get the underlying Archetype's componentSlice
cSlice, ok := store.slice[archId]
if !ok {
cSlice = &componentSlice[T]{
comp: make([]T, 0, DefaultAllocation),
}
store.slice[archId] = cSlice
}
cSlice.Write(index, val)
}
func readArch[T any](e *archEngine, archId archetypeId, id Id) (T, bool) {
var ret T
lookup := e.lookup[archId]
if lookup == nil {
return ret, false // TODO: when could this possibly happen?
}
index, ok := lookup.index.Get(id)
if !ok {
return ret, false
}
// Get the dynamic componentSliceStorage
n := name(ret)
ss := e.compSliceStorage[n]
if ss == nil {
return ret, false
}
// fmt.Printf("componentSliceStorage[T] type: %s != %s", name(ss), name(ret))
storage, ok := ss.(*componentSliceStorage[T])
if !ok {
panic(fmt.Sprintf("Wrong componentSliceStorage[T] type: %d != %d", name(ss), name(ret)))
}
// Get the underlying Archetype's componentSlice
cSlice, ok := storage.slice[archId]
if !ok {
return ret, false
}
return cSlice.comp[index], true
}
func readPtrArch[T any](e *archEngine, archId archetypeId, id Id) *T {
var ret T
lookup := e.lookup[archId]
if lookup == nil {
return nil
}
index, ok := lookup.index.Get(id)
if !ok {
return nil
}
// Get the dynamic componentSliceStorage
n := name(ret)
ss := e.compSliceStorage[n]
if ss == nil {
return nil
}
storage, ok := ss.(*componentSliceStorage[T])
if !ok {
panic(fmt.Sprintf("Wrong componentSliceStorage[T] type: %d != %d", name(ss), name(ret)))
}
// Get the underlying Archetype's componentSlice
cSlice, ok := storage.slice[archId]
if !ok {
return nil
}
return &cSlice.comp[index]
}
// TODO - Think: Is it better to read everything then push it into the new archetypeId? Or better to migrate everything in place?
// Returns the archetypeId of where the entity ends up
func (e *archEngine) rewriteArch(archId archetypeId, id Id, comp ...Component) archetypeId {
// Calculate the new mask based on the bitwise or of the old and added masks
lookup := e.lookup[archId]
oldMask := lookup.mask
addMask := buildArchMask(comp...)
newMask := oldMask.bitwiseOr(addMask)
if oldMask == newMask {
// Case 1: Archetype stays the same.
// This means that we only need to write the newly added components because we wont be moving the base entity data
e.write(archId, id, comp...)
return archId
} else {
ent := e.ReadEntity(archId, id)
ent.Add(comp...)
combinedComps := ent.Comps()
// Note (maybe TODO): Right now we don't know if the new archtypeId exists for this, so we have to go through the normal code path to potentially create it (even though we currently have the new archtypeMask)
newArchId := e.getArchetypeId(combinedComps...)
// Case 2: Archetype changes
// 1: Delete all components in old archetype
e.TagForDeletion(archId, id)
// 2: We need to write the entire list of combinedComps
e.write(newArchId, id, combinedComps...)
return newArchId
}
}
func (e *archEngine) ReadEntity(archId archetypeId, id Id) *Entity {
lookup := e.lookup[archId]
if lookup == nil {
panic("Archetype doesn't have lookup list")
}
index, ok := lookup.index.Get(id)
if !ok {
panic("Archetype doesn't contain ID")
}
ent := NewEntity()
for n := range e.compSliceStorage {
if e.compSliceStorage[n] != nil {
e.compSliceStorage[n].ReadToEntity(ent, archId, index)
}
}
return ent
}
func (e *archEngine) ReadRawEntity(archId archetypeId, id Id) *RawEntity {
lookup := e.lookup[archId]
if lookup == nil {
panic("Archetype doesn't have lookup list")
}
index, ok := lookup.index.Get(id)
if !ok {
panic("Archetype doesn't contain ID")
}
ent := NewRawEntity()
for n := range e.compSliceStorage {
if e.compSliceStorage[n] != nil {
e.compSliceStorage[n].ReadToRawEntity(ent, archId, index)
}
}
return ent
}
// This creates a "hole" in the archetype at the specified Id
// Once we get enough holes, we can re-pack the entire slice
// TODO - How many holes before we repack? How many holes to pack at a time?
func (e *archEngine) TagForDeletion(archId archetypeId, id Id) {
lookup := e.lookup[archId]
if lookup == nil {
panic("Archetype doesn't have lookup list")
}
index, ok := lookup.index.Get(id)
if !ok {
panic("Archetype doesn't contain ID")
}
// This indicates that the index needs to be cleaned up and should be skipped in any list processing
lookup.id[index] = InvalidEntity
lookup.index.Delete(id)
// This is used to track the current list of indices that need to be cleaned
lookup.holes = append(lookup.holes, index)
}
func (e *archEngine) CleanupHoles(archId archetypeId) {
lookup := e.lookup[archId]
if lookup == nil {
panic("Archetype doesn't have lookup list")
}
for _, index := range lookup.holes {
// Pop all holes off the end of the archetype
for {
lastIndex := len(lookup.id) - 1
if lastIndex < 0 {
break
} // Break if the index we are trying to pop off is -1
lastId := lookup.id[lastIndex]
if lastId == InvalidEntity {
// If the last id is a hole, then slice it off
lookup.id = lookup.id[:lastIndex]
for n := range e.compSliceStorage {
if e.compSliceStorage[n] != nil {
e.compSliceStorage[n].Delete(archId, lastIndex)
}
}
continue // Try again
}
break
}
// Check bounds because we may have popped past our original index
if index >= len(lookup.id) {
continue
}
// Swap lastIndex (which is not a hole) with index (which is a hole)
lastIndex := len(lookup.id) - 1
lastId := lookup.id[lastIndex]
if lastId == InvalidEntity {
panic("Bug: This shouldn't happen")
}
lookup.id[index] = lastId
lookup.id = lookup.id[:lastIndex]
lookup.index.Put(lastId, index)
for n := range e.compSliceStorage {
if e.compSliceStorage[n] != nil {
e.compSliceStorage[n].Delete(archId, index)
}
}
}
// Clear holes slice
lookup.holes = lookup.holes[:0]
}