• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlange-42 / ark / 13752743521

09 Mar 2025 09:34PM CUT coverage: 99.43%. Remained the same
13752743521

Pull #169

github

web-flow
Merge 6a3e26b95 into c98324113
Pull Request #169: Useful error on incomplete relations

2 of 2 new or added lines in 1 file covered. (100.0%)

6458 of 6495 relevant lines covered (99.43%)

29592.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.16
/ecs/world_internal.go
1
package ecs
2

3
import (
4
        "reflect"
5
        "unsafe"
6
)
7

8
func (w *World) newEntityWith(ids []ID, comps []unsafe.Pointer, relations []RelationID) Entity {
527,383✔
9
        w.checkLocked()
527,383✔
10

527,383✔
11
        mask := bitMask{}
527,383✔
12
        newTable := w.storage.findOrCreateTable(&w.storage.tables[0], ids, nil, relations, &mask)
527,383✔
13
        entity, idx := w.storage.createEntity(newTable.id)
527,383✔
14

527,383✔
15
        if comps != nil {
1,054,593✔
16
                if len(ids) != len(comps) {
527,210✔
17
                        panic("lengths of IDs and components to add do not match")
×
18
                }
19
                for i, id := range ids {
1,058,237✔
20
                        newTable.Set(id, idx, comps[i])
531,027✔
21
                }
531,027✔
22
        }
23
        w.storage.registerTargets(relations)
527,382✔
24
        return entity
527,382✔
25
}
26

27
func (w *World) newEntitiesWith(count int, ids []ID, comps []unsafe.Pointer, relations []RelationID) {
28✔
28
        w.checkLocked()
28✔
29

28✔
30
        mask := bitMask{}
28✔
31
        newTable := w.storage.findOrCreateTable(&w.storage.tables[0], ids, nil, relations, &mask)
28✔
32

28✔
33
        startIdx := newTable.Len()
28✔
34
        w.storage.createEntities(newTable, count)
28✔
35

28✔
36
        if comps != nil {
56✔
37
                if len(ids) != len(comps) {
28✔
38
                        panic("lengths of IDs and components to add do not match")
×
39
                }
40
                for i := range count {
686✔
41
                        for j, id := range ids {
3,428✔
42
                                newTable.Set(id, uint32(startIdx+i), comps[j])
2,770✔
43
                        }
2,770✔
44
                }
45
        }
46
        w.storage.registerTargets(relations)
28✔
47
}
48

49
func (w *World) newEntities(count int, ids []ID, relations []RelationID) (tableID, int) {
128✔
50
        w.checkLocked()
128✔
51

128✔
52
        mask := bitMask{}
128✔
53
        newTable := w.storage.findOrCreateTable(&w.storage.tables[0], ids, nil, relations, &mask)
128✔
54

128✔
55
        startIdx := newTable.Len()
128✔
56
        w.storage.createEntities(newTable, count)
128✔
57
        w.storage.registerTargets(relations)
128✔
58

128✔
59
        return newTable.id, startIdx
128✔
60
}
128✔
61

62
func (w *World) exchange(entity Entity, add []ID, rem []ID, addComps []unsafe.Pointer, relations []RelationID) {
791✔
63
        w.checkLocked()
791✔
64

791✔
65
        if !w.Alive(entity) {
815✔
66
                panic("can't exchange components on a dead entity")
24✔
67
        }
68
        if len(add) == 0 && len(rem) == 0 {
767✔
69
                if len(relations) > 0 {
×
70
                        panic("exchange operation has no effect, but relations were specified. Use SetRelation(s) instead")
×
71
                }
72
                return
×
73
        }
74

75
        index := &w.storage.entities[entity.id]
767✔
76
        oldTable := &w.storage.tables[index.table]
767✔
77
        oldArchetype := &w.storage.archetypes[oldTable.archetype]
767✔
78

767✔
79
        oldIDs := oldArchetype.components
767✔
80

767✔
81
        mask := bitMask{}
767✔
82
        newTable := w.storage.findOrCreateTable(oldTable, add, rem, relations, &mask)
767✔
83
        newIndex := newTable.Add(entity)
767✔
84

767✔
85
        for _, id := range oldIDs {
2,507✔
86
                if mask.Get(id) {
2,468✔
87
                        comp := oldTable.Get(id, uintptr(index.row))
728✔
88
                        newTable.Set(id, newIndex, comp)
728✔
89
                }
728✔
90
        }
91
        if addComps != nil {
1,098✔
92
                if len(add) != len(addComps) {
331✔
93
                        panic("lengths of IDs and components to add do not match")
×
94
                }
95
                for i, id := range add {
1,057✔
96
                        newTable.Set(id, newIndex, addComps[i])
726✔
97
                }
726✔
98
        }
99

100
        swapped := oldTable.Remove(index.row)
767✔
101

767✔
102
        if swapped {
865✔
103
                swapEntity := oldTable.GetEntity(uintptr(index.row))
98✔
104
                w.storage.entities[swapEntity.id].row = index.row
98✔
105
        }
98✔
106
        w.storage.entities[entity.id] = entityIndex{table: newTable.id, row: newIndex}
767✔
107

767✔
108
        w.storage.registerTargets(relations)
767✔
109
}
110

