voxel-framework 0.10.0

A reusable voxel engine framework built on Bevy
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
//! Drop table, world-entity lifecycle, and magnet pickup systems.
//!
//! When a voxel is destroyed the mining subsystem emits a
//! [`crate::world::VoxelBroken`] message.  This module listens to that message
//! and drives the full item-drop pipeline:
//!
//! 1. **Spawn** — `spawn_drops_system` consults [`DropTable`] and spawns a
//!    [`DroppedItem`] entity at the broken voxel's world position.
//! 2. **Attract** — `magnet_system` applies a configurable acceleration toward
//!    any entity tagged with [`ItemCollector`] once the item is within
//!    [`MagnetConfig::radius`] world units.
//! 3. **Collect** — when the item reaches within 0.5 units of the collector,
//!    `magnet_system` calls [`crate::items::inventory::Inventory::add`], emits
//!    [`crate::items::inventory::ItemPickedUp`], and despawns the entity.
//! 4. **Expire** — items that are never collected despawn automatically after
//!    their [`DroppedItem::lifetime`] reaches zero (default 60 seconds).
//!
//! # Plugin registration
//!
//! [`DropsPlugin`] is intentionally **not** bundled inside
//! [`crate::VoxelPlugin`] because it is generic over `I: `[`ItemType`].
//! Add it explicitly in game code, alongside
//! [`crate::generator::GeneratorPlugin`]:
//!
//! ```rust,no_run
//! use bevy::prelude::*;
//! use voxel_framework::items::{DefaultItem, DropsPlugin};
//!
//! App::new().add_plugins(DropsPlugin::<DefaultItem>::new());
//! ```

use std::collections::HashMap;
use std::marker::PhantomData;

use bevy::prelude::*;

use crate::world::VoxelBroken;

use super::inventory::{ContainerComponent, ItemPickedUp, ItemType, PrimaryContainer};

// ─── DropTable ────────────────────────────────────────────────────────────────

/// One runtime drop entry for a voxel byte.
///
/// This is the runtime counterpart of
/// [`crate::content::BlockDropDefinition`]. It is intentionally keyed by voxel
/// byte because [`DropsPlugin`] reacts to [`crate::world::VoxelBroken`] events,
/// and those events carry the destroyed voxel byte.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DropTableEntry<I: ItemType> {
    /// Item produced by this entry.
    pub item: I,
    /// Minimum count, inclusive.
    pub min_count: u32,
    /// Maximum count, inclusive.
    pub max_count: u32,
    /// Probability in `0.0..=1.0`.
    pub chance: f32,
}

impl<I: ItemType> DropTableEntry<I> {
    /// Creates a guaranteed drop entry.
    pub fn new(item: I, min_count: u32, max_count: u32) -> Self {
        Self::with_chance(item, min_count, max_count, 1.0)
    }

    /// Creates a drop entry with an explicit probability.
    pub fn with_chance(item: I, min_count: u32, max_count: u32, chance: f32) -> Self {
        Self {
            item,
            min_count: min_count.min(max_count),
            max_count: min_count.max(max_count),
            chance: chance.clamp(0.0, 1.0),
        }
    }
}

/// Mapping from voxel byte IDs to item drop definitions.
///
/// Each entry associates a voxel byte value (the `u8` returned by
/// [`crate::voxel::VoxelType::to_byte`]) with an item type and a drop-count
/// range `[min, max]`. Runtime entries may also carry a chance, and a voxel may
/// have multiple entries. When a matching voxel is broken,
/// `spawn_drops_system` queries this table and selects counts/chance outcomes
/// using deterministic pseudo-random values seeded from the voxel's world
/// position. If `min == max` the count is always fixed.
///
/// [`DropTable`] is initialized empty; register entries before the first
/// [`crate::world::VoxelBroken`] message is processed.  It is available as a
/// Bevy [`Resource`] after [`DropsPlugin`] is added.
///
/// # Examples
///
/// ```rust,no_run
/// use voxel_framework::items::{DropTable, DefaultItem};
///
/// let mut table = DropTable::<DefaultItem>::new();
/// // Voxel byte 1 drops 1–3 rock fragments.
/// table.register(1, DefaultItem::RockFragment, 1, 3);
/// // Voxel byte 2 always drops exactly 1 iron ore.
/// table.register(2, DefaultItem::IronOre, 1, 1);
/// // Voxel byte 3 can produce multiple runtime entries.
/// table.add_entry(3, voxel_framework::items::DropTableEntry::new(DefaultItem::Cobblestone, 1, 1));
/// table.add_entry(3, voxel_framework::items::DropTableEntry::with_chance(DefaultItem::Coal, 1, 2, 0.25));
/// ```
#[derive(Resource, Debug, Clone)]
pub struct DropTable<I: ItemType> {
    /// Internal map: `voxel_byte` → runtime drop entries.
    entries: HashMap<u8, Vec<DropTableEntry<I>>>,
}

