diff --git a/include/joker.h b/include/joker.h index 9e387a0..a735066 100644 --- a/include/joker.h +++ b/include/joker.h @@ -60,8 +60,8 @@ typedef struct { u8 base_value; JokerEffectFunc effect; } JokerInfo; -extern const JokerInfo joker_registry[]; -extern const size_t joker_registry_size; +const JokerInfo* get_joker_registry_entry(int joker_id); +size_t get_joker_registry_size(void); void joker_init(); diff --git a/include/util.h b/include/util.h index 39b872b..54edbc1 100644 --- a/include/util.h +++ b/include/util.h @@ -35,4 +35,6 @@ static inline int get_digits_even(int n) #define UNDEFINED -1 +#define NUM_ELEM_IN_ARR(arr) (sizeof(arr) / sizeof((arr)[0])) + #endif // UTIL_H \ No newline at end of file diff --git a/source/game.c b/source/game.c index 9089327..204c9c3 100644 --- a/source/game.c +++ b/source/game.c @@ -1825,7 +1825,7 @@ static void game_shop_create_items(JokerObject *shop_jokers[], bool first_time) joker_object_destroy(&shop_jokers[i]); // Destroy the joker object if it exists } - u8 joker_id = random() % joker_registry_size; + u8 joker_id = random() % get_joker_registry_size(); shop_jokers[i] = joker_object_new(joker_new(joker_id)); shop_jokers[i]->sprite_object->x = int2fx(120 + i * 32); diff --git a/source/joker.c b/source/joker.c index 2233405..233769b 100644 --- a/source/joker.c +++ b/source/joker.c @@ -38,10 +38,10 @@ void joker_init() Joker *joker_new(u8 id) { - if (id >= joker_registry_size) return NULL; + if (id >= get_joker_registry_size()) return NULL; Joker *joker = malloc(sizeof(Joker)); - const JokerInfo *jinfo = &joker_registry[id]; + const JokerInfo *jinfo = get_joker_registry_entry(id); joker->id = id; joker->modifier = BASE_EDITION; // TODO: Make this random later @@ -61,8 +61,10 @@ void joker_destroy(Joker **joker) JokerEffect joker_get_score_effect(Joker *joker, Card *scored_card) { - if (joker->id >= joker_registry_size) return (JokerEffect){0}; - return joker_registry[joker->id].effect(joker, scored_card); + const JokerInfo *jinfo = get_joker_registry_entry(joker->id); + if (!jinfo) return (JokerEffect){0}; + + return jinfo->effect(joker, scored_card); } // JokerObject methods diff --git a/source/joker_effects.c b/source/joker_effects.c index 08b0728..438af93 100644 --- a/source/joker_effects.c +++ b/source/joker_effects.c @@ -1,4 +1,5 @@ #include "joker.h" +#include "util.h" static JokerEffect default_joker_effect(Joker *joker, Card *scored_card) { @@ -38,4 +39,15 @@ const JokerInfo joker_registry[] = { { COMMON_JOKER, 5, gluttonous_joker_effect }, }; -const size_t joker_registry_size = sizeof(joker_registry) / sizeof(joker_registry[0]); +static const size_t joker_registry_size = NUM_ELEM_IN_ARR(joker_registry); + +const JokerInfo* get_joker_registry_entry(int joker_id) { + if (joker_id < 0 || (size_t)joker_id >= joker_registry_size) { + return NULL; + } + return &joker_registry[joker_id]; +} + +size_t get_joker_registry_size(void) { + return joker_registry_size; +}