Refactor shop code to use generic Item (#536)
* Turned SpriteObject into a member of JokerObject and CardObject and expiremented with casting * Extracted shop code to use generic `SpriteObject` * Extracted shop joker generation to a function * Renamed object -> item * Added setters and getters for `SpriteObject` members and updated all accesses to use them with casting (AI used for tedious work and reviewed - model Raptor mini) * Added sprite_object_set_vx() because sprite_object_set_vy() exists and reordered the function order to have x,y parameters first, then targets * Added Item object to act as a purchasable interface for the shop - AI used (model Raptor mini) * Added -fms-extensions and switched to anonymous inheritance * Fixed anonymous inheritance for joker and added direct access for that too * fix build error from previous commit + clang-format * Fixed build errors + clang-format for real this time (I hope...) * clang-format one last time, I don't understand it anymore * clang-format is annoying * Added uses of CHECK_NULL_ARG macros * Merged item_defs.h into item.h and fixed build error from previous commit * clang-format...? * Changed sprite_object_new() into sprite_object_init() and made it accept a parameter instead of returning one to make it less awkward * Updated sprite_object_init() documentation * Updated sprite_object_destroy documentation * More clang-format * Renamed add_to_inventory to acquire and updated documentation * Added function documentations * Update documentation + swap order of consumable and playing card types * Added documentation for new files (+ small addition to button.h) * clang-format * Used typedef * A bit more documentation * Changed sho pcode to use generic Item instead of SpriteObject * clang-format + fixed NULL-check error * Revert weird clang-format...? * clang-format for real * Fixed errors from rebase on log_function_name * Fixed build errors from rebase on main * Added some more documentation about first member struct inheritance * typo fix * typo fix for real * Removed SpriteObject field getters/setters (AI used - model Raptor mini) * Renamed item_acquire -> item_on_acquired * Added item_can_acquire() * clang-format * Fixed some sprite issues but not all... * Fixed some more issues * Cleanup JokerObject some more JokerObjects in the shop * Added `item_destroy()` * Added item_set_available_to_shop, CHECK_ITEM_TYPE macros + small documentation updates * Another small JokerObject->Item replacement * Removed item_set_available_to_shop and replaced with item_roll_new() and item_dispose(), also changed "available_to_shop" semantics to "rollable" * Re-applied changes from game.c to round.c after rebase on main * Added partial implementations of item functions for playing cards * Line endings fix... * Macro renames, function documentations * Updated documentations * Apply fixes from Copilot review Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Fix card_object_get_buy_price type checking Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Avoid double pointer cast on dispose Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Apply suggestions from @ricfehr3's code review Co-authored-by: Rickey <ric@rf3.xyz> * Remove redundant comments * Moved error checking before joker_get_random_rarity Co-authored-by: Rickey <ric@rf3.xyz> * Fixed build error from rebase * clang-format * Added doxygen documentation for Item explaining inheritance * Removed unnecessary comment --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: Rickey <ric@rf3.xyz>
This commit is contained in:
+170
-14
@@ -4,6 +4,8 @@
|
||||
#include "game/round.h"
|
||||
#include "game_variables.h"
|
||||
#include "graphic_utils.h"
|
||||
#include "layout.h"
|
||||
#include "mgba_logger.h"
|
||||
#include "pool.h"
|
||||
#include "random.h"
|
||||
#include "soundbank.h"
|
||||
@@ -11,6 +13,7 @@
|
||||
|
||||
// Tiles and palettes
|
||||
#include "card_rarity_pal_gfx.h"
|
||||
#include "item.h"
|
||||
#include "joker_gfx.h"
|
||||
|
||||
#include <maxmod.h>
|
||||
@@ -58,6 +61,8 @@ static bool used_layers[MAX_JOKER_OBJECTS] = {false}; // Track used layers for j
|
||||
static int joker_spritesheet_pb_map[MAX_NUM_JOKERS_SPRITESHEETS];
|
||||
static int joker_pb_num_sprite_users[JOKER_LAST_PB - JOKER_BASE_PB + 1] = {0};
|
||||
|
||||
BITSET_DEFINE(s_rollable_jokers_bitset, MAX_DEFINABLE_JOKERS)
|
||||
|
||||
// See linked issue for context of maps
|
||||
// https://github.com/GBALATRO/balatro-gba/issues/274#issue-3685075538
|
||||
|
||||
@@ -179,12 +184,16 @@ u16 joker_get_rarity_color(u8 rarity, bool main_color)
|
||||
return card_rarity_pal_gfxPal[1 + 2 * rarity + (main_color ? 0 : 1)];
|
||||
}
|
||||
|
||||
int joker_get_buy_price(const Joker* joker)
|
||||
{
|
||||
GBAL_RETURN_IF_NULL_RET(joker, UNDEFINED);
|
||||
|
||||
return joker->value;
|
||||
}
|
||||
|
||||
int joker_get_sell_value(const Joker* joker)
|
||||
{
|
||||
if (joker == NULL)
|
||||
{
|
||||
return UNDEFINED;
|
||||
}
|
||||
GBAL_RETURN_IF_NULL_RET(joker, UNDEFINED);
|
||||
|
||||
return joker->value / 2;
|
||||
}
|
||||
@@ -194,6 +203,8 @@ JokerObject* joker_object_new(Joker* joker)
|
||||
{
|
||||
JokerObject* joker_object = POOL_GET(JokerObject);
|
||||
|
||||
sprite_object_init((SpriteObject*)joker_object);
|
||||
|
||||
int layer = 0;
|
||||
for (int i = 0; i < MAX_JOKER_OBJECTS; i++)
|
||||
{
|
||||
@@ -206,7 +217,8 @@ JokerObject* joker_object_new(Joker* joker)
|
||||
}
|
||||
|
||||
joker_object->joker = joker;
|
||||
joker_object->sprite_object = sprite_object_new();
|
||||
|
||||
joker_object->type = ITEM_TYPE_JOKER;
|
||||
|
||||
int tile_index = JOKER_TID + layer * JOKER_SPRITE_OFFSET;
|
||||
|
||||
@@ -222,7 +234,7 @@ JokerObject* joker_object_new(Joker* joker)
|
||||
);
|
||||
|
||||
sprite_object_set_sprite(
|
||||
joker_object->sprite_object,
|
||||
(SpriteObject*)joker_object,
|
||||
sprite_new(
|
||||
ATTR0_SQUARE | ATTR0_4BPP | ATTR0_AFF,
|
||||
ATTR1_SIZE_32,
|
||||
@@ -250,18 +262,162 @@ void joker_object_destroy(JokerObject** joker_object)
|
||||
UNDEFINED;
|
||||
}
|
||||
|
||||
sprite_object_destroy(&(*joker_object)->sprite_object); // Destroy the sprite
|
||||
joker_destroy(&(*joker_object)->joker); // Destroy the joker
|
||||
sprite_object_destroy((SpriteObject*)(*joker_object));
|
||||
joker_destroy(&(*joker_object)->joker);
|
||||
POOL_FREE(JokerObject, *joker_object);
|
||||
*joker_object = NULL;
|
||||
}
|
||||
|
||||
void joker_object_shake(JokerObject* joker_object, mm_word sound_id)
|
||||
void joker_object_dispose(Item** joker_object_item)
|
||||
{
|
||||
sprite_object_shake(joker_object->sprite_object, sound_id);
|
||||
GBAL_RETURN_IF_NULL_VOID(joker_object_item);
|
||||
GBAL_RETURN_IF_NULL_VOID(*joker_object_item);
|
||||
ITEM_RETURN_IF_UNEXPECTED_TYPE_VOID(*joker_object_item, ITEM_TYPE_JOKER);
|
||||
|
||||
JokerObject* joker_object = (JokerObject*)(*joker_object_item);
|
||||
GBAL_RETURN_IF_NULL_VOID(joker_object->joker);
|
||||
|
||||
joker_set_rollable(joker_object->joker->id, true);
|
||||
|
||||
joker_object_destroy(&joker_object);
|
||||
*joker_object_item = NULL;
|
||||
}
|
||||
|
||||
void set_and_shift_text(char* str, int* cursor_pos_x, int* cursor_pos_y, int color_pb)
|
||||
void joker_object_shake(JokerObject* joker_object, mm_word sound_id)
|
||||
{
|
||||
sprite_object_shake((SpriteObject*)joker_object, sound_id);
|
||||
}
|
||||
|
||||
int joker_object_get_buy_price(Item* joker_object)
|
||||
{
|
||||
GBAL_RETURN_IF_NULL_RET(joker_object, UNDEFINED);
|
||||
ITEM_RETURN_IF_UNEXPECTED_TYPE_RET(joker_object, ITEM_TYPE_JOKER, UNDEFINED);
|
||||
|
||||
return ((JokerObject*)joker_object)->joker->value;
|
||||
}
|
||||
|
||||
void joker_object_add_to_owned(Item* joker_object)
|
||||
{
|
||||
GBAL_RETURN_IF_NULL_VOID(joker_object);
|
||||
ITEM_RETURN_IF_UNEXPECTED_TYPE_VOID(joker_object, ITEM_TYPE_JOKER);
|
||||
|
||||
joker_object->ty = int2fx(HELD_JOKERS_POS.y);
|
||||
add_joker((JokerObject*)joker_object);
|
||||
}
|
||||
|
||||
void joker_set_rollable(int joker_id, bool rollable)
|
||||
{
|
||||
bitset_set_idx(&s_rollable_jokers_bitset, joker_id, rollable);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Computes the number of Jokers we can currently roll in the Shop.
|
||||
* The Jokers we own is taken into account and can't be rolled again.
|
||||
*/
|
||||
static inline int get_num_rollable_jokers(void)
|
||||
{
|
||||
return bitset_num_set_bits(&s_rollable_jokers_bitset);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns true if we can't roll any Joker
|
||||
*/
|
||||
static inline bool no_rollable_jokers(void)
|
||||
{
|
||||
return bitset_is_empty(&s_rollable_jokers_bitset);
|
||||
}
|
||||
|
||||
GBAL_UNUSED
|
||||
static inline bool joker_is_rollable(int joker_id)
|
||||
{
|
||||
return bitset_get_idx(&s_rollable_jokers_bitset, joker_id);
|
||||
}
|
||||
|
||||
void joker_reset_rollable_jokers(void)
|
||||
{
|
||||
int num_jokers = get_joker_registry_size();
|
||||
|
||||
bitset_clear(&s_rollable_jokers_bitset);
|
||||
for (int i = 0; i < num_jokers; i++)
|
||||
{
|
||||
bitset_set_idx(&s_rollable_jokers_bitset, i, true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Rolls a random Joker among the available ones
|
||||
*/
|
||||
static int joker_roll_id(void)
|
||||
{
|
||||
// Now determine how many jokers are available based on the rarity
|
||||
int jokers_avail_size = get_num_rollable_jokers();
|
||||
|
||||
if (jokers_avail_size == 0)
|
||||
return UNDEFINED;
|
||||
|
||||
// Roll for what rarity the joker will be
|
||||
int joker_rarity = joker_get_random_rarity();
|
||||
|
||||
int matching_joker_ids[jokers_avail_size];
|
||||
int fallback_random_idx = rng_get_u32() % jokers_avail_size;
|
||||
int fallback_random_joker_id = UNDEFINED;
|
||||
int match_count = 0;
|
||||
|
||||
BitsetItr itr = bitset_itr_create(&s_rollable_jokers_bitset);
|
||||
|
||||
int i = 0;
|
||||
int joker_id = UNDEFINED;
|
||||
while ((joker_id = bitset_itr_next(&itr)) != UNDEFINED)
|
||||
{
|
||||
if (i++ == fallback_random_idx)
|
||||
fallback_random_joker_id = joker_id;
|
||||
const JokerInfo* info = get_joker_registry_entry(joker_id);
|
||||
if (info->rarity == joker_rarity)
|
||||
{
|
||||
matching_joker_ids[match_count++] = joker_id;
|
||||
}
|
||||
}
|
||||
|
||||
int selected_joker_id = (match_count > 0) ? matching_joker_ids[rng_get_u32() % match_count]
|
||||
: fallback_random_joker_id;
|
||||
|
||||
return selected_joker_id;
|
||||
}
|
||||
|
||||
Item* joker_object_roll_new(void)
|
||||
{
|
||||
if (no_rollable_jokers())
|
||||
return NULL;
|
||||
|
||||
int joker_id = 0;
|
||||
#ifdef TEST_JOKER_ID0 // Allow defining an ID for a joker to always appear in shop and be tested
|
||||
if (joker_is_rollable(TEST_JOKER_ID0))
|
||||
{
|
||||
joker_id = TEST_JOKER_ID0;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
#ifdef TEST_JOKER_ID1
|
||||
if (joker_is_rollable(TEST_JOKER_ID1))
|
||||
{
|
||||
joker_id = TEST_JOKER_ID1;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
joker_id = joker_roll_id();
|
||||
}
|
||||
|
||||
// If for some reason only no joker is left, don't make another
|
||||
if (joker_id == UNDEFINED)
|
||||
return NULL;
|
||||
|
||||
joker_set_rollable(joker_id, false);
|
||||
|
||||
return (Item*)joker_object_new(joker_new(joker_id));
|
||||
}
|
||||
|
||||
static void set_and_shift_text(char* str, int* cursor_pos_x, int* cursor_pos_y, int color_pb)
|
||||
{
|
||||
tte_set_pos(*cursor_pos_x, *cursor_pos_y);
|
||||
tte_set_special(color_pb * TTE_SPECIAL_PB_MULT_OFFSET);
|
||||
@@ -305,12 +461,12 @@ bool joker_object_score(
|
||||
{
|
||||
// display the text on top of the card instead of below the Joker for Held Cards effects
|
||||
// scored_card cannot be NULL here because of the joker event
|
||||
cursorPosX += fx2int(card_object->sprite_object->x);
|
||||
cursorPosX += fx2int(card_object->x);
|
||||
cursorPosY = HELD_CARD_SCORE_TEXT_Y;
|
||||
}
|
||||
else
|
||||
{
|
||||
cursorPosX += fx2int(joker_object->sprite_object->x);
|
||||
cursorPosX += fx2int(joker_object->x);
|
||||
cursorPosY = JOKER_SCORE_TEXT_Y;
|
||||
}
|
||||
|
||||
@@ -375,7 +531,7 @@ Sprite* joker_object_get_sprite(JokerObject* joker_object)
|
||||
{
|
||||
if (joker_object == NULL)
|
||||
return NULL;
|
||||
return sprite_object_get_sprite(joker_object->sprite_object);
|
||||
return sprite_object_get_sprite((SpriteObject*)joker_object);
|
||||
}
|
||||
|
||||
int joker_get_random_rarity()
|
||||
|
||||
Reference in New Issue
Block a user