GBAlatro
A Demake of Balatro for the GBA
Loading...
Searching...
No Matches
item.h File Reference

The core structure for items in the shop and inventory. Provides a common API for the shop and inventory to handle all types of items. Uses struct inheritance so all inherited items can implement an is-a relationship with Item. This means that pointers to structs that inherit Item using first member struct inheritance can and should be cast to Item* so code that expects an Item* can use them. More...

#include "graphic_utils.h"
#include "mgba_logger.h"
#include "random.h"
#include "sprite.h"
#include "util.h"
#include <stdint.h>

Go to the source code of this file.

Data Structures

struct  ItemDescSubtypeInfo
 Structure containing the main and shadow colors, and the name string associated with an Item's subtype. More...
 
struct  Item
 A generic interface for all items that can appear in the shop or be in the inventory. This uses first member struct inheritance - other structs are meant to inherit it by making their first member field Item. Then casts from inheriting structs to Item* are allowed and intentional and this allows for generic code that uses polymorphism. The -fms-extensions compile flag allows for anonymous members making it behave fully as inheritance. It makes all member fields be fully inherited so any struct that inherits Item for example will have all its fields accessible directly, e.g. JokerObject joker_object; joker_object.type = ITEM_TYPE_JOKER More...
 
struct  ItemFuncs
 The set of functions that each item type implements. More...
 

Macros

#define ITEM_NAME_UNDEFINED   "UNDEFINED"
 Default Item name, for the sake of consistency.
 
#define ITEM_SUBTYPE_INFO_DEFAULT   {.main_color = 0, .shadow_color = 0, .name_str = ITEM_NAME_UNDEFINED}
 Default subtype info struct declaration.
 

Typedefs

typedef struct ItemDescSubtypeInfo ItemDescSubtypeInfo
 Structure containing the main and shadow colors, and the name string associated with an Item's subtype.
 
typedef struct Item Item
 A generic interface for all items that can appear in the shop or be in the inventory. This uses first member struct inheritance - other structs are meant to inherit it by making their first member field Item. Then casts from inheriting structs to Item* are allowed and intentional and this allows for generic code that uses polymorphism. The -fms-extensions compile flag allows for anonymous members making it behave fully as inheritance. It makes all member fields be fully inherited so any struct that inherits Item for example will have all its fields accessible directly, e.g. JokerObject joker_object; joker_object.type = ITEM_TYPE_JOKER
 
typedef struct ItemFuncs ItemFuncs
 The set of functions that each item type implements.
 

Enumerations

enum  ItemType { ITEM_TYPE_JOKER , ITEM_TYPE_PLAYING_CARD , ITEM_NUM_TYPES }
 

Functions

Item * item_roll_new (enum ItemType item_type, enum RngSequence key)
 Rolls a random item of type item_type and returns a newly created one. Manages rollable items set if necessary (i.e. not rolling items already in inventory) To be used when rolling new items for the shop or packs.
 
int item_get_buy_price (Item *item)
 Returns the buy price of the item.
 
int item_get_sell_price (Item *item)
 Returns the sell price of the item.
 
const char * item_get_name (Item *item)
 Returns the name of the Item.
 
ItemDescSubtypeInfo item_get_subtype_info (Item *item)
 Returns the colors and name of the Item's subtype.
 
void item_acquire (Item *item)
 Acquires the item, adding to inventory if applicable. Called when it is purchased from the shop, note that it does not perform the purchase operation of decrementing the player's money, that should be handled by the shop code. For packs this can be to just open the pack, for vouchers, this will apply their effect.
 
bool item_can_acquire (Item *item)
 Returns true if the item can be acquired, i.e. added to inventory. Does not check if the player has enough money to buy the item, that is the shop's job, as this will be used both when purchasing and when selecting in a pack.
 
void item_dispose (Item **item)
 Destroys an item, freeing underlying resources, and manages rollable items sets if needed. To be used when destroying items from the inventory, shop, or packs.
 
int item_print_description (Item *item, Rect dest_rect)
 Prints the item's description inside the given rectangle.
 
bool item_is_owned (Item *item)
 Returns whether or not the given Item is in the player's possession.
 
void item_sell (Item *item)
 Performs the item sell transaction, gaining its sell value and discarding it.
 
void item_print_buy_price_under (Item *item)
 Prints the buy price under the item Relies on the fact item is a SpriteObject.
 

Detailed Description

The core structure for items in the shop and inventory. Provides a common API for the shop and inventory to handle all types of items. Uses struct inheritance so all inherited items can implement an is-a relationship with Item. This means that pointers to structs that inherit Item using first member struct inheritance can and should be cast to Item* so code that expects an Item* can use them.

Definition in file item.h.

Macro Definition Documentation

◆ ITEM_NAME_UNDEFINED

#define ITEM_NAME_UNDEFINED   "UNDEFINED"

Default Item name, for the sake of consistency.

Definition at line 38 of file item.h.

◆ ITEM_SUBTYPE_INFO_DEFAULT

