Module loot

Source
Expand description

Seeded weighted choices with setup-time validation, fixed storage and no floats.

Single RewardTable::choose and RewardTable::roll retain their legacy mappings. Distinct draws remove indices, not caller-defined reward values, from local scratch weights; authored weights never change. Each call starts afresh. Persist the accepted prefix for a menu; do not reroll on redraw.

A headless optional reward recipe, using accepted wave clears, actual earned promotions and capped upgrades (the existing games’ selection policies stay unchanged). Run with cargo test -p ferravex-play --doc gameplay::loot --offline --locked -j 2. This host confirms a defeat after an admitted spawn; a game supplies that fact from its combat system on an actual simulation tick.

use ferravex_play::gameplay::{
    loot::{DistinctDrawStatus, RewardRng, RewardTable},
    progression::Progression,
    upgrades::Upgrades,
    waves::{WaveSequence, WaveSpec},
};
let mut waves = WaveSequence::new([WaveSpec {
    credits: 1, interval: 0.125, rest: 0.0, reward: 10,
}], 1).unwrap();
let mut xp = Progression::new(10, 0, 4).unwrap();
let caps = [1, 1, 1];
let mut upgrades = Upgrades::new(caps).unwrap();
let mut rng = RewardRng::new(7);
let mut points = 0;
assert!(waves.admit(1, 0));
assert_eq!(waves.advance(0.125, 0.0, 1).unwrap(), None);
// The admitted actor has now been defeated; only the clear earns XP.
let clear = waves.advance(0.125, 0.0, 0).unwrap().unwrap();
points += xp.award(u64::from(clear.reward));
let authored = [3, 2, 1];
let weights: [u32; 3] = std::array::from_fn(|id| {
    if upgrades.rank(id).unwrap() < caps[id] { authored[id] } else { 0 }
});
// All-capped pools need no table or entropy. Never spend an unearned point.
if points > 0 && weights.iter().any(|&w| w > 0) {
    let table = RewardTable::new(weights).unwrap();
    let mut offers = [usize::MAX; 3];
    let report = table.roll_distinct_into(&mut rng, 3, &mut offers, 16).unwrap();
    assert_eq!(report.status, DistinctDrawStatus::Complete);
    // Keep this prefix across redraws. This headless host chooses its first
    // offer; an interactive host uses the player's submitted offered index.
    let displayed = &offers[..report.written];
    let chosen = displayed[0];
    if points > 0 && displayed.contains(&chosen)
        && upgrades.rank(chosen).unwrap() < caps[chosen]
    {
        points -= 1;
        upgrades.apply(chosen).unwrap();
    }
    assert_eq!(upgrades.rank(chosen), Some(1));
}
assert_eq!(points, 0);
assert_eq!(xp.level(), 2);
assert_eq!(waves.advance(0.125, 0.0, 0).unwrap(), None);

Structs§

DistinctDrawReport
Accepted prefix and exact input consumption, including rejected RNG samples.
RewardRng
RewardTable

Enums§

DistinctDrawError
Preflight failures: neither caller output nor RNG is changed.
DistinctDrawStatus
Why a distinct call stopped; completion takes precedence over pool exhaustion, which takes precedence over input exhaustion. See DistinctDrawReport.

Constants§

MAX_DISTINCT_DRAWS
Maximum request size for either distinct operation, independent of table size.
MAX_DISTINCT_SAMPLES
Maximum number of SplitMix64 outputs consumed by one distinct roll call.