impl<I: ItemType> DropTable<I> {
    /// Creates an empty drop table.
    pub fn new() -> Self {
        Self {
            entries: HashMap::new(),
        }
    }

    /// Registers a drop for the given voxel byte.
    ///
    /// When a voxel with `voxel_byte` is broken, between `min` and `max` items
    /// (inclusive) of `item` will be dropped. If `min == max` the count is
    /// always fixed.
    ///
    /// This replaces any previous runtime entries for the voxel byte, preserving
    /// the historical one-entry behavior of [`DropTable::register`]. Use
    /// [`DropTable::add_entry`] when a voxel should produce multiple drops.
    pub fn register(&mut self, voxel_byte: u8, item: I, min: u32, max: u32) {
        self.replace_entries(voxel_byte, [DropTableEntry::new(item, min, max)]);
    }

    /// Registers a single probabilistic drop for the given voxel byte.
    ///
    /// Like [`DropTable::register`], this replaces any previous entries for the
    /// voxel byte.
    pub fn register_with_chance(
        &mut self,
        voxel_byte: u8,
        item: I,
        min: u32,
        max: u32,
        chance: f32,
    ) {
        self.replace_entries(
            voxel_byte,
            [DropTableEntry::with_chance(item, min, max, chance)],
        );
    }

    /// Appends one runtime entry without removing existing entries for the same
    /// voxel byte.
    pub fn add_entry(&mut self, voxel_byte: u8, entry: DropTableEntry<I>) {
        self.entries.entry(voxel_byte).or_default().push(entry);
    }

    /// Replaces all runtime entries for one voxel byte.
    pub fn replace_entries(
        &mut self,
        voxel_byte: u8,
        entries: impl IntoIterator<Item = DropTableEntry<I>>,
    ) {
        self.entries
            .insert(voxel_byte, entries.into_iter().collect());
    }

    /// Iterates over all explicitly registered drop entries.
    ///
    /// Yields `(voxel_byte, item, min_count, max_count)` for each registration.
    /// Identity-fallback entries (not explicitly registered) are not included.
    pub fn iter_entries(&self) -> impl Iterator<Item = (u8, I, u32, u32)> + '_ {
        self.iter_drop_entries()
            .map(|(voxel_byte, entry)| (voxel_byte, entry.item, entry.min_count, entry.max_count))
    }

    /// Iterates over all explicitly registered runtime drop entries, including
    /// chance metadata.
    pub fn iter_drop_entries(&self) -> impl Iterator<Item = (u8, &DropTableEntry<I>)> + '_ {
        self.entries
            .iter()
            .flat_map(|(&voxel_byte, entries)| entries.iter().map(move |entry| (voxel_byte, entry)))
    }

    /// Returns the drop for `voxel_byte`, using `seed` for deterministic count
    /// selection within `[min, max]`.
    ///
    /// Explicit registrations always win. If no entry is registered for
    /// `voxel_byte`, falls back to the identity mapping: `I::from_byte(voxel_byte)`
    /// with count 1. If the resulting item is the empty sentinel (i.e.
    /// `item.to_byte() == 0`), returns `None` — meaning no drop.
    pub fn get_drop(&self, voxel_byte: u8, seed: u64) -> Option<(I, u32)> {
        self.get_drops(voxel_byte, seed).into_iter().next()
    }

    /// Returns all drops produced for `voxel_byte` with deterministic chance
    /// rolls and count selection.
    pub fn get_drops(&self, voxel_byte: u8, seed: u64) -> Vec<(I, u32)> {
        if let Some(entries) = self.entries.get(&voxel_byte) {
            return entries
                .iter()
                .enumerate()
                .filter_map(|(index, entry)| entry.roll(seed, index as u64))
                .collect();
        }

        // Identity fallback: try mapping the voxel byte directly to an item.
        let item = I::from_byte(voxel_byte);
        if !item.is_empty() {
            vec![(item, 1)]
        } else {
            Vec::new()
        }
    }
}

