suon_chunk 0.1.0

World chunk and map-grid primitives for the Suon MMORPG framework
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
//! Chunk-local occupancy tracking.
//!
//! This module keeps track of blocked floor-position pairs inside chunk entities
//! and synchronizes that state from world-space entity movement.

use crate::{chunks::Chunks, occupancy::occupied::Occupied};
use bevy::prelude::*;
use std::collections::*;
use suon_position::{floor::Floor, position::Position, previous_position::PreviousPosition};

pub mod occupied;

#[derive(Component, Default, Debug)]
/// Per-chunk occupancy map grouped by floor and world position.
pub struct Occupancy {
    floors: HashMap<Floor, HashSet<Position>>,
}

impl Occupancy {
    /// Marks the provided floor-position pair as occupied.
    pub(crate) fn occupy(&mut self, floor: Floor, position: Position) -> bool {
        self.floors.entry(floor).or_default().insert(position)
    }

    /// Releases the provided floor-position pair from the occupancy map.
    pub(crate) fn release(&mut self, floor: &Floor, position: &Position) -> bool {
        self.floors
            .get_mut(floor)
            .map(|positions| positions.remove(position))
            .unwrap_or(false)
    }

    /// Returns whether the provided floor-position pair is currently occupied.
    ///
    /// # Examples
    /// ```no_run
    /// use suon_chunk::{Chunk, ChunkPlugin, chunks::Chunks, occupancy::{Occupancy, occupied::Occupied}};
    /// use bevy::prelude::*;
    /// use suon_position::{floor::Floor, position::Position};
    ///
    /// let mut app = App::new();
    /// app.add_plugins(MinimalPlugins);
    /// app.add_plugins(ChunkPlugin);
    ///
    /// let chunk = app.world_mut().spawn(Chunk).id();
    /// app.insert_resource(Chunks::from_iter([(Position { x: 4, y: 4 }, chunk)]));
    /// app.world_mut().spawn((Position { x: 4, y: 4 }, Floor { z: 0 }, Occupied));
    /// app.update();
    ///
    /// let occupancy = app.world().get::<Occupancy>(chunk).unwrap();
    /// assert!(occupancy.contains(&Floor { z: 0 }, &Position { x: 4, y: 4 }));
    /// ```
    pub fn contains(&self, floor: &Floor, position: &Position) -> bool {
        self.floors
            .get(floor)
            .map(|positions| positions.contains(position))
            .unwrap_or(false)
    }
}

/// Registers occupancy when an entity gains [`Occupied`].
pub(crate) fn sync_occupancy_register(
    event: On<Add, Occupied>,
    entities: Query<(&Position, &Floor)>,
    mut occupancies: Query<&mut Occupancy>,
    chunks: Res<Chunks>,
) {
    // Occupancy registration follows the chunk resolved from the current world position.
    let entity = event.event_target();

    let Ok((position, floor)) = entities.get(entity) else {
        return;
    };

    let Some(chunk) = chunks.get(position) else {
        return;
    };

    if let Ok(mut occupancy) = occupancies.get_mut(chunk) {
        occupancy.occupy(*floor, *position);
    }
}

/// Releases occupancy when an entity loses [`Occupied`].
pub(crate) fn sync_occupancy_unregister(
    event: On<Remove, Occupied>,
    entities: Query<(&Position, &Floor, Option<&PreviousPosition>)>,
    mut occupancies: Query<&mut Occupancy>,
    chunks: Res<Chunks>,
) {
    // Removal releases both the current and the previously known coordinate so stale
    // occupancy cannot survive a remove that happens in the same frame as movement.
    let entity = event.event_target();

    let Ok((position, floor, previous_position)) = entities.get(entity) else {
        return;
    };

    if let Some(chunk) = chunks.get(position)
        && let Ok(mut occupancy) = occupancies.get_mut(chunk)
    {
        occupancy.release(floor, position);
    }

    let Some(previous_position) = previous_position else {
        return;
    };

    let previous_position = Position {
        x: previous_position.x,
        y: previous_position.y,
    };

    if previous_position == *position {
        return;
    }

    if let Some(previous_chunk) = chunks.get(&previous_position)
        && let Ok(mut occupancy) = occupancies.get_mut(previous_chunk)
    {
        occupancy.release(floor, &previous_position);
    }
}

