Struct RewardTable

Source
pub struct RewardTable<const N: usize> { /* private fields */ }

Implementations§

Source§

impl<const N: usize> RewardTable<N>

Source

pub fn new(weights: [u32; N]) -> Result<RewardTable<N>, LoopError>

Source

pub fn choose(&self, ticket: u32) -> usize

Caller can supply a saved/replayed ticket. Zero weight is never selected.

Source

pub fn roll(&self, rng: &mut RewardRng) -> usize

Fixed work; multiply-high maps the full 64-bit sample to the weight range. This legacy mapping has rounding bias for totals that do not divide 2^64; its results and one-output RNG advancement are preserved.

Source

pub fn roll_distinct_into( &self, rng: &mut RewardRng, requested: usize, out: &mut [usize], sample_budget: usize, ) -> Result<DistinctDrawReport, DistinctDrawError>

Weighted sampling without replacement, into caller storage.

For each attempt, consume exactly one RewardRng::next_u64 output and use its upper 32 bits as x. For remaining total W, reject x < (2^32 mod W); otherwise use x % W as a cumulative-weight ticket, in original index order. Remove the selected index’s entire weight before the next draw. Zero-weight entries never appear; no duplicate retries occur. Even W == 1 consumes a sample for an accepted draw.

With independent uniform 32-bit samples, rejection leaves equally many representatives of every ticket. Conditional on a next draw succeeding, its index probability is weight[index] / W. A finite budget can censor the prefix depending on earlier choices, so conditioning on an entire request completing need not retain that distribution. SplitMix64 is a deterministic gameplay PRNG, not a claim of independent random entropy or security. There is no biased fallback on budget exhaustion.

Preflight checks, in order: requested <= MAX_DISTINCT_DRAWS, output has at least min(requested, positive_weight_entries) slots, then sample_budget <= MAX_DISTINCT_SAMPLES. Errors consume no RNG and write nothing. Zero budget is valid: a nonzero request returns an empty prefix with SampleBudgetExhausted; a zero request completes without samples. Accepted or rejected attempts each consume one budget unit and RNG output. Only the accepted prefix is written, including on exhaustion; no rollback.

At most N <= 256 scratch weights (1024 bytes), requested <= 256 writes, and sample_budget <= 4096 samples. Work is O(N + sample_budget + N * min(requested, positive_weight_entries)), without allocation, floating point, callbacks, wall clocks or authored-weight mutation. A fresh call restarts the pool; do not concatenate partial calls as a continuation.

use ferravex_play::gameplay::loot::{RewardRng, RewardTable, DistinctDrawStatus};
let table = RewardTable::new([1, 0, 3]).unwrap();
let mut rng = RewardRng::new(0);
let mut out = [99; 3];
let report = table.roll_distinct_into(&mut rng, 3, &mut out, 8).unwrap();
assert_eq!(report.status, DistinctDrawStatus::TableExhausted);
assert_eq!((report.written, report.samples_used), (2, 2));
assert!(out[..2].contains(&0) && out[..2].contains(&2));
assert_eq!(out[2], 99);
Source

pub fn choose_distinct_into( &self, tickets: &[u32], requested: usize, out: &mut [usize], ) -> Result<DistinctDrawReport, DistinctDrawError>

Deterministic distinct draws from saved/authored tickets, without RNG.

Apply each consumed ticket modulo the remaining weight total and remove that selected index. No tickets are read after completion or pool exhaustion. samples_used reports the number consumed, at most 256; TicketsExhausted preserves the accepted prefix if the slice ends early. This is an explicit replay mapping, not an unbiased conversion of arbitrary random u32s. Independent uniform tickets in each remaining range give sequential weight-proportional draws. Tickets depend on weights and order; saved indices still depend on order. Hosts own stable reward identities and any replay/content versioning.

Request/capacity preflight, tail preservation and fresh-pool semantics match Self::roll_distinct_into. Work is O(N * (1 + min(requested, positive_weight_entries, tickets.len()))); storage is N scratch weights, with no allocation. Even an arbitrarily long ticket slice is not scanned.

use ferravex_play::gameplay::loot::{RewardTable, DistinctDrawStatus};
let table = RewardTable::new([1, 0, 3]).unwrap();
let mut out = [99; 4];
let report = table.choose_distinct_into(&[3, 0, 7], 3, &mut out).unwrap();
assert_eq!(&out, &[2, 0, 99, 99]);
assert_eq!((report.written, report.samples_used), (2, 2));
assert_eq!(report.status, DistinctDrawStatus::TableExhausted);

Trait Implementations§

Source§

impl<const N: usize> Clone for RewardTable<N>

Source§

fn clone(&self) -> RewardTable<N>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const N: usize> Debug for RewardTable<N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<const N: usize> Copy for RewardTable<N>

Auto Trait Implementations§

§

impl<const N: usize> Freeze for RewardTable<N>

§

impl<const N: usize> RefUnwindSafe for RewardTable<N>

§

impl<const N: usize> Send for RewardTable<N>

§

impl<const N: usize> Sync for RewardTable<N>

§

impl<const N: usize> Unpin for RewardTable<N>

§

impl<const N: usize> UnwindSafe for RewardTable<N>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.