Struct Progression

Source
pub struct Progression { /* private fields */ }
Expand description

Private, normalized progression state with an immutable authored curve.

Implementations§

Source§

impl Progression

Source

pub fn new( base: u32, growth: u32, maximum: u16, ) -> Result<Progression, LoopError>

Start at level 1 with no XP. Zero growth and a maximum of 1 are valid.

use ferravex_play::gameplay::progression::Progression;
let p = Progression::new(10, 5, 3).unwrap();
assert_eq!((p.level(), p.xp(), p.next_cost()), (1, 0, Some(10)));
assert!(Progression::new(0, 5, 3).is_err());
Source

pub fn level(&self) -> u16

Source

pub fn xp(&self) -> u64

Source

pub fn next_cost(&self) -> Option<u64>

Source

pub fn award(&mut self, amount: u64) -> u16

Returns earned promotions; hosts apply rewards once from this result. Excess at the level cap is retained for display, never re-awarded. Addition saturates at u64::MAX before promotion costs are subtracted. This is equivalent to Self::award_report’s promotions field.

Source

pub fn preview_award(&self, amount: u64) -> AwardReport

Compute the exact award without changing state or issuing rewards.

A preview is not a reservation; recompute on commit after other awards or restores. At most 1023 iterations, with no allocation or callbacks.

use ferravex_play::gameplay::progression::Progression;
let mut p = Progression::new(10, 5, 3).unwrap();
let preview = p.preview_award(30);
assert_eq!((p.level(), p.xp()), (1, 0));
assert_eq!((preview.promotions, preview.xp_spent, preview.xp_after), (2, 25, 5));
assert!(preview.at_cap);
assert_eq!(p.award_report(30), preview);
Source

pub fn award_report(&mut self, amount: u64) -> AwardReport

Commit the same rule as Self::preview_award, returning accepted data.

Caller owns game-event acceptance and deduplication. Reward only this call’s promotions, once; no lifetime reward ledger is kept here.

use ferravex_play::gameplay::progression::Progression;
let mut p = Progression::new(10, 0, 2).unwrap();
p.award(9);
let report = p.award_report(u64::MAX);
assert_eq!((report.accepted_xp, report.discarded_xp), (u64::MAX - 9, 9));
assert!(report.saturated);
assert_eq!((report.promotions, p.xp()), (1, u64::MAX - 10));
Source

pub fn snapshot(&self) -> ProgressionSnapshot

Capture state and every authored value, without normalizing or awarding. See Self::restore for checked loading and an executable example.

Source

pub fn restore( &mut self, snapshot: ProgressionSnapshot, ) -> Result<RestoreReport, RestoreError>

Atomically load validated state for this exact curve; never issue rewards.

Checks authoring, exact configuration, level bounds, then XP normalization. At the cap any u64 remainder is valid. Older same-curve snapshots are deliberately accepted for rollback, without undoing external side effects. Restore corresponding game-owned reward/event state together, or maintain an external deduplication ledger for effects that must survive rollback.

use ferravex_play::gameplay::progression::{Progression, RestoreError};
let mut p = Progression::new(10, 5, 3).unwrap();
let start = p.snapshot();
p.award_report(12);
let loaded = p.restore(start).unwrap();
assert_eq!((loaded.before.level, loaded.after.level), (2, 1));
let again = p.restore(start).unwrap();
assert_eq!(again.before, again.after); // No reward to replay.
let mut invalid = start;
invalid.xp = 10; // A pending promotion is not normalized saved state.
assert_eq!(p.restore(invalid), Err(RestoreError::UnsettledXp));
assert_eq!(p.snapshot(), start);

Trait Implementations§

Source§

impl Clone for Progression

Source§

fn clone(&self) -> Progression

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 Debug for Progression

Source§

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

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

impl Copy for Progression

Auto Trait Implementations§

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.