Refactored list code to be generic void* list
This commit is contained in:
+9
-9
@@ -174,7 +174,7 @@ int get_jokers_top(void) {
|
||||
return jokers_top;
|
||||
}
|
||||
|
||||
IntList* jokers_available_to_shop; // List of joker IDs
|
||||
List* jokers_available_to_shop; // List of joker IDs
|
||||
|
||||
// Consts
|
||||
|
||||
@@ -1009,8 +1009,8 @@ void game_set_state(enum GameState new_game_state)
|
||||
void jokers_available_to_shop_init()
|
||||
{
|
||||
int num_defined_jokers = get_joker_registry_size();
|
||||
jokers_available_to_shop = int_list_new(num_defined_jokers);
|
||||
for (int i = 0; i < num_defined_jokers; i++)
|
||||
jokers_available_to_shop = list_new(num_defined_jokers);
|
||||
for (intptr_t i = 0; i < num_defined_jokers; i++)
|
||||
{
|
||||
// Add all joker IDs sequentially
|
||||
int_list_append(jokers_available_to_shop, i);
|
||||
@@ -2151,7 +2151,7 @@ static int reroll_cost = REROLL_BASE_COST;
|
||||
static void game_shop_create_items(JokerObject *shop_jokers[])
|
||||
{
|
||||
tte_erase_rect_wrapper(SHOP_PRICES_TEXT_RECT);
|
||||
if (int_list_get_size(jokers_available_to_shop) == 0)
|
||||
if (list_get_size(jokers_available_to_shop) == 0)
|
||||
{
|
||||
// No jokers to create
|
||||
return;
|
||||
@@ -2160,9 +2160,9 @@ static void game_shop_create_items(JokerObject *shop_jokers[])
|
||||
for (int i = 0; i < MAX_SHOP_JOKERS; i++)
|
||||
{
|
||||
// TODO: weight the random choice by joker rarity
|
||||
int joker_idx = random() % int_list_get_size(jokers_available_to_shop);
|
||||
int joker_id = int_list_get(jokers_available_to_shop, joker_idx);
|
||||
int_list_remove_by_idx(jokers_available_to_shop, joker_idx);
|
||||
int joker_idx = random() % list_get_size(jokers_available_to_shop);
|
||||
intptr_t joker_id = int_list_get(jokers_available_to_shop, joker_idx);
|
||||
list_remove_by_idx(jokers_available_to_shop, joker_idx);
|
||||
|
||||
shop_jokers[i] = joker_object_new(joker_new(joker_id));
|
||||
shop_jokers[i]->sprite_object->x = int2fx(120 + i * CARD_SPRITE_SIZE);
|
||||
@@ -2220,7 +2220,7 @@ static void game_shop_reroll(JokerObject** shop_jokers, int *reroll_cost)
|
||||
{
|
||||
if (shop_jokers[i] != NULL)
|
||||
{
|
||||
int_list_append(jokers_available_to_shop, shop_jokers[i]->joker->id);
|
||||
int_list_append(jokers_available_to_shop, (intptr_t)shop_jokers[i]->joker->id);
|
||||
joker_object_destroy(&shop_jokers[i]); // Destroy the joker object if it exists
|
||||
}
|
||||
}
|
||||
@@ -2482,7 +2482,7 @@ void game_shop()
|
||||
if (shop_jokers[i] != NULL)
|
||||
{
|
||||
// Make the joker available back to shop
|
||||
int_list_append(jokers_available_to_shop, shop_jokers[i]->joker->id);
|
||||
int_list_append(jokers_available_to_shop, (intptr_t)shop_jokers[i]->joker->id);
|
||||
}
|
||||
joker_object_destroy(&shop_jokers[i]); // Destroy the joker objects
|
||||
}
|
||||
|
||||
+28
-12
@@ -3,10 +3,10 @@
|
||||
#include "list.h"
|
||||
#include "util.h"
|
||||
|
||||
IntList *int_list_new(int init_size) {
|
||||
IntList *list = (IntList *)malloc(sizeof(IntList));
|
||||
List *list_new(int init_size) {
|
||||
List *list = (List *)malloc(sizeof(List));
|
||||
if (list == NULL) return NULL;
|
||||
list->_array = (int *)malloc(sizeof(int) * init_size);
|
||||
list->_array = (void **)malloc(sizeof(void*) * init_size);
|
||||
if (!list->_array)
|
||||
{
|
||||
free(list);
|
||||
@@ -17,7 +17,7 @@ IntList *int_list_new(int init_size) {
|
||||
return list;
|
||||
}
|
||||
|
||||
void int_list_destroy(IntList **list) {
|
||||
void list_destroy(List **list) {
|
||||
if (list == NULL || *list == NULL)
|
||||
return;
|
||||
{
|
||||
@@ -28,22 +28,28 @@ void int_list_destroy(IntList **list) {
|
||||
*list = NULL;
|
||||
}
|
||||
|
||||
bool int_list_append(IntList *list, int value)
|
||||
bool int_list_append(List *list, intptr_t value)
|
||||
{
|
||||
return list_append(list, (void*)value);
|
||||
}
|
||||
|
||||
bool list_append(List *list, void *value)
|
||||
{
|
||||
if (list->size >= list->allocated_size)
|
||||
{
|
||||
int new_size = list->allocated_size * 2;
|
||||
int *new_arr = (int *)realloc(list->_array, sizeof(int) * new_size);
|
||||
void **new_arr = (void **)realloc(list->_array, sizeof(void*) * new_size);
|
||||
if (new_arr == NULL)
|
||||
return false;
|
||||
list->_array = new_arr;
|
||||
list->allocated_size = new_size;
|
||||
}
|
||||
|
||||
list->_array[list->size++] = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool int_list_remove_by_idx(IntList *list, int index) {
|
||||
bool list_remove_by_idx(List *list, int index) {
|
||||
if (index < 0 || index >= list->size)
|
||||
return false;
|
||||
for (int i = index; i < list->size - 1; ++i)
|
||||
@@ -54,27 +60,37 @@ bool int_list_remove_by_idx(IntList *list, int index) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool int_list_remove_by_value(IntList *list, int value)
|
||||
bool list_remove_by_value(List *list, void* value)
|
||||
{
|
||||
for (int i = 0; i < list->size; i++)
|
||||
{
|
||||
if (list->_array[i] == value)
|
||||
{
|
||||
return int_list_remove_by_idx(list, i);
|
||||
return list_remove_by_idx(list, i);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int int_list_get(IntList *list, int index)
|
||||
bool int_list_remove_by_value(List *list, intptr_t value)
|
||||
{
|
||||
return list_remove_by_value(list, (void*)value);
|
||||
}
|
||||
|
||||
void* list_get(List *list, int index)
|
||||
{
|
||||
if (index < 0 || index >= list->size)
|
||||
return 0;
|
||||
return NULL;
|
||||
return list->_array[index];
|
||||
}
|
||||
|
||||
int int_list_get_size(IntList *list)
|
||||
intptr_t int_list_get(List *list, int index)
|
||||
{
|
||||
return (intptr_t)list_get(list, index);
|
||||
}
|
||||
|
||||
int list_get_size(List *list)
|
||||
{
|
||||
if (list == NULL)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user