111
func (w *World) exchangeBatch(batch *Batch, add []ID, rem []ID,
112
        addComps []unsafe.Pointer, relations []RelationID, fn func(table tableID, start, len int)) {
84✔
113
        w.checkLocked()
84✔
114

84✔
115
        if len(add) == 0 && len(rem) == 0 {
84✔
116
                if len(relations) > 0 {
×
117
                        panic("exchange operation has no effect, but relations were specified. Use SetRelationBatch instead")
×
118
                }
119
                return
×
120
        }
121

122
        tables := w.storage.getTables(batch)
84✔
123
        lengths := make([]uint32, len(tables))
84✔
124
        var totalEntities uint32 = 0
84✔
125
        for i, table := range tables {
252✔
126
                lengths[i] = uint32(table.Len())
168✔
127
                totalEntities += uint32(table.Len())
168✔
128
        }
168✔
129

130
        for i, table := range tables {
252✔
131
                tableLen := lengths[i]
168✔
132

168✔
133
                if tableLen == 0 {
168✔
134
                        continue
×
135
                }
136
                t, start, len := w.exchangeTable(table, int(tableLen), add, rem, addComps, relations)
168✔
137
                if fn != nil {
252✔
138
                        fn(t, start, len)
84✔
139
                }
84✔
140
        }
141
}
142

143
func (w *World) exchangeTable(oldTable *table, oldLen int, add []ID, rem []ID, addComps []unsafe.Pointer, relations []RelationID) (tableID, int, int) {
168✔
144
        oldArchetype := &w.storage.archetypes[oldTable.archetype]
168✔
145

168✔
146
        oldIDs := oldArchetype.components
168✔
147

168✔
148
        mask := bitMask{}
168✔
149
        newTable := w.storage.findOrCreateTable(oldTable, add, rem, relations, &mask)
168✔
150
        startIdx := uintptr(newTable.Len())
168✔
151
        count := uintptr(oldLen)
168✔
152

168✔
153
        var i uintptr
168✔
154
        for i = 0; i < count; i++ {
2,184✔
155
                idx := startIdx + i
2,016✔
156
                entity := oldTable.GetEntity(i)
2,016✔
157
                index := &w.storage.entities[entity.id]
2,016✔
158
                index.table = newTable.id
2,016✔
159
                index.row = uint32(idx)
2,016✔
160
        }
2,016✔
161

162
        newTable.AddAllEntities(oldTable, uint32(oldLen))
168✔
163
        for _, id := range oldIDs {
712✔
164
                if mask.Get(id) {
876✔
165
                        oldCol := oldTable.GetColumn(id)
332✔
166
                        newCol := newTable.GetColumn(id)
332✔
167
                        newCol.SetLast(oldCol, newTable.len, uint32(oldLen))
332✔
168
                }
332✔
169
        }
170
        if addComps != nil {
218✔
171
                if len(add) != len(addComps) {
50✔
172
                        panic("lengths of IDs and components to add do not match")
×
173
                }
174
                for i := range count {
650✔
175
                        for j, id := range add {
3,216✔
176
                                newTable.Set(id, uint32(startIdx+i), addComps[j])
2,616✔
177
                        }
2,616✔
178
                }
179
        }
180

181
        oldTable.Reset()
168✔
182
        w.storage.registerTargets(relations)
168✔
183

168✔
184
        return newTable.id, int(startIdx), int(count)
168✔
185
}
186

