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
impl ResourcePool
Sourcepub fn set(&mut self, id: u16, current: u16, max: u16)
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.
Sourcepub fn current(&self, id: u16) -> Option<u16>
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”.
Sourcepub fn can_pay(&self, id: u16, amount: u16) -> bool
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.
Sourcepub fn pay(&mut self, id: u16, amount: u16) -> bool
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.