Update card handling logic and improve background management; add hand types and refine game state transitions.

This commit is contained in:
cellos51
2025-05-19 11:24:13 -04:00
parent 7ce6082edd
commit 2d21c83f89
4 changed files with 558 additions and 30 deletions
+25 -3
View File
@@ -9,7 +9,7 @@
#define HEARTS 0
#define DIAMONDS 1
#define CLUBS 2
#define SPADES 2
#define SPADES 3
// Card ranks
#define TWO 0
@@ -26,13 +26,33 @@
#define KING 11
#define ACE 12
// Hand types
enum HandType
{
NONE,
HIGH_CARD,
PAIR,
TWO_PAIR,
THREE_OF_A_KIND,
STRAIGHT,
FLUSH,
FULL_HOUSE,
FOUR_OF_A_KIND,
STRAIGHT_FLUSH,
ROYAL_FLUSH,
FIVE_OF_A_KIND,
FLUSH_HOUSE,
FLUSH_FIVE
};
// Hand states
enum HandState
{
HAND_DRAW,
HAND_SELECT,
HAND_DISCARD,
HAND_PLAY,
HAND_DISCARD
HAND_PLAYING
};
// Card types
@@ -68,9 +88,11 @@ void hand_set_focus(int index); // This makes the card at the given index the on
int hand_get_focus(); // This gets the index of the card that's being looked at
void hand_select(); // This lets the player select the card they're looking at for the next action
void hand_change_sort(); // This sorts the hand by suit or rank
bool hand_discard(); // This discards the selected cards
int hand_get_size(); // This gets the size of the hand
int hand_get_max_size(); // This gets the maximum size of the hand (this doesn't get the array max, it gets the total amount of cards that you have)
enum HandType hand_get_type(); // This gets the type of the hand
bool hand_discard(); // This discards the selected cards
bool hand_play(); // This plays the selected cards
// Deck functions
int deck_get_size(); // This gets the size of the deck
+358 -23
View File
@@ -20,6 +20,8 @@ const static u16 card_sprite_lut[4][13] = {
static enum HandState hand_state = HAND_DRAW;
static enum HandType hand_type = NONE;
static CardObject *played[MAX_SELECTION_SIZE] = {NULL};
static CardObject *hand[MAX_HAND_SIZE] = {NULL};
static Card *deck[MAX_DECK_SIZE] = {NULL};
@@ -36,7 +38,7 @@ static int card_focused = 0;
static bool sort_by_suit = false;
// Sorting functions
// General functions
static inline void sort_cards()
{
if (sort_by_suit)
@@ -91,26 +93,26 @@ static inline void sort_cards()
}
// Played stack
inline void played_push(CardObject *card_object)
static inline void played_push(CardObject *card_object)
{
if (played_top >= MAX_SELECTION_SIZE - 1) return;
played[++played_top] = card_object;
}
inline CardObject *played_pop()
static inline CardObject *played_pop()
{
if (played_top < 0) return NULL;
return played[played_top--];
}
// Deck stack
inline void deck_push(Card *card)
static inline void deck_push(Card *card)
{
if (deck_top >= MAX_DECK_SIZE - 1) return;
deck[++deck_top] = card;
}
inline Card *deck_pop()
static inline Card *deck_pop()
{
if (deck_top < 0) return NULL;
return deck[deck_top--];
@@ -208,7 +210,9 @@ void card_update() // This whole function is currently pretty unoptimized due to
{
const int update_frame = 10; // This is the number of frames between each card event
static int timer = 0;
static int timer = 1;
timer++;
if (hand_state == HAND_DRAW && cards_drawn < hand_size)
{
if (timer % update_frame == 0) // Draw a card every 10 frames
@@ -216,15 +220,16 @@ void card_update() // This whole function is currently pretty unoptimized due to
cards_drawn++;
card_draw();
}
timer++;
}
else if (hand_state == HAND_DRAW)
{
hand_state = HAND_SELECT; // Change the hand state to select after drawing all the cards
cards_drawn = 0;
timer = 0;
timer = 1;
}
static int played_selections = 0;
static bool sound_played = false;
bool discarded_card = false;
for (int i = 0; i < MAX_HAND_SIZE; i++)
@@ -264,8 +269,6 @@ void card_update() // This whole function is currently pretty unoptimized due to
hand_x = hand_x + (int2fx(i) - int2fx(hand_top) / 2) * -spacing_lut[hand_top]; // TODO: Change this later to reference a 2D LUT of positions
break;
case HAND_DISCARD: // TODO: Add sound
static bool sound_played = false;
if (hand[i]->selected)
{
if (!discarded_card)
@@ -289,6 +292,7 @@ void card_update() // This whole function is currently pretty unoptimized due to
hand_top--;
cards_drawn++; // This technically isn't drawing cards, I'm just reusing the variable
sound_played = false;
timer = 1;
}
discarded_card = true;
@@ -304,19 +308,169 @@ void card_update() // This whole function is currently pretty unoptimized due to
hand_x = hand_x + (int2fx(i) - int2fx(hand_top) / 2) * -spacing_lut[hand_top];
}
if (i == hand_top && discarded_card == false)
if (i == hand_top && discarded_card == false && timer % update_frame == 0)
{
hand_state = HAND_DRAW;
sound_played = false;
cards_drawn = 0;
hand_selections = 0;
timer = 0;
timer = 1;
break;
}
timer++;
break;
case HAND_PLAY:
hand_x = hand_x + (int2fx(i) - int2fx(hand_top) / 2) * -spacing_lut[hand_top];
hand_y += (24 << FIX_SHIFT);
if (hand[i]->selected && timer % update_frame == 0)
{
hand[i]->selected = false;
played_push(hand[i]);
sprite_destroy(&hand[i]->sprite);
hand[i] = NULL;
sort_cards();
const int pitch_lut[MAX_SELECTION_SIZE] = {1024, 960, 896, 832, 768};
mm_sound_effect sfx_draw = {{SFX_CARD_DRAW}, pitch_lut[cards_drawn], 0, 255, 128,};
mmEffectEx(&sfx_draw);
hand_top--;
hand_selections--;
cards_drawn++;
timer = 1;
}
if (i == hand_top && timer % update_frame == 0)
{
hand_state = HAND_PLAYING;
cards_drawn = 0;
hand_selections = 0;
timer = 1;
played_selections = played_top + 1;
switch (hand_type) // select the cards that apply to the hand type
{
case NONE:
break;
case HIGH_CARD:
int highest_rank_index = 0;
for (int i = 0; i <= played_top; i++) // find the card with the highest rank in the hand
{
if (played[i]->card->rank > played[highest_rank_index]->card->rank)
{
highest_rank_index = i;
}
}
played[highest_rank_index]->selected = true;
break;
case PAIR:
for (int i = 0; i <= played_top - 1; i++) // find two cards with the same rank
{
if (played[i]->card->rank == played[i + 1]->card->rank)
{
played[i]->selected = true;
played[i + 1]->selected = true;
break;
}
}
break;
case TWO_PAIR:
for (int i = 0; i <= played_top - 1; i++) // find two pairs of cards with the same rank
{
if (played[i]->card->rank == played[i + 1]->card->rank)
{
played[i]->selected = true;
played[i + 1]->selected = true;
}
}
break;
case THREE_OF_A_KIND:
for (int i = 0; i <= played_top - 2; i++) // find three cards with the same rank
{
if (played[i]->card->rank == played[i + 1]->card->rank && played[i]->card->rank == played[i + 2]->card->rank)
{
played[i]->selected = true;
played[i + 1]->selected = true;
played[i + 2]->selected = true;
break;
}
}
break;
case STRAIGHT:
for (int i = 0; i <= played_top; i++) // find five cards in a row
{
played[i]->selected = true; // we don't have to check if cards are being played because a straight needs 5 cards which is the max selection size
}
break;
case FLUSH:
for (int i = 0; i <= played_top; i++) // find five cards with the same suit
{
played[i]->selected = true; // flush functions the same as straight
}
break;
case FULL_HOUSE:
for (int i = 0; i <= played_top; i++) // find three cards with the same rank and two cards with the same rank
{
played[i]->selected = true; // full house functions the same as straight
}
break;
case FOUR_OF_A_KIND:
for (int i = 0; i <= played_top - 3; i++) // find four cards with the same rank
{
if (played[i]->card->rank == played[i + 1]->card->rank && played[i]->card->rank == played[i + 2]->card->rank && played[i]->card->rank == played[i + 3]->card->rank)
{
played[i]->selected = true;
played[i + 1]->selected = true;
played[i + 2]->selected = true;
played[i + 3]->selected = true;
break;
}
}
break;
case STRAIGHT_FLUSH:
for (int i = 0; i <= played_top; i++) // find five cards in a row with the same suit
{
played[i]->selected = true; // straight flush functions the same as straight
}
break;
case ROYAL_FLUSH:
for (int i = 0; i <= played_top; i++) // find five cards in a row with the same suit starting with a 10
{
played[i]->selected = true; // royal flush functions the same as straight
}
break;
case FIVE_OF_A_KIND:
for (int i = 0; i <= played_top; i++) // find five cards with the same rank
{
played[i]->selected = true; // five of a kind functions the same as straight
}
break;
case FLUSH_HOUSE:
for (int i = 0; i <= played_top; i++) // find three cards with the same rank and two cards with the same suit
{
played[i]->selected = true; // flush house functions the same as straight
}
break;
case FLUSH_FIVE:
for (int i = 0; i <= played_top; i++) // find five cards with the same suit
{
played[i]->selected = true; // flush five functions the same as straight
}
break;
}
}
break;
case HAND_PLAYING:
hand_x = hand_x + (int2fx(i) - int2fx(hand_top) / 2) * -spacing_lut[hand_top];
hand_y += (24 << FIX_SHIFT);
// Method for switching back to drawn not thought of yet
break;
}
@@ -346,6 +500,57 @@ void card_update() // This whole function is currently pretty unoptimized due to
sprite_position(hand[i]->sprite, fx2int(hand[i]->x), fx2int(hand[i]->y));
}
}
if (played_selections > 0 && (timer % update_frame == 0 || played[played_selections - 1]->selected == false))
{
played_selections--;
}
for (int i = 0; i < played_top + 1; i++)
{
if (played[i] != NULL)
{
if (played[i]->sprite == NULL)
{
played[i]->sprite = sprite_new(ATTR0_SQUARE | ATTR0_4BPP | ATTR0_AFF, ATTR1_SIZE_32, card_sprite_lut[played[i]->card->suit][played[i]->card->rank], 0, i + MAX_HAND_SIZE);
}
FIXED played_x = int2fx(120);
FIXED played_y = int2fx(70);
played_x = played_x + (int2fx(i) - int2fx(played_top) / 2) * -27;
if (played[i]->selected && played_selections <= i)
{
played_y -= (10 << FIX_SHIFT);
}
played[i]->vx += (played_x - played[i]->x) / 8;
played[i]->vy += (played_y - played[i]->y) / 8;
// set velocity to 0 if it's close enough to the target
const float epsilon = float2fx(0.01f);
if (played[i]->vx < epsilon && played[i]->vx > -epsilon && played[i]->vy < epsilon && played[i]->vy > -epsilon)
{
played[i]->vx = 0;
played[i]->vy = 0;
played[i]->x = played_x;
played[i]->y = played_y;
}
else
{
played[i]->vx = (played[i]->vx * 7) / 10;
played[i]->vy = (played[i]->vy * 7) / 10;
played[i]->x += played[i]->vx;
played[i]->y += played[i]->vy;
}
obj_aff_rotate(played[i]->sprite->aff, -played[i]->vx);
sprite_position(played[i]->sprite, fx2int(played[i]->x), fx2int(played[i]->y));
}
}
}
// Hand functions
@@ -391,14 +596,6 @@ void hand_change_sort()
sort_cards();
}
bool hand_discard()
{
if (hand_state != HAND_SELECT || hand_selections == 0) return false;
hand_state = HAND_DISCARD;
card_focused = 0;
return true;
}
int hand_get_size()
{
return hand_top + 1;
@@ -409,8 +606,146 @@ int hand_get_max_size()
return hand_size;
}
// Deck functions
enum HandType hand_get_type()
{
// Idk if this is how Balatro does it but this is how I'm doing it
if (hand_selections == 0)
{
hand_type = NONE;
return hand_type;
}
hand_type = HIGH_CARD;
u8 suits[4] = {0};
u8 ranks[13] = {0};
for (int i = 0; i <= hand_top; i++)
{
if (hand[i] != NULL && hand[i]->selected)
{
suits[hand[i]->card->suit]++;
ranks[hand[i]->card->rank]++;
}
}
// Check for flush
for (int i = 0; i < 4; i++)
{
if (suits[i] >= MAX_SELECTION_SIZE) // if i add jokers just MAX_SELECTION_SIZE - 1 for four fingers
{
hand_type = FLUSH;
break;
}
}
// Check for straight
for (int i = 0; i < 9; i++)
{
if (ranks[i] && ranks[i + 1] && ranks[i + 2] && ranks[i + 3] && ranks[i + 4])
{
if (hand_type == FLUSH)
{
hand_type = STRAIGHT_FLUSH;
}
else
{
hand_type = STRAIGHT;
}
break;
}
}
// Check for ace low straight
if (ranks[ACE] && ranks[TWO] && ranks[THREE] && ranks[FOUR] && ranks[FIVE])
{
hand_type = STRAIGHT;
}
// Check for royal flush
if (hand_type == STRAIGHT_FLUSH && ranks[TEN] && ranks[JACK] && ranks[QUEEN] && ranks[KING] && ranks[ACE])
{
hand_type = ROYAL_FLUSH;
return hand_type;
}
// Check for straight flush
if (hand_type == STRAIGHT_FLUSH)
{
hand_type = STRAIGHT_FLUSH;
return hand_type;
}
// Check for flush five and five of a kind
if (hand_type == FLUSH && ranks[FIVE] >= 5)
{
hand_type = FLUSH_FIVE;
return hand_type;
}
else if (ranks[FIVE] >= 5)
{
return FIVE_OF_A_KIND;
}
// Check for for of a kind
for (int i = 0; i < 13; i++)
{
if (ranks[i] >= 4)
{
hand_type = FOUR_OF_A_KIND;
return hand_type;
}
else if (ranks[i] == 3)
{
if (hand_type == PAIR)
{
hand_type = FULL_HOUSE;
return hand_type;
}
else
{
hand_type = THREE_OF_A_KIND;
}
}
else if (ranks[i] == 2)
{
if (hand_type == THREE_OF_A_KIND)
{
hand_type = FULL_HOUSE;
return hand_type;
}
else if (hand_type == PAIR)
{
hand_type = TWO_PAIR;
return hand_type;
}
else
{
hand_type = PAIR;
}
}
}
return hand_type;
}
bool hand_discard()
{
if (hand_state != HAND_SELECT || hand_selections == 0) return false;
hand_state = HAND_DISCARD;
card_focused = 0;
return true;
}
bool hand_play()
{
if (hand_state != HAND_SELECT || hand_selections == 0) return false;
hand_state = HAND_PLAY;
card_focused = 0;
return true;
}
// Deck functions
int deck_get_size()
{
return deck_top + 1;
+173 -1
View File
@@ -8,11 +8,75 @@
#include "sprite.h"
#include "card.h"
#include "background_gfx.h"
#include "background_play_gfx.h"
static int hand = 4;
static int discard = 4;
static int chips = 0;
static int mult = 0;
static int background = 0;
void change_background(int id)
{
if (background == id)
{
return;
}
else if (id == 1) // selecting
{
memcpy(&se_mem[30][0], background_gfxMap, background_gfxMapLen);
// tte_printf("#{P:128,128; cx:0}%d/%d", hand_get_size(), hand_get_max_size()); // Hand size/max size
// tte_printf("#{P:200,152}%d/%d", deck_get_size(), deck_get_max_size()); // Deck size/max size
// tte_printf("#{P:32,48;}*%d", 0); // Score
// tte_printf("#{P:24,80;}%d", 0); // Chips
// tte_printf("#{P:40,80;}%d", 0); // Mult
// tte_printf("#{P:16,104; cx:0x2000}%d", hand); // Hand
// tte_printf("#{P:48,104; cx:0x3000}%d", discard); // Discard
// tte_printf("#{P:24,120; cx:0x1000}$%d", 4); // Money
// tte_printf("#{P:48,144}%d", 1); // Round
// tte_printf("#{P:8,144}%d#{cx:0}/%d", 1, 8); // Ante
tte_erase_rect(128, 152, 160, 136);
}
else if (id == 2) // playing
{
memcpy(&se_mem[30][0], background_play_gfxMap, background_play_gfxMapLen);
// tte_printf("#{P:128,128; cx:0}%d/%d", hand_get_size(), hand_get_max_size()); // Hand size/max size
// tte_printf("#{P:200,152}%d/%d", deck_get_size(), deck_get_max_size()); // Deck size/max size
// tte_printf("#{P:32,48;}*%d", 0); // Score
// tte_printf("#{P:24,80;}%d", 0); // Chips
// tte_printf("#{P:40,80;}%d", 0); // Mult
// tte_printf("#{P:16,104; cx:0x2000}%d", hand); // Hand
// tte_printf("#{P:48,104; cx:0x3000}%d", discard); // Discard
// tte_printf("#{P:24,120; cx:0x1000}$%d", 4); // Money
// tte_printf("#{P:48,144}%d", 1); // Round
// tte_printf("#{P:8,144}%d#{cx:0}/%d", 1, 8); // Ante
tte_erase_rect(128, 128, 152, 136);
}
background = id;
}
void game_init()
{
change_background(1);
tte_printf("#{P:128,128; cx:0}%d/%d", hand_get_size(), hand_get_max_size()); // Hand size/max size
tte_printf("#{P:200,152}%d/%d", deck_get_size(), deck_get_max_size()); // Deck size/max size
@@ -44,6 +108,99 @@ void game_update()
if (key_hit(KEY_A))
{
hand_select();
tte_erase_rect(8, 64, 64, 72);
tte_erase_rect(8, 80, 32, 88);
tte_erase_rect(40, 80, 64, 88);
switch (hand_get_type())
{
case HIGH_CARD:
tte_printf("#{P:8,64;}HIGH C");
chips = 5;
mult = 1;
break;
case PAIR:
tte_printf("#{P:8,64;}PAIR");
chips = 10;
mult = 2;
break;
case TWO_PAIR:
tte_printf("#{P:8,64;}2 PAIR");
chips = 20;
mult = 2;
break;
case THREE_OF_A_KIND:
tte_printf("#{P:8,64;}3 OAK");
chips = 30;
mult = 3;
break;
case STRAIGHT:
tte_printf("#{P:8,64;}STRT");
chips = 30;
mult = 4;
break;
case FLUSH:
tte_printf("#{P:8,64;}FLUSH");
chips = 35;
mult = 4;
break;
case FULL_HOUSE:
tte_printf("#{P:8,64;}FULL H");
chips = 40;
mult = 4;
break;
case FOUR_OF_A_KIND:
tte_printf("#{P:8,64;}4 OAK");
chips = 60;
mult = 7;
break;
case STRAIGHT_FLUSH:
tte_printf("#{P:8,64;}STRT F");
chips = 100;
mult = 8;
break;
case ROYAL_FLUSH:
tte_printf("#{P:8,64;}ROYAL F");
chips = 100;
mult = 8;
break;
case FIVE_OF_A_KIND:
tte_printf("#{P:8,64;}5 OAK");
chips = 120;
mult = 12;
break;
case FLUSH_HOUSE:
tte_printf("#{P:8,64;}FLUSH H");
chips = 140;
mult = 14;
break;
case FLUSH_FIVE:
tte_printf("#{P:8,64;}FLUSH 5");
chips = 160;
mult = 16;
break;
default:
chips = 0;
mult = 0;
break;
}
if (chips < 10)
{
tte_printf("#{P:24,80;}%d", chips); // Chips
}
else if (chips < 100)
{
tte_printf("#{P:16,80;}%d", chips);
}
else
{
tte_printf("#{P:8,80;}%d", chips);
}
tte_printf("#{P:40,80;}%d", mult); // Mult
}
if (key_hit(KEY_B))
@@ -57,15 +214,30 @@ void game_update()
{
discard--;
tte_printf("#{P:48,104; cx:0x3000}%d", discard);
tte_erase_rect(8, 64, 64, 72);
}
}
if (key_hit(KEY_START))
{
hand_play();
change_background(2);
}
static int last_hand_size = 0;
static int last_deck_size = 0;
if (last_hand_size != hand_get_size() || last_deck_size != deck_get_size())
{
tte_printf("#{P:128,128; cx:0}%d/%d", hand_get_size(), hand_get_max_size()); // Hand size/max size
if (background == 1)
{
tte_printf("#{P:128,128; cx:0}%d/%d", hand_get_size(), hand_get_max_size()); // Hand size/max size
}
else if (background == 2)
{
tte_printf("#{P:128,152; cx:0}%d/%d", hand_get_size(), hand_get_max_size()); // Hand size/max size
}
tte_printf("#{P:200,152}%d/%d", deck_get_size(), deck_get_max_size()); // Deck size/max size
last_hand_size = hand_get_size();
+2 -3
View File
@@ -30,9 +30,8 @@ void init()
REG_BG1CNT = BG_PRIO(0) | BG_CBB(1) | BG_SBB(31) | BG_4BPP;
// Load the tiles and palette
memcpy(pal_bg_mem, background_gfxPal, background_gfxPalLen);
memcpy(&tile_mem[0][0], background_gfxTiles, background_gfxTilesLen);
memcpy32(&se_mem[30][0], background_gfxMap, background_gfxMapLen);
memcpy16(pal_bg_mem, background_gfxPal, background_gfxPalLen);
memcpy32(&tile_mem[0][0], background_gfxTiles, background_gfxTilesLen);
memcpy(&tile_mem[4][0], deck_gfxTiles, deck_gfxTilesLen);
memcpy(pal_obj_mem, deck_gfxPal, deck_gfxPalLen);