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 "mgba_logger.h"
#include "random.h"
#include "sprite.h"

Go to the source code of this file.

Data Structures

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_RETURN_IF_UNEXPECTED_TYPE_RET(item, expected_type, ret_val)
 Checks if item is of type expected_type - logs error message and returns otherwise.
 
#define ITEM_RETURN_IF_UNEXPECTED_TYPE_VOID(item, expected_type)
 Checks if item is of type expected_type - logs error message and returns otherwise.
 

Typedefs

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

Itemitem_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.
 
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.
 
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_RETURN_IF_UNEXPECTED_TYPE_RET

#define ITEM_RETURN_IF_UNEXPECTED_TYPE_RET (   item,
  expected_type,
  ret_val 
)
Value:
do \
{ \
if ((item)->type != expected_type) \
{ \
MGBA_FUNC_ERROR("Unexpected %s->type != %s", #item, #expected_type); \
return (ret_val); \
} \
} while (0)

Checks if item is of type expected_type - logs error message and returns otherwise.

This version is for a function that returns a value, while ITEM_RETURN_IF_UNEXPECTED_TYPE_VOID is for a void function.

Definition at line 25 of file item.h.

◆ ITEM_RETURN_IF_UNEXPECTED_TYPE_VOID

#define ITEM_RETURN_IF_UNEXPECTED_TYPE_VOID (   item,
  expected_type 
)
Value:
do \
{ \
if ((item)->type != expected_type) \
{ \
MGBA_FUNC_ERROR("Unexpected %s->type != %s", #item, #expected_type); \
return; \
} \
} while (0)

Checks if item is of type expected_type - logs error message and returns otherwise.

This version is for a void function, while ITEM_RETURN_IF_UNEXPECTED_TYPE_RET is for one with a return value.

Definition at line 41 of file item.h.

Enumeration Type Documentation

◆ ItemType

enum ItemType

Definition at line 51 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 35 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 46 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.

Parameters
itemA pointer to an item for destruction.

Definition at line 56 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 24 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 67 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 9 of file item.c.