/// Reconciles occupancy after an occupied [`Position`] is inserted or replaced.
pub(crate) fn resync_occupied_positions(
    event: On<Insert, Position>,
    entities: Query<(&Position, &PreviousPosition, &Floor), With<Occupied>>,
    mut occupancies: Query<&mut Occupancy>,
    chunks: Res<Chunks>,
) {
    // Moving an occupied entity requires releasing the previous coordinate before
    // registering the new one so chunk occupancy remains authoritative.
    let entity = event.event_target();

    let Ok((position, previous_position, floor)) = entities.get(entity) else {
        return;
    };

    let previous_position = Position {
        x: previous_position.x,
        y: previous_position.y,
    };

    if let Some(previous_chunk) = chunks.get(&previous_position)
        && let Ok(mut occupancy) = occupancies.get_mut(previous_chunk)
    {
        occupancy.release(floor, &previous_position);
    }

    let Some(current_chunk) = chunks.get(position) else {
        return;
    };

    if let Ok(mut occupancy) = occupancies.get_mut(current_chunk) {
        occupancy.occupy(*floor, *position);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Chunk, ChunkPlugin};

    #[test]
    fn should_register_occupied_tile_when_component_is_added() {
        let mut app = App::new();
        app.add_plugins(MinimalPlugins);
        app.add_plugins(ChunkPlugin);

        // Spawning an Occupied entity should automatically populate the owning chunk map.
        let chunk_entity = app.world_mut().spawn(Chunk).id();
        app.world_mut()
            .resource_mut::<Chunks>()
            .insert(&Position { x: 4, y: 4 }, chunk_entity);

        app.world_mut()
            .spawn((Position { x: 4, y: 4 }, Floor { z: 0 }, Occupied));

        app.update();

        let occupancy = app
            .world()
            .get::<Occupancy>(chunk_entity)
            .expect("Chunk should carry Occupancy");

        assert!(
            occupancy.contains(&Floor { z: 0 }, &Position { x: 4, y: 4 }),
            "Adding Occupied should mark the tile as occupied in its chunk"
        );
    }

    #[test]
    fn should_unregister_occupied_tile_when_component_is_removed() {
        let mut app = App::new();
        app.add_plugins(MinimalPlugins);
        app.add_plugins(ChunkPlugin);

        let chunk_entity = app.world_mut().spawn(Chunk).id();
        app.world_mut()
            .resource_mut::<Chunks>()
            .insert(&Position { x: 9, y: 9 }, chunk_entity);

        let entity = app
            .world_mut()
            .spawn((Position { x: 9, y: 9 }, Floor { z: 1 }, Occupied))
            .id();

        // Removing the marker should release the occupied coordinate on the next update.
        app.update();
        app.world_mut().entity_mut(entity).remove::<Occupied>();
        app.update();

        let occupancy = app
            .world()
            .get::<Occupancy>(chunk_entity)
            .expect("Chunk should carry Occupancy");

        assert!(
            !occupancy.contains(&Floor { z: 1 }, &Position { x: 9, y: 9 }),
            "Removing Occupied should release the tile from the chunk occupancy"
        );
    }

    #[test]
    fn should_register_occupied_tile_without_manual_at_chunk_component() {
        let mut app = App::new();
        app.add_plugins(MinimalPlugins);
        app.add_plugins(ChunkPlugin);

        let chunk_entity = app.world_mut().spawn(Chunk).id();
        app.world_mut()
            .resource_mut::<Chunks>()
            .insert(&Position { x: 16, y: 16 }, chunk_entity);

        app.world_mut()
            .spawn((Position { x: 16, y: 16 }, Floor { z: 0 }, Occupied));

        // Occupancy registration should resolve the owner from Position alone.
        app.update();

        let occupancy = app
            .world()
            .get::<Occupancy>(chunk_entity)
            .expect("Chunk should carry Occupancy");

        assert!(
            occupancy.contains(&Floor { z: 0 }, &Position { x: 16, y: 16 }),
            "Sync should not require AtChunk when the chunk can be derived from Position"
        );
    }

    #[test]
    fn should_ignore_registration_when_position_has_no_registered_chunk() {
        let mut app = App::new();
        app.add_plugins(MinimalPlugins);
        app.add_plugins(ChunkPlugin);

        let chunk_entity = app.world_mut().spawn(Chunk).id();
        app.world_mut()
            .spawn((Position { x: 30, y: 30 }, Floor { z: 2 }, Occupied));

        // If the registry does not know the position, synchronization should no-op.
        app.update();

        let occupancy = app
            .world()
            .get::<Occupancy>(chunk_entity)
            .expect("Chunk should carry Occupancy");

        assert!(
            !occupancy.contains(&Floor { z: 2 }, &Position { x: 30, y: 30 }),
            "Sync should be a no-op when the position is not mapped in Chunks"
        );
    }

    #[test]
    fn should_track_floors_independently_for_same_position() {
        let mut occupancy = Occupancy::default();
        let tracked_position = Position { x: 2, y: 2 };

        // The same world coordinate can be occupied independently on different floors.
        assert!(
            occupancy.occupy(Floor { z: 0 }, tracked_position),
            "A fresh floor-position pair should be inserted"
        );

        assert!(
            occupancy.occupy(Floor { z: 1 }, tracked_position),
            "The same position on another floor should be tracked independently"
        );

        assert!(
            occupancy.contains(&Floor { z: 0 }, &tracked_position),
            "Floor zero should still contain the position"
        );

        assert!(
            occupancy.contains(&Floor { z: 1 }, &tracked_position),
            "Floor one should also contain the position"
        );

        assert!(
            occupancy.release(&Floor { z: 0 }, &tracked_position),
            "Releasing an occupied position should report success"
        );

        assert!(
            !occupancy.contains(&Floor { z: 0 }, &tracked_position),
            "The released floor should no longer contain the position"
        );

        assert!(
            occupancy.contains(&Floor { z: 1 }, &tracked_position),
            "Other floors should remain untouched by a release"
        );

        assert!(
            !occupancy.release(&Floor { z: 0 }, &tracked_position),
            "Releasing the same position twice should report that nothing changed"
        );
    }

    #[test]
    fn should_report_duplicate_occupy_on_same_floor_and_position() {
        let mut occupancy = Occupancy::default();
        let floor = Floor { z: 0 };
        let position = Position { x: 3, y: 3 };

        assert!(
            occupancy.occupy(floor, position),
            "The first insertion of a floor-position pair should succeed"
        );

        assert!(
            !occupancy.occupy(floor, position),
            "Reinserting the same floor-position pair should report no change"
        );
    }

    #[test]
    fn should_return_false_when_releasing_unknown_floor_or_position() {
        let mut occupancy = Occupancy::default();

        assert!(
            !occupancy.release(&Floor { z: 7 }, &Position { x: 11, y: 11 }),
            "Releasing a floor-position pair that was never occupied should report no change"
        );
    }

    #[test]
    fn should_ignore_registration_when_occupied_entity_is_missing_floor() {
        let mut app = App::new();
        app.add_plugins(MinimalPlugins);
        app.add_plugins(ChunkPlugin);

        let chunk_entity = app.world_mut().spawn(Chunk).id();
        app.world_mut()
            .resource_mut::<Chunks>()
            .insert(&Position { x: 4, y: 4 }, chunk_entity);

        app.world_mut().spawn((Position { x: 4, y: 4 }, Occupied));

        app.update();

        let occupancy = app
            .world()
            .get::<Occupancy>(chunk_entity)
            .expect("Chunk should carry Occupancy");

        assert!(
            !occupancy.contains(&Floor { z: 0 }, &Position { x: 4, y: 4 }),
            "Registration should no-op when the occupied entity does not have a Floor"
        );
    }

    #[test]
    fn should_resync_occupied_entity_after_position_change_in_same_chunk() {
        let mut app = App::new();
        app.add_plugins(MinimalPlugins);
        app.add_plugins(ChunkPlugin);

        let chunk_entity = app.world_mut().spawn(Chunk).id();
        app.world_mut()
            .resource_mut::<Chunks>()
            .insert(&Position { x: 1, y: 1 }, chunk_entity);
        app.world_mut()
            .resource_mut::<Chunks>()
            .insert(&Position { x: 2, y: 1 }, chunk_entity);

        let entity = app
            .world_mut()
            .spawn((Position { x: 1, y: 1 }, Floor { z: 0 }, Occupied))
            .id();

        // Updating Position with a PreviousPosition should migrate occupancy in place.
        app.update();

        app.world_mut()
            .entity_mut(entity)
            .insert((PreviousPosition { x: 1, y: 1 }, Position { x: 2, y: 1 }));

        app.update();

        let occupancy = app
            .world()
            .get::<Occupancy>(chunk_entity)
            .expect("Chunk should carry Occupancy");

        assert!(
            !occupancy.contains(&Floor { z: 0 }, &Position { x: 1, y: 1 }),
            "The old position should be released after a move"
        );

        assert!(
            occupancy.contains(&Floor { z: 0 }, &Position { x: 2, y: 1 }),
            "The new position should be registered after a move"
        );
    }

    #[test]
    fn should_resync_occupied_entity_across_chunks() {
        let mut app = App::new();
        app.add_plugins(MinimalPlugins);
        app.add_plugins(ChunkPlugin);

        let start = Position { x: 7, y: 7 };
        let target = Position { x: 8, y: 7 };

        let start_chunk = app.world_mut().spawn(Chunk).id();
        let target_chunk = app.world_mut().spawn(Chunk).id();

        app.world_mut()
            .resource_mut::<Chunks>()
            .insert(&start, start_chunk);
        app.world_mut()
            .resource_mut::<Chunks>()
            .insert(&target, target_chunk);

        let entity = app
            .world_mut()
            .spawn((start, Floor { z: 0 }, Occupied))
            .id();

        // Cross-chunk movement should release the source chunk and populate the target.
        app.update();

        app.world_mut().entity_mut(entity).insert((
            PreviousPosition {
                x: start.x,
                y: start.y,
            },
            target,
        ));

        app.update();

        let start_occupancy = app
            .world()
            .get::<Occupancy>(start_chunk)
            .expect("Start chunk should carry Occupancy");
        let target_occupancy = app
            .world()
            .get::<Occupancy>(target_chunk)
            .expect("Target chunk should carry Occupancy");

        assert!(
            !start_occupancy.contains(&Floor { z: 0 }, &start),
            "Cross-chunk movement should release the old chunk occupancy"
        );

        assert!(
            target_occupancy.contains(&Floor { z: 0 }, &target),
            "Cross-chunk movement should register occupancy in the target chunk"
        );
    }

    #[test]
    fn should_release_previous_occupancy_even_when_new_position_has_no_registered_chunk() {
        let mut app = App::new();
        app.add_plugins(MinimalPlugins);
        app.add_plugins(ChunkPlugin);

        let start = Position { x: 1, y: 1 };
        let unmapped_target = Position { x: 40, y: 40 };
        let chunk_entity = app.world_mut().spawn(Chunk).id();
        app.world_mut()
            .resource_mut::<Chunks>()
            .insert(&start, chunk_entity);

        let entity = app
            .world_mut()
            .spawn((start, Floor { z: 0 }, Occupied))
            .id();

        app.update();

        app.world_mut().entity_mut(entity).insert((
            PreviousPosition {
                x: start.x,
                y: start.y,
            },
            unmapped_target,
        ));

        app.update();

        let occupancy = app
            .world()
            .get::<Occupancy>(chunk_entity)
            .expect("Chunk should carry Occupancy");

        assert!(
            !occupancy.contains(&Floor { z: 0 }, &start),
            "Moving to an unmapped position should still release the previous occupancy"
        );

        assert!(
            !occupancy.contains(&Floor { z: 0 }, &unmapped_target),
            "Moving to an unmapped position should not register occupancy anywhere"
        );
    }

    #[test]
    fn should_unregister_using_previous_position_when_removed_after_move() {
        let mut app = App::new();
        app.add_plugins(MinimalPlugins);
        app.add_plugins(ChunkPlugin);

        let start = Position { x: 7, y: 7 };
        let target = Position { x: 8, y: 7 };
        let start_chunk = app.world_mut().spawn(Chunk).id();
        let target_chunk = app.world_mut().spawn(Chunk).id();

        app.world_mut()
            .resource_mut::<Chunks>()
            .insert(&start, start_chunk);
        app.world_mut()
            .resource_mut::<Chunks>()
            .insert(&target, target_chunk);

        let entity = app
            .world_mut()
            .spawn((start, Floor { z: 0 }, Occupied))
            .id();

        app.update();

        app.world_mut().entity_mut(entity).insert((
            PreviousPosition {
                x: start.x,
                y: start.y,
            },
            target,
        ));

        app.update();
        app.world_mut().entity_mut(entity).remove::<Occupied>();
        app.update();

        let start_occupancy = app
            .world()
            .get::<Occupancy>(start_chunk)
            .expect("Start chunk should carry Occupancy");
        let target_occupancy = app
            .world()
            .get::<Occupancy>(target_chunk)
            .expect("Target chunk should carry Occupancy");

        assert!(
            !start_occupancy.contains(&Floor { z: 0 }, &start),
            "Removing Occupied after a move should not leave stale occupancy in the previous chunk"
        );

        assert!(
            !target_occupancy.contains(&Floor { z: 0 }, &target),
            "Removing Occupied after a move should release the current chunk occupancy too"
        );
    }

    #[test]
    fn should_unregister_current_position_when_previous_position_matches_current() {
        let mut app = App::new();
        app.add_plugins(MinimalPlugins);
        app.add_plugins(ChunkPlugin);

        let position = Position { x: 5, y: 5 };
        let chunk_entity = app.world_mut().spawn(Chunk).id();
        app.world_mut()
            .resource_mut::<Chunks>()
            .insert(&position, chunk_entity);

        let entity = app
            .world_mut()
            .spawn((
                position,
                PreviousPosition {
                    x: position.x,
                    y: position.y,
                },
                Floor { z: 0 },
                Occupied,
            ))
            .id();

        app.update();
        app.world_mut().entity_mut(entity).remove::<Occupied>();
        app.update();

        let occupancy = app
            .world()
            .get::<Occupancy>(chunk_entity)
            .expect("Chunk should carry Occupancy");

        assert!(
            !occupancy.contains(&Floor { z: 0 }, &position),
            "When previous and current positions match, removal should still release the current \
             occupancy"
        );
    }

    #[test]
    fn should_treat_other_floors_as_unoccupied() {
        let mut occupancy = Occupancy::default();
        let position = Position { x: 10, y: 10 };

        occupancy.occupy(Floor { z: 2 }, position);

        assert!(
            !occupancy.contains(&Floor { z: 3 }, &position),
            "contains should remain false for floors that were never occupied"
        );
    }
}