impl<I: ItemType> DropTableEntry<I> {
    fn roll(&self, seed: u64, salt: u64) -> Option<(I, u32)> {
        if self.item.is_empty() || self.max_count == 0 || self.chance <= 0.0 {
            return None;
        }

        let chance_roll = unit_interval(seed, salt);
        if chance_roll > self.chance {
            return None;
        }

        let count = if self.min_count == self.max_count {
            self.min_count
        } else {
            let range = (self.max_count - self.min_count + 1) as u64;
            self.min_count + (lcg(seed, salt.wrapping_add(1)) % range) as u32
        };

        (count > 0).then_some((self.item, count))
    }
}

fn lcg(seed: u64, salt: u64) -> u64 {
    seed.wrapping_add(salt.wrapping_mul(0x9E37_79B9_7F4A_7C15))
        .wrapping_mul(6364136223846793005)
        .wrapping_add(1442695040888963407)
}

fn unit_interval(seed: u64, salt: u64) -> f32 {
    let value = lcg(seed, salt) >> 40;
    value as f32 / ((1u64 << 24) - 1) as f32
}

impl<I: ItemType> Default for DropTable<I> {
    fn default() -> Self {
        Self::new()
    }
}

// ─── MagnetConfig ─────────────────────────────────────────────────────────────

/// Configuration for the magnetic item-attraction system.
///
/// This resource is inserted by [`DropsPlugin`] with [`Default`] values and
/// can be overwritten after plugin registration to tune pickup feel.
///
/// # Examples
///
/// ```rust,no_run
/// use bevy::prelude::*;
/// use voxel_framework::items::MagnetConfig;
///
/// fn setup(mut config: ResMut<MagnetConfig>) {
///     config.radius = 8.0;
///     config.force  = 16.0;
/// }
/// ```
#[derive(Resource, Debug, Clone)]
pub struct MagnetConfig {
    /// Distance (in world units) within which [`DroppedItem`] entities are
    /// attracted toward the [`ItemCollector`].
    ///
    /// Items outside this radius are neither attracted nor collected.
    /// Default: `4.0`.
    pub radius: f32,
    /// Acceleration magnitude applied toward the collector each second
    /// (world units/sec²).
    ///
    /// Higher values make items snap to the player faster.  Default: `8.0`.
    pub force: f32,
}

impl Default for MagnetConfig {
    fn default() -> Self {
        Self {
            radius: 4.0,
            force: 8.0,
        }
    }
}

// ─── Components ───────────────────────────────────────────────────────────────

/// Component on a world entity representing an uncollected item drop.
///
/// `DroppedItem` entities are spawned by `spawn_drops_system` in response to
/// [`crate::world::VoxelBroken`] messages and despawned by `magnet_system`
/// when either collected or expired.
///
/// The plugin spawns each drop with a [`Transform`] at the broken voxel's
/// world-space center, ready for spatial magnet logic. Manually spawned drops
/// must also provide a [`Transform`] to participate in spatial attraction.
///
/// # Lifecycle
///
/// 1. Spawned with `velocity` set to `(0, 2, 0)` (small upward bounce).
/// 2. Attracted toward the [`ItemCollector`] once within [`MagnetConfig::radius`].
/// 3. Collected when within 0.5 world units of the collector. A full inventory
///    that accepts no items leaves the drop available until pickup or expiry.
///    Partial pickups leave the unaccepted remainder in the world.
/// 4. Despawned after `lifetime` seconds regardless of collection.
#[derive(Component, Debug, Clone)]
pub struct DroppedItem<I: ItemType> {
    /// The item type carried by this drop entity.
    pub item: I,
    /// Number of items in this drop stack.  Always `>= 1`.
    pub count: u32,
    /// Current velocity in world space (units/sec).
    ///
    /// The magnet system accelerates drops inside the attraction radius and
    /// advances their position by velocity times elapsed time while a collector
    /// is available. Outside the radius, existing velocity is preserved.
    /// Game code can modify velocity for custom gravity or bounce; it should
    /// not integrate the same movement a second time.
    pub velocity: Vec3,
    /// Seconds remaining before the entity is automatically despawned.
    ///
    /// Decremented each frame.  Default: `60.0`.
    pub lifetime: f32,
}

