GBAlatro
A Demake of Balatro for the GBA
Loading...
Searching...
No Matches
blind.c
Go to the documentation of this file.
1
6#include "blind.h"
7
8#include "blind_gfx.h"
9#include "game.h"
10#include "graphic_utils.h"
11#include "hand.h"
12#include "list.h"
13#include "random.h"
14#include "stdbool.h"
15#include "util.h"
16
17#include <stdlib.h>
18#include <tonc.h>
19
20#define NORMAL_BLIND_PB 2
21#define BOSS_BLIND_PB 3
22
23#define BLIND_BASE_LAYER (MAX_HAND_SIZE + MAX_SELECTION_SIZE)
24
25#define BLIND_SPRITE_OFFSET 16
26#define BLIND_SPRITE_COPY_SIZE (BLIND_SPRITE_OFFSET * TILE_SIZE)
27
28#define BLIND_TOKENS_PER_SPRITESHEET 2
29#define BLIND_TOKEN_PALETTE_SIZE 8
30
31#define SMALL_BLIND_REWARD 3
32#define BIG_BLIND_REWARD 4
33#define BOSS_BLIND_REWARD 5
34#define SHOWDOWN_BLIND_REWARD 8
35
36static const unsigned int* blind_gfxTiles[] = {
37#define DEF_BLIND_GFX(idx) blind_gfx##idx##Tiles,
38#include "../include/def_blind_gfx_table.h"
39#undef DEF_BLIND_GFX
40};
41
42static const unsigned short* blind_gfxPal[] = {
43#define DEF_BLIND_GFX(idx) blind_gfx##idx##Pal,
44#include "../include/def_blind_gfx_table.h"
45#undef DEF_BLIND_GFX
46};
47
48// Bitfields storing blinds we have yet to beat during the current run
49static List s_unbeaten_boss_blinds = LIST_DEFAULT;
50static List s_unbeaten_showdown_blinds = LIST_DEFAULT;
51
52// Maps the ante number to the base blind requirement for that ante.
53// The game starts at ante 1 which is at index 1 for base requirement 300.
54// Ante 0 is also there in case it is ever reached.
55static const u32 ANTE_LUT[] = {100, 300, 800, 2000, 5000, 11000, 20000, 35000, 50000};
56
57// clang-format off
63static Blind s_blind_type_map[BLIND_TYPE_MAX] = {
64 {BLIND_TYPE_SMALL, FIX_ONE },
65 {BLIND_TYPE_BIG, (FIX_ONE * 3) / 2},
66 {BLIND_TYPE_HOOK, FIX_ONE * 2 },
67 {BLIND_TYPE_OX, FIX_ONE * 2 },
68 {BLIND_TYPE_HOUSE, FIX_ONE * 2 },
69 {BLIND_TYPE_WHEEL, FIX_ONE * 2 },
70 {BLIND_TYPE_WALL, FIX_ONE * 2 }, // x4 score requirement will be part of the effect
71 {BLIND_TYPE_ARM, FIX_ONE * 2 },
72 {BLIND_TYPE_CLUB, FIX_ONE * 2 },
73 {BLIND_TYPE_FISH, FIX_ONE * 2 },
74 {BLIND_TYPE_PSYCHIC, FIX_ONE * 2 },
75 {BLIND_TYPE_GOAD, FIX_ONE * 2 },
76 {BLIND_TYPE_WATER, FIX_ONE * 2 },
77 {BLIND_TYPE_WINDOW, FIX_ONE * 2 },
78 {BLIND_TYPE_MANACLE, FIX_ONE * 2 },
79 {BLIND_TYPE_EYE, FIX_ONE * 2 },
80 {BLIND_TYPE_MOUTH, FIX_ONE * 2 },
81 {BLIND_TYPE_PLANT, FIX_ONE * 2 },
82 {BLIND_TYPE_SERPENT, FIX_ONE * 2 },
83 {BLIND_TYPE_PILLAR, FIX_ONE * 2 },
84 {BLIND_TYPE_NEEDLE, FIX_ONE * 2 }, // Same as the Wall with normal requirement
85 {BLIND_TYPE_HEAD, FIX_ONE * 2 },
86 {BLIND_TYPE_TOOTH, FIX_ONE * 2 },
87 {BLIND_TYPE_FLINT, FIX_ONE * 2 },
88 {BLIND_TYPE_MARK, FIX_ONE * 2 },
89 {BLIND_TYPE_ACORN, FIX_ONE * 2 },
90 {BLIND_TYPE_LEAF, FIX_ONE * 2 },
91 {BLIND_TYPE_VESSEL, FIX_ONE * 2 }, // Same as the Wall with x6 requirement
92 {BLIND_TYPE_HEART, FIX_ONE * 2 },
93 {BLIND_TYPE_BELL, FIX_ONE * 2 }
94};
95// clang-format on
96
97void blind_init()
98{
99 // Set up the palette used by normal blind tokens.
100 // We will never need to edit it ever again.
101 memcpy16(&pal_obj_bank[NORMAL_BLIND_PB], blind_gfxPal[0], NUM_ELEM_IN_ARR(blind_gfx0Pal));
102
103 return;
104}
105
106u32 blind_get_requirement(enum BlindType type, int ante)
107{
108 // Ensure ante is within valid range
109 if (ante < 0 || ante > MAX_ANTE)
110 ante = 0;
111
112 return fx2int(s_blind_type_map[type].score_req_multiplier * ANTE_LUT[ante]);
113}
114
116{
117 switch (type)
118 {
119 case BLIND_TYPE_SMALL:
120 return SMALL_BLIND_REWARD;
121 case BLIND_TYPE_BIG:
122 return BIG_BLIND_REWARD;
123 case BLIND_TYPE_BOSS ...(BLIND_TYPE_SHOWDOWN - 1):
124 return BOSS_BLIND_REWARD;
125 default:
126 return SHOWDOWN_BLIND_REWARD;
127 }
128}
129
140static void init_unbeaten_blinds_list(bool showdown)
141{
142 List* p_unbeaten_blinds = showdown ? &s_unbeaten_showdown_blinds : &s_unbeaten_boss_blinds;
143
144 // empty the list just to be sure
145 list_clear(p_unbeaten_blinds);
146
147 int lower_blind = showdown ? BLIND_TYPE_SHOWDOWN : BLIND_TYPE_BOSS;
148 int upper_blind = showdown ? BLIND_TYPE_MAX - 1 : BLIND_TYPE_SHOWDOWN - 1;
149
150 for (int i = lower_blind; i <= upper_blind; i++)
151 {
152 list_push_back(p_unbeaten_blinds, &s_blind_type_map[i]);
153 }
154}
155
161
162enum BlindType roll_blind_type(bool showdown)
163{
164 List* p_unbeaten_blinds = showdown ? &s_unbeaten_showdown_blinds : &s_unbeaten_boss_blinds;
165
166 // Fill the list with all blinds if it is empty
167 // (happens on startup or if we have beaten all blinds)
168 if (list_is_empty(p_unbeaten_blinds))
169 {
171 }
172
173 // roll a random blind among the unbeaten ones
174 int random_blind_idx = rng_get_u32(RNG_SEQ_BLIND) % list_get_len(p_unbeaten_blinds);
175 Blind* random_blind = list_get_at_idx(p_unbeaten_blinds, random_blind_idx);
176
177 return random_blind->type;
178}
179
181{
182 bool showdown = (type >= BLIND_TYPE_SHOWDOWN);
183 List* p_unbeaten_blinds = showdown ? &s_unbeaten_showdown_blinds : &s_unbeaten_boss_blinds;
184
185 // find the beaten blind idx in the list
186 int beaten_idx = 0;
187 Blind* beaten_blind = NULL;
188 ListItr itr = list_itr_create(p_unbeaten_blinds);
189
190 while ((beaten_blind = list_itr_next(&itr)))
191 {
192 if (beaten_blind->type == type)
193 {
194 break;
195 }
196 beaten_idx++;
197 }
198
199 if (beaten_idx < list_get_len(p_unbeaten_blinds))
200 {
201 list_remove_at_idx(p_unbeaten_blinds, beaten_idx);
202 }
203}
204
205static u32 get_blind_pb(enum BlindType type)
206{
207 return (type <= BLIND_TYPE_BIG) ? NORMAL_BLIND_PB : BOSS_BLIND_PB;
208}
209
210static u32 get_blind_spritesheet_idx(enum BlindType type)
211{
212 // See comment below in blind_get_color why we differenciate before/after the Mark
213 return (type < BLIND_TYPE_MARK)
214 ? type / BLIND_TOKENS_PER_SPRITESHEET
215 : BLIND_TYPE_MARK / BLIND_TOKENS_PER_SPRITESHEET + type - BLIND_TYPE_MARK;
216}
217
218u16 blind_get_color(enum BlindType type, enum BlindColorIndex index)
219{
220 // Do a little translation of palette idx -> custom array idx
221 // All blinds before the Mark are arranged in pairs with their palettte split in two
222 // | XX | 1 | 2 | 3 | 4 | 5 | 6 | 7 | -> first sprite
223 // | XX | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -> second sprite
224 // so we need to offset the color indices by 8 for blinds with an odd type
225 // From the Mark onwards, all sprites are alone in their spritesheet, so no need to offset
226 u32 color_idx = index + ((type < BLIND_TYPE_MARK)
227 ? BLIND_TOKEN_PALETTE_SIZE * (type % BLIND_TOKENS_PER_SPRITESHEET)
228 : 0);
229 return blind_gfxPal[get_blind_spritesheet_idx(type)][color_idx];
230}
231
233{
234 // keep track of active boss blind spritesheet to copy colors only when changing
235 static u32 active_boss_spritesheet = BLIND_TYPE_MAX;
236 u32 new_spritesheet = get_blind_spritesheet_idx(type);
237
238 // No need to update normal blind palette
239 if (type < BLIND_TYPE_BOSS || active_boss_spritesheet == new_spritesheet)
240 {
241 return;
242 }
243
244 active_boss_spritesheet = new_spritesheet;
245
246 // Just copy the palette as is to the sprite palette bank
247 memcpy16(
248 &pal_obj_bank[BOSS_BLIND_PB],
249 blind_gfxPal[active_boss_spritesheet],
250 NUM_ELEM_IN_ARR(blind_gfx0Pal)
251 );
252}
253
263{
264 // All Blind sprites are stored sequentially and correspond to their IDs
265 return (BLIND_BASE_LAYER + layer) * BLIND_SPRITE_OFFSET;
266}
267
269{
270 u32 spritesheet_idx = get_blind_spritesheet_idx(type);
271 u32 sprite_idx = (type < BLIND_TYPE_MARK) ? type % BLIND_TOKENS_PER_SPRITESHEET : 0;
272 memcpy32(
274 &blind_gfxTiles[spritesheet_idx][sprite_idx * BLIND_SPRITE_COPY_SIZE],
275 BLIND_SPRITE_COPY_SIZE
276 );
277
278 apply_blind_colors(type);
279}
280
281Sprite* blind_token_new(enum BlindType type, int x, int y, enum BlindTokenLayers layer)
282{
283 apply_blind_tiles(type, layer);
284
285 Sprite* sprite = sprite_new(
286 ATTR0_SQUARE | ATTR0_4BPP,
287 ATTR1_SIZE_32x32,
289 get_blind_pb(type),
290 BLIND_BASE_LAYER + layer
291 );
292 sprite_position(sprite, x, y);
293
294 return sprite;
295}
void apply_blind_tiles(enum BlindType type, enum BlindTokenLayers layer)
Change the tiles of the BlindToken Sprite at a given layer to that of the given BlindType.
Definition blind.c:268
static u32 get_layer_tile_index(enum BlindTokenLayers layer)
Get the starting tile index in tiles memory for the given BlindToken sprite layer.
Definition blind.c:262
enum BlindType roll_blind_type(bool showdown)
Choose a random Blind among the ones that haven't been beaten yet.
Definition blind.c:162
void apply_blind_colors(enum BlindType type)
Copy the palette associated with the given Blind and copy it in the right spot in the palette memory.
Definition blind.c:232
static void init_unbeaten_blinds_list(bool showdown)
Fill Lists of unbeaten Boss and Showdown Blinds.
Definition blind.c:140
Sprite * blind_token_new(enum BlindType type, int x, int y, enum BlindTokenLayers layer)
Create a new BlindToken sprite.
Definition blind.c:281
static Blind s_blind_type_map[BLIND_TYPE_MAX]
Stores an instance of the Blind struct for each BlindType value, ordered in the same way....
Definition blind.c:63
u16 blind_get_color(enum BlindType type, enum BlindColorIndex index)
Get the color associated with a given Blind.
Definition blind.c:218
u32 blind_get_requirement(enum BlindType type, int ante)
Get the score required to beat a certain blind (either the Small, Big, or any Boss/Showdown blind) at...
Definition blind.c:106
void set_blind_beaten(enum BlindType type)
Remove the given Blind from the corresponding List so that we don't roll it again in the future.
Definition blind.c:180
void init_unbeaten_blinds_lists(void)
Initialize Boss and Showdown Blinds lists to roll from.
Definition blind.c:156
int blind_get_reward(enum BlindType type)
Get the amount of money gained from beating a certain Blind.
Definition blind.c:115
Data structures and functions relative to the behaviour and graphics of Blinds.
BlindType
All Blind types in the game are listed here.
Definition blind.h:69
BlindTokenLayers
Sprite IDs of the various Blind Tokens used in the game, expressed as an offset relative to BLIND_BAS...
Definition blind.h:36
BlindColorIndex
Indices of the Blind sprites' colors as encoded in the files' palettes with Aseprite.
Definition blind.h:49
Graphic utility functions.
#define TILE_MEM_OBJ_CHARBLOCK0_IDX
The index for the first charblock of the object memory in tonc's tile_mem.
Functions relative to manipulating and analyzing the contents of the Hand, a.k.a. the Cards we hold a...
A doubly-linked list.
bool list_is_empty(const List *list)
Definition list.c:62
void * list_get_at_idx(List *list, unsigned int idx)
Definition list.c:219
int list_get_len(const List *list)
Definition list.c:214
void list_clear(List *list)
Definition list.c:44
void * list_itr_next(ListItr *itr)
Definition list.c:281
bool list_remove_at_idx(List *list, unsigned int idx)
Definition list.c:237
#define LIST_DEFAULT
Default list declaration for empty lists.
Definition list.h:30
void list_push_back(List *list, void *data)
Definition list.c:89
ListItr list_itr_create(List *list)
Definition list.c:257
Common functions to handle RNG manipulation. Using this interface has three goals:
u32 rng_get_u32(enum RngSequence key)
Get the next "randomly" generated number in the sequence corresponding to the given type.
Definition random.c:56
INLINE void sprite_position(Sprite *sprite, int x, int y)
Set sprite position. Inlined for efficiency.
Definition sprite.h:400
Sprite * sprite_new(u16 a0, u16 a1, u32 tid, u32 pb, s16 sprite_index)
Allocate and retrieve a pointer to a valid Sprite.
Definition sprite.c:36
Data structure containing data about a BlindType.
Definition blind.h:137
An iterator into a list.
Definition list.h:91
A doubly-linked list.
Definition list.h:61
Sprite struct for GBA hardware specifics.
Definition sprite.h:29
Utilities relating around number string representation and protected arithmatic helper functions.
#define NUM_ELEM_IN_ARR(arr)
Get the number of elements in an array.
Definition util.h:43