void cmp_appr_anim_sprite_common_free(struct CmpApprAnimSpriteCommon* common) { res_dispose_frame_sheet(common->frames, common->frames_count); free_or_die(common->frame_indices); free_or_die(common->frame_times); free_or_die(common); }
struct result selection_add_item(struct selection *selection, char const *description, selection_action_fn *action) { if (!selection) return result_set_system_error(EINVAL); if (!description) return result_set_system_error(EINVAL); if (!description[0]) return result_set_system_error(EINVAL); int index = selection->items_count; int null_index = index + 1; ++selection->items_count; selection->items = reallocarray_or_die(selection->items, selection->items_count + 1, sizeof(ITEM *)); char name[] = "1"; name[0] += index; char *name_dup = strdup_or_die(name); char *description_dup = strdup_or_die(description); selection->items[index] = new_item(name_dup, description_dup); if (!selection->items[index]) { free_or_die(description_dup); free_or_die(name_dup); return result_system_error(); } struct selection_item *selection_item = calloc_or_die(1, sizeof(struct selection_item)); selection_item->action = action; set_item_userptr(selection->items[index], selection_item); selection->items[null_index] = NULL; return result_success(); }
static void generate_treasure_type(struct rnd *rnd, FILE *out, char letter) { struct treasure treasure; int individual_count; fprintf(out, "Treasure type %c: ", letter); if (letter >= 'J' && letter <= 'N') { individual_count = roll("1d10", rnd); } else { individual_count = 0; } treasure_initialize(&treasure); treasure_type_generate(treasure_type_by_letter(letter), rnd, individual_count, &treasure); char *description = treasure_alloc_description(&treasure); int value_cp = treasure_value_in_cp(&treasure); char *value_gp = coins_alloc_gp_cp_description(value_cp); fprintf(out, "%s (total %s)\n", description, value_gp); free_or_die(value_gp); free_or_die(description); enumerate_treasure_items(&treasure, out); treasure_finalize(&treasure); }
static void treasure_type_by_letter_A_test(void) { struct treasure_type *treasure_type = treasure_type_by_letter('A'); assert(NULL != treasure_type); char *name = treasure_type_alloc_name(treasure_type); assert(str_eq("A", name)); free_or_die(name); char *description = treasure_type_alloc_description(treasure_type, false); char const *expected = " A | 1-6:25% | 1-6:30% | 1-6:35% | 1-10:40% | 1-4:25% | 4-40:60% | 3-30:50% | any 3: 30%\n"; assert(str_eq(expected, description)); free_or_die(description); description = treasure_type_alloc_description(treasure_type, true); expected = " | 1,000's | 1,000's | 1,000's | 1,000's | 100's | | | Maps \n" "Treasure | of | of | of | of | of | | | or \n" " Type | Copper | Silver | Electrum | Gold | Platinum | Gems | Jewelry | Magic \n" "----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------\n" " A | 1-6:25% | 1-6:30% | 1-6:35% | 1-10:40% | 1-4:25% | 4-40:60% | 3-30:50% | any 3: 30%\n"; assert(str_eq(expected, description)); free_or_die(description); }
void text_rectangle_free(struct text_rectangle *text_rectangle) { if (text_rectangle) { free_or_die(text_rectangle->chars); free_or_die(text_rectangle); } }
void options_free(struct options *options) { if (options) { rnd_free(options->rnd); free_or_die(options->command_name); free_or_die(options); } }
void app_free(struct app *app) { if (!app) return; for (int i = 0; i < app->views_count; ++i) { view_free(app->views[i]); } free_or_die(app->views); free_or_die(app); }
static void str_alloc_centered_and_formatted_test(void) { char *str = str_alloc_centered_and_formatted(5, "foobar"); assert(0 == strcmp("foobar", str)); free_or_die(str); str = str_alloc_centered_and_formatted(6, "foobar"); assert(0 == strcmp("foobar", str)); free_or_die(str); str = str_alloc_centered_and_formatted(7, "foobar"); assert(0 == strcmp(" foobar", str)); free_or_die(str); str = str_alloc_centered_and_formatted(8, "foobar"); assert(0 == strcmp(" foobar ", str)); free_or_die(str); str = str_alloc_centered_and_formatted(9, "foobar"); assert(0 == strcmp(" foobar ", str)); free_or_die(str); str = str_alloc_centered_and_formatted(10, "foobar"); assert(0 == strcmp(" foobar ", str)); free_or_die(str); str = str_alloc_centered_and_formatted(4, "%s", ""); assert(0 == strcmp(" ", str)); free_or_die(str); str = str_alloc_centered_and_formatted(6, "%.2f", M_PI); assert(0 == strcmp(" 3.14 ", str)); free_or_die(str); }
static void str_realloc_format_test(void) { char *str = NULL; str_realloc_append_formatted(&str, "foo"); assert(0 == strcmp("foo", str)); str_realloc_append_formatted(&str, "%s", "bar"); assert(0 == strcmp("foobar", str)); str_realloc_append_formatted(&str, "%c%c%c", 'b', 'a', 'z'); assert(0 == strcmp("foobarbaz", str)); str_realloc_append_formatted(&str, " %i", 42); assert(0 == strcmp("foobarbaz 42", str)); str_realloc_append_formatted(&str, " x %.2f", M_PI); assert(0 == strcmp("foobarbaz 42 x 3.14", str)); str_realloc_append_formatted(&str, " x %i%%", 42); assert(0 == strcmp("foobarbaz 42 x 3.14 x 42%", str)); free_or_die(str); }
static void dice_alloc_description_test(void) { char *description = dice_alloc_description(dice_make(0, 6)); assert(0 == strcmp("0", description)); free_or_die(description); description = dice_alloc_description(dice_make(5, 1)); assert(0 == strcmp("5", description)); free_or_die(description); description = dice_alloc_description(dice_make(3, 6)); assert(0 == strcmp("3d6", description)); free_or_die(description); description = dice_alloc_description(dice_make_plus(2, 8, 1)); assert(0 == strcmp("2d8+1", description)); free_or_die(description); description = dice_alloc_description(dice_make_plus(2, 4, -1)); assert(0 == strcmp("2d4-1", description)); free_or_die(description); description = dice_alloc_description(dice_make_plus_times(1, 10, 0, 10)); assert(0 == strcmp("1d10x10", description)); free_or_die(description); description = dice_alloc_description(dice_make_plus_times(1, 4, 1, 10000)); assert(0 == strcmp("1d4+1x10000", description)); free_or_die(description); }
static void generate_treasure_type_table(FILE *out) { for (char letter = 'A'; letter <= 'Z'; ++letter) { struct treasure_type *treasureType = treasure_type_by_letter(letter); char *description = treasure_type_alloc_description(treasureType, letter == 'A'); fprintf(out, "%s", description); free_or_die(description); } }
static void dice_alloc_range_description_test(void) { char *description = dice_alloc_range_description(dice_make(0, 6)); assert(0 == strcmp("0", description)); free_or_die(description); description = dice_alloc_range_description(dice_make_plus(0, 6, 2)); assert(0 == strcmp("2", description)); free_or_die(description); description = dice_alloc_range_description(dice_make_plus_times(0, 4, 4, 10)); assert(0 == strcmp("40", description)); free_or_die(description); description = dice_alloc_range_description(dice_make(3, 1)); assert(0 == strcmp("3", description)); free_or_die(description); description = dice_alloc_range_description(dice_make(3, 6)); assert(0 == strcmp("3-18", description)); free_or_die(description); description = dice_alloc_range_description(dice_make_plus(2, 8, 1)); assert(0 == strcmp("3-17", description)); free_or_die(description); description = dice_alloc_range_description(dice_make_plus(2, 4, -1)); assert(0 == strcmp("1-7", description)); free_or_die(description); description = dice_alloc_range_description(dice_make_plus_times(1, 10, 0, 10)); assert(0 == strcmp("10-100", description)); free_or_die(description); description = dice_alloc_range_description(dice_make_plus_times(1, 4, 1, 10000)); assert(0 == strcmp("20000-50000", description)); free_or_die(description); }
void text_rectangle_print_format(struct text_rectangle *text_rectangle, char const *format, ...) { char *buffer; va_list arguments; va_start(arguments, format); int chars_printed = vasprintf_or_die(&buffer, format, arguments); va_end(arguments); int chars_available = text_rectangle->column_count - text_rectangle->caret.column_index; int chars_to_copy = chars_printed > chars_available ? chars_available : chars_printed; char *chars = text_rectangle_row_at(text_rectangle, text_rectangle->caret.row_index) + text_rectangle->caret.column_index; memcpy(chars, buffer, chars_to_copy); free_or_die(buffer); text_rectangle->caret.column_index += chars_printed; }
void selection_free_or_die(struct selection *selection) { if (!selection) return; if (selection->menu) { unpost_menu(selection->menu); free_menu(selection->menu); } if (selection->sub_window) delwin(selection->sub_window); if (selection->window) delwin(selection->window); for (int i = 0; i < selection->items_count; ++i) { free_or_die((char *)item_name(selection->items[i])); free_or_die((char *)item_description(selection->items[i])); free_or_die(item_userptr(selection->items[i])); free_item(selection->items[i]); } free_or_die(selection->items); free_or_die(selection->title); free_or_die(selection); }
void dungeon_options_free(struct dungeon_options *dungeon_options) { free_or_die(dungeon_options); }
void tile_free(struct tile *tile) { free_or_die(tile); }
void treasure_map_finalize(struct treasure_map *treasure_map) { treasure_finalize(&treasure_map->treasure); free_or_die(treasure_map->true_description); }
void view_free(struct view *view) { if (!view) return; free_or_die(view); }
static void generate_character(struct rnd *rnd, FILE *out, enum characteristic_generation_method method) { char const *method_name = characteristic_generation_method_name(method); uint32_t special_characteristics = STRENGTH; int *characteristics = alloc_characteristics(rnd, method, special_characteristics); if ( method == characteristic_generation_method_1 || method == characteristic_generation_method_2) { fprintf(out, "Character (%s): -------------------------\n", method_name); fprintf(out, "(%s)\n", characteristic_generation_method_description(method)); for (int i = 0; i < 6; ++i) { fprintf(out, " %2i) %i\n", i + 1, characteristics[i]); } } else if (method == characteristic_generation_method_4) { fprintf(out, "Possible Characters (%s): -------------------------\n", method_name); fprintf(out, "(%s)\n", characteristic_generation_method_description(method)); fprintf(out, " %2i %2i %2i %2i %2i %2i %2i %2i %2i %2i %2i %2i\n", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12); fprintf(out, " %2s %2s %2s %2s %2s %2s %2s %2s %2s %2s %2s %2s\n", "--", "--", "--", "--", "--", "--", "--", "--", "--", "--", "--", "--"); fprintf(out, " Strength: %2i %2i %2i %2i %2i %2i %2i %2i %2i %2i %2i %2i\n", characteristics[0], characteristics[0 + 6], characteristics[0 + 12], characteristics[0 + 18], characteristics[0 + 24], characteristics[0 + 30], characteristics[0 + 36], characteristics[0 + 42], characteristics[0 + 48], characteristics[0 + 54], characteristics[0 + 60], characteristics[0 + 66]); fprintf(out, " Intelligence: %2i %2i %2i %2i %2i %2i %2i %2i %2i %2i %2i %2i\n", characteristics[1], characteristics[1 + 6], characteristics[1 + 12], characteristics[1 + 18], characteristics[1 + 24], characteristics[1 + 30], characteristics[1 + 36], characteristics[1 + 42], characteristics[1 + 48], characteristics[1 + 54], characteristics[1 + 60], characteristics[1 + 66]); fprintf(out, " Wisdom: %2i %2i %2i %2i %2i %2i %2i %2i %2i %2i %2i %2i\n", characteristics[2], characteristics[2 + 6], characteristics[2 + 12], characteristics[2 + 18], characteristics[2 + 24], characteristics[2 + 30], characteristics[2 + 36], characteristics[2 + 42], characteristics[2 + 48], characteristics[2 + 54], characteristics[2 + 60], characteristics[2 + 66]); fprintf(out, " Dexterity: %2i %2i %2i %2i %2i %2i %2i %2i %2i %2i %2i %2i\n", characteristics[3], characteristics[3 + 6], characteristics[3 + 12], characteristics[3 + 18], characteristics[3 + 24], characteristics[3 + 30], characteristics[3 + 36], characteristics[3 + 42], characteristics[3 + 48], characteristics[3 + 54], characteristics[3 + 60], characteristics[3 + 66]); fprintf(out, " Constitution: %2i %2i %2i %2i %2i %2i %2i %2i %2i %2i %2i %2i\n", characteristics[4], characteristics[4 + 6], characteristics[4 + 12], characteristics[4 + 18], characteristics[4 + 24], characteristics[4 + 30], characteristics[4 + 36], characteristics[4 + 42], characteristics[4 + 48], characteristics[4 + 54], characteristics[4 + 60], characteristics[4 + 66]); fprintf(out, " Charisma: %2i %2i %2i %2i %2i %2i %2i %2i %2i %2i %2i %2i\n", characteristics[5], characteristics[5 + 6], characteristics[5 + 12], characteristics[5 + 18], characteristics[5 + 24], characteristics[5 + 30], characteristics[5 + 36], characteristics[5 + 42], characteristics[5 + 48], characteristics[5 + 54], characteristics[5 + 60], characteristics[5 + 66]); } else { fprintf(out, "Character (%s): -------------------------\n", method_name); fprintf(out, "(%s)\n", characteristic_generation_method_description(method)); fprintf(out, " Strength: %2i\n", characteristics[0]); fprintf(out, " Intelligence: %2i\n", characteristics[1]); fprintf(out, " Wisdom: %2i\n", characteristics[2]); fprintf(out, " Dexterity: %2i\n", characteristics[3]); fprintf(out, " Constitution: %2i\n", characteristics[4]); fprintf(out, " Charisma: %2i\n", characteristics[5]); } free_or_die(characteristics); }
void treasure_map_generate(struct treasure_map *treasure_map, struct rnd *rnd) { enum treasure_map_type treasure_map_type; int score = roll("1d100", rnd); if (score <= 5) { treasure_map_type = treasure_map_type_false_map; treasure_map->is_false = true; /* TODO: generate description of false treasure */ } else if (score <= 70) { treasure_map_type = treasure_map_type_map_to_monetary_treasure; generate_monetary_treasure(&treasure_map->treasure, rnd); } else if (score <= 90) { treasure_map_type = treasure_map_type_map_to_magic_treasure; generate_magic_treasure(&treasure_map->treasure, rnd); } else { treasure_map_type = treasure_map_type_map_to_combined_hoard; generate_combined_hoard(&treasure_map->treasure, rnd); } int miles; score = roll("1d100", rnd); if (score <= 20) { miles = 0; } else if (score <= 60) { miles = roll("1d4", rnd) + 4; } else if (score <= 90) { miles = roll("1d4", rnd) * 10; } else { miles = roll("1d10", rnd) * 50; } enum direction direction = direction_random(rnd); if (miles) { char const *disposition; score = roll("1d100", rnd); if (score <= 10) { disposition = "buried and unguarded"; } else if (score <= 20) { disposition = "hidden in water"; } else if (score <= 70) { disposition = "guarded in a lair"; } else if (score <= 80) { disposition = "somewhere in a ruins"; } else if (score <= 90) { disposition = "in a burial crypt"; } else { disposition = "secreted in a town"; } char *description = treasure_alloc_description(&treasure_map->treasure); treasure_map->true_description = str_alloc_formatted("%smap to %s of %s %i miles to the %s, %s", (treasure_map->is_false ? "false " : ""), treasure_map_types[treasure_map_type], description, miles, direction_name(direction), disposition); free_or_die(description); } else { char *description = treasure_alloc_description(&treasure_map->treasure); treasure_map->true_description = str_alloc_formatted("%smap to %s of %s in nearby labyrinth to the %s", (treasure_map->is_false ? "false " : ""), treasure_map_types[treasure_map_type], description, direction_name(direction)); free_or_die(description); } }
void area_free(struct area *area) { free_or_die(area); }