GBAlatro
A Demake of Balatro for the GBA
Loading...
Searching...
No Matches
item.c
1#include "item.h"
2
3#include "game.h"
4#include "game_variables.h"
5#include "item_funcs.h"
6#include "mgba_logger.h"
7#include "util.h"
8
9#include <tonc.h>
10
11Item* item_roll_new(enum ItemType item_type, enum RngSequence key)
12{
13 if ((int)item_type < 0 || item_type >= ITEM_NUM_TYPES)
14 {
15 MGBA_FUNC_ERROR("Invalid type %d", item_type);
16 return NULL;
17 }
18
19 ItemFuncs* item_funcs = get_item_type_funcs(item_type);
20
21 GBAL_RETURN_IF_NULL(item_funcs, NULL);
22 if (item_funcs->roll_new == NULL)
23 {
24 MGBA_FUNC_ERROR("Unimplemented 'roll_new' function called for item type %d", item_type);
25 return NULL;
26 }
27
28 Item* ret_item = item_funcs->roll_new(key);
29 ret_item->is_owned = false;
30
31 return ret_item;
32}
33
35{
36 GBAL_RETURN_IF_NULL(item, UNDEFINED);
37
38 ItemFuncs* item_funcs = get_item_type_funcs(item->type);
39 GBAL_RETURN_IF_NULL(item_funcs, UNDEFINED);
40 if (item_funcs->get_buy_price == NULL)
41 {
42 MGBA_FUNC_ERROR(
43 "Unimplemented 'get_buy_price' function called for item type %d",
44 item->type
45 );
46 return UNDEFINED;
47 }
48
49 return item_funcs->get_buy_price(item);
50}
51
53{
54 GBAL_RETURN_IF_NULL(item, UNDEFINED);
55
56 ItemFuncs* item_funcs = get_item_type_funcs(item->type);
57 GBAL_RETURN_IF_NULL(item_funcs, UNDEFINED);
58 if (item_funcs->get_sell_price == NULL)
59 {
60 MGBA_FUNC_ERROR(
61 "Unimplemented 'get_sell_price' function called for item type %d",
62 item->type
63 );
64 return UNDEFINED;
65 }
66
67 return item_funcs->get_sell_price(item);
68}
69
70const char* item_get_name(Item* item)
71{
73
74 ItemFuncs* item_funcs = get_item_type_funcs(item->type);
76 if (item_funcs->get_name == NULL)
77 {
78 MGBA_FUNC_ERROR("Unimplemented 'get_name' function called for item type %d", item->type);
80 }
81
82 // Guarantees no implementation ever returns NULL
83 const char* item_name = item_funcs->get_name(item);
85
86 return item_name;
87}
88
90{
92
93 GBAL_RETURN_IF_NULL(item, error_info);
94
95 ItemFuncs* item_funcs = get_item_type_funcs(item->type);
96 GBAL_RETURN_IF_NULL(item_funcs, error_info);
97 if (item_funcs->get_subtype_info == NULL)
98 {
99 MGBA_FUNC_ERROR(
100 "Unimplemented 'get_subtype_info' function called for item type %d",
101 item->type
102 );
103 return error_info;
104 }
105
106 ItemDescSubtypeInfo ret_info = item_funcs->get_subtype_info(item);
107 if (ret_info.name_str == NULL)
108 {
109 MGBA_FUNC_ERROR("Item of type %d returned NULL as description subtype name", item->type);
110 ret_info.name_str = ITEM_NAME_UNDEFINED;
111 }
112
113 return ret_info;
114}
115
117{
119
120 ItemFuncs* item_funcs = get_item_type_funcs(item->type);
121 GBAL_RETURN_IF_NULL(item_funcs, RET_NONE);
122 GBAL_RETURN_IF_NULL(item_funcs->acquire, RET_NONE);
123 if (item_funcs->acquire == NULL)
124 {
125 MGBA_FUNC_ERROR("Unimplemented 'acquire' function called for item type %d", item->type);
126 return;
127 }
128
129 item_funcs->acquire(item);
130 item->is_owned = true;
131}
132
134{
135 GBAL_RETURN_IF_NULL(item, false);
136 ItemFuncs* item_funcs = get_item_type_funcs(item->type);
137 GBAL_RETURN_IF_NULL(item_funcs, false);
138 if (item_funcs->can_acquire == NULL)
139 {
140 MGBA_FUNC_ERROR("Unimplemented 'can_acquire' function called for item type %d", item->type);
141 return false;
142 }
143
144 return item_funcs->can_acquire(item);
145}
146
147void item_dispose(Item** item)
148{
151 ItemFuncs* item_funcs = get_item_type_funcs((*item)->type);
152 GBAL_RETURN_IF_NULL(item_funcs, RET_NONE);
153 if (item_funcs->dispose == NULL)
154 {
155 MGBA_FUNC_ERROR("Unimplemented 'dispose' function called for item type %d", (*item)->type);
156 return;
157 }
158
159 item_funcs->dispose(item);
160}
161
162int item_print_description(Item* item, Rect dest_rect)
163{
164 GBAL_RETURN_IF_NULL(item, 0);
165 ItemFuncs* item_funcs = get_item_type_funcs(item->type);
166 GBAL_RETURN_IF_NULL(item_funcs, 0);
167 if (item_funcs->print_description == NULL)
168 {
169 MGBA_FUNC_ERROR(
170 "Unimplemented 'print_description' function called for item type %d",
171 item->type
172 );
173 return 0;
174 }
175
176 return item_funcs->print_description(item, dest_rect);
177}
178
180{
181 GBAL_RETURN_IF_NULL(item, false);
182 return item->is_owned;
183}
184
185void item_sell(Item* item)
186{
188
189 int sell_price = item_get_sell_price(item);
190
191 if (sell_price == UNDEFINED)
192 {
193 MGBA_FUNC_ERROR("Undefined sell price for item of type %d", item->type);
194 return;
195 }
196
197 g_game_vars.money += sell_price;
198 display_money();
200 item_start_discard_animation(item);
201}
202
Game global game variables struct definition.
The core structure for items in the shop and inventory. Provides a common API for the shop and invent...
bool item_is_owned(Item *item)
Returns whether or not the given Item is in the player's possession.
Definition item.c:179
int item_get_sell_price(Item *item)
Returns the sell price of the item.
Definition item.c:52
void item_dispose(Item **item)
Destroys an item, freeing underlying resources, and manages rollable items sets if needed....
Definition item.c:147
#define ITEM_NAME_UNDEFINED
Default Item name, for the sake of consistency.
Definition item.h:38
const char * item_get_name(Item *item)
Returns the name of the Item.
Definition item.c:70
void item_print_buy_price_under(Item *item)
Prints the buy price under the item Relies on the fact item is a SpriteObject.
Definition item.c:203
#define ITEM_SUBTYPE_INFO_DEFAULT
Default subtype info struct declaration.
Definition item.h:44
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 ...
Definition item.c:11
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 e...
Definition item.c:133
ItemDescSubtypeInfo item_get_subtype_info(Item *item)
Returns the colors and name of the Item's subtype.
Definition item.c:89
void item_acquire(Item *item)
Acquires the item, adding to inventory if applicable. Called when it is purchased from the shop,...
Definition item.c:116
void item_sell(Item *item)
Performs the item sell transaction, gaining its sell value and discarding it.
Definition item.c:185
int item_print_description(Item *item, Rect dest_rect)
Prints the item's description inside the given rectangle.
Definition item.c:162
int item_get_buy_price(Item *item)
Returns the buy price of the item.
Definition item.c:34
An API for the item functions defined in item.h Separated module from item.c/.h so item_funcs....
ItemFuncs * get_item_type_funcs(enum ItemType type)
Returns the function table for a given item type.
Definition item_funcs.c:45
Interface to interact with the mgba logger.
RngSequence
Keys to different independent RNG sequences.
Definition random.h:19
void sprite_object_print_price_under(SpriteObject *sprite_object, int price)
Print the price string directly beneath a SpriteObject. More specialized version of sprite_object_pri...
Definition sprite.c:523
void sprite_object_erase_text_under(SpriteObject *sprite_object)
Erase the text within the Rect directly beneath a SpriteObject. This is used only for Cards for now.
Definition sprite.c:533
Structure containing the main and shadow colors, and the name string associated with an Item's subtyp...
Definition item.h:65
The set of functions that each item type implements.
Definition item.h:107
Item *(* roll_new)(enum RngSequence key)
Definition item.h:112
A generic interface for all items that can appear in the shop or be in the inventory....
Definition item.h:83
enum ItemType type
The item type - used to dispatch the function implementations for inheriting types.
Definition item.h:94
bool is_owned
Whether the item is currently held - used in cases where a behaviour needs to be different for Items ...
Definition item.h:100
A sprite object is a sprite that is focusable and movable in animation.
Definition sprite.h:86
Utilities relating around number string representation and protected arithmatic helper functions.
#define GBAL_RETURN_IF_NULL(param, ret_val)
Returns ret_val and prints a default error message if param is equal to NULL. Useful for checking arg...
Definition util.h:138
#define RET_NONE
An empty return value for RETURN_IF macros when used in void functions Expands to nothing because mac...
Definition util.h:146