/// Tag component marking the entity that attracts and collects nearby [`DroppedItem`] entities.
///
/// Add this component to the player entity after spawning it.  Only one
/// collector entity is supported; `magnet_system` uses
/// [`bevy::ecs::query::QuerySingleError`] and silently skips collection if
/// zero or more than one entity carries this tag.
///
/// # Examples
///
/// ```rust,no_run
/// use bevy::prelude::*;
/// use voxel_framework::items::ItemCollector;
///
/// fn spawn_player(mut commands: Commands) {
///     commands.spawn((Transform::default(), ItemCollector));
/// }
/// ```
#[derive(Component, Default)]
pub struct ItemCollector;

// ─── Systems ─────────────────────────────────────────────────────────────────

/// Reads [`VoxelBroken`] messages and spawns [`DroppedItem`] entities using
/// [`DropTable`].
fn spawn_drops_system<I: ItemType>(
    mut broken_reader: MessageReader<VoxelBroken>,
    drop_table: Res<DropTable<I>>,
    mut commands: Commands,
) {
    for event in broken_reader.read() {
        // Use world position bits as a deterministic seed.
        let seed = {
            let x = event.world_pos.x.to_bits() as u64;
            let y = event.world_pos.y.to_bits() as u64;
            let z = event.world_pos.z.to_bits() as u64;
            x.wrapping_mul(1000003)
                .wrapping_add(y.wrapping_mul(1000033))
                .wrapping_add(z)
        };

        for (item, count) in drop_table.get_drops(event.voxel_byte, seed) {
            commands.spawn((
                DroppedItem {
                    item,
                    count,
                    velocity: Vec3::new(0.0, 2.0, 0.0),
                    lifetime: 60.0,
                },
                Transform::from_translation(event.world_pos),
            ));
        }
    }
}

/// Applies magnet force toward [`ItemCollector`] and picks up items within
/// pickup range, adding them to the [`ContainerComponent`] on the
/// [`PrimaryContainer`] entity and emitting [`ItemPickedUp`].
fn magnet_system<I: ItemType>(
    time: Res<Time>,
    config: Res<MagnetConfig>,
    collector_query: Query<&Transform, (With<ItemCollector>, Without<DroppedItem<I>>)>,
    mut item_query: Query<(Entity, &mut DroppedItem<I>, Option<&mut Transform>)>,
    mut container_query: Query<&mut ContainerComponent<I>, With<PrimaryContainer>>,
    mut pickup_writer: MessageWriter<ItemPickedUp<I>>,
    mut commands: Commands,
) {
    let Ok(collector_transform) = collector_query.single() else {
        return;
    };
    let collector_pos = collector_transform.translation;
    let dt = time.delta_secs();

    for (entity, mut dropped, transform_opt) in item_query.iter_mut() {
        // Tick lifetime.
        dropped.lifetime -= dt;
        if dropped.lifetime <= 0.0 {
            commands.entity(entity).despawn();
            continue;
        }

        // If entity has no transform we can't do spatial logic — skip.
        let Some(mut transform) = transform_opt else {
            continue;
        };

        let item_pos = transform.translation;
        let diff = collector_pos - item_pos;
        let dist = diff.length();

        if dist < 0.5 {
            // Close enough — pick up via PrimaryContainer entity.
            let Ok(mut container) = container_query.single_mut() else {
                continue;
            };
            let added = dropped.count - container.0.add(dropped.item, dropped.count);
            if added == 0 {
                continue;
            }
            dropped.count -= added;
            pickup_writer.write(ItemPickedUp {
                item: dropped.item,
                count: added,
            });
            if dropped.count == 0 {
                commands.entity(entity).despawn();
            }
            continue;
        } else if dist < config.radius {
            // Apply attraction force.
            let dir = diff / dist;
            dropped.velocity += dir * config.force * dt;
        }
        transform.translation += dropped.velocity * dt;
    }
}

// ─── DropsPlugin ─────────────────────────────────────────────────────────────

/// Plugin that wires up the full item-drop pipeline for item type `I`.
///
/// Registering this plugin inserts the following resources (with defaults) and
/// systems into the Bevy [`App`]:
///
/// | Resource | Purpose |
/// |---|---|
/// | [`DropTable<I>`] | Empty table; populate it in a `Startup` system |
/// | [`MagnetConfig`] | Tunable attraction radius and force |
/// | [`crate::items::inventory::Inventory<I>`] | Player's item storage |
///
/// It also registers the [`crate::items::inventory::ItemPickedUp<I>`] message
/// type and adds `spawn_drops_system` and `magnet_system` to the `Update`
/// schedule.
///
/// **This plugin is NOT included in [`crate::VoxelPlugin`]** — because it is
/// generic over `I: `[`ItemType`], it must be added separately by game code.
/// Add it alongside [`crate::generator::GeneratorPlugin`]:
///
/// ```rust,no_run
/// use bevy::prelude::*;
/// use voxel_framework::items::{DefaultItem, DropsPlugin};
///
/// App::new().add_plugins(DropsPlugin::<DefaultItem>::new());
/// ```
pub struct DropsPlugin<I: ItemType> {
    _marker: PhantomData<I>,
}

impl<I: ItemType> DropsPlugin<I> {
    /// Creates a new `DropsPlugin`.
    pub fn new() -> Self {
        Self {
            _marker: PhantomData,
        }
    }
}

impl<I: ItemType> Default for DropsPlugin<I> {
    fn default() -> Self {
        Self::new()
    }
}

impl<I: ItemType> Plugin for DropsPlugin<I> {
    fn build(&self, app: &mut App) {
        app.init_resource::<DropTable<I>>();
        app.init_resource::<MagnetConfig>();
        app.add_message::<ItemPickedUp<I>>();
        app.add_systems(Update, spawn_drops_system::<I>);
        app.add_systems(Update, magnet_system::<I>);
    }
}

// ─── Tests ───────────────────────────────────────────────────────────────────

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

    #[test]
    fn broken_voxel_spawns_one_mapped_drop_at_world_position() {
        let mut app = App::new();
        app.init_resource::<Time>()
            .add_message::<VoxelBroken>()
            .add_plugins(DropsPlugin::<DefaultItem>::new());
        app.world_mut()
            .resource_mut::<DropTable<DefaultItem>>()
            .register(1, DefaultItem::RockFragment, 1, 1);
        let position = Vec3::new(18.5, -12.5, 36.5);
        app.world_mut().write_message(VoxelBroken {
            chunk_coord: IVec3::new(1, -1, 2),
            local: (2, 3, 4),
            world_pos: position,
            voxel_byte: 1,
        });

        app.update();

        let world = app.world_mut();
        let mut query = world.query::<(&DroppedItem<DefaultItem>, Option<&Transform>)>();
        let drops: Vec<_> = query.iter(world).collect();
        assert_eq!(drops.len(), 1);
        assert_eq!(drops[0].0.item, DefaultItem::RockFragment);
        assert_eq!(drops[0].0.count, 1);
        assert_eq!(
            drops[0]
                .1
                .expect("spawned drop must have a world position")
                .translation,
            position
        );
    }

    #[test]
    fn nearby_drop_moves_and_is_collected_once_without_attracting_distant_drop() {
        let mut app = App::new();
        app.init_resource::<Time>()
            .add_message::<VoxelBroken>()
            .add_plugins(DropsPlugin::<DefaultItem>::new());
        let player = app
            .world_mut()
            .spawn((
                Transform::default(),
                ItemCollector,
                PrimaryContainer,
                ContainerComponent::<DefaultItem>::new(9),
            ))
            .id();
        let spawn_drop = |world: &mut World, position| {
            world
                .spawn((
                    DroppedItem {
                        item: DefaultItem::RockFragment,
                        count: 1,
                        velocity: Vec3::ZERO,
                        lifetime: 60.0,
                    },
                    Transform::from_translation(position),
                ))
                .id()
        };
        let nearby = spawn_drop(app.world_mut(), Vec3::new(2.0, 0.0, 0.0));
        let distant_position = Vec3::new(8.0, 0.0, 0.0);
        let distant = spawn_drop(app.world_mut(), distant_position);
        app.world_mut()
            .resource_mut::<Time>()
            .advance_by(std::time::Duration::from_millis(100));

        app.update();
        assert!(
            app.world()
                .get::<Transform>(nearby)
                .unwrap()
                .translation
                .length()
                < 2.0,
            "nearby drop must move toward the collector"
        );

        let mut pickups = Vec::new();
        for _ in 0..30 {
            app.world_mut()
                .resource_mut::<Time>()
                .advance_by(std::time::Duration::from_millis(100));
            app.update();
            pickups.extend(
                app.world_mut()
                    .resource_mut::<Messages<ItemPickedUp<DefaultItem>>>()
                    .drain(),
            );
        }
        assert!(app.world().get_entity(nearby).is_err());
        assert_eq!(
            app.world()
                .get::<ContainerComponent<DefaultItem>>(player)
                .unwrap()
                .0
                .count_of(DefaultItem::RockFragment),
            1
        );
        assert_eq!(pickups.len(), 1);
        assert_eq!(pickups[0].item, DefaultItem::RockFragment);
        assert_eq!(pickups[0].count, 1);
        assert_eq!(
            app.world().get::<Transform>(distant).unwrap().translation,
            distant_position
        );
        assert_eq!(
            app.world()
                .get::<DroppedItem<DefaultItem>>(distant)
                .unwrap()
                .velocity,
            Vec3::ZERO
        );
    }

    #[test]
    fn partial_pickup_retains_remainder_until_inventory_has_room() {
        let mut app = App::new();
        app.init_resource::<Time>()
            .add_message::<VoxelBroken>()
            .add_plugins(DropsPlugin::<DefaultItem>::new());
        let item = DefaultItem::RockFragment;
        let mut container = ContainerComponent::<DefaultItem>::new(1);
        assert_eq!(container.0.add(item, u32::MAX - 1), 0);
        let player = app
            .world_mut()
            .spawn((
                Transform::default(),
                ItemCollector,
                PrimaryContainer,
                container,
            ))
            .id();
        let drop = app
            .world_mut()
            .spawn((
                DroppedItem {
                    item,
                    count: 3,
                    velocity: Vec3::ZERO,
                    lifetime: 60.0,
                },
                Transform::from_xyz(0.25, 0.0, 0.0),
            ))
            .id();

        for expected_pickup in [1, 0, 2] {
            if expected_pickup == 2 {
                assert_eq!(
                    app.world_mut()
                        .get_mut::<ContainerComponent<DefaultItem>>(player)
                        .unwrap()
                        .0
                        .remove(item, 2),
                    2
                );
            }
            app.update();
            assert_eq!(
                app.world()
                    .get::<ContainerComponent<DefaultItem>>(player)
                    .unwrap()
                    .0
                    .count_of(item),
                u32::MAX
            );
            if expected_pickup == 2 {
                assert!(app.world().get_entity(drop).is_err());
            } else {
                let retained = app
                    .world()
                    .get::<DroppedItem<DefaultItem>>(drop)
                    .expect("partial pickup must preserve the remainder");
                assert_eq!(retained.item, item);
                assert_eq!(retained.count, 2);
            }
            let pickups: Vec<_> = app
                .world_mut()
                .resource_mut::<Messages<ItemPickedUp<DefaultItem>>>()
                .drain()
                .collect();
            if expected_pickup == 0 {
                assert!(pickups.is_empty());
            } else {
                assert_eq!(pickups.len(), 1);
                assert_eq!(pickups[0].item, item);
                assert_eq!(pickups[0].count, expected_pickup);
            }
        }
    }

    #[test]
    fn full_inventory_retains_drop_without_reporting_pickup() {
        let mut app = App::new();
        app.init_resource::<Time>()
            .add_message::<VoxelBroken>()
            .add_plugins(DropsPlugin::<DefaultItem>::new());
        let mut container = ContainerComponent::<DefaultItem>::new(1);
        assert_eq!(container.0.add(DefaultItem::IronOre, 1), 0);
        let player = app
            .world_mut()
            .spawn((
                Transform::default(),
                ItemCollector,
                PrimaryContainer,
                container,
            ))
            .id();
        let drop = app
            .world_mut()
            .spawn((
                DroppedItem {
                    item: DefaultItem::RockFragment,
                    count: 3,
                    velocity: Vec3::ZERO,
                    lifetime: 60.0,
                },
                Transform::from_xyz(0.25, 0.0, 0.0),
            ))
            .id();

        for _ in 0..3 {
            app.world_mut()
                .resource_mut::<Time>()
                .advance_by(std::time::Duration::from_millis(100));
            app.update();
            let retained = app
                .world()
                .get::<DroppedItem<DefaultItem>>(drop)
                .expect("full inventory must retain the unaccepted drop");
            assert_eq!(retained.item, DefaultItem::RockFragment);
            assert_eq!(retained.count, 3);
            let inventory = &app
                .world()
                .get::<ContainerComponent<DefaultItem>>(player)
                .unwrap()
                .0;
            assert_eq!(inventory.count_of(DefaultItem::IronOre), 1);
            assert_eq!(inventory.count_of(DefaultItem::RockFragment), 0);
            assert_eq!(
                app.world_mut()
                    .resource_mut::<Messages<ItemPickedUp<DefaultItem>>>()
                    .drain()
                    .count(),
                0
            );
        }
    }

    #[test]
    fn test_drop_table_get_drop() {
        let mut table = DropTable::<DefaultItem>::new();
        table.register(1, DefaultItem::RockFragment, 1, 3);

        // With seed 0: deterministic count should be in [1, 3].
        let result = table.get_drop(1, 0);
        assert!(result.is_some());
        let (item, count) = result.unwrap();
        assert_eq!(item, DefaultItem::RockFragment);
        assert!((1..=3).contains(&count));
    }

    #[test]
    fn test_drop_table_no_entry_returns_none() {
        let table = DropTable::<DefaultItem>::new();
        assert!(table.get_drop(99, 42).is_none());
    }

    #[test]
    fn test_drop_table_fixed_count() {
        let mut table = DropTable::<DefaultItem>::new();
        table.register(2, DefaultItem::IronOre, 5, 5);
        let (_, count) = table.get_drop(2, 12345).unwrap();
        assert_eq!(count, 5);
    }

    #[test]
    fn test_drop_table_identity_fallback_with_other_entries() {
        let mut table = DropTable::<DefaultItem>::new();
        table.register(2, DefaultItem::Dirt, 1, 1);
        let (item, count) = table.get_drop(11, 0).unwrap();
        assert_eq!(item, DefaultItem::IronOre);
        assert_eq!(count, 1);
    }

    #[test]
    fn drop_table_supports_multiple_runtime_entries() {
        let mut table = DropTable::<DefaultItem>::new();
        table.add_entry(3, DropTableEntry::new(DefaultItem::Cobblestone, 1, 1));
        table.add_entry(3, DropTableEntry::new(DefaultItem::Coal, 2, 2));

        let drops = table.get_drops(3, 123);
        assert_eq!(drops.len(), 2);
        assert_eq!(drops[0], (DefaultItem::Cobblestone, 1));
        assert_eq!(drops[1], (DefaultItem::Coal, 2));
    }

    #[test]
    fn register_preserves_single_entry_replacement_behavior() {
        let mut table = DropTable::<DefaultItem>::new();
        table.add_entry(3, DropTableEntry::new(DefaultItem::Cobblestone, 1, 1));
        table.add_entry(3, DropTableEntry::new(DefaultItem::Coal, 1, 1));

        table.register(3, DefaultItem::IronOre, 1, 1);

        let drops = table.get_drops(3, 0);
        assert_eq!(drops, vec![(DefaultItem::IronOre, 1)]);
    }

    #[test]
    fn drop_table_entry_chance_can_disable_a_drop() {
        let mut table = DropTable::<DefaultItem>::new();
        table.add_entry(
            3,
            DropTableEntry::with_chance(DefaultItem::Cobblestone, 1, 1, 0.0),
        );
        table.add_entry(3, DropTableEntry::with_chance(DefaultItem::Coal, 1, 1, 1.0));

        let drops = table.get_drops(3, 0);
        assert_eq!(drops, vec![(DefaultItem::Coal, 1)]);
    }

    #[test]
    fn iter_drop_entries_includes_chance_metadata() {
        let mut table = DropTable::<DefaultItem>::new();
        table.register_with_chance(3, DefaultItem::Cobblestone, 1, 2, 0.25);

        let entries: Vec<_> = table.iter_drop_entries().collect();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].0, 3);
        assert_eq!(entries[0].1.item, DefaultItem::Cobblestone);
        assert_eq!(entries[0].1.min_count, 1);
        assert_eq!(entries[0].1.max_count, 2);
        assert_eq!(entries[0].1.chance, 0.25);
    }
}