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

mlange-42 / ark / 13593874277

28 Feb 2025 06:06PM CUT coverage: 96.872% (+2.1%) from 94.795%
13593874277

Pull #79

github

web-flow
Merge 94da96c6d into 10315654e
Pull Request #79: Relation getters for queries and maps

118 of 118 new or added lines in 6 files covered. (100.0%)

8 existing lines in 1 file now uncovered.

3562 of 3677 relevant lines covered (96.87%)

50530.93 hits per line

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

90.48
/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 {
497,332✔
9
        w.checkLocked()
497,332✔
10

497,332✔
11
        mask := All(ids...)
497,332✔
12
        newTable := w.storage.findOrCreateTable(&w.storage.tables[0], &mask, relations)
497,332✔
13
        entity, idx := w.storage.createEntity(newTable.id)
497,332✔
14

497,332✔
15
        if comps != nil {
994,664✔
16
                if len(ids) != len(comps) {
497,332✔
17
                        panic("lengths of IDs and components to add do not match")
×
18
                }
19
                for i, id := range ids {
997,618✔
20
                        newTable.Set(id, idx, comps[i])
500,286✔
21
                }
500,286✔
22
        }
23
        w.storage.registerTargets(relations)
497,332✔
24
        return entity
497,332✔
25
}
26

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

8✔
30
        mask := All(ids...)
8✔
31
        newTable := w.storage.findOrCreateTable(&w.storage.tables[0], &mask, relations)
8✔
32

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

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

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

8✔
52
        mask := All(ids...)
8✔
53
        newTable := w.storage.findOrCreateTable(&w.storage.tables[0], &mask, relations)
8✔
54

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

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

62
func (w *World) get(entity Entity, component ID) unsafe.Pointer {
2✔
63
        if !w.storage.entityPool.Alive(entity) {
2✔
64
                panic("can't get component of a dead entity")
×
65
        }
66
        index := w.storage.entities[entity.id]
2✔
67
        return w.storage.tables[index.table].Get(component, uintptr(index.row))
2✔
68
}
69

70
func (w *World) has(entity Entity, component ID) bool {
6✔
71
        if !w.storage.entityPool.Alive(entity) {
6✔
72
                panic("can't get component of a dead entity")
×
73
        }
74
        index := w.storage.entities[entity.id]
6✔
75
        return w.storage.tables[index.table].Has(component)
6✔
76
}
77

78
func (w *World) exchange(entity Entity, add []ID, rem []ID, addComps []unsafe.Pointer, relations []relationID) {
612✔
79
        w.checkLocked()
612✔
80

612✔
81
        if !w.Alive(entity) {
612✔
82
                panic("can't exchange components on a dead entity")
×
83
        }
84
        if len(add) == 0 && len(rem) == 0 {
612✔
85
                return
×
86
        }
×
87

88
        index := &w.storage.entities[entity.id]
612✔
89
        oldTable := &w.storage.tables[index.table]
612✔
90
        oldArchetype := &w.storage.archetypes[oldTable.archetype]
612✔
91

612✔
92
        mask := oldArchetype.mask
612✔
93
        w.storage.getExchangeMask(&mask, add, rem)
612✔
94

612✔
95
        oldIDs := oldArchetype.components
612✔
96

612✔
97
        newTable := w.storage.findOrCreateTable(oldTable, &mask, relations)
612✔
98
        newIndex := newTable.Add(entity)
612✔
99

612✔
100
        for _, id := range oldIDs {
2,252✔
101
                if mask.Get(id) {
2,302✔
102
                        comp := oldTable.Get(id, uintptr(index.row))
662✔
103
                        newTable.Set(id, newIndex, comp)
662✔
104
                }
662✔
105
        }
106
        if addComps != nil {
939✔
107
                if len(add) != len(addComps) {
327✔
108
                        panic("lengths of IDs and components to add do not match")
×
109
                }
110
                for i, id := range add {
1,046✔
111
                        newTable.Set(id, newIndex, addComps[i])
719✔
112
                }
719✔
113
        }
114

115
        swapped := oldTable.Remove(index.row)
612✔
116

612✔
117
        if swapped {
710✔
118
                swapEntity := oldTable.GetEntity(uintptr(index.row))
98✔
119
                w.storage.entities[swapEntity.id].row = index.row
98✔
120
        }
98✔
121
        w.storage.entities[entity.id] = entityIndex{table: newTable.id, row: newIndex}
612✔
122

612✔
123
        w.storage.registerTargets(relations)
612✔
124
}
125

126
// setRelations sets the target entities for an entity relations.
127
func (w *World) setRelations(entity Entity, relations []relationID) {
42✔
128
        w.checkLocked()
42✔
129

42✔
130
        if !w.storage.entityPool.Alive(entity) {
42✔
131
                panic("can't set relation for a dead entity")
×
132
        }
133

134
        index := &w.storage.entities[entity.id]
42✔
135
        oldTable := &w.storage.tables[index.table]
42✔
136

42✔
137
        newRelations, changed := w.storage.getExchangeTargets(oldTable, relations)
42✔
138
        if !changed {
42✔
139
                return
×
140
        }
×
141

142
        oldArch := &w.storage.archetypes[oldTable.archetype]
42✔
143
        newTable, ok := oldArch.GetTable(&w.storage, newRelations)
42✔
144
        if !ok {
53✔
145
                newTable = w.storage.createTable(oldArch, newRelations)
11✔
146
        }
11✔
147
        newIndex := newTable.Add(entity)
42✔
148

42✔
149
        for _, id := range oldArch.components {
145✔
150
                comp := oldTable.Get(id, uintptr(index.row))
103✔
151
                newTable.Set(id, newIndex, comp)
103✔
152
        }
103✔
153

154
        swapped := oldTable.Remove(index.row)
42✔
155

42✔
156
        if swapped {
58✔
157
                swapEntity := oldTable.GetEntity(uintptr(index.row))
16✔
158
                w.storage.entities[swapEntity.id].row = index.row
16✔
159
        }
16✔
160
        w.storage.entities[entity.id] = entityIndex{table: newTable.id, row: newIndex}
42✔
161

42✔
162
        w.storage.registerTargets(relations)
42✔
163
}
164

165
func (w *World) componentID(tp reflect.Type) ID {
1,937✔
166
        id, newID := w.storage.registry.ComponentID(tp)
1,937✔
167
        if newID {
2,392✔
168
                if w.IsLocked() {
455✔
169
                        w.storage.registry.unregisterLastComponent()
×
170
                        panic("attempt to register a new component in a locked world")
×
171
                }
172
                w.storage.AddComponent(id)
455✔
173
        }
174
        return ID{id: id}
1,937✔
175
}
176

177
func (w *World) resourceID(tp reflect.Type) ResID {
3✔
178
        id, _ := w.resources.registry.ComponentID(tp)
3✔
179
        return ResID{id: id}
3✔
180
}
3✔
181

182
// lock the world and get the lock bit for later unlocking.
183
func (w *World) lock() uint8 {
1,084✔
184
        return w.locks.Lock()
1,084✔
185
}
1,084✔
186

187
// unlock unlocks the given lock bit.
188
func (w *World) unlock(l uint8) {
1,084✔
189
        w.locks.Unlock(l)
1,084✔
190
}
1,084✔
191

192
// checkLocked checks if the world is locked, and panics if so.
193
func (w *World) checkLocked() {
994,832✔
194
        if w.IsLocked() {
994,832✔
195
                panic("attempt to modify a locked world")
×
196
        }
197
}
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