pub struct Progression { /* private fields */ }Expand description
Private, normalized progression state with an immutable authored curve.
Implementations§
Source§impl Progression
impl Progression
Sourcepub fn new(
base: u32,
growth: u32,
maximum: u16,
) -> Result<Progression, LoopError>
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());pub fn level(&self) -> u16
pub fn xp(&self) -> u64
pub fn next_cost(&self) -> Option<u64>
Sourcepub fn award(&mut self, amount: u64) -> u16
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.
Sourcepub fn preview_award(&self, amount: u64) -> AwardReport
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);Sourcepub fn award_report(&mut self, amount: u64) -> AwardReport
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));Sourcepub fn snapshot(&self) -> ProgressionSnapshot
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.
Sourcepub fn restore(
&mut self,
snapshot: ProgressionSnapshot,
) -> Result<RestoreReport, RestoreError>
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
impl Clone for Progression
Source§fn clone(&self) -> Progression
fn clone(&self) -> Progression
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more