#define ITEM_SUBTYPE_INFO_DEFAULT   {.main_color = 0, .shadow_color = 0, .name_str = ITEM_NAME_UNDEFINED}

Default subtype info struct declaration.

Definition at line 44 of file item.h.

Typedef Documentation

◆ ItemDescSubtypeInfo

Structure containing the main and shadow colors, and the name string associated with an Item's subtype.

Shadow colors are always a darker tone of the main color. The colors are organized in the card_rarity_pal_gfx.png file which is organized like this:

  • 0 -> transparency
  • 1,2 -> Common Joker (blue)
  • 3,4 -> Uncommon Joker (green)
  • 5,6 -> Rare Joker (red)
  • 7,8 -> Legendary Joker / Tarot Card (purple)
  • 9,10 -> Planet Card (blue with a tint of green)
  • 11,12 -> Spectral Card (deep blue)
  • 13,14 -> Voucher (red with a tint of orange)
See also
get_subtype_info

Enumeration Type Documentation

◆ ItemType

enum ItemType

Definition at line 22 of file item.h.

Function Documentation

◆ item_acquire()

void item_acquire ( Item *  item)

Acquires the item, adding to inventory if applicable. Called when it is purchased from the shop, note that it does not perform the purchase operation of decrementing the player's money, that should be handled by the shop code. For packs this can be to just open the pack, for vouchers, this will apply their effect.

Matches ItemFuncs.acquire()

Parameters
itemThe item to acquire

Definition at line 116 of file item.c.

◆ item_can_acquire()

bool item_can_acquire ( Item *  item)

Returns true if the item can be acquired, i.e. added to inventory. Does not check if the player has enough money to buy the item, that is the shop's job, as this will be used both when purchasing and when selecting in a pack.

Matches ItemFuncs.can_acquire()

Parameters
itemThe item to check

Definition at line 133 of file item.c.

◆ item_dispose()

void item_dispose ( Item **  item)

Destroys an item, freeing underlying resources, and manages rollable items sets if needed. To be used when destroying items from the inventory, shop, or packs.

Matches ItemFuncs.dispose()

Parameters
itemA pointer to an item for destruction.

Definition at line 147 of file item.c.

◆ item_get_buy_price()

int item_get_buy_price ( Item *  item)

Returns the buy price of the item.

Matches ItemFuncs.get_buy_price()

Parameters
itemThe item whose price to return.
Returns
UNDEFINED in case of error, the item's buy price otherwise.

Definition at line 34 of file item.c.

◆ item_get_name()

const char * item_get_name ( Item *  item)

Returns the name of the Item.

Matches ItemFuncs.get_name()

Parameters
itemThe item whose name to return.
Returns
The item name. In case of error ITEM_NAME_UNDEFINED, will not be NULL

Definition at line 70 of file item.c.

◆ item_get_sell_price()

int item_get_sell_price ( Item *  item)

Returns the sell price of the item.

Matches ItemFuncs.get_sell_price()

Parameters
itemThe item whose price to return.
Returns
UNDEFINED in case of error, the item's sell price otherwise.

Definition at line 52 of file item.c.

◆ item_get_subtype_info()

ItemDescSubtypeInfo item_get_subtype_info ( Item *  item)

Returns the colors and name of the Item's subtype.

Matches ItemFuncs.get_subtype_info()

Parameters
itemThe item whose subtype's color and name to return.
Returns
Struct containing values of main and shadow colors, as well as the name of the subtype. In case of an error, all colors will be 0 and the name "UNDEFINED"
See also
ItemDescSubtypeInfo

Definition at line 89 of file item.c.

◆ item_is_owned()

bool item_is_owned ( Item *  item)

Returns whether or not the given Item is in the player's possession.

Parameters
itemthe Item to test
Returns
true if the item is owned, false otherwise

Definition at line 179 of file item.c.

◆ item_print_buy_price_under()

void item_print_buy_price_under ( Item *  item)

Prints the buy price under the item Relies on the fact item is a SpriteObject.

Parameters
itemThe item to print under

Definition at line 203 of file item.c.

◆ item_print_description()

int item_print_description ( Item *  item,
Rect  dest_rect 
)

Prints the item's description inside the given rectangle.

Parameters
itemThe item to print the description of
dest_rectthe target rectangle the description needs to fit in

Matches ItemFuncs.print_description()

Returns
the number of lines used by the description

Definition at line 162 of file item.c.

◆ item_roll_new()

Item * item_roll_new ( enum ItemType  item_type,
enum RngSequence  key 
)

Rolls a random item of type item_type and returns a newly created one. Manages rollable items set if necessary (i.e. not rolling items already in inventory) To be used when rolling new items for the shop or packs.

Matches ItemFuncs::roll_new()

Parameters
item_typeThe type of the item to roll
keyto the RNG sequence used to roll the Item
Returns
The newly created randomly rolled item

Definition at line 11 of file item.c.

◆ item_sell()

void item_sell ( Item *  item)

Performs the item sell transaction, gaining its sell value and discarding it.

Parameters
itemThe sold item

Definition at line 185 of file item.c.