Skip to main content

ResourcePool

Struct ResourcePool 

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

A per-battler pool of generic, game-defined consumable resources (MP / SP / TP / mana / charge — the engine assigns the concept no name; doc 13 §4, the highest-value STATE-SEAM).

§Why a u16-keyed container, not EnumMap<P::Resource>

The doc’s sketch suggested EnumMap<P::Resource, u16> on BattlerState. That would require a new associated type Resource on BattleProvider. A non-defaulted assoc type breaks all 16 existing impl BattleProvider blocks (an unacceptable, non-additive change), and a defaulted assoc type (type Resource: … = …;) is unstable on stable Rust (E0658, associated_type_defaults, issue #29661) — so it cannot ship on the workspace’s stable toolchain either. The fully-additive choice is therefore a P-independent container keyed by a small opaque integer resource id (the game assigns ids; the engine never interprets them). This adds a single field to BattlerState and zero changes to the BattleProvider trait or any of its 16 impls.

Each entry tracks (current, max). The pool defaults to EMPTY, so a battler that declares no resource behaves byte-for-byte as before. Lookups are an O(n) linear scan, like EnumMap — pools are tiny.

Implementations§

Source§

impl ResourcePool

Source

pub fn new() -> Self

An empty pool (the default — a battler with no declared resources).

Source

pub fn set(&mut self, id: u16, current: u16, max: u16)

Set id’s current value and max (inserting the entry if absent). The current value is clamped to max.

Source

pub fn current(&self, id: u16) -> Option<u16>

The current value of resource id, or None if the battler has no such resource. A None resource is treated as “cannot pay any positive cost”.

Source

pub fn max(&self, id: u16) -> Option<u16>

The max value of resource id, or None if absent.

Source

pub fn can_pay(&self, id: u16, amount: u16) -> bool

Whether the battler can pay amount of resource id. A 0 cost is always payable (even with no such resource — it is inert). A positive cost on a resource the battler does not have is not payable.

Source

pub fn pay(&mut self, id: u16, amount: u16) -> bool

Deduct amount of resource id (saturating at 0). No-op for a 0 amount or an absent resource. Returns true if a deduction was applied. Pure arithmetic — consumes no randomness.

Source

pub fn restore(&mut self, id: u16, amount: u16)

Restore amount of resource id (clamped to its max). No-op for an absent resource.

Source

pub fn len(&self) -> usize

Number of distinct resources in the pool.

Source

pub fn is_empty(&self) -> bool

Whether the pool declares no resources (the default — fully inert).

Trait Implementations§

Source§

impl Clone for ResourcePool

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for ResourcePool

Source§

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

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

impl Default for ResourcePool

Source§

fn default() -> ResourcePool

Returns the “default value” for a type. Read more

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.