187
// setRelations sets the target entities for an entity relations.
188
func (w *World) setRelations(entity Entity, relations []RelationID) {
61✔
189
        w.checkLocked()
61✔
190

61✔
191
        if !w.storage.entityPool.Alive(entity) {
69✔
192
                panic("can't set relation for a dead entity")
8✔
193
        }
194
        if len(relations) == 0 {
61✔
195
                panic("no relations specified")
8✔
196
        }
197

198
        index := &w.storage.entities[entity.id]
45✔
199
        oldTable := &w.storage.tables[index.table]
45✔
200

45✔
201
        newRelations, changed := w.storage.getExchangeTargets(oldTable, relations)
45✔
202
        if !changed {
45✔
203
                return
×
204
        }
×
205

206
        oldArch := &w.storage.archetypes[oldTable.archetype]
45✔
207
        newTable, ok := oldArch.GetTable(&w.storage, newRelations)
45✔
208
        if !ok {
58✔
209
                newTable = w.storage.createTable(oldArch, newRelations)
13✔
210
        }
13✔
211
        newIndex := newTable.Add(entity)
45✔
212

45✔
213
        for _, id := range oldArch.components {
153✔
214
                comp := oldTable.Get(id, uintptr(index.row))
108✔
215
                newTable.Set(id, newIndex, comp)
108✔
216
        }
108✔
217

218
        swapped := oldTable.Remove(index.row)
45✔
219

45✔
220
        if swapped {
61✔
221
                swapEntity := oldTable.GetEntity(uintptr(index.row))
16✔
222
                w.storage.entities[swapEntity.id].row = index.row
16✔
223
        }
16✔
224
        w.storage.entities[entity.id] = entityIndex{table: newTable.id, row: newIndex}
45✔
225

45✔
226
        w.storage.registerTargets(relations)
45✔
227
}
228

229
func (w *World) setRelationsBatch(batch *Batch, relations []RelationID, fn func(table tableID, start, len int)) {
17✔
230
        w.checkLocked()
17✔
231

17✔
232
        if len(relations) == 0 {
25✔
233
                panic("no relations specified")
8✔
234
        }
235

236
        tables := w.storage.getTables(batch)
9✔
237
        lengths := make([]uint32, len(tables))
9✔
238
        var totalEntities uint32 = 0
9✔
239
        for i, table := range tables {
18✔
240
                lengths[i] = uint32(table.Len())
9✔
241
                totalEntities += uint32(table.Len())
9✔
242
        }
9✔
243

244
        for i, table := range tables {
18✔
245
                tableLen := lengths[i]
9✔
246

9✔
247
                if tableLen == 0 {
9✔
248
                        continue
×
249
                }
250
                t, start, len := w.setRelationsTable(table, int(tableLen), relations)
9✔
251
                if fn != nil {
18✔
252
                        fn(t, start, len)
9✔
253
                }
9✔
254
        }
255

256
        w.storage.registerTargets(relations)
9✔
257
}
258

259
func (w *World) setRelationsTable(oldTable *table, oldLen int, relations []RelationID) (tableID, int, int) {
9✔
260
        newRelations, changed := w.storage.getExchangeTargets(oldTable, relations)
9✔
261

9✔
262
        if !changed {
9✔
263
                return oldTable.id, 0, oldLen
×
264
        }
×
265
        oldArch := &w.storage.archetypes[oldTable.archetype]
9✔
266
        newTable, ok := oldArch.GetTable(&w.storage, newRelations)
9✔
267
        if !ok {
18✔
268
                newTable = w.storage.createTable(oldArch, newRelations)
9✔
269
        }
9✔
270
        startIdx := newTable.Len()
9✔
271
        w.storage.moveEntities(oldTable, newTable, uint32(oldLen))
9✔
272

9✔
273
        return newTable.id, startIdx, oldLen
9✔
274

275
}
276

277
func (w *World) componentID(tp reflect.Type) ID {
2,821✔
278
        id, newID := w.storage.registry.ComponentID(tp)
2,821✔
279
        if newID {
3,741✔
280
                if w.IsLocked() {
921✔
281
                        w.storage.registry.unregisterLastComponent()
1✔
282
                        panic("attempt to register a new component in a locked world")
1✔
283
                }
284
                w.storage.AddComponent(id)
919✔
285
        }
286
        return ID{id: id}
2,820✔
287
}
288

289
func (w *World) resourceID(tp reflect.Type) ResID {
12✔
290
        id, _ := w.resources.registry.ComponentID(tp)
12✔
291
        return ResID{id: id}
12✔
292
}
12✔
293

294
// lock the world and get the lock bit for later unlocking.
295
func (w *World) lock() uint8 {
1,450✔
296
        return w.locks.Lock()
1,450✔
297
}
1,450✔
298

299
// unlock unlocks the given lock bit.
300
func (w *World) unlock(l uint8) {
1,449✔
301
        w.locks.Unlock(l)
1,449✔
302
}
1,449✔
303

304
// checkLocked checks if the world is locked, and panics if so.
305
func (w *World) checkLocked() {
1,054,379✔
306
        if w.IsLocked() {
1,054,380✔
307
                panic("attempt to modify a locked world")
1✔
308
        }
309
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc