kiran 1.0.0

Kiran — AI-native game engine for AGNOS
Documentation
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
//! Archetype-based SOA component storage.
//!
//! Groups entities by their component signature (archetype). Entities with the
//! same set of component types are stored contiguously for cache-friendly iteration.
//!
//! This is an **opt-in** storage layer that sits alongside the existing
//! `World` component storage. Use it for hot-path iteration over large entity
//! sets where cache locality matters (e.g. physics bodies, particles, transforms).

use std::any::{Any, TypeId};
use std::collections::HashMap;

use crate::world::Entity;

/// A set of component types that defines an archetype.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ArchetypeId(Vec<TypeId>);

impl ArchetypeId {
    /// Create an archetype ID from a sorted list of type IDs.
    fn from_sorted(mut types: Vec<TypeId>) -> Self {
        types.sort();
        Self(types)
    }

    /// Number of component types in this archetype.
    #[must_use]
    #[inline]
    pub fn component_count(&self) -> usize {
        self.0.len()
    }

    /// Check if this archetype contains a specific component type.
    #[must_use]
    pub fn contains(&self, type_id: &TypeId) -> bool {
        self.0.contains(type_id)
    }
}

/// A column of components for a single type within an archetype.
struct Column {
    /// Type-erased component data.
    data: Vec<Box<dyn Any + Send + Sync>>,
}

impl Column {
    fn new() -> Self {
        Self { data: Vec::new() }
    }

    fn push(&mut self, value: Box<dyn Any + Send + Sync>) {
        self.data.push(value);
    }

    fn get<T: 'static>(&self, row: usize) -> Option<&T> {
        self.data.get(row)?.downcast_ref::<T>()
    }

    fn get_mut<T: 'static>(&mut self, row: usize) -> Option<&mut T> {
        self.data.get_mut(row)?.downcast_mut::<T>()
    }

    fn swap_remove(&mut self, row: usize) -> Box<dyn Any + Send + Sync> {
        self.data.swap_remove(row)
    }
}

/// An archetype — a group of entities with the same component set.
struct Archetype {
    id: ArchetypeId,
    /// Entity IDs in this archetype (parallel with column rows).
    entities: Vec<Entity>,
    /// Component columns keyed by TypeId.
    columns: HashMap<TypeId, Column>,
}

impl Archetype {
    fn new(id: ArchetypeId) -> Self {
        let mut columns = HashMap::new();
        for &tid in &id.0 {
            columns.insert(tid, Column::new());
        }
        Self {
            id,
            entities: Vec::new(),
            columns,
        }
    }

    fn len(&self) -> usize {
        self.entities.len()
    }

    /// Remove an entity by swapping with the last row.
    fn remove(&mut self, row: usize) -> Entity {
        let entity = self.entities.swap_remove(row);
        for column in self.columns.values_mut() {
            column.swap_remove(row);
        }
        entity
    }
}

/// Archetype storage — manages all archetypes and entity→archetype mapping.
pub struct ArchetypeStore {
    /// All archetypes, indexed by archetype ID.
    archetypes: Vec<Archetype>,
    /// ArchetypeId → index into `archetypes`.
    id_to_index: HashMap<ArchetypeId, usize>,
    /// Entity → (archetype index, row within archetype).
    entity_locations: HashMap<Entity, (usize, usize)>,
}

impl Default for ArchetypeStore {
    fn default() -> Self {
        Self::new()
    }
}

impl ArchetypeStore {
    /// Create an empty archetype store.
    pub fn new() -> Self {
        Self {
            archetypes: Vec::new(),
            id_to_index: HashMap::new(),
            entity_locations: HashMap::new(),
        }
    }

    /// Insert an entity with its components into the appropriate archetype.
    ///
    /// Components are provided as `(TypeId, Box<dyn Any>)` pairs.
    pub fn insert(
        &mut self,
        entity: Entity,
        components: Vec<(TypeId, Box<dyn Any + Send + Sync>)>,
    ) {
        let type_ids: Vec<TypeId> = components.iter().map(|(tid, _)| *tid).collect();
        let arch_id = ArchetypeId::from_sorted(type_ids);

        let arch_idx = if let Some(&idx) = self.id_to_index.get(&arch_id) {
            idx
        } else {
            let idx = self.archetypes.len();
            self.archetypes.push(Archetype::new(arch_id.clone()));
            self.id_to_index.insert(arch_id, idx);
            idx
        };

        let archetype = &mut self.archetypes[arch_idx];
        let row = archetype.len();
        archetype.entities.push(entity);

        for (tid, value) in components {
            if let Some(column) = archetype.columns.get_mut(&tid) {
                column.push(value);
            }
        }

        self.entity_locations.insert(entity, (arch_idx, row));
    }

    /// Remove an entity from its archetype.
    pub fn remove(&mut self, entity: Entity) -> bool {
        let Some((arch_idx, row)) = self.entity_locations.remove(&entity) else {
            return false;
        };

        let archetype = &mut self.archetypes[arch_idx];
        archetype.remove(row);

        // If we swapped, update the moved entity's location
        if row < archetype.len() {
            let moved_entity = archetype.entities[row];
            self.entity_locations.insert(moved_entity, (arch_idx, row));
        }

        true
    }

    /// Get a component for an entity.
    pub fn get<T: 'static + Send + Sync>(&self, entity: Entity) -> Option<&T> {
        let &(arch_idx, row) = self.entity_locations.get(&entity)?;
        let archetype = &self.archetypes[arch_idx];
        let column = archetype.columns.get(&TypeId::of::<T>())?;
        column.get::<T>(row)
    }

    /// Get a mutable component for an entity.
    pub fn get_mut<T: 'static + Send + Sync>(&mut self, entity: Entity) -> Option<&mut T> {
        let &(arch_idx, row) = self.entity_locations.get(&entity)?;
        let archetype = &mut self.archetypes[arch_idx];
        let column = archetype.columns.get_mut(&TypeId::of::<T>())?;
        column.get_mut::<T>(row)
    }

    /// Iterate over all entities with component A (cache-friendly within archetypes).
    pub fn query<A: 'static + Send + Sync>(&self) -> Vec<(Entity, &A)> {
        let tid = TypeId::of::<A>();
        let mut results = Vec::new();

        for archetype in &self.archetypes {
            if !archetype.id.contains(&tid) {
                continue;
            }
            let column = match archetype.columns.get(&tid) {
                Some(c) => c,
                None => continue,
            };
            for (row, entity) in archetype.entities.iter().enumerate() {
                if let Some(component) = column.get::<A>(row) {
                    results.push((*entity, component));
                }
            }
        }

        results
    }

    /// Iterate over entities with components A and B.
    pub fn query2<A: 'static + Send + Sync, B: 'static + Send + Sync>(
        &self,
    ) -> Vec<(Entity, &A, &B)> {
        let tid_a = TypeId::of::<A>();
        let tid_b = TypeId::of::<B>();
        let mut results = Vec::new();

        for archetype in &self.archetypes {
            if !archetype.id.contains(&tid_a) || !archetype.id.contains(&tid_b) {
                continue;
            }
            let col_a = match archetype.columns.get(&tid_a) {
                Some(c) => c,
                None => continue,
            };
            let col_b = match archetype.columns.get(&tid_b) {
                Some(c) => c,
                None => continue,
            };
            for (row, entity) in archetype.entities.iter().enumerate() {
                if let (Some(a), Some(b)) = (col_a.get::<A>(row), col_b.get::<B>(row)) {
                    results.push((*entity, a, b));
                }
            }
        }

        results
    }

    /// Check if an entity is in the archetype store.
    #[must_use]
    pub fn contains(&self, entity: Entity) -> bool {
        self.entity_locations.contains_key(&entity)
    }

    /// Total entities across all archetypes.
    #[must_use]
    pub fn entity_count(&self) -> usize {
        self.entity_locations.len()
    }

    /// Number of archetypes.
    #[must_use]
    pub fn archetype_count(&self) -> usize {
        self.archetypes.len()
    }

    /// Get the sizes of each archetype (for diagnostics).
    #[must_use]
    pub fn archetype_sizes(&self) -> Vec<usize> {
        self.archetypes.iter().map(|a| a.len()).collect()
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Debug, PartialEq)]
    struct Position(f32, f32);
    #[derive(Debug, PartialEq)]
    struct Velocity(f32, f32);
    #[derive(Debug, PartialEq)]
    struct Health(u32);

    fn pos_component(x: f32, y: f32) -> (TypeId, Box<dyn Any + Send + Sync>) {
        (TypeId::of::<Position>(), Box::new(Position(x, y)))
    }

    fn vel_component(x: f32, y: f32) -> (TypeId, Box<dyn Any + Send + Sync>) {
        (TypeId::of::<Velocity>(), Box::new(Velocity(x, y)))
    }

    fn health_component(hp: u32) -> (TypeId, Box<dyn Any + Send + Sync>) {
        (TypeId::of::<Health>(), Box::new(Health(hp)))
    }

    #[test]
    fn store_new() {
        let store = ArchetypeStore::new();
        assert_eq!(store.entity_count(), 0);
        assert_eq!(store.archetype_count(), 0);
    }

    #[test]
    fn insert_and_get() {
        let mut store = ArchetypeStore::new();
        let e = Entity::new(0, 0);
        store.insert(e, vec![pos_component(1.0, 2.0), vel_component(3.0, 4.0)]);

        let pos = store.get::<Position>(e).unwrap();
        assert_eq!(*pos, Position(1.0, 2.0));
        let vel = store.get::<Velocity>(e).unwrap();
        assert_eq!(*vel, Velocity(3.0, 4.0));
    }

    #[test]
    fn same_archetype_grouped() {
        let mut store = ArchetypeStore::new();
        let e1 = Entity::new(0, 0);
        let e2 = Entity::new(1, 0);
        store.insert(e1, vec![pos_component(1.0, 0.0), vel_component(0.0, 1.0)]);
        store.insert(e2, vec![pos_component(2.0, 0.0), vel_component(0.0, 2.0)]);

        assert_eq!(store.archetype_count(), 1); // same archetype
        assert_eq!(store.entity_count(), 2);
    }

    #[test]
    fn different_archetypes() {
        let mut store = ArchetypeStore::new();
        let e1 = Entity::new(0, 0);
        let e2 = Entity::new(1, 0);
        store.insert(e1, vec![pos_component(1.0, 0.0)]);
        store.insert(e2, vec![pos_component(2.0, 0.0), vel_component(0.0, 1.0)]);

        assert_eq!(store.archetype_count(), 2);
    }

    #[test]
    fn get_mut() {
        let mut store = ArchetypeStore::new();
        let e = Entity::new(0, 0);
        store.insert(e, vec![pos_component(1.0, 2.0)]);

        store.get_mut::<Position>(e).unwrap().0 = 99.0;
        assert_eq!(store.get::<Position>(e).unwrap().0, 99.0);
    }

    #[test]
    fn remove_entity() {
        let mut store = ArchetypeStore::new();
        let e = Entity::new(0, 0);
        store.insert(e, vec![pos_component(1.0, 2.0)]);
        assert!(store.contains(e));

        store.remove(e);
        assert!(!store.contains(e));
        assert_eq!(store.entity_count(), 0);
    }

    #[test]
    fn remove_with_swap() {
        let mut store = ArchetypeStore::new();
        let e1 = Entity::new(0, 0);
        let e2 = Entity::new(1, 0);
        let e3 = Entity::new(2, 0);
        store.insert(e1, vec![pos_component(1.0, 0.0)]);
        store.insert(e2, vec![pos_component(2.0, 0.0)]);
        store.insert(e3, vec![pos_component(3.0, 0.0)]);

        store.remove(e1); // e3 gets swapped into row 0
        assert_eq!(store.entity_count(), 2);
        assert!(store.contains(e2));
        assert!(store.contains(e3));
        assert_eq!(store.get::<Position>(e3).unwrap().0, 3.0);
    }

    #[test]
    fn query_single() {
        let mut store = ArchetypeStore::new();
        let e1 = Entity::new(0, 0);
        let e2 = Entity::new(1, 0);
        let e3 = Entity::new(2, 0);
        store.insert(e1, vec![pos_component(1.0, 0.0)]);
        store.insert(e2, vec![pos_component(2.0, 0.0), vel_component(0.0, 1.0)]);
        store.insert(e3, vec![health_component(100)]);

        let positions = store.query::<Position>();
        assert_eq!(positions.len(), 2); // e1 and e2 have Position
    }

    #[test]
    fn query2_components() {
        let mut store = ArchetypeStore::new();
        let e1 = Entity::new(0, 0);
        let e2 = Entity::new(1, 0);
        store.insert(e1, vec![pos_component(1.0, 0.0)]);
        store.insert(e2, vec![pos_component(2.0, 0.0), vel_component(0.0, 1.0)]);

        let results = store.query2::<Position, Velocity>();
        assert_eq!(results.len(), 1); // only e2 has both
        assert_eq!(results[0].1, &Position(2.0, 0.0));
    }

    #[test]
    fn get_missing_component() {
        let mut store = ArchetypeStore::new();
        let e = Entity::new(0, 0);
        store.insert(e, vec![pos_component(1.0, 0.0)]);
        assert!(store.get::<Velocity>(e).is_none());
    }

    #[test]
    fn get_missing_entity() {
        let store = ArchetypeStore::new();
        let e = Entity::new(99, 0);
        assert!(store.get::<Position>(e).is_none());
    }

    #[test]
    fn remove_missing_entity() {
        let mut store = ArchetypeStore::new();
        assert!(!store.remove(Entity::new(99, 0)));
    }

    #[test]
    fn archetype_sizes() {
        let mut store = ArchetypeStore::new();
        for i in 0..5 {
            store.insert(
                Entity::new(i, 0),
                vec![pos_component(i as f32, 0.0), vel_component(0.0, 0.0)],
            );
        }
        for i in 5..8 {
            store.insert(Entity::new(i, 0), vec![pos_component(i as f32, 0.0)]);
        }

        let sizes = store.archetype_sizes();
        assert_eq!(sizes.len(), 2);
        assert!(sizes.contains(&5));
        assert!(sizes.contains(&3));
    }

    #[test]
    fn archetype_id_order_independent() {
        // Components added in different order should produce the same archetype
        let mut store = ArchetypeStore::new();
        let e1 = Entity::new(0, 0);
        let e2 = Entity::new(1, 0);
        store.insert(e1, vec![pos_component(1.0, 0.0), vel_component(0.0, 1.0)]);
        store.insert(e2, vec![vel_component(0.0, 2.0), pos_component(2.0, 0.0)]);

        assert_eq!(store.archetype_count(), 1); // same archetype regardless of insert order
    }

    #[test]
    fn store_as_world_resource() {
        let mut world = crate::World::new();
        world.insert_resource(ArchetypeStore::new());
        let store = world.get_resource::<ArchetypeStore>().unwrap();
        assert_eq!(store.entity_count(), 0);
    }

    #[test]
    fn large_archetype_iteration() {
        let mut store = ArchetypeStore::new();
        for i in 0..1000u32 {
            store.insert(
                Entity::new(i, 0),
                vec![pos_component(i as f32, 0.0), vel_component(1.0, 1.0)],
            );
        }

        let results = store.query::<Position>();
        assert_eq!(results.len(), 1000);
    }
}