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

mlange-42 / ark / 13768706324

10 Mar 2025 03:38PM CUT coverage: 99.437%. Remained the same
13768706324

Pull #179

github

web-flow
Merge 8094be2da into 093db9782
Pull Request #179: Fix false-positive debug checks

6538 of 6575 relevant lines covered (99.44%)

27299.41 hits per line

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

93.1
/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 {
496,891✔
9
        w.checkLocked()
496,891✔
10

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

496,891✔
15
        if comps != nil {
993,609✔
16
                if len(ids) != len(comps) {
496,718✔
17
                        panic("lengths of IDs and components to add do not match")
×
18
                }
19
                for i, id := range ids {
997,253✔
20
                        newTable.Set(id, idx, comps[i])
500,535✔
21
                }
500,535✔
22
        }
23
        w.storage.registerTargets(relations)
496,890✔
24
        return entity
496,890✔
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) {
131✔
50
        w.checkLocked()
131✔
51

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

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

131✔
59
        return newTable.id, startIdx
131✔
60
}
131✔
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
        mask := oldArchetype.mask
767✔
80
        newTable := w.storage.findOrCreateTable(oldTable, add, rem, relations, &mask)
767✔
81
        newIndex := newTable.Add(entity)
767✔
82

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

98
        swapped := oldTable.Remove(index.row)
767✔
99

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

767✔
106
        w.storage.registerTargets(relations)
767✔
107
}
108

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

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

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

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

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

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

168✔
144
        oldIDs := oldArchetype.components
168✔
145

168✔
146
        mask := oldArchetype.mask
168✔
147
        newTable := w.storage.findOrCreateTable(oldTable, add, rem, relations, &mask)
168✔
148
        startIdx := uintptr(newTable.Len())
168✔
149
        count := uintptr(oldLen)
168✔
150

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

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

179
        oldTable.Reset()
168✔
180
        w.storage.registerTargets(relations)
168✔
181

168✔
182
        return newTable.id, int(startIdx), int(count)
168✔
183
}
184

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

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

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

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

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

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

216
        swapped := oldTable.Remove(index.row)
45✔
217

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

45✔
224
        w.storage.registerTargets(relations)
45✔
225
}
226

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

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

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

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

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

254
        w.storage.registerTargets(relations)
9✔
255
}
256

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

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

9✔
271
        return newTable.id, startIdx, oldLen
9✔
272

273
}
274

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

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

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

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

302
// checkLocked checks if the world is locked, and panics if so.
303
func (w *World) checkLocked() {
994,077✔
304
        if w.IsLocked() {
994,078✔
305
                panic("attempt to modify a locked world")
1✔
306
        }
307
}
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