1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
//! Structs and functions to interact with the data of items in a general context.
use crate::api::_common::get_faint_reason;
use crate::api::moves::MoveId;
use crate::api::overlay::OverlayLoadLease;
use crate::ctypes::c_int;
use crate::ffi;
use core::marker::PhantomData;
use core::mem;
/// An item ID with associated methods to get metadata.
///
/// Use the associated constants or the [`Self::new`] method to get instances of this.
pub type ItemId = ffi::item_id;
impl Copy for ItemId {}
/// This impl provides general metadata about items in the game.
impl ItemId {
/// Returns the ID struct for the item with the given ID.
///
/// # Safety
/// The caller must make sure the ID is valid (refers to an existing item),
/// otherwise this is UB.
pub const unsafe fn new(id: u32) -> Self {
Self(id)
}
/// Returns the ID of this item.
pub const fn id(&self) -> u32 {
self.0
}
/// Returns the category ID of this item.
pub fn category(&self) -> ItemCategoryId {
unsafe { ffi::GetItemCategory(*self) }
}
// Returns whether or not this item is an item that can be thrown
// (`ItemCategoryId::CATEGORY_THROWN_LINE` or `ItemCategoryId::CATEGORY_THROWN_ARC`).
pub fn can_be_thrown(&self) -> bool {
unsafe { ffi::IsThrownItem(*self) > 0 }
}
// Checks if this item ID is valid(?).
pub fn is_valid(&self) -> bool {
unsafe { ffi::IsItemValid(*self) > 0 }
}
// Checks if the given item ID is valid (using `Self::is_valid`).
// If so, return the given item ID. Otherwise, return `ItemId::ITEM_PLAIN_SEED`.
pub fn fallback_if_invalid(self) -> Self {
unsafe { ffi::EnsureValidItem(self) }
}
// Returns whether or not this item is `Self::ITEM_POKE`.
pub fn is_money(&self) -> bool {
unsafe { ffi::IsNotMoney(*self) == 0 }
}
/// Checks if the item is one of the aura bows received at the start of the game.
pub fn is_aura_bow(&self) -> bool {
unsafe { ffi::IsAuraBow(*self) > 0 }
}
/// Gets the exclusive item offset, which is the item ID relative to that of the first exclusive
/// item, the Prism Ruff.
pub fn get_exclusive_item_offset(&self) -> i32 {
unsafe { ffi::GetExclusiveItemOffset(*self) }
}
/// Returns the action ID that corresponds to an item given its ID.
///
/// The action is based on the category of the item (see `ITEM_CATEGORY_ACTIONS`), unless the
/// specified ID is 0x16B, in which case `ACTION_UNK_35` is returned.
///
/// Some items can have unexpected actions, such as thrown items, which have `ACTION_NOTHING`.
/// This is done to prevent duplicate actions from being listed in the menu (since items always
/// have a "throw" option), since a return value of `ACTION_NOTHING` prevents the option from
/// showing up in the menu.
pub fn get_dungeon_item_action(&self, _ov29: OverlayLoadLease<29>) -> ffi::action::Type {
unsafe { ffi::GetItemAction(self.0 as c_int) }
}
/// Gets the exclusive item offset, which is the item ID relative to that of the first
/// exclusive item, the Prism Ruff.
///
/// If the given item ID is not a valid item ID, `ItemId::ITEM_PLAIN_SEED` (0x55) is returned.
/// This is a bug, since 0x55 is the valid exclusive item offset for the Icy Globe.
pub fn get_exclusive_item_offset_checked_for_validity(&self) -> i32 {
unsafe { ffi::GetExclusiveItemOffsetEnsureValid(*self) }
}
/// Get the minimum quantity for this (thrown) item ID.
pub fn get_thrown_item_quantity_minimum(&self) -> u8 {
unsafe { ffi::GetThrownItemQuantityLimit(*self, 0) }
}
/// Get the maximum quantity for this (thrown) item ID.
pub fn get_thrown_item_quantity_maximum(&self) -> u8 {
unsafe { ffi::GetThrownItemQuantityLimit(*self, 1) }
}
/// Applies stat boosts from an exclusive item.
pub fn apply_exclusive_item_stat_boosts(
&self,
atk_to_modify: &mut u8,
sp_atk_to_modify: &mut u8,
def_to_modify: &mut u8,
sp_def_to_modify: &mut u8,
) {
unsafe {
ffi::ApplyExclusiveItemStatBoosts(
*self,
atk_to_modify,
sp_atk_to_modify,
def_to_modify,
sp_def_to_modify,
)
}
}
/// Gets the faint reason code (see HandleFaint) for a given move-item combination.
///
/// If there's no item, the reason code is the move ID. If the item is an orb, return
/// FAINT_REASON_ORB_ITEM. Otherwise, return FAINT_REASON_NON_ORB_ITEM.
pub fn get_faint_reason(&self, move_id: MoveId) -> ffi::faint_reason {
get_faint_reason(move_id, *self)
}
}
impl From<ItemId> for u32 {
fn from(v: ItemId) -> Self {
v.0
}
}
/// An item slot. It has a quantity if it's stackable
/// and optionally a reference to an entity that holds it.
///
/// A quantity of zero indicates that the item is not stackable.
pub type Item = ffi::item;
impl Item {
/// Allocates a new item.
///
/// This will resolve the quantity based on the item type:
///
/// - For Poké, the quantity code will always be set to 1.
/// - For thrown items, the quantity code will be randomly generated on the range of valid
/// quantities for that item type.
/// - For non-stackable items, the quantity code will always be set to 0.
/// - Otherwise, the quantity will be assigned from the quantity argument.
pub fn new(item_id: ItemId, quantity: u16, sticky: bool) -> Self {
// SAFETY: We init the value right after.
let mut slf: Self = unsafe { mem::zeroed() };
slf.init(item_id, quantity, sticky);
slf
}
/// Initialize an item struct with the given information.
///
/// This will resolve the quantity based on the item type:
///
/// - For Poké, the quantity code will always be set to 1.
/// - For thrown items, the quantity code will be randomly generated on the range of valid
/// quantities for that item type.
/// - For non-stackable items, the quantity code will always be set to 0.
/// - Otherwise, the quantity will be assigned from the quantity argument.
pub fn init(&mut self, item_id: ItemId, quantity: u16, sticky: bool) {
unsafe { ffi::InitItem(self, item_id, quantity, sticky as ffi::bool_) }
}
}
/// An exclusive item effect ID with associated methods to get metadata.
///
/// Use the associated constants or the [`Self::new`] method to get instances of this.
pub type ExclusiveItemEffectId = ffi::exclusive_item_effect_id;
impl Copy for ExclusiveItemEffectId {}
/// This impl provides general metadata about exclusive item effects in the game.
impl ExclusiveItemEffectId {
/// Returns the ID struct for the exclusive item effect with the given ID.
///
/// # Safety
/// The caller must make sure the ID is valid (refers to an existing exclusive item effect),
/// otherwise this is UB.
pub const unsafe fn new(id: u32) -> Self {
Self(id)
}
/// Returns the ID of this exclusive item effect.
pub const fn id(&self) -> u32 {
self.0
}
/// Sets the bit for an exclusive item effect.
pub fn set_exclusive_item_effect(&self, effect_flags: &mut u32) {
unsafe { ffi::SetExclusiveItemEffect(effect_flags, *self) }
}
/// Tests the exclusive item bitvector for a specific exclusive item effect.
pub fn test_exclusive_item_effect_flag(&self, effect_flags: &mut u32) -> bool {
unsafe { ffi::ExclusiveItemEffectFlagTest(effect_flags, *self) > 0 }
}
}
impl From<ExclusiveItemEffectId> for u32 {
fn from(v: ExclusiveItemEffectId) -> Self {
v.0
}
}
/// An item category ID with associated methods to get metadata.
///
/// Use the associated constants or the [`Self::new`] method to get instances of this.
pub type ItemCategoryId = ffi::item_category;
impl Copy for ItemCategoryId {}
/// This impl provides general metadata about item categories in the game.
impl ItemCategoryId {
/// Returns the ID struct for the item category with the given ID.
///
/// # Safety
/// The caller must make sure the ID is valid (refers to an existing item),
/// otherwise this is UB.
pub const unsafe fn new(id: u32) -> Self {
Self(id)
}
/// Returns the ID of this item category.
pub const fn id(&self) -> u32 {
self.0
}
}
impl From<ItemCategoryId> for u32 {
fn from(v: ItemCategoryId) -> Self {
v.0
}
}
/// The money that the player is carrying.
pub struct MoneyCarried(PhantomData<()>);
impl MoneyCarried {
/// Returns an internal reference to the money carried. Note that this isn't a reference
/// to the actual struct in memory (yet).
///
/// # Safety
/// This is unsafe, since it essentially borrows a global variable mutably, see
/// safety rules for `static mut`s.
pub unsafe fn get() -> Self {
Self(PhantomData)
}
/// Sets the amount of money the player is carrying, clamping the value to the range
/// [0, MAX_MONEY_CARRIED].
pub fn set_money(&mut self, money: i32) {
unsafe { ffi::SetMoneyCarried(money) }
}
}
/// The money that the player is storing at the Duskull Bank.
pub struct MoneyStored(PhantomData<()>);
impl MoneyStored {
/// Returns an internal reference to the money stored. Note that this isn't a reference
/// to the actual struct in memory (yet).
///
/// # Safety
/// This is unsafe, since it essentially borrows a global mutable variable (`static mut`), see
/// safety rules for `static mut`s.
pub unsafe fn get() -> Self {
Self(PhantomData)
}
/// Sets the amount of money the player has stored in the Duskull Bank, clamping the value to the
/// range [0, MAX_MONEY_STORED].
pub fn set_money(&mut self, money: i32) {
unsafe { ffi::SetMoneyStored(money) }
}
}
/// The player's bag.
pub struct InventoryBag(PhantomData<()>);
impl InventoryBag {
/// Returns an internal reference to the player's bag. Note that this isn't a reference
/// to the actual struct in memory (yet).
///
/// # Safety
/// This is unsafe, since it essentially borrows a global mutable variable (`static mut`), see
/// safety rules for `static mut`s.
pub unsafe fn get() -> Self {
Self(PhantomData)
}
/// Checks if the player's bag is full.
pub fn is_full(&self) -> bool {
unsafe { ffi::IsBagFull() > 0 }
}
/// Count the amount of the specified item in the player's bag.
pub fn is_in_bag(&self, item_id: ItemId) -> bool {
unsafe { ffi::IsItemInBag(item_id) > 0 }
}
/// Count the amount of the specified item in the player's bag.
pub fn count_item_type(&self, item_id: ItemId) -> i32 {
unsafe { ffi::CountItemTypeInBag(item_id) }
}
/// Adds the specified amount of an item to the player's bag. Returns whether or not any
/// items could be added.
pub fn add_item(&mut self, item_id: ItemId, quantity: u16) -> bool {
unsafe {
ffi::AddItemToBag(&mut ffi::bulk_item {
id: ffi::item_id_16 {
_bitfield_align_1: [],
_bitfield_1: ffi::item_id_16::new_bitfield_1(item_id),
},
quantity,
}) > 0
}
}
}
/// The player's inventory in the storage.
pub struct InventoryStorage(PhantomData<()>);
impl InventoryStorage {
/// Returns an internal reference to the player's inventory in the storage.
/// Note that this isn't a reference to the actual struct in memory (yet).
///
/// # Safety
/// This is unsafe, since it essentially borrows a global mutable variable (`static mut`), see
/// safety rules for `static mut`s.
pub unsafe fn get() -> Self {
Self(PhantomData)
}
/// Special process 0x39.
///
/// This is *probably* is_storage_full: checks if the player's storage is full.
pub fn is_full(&self) -> bool {
unsafe { ffi::ScriptSpecialProcess0x39() > 0 }
}
/// Count the amount of the specified item in the player's storage.
pub fn count_item_type(&self, item_id: ItemId) -> i32 {
unsafe {
ffi::CountItemTypeInStorage(&mut ffi::bulk_item {
id: ffi::item_id_16 {
_bitfield_align_1: [],
_bitfield_1: ffi::item_id_16::new_bitfield_1(item_id),
},
quantity: 0,
})
}
}
/// Adds the specified amount of an item to the player's bag. Returns whether or not any
/// items could be added.
pub fn add_item(&mut self, item_id: ItemId, quantity: u16) -> bool {
unsafe {
ffi::AddItemToStorage(&mut ffi::bulk_item {
id: ffi::item_id_16 {
_bitfield_align_1: [],
_bitfield_1: ffi::item_id_16::new_bitfield_1(item_id),
},
quantity,
}) > 0
}
}
/// Removes (the specified amount...?) of the given item type from the storage.
pub fn remove_item(&mut self, item_id: ItemId, quantity: u16) -> bool {
unsafe {
ffi::RemoveItemsTypeInStorage(&mut ffi::bulk_item {
id: ffi::item_id_16 {
_bitfield_align_1: [],
_bitfield_1: ffi::item_id_16::new_bitfield_1(item_id),
},
quantity,
}) > 0
}
}
}