Files
balatro-gba-chinese-jocker-…/source/card.c
T
d24b0b5619 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>
2026-07-09 09:13:51 +03:00

205 lines
5.2 KiB
C

#include "card.h"
#include "graphic_utils.h"
#include "item.h"
#include "util.h"
#include <maxmod.h>
#include <stdlib.h>
// Audio
#include "pool.h"
#include "soundbank.h"
// Card Sprites and Palettes
#include "deck_big_gfx.h"
#include "deck_gfx.h"
#include "decks_face_down_gfx.h"
#include "high_contrast_deck_pal_gfx.h"
// Card sprites lookup table. First index is the suit, second index is the rank. The value is the
// tile index.
const static u16 _card_sprite_lut[NUM_SUITS][NUM_RANKS] = {
{0, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192},
{208, 224, 240, 256, 272, 288, 304, 320, 336, 352, 368, 384, 400},
{416, 432, 448, 464, 480, 496, 512, 528, 544, 560, 576, 592, 608},
{624, 640, 656, 672, 688, 704, 720, 736, 752, 768, 784, 800, 816}
};
// Deck sprites lookup table. Index is the deck Id. The value is the tile index.
const static u16 _deck_sprite_lut[DECK_TYPE_MAX] = {0, 16, 32, 48, 64, 80};
bool high_contrast = DEFAULT_HIGH_CONTRAST;
bool more_readable = DEFAULT_MORE_READABLE;
void card_init()
{
GRIT_CPY(&pal_obj_mem[DECK_SPRITES_PB * PAL_ROW_LEN], decks_face_down_gfxPal);
}
void set_cards_high_contrast(bool enable)
{
high_contrast = enable;
if (high_contrast)
{
GRIT_CPY(&pal_obj_mem[CARD_PB * PAL_ROW_LEN], high_contrast_deck_pal_gfxPal);
}
else
{
GRIT_CPY(&pal_obj_mem[CARD_PB * PAL_ROW_LEN], deck_gfxPal);
}
}
void set_cards_more_readable(bool enable)
{
more_readable = enable;
}
bool get_cards_high_contrast(void)
{
return high_contrast;
}
bool get_cards_more_readable(void)
{
return more_readable;
}
// Card methods
Card* card_new(u8 suit, u8 rank)
{
Card* card = POOL_GET(Card);
card->suit = suit;
card->rank = rank;
return card;
}
void card_destroy(Card** card)
{
POOL_FREE(Card, *card);
*card = NULL;
}
u8 card_get_value(Card* card)
{
if (card->rank == JACK || card->rank == QUEEN || card->rank == KING)
{
return 10; // Face cards are worth 10
}
else if (card->rank == ACE)
{
return 11; // Ace is worth 11
}
else
{
return card->rank + RANK_OFFSET; // 2-10 are worth their rank + RANK_OFFSET
}
return 0; // Should never reach here, but just in case
}
// CardObject methods
CardObject* card_object_new(Card* card)
{
CardObject* card_object = POOL_GET(CardObject);
sprite_object_init((SpriteObject*)card_object);
card_object->card = card;
card_object->type = ITEM_TYPE_PLAYING_CARD;
card_object->selected = false;
return card_object;
}
void card_object_destroy(CardObject** card_object)
{
if (*card_object == NULL)
return;
sprite_object_destroy((SpriteObject*)(*card_object));
POOL_FREE(CardObject, *card_object);
*card_object = NULL;
}
void card_object_set_sprite(CardObject* card_object, int layer)
{
int tile_index = CARD_TID + (layer * CARD_SPRITE_OFFSET);
const unsigned int* card_tiles = more_readable ? deck_big_gfxTiles : deck_gfxTiles;
memcpy32(
&tile_mem[TILE_MEM_OBJ_CHARBLOCK0_IDX][tile_index],
&card_tiles[_card_sprite_lut[card_object->card->suit][card_object->card->rank] * TILE_SIZE],
TILE_SIZE * CARD_SPRITE_OFFSET
);
Sprite* sprite = sprite_new(
ATTR0_SQUARE | ATTR0_4BPP | ATTR0_AFF,
ATTR1_SIZE_32,
tile_index,
CARD_PB,
layer + CARD_STARTING_LAYER
);
sprite_object_set_sprite((SpriteObject*)card_object, sprite);
}
void card_object_set_sprite_face_down(CardObject* card_object, enum DeckType deck, int layer)
{
int tile_index = CARD_TID + (layer * CARD_SPRITE_OFFSET);
memcpy32(
&tile_mem[TILE_MEM_OBJ_CHARBLOCK0_IDX][tile_index],
&decks_face_down_gfxTiles[_deck_sprite_lut[deck] * TILE_SIZE],
TILE_SIZE * CARD_SPRITE_OFFSET
);
Sprite* sprite = sprite_new(
ATTR0_SQUARE | ATTR0_4BPP | ATTR0_AFF,
ATTR1_SIZE_32,
tile_index,
DECK_SPRITES_PB,
layer + CARD_STARTING_LAYER
);
sprite_object_set_sprite((SpriteObject*)card_object, sprite);
}
void card_object_shake(CardObject* card_object, mm_word sound_id)
{
sprite_object_shake((SpriteObject*)card_object, sound_id);
}
void card_object_set_selected(CardObject* card_object, bool selected)
{
if (card_object == NULL)
return;
card_object->selected = selected;
}
bool card_object_is_selected(CardObject* card_object)
{
if (card_object == NULL)
return false;
return card_object->selected;
}
Sprite* card_object_get_sprite(CardObject* card_object)
{
if (card_object == NULL)
return NULL;
return sprite_object_get_sprite((SpriteObject*)card_object);
}
int card_object_get_buy_price(Item* card_object)
{
GBAL_RETURN_IF_NULL_RET(card_object, UNDEFINED);
ITEM_RETURN_IF_UNEXPECTED_TYPE_RET(card_object, ITEM_TYPE_PLAYING_CARD, UNDEFINED);
return 1;
}
void card_object_dispose(Item** card_object_item)
{
GBAL_RETURN_IF_NULL_VOID(card_object_item);
GBAL_RETURN_IF_NULL_VOID(*card_object_item);
ITEM_RETURN_IF_UNEXPECTED_TYPE_VOID(*card_object_item, ITEM_TYPE_PLAYING_CARD);
CardObject* card_object = (CardObject*)(*card_object_item);
card_object_destroy(&card_object);
*card_object_item = NULL;
}