diff options
-rw-r--r-- | src/CMakeLists.txt | 44 | ||||
-rw-r--r-- | src/birth.cc (renamed from src/birth.c) | 73 | ||||
-rw-r--r-- | src/bldg.cc (renamed from src/bldg.c) | 2 | ||||
-rw-r--r-- | src/cave.cc (renamed from src/cave.c) | 130 | ||||
-rw-r--r-- | src/cmd1.cc (renamed from src/cmd1.c) | 0 | ||||
-rw-r--r-- | src/cmd2.cc (renamed from src/cmd2.c) | 0 | ||||
-rw-r--r-- | src/cmd3.cc (renamed from src/cmd3.c) | 230 | ||||
-rw-r--r-- | src/cmd4.cc (renamed from src/cmd4.c) | 147 | ||||
-rw-r--r-- | src/cmd5.cc (renamed from src/cmd5.c) | 0 | ||||
-rw-r--r-- | src/cmd6.cc (renamed from src/cmd6.c) | 0 | ||||
-rw-r--r-- | src/cmd7.cc (renamed from src/cmd7.c) | 0 | ||||
-rw-r--r-- | src/cmovie.cc (renamed from src/cmovie.c) | 6 | ||||
-rw-r--r-- | src/corrupt.cc (renamed from src/corrupt.c) | 0 | ||||
-rw-r--r-- | src/device_allocation.cc (renamed from src/device_allocation.c) | 2 | ||||
-rw-r--r-- | src/dice.cc (renamed from src/dice.c) | 0 | ||||
-rw-r--r-- | src/dungeon.cc (renamed from src/dungeon.c) | 0 | ||||
-rw-r--r-- | src/externs.h | 7 | ||||
-rw-r--r-- | src/files.cc (renamed from src/files.c) | 109 | ||||
-rw-r--r-- | src/gen_evol.cc (renamed from src/gen_evol.c) | 0 | ||||
-rw-r--r-- | src/gen_maze.cc (renamed from src/gen_maze.c) | 10 | ||||
-rw-r--r-- | src/generate.cc (renamed from src/generate.c) | 47 | ||||
-rw-r--r-- | src/gods.cc (renamed from src/gods.c) | 0 | ||||
-rw-r--r-- | src/help.cc (renamed from src/help.c) | 0 | ||||
-rw-r--r-- | src/hiscore.cc (renamed from src/hiscore.c) | 0 | ||||
-rw-r--r-- | src/hooks.cc (renamed from src/hooks.c) | 7 | ||||
-rw-r--r-- | src/include/tome/make_array.hpp | 13 | ||||
-rw-r--r-- | src/init1.cc (renamed from src/init1.c) | 0 | ||||
-rw-r--r-- | src/init2.cc (renamed from src/init2.c) | 433 | ||||
-rw-r--r-- | src/joke.cc (renamed from src/joke.c) | 0 | ||||
-rw-r--r-- | src/levels.cc (renamed from src/levels.c) | 0 | ||||
-rw-r--r-- | src/loadsave.cc (renamed from src/loadsave.c) | 97 | ||||
-rw-r--r-- | src/lua_bind.cc (renamed from src/lua_bind.c) | 0 | ||||
-rw-r--r-- | src/melee1.cc (renamed from src/melee1.c) | 0 | ||||
-rw-r--r-- | src/melee2.cc (renamed from src/melee2.c) | 0 | ||||
-rw-r--r-- | src/messages.cc (renamed from src/messages.c) | 9 | ||||
-rw-r--r-- | src/mimic.cc (renamed from src/mimic.c) | 0 | ||||
-rw-r--r-- | src/modules.cc (renamed from src/modules.c) | 0 | ||||
-rw-r--r-- | src/monster1.cc (renamed from src/monster1.c) | 18 | ||||
-rw-r--r-- | src/monster2.cc (renamed from src/monster2.c) | 6 | ||||
-rw-r--r-- | src/monster3.cc (renamed from src/monster3.c) | 0 | ||||
-rw-r--r-- | src/notes.cc (renamed from src/notes.c) | 0 | ||||
-rw-r--r-- | src/object1.cc (renamed from src/object1.c) | 0 | ||||
-rw-r--r-- | src/object2.cc (renamed from src/object2.c) | 22 | ||||
-rw-r--r-- | src/powers.cc (renamed from src/powers.c) | 0 | ||||
-rw-r--r-- | src/q_betwen.cc (renamed from src/q_betwen.c) | 0 | ||||
-rw-r--r-- | src/q_bounty.cc (renamed from src/q_bounty.c) | 0 | ||||
-rw-r--r-- | src/q_dragons.cc (renamed from src/q_dragons.c) | 0 | ||||
-rw-r--r-- | src/q_eol.cc (renamed from src/q_eol.c) | 0 | ||||
-rw-r--r-- | src/q_evil.cc (renamed from src/q_evil.c) | 0 | ||||
-rw-r--r-- | src/q_fireprof.cc (renamed from src/q_fireprof.c) | 0 | ||||
-rw-r--r-- | src/q_god.cc (renamed from src/q_god.c) | 0 | ||||
-rw-r--r-- | src/q_haunted.cc (renamed from src/q_haunted.c) | 0 | ||||
-rw-r--r-- | src/q_hobbit.cc (renamed from src/q_hobbit.c) | 0 | ||||
-rw-r--r-- | src/q_invas.cc (renamed from src/q_invas.c) | 0 | ||||
-rw-r--r-- | src/q_library.cc (renamed from src/q_library.c) | 0 | ||||
-rw-r--r-- | src/q_main.cc (renamed from src/q_main.c) | 0 | ||||
-rw-r--r-- | src/q_narsil.cc (renamed from src/q_narsil.c) | 0 | ||||
-rw-r--r-- | src/q_nazgul.cc (renamed from src/q_nazgul.c) | 0 | ||||
-rw-r--r-- | src/q_nirna.cc (renamed from src/q_nirna.c) | 0 | ||||
-rw-r--r-- | src/q_one.cc (renamed from src/q_one.c) | 0 | ||||
-rw-r--r-- | src/q_poison.cc (renamed from src/q_poison.c) | 0 | ||||
-rw-r--r-- | src/q_rand.cc (renamed from src/q_rand.c) | 28 | ||||
-rw-r--r-- | src/q_shroom.cc (renamed from src/q_shroom.c) | 0 | ||||
-rw-r--r-- | src/q_spider.cc (renamed from src/q_spider.c) | 0 | ||||
-rw-r--r-- | src/q_thief.cc (renamed from src/q_thief.c) | 0 | ||||
-rw-r--r-- | src/q_thrain.cc (renamed from src/q_thrain.c) | 0 | ||||
-rw-r--r-- | src/q_troll.cc (renamed from src/q_troll.c) | 0 | ||||
-rw-r--r-- | src/q_ultrae.cc (renamed from src/q_ultrae.c) | 0 | ||||
-rw-r--r-- | src/q_ultrag.cc (renamed from src/q_ultrag.c) | 0 | ||||
-rw-r--r-- | src/q_wight.cc (renamed from src/q_wight.c) | 0 | ||||
-rw-r--r-- | src/q_wolves.cc (renamed from src/q_wolves.c) | 0 | ||||
-rw-r--r-- | src/quark.cc (renamed from src/quark.c) | 6 | ||||
-rw-r--r-- | src/randart.cc (renamed from src/randart.c) | 23 | ||||
-rw-r--r-- | src/range.cc (renamed from src/range.c) | 0 | ||||
-rw-r--r-- | src/script.cc (renamed from src/script.c) | 0 | ||||
-rw-r--r-- | src/skills.cc (renamed from src/skills.c) | 314 | ||||
-rw-r--r-- | src/spell_type.cc (renamed from src/spell_type.c) | 9 | ||||
-rw-r--r-- | src/spell_type.h | 4 | ||||
-rw-r--r-- | src/spells1.cc (renamed from src/spells1.c) | 0 | ||||
-rw-r--r-- | src/spells2.cc (renamed from src/spells2.c) | 31 | ||||
-rw-r--r-- | src/spells3.cc (renamed from src/spells3.c) | 22 | ||||
-rw-r--r-- | src/spells4.cc (renamed from src/spells4.c) | 2 | ||||
-rw-r--r-- | src/spells5.cc (renamed from src/spells5.c) | 0 | ||||
-rw-r--r-- | src/spells6.cc (renamed from src/spells6.c) | 33 | ||||
-rw-r--r-- | src/status.cc (renamed from src/status.c) | 0 | ||||
-rw-r--r-- | src/store.cc (renamed from src/store.c) | 4 | ||||
-rw-r--r-- | src/string_list.cc (renamed from src/string_list.c) | 2 | ||||
-rw-r--r-- | src/tables.cc (renamed from src/tables.c) | 0 | ||||
-rw-r--r-- | src/traps.cc (renamed from src/traps.c) | 0 | ||||
-rw-r--r-- | src/types.h | 16 | ||||
-rw-r--r-- | src/util.cc (renamed from src/util.c) | 26 | ||||
-rw-r--r-- | src/util.hpp | 8 | ||||
-rw-r--r-- | src/variable.cc (renamed from src/variable.c) | 4 | ||||
-rw-r--r-- | src/wild.cc (renamed from src/wild.c) | 26 | ||||
-rw-r--r-- | src/wizard1.cc (renamed from src/wizard1.c) | 16 | ||||
-rw-r--r-- | src/wizard2.cc (renamed from src/wizard2.c) | 0 | ||||
-rw-r--r-- | src/xtra1.cc (renamed from src/xtra1.c) | 0 | ||||
-rw-r--r-- | src/xtra2.cc (renamed from src/xtra2.c) | 0 | ||||
-rw-r--r-- | src/z-virt.h | 4 |
99 files changed, 792 insertions, 1198 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ba0fd9d2..221dfa2b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,29 +8,29 @@ ADD_SUBDIRECTORY (squelch) SET(SRCS main-gcu.c main-x11.c main-xaw.c main-sdl.c main-gtk2.c z-rand.c z-util.c z-form.c z-virt.c z-term.c - variable.c tables.c hooks.c util.c cave.c dungeon.c - melee1.c melee2.c messages.c modules.c - q_god.c q_library.c q_fireprof.c q_bounty.c q_thrain.c - q_narsil.c q_evil.c q_betwen.c q_haunted.c q_invas.c - q_nirna.c q_eol.c q_god.c q_dragons.c q_poison.c - q_spider.c q_wolves.c q_shroom.c q_nazgul.c q_wight.c - q_troll.c q_hobbit.c q_thief.c q_ultrae.c q_ultrag.c - q_one.c q_main.c q_rand.c - object1.c object2.c randart.c squeltch.cc traps.c - monster1.c monster2.c monster3.c - xtra1.c xtra2.c skills.c powers.c gods.c - spells1.c spells2.c spells3.c spells4.c spells5.c spells6.c - spell_type.c device_allocation.c - corrupt.c joke.c mimic.c - status.c files.c notes.c loadsave.c string_list.c - cmd1.c cmd2.c cmd3.c cmd4.c cmd5.c cmd6.c cmd7.c - help.c hiscore.c range.c dice.c - generate.c gen_maze.c gen_evol.c wild.c levels.c store.c bldg.c - cmovie.c - wizard2.c init2.c birth.c wizard1.c init1.c main.c - quark.c + variable.cc tables.cc hooks.cc util.cc cave.cc dungeon.cc + melee1.cc melee2.cc messages.cc modules.cc + q_god.cc q_library.cc q_fireprof.cc q_bounty.cc q_thrain.cc + q_narsil.cc q_evil.cc q_betwen.cc q_haunted.cc q_invas.cc + q_nirna.cc q_eol.cc q_god.cc q_dragons.cc q_poison.cc + q_spider.cc q_wolves.cc q_shroom.cc q_nazgul.cc q_wight.cc + q_troll.cc q_hobbit.cc q_thief.cc q_ultrae.cc q_ultrag.cc + q_one.cc q_main.cc q_rand.cc + object1.cc object2.cc randart.cc squeltch.cc traps.cc + monster1.cc monster2.cc monster3.cc + xtra1.cc xtra2.cc skills.cc powers.cc gods.cc + spells1.cc spells2.cc spells3.cc spells4.cc spells5.cc spells6.cc + spell_type.cc device_allocation.cc + corrupt.cc joke.cc mimic.cc + status.cc files.cc notes.cc loadsave.cc string_list.cc + cmd1.cc cmd2.cc cmd3.cc cmd4.cc cmd5.cc cmd6.cc cmd7.cc + help.cc hiscore.cc range.cc dice.cc + generate.cc gen_maze.cc gen_evol.cc wild.cc levels.cc store.cc bldg.cc + cmovie.cc + wizard1.cc wizard2.cc init1.cc init2.cc birth.cc main.c + quark.cc # Lua bits: - lua_bind.c script.c + lua_bind.cc script.cc ) # Need a few additional source files for Windows. diff --git a/src/birth.c b/src/birth.cc index 60a6b3ee..8b6ef896 100644 --- a/src/birth.c +++ b/src/birth.cc @@ -11,10 +11,13 @@ */ #include "angband.h" + #include "messages.h" #include "hooks.h" #include "q_rand.h" +#include <string> + /* * How often the autoroller will update the display and pause * to check for user interuptions. @@ -805,7 +808,7 @@ static void player_wipe(void) wipe_saved(); /* Hack -- zero the struct */ - p_ptr = WIPE(p_ptr, player_type); + WIPE(p_ptr, player_type); /* Not dead yet */ p_ptr->lives = 0; @@ -1446,12 +1449,9 @@ int dump_classes(s16b *classes, int sel, u32b *restrictions) int n = 0; char buf[80]; - char *desc; cptr str; - C_MAKE(desc, c_head->text_size, char); - /* Clean up */ clear_from(12); @@ -1478,9 +1478,15 @@ int dump_classes(s16b *classes, int sel, u32b *restrictions) /* Print some more info */ if (sel == n) { - strnfmt(desc, c_head->text_size, "%s%s", cp_ptr->desc + c_text, - cp_ptr->flags1 & PR1_EXPERIMENTAL ? "\nEXPERIMENTAL" : ""); - print_desc(desc); + std::string desc; + + desc += (cp_ptr->desc + c_text); + if (cp_ptr->flags1 & PR1_EXPERIMENTAL) + { + desc += "\nEXPERIMENTAL"; + } + + print_desc(desc.c_str()); if (!(restrictions[classes[n] / 32] & BIT(classes[n])) || cp_ptr->flags1 & PR1_EXPERIMENTAL) @@ -1499,8 +1505,6 @@ int dump_classes(s16b *classes, int sel, u32b *restrictions) n++; } - C_FREE(desc, c_head->text_size, char); - return (n); } @@ -1509,12 +1513,9 @@ int dump_specs(int sel) int n = 0; char buf[80]; - char *desc; cptr str; - C_MAKE(desc, c_head->text_size, char); - /* Clean up */ clear_from(12); @@ -1542,9 +1543,15 @@ int dump_specs(int sel) /* Print some more info */ if (sel == n) { - strnfmt(desc, c_head->text_size, "%s%s", spp_ptr->desc + c_text, - spp_ptr->flags1 & PR1_EXPERIMENTAL ? "\nEXPERIMENTAL" : ""); - print_desc(desc); + std::string desc; + + desc += (spp_ptr->desc + c_text); + if (spp_ptr->flags1 & PR1_EXPERIMENTAL) + { + desc += "\nEXPERIMENTAL"; + } + + print_desc(desc.c_str()); if (spp_ptr->flags1 & PR1_EXPERIMENTAL) c_put_str(TERM_BLUE, buf, 18 + (n / 4), 1 + 20 * (n % 4)); @@ -1560,8 +1567,6 @@ int dump_specs(int sel) } } - C_FREE(desc, c_head->text_size, char); - return (n); } @@ -1570,12 +1575,9 @@ int dump_races(int sel) int n = 0; char buf[80]; - char *desc; cptr str; - C_MAKE(desc, rp_head->text_size, char); - /* Clean up */ clear_from(12); @@ -1600,9 +1602,15 @@ int dump_races(int sel) /* Print some more info */ if (sel == n) { - strnfmt(desc, rp_head->text_size, "%s%s", rp_ptr->desc + rp_text, - rp_ptr->flags1 & PR1_EXPERIMENTAL ? "\nEXPERIMENTAL" : ""); - print_desc(desc); + std::string desc; + + desc += (rp_ptr->desc + rp_text); + if (rp_ptr->flags1 & PR1_EXPERIMENTAL) + { + desc += "\nEXPERIMENTAL"; + } + + print_desc(desc.c_str()); if (rp_ptr->flags1 & PR1_EXPERIMENTAL) c_put_str(TERM_BLUE, buf, 18 + (n / 5), 1 + 15 * (n % 5)); @@ -1618,8 +1626,6 @@ int dump_races(int sel) } } - C_FREE(desc, rp_head->text_size, char); - return (n); } @@ -1629,12 +1635,9 @@ int dump_rmods(int sel, int *racem, int max) int n = 0; char buf[80]; - char *desc; cptr str; - C_MAKE(desc, rmp_head->text_size, char); - /* Clean up */ clear_from(12); @@ -1663,9 +1666,15 @@ int dump_rmods(int sel, int *racem, int max) /* Print some more info */ if (sel == n) { - strnfmt(desc, rmp_head->text_size, "%s%s", rmp_ptr->desc + rmp_text, - rmp_ptr->flags1 & PR1_EXPERIMENTAL ? "\nEXPERIMENTAL" : ""); - print_desc(desc); + std::string desc; + + desc += (rmp_ptr->desc + rmp_text); + if (rmp_ptr->flags1 & PR1_EXPERIMENTAL) + { + desc += "\nEXPERIMENTAL"; + } + + print_desc(desc.c_str()); if (rmp_ptr->flags1 & PR1_EXPERIMENTAL) c_put_str(TERM_BLUE, buf, 18 + (n / 5), 1 + 15 * (n % 5)); @@ -1681,8 +1690,6 @@ int dump_rmods(int sel, int *racem, int max) } } - C_FREE(desc, rmp_head->text_size, char); - return (n); } diff --git a/src/bldg.c b/src/bldg.cc index 468e3c1d..2a87f035 100644 --- a/src/bldg.c +++ b/src/bldg.cc @@ -900,7 +900,7 @@ static void town_history(void) /* * compare_weapon_aux2 -KMW- */ -static void compare_weapon_aux2(object_type *o_ptr, int numblows, int r, int c, int mult, char attr[80], u32b f1, u32b f2, u32b f3, byte color) +static void compare_weapon_aux2(object_type *o_ptr, int numblows, int r, int c, int mult, const char *attr, u32b f1, u32b f2, u32b f3, byte color) { char tmp_str[80]; diff --git a/src/cave.c b/src/cave.cc index 2a02f017..a0b8c7fc 100644 --- a/src/cave.c +++ b/src/cave.cc @@ -6,6 +6,9 @@ #include "angband.h" #include "q_rand.h" +#include <vector> +#include <iterator> +#include <algorithm> /* * Support for Adam Bolt's tileset, lighting and transparency effects @@ -2383,11 +2386,6 @@ void display_map(int *cy, int *cx) byte tp; - byte **ma; - char **mc; - - byte **mp; - bool_ old_view_special_lite; bool_ old_view_granite_lite; @@ -2426,32 +2424,22 @@ void display_map(int *cy, int *cx) view_granite_lite = FALSE; - /* Allocate temporary memory for the maps */ - C_MAKE(ma, hgt + 2, byte *); - C_MAKE(mc, hgt + 2, char *); - C_MAKE(mp, hgt + 2, byte *); - - /* Allocate each line in the maps */ + /* Set up initial maps */ + std::vector<std::vector<byte>> ma; + std::vector<std::vector<char>> mc; + std::vector<std::vector<byte>> mp; for (i = 0; i < hgt + 2; i++) { - C_MAKE(ma[i], wid + 2, byte); - C_MAKE(mc[i], wid + 2, char); - C_MAKE(mp[i], wid + 2, byte); - } + // Nothing there. + ma.push_back(std::vector<byte>(wid + 2, TERM_WHITE)); + mc.push_back(std::vector<char>(wid + 2, ' ')); - /* Clear the chars and attributes */ - for (y = 0; y < hgt + 2; ++y) - { - for (x = 0; x < wid + 2; ++x) - { - /* Nothing here */ - ma[y][x] = TERM_WHITE; - mc[y][x] = ' '; - - /* No priority */ - mp[y][x] = 0; - } + // No priority. + mp.push_back(std::vector<byte>(wid + 2, 0)); } + assert(ma.size() == hgt + 2); + assert(mc.size() == hgt + 2); + assert(mp.size() == hgt + 2); /* Calculate scaling factors */ yfactor = ((cur_hgt / hgt < 4) && (cur_hgt > hgt)) ? 10 : 1; @@ -2557,20 +2545,6 @@ void display_map(int *cy, int *cx) *cx = (p_ptr->px * xfactor / xrat + 1) * 2 - 1 + COL_MAP; } - /* Free each line in the maps */ - for (i = 0; i < hgt + 2; i++) - { - C_FREE(ma[i], wid + 2, byte); - C_FREE(mc[i], wid + 2, char); - C_FREE(mp[i], wid + 2, byte); - } - - /* Allocate temporary memory for the maps */ - C_FREE(ma, hgt + 2, byte *); - C_FREE(mc, hgt + 2, char *); - C_FREE(mp, hgt + 2, byte *); - - /* Restore lighting effects */ view_special_lite = old_view_special_lite; view_granite_lite = old_view_granite_lite; @@ -3194,38 +3168,6 @@ struct vinfo_hack /* - * Sorting hook -- comp function -- array of long's (see below) - * - * We use "u" to point to an array of long integers. - */ -static bool_ ang_sort_comp_hook_longs(vptr u, vptr v, int a, int b) -{ - long *x = (long*)(u); - - return (x[a] <= x[b]); -} - - -/* - * Sorting hook -- comp function -- array of long's (see below) - * - * We use "u" to point to an array of long integers. - */ -static void ang_sort_swap_hook_longs(vptr u, vptr v, int a, int b) -{ - long *x = (long*)(u); - - long temp; - - /* Swap */ - temp = x[a]; - x[a] = x[b]; - x[b] = temp; -} - - - -/* * Save a slope */ static void vinfo_init_aux(vinfo_hack *hack, int y, int x, long m) @@ -3283,8 +3225,6 @@ errr vinfo_init(void) long m; - vinfo_hack *hack; - int num_grids = 0; int queue_head = 0; @@ -3293,8 +3233,8 @@ errr vinfo_init(void) /* Make hack */ - MAKE(hack, vinfo_hack); - + vinfo_hack hack; + WIPE(&hack, vinfo_hack); /* Analyze grids */ for (y = 0; y <= MAX_SIGHT; ++y) @@ -3305,8 +3245,8 @@ errr vinfo_init(void) if (distance(0, 0, y, x) > MAX_SIGHT) continue; /* Default slope range */ - hack->slopes_min[y][x] = 999999999; - hack->slopes_max[y][x] = 0; + hack.slopes_min[y][x] = 999999999; + hack.slopes_max[y][x] = 0; /* Paranoia */ if (num_grids >= VINFO_MAX_GRIDS) @@ -3322,25 +3262,25 @@ errr vinfo_init(void) m = SCALE * (1000L * y - 500) / (1000L * x + 500); /* Handle "legal" slopes */ - vinfo_init_aux(hack, y, x, m); + vinfo_init_aux(&hack, y, x, m); /* Slope to top left corner */ m = SCALE * (1000L * y - 500) / (1000L * x - 500); /* Handle "legal" slopes */ - vinfo_init_aux(hack, y, x, m); + vinfo_init_aux(&hack, y, x, m); /* Slope to bottom right corner */ m = SCALE * (1000L * y + 500) / (1000L * x + 500); /* Handle "legal" slopes */ - vinfo_init_aux(hack, y, x, m); + vinfo_init_aux(&hack, y, x, m); /* Slope to bottom left corner */ m = SCALE * (1000L * y + 500) / (1000L * x - 500); /* Handle "legal" slopes */ - vinfo_init_aux(hack, y, x, m); + vinfo_init_aux(&hack, y, x, m); } } @@ -3353,21 +3293,15 @@ errr vinfo_init(void) } /* Enforce maximal efficiency */ - if (hack->num_slopes < VINFO_MAX_SLOPES) + if (hack.num_slopes < VINFO_MAX_SLOPES) { quit_fmt("Too few slopes (%d < %d)!", - hack->num_slopes, VINFO_MAX_SLOPES); + hack.num_slopes, VINFO_MAX_SLOPES); } /* Sort slopes numerically */ - ang_sort_comp = ang_sort_comp_hook_longs; - - /* Sort slopes numerically */ - ang_sort_swap = ang_sort_swap_hook_longs; - - /* Sort the (unique) slopes */ - ang_sort(hack->slopes, NULL, hack->num_slopes); + std::sort(std::begin(hack.slopes), std::end(hack.slopes)); @@ -3410,14 +3344,14 @@ errr vinfo_init(void) /* Analyze slopes */ - for (i = 0; i < hack->num_slopes; ++i) + for (i = 0; i < hack.num_slopes; ++i) { - m = hack->slopes[i]; + m = hack.slopes[i]; /* Memorize intersection slopes (for non-player-grids) */ if ((e > 0) && - (hack->slopes_min[y][x] < m) && - (m < hack->slopes_max[y][x])) + (hack.slopes_min[y][x] < m) && + (m < hack.slopes_max[y][x])) { switch (i / 32) { @@ -3498,10 +3432,6 @@ errr vinfo_init(void) } - /* Kill hack */ - KILL(hack, vinfo_hack); - - /* Success */ return (0); } diff --git a/src/cmd1.c b/src/cmd1.cc index ff493174..ff493174 100644 --- a/src/cmd1.c +++ b/src/cmd1.cc diff --git a/src/cmd2.c b/src/cmd2.cc index 67f25098..67f25098 100644 --- a/src/cmd2.c +++ b/src/cmd2.cc diff --git a/src/cmd3.c b/src/cmd3.cc index 9f4170bc..6b618cb8 100644 --- a/src/cmd3.c +++ b/src/cmd3.cc @@ -15,6 +15,10 @@ #include "quark.h" #include "hooks.h" +#include <algorithm> +#include <memory> +#include <utility> + /* * Display p_ptr->inventory */ @@ -1271,107 +1275,75 @@ static cptr ident_info[] = }; - -/* - * Sorting hook -- Comp function -- see below - * - * We use "u" to point to array of monster indexes, - * and "v" to select the type of sorting to perform on "u". +/** + * Sort by monster experience. */ -static bool_ ang_sort_comp_hook(vptr u, vptr v, int a, int b) +static bool compare_monster_experience(int w1, int w2) { - u16b *who = (u16b*)(u); - - u16b *why = (u16b*)(v); - - int w1 = who[a]; - - int w2 = who[b]; - - int z1, z2; + /* Extract experience */ + s32b z1 = r_info[w1].mexp; + s32b z2 = r_info[w2].mexp; + /* Compare experience */ + if (z1 < z2) return true; + if (z1 > z2) return false; - /* Sort by player kills */ - if (*why >= 4) - { - /* Extract player kills */ - z1 = r_info[w1].r_pkills; - z2 = r_info[w2].r_pkills; - - /* Compare player kills */ - if (z1 < z2) return (TRUE); - if (z1 > z2) return (FALSE); - } - - - /* Sort by total kills */ - if (*why >= 3) - { - /* Extract total kills */ - z1 = r_info[w1].r_tkills; - z2 = r_info[w2].r_tkills; - - /* Compare total kills */ - if (z1 < z2) return (TRUE); - if (z1 > z2) return (FALSE); - } - - - /* Sort by monster level */ - if (*why >= 2) - { - /* Extract levels */ - z1 = r_info[w1].level; - z2 = r_info[w2].level; - - /* Compare levels */ - if (z1 < z2) return (TRUE); - if (z1 > z2) return (FALSE); - } - + /* Punt to index */ + return w1 < w2; +} - /* Sort by monster experience */ - if (*why >= 1) - { - /* Extract experience */ - z1 = r_info[w1].mexp; - z2 = r_info[w2].mexp; +/** + * Sort by monster level. + */ +static bool compare_monster_level(int w1, int w2) +{ + /* Extract levels */ + byte z1 = r_info[w1].level; + byte z2 = r_info[w2].level; + + /* Compare levels */ + if (z1 < z2) return true; + if (z1 > z2) return false; + + /* Punt to monster experience. */ + return compare_monster_experience(w1, w2); +} - /* Compare experience */ - if (z1 < z2) return (TRUE); - if (z1 > z2) return (FALSE); - } +/** + * Sort by total number of kills + */ +static bool compare_total_kills(int w1, int w2) +{ + /* Extract total kills */ + s16b z1 = r_info[w1].r_tkills; + s16b z2 = r_info[w2].r_tkills; + /* Compare total kills */ + if (z1 < z2) return true; + if (z1 > z2) return false; - /* Compare indexes */ - return (w1 <= w2); + /* Punt to monster level. */ + return compare_monster_level(w1, w2); } - /* - * Sorting hook -- Swap function -- see below - * - * We use "u" to point to array of monster indexes, - * and "v" to select the type of sorting to perform. + * Sort by player kills */ -static void ang_sort_swap_hook(vptr u, vptr v, int a, int b) +static bool compare_player_kills(int w1, int w2) { - u16b *who = (u16b*)(u); - - u16b holder; + /* Extract player kills */ + s16b z1 = r_info[w1].r_pkills; + s16b z2 = r_info[w2].r_pkills; + /* Compare player kills */ + if (z1 < z2) return true; + if (z1 > z2) return false; - /* XXX XXX */ - v = v ? v : 0; - - /* Swap */ - holder = who[a]; - who[a] = who[b]; - who[b] = holder; + /* Punt to total number of kills. */ + return compare_total_kills(w1, w2); } - /* * Hack -- Display the "name" and "attr/chars" of a monster race */ @@ -1437,7 +1409,7 @@ static void roff_top(int r_idx) */ void do_cmd_query_symbol(void) { - int i, n, r_idx; + int i, r_idx; char sym, query; @@ -1458,11 +1430,7 @@ void do_cmd_query_symbol(void) bool_ recall = FALSE; - - u16b why = 0; - - u16b *who; - + bool (*sort_by)(int,int) = nullptr; /* Get a character, or abort */ if (!get_com("Enter character to be identified, " @@ -1509,11 +1477,9 @@ void do_cmd_query_symbol(void) /* Display the result */ prt(buf, 0, 0); - /* Allocate the "who" array */ - C_MAKE(who, max_r_idx, u16b); - /* Collect matching monsters */ - for (n = 0, i = 1; i < max_r_idx; i++) + std::vector<u16b> who; + for (i = 1; i < max_r_idx; i++) { monster_race *r_ptr = &r_info[i]; @@ -1538,15 +1504,14 @@ void do_cmd_query_symbol(void) } /* Collect "appropriate" monsters */ - if (all || (r_ptr->d_char == sym)) who[n++] = i; + if (all || (r_ptr->d_char == sym)) { + who.push_back(i); + } } /* Nothing to recall */ - if (!n) + if (who.empty()) { - /* Free the "who" array */ - C_KILL(who, max_r_idx, u16b); - return; } @@ -1564,41 +1529,34 @@ void do_cmd_query_symbol(void) /* Sort by kills (and level) */ if (query == 'k') { - why = 4; + sort_by = compare_player_kills; query = 'y'; } /* Sort by level */ if (query == 'p') { - why = 2; + sort_by = compare_monster_level; query = 'y'; } /* Catch "escape" */ if (query != 'y') { - /* Free the "who" array */ - C_KILL(who, max_r_idx, u16b); - return; } /* Sort if needed */ - if (why) + if (sort_by) { - /* Select the sort method */ - ang_sort_comp = ang_sort_comp_hook; - ang_sort_swap = ang_sort_swap_hook; - /* Sort the array */ - ang_sort(who, &why, n); + std::sort(std::begin(who), std::end(who), sort_by); } /* Start at the end */ - i = n - 1; + i = who.size() - 1; /* Scan the monster memory */ while (1) @@ -1659,7 +1617,7 @@ void do_cmd_query_symbol(void) /* Move to "prev" monster */ if (query == '-') { - if (++i == n) + if (++i == who.size()) { i = 0; if (!expand_list) break; @@ -1671,7 +1629,7 @@ void do_cmd_query_symbol(void) { if (i-- == 0) { - i = n - 1; + i = who.size() - 1; if (!expand_list) break; } } @@ -1679,9 +1637,6 @@ void do_cmd_query_symbol(void) /* Re-display the identity */ prt(buf, 0, 0); - - /* Free the "who" array */ - C_KILL(who, max_r_idx, u16b); } @@ -1691,7 +1646,7 @@ void do_cmd_query_symbol(void) */ bool_ research_mon() { - int i, n, r_idx; + int i, r_idx; char sym, query; @@ -1716,13 +1671,8 @@ bool_ research_mon() bool_ recall = FALSE; - u16b why = 0; - monster_race *r2_ptr; - u16b *who; - - /* Hack -- Remember "cheat_know" flag */ oldcheat = cheat_know; @@ -1730,9 +1680,6 @@ bool_ research_mon() /* Get a character, or abort */ if (!get_com("Enter character of monster: ", &sym)) return (TRUE); - /* Allocate the "who" array */ - C_MAKE(who, max_r_idx, u16b); - /* Find that character info, and describe it */ for (i = 0; ident_info[i]; ++i) { @@ -1753,7 +1700,8 @@ bool_ research_mon() /* Collect matching monsters */ - for (n = 0, i = 1; i < max_r_idx; i++) + std::vector<u16b> who; + for (i = 1; i < max_r_idx; i++) { monster_race *r_ptr = &r_info[i]; @@ -1770,15 +1718,14 @@ bool_ research_mon() if (uniq && !(r_ptr->flags1 & (RF1_UNIQUE))) continue; /* Collect "appropriate" monsters */ - if (all || (r_ptr->d_char == sym)) who[n++] = i; + if (all || (r_ptr->d_char == sym)) { + who.push_back(i); + } } /* Nothing to recall */ - if (!n) + if (who.empty()) { - /* Free the "who" array */ - C_KILL(who, max_r_idx, u16b); - /* Restore the "cheat_know" flag */ cheat_know = oldcheat; @@ -1786,24 +1733,14 @@ bool_ research_mon() } - /* Sort by level */ - why = 2; query = 'y'; - /* Sort if needed */ - if (why) - { - /* Select the sort method */ - ang_sort_comp = ang_sort_comp_hook; - ang_sort_swap = ang_sort_swap_hook; - - /* Sort the array */ - ang_sort(who, &why, n); - } + /* Sort by level */ + std::sort(std::begin(who), std::end(who), compare_monster_level); /* Start at the end */ - i = n - 1; + i = who.size() - 1; notpicked = TRUE; @@ -1874,7 +1811,7 @@ bool_ research_mon() /* Move to "prev" monster */ if (query == '-') { - if (++i == n) + if (++i == who.size()) { i = 0; if (!expand_list) break; @@ -1886,7 +1823,7 @@ bool_ research_mon() { if (i-- == 0) { - i = n - 1; + i = who.size() - 1; if (!expand_list) break; } } @@ -1896,9 +1833,6 @@ bool_ research_mon() /* Re-display the identity */ /* prt(buf, 5, 5);*/ - /* Free the "who" array */ - C_KILL(who, max_r_idx, u16b); - /* Restore the "cheat_know" flag */ cheat_know = oldcheat; diff --git a/src/cmd4.c b/src/cmd4.cc index 3a6ef2cc..e00f5835 100644 --- a/src/cmd4.c +++ b/src/cmd4.cc @@ -15,6 +15,11 @@ #include "messages.h" #include "hooks.h" +#include <memory> +#include <string> +#include <vector> +#include <algorithm> + /* * Hack -- redraw the screen * @@ -2185,7 +2190,8 @@ void do_cmd_visuals(void) /* Dump the monster attr/char info */ fprintf(fff, "R:%d:0x%02X:0x%02X\n\n", i, - (byte)(r_ptr->x_attr), (byte)(r_ptr->x_char)); + static_cast<unsigned int>(r_ptr->x_attr), + static_cast<unsigned int>(r_ptr->x_char)); } /* All done */ @@ -3160,13 +3166,6 @@ void do_cmd_knowledge_artifacts(void) char base_name[80]; - bool_ *okay, *okayk; - - - /* Allocate the "okay" array */ - C_MAKE(okay, max_a_idx, bool_); - C_MAKE(okayk, max_k_idx, bool_); - /* Temporary file */ if (path_temp(file_name, 1024)) return; @@ -3174,6 +3173,7 @@ void do_cmd_knowledge_artifacts(void) fff = my_fopen(file_name, "w"); /* Scan the artifacts */ + std::unique_ptr<bool_[]> okay(new bool_[max_a_idx]); for (k = 0; k < max_a_idx; k++) { artifact_type *a_ptr = &a_info[k]; @@ -3191,6 +3191,7 @@ void do_cmd_knowledge_artifacts(void) okay[k] = TRUE; } + std::unique_ptr<bool_[]> okayk(new bool_[max_k_idx]); for (k = 0; k < max_k_idx; k++) { object_kind *k_ptr = &k_info[k]; @@ -3396,9 +3397,6 @@ void do_cmd_knowledge_artifacts(void) /* Remove the file */ fd_kill(file_name); - - C_FREE(okay, max_a_idx, bool_); - C_FREE(okayk, max_k_idx, bool_); } @@ -3449,54 +3447,28 @@ void do_cmd_knowledge_traps(void) } +static int monster_get_race_level(int r_idx) { + /* Hack -- Morgoth is always last */ + if (r_idx == 862) { + return 20000; + } + /* Otherwise, we'll use the real level. */ + return r_info[r_idx].level; +} + +static bool compare_monster_level(int r_idx1, int r_idx2) { + return monster_get_race_level(r_idx1) < monster_get_race_level(r_idx2); +} + /* * Display known uniques * * Note that the player ghosts are ignored. XXX XXX XXX */ -static void insert_sort_unique(int *sort_uniques, int *num, int r_idx) -{ - int i, j; - - monster_race *r_ptr = &r_info[r_idx]; - - int level = r_ptr->level; - - - /* Hack -- Morgoth is always at the bottom of the list */ - if (r_idx == 862) level = 20000; - - /* Find the place */ - for (i = 0; i < *num; i++) - { - monster_race *r2_ptr = &r_info[sort_uniques[i]]; - int level2 = r2_ptr->level; - - if (sort_uniques[i] == 862) level2 = 20000; - - if (level < level2) break; - } - - /* Move the remaining items */ - for (j = *num - 1; j >= i; j--) - { - sort_uniques[j + 1] = sort_uniques[j]; - } - - /* Insert it */ - sort_uniques[i] = r_idx; - (*num)++; -} - - static void do_cmd_knowledge_uniques(void) { int k; - int *sort_uniques; - - int num = 0; - FILE *fff; char file_name[1024]; @@ -3508,26 +3480,30 @@ static void do_cmd_knowledge_uniques(void) /* Open a new file */ fff = my_fopen(file_name, "w"); - C_MAKE(sort_uniques, max_r_idx, int); - - /* Sort the monster races */ + // Extract the unique race indexes. + std::vector<int> unique_r_idxs; for (k = 1; k < max_r_idx; k++) { monster_race *r_ptr = &r_info[k]; /* Only print Uniques */ if (r_ptr->flags1 & (RF1_UNIQUE) && - !(r_ptr->flags7 & RF7_PET) && - !(r_ptr->flags7 & RF7_NEUTRAL)) + !(r_ptr->flags7 & RF7_PET) && + !(r_ptr->flags7 & RF7_NEUTRAL)) { - insert_sort_unique(sort_uniques, &num, k); + unique_r_idxs.push_back(k); } } - /* Scan the monster races -- sorted */ - for (k = 0; k < num; k++) + // Sort races by level. + std::sort(std::begin(unique_r_idxs), + std::end(unique_r_idxs), + compare_monster_level); + + /* Scan the monster races */ + for (int r_idx : unique_r_idxs) { - monster_race *r_ptr = &r_info[sort_uniques[k]]; + monster_race *r_ptr = &r_info[r_idx]; /* Only print Uniques */ if (r_ptr->flags1 & (RF1_UNIQUE)) @@ -3576,8 +3552,6 @@ static void do_cmd_knowledge_uniques(void) } } - C_FREE(sort_uniques, max_r_idx, int); - /* Close the file */ my_fclose(fff); @@ -4570,57 +4544,58 @@ void do_cmd_time() * It records all keypresses and then put them in a macro * Not as powerful as the macro screen, but much easier for newbies */ -char *macro_recorder_current = NULL; + +std::string *macro_recorder_current = nullptr; + void macro_recorder_start() { msg_print("Starting macro recording, press this key again to stop. Note that if the action you want to record accepts the @ key, use it; it will remove your the need to inscribe stuff."); - C_MAKE(macro_recorder_current, 1, char); - macro_recorder_current[0] = '\0'; + assert (macro_recorder_current == nullptr); + macro_recorder_current = new std::string(); } void macro_recorder_add(char c) { - char *old_macro_recorder_current = macro_recorder_current; - - if (macro_recorder_current == NULL) return; - - C_MAKE(macro_recorder_current, strlen(macro_recorder_current) + 1 + 1, char); - sprintf(macro_recorder_current, "%s%c", old_macro_recorder_current, c); - C_FREE(old_macro_recorder_current, strlen(old_macro_recorder_current) + 1, char); + // Gets called unconditionally for all input, so ignore unless + // we're actual recording. + if (macro_recorder_current) { + macro_recorder_current->push_back(c); + } } void macro_recorder_stop() { - char *str, *macro; - char buf[1024]; + assert(macro_recorder_current != nullptr); + + // Remove the last key, because it is the key to stop recording + macro_recorder_current->pop_back(); - /* Ok we remove the last key, because it is the key to stop recording */ - macro_recorder_current[strlen(macro_recorder_current) - 1] = '\0'; + // Copy out current macro text. + std::string macro(*macro_recorder_current); - /* Stop the recording */ - macro = macro_recorder_current; - macro_recorder_current = NULL; + // Stop recording. + delete macro_recorder_current; + macro_recorder_current = nullptr; /* Add it */ if (get_check("Are you satisfied and want to create the macro? ")) { + char buf[1024]; + prt("Trigger: ", 0, 0); /* Get a macro trigger */ do_cmd_macro_aux(buf, FALSE); /* Link the macro */ - macro_add(buf, macro); + macro_add(buf, macro.c_str()); /* Prompt */ - C_MAKE(str, (strlen(macro) + 1) * 3, char); - ascii_to_text(str, macro); - msg_format("Added a macro '%s'. If you want it to stay permanently, press @ now and dump macros to a file.", str); - C_FREE(str, (strlen(macro) + 1) * 3, char); + std::unique_ptr<char[]> str(new char[(macro.length() + 1) * 3]); + str[0] = '\0'; + ascii_to_text(str.get(), macro.c_str()); + msg_format("Added a macro '%s'. If you want it to stay permanently, press @ now and dump macros to a file.", str.get()); } - - /* Ok now rid of useless stuff */ - C_FREE(macro, strlen(macro) + 1, char); } void do_cmd_macro_recorder() diff --git a/src/cmd5.c b/src/cmd5.cc index 95ae0346..95ae0346 100644 --- a/src/cmd5.c +++ b/src/cmd5.cc diff --git a/src/cmd6.c b/src/cmd6.cc index b8a9f1df..b8a9f1df 100644 --- a/src/cmd6.c +++ b/src/cmd6.cc diff --git a/src/cmd7.c b/src/cmd7.cc index c9be0c66..c9be0c66 100644 --- a/src/cmd7.c +++ b/src/cmd7.cc diff --git a/src/cmovie.c b/src/cmovie.cc index d1459e02..85db2a32 100644 --- a/src/cmovie.c +++ b/src/cmovie.cc @@ -228,7 +228,11 @@ s16b do_play_cmovie(cptr cmov_file) /* Get the index */ y = atoi(buf + 2); - C_COPY(cbuf, s, 80, char); + /* Copy */ + for (int i = 0; i < 80; i++) + { + cbuf[i] = s[i]; + } /* Next... */ continue; diff --git a/src/corrupt.c b/src/corrupt.cc index a4c579d4..a4c579d4 100644 --- a/src/corrupt.c +++ b/src/corrupt.cc diff --git a/src/device_allocation.c b/src/device_allocation.cc index e64f0401..f97de2ec 100644 --- a/src/device_allocation.c +++ b/src/device_allocation.cc @@ -20,7 +20,7 @@ void device_allocation_init(device_allocation *device_allocation, byte tval) device_allocation *device_allocation_new(byte tval) { - device_allocation *d = malloc(sizeof(device_allocation)); + device_allocation *d = new device_allocation; device_allocation_init(d, tval); return d; } diff --git a/src/dice.c b/src/dice.cc index ea9b7533..ea9b7533 100644 --- a/src/dice.c +++ b/src/dice.cc diff --git a/src/dungeon.c b/src/dungeon.cc index df3b41aa..df3b41aa 100644 --- a/src/dungeon.c +++ b/src/dungeon.cc diff --git a/src/externs.h b/src/externs.h index 20f094dd..29a64267 100644 --- a/src/externs.h +++ b/src/externs.h @@ -316,7 +316,7 @@ extern term *angband_term[ANGBAND_TERM_MAX]; extern char angband_term_name[ANGBAND_TERM_MAX][80]; extern byte angband_color_table[256][4]; extern char angband_sound_name[SOUND_MAX][16]; -extern cave_type *cave[MAX_HGT]; +extern cave_type **cave; extern object_type *o_list; extern monster_type *m_list; extern monster_type *km_list; @@ -1902,8 +1902,6 @@ s16b get_random_spell(s16b random_type, int lev); /* spells6.c */ -SGLIB_DEFINE_LIST_PROTOTYPES(school_provider, compare_school_provider, next); - void schools_init(); school_type *school_at(int index); @@ -1945,7 +1943,6 @@ extern s32b rescale(s32b x, s32b max, s32b new_max); extern bool_ input_box(cptr text, int y, int x, char *buf, int max); extern void draw_box(int y, int x, int h, int w); extern void display_list(int y, int x, int h, int w, cptr title, cptr *list, int max, int begin, int sel, byte sel_color); -extern int ask_menu(cptr ask, char **items, int max); extern cptr get_player_race_name(int pr, int ps); extern cptr get_month_name(int month, bool_ full, bool_ compact); extern cptr get_day(int day); @@ -2324,7 +2321,7 @@ extern s16b get_skill_scale(int skill, u32b scale); extern void do_cmd_skill(void); extern void do_cmd_activate_skill(void); extern s16b melee_skills[MAX_MELEE]; -extern char *melee_names[MAX_MELEE]; +extern const char *melee_names[MAX_MELEE]; extern s16b get_melee_skills(void); extern s16b get_melee_skill(void); extern bool_ forbid_gloves(void); diff --git a/src/files.c b/src/files.cc index a599af23..ca4e15a6 100644 --- a/src/files.c +++ b/src/files.cc @@ -15,6 +15,8 @@ #include "hiscore.h" #include "hooks.h" +#include <memory> +#include <unordered_set> /* * Extract the first few "tokens" from a buffer @@ -465,10 +467,14 @@ errr process_pref_file_aux(char *buf) if (macro_template != NULL) { - free(macro_template); + delete[] macro_template; macro_template = NULL; + for (i = 0; i < max_macrotrigger; i++) - free(macro_trigger_name[i]); + { + delete[] macro_trigger_name[i]; + macro_trigger_name[i] = nullptr; + } max_macrotrigger = 0; } @@ -479,7 +485,7 @@ errr process_pref_file_aux(char *buf) len = strlen(zz[0]) + 1 + num + 1; for (i = 0; i < num; i++) len += strlen(zz[2 + i]) + 1; - macro_template = malloc(len); + macro_template = new char[len]; strcpy(macro_template, zz[0]); macro_modifier_chr = @@ -509,7 +515,7 @@ errr process_pref_file_aux(char *buf) len = strlen(zz[0]) + 1 + strlen(zz[1]) + 1; if (tok == 3) len += strlen(zz[2]) + 1; - macro_trigger_name[m] = malloc(len); + macro_trigger_name[m] = new char[len]; t = macro_trigger_name[m]; s = zz[0]; @@ -2470,46 +2476,26 @@ void file_character_print_store(FILE *fff, wilderness_type_info *place, int stor } } -/* +/** * Helper function for file_character * * Checks if the store hasn't been added to the list yet, and then adds it if it * was not already there. XXX This is an ugly workaround for the double Gondolin * problem. - * - * Beware of the ugly pointer gymnastics. */ -bool_ file_character_check_stores(store_type ***store_list, int *store_list_count, wilderness_type_info *place, int store) +static bool_ file_character_check_stores(std::unordered_set<store_type *> *seen_stores, wilderness_type_info *place, int store) { town_type *town = &town_info[place->entrance]; store_type *st_ptr = &town->store[store]; - store_type **head = *store_list; - int i; - - /* check the list for this store */ - for (i = 0; i < *store_list_count; ++i) - { - if (*head == st_ptr) return FALSE; - ++head; - } - /* make room for the new one in the array */ - if (*store_list) + // Already seen store? + if (seen_stores->find(st_ptr) != seen_stores->end()) { - head = *store_list; - *store_list = C_RNEW(*store_list_count + 1, store_type *); - C_COPY(*store_list, head, *store_list_count, store_type *); - C_FREE(head, *store_list_count, store_type *); + return FALSE; } - else - { - *store_list = RNEW(store_type *); - } - - /* update data */ - (*store_list)[*store_list_count] = st_ptr; - ++*store_list_count; + // Add + seen_stores->insert(st_ptr); return TRUE; } @@ -2527,8 +2513,6 @@ errr file_character(cptr name, bool_ full) int fd = -1; FILE *fff = NULL; char buf[1024]; - store_type **store_list = NULL; - int store_list_count = 0; /* Build the filename */ path_build(buf, 1024, ANGBAND_DIR_USER, name); @@ -2831,24 +2815,30 @@ errr file_character(cptr name, bool_ full) fprintf(fff, "\n\n"); /* Print all homes in the different towns */ - for (j = 0; j < max_wf_idx; j++) { - if (wf_info[j].feat == FEAT_TOWN && - file_character_check_stores(&store_list, &store_list_count, &wf_info[j], 7)) - file_character_print_store(fff, &wf_info[j], 7, full); + std::unordered_set<store_type *> seen_stores; + for (j = 0; j < max_wf_idx; j++) + { + if (wf_info[j].feat == FEAT_TOWN && + file_character_check_stores(&seen_stores, &wf_info[j], 7)) + { + file_character_print_store(fff, &wf_info[j], 7, full); + } + } } - store_list = C_FREE(store_list, store_list_count, store_type *); - store_list_count = 0; /* Print all Mathom-houses in the different towns */ - for (j = 0; j < max_wf_idx; j++) { - if (wf_info[j].feat == FEAT_TOWN && - file_character_check_stores(&store_list, &store_list_count, &wf_info[j], 57)) - file_character_print_store(fff, &wf_info[j], 57, full); + std::unordered_set<store_type *> seen_stores; + for (j = 0; j < max_wf_idx; j++) + { + if (wf_info[j].feat == FEAT_TOWN && + file_character_check_stores(&seen_stores, &wf_info[j], 57)) + { + file_character_print_store(fff, &wf_info[j], 57, full); + } + } } - store_list = C_FREE(store_list, store_list_count, store_type *); - store_list_count = 0; text_out_indent = 0; @@ -2934,9 +2924,6 @@ bool_ show_file(cptr name, cptr what, int line, int mode) /* Find this string (if any) */ cptr find = NULL; - /* Char array type of hyperlink info */ - hyperlink_type *h_ptr; - /* Pointer to general buffer in the above */ char *buf; @@ -2946,7 +2933,8 @@ bool_ show_file(cptr name, cptr what, int line, int mode) int wid, hgt; /* Allocate hyperlink data */ - MAKE(h_ptr, hyperlink_type); + std::unique_ptr<hyperlink_type> h_ptr(new hyperlink_type); + memset(h_ptr.get(), 0, sizeof(hyperlink_type)); /* Setup buffer pointer */ buf = h_ptr->rbuf; @@ -3016,9 +3004,6 @@ bool_ show_file(cptr name, cptr what, int line, int mode) msg_format("Cannot open '%s'.", name); msg_print(NULL); - /* Free hyperlink info */ - KILL(h_ptr, hyperlink_type); - /* Oops */ return (TRUE); } @@ -3146,9 +3131,6 @@ bool_ show_file(cptr name, cptr what, int line, int mode) /* Oops */ if (!fff) { - /* Free hyperlink info */ - KILL(h_ptr, hyperlink_type); - return (FALSE); } @@ -3489,9 +3471,6 @@ bool_ show_file(cptr name, cptr what, int line, int mode) /* Close the file */ my_fclose(fff); - /* Free hyperlink buffers */ - KILL(h_ptr, hyperlink_type); - /* Escape */ if (k == ESCAPE) return (FALSE); @@ -3520,9 +3499,6 @@ bool_ txt_to_html(cptr head, cptr foot, cptr base, cptr ext, bool_ force, bool_ /* Current html file */ FILE *htm = NULL; - /* Char array type of hyperlink info */ - hyperlink_type *h_ptr; - cptr file_ext = "html"; cptr link_prefix = ""; cptr link_suffix = ""; @@ -3531,7 +3507,8 @@ bool_ txt_to_html(cptr head, cptr foot, cptr base, cptr ext, bool_ force, bool_ char *buf; /* Allocate hyperlink data */ - MAKE(h_ptr, hyperlink_type); + std::unique_ptr<hyperlink_type> h_ptr(new hyperlink_type); + memset(h_ptr.get(), 0, sizeof(hyperlink_type)); /* Setup buffer pointer */ buf = h_ptr->rbuf; @@ -3566,9 +3543,6 @@ bool_ txt_to_html(cptr head, cptr foot, cptr base, cptr ext, bool_ force, bool_ /* Oops */ if (!fff || !htm) { - /* Free hyperlink info */ - KILL(h_ptr, hyperlink_type); - my_fclose(fff); my_fclose(htm); @@ -3812,9 +3786,6 @@ bool_ txt_to_html(cptr head, cptr foot, cptr base, cptr ext, bool_ force, bool_ my_fclose(htm); my_fclose(fff); - /* Free hyperlink buffers */ - KILL(h_ptr, hyperlink_type); - /* Normal return */ return (TRUE); } @@ -4430,7 +4401,7 @@ static void print_tomb(void) FILE *fp; - time_t ct = time((time_t)0); + time_t ct = time(nullptr); /* Clear screen */ diff --git a/src/gen_evol.c b/src/gen_evol.cc index bfdfbd68..bfdfbd68 100644 --- a/src/gen_evol.c +++ b/src/gen_evol.cc diff --git a/src/gen_maze.c b/src/gen_maze.cc index 92cb482f..86f6d866 100644 --- a/src/gen_maze.c +++ b/src/gen_maze.cc @@ -13,6 +13,8 @@ #include "angband.h" +#include <memory> + /* * If we wasted static memory for this, it would look like: * @@ -145,10 +147,9 @@ bool_ level_generate_maze() int y, dy = 0; int x, dx = 0; int m_1 = 0, m_2 = 0; - maze_row *maze; /* Allocate temporary memory */ - C_MAKE(maze, (MAX_HGT / 2) + 2, maze_row); + std::unique_ptr<maze_row[]> maze(new maze_row[(MAX_HGT / 2) + 2]); /* * the empty maze is: @@ -209,7 +210,7 @@ bool_ level_generate_maze() x = rand_range(1, (cur_wid / 2)); d = rand_range(0, 3); - dig(maze, y, x, d); + dig(maze.get(), y, x, d); maze[y][x] = 0; @@ -283,9 +284,6 @@ bool_ level_generate_maze() } } - /* Free temporary memory */ - C_FREE(maze, (MAX_HGT / 2) + 2, maze_row); - /* Determine the character location */ if (!new_player_spot(get_branch())) return FALSE; diff --git a/src/generate.c b/src/generate.cc index b06129ab..934b717d 100644 --- a/src/generate.c +++ b/src/generate.cc @@ -13,6 +13,9 @@ #include "angband.h" #include "hooks.h" +#include <memory> +#include <vector> + #define SAFE_MAX_ATTEMPTS 5000 /* @@ -275,7 +278,7 @@ typedef struct level_generator_type level_generator_type; struct level_generator_type { cptr name; - bool_ (*generator)(cptr); + bool_ (*generator)(); bool_ default_stairs; bool_ default_monsters; @@ -292,9 +295,8 @@ static level_generator_type *level_generators = NULL; */ void add_level_generator(cptr name, bool_ (*generator)(), bool_ stairs, bool_ monsters, bool_ objects, bool_ miscs) { - level_generator_type *g; + level_generator_type *g = new level_generator_type; - MAKE(g, level_generator_type); g->name = string_make(name); g->generator = generator; @@ -4723,7 +4725,7 @@ static void fill_treasure(int x1, int x2, int y1, int y2, int difficulty) * Thing added based on distance to center of vault * Difficulty is 1-easy to 10-hard */ - value = (((s32b)distance(cx, cy, x, y) * 100) / size) + + value = ((static_cast<s32b>(distance(cx, cy, x, y)) * 100) / size) + randint(10) - difficulty; /* Hack -- Empty square part of the time */ @@ -5290,7 +5292,7 @@ static void build_maze_vault(int x0, int y0, int xsize, int ysize) { int y, x, dy, dx; int y1, x1, y2, x2; - int i, m, n, num_vertices, *visited; + int m, n, num_vertices; bool_ light; cave_type *c_ptr; @@ -5337,22 +5339,13 @@ static void build_maze_vault(int x0, int y0, int xsize, int ysize) num_vertices = m * n; /* Allocate an array for visited vertices */ - C_MAKE(visited, num_vertices, int); - - /* Initialise array of visited vertices */ - for (i = 0; i < num_vertices; i++) - { - visited[i] = 0; - } + std::vector<int> visited(num_vertices, 0); /* Traverse the graph to create a spaning tree, pick a random root */ - r_visit(y1, x1, y2, x2, rand_int(num_vertices), 0, visited); + r_visit(y1, x1, y2, x2, rand_int(num_vertices), 0, visited.data()); /* Fill with monsters and treasure, low difficulty */ fill_treasure(x1, x2, y1, y2, randint(5)); - - /* Free the array for visited vertices */ - C_FREE(visited, num_vertices, int); } @@ -5368,8 +5361,7 @@ static void build_mini_c_vault(int x0, int y0, int xsize, int ysize) { int dy, dx; int y1, x1, y2, x2, y, x, total; - int i, m, n, num_vertices; - int *visited; + int m, n, num_vertices; if (cheat_room) msg_print("Mini Checker Board Vault"); @@ -5402,16 +5394,10 @@ static void build_mini_c_vault(int x0, int y0, int xsize, int ysize) num_vertices = m * n; /* Allocate an array for visited vertices */ - C_MAKE(visited, num_vertices, int); - - /* Initialise array of visited vertices */ - for (i = 0; i < num_vertices; i++) - { - visited[i] = 0; - } + std::vector<int> visited(num_vertices, 0); /* Traverse the graph to create a spannng tree, pick a random root */ - r_visit(y1, x1, y2, x2, rand_int(num_vertices), 0, visited); + r_visit(y1, x1, y2, x2, rand_int(num_vertices), 0, visited.data()); /* Make it look like a checker board vault */ for (x = x1; x <= x2; x++) @@ -5446,9 +5432,6 @@ static void build_mini_c_vault(int x0, int y0, int xsize, int ysize) /* Fill with monsters and treasure, highest difficulty */ fill_treasure(x1, x2, y1, y2, 10); - - /* Free the array for visited vertices */ - C_FREE(visited, num_vertices, int); } @@ -7217,7 +7200,9 @@ void save_all_friends() { if (p_ptr->old_wild_mode) return; - C_COPY(km_list, m_list, max_m_idx, monster_type); + for (int i = 0; i < max_m_idx; i++) { + km_list[i] = m_list[i]; + } } @@ -7587,7 +7572,7 @@ static bool_ cave_gen(void) { if (!strcmp(generator->name, generator_name)) { - if (!generator->generator(generator->name)) + if (!generator->generator()) return FALSE; break; } diff --git a/src/gods.c b/src/gods.cc index f940e21a..f940e21a 100644 --- a/src/gods.c +++ b/src/gods.cc diff --git a/src/help.c b/src/help.cc index 4c14da9d..4c14da9d 100644 --- a/src/help.c +++ b/src/help.cc diff --git a/src/hiscore.c b/src/hiscore.cc index 357026da..357026da 100644 --- a/src/hiscore.c +++ b/src/hiscore.cc diff --git a/src/hooks.c b/src/hooks.cc index 08418035..ed8be7e9 100644 --- a/src/hooks.c +++ b/src/hooks.cc @@ -53,7 +53,8 @@ hooks_chain* add_hook(int h_idx, hook_type hook, cptr name) /* If not already in the list, add it */ if (c == NULL) { - MAKE(new_, hooks_chain); + new_ = new hooks_chain(); + memset(new_, 0, sizeof(hooks_chain)); new_->hook = hook; sprintf(new_->name, "%s", name); new_->next = hooks_heads[h_idx]; @@ -89,12 +90,12 @@ void del_hook(int h_idx, hook_type hook) if (p == NULL) { hooks_heads[h_idx] = c->next; - FREE(c, hooks_chain); + delete c; } else { p->next = c->next; - FREE(c, hooks_chain); + delete c; } } } diff --git a/src/include/tome/make_array.hpp b/src/include/tome/make_array.hpp new file mode 100644 index 00000000..ac0edb80 --- /dev/null +++ b/src/include/tome/make_array.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include <type_traits> + +/* + * Make an array of a POD type. + */ +template <typename T> T *make_array(size_t n) { + static_assert(std::is_pod<T>::value, "Type parameter must be POD type"); + T *array = new T[n]; + memset(array, 0, n*sizeof(T)); + return array; +} diff --git a/src/init1.c b/src/init1.cc index 076ca70e..076ca70e 100644 --- a/src/init1.c +++ b/src/init1.cc diff --git a/src/init2.c b/src/init2.cc index f5001e0d..4fb97a7e 100644 --- a/src/init2.c +++ b/src/init2.cc @@ -4,11 +4,14 @@ #include "angband.h" -#include <assert.h> - #include "messages.h" #include "quark.h" #include "hooks.h" +#include "tome/make_array.hpp" + +#include <assert.h> + +#include <type_traits> /* * This file is used to initialise various variables and arrays for the @@ -202,21 +205,6 @@ void init_file_paths(char *path) } -/** - * Realloc the given character array. - */ -static void z_realloc(char **p, size_t n) { - /* realloc doesn't really support size 0, but we want to shrink the allocated area regardless. */ - if (n == 0) { - n = 1; - } - /* do the reallocation */ - *p = realloc(*p, n); - if (*p == NULL) { - quit("Error during realloc."); - } -} - /* * Hack -- help give useful error messages */ @@ -252,6 +240,18 @@ static void note(cptr str) } +/* + * Make a new header + */ +static header *make_header(u16b info_num) +{ + header *h = new header; + h->info_num = info_num; + h->name_size = 0; + h->text_size = 0; + return h; +} + /* * Initialise the "f_info" array @@ -270,22 +270,17 @@ static errr init_f_info(void) /*** Make the header ***/ - - /* Allocate the "header" */ - MAKE(f_head, header); - - /* Save the "record" information */ - f_head->info_num = max_f_idx; + f_head = make_header(max_f_idx); /*** Make the fake arrays ***/ /* Allocate the "f_info" array */ - C_MAKE(f_info, f_head->info_num, feature_type); + f_info = make_array<feature_type>(f_head->info_num); /* Hack -- make "fake" arrays */ - C_MAKE(f_name, FAKE_NAME_SIZE, char); - C_MAKE(f_text, FAKE_TEXT_SIZE, char); + f_name = make_array<char>(FAKE_NAME_SIZE); + f_text = make_array<char>(FAKE_TEXT_SIZE); /*** Load the ascii template file ***/ @@ -323,10 +318,6 @@ static errr init_f_info(void) quit("Error in 'f_info.txt' file."); } - /* Reduce sizes of the arrays */ - z_realloc(&f_name, f_head->name_size); - z_realloc(&f_text, f_head->text_size); - /* Success */ return (0); } @@ -350,22 +341,17 @@ static errr init_k_info(void) /*** Make the header ***/ - - /* Allocate the "header" */ - MAKE(k_head, header); - - /* Save the "record" information */ - k_head->info_num = max_k_idx; + k_head = make_header(max_k_idx); /*** Make the fake arrays ***/ /* Allocate the "k_info" array */ - C_MAKE(k_info, k_head->info_num, object_kind); + k_info = make_array<object_kind>(k_head->info_num); /* Hack -- make "fake" arrays */ - C_MAKE(k_name, FAKE_NAME_SIZE, char); - C_MAKE(k_text, FAKE_TEXT_SIZE, char); + k_name = make_array<char>(FAKE_NAME_SIZE); + k_text = make_array<char>(FAKE_TEXT_SIZE); /*** Load the ascii template file ***/ @@ -403,10 +389,6 @@ static errr init_k_info(void) quit("Error in 'k_info.txt' file."); } - /* Reduce sizes of the arrays */ - z_realloc(&k_name, k_head->name_size); - z_realloc(&k_text, k_head->text_size); - /* Success */ return (0); } @@ -430,22 +412,17 @@ static errr init_set_info(void) /*** Make the "header" ***/ - - /* Allocate the "header" */ - MAKE(set_head, header); - - /* Save the "record" information */ - set_head->info_num = max_set_idx; + set_head = make_header(max_set_idx); /*** Make the fake arrays ***/ /* Allocate the "set_info" array */ - C_MAKE(set_info, set_head->info_num, set_type); + set_info = make_array<set_type>(set_head->info_num); /* Hack -- make "fake" arrays */ - C_MAKE(set_name, FAKE_NAME_SIZE, char); - C_MAKE(set_text, FAKE_TEXT_SIZE, char); + set_name = make_array<char>(FAKE_NAME_SIZE); + set_text = make_array<char>(FAKE_TEXT_SIZE); /*** Load the ascii template file ***/ @@ -483,10 +460,6 @@ static errr init_set_info(void) quit("Error in 'set_info.txt' file."); } - /* Reduce sizes of the arrays */ - z_realloc(&set_name, set_head->name_size); - z_realloc(&set_text, set_head->text_size); - /* Success */ return (0); } @@ -509,22 +482,17 @@ static errr init_a_info(void) /*** Make the "header" ***/ - - /* Allocate the "header" */ - MAKE(a_head, header); - - /* Save the "record" information */ - a_head->info_num = max_a_idx; + a_head = make_header(max_a_idx); /*** Make the fake arrays ***/ /* Allocate the "a_info" array */ - C_MAKE(a_info, a_head->info_num, artifact_type); + a_info = make_array<artifact_type>(a_head->info_num); /* Hack -- make "fake" arrays */ - C_MAKE(a_name, FAKE_NAME_SIZE, char); - C_MAKE(a_text, FAKE_TEXT_SIZE, char); + a_name = make_array<char>(FAKE_NAME_SIZE); + a_text = make_array<char>(FAKE_TEXT_SIZE); /*** Load the ascii template file ***/ @@ -562,10 +530,6 @@ static errr init_a_info(void) quit("Error in 'a_info.txt' file."); } - /* Reduce sizes of the arrays */ - z_realloc(&a_name, a_head->name_size); - z_realloc(&a_text, a_head->text_size); - /* Success */ return (0); } @@ -588,22 +552,17 @@ static errr init_s_info(void) /*** Make the "header" ***/ - - /* Allocate the "header" */ - MAKE(s_head, header); - - /* Save the "record" information */ - s_head->info_num = max_s_idx; + s_head = make_header(max_s_idx); /*** Make the fake arrays ***/ /* Allocate the "s_info" array */ - C_MAKE(s_info, s_head->info_num, skill_type); + s_info = make_array<skill_type>(s_head->info_num); /* Hack -- make "fake" arrays */ - C_MAKE(s_name, FAKE_NAME_SIZE, char); - C_MAKE(s_text, FAKE_TEXT_SIZE, char); + s_name = make_array<char>(FAKE_NAME_SIZE); + s_text = make_array<char>(FAKE_TEXT_SIZE); /*** Load the ascii template file ***/ @@ -640,10 +599,6 @@ static errr init_s_info(void) quit("Error in 's_info.txt' file."); } - /* Reduce sizes of the arrays */ - z_realloc(&s_name, s_head->name_size); - z_realloc(&s_text, s_head->text_size); - /* Success */ return (0); } @@ -664,24 +619,18 @@ static errr init_ab_info(void) char buf[1024]; - /*** Make the "header" ***/ - - /* Allocate the "header" */ - MAKE(ab_head, header); - - /* Save the "record" information */ - ab_head->info_num = max_ab_idx; + ab_head = make_header(max_ab_idx); /*** Make the fake arrays ***/ /* Allocate the "ab_info" array */ - C_MAKE(ab_info, ab_head->info_num, ability_type); + ab_info = make_array<ability_type>(ab_head->info_num); /* Hack -- make "fake" arrays */ - C_MAKE(ab_name, FAKE_NAME_SIZE, char); - C_MAKE(ab_text, FAKE_TEXT_SIZE, char); + ab_name = make_array<char>(FAKE_NAME_SIZE); + ab_text = make_array<char>(FAKE_TEXT_SIZE); /*** Load the ascii template file ***/ @@ -718,10 +667,6 @@ static errr init_ab_info(void) quit("Error in 'ab_info.txt' file."); } - /* Reduce sizes of the arrays */ - z_realloc(&ab_name, ab_head->name_size); - z_realloc(&ab_text, ab_head->text_size); - /* Success */ return (0); } @@ -745,22 +690,17 @@ static errr init_e_info(void) /*** Make the "header" ***/ - - /* Allocate the "header" */ - MAKE(e_head, header); - - /* Save the "record" information */ - e_head->info_num = max_e_idx; + e_head = make_header(max_e_idx); /*** Make the fake arrays ***/ /* Allocate the "e_info" array */ - C_MAKE(e_info, e_head->info_num, ego_item_type); + e_info = make_array<ego_item_type>(e_head->info_num); /* Hack -- make "fake" arrays */ - C_MAKE(e_name, FAKE_NAME_SIZE, char); - C_MAKE(e_text, FAKE_TEXT_SIZE, char); + e_name = make_array<char>(FAKE_NAME_SIZE); + e_text = make_array<char>(FAKE_TEXT_SIZE); /*** Load the ascii template file ***/ @@ -798,10 +738,6 @@ static errr init_e_info(void) quit("Error in 'e_info.txt' file."); } - /* Reduce sizes of the arrays */ - z_realloc(&e_name, e_head->name_size); - z_realloc(&e_text, e_head->text_size); - /* Success */ return (0); } @@ -824,18 +760,13 @@ static errr init_ra_info(void) char buf[1024]; /*** Make the "header" ***/ - - /* Allocate the "header" */ - MAKE(ra_head, header); - - /* Save the "record" information */ - ra_head->info_num = max_ra_idx; + ra_head = make_header(max_ra_idx); /*** Make the fake arrays ***/ /* Allocate the "ra_info" array */ - C_MAKE(ra_info, ra_head->info_num, randart_part_type); + ra_info = make_array<randart_part_type>(ra_head->info_num); /*** Load the ascii template file ***/ @@ -895,22 +826,17 @@ static errr init_r_info(void) /*** Make the header ***/ - - /* Allocate the "header" */ - MAKE(r_head, header); - - /* Save the "record" information */ - r_head->info_num = max_r_idx; + r_head = make_header(max_r_idx); /*** Make the fake arrays ***/ /* Allocate the "r_info" array */ - C_MAKE(r_info, r_head->info_num, monster_race); + r_info = make_array<monster_race>(r_head->info_num); /* Hack -- make "fake" arrays */ - C_MAKE(r_name, FAKE_NAME_SIZE, char); - C_MAKE(r_text, FAKE_TEXT_SIZE, char); + r_name = make_array<char>(FAKE_NAME_SIZE); + r_text = make_array<char>(FAKE_TEXT_SIZE); /*** Load the ascii template file ***/ @@ -948,10 +874,6 @@ static errr init_r_info(void) quit("Error in 'r_info.txt' file."); } - /* Reduce sizes of the arrays */ - z_realloc(&r_name, r_head->name_size); - z_realloc(&r_text, r_head->text_size); - /* Success */ return (0); } @@ -974,21 +896,16 @@ static errr init_re_info(void) /*** Make the header ***/ - - /* Allocate the "header" */ - MAKE(re_head, header); - - /* Save the "record" information */ - re_head->info_num = max_re_idx; + re_head = make_header(max_re_idx); /*** Make the fake arrays ***/ /* Allocate the "re_info" array */ - C_MAKE(re_info, re_head->info_num, monster_ego); + re_info = make_array<monster_ego>(re_head->info_num); /* Hack -- make "fake" arrays */ - C_MAKE(re_name, FAKE_NAME_SIZE, char); + re_name = make_array<char>(FAKE_NAME_SIZE); /*** Load the ascii template file ***/ @@ -1026,9 +943,6 @@ static errr init_re_info(void) quit("Error in 're_info.txt' file."); } - /* Reduce sizes of the arrays */ - z_realloc(&re_name, re_head->name_size); - /* Success */ return (0); } @@ -1051,22 +965,17 @@ static errr init_d_info(void) /*** Make the header ***/ - - /* Allocate the "header" */ - MAKE(d_head, header); - - /* Save the "record" information */ - d_head->info_num = max_d_idx; + d_head = make_header(max_d_idx); /*** Make the fake arrays ***/ /* Allocate the "d_info" array */ - C_MAKE(d_info, d_head->info_num, dungeon_info_type); + d_info = make_array<dungeon_info_type>(d_head->info_num); /* Hack -- make "fake" arrays */ - C_MAKE(d_name, FAKE_NAME_SIZE, char); - C_MAKE(d_text, FAKE_TEXT_SIZE, char); + d_name = make_array<char>(FAKE_NAME_SIZE); + d_text = make_array<char>(FAKE_TEXT_SIZE); /*** Load the ascii template file ***/ @@ -1104,10 +1013,6 @@ static errr init_d_info(void) quit("Error in 'd_info.txt' file."); } - /* Reduce sizes of the arrays */ - z_realloc(&d_name, d_head->name_size); - z_realloc(&d_text, d_head->text_size); - /* Success */ return (0); } @@ -1132,63 +1037,48 @@ static errr init_player_info(void) /*** Make the header ***/ - - /* Allocate the "header" */ - MAKE(rp_head, header); - - /* Save the "record" information */ - rp_head->info_num = max_rp_idx; + rp_head = make_header(max_rp_idx); /*** Make the header ***/ - - /* Allocate the "header" */ - MAKE(rmp_head, header); - - /* Save the "record" information */ - rmp_head->info_num = max_rmp_idx; + rmp_head = make_header(max_rmp_idx); /*** Make the header ***/ - - /* Allocate the "header" */ - MAKE(c_head, header); - - /* Save the "record" information */ - c_head->info_num = max_c_idx; + c_head = make_header(max_c_idx); /*** Make the fake arrays ***/ /* Allocate the "rp_info" array */ - C_MAKE(race_info, rp_head->info_num, player_race); + race_info = make_array<player_race>(rp_head->info_num); /* Hack -- make "fake" arrays */ - C_MAKE(rp_name, FAKE_NAME_SIZE, char); - C_MAKE(rp_text, FAKE_TEXT_SIZE, char); + rp_name = make_array<char>(FAKE_NAME_SIZE); + rp_text = make_array<char>(FAKE_TEXT_SIZE); /* Allocate the "rmp_info" array */ - C_MAKE(race_mod_info, rmp_head->info_num, player_race_mod); + race_mod_info = make_array<player_race_mod>(rmp_head->info_num); /* Hack -- make "fake" arrays */ - C_MAKE(rmp_name, FAKE_NAME_SIZE, char); - C_MAKE(rmp_text, FAKE_TEXT_SIZE, char); + rmp_name = make_array<char>(FAKE_NAME_SIZE); + rmp_text = make_array<char>(FAKE_TEXT_SIZE); /* Allocate the "c_info" array */ - C_MAKE(class_info, c_head->info_num, player_class); + class_info = make_array<player_class>(c_head->info_num); /* Hack -- make "fake" arrays */ - C_MAKE(c_name, FAKE_NAME_SIZE, char); - C_MAKE(c_text, FAKE_TEXT_SIZE, char); + c_name = make_array<char>(FAKE_NAME_SIZE); + c_text = make_array<char>(FAKE_TEXT_SIZE); /* Allocate the "bg" array */ - C_MAKE(bg, max_bg_idx, hist_type); + bg = make_array<hist_type>(max_bg_idx); /* Allocate the "meta_class" array */ - C_MAKE(meta_class_info, max_mc_idx, meta_class_type); + meta_class_info = make_array<meta_class_type>(max_mc_idx); for (i = 0; i < max_mc_idx; i++) { - C_MAKE(meta_class_info[i].classes, max_c_idx, s16b); + meta_class_info[i].classes = make_array<s16b>(max_c_idx); } /*** Load the ascii template file ***/ @@ -1226,14 +1116,6 @@ static errr init_player_info(void) quit("Error in 'p_info.txt' file."); } - /* Reallocate arrays. */ - z_realloc(&rp_name, rp_head->name_size); - z_realloc(&rp_text, rp_head->text_size); - z_realloc(&rmp_name, rmp_head->name_size); - z_realloc(&rmp_text, rmp_head->text_size); - z_realloc(&c_name, c_head->name_size); - z_realloc(&c_text, c_head->text_size); - /* Success */ return (0); } @@ -1255,21 +1137,16 @@ static errr init_st_info(void) /*** Make the header ***/ - - /* Allocate the "header" */ - MAKE(st_head, header); - - /* Save the "record" information */ - st_head->info_num = max_st_idx; + st_head = make_header(max_st_idx); /*** Make the fake arrays ***/ /* Allocate the "st_info" array */ - C_MAKE(st_info, st_head->info_num, store_info_type); + st_info = make_array<store_info_type>(st_head->info_num); /* Hack -- make "fake" arrays */ - C_MAKE(st_name, FAKE_NAME_SIZE, char); + st_name = make_array<char>(FAKE_NAME_SIZE); /*** Load the ascii template file ***/ @@ -1307,9 +1184,6 @@ static errr init_st_info(void) quit("Error in 'st_info.txt' file."); } - /* Reduce sizes of the arrays */ - z_realloc(&st_name, st_head->name_size); - /* Success */ return (0); } @@ -1331,21 +1205,16 @@ static errr init_ow_info(void) /*** Make the header ***/ - - /* Allocate the "header" */ - MAKE(ow_head, header); - - /* Save the "record" information */ - ow_head->info_num = max_ow_idx; + ow_head = make_header(max_ow_idx); /*** Make the fake arrays ***/ /* Allocate the "ow_info" array */ - C_MAKE(ow_info, ow_head->info_num, owner_type); + ow_info = make_array<owner_type>(ow_head->info_num); /* Hack -- make "fake" arrays */ - C_MAKE(ow_name, FAKE_NAME_SIZE, char); + ow_name = make_array<char>(FAKE_NAME_SIZE); /*** Load the ascii template file ***/ @@ -1383,9 +1252,6 @@ static errr init_ow_info(void) quit("Error in 'ow_info.txt' file."); } - /* Reduce sizes of the arrays */ - z_realloc(&ow_name, ow_head->name_size); - /* Success */ return (0); } @@ -1407,21 +1273,16 @@ static errr init_ba_info(void) /*** Make the header ***/ - - /* Allocate the "header" */ - MAKE(ba_head, header); - - /* Save the "record" information */ - ba_head->info_num = max_ba_idx; + ba_head = make_header(max_ba_idx); /*** Make the fake arrays ***/ /* Allocate the "ba_info" array */ - C_MAKE(ba_info, ba_head->info_num, store_action_type); + ba_info = make_array<store_action_type>(ba_head->info_num); /* Hack -- make "fake" arrays */ - C_MAKE(ba_name, FAKE_NAME_SIZE, char); + ba_name = make_array<char>(FAKE_NAME_SIZE); /*** Load the ascii template file ***/ @@ -1459,9 +1320,6 @@ static errr init_ba_info(void) quit("Error in 'ba_info.txt' file."); } - /* Reduce sizes of the arrays */ - z_realloc(&ba_name, ba_head->name_size); - /* Success */ return (0); } @@ -1483,22 +1341,17 @@ static errr init_wf_info(void) /*** Make the header ***/ - - /* Allocate the "header" */ - MAKE(wf_head, header); - - /* Save the "record" information */ - wf_head->info_num = max_wf_idx; + wf_head = make_header(max_wf_idx); /*** Make the fake arrays ***/ /* Allocate the "r_info" array */ - C_MAKE(wf_info, wf_head->info_num, wilderness_type_info); + wf_info = make_array<wilderness_type_info>(wf_head->info_num); /* Hack -- make "fake" arrays */ - C_MAKE(wf_name, FAKE_NAME_SIZE, char); - C_MAKE(wf_text, FAKE_TEXT_SIZE, char); + wf_name = make_array<char>(FAKE_NAME_SIZE); + wf_text = make_array<char>(FAKE_TEXT_SIZE); /*** Load the ascii template file ***/ @@ -1536,10 +1389,6 @@ static errr init_wf_info(void) quit("Error in 'wf_info.txt' file."); } - /* Reduce sizes of the arrays */ - z_realloc(&wf_name, wf_head->name_size); - z_realloc(&wf_text, wf_head->text_size); - /* Success */ return (0); } @@ -1562,22 +1411,17 @@ static errr init_t_info(void) /*** Make the header ***/ - - /* Allocate the "header" */ - MAKE(t_head, header); - - /* Save the "record" information */ - t_head->info_num = max_t_idx; + t_head = make_header(max_t_idx); /*** Make the fake arrays ***/ /* Allocate the "t_info" array */ - C_MAKE(t_info, t_head->info_num, trap_type); + t_info = make_array<trap_type>(t_head->info_num); /* Hack -- make "fake" arrays */ - C_MAKE(t_name, FAKE_NAME_SIZE, char); - C_MAKE(t_text, FAKE_TEXT_SIZE, char); + t_name = make_array<char>(FAKE_NAME_SIZE); + t_text = make_array<char>(FAKE_TEXT_SIZE); /*** Load the ascii template file ***/ @@ -1615,10 +1459,6 @@ static errr init_t_info(void) quit("Error in 'tr_info.txt' file."); } - /* Reduce sizes of the arrays */ - z_realloc(&t_name, t_head->name_size); - z_realloc(&t_text, t_head->text_size); - /* Success */ return (0); } @@ -1639,32 +1479,15 @@ errr init_al_info(void) char buf[1024]; /*** Make the header ***/ - - /* Allocate the "header" */ - MAKE(al_head, header); - - /* Save the "record" information */ - al_head->info_num = max_al_idx; - - + al_head = make_header(max_al_idx); /* Allocate the "al_info" array */ - C_MAKE(alchemist_recipes, al_head->info_num, alchemist_recipe); + alchemist_recipes = make_array<alchemist_recipe>(al_head->info_num); /* Allocate the fake arrays */ - /* ok, so we fudge a bit, but - fake text size will ALWAYS be larger - than 32*5*sizeof(artifact_select_flag) = 10 int and 5 bytes - which is the maximum size of the a_select_flags array - */ - C_MAKE(al_name, FAKE_NAME_SIZE, char); - - { - char *hack; - C_MAKE(hack, FAKE_TEXT_SIZE, char); - a_select_flags = (artifact_select_flag *) hack; - } + al_name = make_array<char>(FAKE_NAME_SIZE); + a_select_flags = make_array<artifact_select_flag>(max_al_idx); /*** Load the ascii template file ***/ @@ -1702,9 +1525,6 @@ errr init_al_info(void) quit("Error in 'al_info.txt' file."); } - /* Reduce sizes of the arrays */ - z_realloc(&al_name, al_head->name_size); - /* Success */ return (0); } @@ -1727,22 +1547,17 @@ errr init_v_info(void) /*** Make the header ***/ - - /* Allocate the "header" */ - MAKE(v_head, header); - - /* Save the "record" information */ - v_head->info_num = max_v_idx; + v_head = make_header(max_v_idx); /*** Make the fake arrays ***/ /* Allocate the "k_info" array */ - C_MAKE(v_info, v_head->info_num, vault_type); + v_info = make_array<vault_type>(v_head->info_num); /* Hack -- make "fake" arrays */ - C_MAKE(v_name, FAKE_NAME_SIZE, char); - C_MAKE(v_text, FAKE_TEXT_SIZE, char); + v_name = make_array<char>(FAKE_NAME_SIZE); + v_text = make_array<char>(FAKE_TEXT_SIZE); /*** Load the ascii template file ***/ @@ -1780,10 +1595,6 @@ errr init_v_info(void) quit("Error in 'v_info.txt' file."); } - /* Reduce sizes of the arrays */ - z_realloc(&v_name, v_head->name_size); - z_realloc(&v_text, v_head->text_size); - /* Success */ return (0); } @@ -1796,15 +1607,15 @@ static void init_basic() int i; /* Macro variables */ - C_MAKE(macro__pat, MACRO_MAX, cptr); - C_MAKE(macro__act, MACRO_MAX, cptr); - C_MAKE(macro__cmd, MACRO_MAX, bool_); + macro__pat = make_array<cptr>(MACRO_MAX); + macro__act = make_array<cptr>(MACRO_MAX); + macro__cmd = make_array<bool_>(MACRO_MAX); /* Macro action buffer */ - C_MAKE(macro__buf, 1024, char); + macro__buf = make_array<char>(1024); /* Extended trigger macros */ - C_MAKE(cli_info, CLI_MAX, cli_comm); + cli_info = make_array<cli_comm>(CLI_MAX); /* Wipe the directory list */ for (i = 0; i < 255; i++) @@ -1863,14 +1674,14 @@ static errr init_towns(void) /*** Prepare the Towns ***/ /* Allocate the towns */ - C_MAKE(town_info, max_towns, town_type); + town_info = make_array<town_type>(max_towns); for (i = 1; i < max_towns; i++) { if (i <= max_real_towns) town_info[i].flags |= (TOWN_REAL); /* Allocate the stores */ - C_MAKE(town_info[i].store, max_st_idx, store_type); + town_info[i].store = make_array<store_type>(max_st_idx); /* Fill in each store */ for (j = 0; j < max_st_idx; j++) @@ -1903,7 +1714,7 @@ void create_stores_stock(int t) st_ptr->stock_size = st_info[j].max_obj; /* Allocate the stock */ - C_MAKE(st_ptr->stock, st_ptr->stock_size, object_type); + st_ptr->stock = make_array<object_type>(st_ptr->stock_size); } t_ptr->stocked = TRUE; } @@ -1921,12 +1732,13 @@ static errr init_wilderness(void) int i; /* Allocate the wilderness (two-dimension array) */ - C_MAKE(wild_map, max_wild_y, wilderness_map_ptr); - C_MAKE(wild_map[0], max_wild_x * max_wild_y, wilderness_map); + wild_map = make_array<wilderness_map *>(max_wild_y); /* Init the other pointers */ - for (i = 1; i < max_wild_y; i++) - wild_map[i] = wild_map[0] + i * max_wild_x; + for (i = 0; i < max_wild_y; i++) + { + wild_map[i] = make_array<wilderness_map>(max_wild_x); + } /* No encounter right now */ generate_encounter = FALSE; @@ -1944,9 +1756,9 @@ static errr init_other(void) /*** Prepare the "dungeon" information ***/ /* Allocate and Wipe the special gene flags */ - C_MAKE(m_allow_special, max_r_idx, bool_); - C_MAKE(k_allow_special, max_k_idx, bool_); - C_MAKE(a_allow_special, max_a_idx, bool_); + m_allow_special = make_array<bool_>(max_r_idx); + k_allow_special = make_array<bool_>(max_k_idx); + a_allow_special = make_array<bool_>(max_a_idx); /*** Prepare "vinfo" array ***/ @@ -1956,28 +1768,29 @@ static errr init_other(void) /* Allocate and Wipe the object list */ - C_MAKE(o_list, max_o_idx, object_type); + o_list = make_array<object_type>(max_o_idx); /* Allocate and Wipe the monster list */ - C_MAKE(m_list, max_m_idx, monster_type); + m_list = make_array<monster_type>(max_m_idx); /* Allocate and Wipe the to keep monster list */ - C_MAKE(km_list, max_m_idx, monster_type); + km_list = make_array<monster_type>(max_m_idx); /* Allocate and Wipe the max dungeon level */ - C_MAKE(max_dlv, max_d_idx, s16b); + max_dlv = make_array<s16b>(max_d_idx); /* Allocate and Wipe the special levels */ for (i = 0; i < MAX_DUNGEON_DEPTH; i++) { - C_MAKE(special_lvl[i], max_d_idx, bool_); + special_lvl[i] = make_array<bool_>(max_d_idx); } /* Allocate and wipe each line of the cave */ + cave = make_array<cave_type *>(MAX_HGT); for (i = 0; i < MAX_HGT; i++) { /* Allocate one row of the cave */ - C_MAKE(cave[i], MAX_WID, cave_type); + cave[i] = make_array<cave_type>(MAX_WID); } /*** Pre-allocate the basic "auto-inscriptions" ***/ @@ -2125,7 +1938,7 @@ static errr init_alloc(void) /*** Initialise object allocation info ***/ /* Allocate the alloc_kind_table */ - C_MAKE(alloc_kind_table, alloc_kind_size, alloc_entry); + alloc_kind_table = make_array<alloc_entry>(alloc_kind_size); /* Access the table entry */ table = alloc_kind_table; @@ -2211,7 +2024,7 @@ static errr init_alloc(void) /*** Initialise monster allocation info ***/ /* Allocate the alloc_race_table */ - C_MAKE(alloc_race_table, alloc_race_size, alloc_entry); + alloc_race_table = make_array<alloc_entry>(alloc_race_size); /* Access the table entry */ table = alloc_race_table; diff --git a/src/joke.c b/src/joke.cc index ed07dcd3..ed07dcd3 100644 --- a/src/joke.c +++ b/src/joke.cc diff --git a/src/levels.c b/src/levels.cc index 71148fc5..71148fc5 100644 --- a/src/levels.c +++ b/src/levels.cc diff --git a/src/loadsave.c b/src/loadsave.cc index 818cbb62..3aaf86e7 100644 --- a/src/loadsave.c +++ b/src/loadsave.cc @@ -10,6 +10,8 @@ #include "quark.h" #include "hooks.h" +#include <memory> + static void do_byte(byte *, int); static void do_bool(bool_ *, int); static void do_u16b(u16b *, int); @@ -229,10 +231,10 @@ static char loaded_game_module[80]; static bool_ do_extra(int flag) { int i, j; - byte tmp8u; - s16b tmp16s; - u32b tmp32u; - u16b tmp16b; + byte tmp8u = 0; + s16b tmp16s = 0; + u32b tmp32u = 0; + u16b tmp16b = 0; u32b dummy32u = 0; do_string(player_name, 32, flag); @@ -2449,6 +2451,53 @@ static void do_timers(int flag) } /* + * Load/save stores. + */ +static void do_stores(int flag) +{ + u16b tmp16u; + u16b real_max = 0; + + /* Note that this forbids max_towns from shrinking, but that is fine */ + std::unique_ptr<byte[]> reals(new byte[max_towns]); + + /* Find the real towns */ + if (flag == LS_SAVE) + { + for (int i = 1; i < max_towns; i++) + { + if (!(town_info[i].flags & (TOWN_REAL))) continue; + reals[real_max++] = i; + } + } + do_u16b(&real_max, flag); + for (int i = 0; i < real_max; i++) + { + do_byte((byte*)&reals[i], flag); + } + + /* Read the stores */ + if (flag == LS_SAVE) tmp16u = max_st_idx; + do_u16b(&tmp16u, flag); + assert(tmp16u <= max_st_idx); + + /* Ok now read the real towns */ + for (int i = 0; i < real_max; i++) + { + int z = reals[i]; + + /* Ultra paranoia */ + if (!town_info[z].stocked) create_stores_stock(z); + + for (int j = 0; j < tmp16u; j++) + { + do_store(&town_info[z].store[j], flag); + } + } +} + + +/* * Actually read the savefile */ static bool_ do_savefile_aux(int flag) @@ -2458,9 +2507,6 @@ static bool_ do_savefile_aux(int flag) byte tmp8u; u16b tmp16u; - bool_ *reals; - u16b real_max = 0; - /* Mention the savefile version */ if (flag == LS_LOAD) { @@ -2824,41 +2870,8 @@ static bool_ do_savefile_aux(int flag) return (FALSE); } - /* Note that this forbids max_towns from shrinking, but that is fine */ - C_MAKE(reals, max_towns, bool_); - - /* Find the real towns */ - if (flag == LS_SAVE) - { - for (i = 1; i < max_towns; i++) - { - if (!(town_info[i].flags & (TOWN_REAL))) continue; - reals[real_max++] = i; - } - } - do_u16b(&real_max, flag); - for (i = 0; i < real_max; i++) - { - do_byte((byte*)&reals[i], flag); - } - - /* Read the stores */ - if (flag == LS_SAVE) tmp16u = max_st_idx; - do_u16b(&tmp16u, flag); - - /* Ok now read the real towns */ - for (i = 0; i < real_max; i++) - { - int z = reals[i]; - - /* Ultra paranoia */ - if (!town_info[z].stocked) create_stores_stock(z); - - for (j = 0; j < tmp16u; j++) - do_store(&town_info[z].store[j], flag); - } - - C_FREE(reals, max_towns, bool_); + /* Stores */ + do_stores(flag); /* I'm not dead yet... */ if (!death) diff --git a/src/lua_bind.c b/src/lua_bind.cc index b56f8dd8..b56f8dd8 100644 --- a/src/lua_bind.c +++ b/src/lua_bind.cc diff --git a/src/melee1.c b/src/melee1.cc index cc0f8ee3..cc0f8ee3 100644 --- a/src/melee1.c +++ b/src/melee1.cc diff --git a/src/melee2.c b/src/melee2.cc index 0a139f89..0a139f89 100644 --- a/src/melee2.c +++ b/src/melee2.cc diff --git a/src/messages.c b/src/messages.cc index e88cf58e..32117198 100644 --- a/src/messages.c +++ b/src/messages.cc @@ -1,4 +1,5 @@ #include "messages.h" +#include "tome/make_array.hpp" #include "angband.h" @@ -83,10 +84,10 @@ static char *message__buf; void message_init() { /* Message variables */ - C_MAKE(message__ptr, MESSAGE_MAX, u16b); - C_MAKE(message__color, MESSAGE_MAX, byte); - C_MAKE(message__count, MESSAGE_MAX, u16b); - C_MAKE(message__buf, MESSAGE_BUF, char); + message__ptr = make_array<u16b>(MESSAGE_MAX); + message__color = make_array<byte>(MESSAGE_MAX); + message__count = make_array<u16b>(MESSAGE_MAX); + message__buf = make_array<char>(MESSAGE_BUF); /* Hack -- No messages yet */ message__tail = MESSAGE_BUF; diff --git a/src/mimic.c b/src/mimic.cc index 90c8a62a..90c8a62a 100644 --- a/src/mimic.c +++ b/src/mimic.cc diff --git a/src/modules.c b/src/modules.cc index 7b11b568..7b11b568 100644 --- a/src/modules.c +++ b/src/modules.cc diff --git a/src/monster1.c b/src/monster1.cc index 68882d27..21d2ac15 100644 --- a/src/monster1.c +++ b/src/monster1.cc @@ -523,16 +523,16 @@ static void roff_aux(int r_idx, int ego, int remem) long i, j; /* calculate the integer exp part */ - i = (long)r_ptr->mexp * r_ptr->level / p_ptr->lev; + i = static_cast<long>(r_ptr->mexp) * r_ptr->level / p_ptr->lev; /* calculate the fractional exp part scaled by 100, */ /* must use long arithmetic to avoid overflow */ - j = ((((long)r_ptr->mexp * r_ptr->level % p_ptr->lev) * - (long)1000 / p_ptr->lev + 5) / 10); + j = (((static_cast<long>(r_ptr->mexp) * r_ptr->level % p_ptr->lev) * + 1000L / p_ptr->lev + 5) / 10); /* Mention the experience */ text_out(" is worth "); - text_out_c(TERM_ORANGE, format("%ld.%02ld", (long)i, (long)j)); + text_out_c(TERM_ORANGE, format("%ld.%02ld", i, j)); text_out(" point"); text_out(((i == 1) && (j == 0)) ? "" : "s"); @@ -551,7 +551,7 @@ static void roff_aux(int r_idx, int ego, int remem) /* Mention the dependance on the player's level */ text_out(format(" for a%s %lu%s level character. ", - q, (long)i, p)); + q, i, p)); } } @@ -1087,9 +1087,9 @@ static void roff_aux(int r_idx, int ego, int remem) /* Do we know how aware it is? */ - if ((((int)r_ptr->r_wake * (int)r_ptr->r_wake) > r_ptr->sleep) || - (r_ptr->r_ignore == MAX_UCHAR) || - ((r_ptr->sleep == 0) && (r_ptr->r_tkills >= 10))) + if (((static_cast<int>(r_ptr->r_wake) * static_cast<int>(r_ptr->r_wake)) > r_ptr->sleep) || + (r_ptr->r_ignore == MAX_UCHAR) || + ((r_ptr->sleep == 0) && (r_ptr->r_tkills >= 10))) { cptr act; @@ -1517,7 +1517,7 @@ static void roff_aux(int r_idx, int ego, int remem) if ((cheat_know) && (remem == 0)) { /* Hack -- restore memory */ - COPY(r_ptr, &save_mem, monster_race); + *r_ptr = save_mem; } } diff --git a/src/monster2.c b/src/monster2.cc index d09f5cf6..6ffc4649 100644 --- a/src/monster2.c +++ b/src/monster2.cc @@ -493,7 +493,7 @@ void delete_monster_idx(int i) } /* Wipe the Monster */ - m_ptr = WIPE(m_ptr, monster_type); + WIPE(m_ptr, monster_type); /* Count monsters */ m_cnt--; @@ -611,7 +611,7 @@ static void compact_monsters_aux(int i1, int i2) COPY(&m_list[i2], &m_list[i1], monster_type); /* Wipe the hole */ - m_ptr = WIPE(&m_list[i1], monster_type); + WIPE(&m_list[i1], monster_type); } @@ -729,7 +729,7 @@ void wipe_m_list(void) cave[m_ptr->fy][m_ptr->fx].m_idx = 0; /* Wipe the Monster */ - m_ptr = WIPE(m_ptr, monster_type); + WIPE(m_ptr, monster_type); } /* Reset "m_max" */ diff --git a/src/monster3.c b/src/monster3.cc index a2b5fb38..a2b5fb38 100644 --- a/src/monster3.c +++ b/src/monster3.cc diff --git a/src/notes.c b/src/notes.cc index 3504f61c..3504f61c 100644 --- a/src/notes.c +++ b/src/notes.cc diff --git a/src/object1.c b/src/object1.cc index 2a555581..2a555581 100644 --- a/src/object1.c +++ b/src/object1.cc diff --git a/src/object2.c b/src/object2.cc index 3e55ed00..59037ab1 100644 --- a/src/object2.c +++ b/src/object2.cc @@ -16,6 +16,7 @@ #include "device_allocation.h" #include "hooks.h" +#include <vector> /* * Calculate the player's total inventory weight. @@ -533,7 +534,7 @@ void wipe_o_list(void) } /* Wipe the object */ - o_ptr = WIPE(o_ptr, object_type); + WIPE(o_ptr, object_type); } /* Reset "o_max" */ @@ -1907,7 +1908,7 @@ s16b lookup_kind(int tval, int sval) void object_wipe(object_type *o_ptr) { /* Wipe the structure */ - o_ptr = WIPE(o_ptr, object_type); + WIPE(o_ptr, object_type); } @@ -1929,7 +1930,7 @@ void object_prep(object_type *o_ptr, int k_idx) object_kind *k_ptr = &k_info[k_idx]; /* Clear the record */ - o_ptr = WIPE(o_ptr, object_type); + WIPE(o_ptr, object_type); /* Save the kind index */ o_ptr->k_idx = k_idx; @@ -2387,13 +2388,12 @@ static bool_ make_artifact(object_type *o_ptr) static bool_ make_ego_item(object_type *o_ptr, bool_ good) { int i = 0, j; - int *ok_ego, ok_num = 0; bool_ ret = FALSE; object_kind *k_ptr = &k_info[o_ptr->k_idx]; if (artifact_p(o_ptr) || o_ptr->name2) return (FALSE); - C_MAKE(ok_ego, max_e_idx, int); + std::vector<int> ok_ego; /* Grab the ok ego */ for (i = 0; i < max_e_idx; i++) @@ -2441,15 +2441,15 @@ static bool_ make_ego_item(object_type *o_ptr, bool_ good) continue; /* ok */ - ok_ego[ok_num++] = i; + ok_ego.push_back(i); } /* Now test them a few times */ - for (i = 0; i < ok_num * 10; i++) + for (i = 0; i < ok_ego.size() * 10; i++) { ego_item_type *e_ptr; - int j = ok_ego[rand_int(ok_num)]; + int j = ok_ego[rand_int(ok_ego.size())]; e_ptr = &e_info[j]; /* XXX XXX Enforce minimum "depth" (loosely) */ @@ -2486,11 +2486,11 @@ static bool_ make_ego_item(object_type *o_ptr, bool_ good) if (magik(7 + luck( -7, 7)) && (!o_ptr->name2b)) { /* Now test them a few times */ - for (i = 0; i < ok_num * 10; i++) + for (i = 0; i < ok_ego.size() * 10; i++) { ego_item_type *e_ptr; - int j = ok_ego[rand_int(ok_num)]; + int j = ok_ego[rand_int(ok_ego.size())]; e_ptr = &e_info[j]; /* Cannot be a double ego of the same ego type */ @@ -2528,8 +2528,6 @@ static bool_ make_ego_item(object_type *o_ptr, bool_ good) } } - C_FREE(ok_ego, max_e_idx, int); - /* Return */ return (ret); } diff --git a/src/powers.c b/src/powers.cc index d6ecdecf..d6ecdecf 100644 --- a/src/powers.c +++ b/src/powers.cc diff --git a/src/q_betwen.c b/src/q_betwen.cc index 6f4072ca..6f4072ca 100644 --- a/src/q_betwen.c +++ b/src/q_betwen.cc diff --git a/src/q_bounty.c b/src/q_bounty.cc index b7cd150e..b7cd150e 100644 --- a/src/q_bounty.c +++ b/src/q_bounty.cc diff --git a/src/q_dragons.c b/src/q_dragons.cc index e63f662a..e63f662a 100644 --- a/src/q_dragons.c +++ b/src/q_dragons.cc diff --git a/src/q_eol.c b/src/q_eol.cc index 4f0aa498..4f0aa498 100644 --- a/src/q_eol.c +++ b/src/q_eol.cc diff --git a/src/q_evil.c b/src/q_evil.cc index 64a9fabc..64a9fabc 100644 --- a/src/q_evil.c +++ b/src/q_evil.cc diff --git a/src/q_fireprof.c b/src/q_fireprof.cc index a515335f..a515335f 100644 --- a/src/q_fireprof.c +++ b/src/q_fireprof.cc diff --git a/src/q_god.c b/src/q_god.cc index 3310c214..3310c214 100644 --- a/src/q_god.c +++ b/src/q_god.cc diff --git a/src/q_haunted.c b/src/q_haunted.cc index 83f97cb2..83f97cb2 100644 --- a/src/q_haunted.c +++ b/src/q_haunted.cc diff --git a/src/q_hobbit.c b/src/q_hobbit.cc index a6a1f140..a6a1f140 100644 --- a/src/q_hobbit.c +++ b/src/q_hobbit.cc diff --git a/src/q_invas.c b/src/q_invas.cc index 10cb5dc6..10cb5dc6 100644 --- a/src/q_invas.c +++ b/src/q_invas.cc diff --git a/src/q_library.c b/src/q_library.cc index b54a9e48..b54a9e48 100644 --- a/src/q_library.c +++ b/src/q_library.cc diff --git a/src/q_main.c b/src/q_main.cc index f0e6214e..f0e6214e 100644 --- a/src/q_main.c +++ b/src/q_main.cc diff --git a/src/q_narsil.c b/src/q_narsil.cc index 521e344b..521e344b 100644 --- a/src/q_narsil.c +++ b/src/q_narsil.cc diff --git a/src/q_nazgul.c b/src/q_nazgul.cc index 5573b40b..5573b40b 100644 --- a/src/q_nazgul.c +++ b/src/q_nazgul.cc diff --git a/src/q_nirna.c b/src/q_nirna.cc index d9e178c3..d9e178c3 100644 --- a/src/q_nirna.c +++ b/src/q_nirna.cc diff --git a/src/q_one.c b/src/q_one.cc index de80afad..de80afad 100644 --- a/src/q_one.c +++ b/src/q_one.cc diff --git a/src/q_poison.c b/src/q_poison.cc index a63dca96..a63dca96 100644 --- a/src/q_poison.c +++ b/src/q_poison.cc diff --git a/src/q_rand.c b/src/q_rand.cc index 84d721b8..6d8f7e49 100644 --- a/src/q_rand.c +++ b/src/q_rand.cc @@ -1,5 +1,6 @@ #include "q_rand.h" #include "hooks.h" +#include "util.hpp" static int randquest_hero[] = { 20, 13, 15, 16, 9, 17, 18, 8, -1 }; @@ -23,19 +24,18 @@ bool_ is_randhero(int level) static void do_get_new_obj(int y, int x) { obj_theme theme; - char *items[3]; object_type *q_ptr[3], forge[3]; - int max = 0, res, i; + int res, i; - /* Create 3 ones */ - max = 0; + /* Create objects */ + std::vector<std::string> items; for (i = 0; i < 3; i++) { /* Get local object */ - q_ptr[max] = &forge[max]; + q_ptr[i] = &forge[i]; /* Wipe the object */ - object_wipe(q_ptr[max]); + object_wipe(q_ptr[i]); /* No themes */ theme.treasure = 100; @@ -44,18 +44,18 @@ static void do_get_new_obj(int y, int x) theme.tools = 100; /* Make a great object */ - make_object(q_ptr[max], TRUE, TRUE, theme); - q_ptr[max]->found = OBJ_FOUND_REWARD; + make_object(q_ptr[i], TRUE, TRUE, theme); + q_ptr[i]->found = OBJ_FOUND_REWARD; - C_MAKE(items[max], 100, char); - object_desc(items[max], q_ptr[max], 0, 0); - max++; + char buf[100]; + object_desc(buf, q_ptr[i], 0, 0); + items.push_back(buf); } while (TRUE) { - res = ask_menu("Choose a reward to get(a-c to choose, ESC to cancel)?", (char **)items, 3); + res = ask_menu("Choose a reward to get(a-c to choose, ESC to cancel)?", items); /* Ok ? lets learn ! */ if (res > -1) @@ -91,10 +91,6 @@ static void do_get_new_obj(int y, int x) } } } - - for (i = 0; i < 3; i++) - C_KILL(items[i], 100, char); - } static void princess_death(s32b m_idx, s32b r_idx) diff --git a/src/q_shroom.c b/src/q_shroom.cc index d402c818..d402c818 100644 --- a/src/q_shroom.c +++ b/src/q_shroom.cc diff --git a/src/q_spider.c b/src/q_spider.cc index 03305f59..03305f59 100644 --- a/src/q_spider.c +++ b/src/q_spider.cc diff --git a/src/q_thief.c b/src/q_thief.cc index 912309a9..912309a9 100644 --- a/src/q_thief.c +++ b/src/q_thief.cc diff --git a/src/q_thrain.c b/src/q_thrain.cc index c22b39e9..c22b39e9 100644 --- a/src/q_thrain.c +++ b/src/q_thrain.cc diff --git a/src/q_troll.c b/src/q_troll.cc index 4a38f2d0..4a38f2d0 100644 --- a/src/q_troll.c +++ b/src/q_troll.cc diff --git a/src/q_ultrae.c b/src/q_ultrae.cc index 44f54158..44f54158 100644 --- a/src/q_ultrae.c +++ b/src/q_ultrae.cc diff --git a/src/q_ultrag.c b/src/q_ultrag.cc index 0eb50d15..0eb50d15 100644 --- a/src/q_ultrag.c +++ b/src/q_ultrag.cc diff --git a/src/q_wight.c b/src/q_wight.cc index ce4c5ac4..ce4c5ac4 100644 --- a/src/q_wight.c +++ b/src/q_wight.cc diff --git a/src/q_wolves.c b/src/q_wolves.cc index 5da28ee7..5da28ee7 100644 --- a/src/q_wolves.c +++ b/src/q_wolves.cc diff --git a/src/quark.c b/src/quark.cc index 32efaaa0..1c8619fa 100644 --- a/src/quark.c +++ b/src/quark.cc @@ -20,7 +20,11 @@ static cptr *quark__str = NULL; void quark_init() { quark__num = 0; - C_MAKE(quark__str, QUARK_MAX, cptr); + quark__str = new cptr[QUARK_MAX]; + + for (int i = 0; i < QUARK_MAX; i++) { + quark__str[i] = nullptr; + } } diff --git a/src/randart.c b/src/randart.cc index 10fd51b0..43f7d256 100644 --- a/src/randart.c +++ b/src/randart.cc @@ -14,6 +14,9 @@ #include "quark.h" +#include <memory> +#include <vector> + /* Chance of using syllables to form the name instead of the "template" files */ #define TABLE_NAME 45 #define A_CURSED 13 @@ -26,11 +29,10 @@ static bool_ grab_one_power(int *ra_idx, object_type *o_ptr, bool_ good, s16b *max_times) { int i = 0, j; - int *ok_ra, ok_num = 0; bool_ ret = FALSE; u32b f1, f2, f3, f4, f5, esp; - C_MAKE(ok_ra, max_ra_idx, int); + std::vector<int> ok_ra; /* Grab the ok randart */ for (i = 0; i < max_ra_idx; i++) @@ -71,15 +73,15 @@ static bool_ grab_one_power(int *ra_idx, object_type *o_ptr, bool_ good, s16b *m if (esp & ra_ptr->aesp) continue; /* ok */ - ok_ra[ok_num++] = i; + ok_ra.push_back(i); } /* Now test them a few times */ - for (i = 0; i < ok_num * 10; i++) + for (i = 0; i < ok_ra.size() * 10; i++) { randart_part_type *ra_ptr; - i = ok_ra[rand_int(ok_num)]; + i = ok_ra[rand_int(ok_ra.size())]; ra_ptr = &ra_info[i]; /* XXX XXX Enforce minimum player level (loosely) */ @@ -110,8 +112,6 @@ static bool_ grab_one_power(int *ra_idx, object_type *o_ptr, bool_ good, s16b *m break; } - C_FREE(ok_ra, max_ra_idx, int); - /* Return */ return (ret); } @@ -264,7 +264,6 @@ bool_ create_artifact(object_type *o_ptr, bool_ a_scroll, bool_ get_name) s32b total_flags, total_power = 0; bool_ a_cursed = FALSE; u32b f1, f2, f3, f4, f5, esp; - s16b *max_times; s16b pval = 0; bool_ limit_blows = FALSE; @@ -283,7 +282,10 @@ bool_ create_artifact(object_type *o_ptr, bool_ a_scroll, bool_ get_name) if (a_cursed) powers /= 2; - C_MAKE(max_times, max_ra_idx, s16b); + std::unique_ptr<s16b[]> max_times(new s16b[max_ra_idx]); + for (int i = 0; i < max_ra_idx; i++) { + max_times[i] = 0; + } /* Main loop */ while (powers) @@ -293,7 +295,7 @@ bool_ create_artifact(object_type *o_ptr, bool_ a_scroll, bool_ get_name) powers--; - if (!grab_one_power(&ra_idx, o_ptr, TRUE, max_times)) continue; + if (!grab_one_power(&ra_idx, o_ptr, TRUE, max_times.get())) continue; ra_ptr = &ra_info[ra_idx]; @@ -327,7 +329,6 @@ bool_ create_artifact(object_type *o_ptr, bool_ a_scroll, bool_ get_name) /* Hack -- obtain pval */ if (((pval > ra_ptr->max_pval) && ra_ptr->max_pval) || (!pval)) pval = ra_ptr->max_pval; }; - C_FREE(max_times, max_ra_idx, s16b); if (pval > 0) o_ptr->pval = randint(pval); if (pval < 0) o_ptr->pval = randint( -pval); diff --git a/src/range.c b/src/range.cc index 0cb21469..0cb21469 100644 --- a/src/range.c +++ b/src/range.cc diff --git a/src/script.c b/src/script.cc index a9407352..a9407352 100644 --- a/src/script.c +++ b/src/script.cc diff --git a/src/skills.c b/src/skills.cc index 900b6452..221a5e27 100644 --- a/src/skills.c +++ b/src/skills.cc @@ -14,12 +14,20 @@ #include <math.h> #include "hooks.h" +#include "util.hpp" + +#include <algorithm> +#include <cassert> +#include <memory> +#include <vector> +#include <tuple> + /* * Advance the skill point of the skill specified by i and * modify related skills */ -void increase_skill(int i, s16b *invest) +static void increase_skill(int i, s16b *invest) { s32b max_skill_overage; @@ -93,7 +101,10 @@ s16b find_skill(cptr name) for (i = 1; i < max_s_idx; i++) { /* The name matches */ - if (streq(s_info[i].name + s_name, name)) return (i); + if (s_info[i].name > 0) + { + if (streq(s_info[i].name + s_name, name)) return (i); + } } /* No match found */ @@ -107,7 +118,9 @@ s16b find_skill_i(cptr name) for (i = 1; i < max_s_idx; i++) { /* The name matches */ - if (0 == stricmp(s_info[i].name + s_name, name)) return (i); + if (s_info[i].name > 0) { + if (0 == stricmp(s_info[i].name + s_name, name)) return (i); + } } /* No match found */ @@ -266,8 +279,8 @@ void dump_skills(FILE *fff) fprintf(fff, "%-49s%s%06.3f [%05.3f]", buf, s_info[i].value < 0 ? "-" : " ", - ((double) ABS(s_info[i].value)) / SKILL_STEP, - ((double) s_info[i].mod) / 1000); + static_cast<double>(ABS(s_info[i].value)) / SKILL_STEP, + static_cast<double>(s_info[i].mod) / 1000); } fprintf(fff, "\n"); @@ -396,7 +409,7 @@ void recalc_skills(bool_ init) /* * Recalc the skill value */ -void recalc_skills_theory(s16b *invest, s32b *base_val, s32b *base_mod, s32b *bonus) +static void recalc_skills_theory(s16b *invest, s32b *base_val, s32b *base_mod, s32b *bonus) { int i, j; @@ -454,11 +467,6 @@ void do_cmd_skill() int i; int wid, hgt; s16b skill_points_save; - s32b *skill_values_save; - s32b *skill_mods_save; - s16b *skill_rates_save; - s16b *skill_invest; - s32b *skill_bonus; recalc_skills(TRUE); @@ -466,11 +474,11 @@ void do_cmd_skill() screen_save(); /* Allocate arrays to save skill values */ - C_MAKE(skill_values_save, MAX_SKILLS, s32b); - C_MAKE(skill_mods_save, MAX_SKILLS, s32b); - C_MAKE(skill_rates_save, MAX_SKILLS, s16b); - C_MAKE(skill_invest, MAX_SKILLS, s16b); - C_MAKE(skill_bonus, MAX_SKILLS, s32b); + std::unique_ptr<s32b[]> skill_values_save(new s32b[MAX_SKILLS]); + std::unique_ptr<s32b[]> skill_mods_save(new s32b[MAX_SKILLS]); + std::unique_ptr<s16b[]> skill_rates_save(new s16b[MAX_SKILLS]); + std::unique_ptr<s16b[]> skill_invest(new s16b[MAX_SKILLS]); + std::unique_ptr<s32b[]> skill_bonus(new s32b[MAX_SKILLS]); /* Save skill points */ skill_points_save = p_ptr->skill_points; @@ -484,6 +492,7 @@ void do_cmd_skill() skill_mods_save[i] = s_ptr->mod; skill_rates_save[i] = s_ptr->rate; skill_invest[i] = 0; + skill_bonus[i] = 0; } /* Clear the screen */ @@ -497,7 +506,7 @@ void do_cmd_skill() Term_get_size(&wid, &hgt); /* Display list of skills */ - recalc_skills_theory(skill_invest, skill_values_save, skill_mods_save, skill_bonus); + recalc_skills_theory(skill_invest.get(), skill_values_save.get(), skill_mods_save.get(), skill_bonus.get()); print_skills(table, max, sel, start); /* Wait for user input */ @@ -546,10 +555,10 @@ void do_cmd_skill() if (table[sel][0] == SKILL_MISC) continue; /* Increase the current skill */ - if (dir == 6) increase_skill(table[sel][0], skill_invest); + if (dir == 6) increase_skill(table[sel][0], skill_invest.get()); /* Decrease the current skill */ - if (dir == 4) decrease_skill(table[sel][0], skill_invest); + if (dir == 4) decrease_skill(table[sel][0], skill_invest.get()); /* XXX XXX XXX Wizard mode commands outside of wizard2.c */ @@ -600,14 +609,6 @@ void do_cmd_skill() } } - - /* Free arrays to save skill values */ - C_FREE(skill_values_save, MAX_SKILLS, s32b); - C_FREE(skill_mods_save, MAX_SKILLS, s32b); - C_FREE(skill_rates_save, MAX_SKILLS, s16b); - C_FREE(skill_invest, MAX_SKILLS, s16b); - C_FREE(skill_bonus, MAX_SKILLS, s32b); - /* Load the screen */ screen_load(); @@ -625,7 +626,7 @@ s16b melee_skills[MAX_MELEE] = SKILL_HAND, SKILL_BEAR, }; -char *melee_names[MAX_MELEE] = +const char *melee_names[MAX_MELEE] = { "Weapon combat", "Barehanded combat", @@ -761,7 +762,7 @@ void select_default_melee() /* * Print a batch of skills. */ -static void print_skill_batch(int *p, cptr *p_desc, int start, int max) +static void print_skill_batch(const std::vector<std::tuple<cptr, int>> &p, int start) { char buff[80]; int i = start, j = 0; @@ -770,12 +771,14 @@ static void print_skill_batch(int *p, cptr *p_desc, int start, int max) for (i = start; i < (start + 20); i++) { - if (i >= max) break; + if (i >= p.size()) + { + break; + } - if (p[i] > 0) - sprintf(buff, " %c - %d) %-30s", I2A(j), p[i], p_desc[i]); - else - sprintf(buff, " %c - %d) %-30s", I2A(j), p[i], "Change melee style"); + sprintf(buff, " %c - %d) %-30s", I2A(j), + std::get<1>(p[i]), + std::get<0>(p[i])); prt(buff, 2 + j, 20); j++; @@ -787,21 +790,15 @@ static void print_skill_batch(int *p, cptr *p_desc, int start, int max) int do_cmd_activate_skill_aux() { char which; - int max = 0, i, start = 0; + int i, start = 0; int ret; - int *p; - cptr *p_desc; - - C_MAKE(p, max_s_idx + max_ab_idx, int); - C_MAKE(p_desc, max_s_idx + max_ab_idx, cptr); - /* Count the max */ + std::vector<std::tuple<cptr,int>> p; /* More than 1 melee skill ? */ if (get_melee_skills() > 1) { - p_desc[max] = "Change melee mode"; - p[max++] = 0; + p.push_back(std::make_tuple("Change melee mode", 0)); } for (i = 1; i < max_s_idx; i++) @@ -812,9 +809,9 @@ int do_cmd_activate_skill_aux() bool_ next = FALSE; /* Already got it ? */ - for (j = 0; j < max; j++) + for (j = 0; j < p.size(); j++) { - if (s_info[i].action_mkey == p[j]) + if (s_info[i].action_mkey == std::get<1>(p[j])) { next = TRUE; break; @@ -822,8 +819,8 @@ int do_cmd_activate_skill_aux() } if (next) continue; - p_desc[max] = s_text + s_info[i].action_desc; - p[max++] = s_info[i].action_mkey; + p.push_back(std::make_tuple(s_text + s_info[i].action_desc, + s_info[i].action_mkey)); } } @@ -835,9 +832,9 @@ int do_cmd_activate_skill_aux() bool_ next = FALSE; /* Already got it ? */ - for (j = 0; j < max; j++) + for (j = 0; j < p.size(); j++) { - if (ab_info[i].action_mkey == p[j]) + if (ab_info[i].action_mkey == std::get<1>(p[j])) { next = TRUE; break; @@ -845,12 +842,12 @@ int do_cmd_activate_skill_aux() } if (next) continue; - p_desc[max] = ab_text + ab_info[i].action_desc; - p[max++] = ab_info[i].action_mkey; + p.push_back(std::make_tuple(ab_text + ab_info[i].action_desc, + ab_info[i].action_mkey)); } } - if (!max) + if (p.empty()) { msg_print("You don't have any activable skills or abilities."); return -1; @@ -861,7 +858,7 @@ int do_cmd_activate_skill_aux() while (1) { - print_skill_batch(p, p_desc, start, max); + print_skill_batch(p, start); which = inkey(); if (which == ESCAPE) @@ -872,7 +869,7 @@ int do_cmd_activate_skill_aux() else if (which == '+') { start += 20; - if (start >= max) start -= 20; + if (start >= p.size()) start -= 20; Term_load(); character_icky = FALSE; } @@ -892,14 +889,14 @@ int do_cmd_activate_skill_aux() return FALSE; /* Find the skill it is related to */ - for (i = 0; i < max; i++) + for (i = 0; i < p.size(); i++) { - if (!strcmp(buf, p_desc[i])) + if (!strcmp(buf, std::get<0>(p[i]))) break; } - if ((i < max)) + if ((i < p.size())) { - ret = p[i]; + ret = std::get<1>(p[i]); break; } @@ -907,7 +904,7 @@ int do_cmd_activate_skill_aux() else { which = tolower(which); - if (start + A2I(which) >= max) + if (start + A2I(which) >= p.size()) { bell(); continue; @@ -918,16 +915,13 @@ int do_cmd_activate_skill_aux() continue; } - ret = p[start + A2I(which)]; + ret = std::get<1>(p[start + A2I(which)]); break; } } Term_load(); character_icky = FALSE; - C_FREE(p, max_s_idx + max_ab_idx, int); - C_FREE(p_desc, max_s_idx + max_ab_idx, cptr); - return ret; } @@ -1250,61 +1244,53 @@ void init_skill(s32b value, s32b mod, int i) * the algorithm given in "Weighted Random Sampling" (2005, Eframidis, * Spirakis) * - * @param n is the total number of items to choose from. - * @param k is the total number of items to choose. + * @param k is the total number of items to choose. This MUST be smaller than or equal to the number of weights. * @param weights is the array of weights. - * @param indexes is the output array containing the chosen permutation. - * The first k values will be set to a value in [0, n[ according to - * which item was chosen. + * @return an output vector of size k containing the chosen indices. */ -static void wrs_without_replacement(size_t n, size_t k, s32b unscaled_weights[], size_t indexes[]) +static std::vector<size_t> wrs_without_replacement(size_t k, const std::vector<s32b> &unscaled_weights) { - size_t i; - s32b scale; - double *weights = NULL; - double *keys = NULL; - size_t *permutation = NULL; + size_t n = unscaled_weights.size(); assert(k <= n); - /* Allocate working memory */ - keys = C_RNEW(n, double); - weights = C_RNEW(n, double); - permutation = C_RNEW(n, double); - - /* Calculate the scale of the weights. */ - scale = 0; - for (i = 0; i < n; i++) + /* Rescale weights into unit interval for numerical stability */ + std::vector<double> weights(unscaled_weights.size()); { - scale += unscaled_weights[i]; - } + s32b scale = 0; + for (s32b weight: unscaled_weights) + { + scale += weight; + } - /* Rescale weights into unit interval for numerical stability */ - for (i = 0; i < n; i++) { - weights[i] = - ((double) unscaled_weights[i]) / - ((double) scale); + for (size_t i = 0; i < n; i++) { + weights[i] = + ((double) unscaled_weights[i]) / + ((double) scale); + } } /* Generate the keys to use for selection. This is where the - magic happens. */ - for (i = 0; i < n; i++) { + magic happens. */ + std::vector<double> keys(unscaled_weights.size()); + for (size_t i = 0; i < n; i++) { double u = ((double) rand_int(100000)) / ((double) 100000); keys[i] = pow(u, 1/weights[i]); } /* Generate the initial permutation */ - for (i = 0; i < n; i++) { + std::vector<size_t> permutation(unscaled_weights.size()); + for (size_t i = 0; i < n; i++) { permutation[i] = i; } /* Select the k indexes with the largest keys */ - for (i = 0; i < k; i++) { + std::vector<size_t> indexes; + for (size_t i = 0; i < k; i++) { /* Find maximal value and its index */ - int max_idx = i; + size_t max_idx = i; double max_value = keys[max_idx]; - size_t j; - for (j = i+1; j < n; j++) { + for (size_t j = i + 1; j < n; j++) { if (keys[j] > max_value) { max_idx = j; max_value = keys[j]; @@ -1313,37 +1299,28 @@ static void wrs_without_replacement(size_t n, size_t k, s32b unscaled_weights[], /* Swap into k'th position */ if (max_idx != i) { - double tmp_key; - size_t tmp_idx; /* Swap keys */ - tmp_key = keys[i]; - keys[i] = keys[max_idx]; - keys[max_idx] = tmp_key; + std::swap(keys[i], keys[max_idx]); /* Swap indexes in permutation */ - tmp_idx = permutation[i]; - permutation[i] = permutation[max_idx]; - permutation[max_idx] = tmp_idx; + std::swap(permutation[i], permutation[max_idx]); } - /* Output the k'th choice. */ - indexes[i] = permutation[i]; + /* Output the k'th choice. We can do this already + since we'll never revisit the i'th position in + permutation vector. */ + indexes.push_back(permutation[i]); } - /* Clean up */ - C_FREE(keys, n, double); - C_FREE(weights, n, double); - C_FREE(permutation, n, size_t); + return indexes; } void do_get_new_skill() { - char *items[LOST_SWORD_NSKILLS]; + std::vector<std::string> items; int skl[LOST_SWORD_NSKILLS]; s32b val[LOST_SWORD_NSKILLS], mod[LOST_SWORD_NSKILLS]; int available_skills[MAX_SKILLS]; int max_a = 0, res, i; - size_t indexes[LOST_SWORD_NSKILLS]; - s32b weights[MAX_SKILLS]; /* Check if some skills didn't influence other stuff */ recalc_skills(TRUE); @@ -1359,11 +1336,14 @@ void do_get_new_skill() } /* Perform the selection */ + std::vector<s32b> weights; for (i = 0; i < max_a; i++) { - weights[i] = s_info[available_skills[i]].random_gain_chance; + weights.push_back(s_info[available_skills[i]].random_gain_chance); } - wrs_without_replacement(max_a, LOST_SWORD_NSKILLS, weights, indexes); + std::vector<size_t> indexes = + wrs_without_replacement(LOST_SWORD_NSKILLS, weights); + assert(indexes.size() == LOST_SWORD_NSKILLS); /* Extract the information needed from the skills */ for (i = 0; i < LOST_SWORD_NSKILLS; i++) @@ -1402,7 +1382,7 @@ void do_get_new_skill() } skl[i] = s_idx; - items[i] = (char *)string_make(format("%-40s: +%02ld.%03ld value, +%01d.%03d modifier", s_ptr->name + s_name, val[i] / SKILL_STEP, val[i] % SKILL_STEP, mod[i] / SKILL_STEP, mod[i] % SKILL_STEP)); + items.push_back(format("%-40s: +%02ld.%03ld value, +%01d.%03d modifier", s_ptr->name + s_name, val[i] / SKILL_STEP, val[i] % SKILL_STEP, mod[i] / SKILL_STEP, mod[i] % SKILL_STEP)); } while (TRUE) @@ -1410,7 +1390,7 @@ void do_get_new_skill() char last = 'a' + (LOST_SWORD_NSKILLS-1); char buf[80]; sprintf(buf, "Choose a skill to learn(a-%c to choose, ESC to cancel)?", last); - res = ask_menu(buf, (char **)items, LOST_SWORD_NSKILLS); + res = ask_menu(buf, items); /* Ok ? lets learn ! */ if (res > -1) @@ -1468,12 +1448,6 @@ void do_get_new_skill() } } - /* Free them ! */ - for (i = 0; i < LOST_SWORD_NSKILLS; i++) - { - string_free(items[i]); - } - /* Check if some skills didn't influence other stuff */ recalc_skills(FALSE); } @@ -1495,7 +1469,9 @@ s16b find_ability(cptr name) for (i = 0; i < max_ab_idx; i++) { /* The name matches */ - if (streq(ab_info[i].name + ab_name, name)) return (i); + if (ab_info[i].name > 0) { + if (streq(ab_info[i].name + ab_name, name)) return (i); + } } /* No match found */ @@ -1588,28 +1564,10 @@ void gain_ability(int ab) p_ptr->skill_points -= ab_info[ab].cost; } -/* helper function to generate a sorted table */ -static void add_sorted_ability(int *table, int *max, int ab) +static bool compare_abilities(const int ab_idx1, const int ab_idx2) { - int i; - - for (i = 0; i < *max; i++) - { - if (strcmp(ab_name + ab_info[ab].name, ab_name + ab_info[table[i]].name) < 0) - { - int z; - - /* Move all indexes up */ - for (z = *max; z > i; z--) - { - table[z] = table[z - 1]; - } - break; - } - } - table[i] = ab; - - (*max)++; + return strcmp(ab_name + ab_info[ab_idx1].name, + ab_name + ab_info[ab_idx2].name) < 0; } /* @@ -1617,27 +1575,30 @@ static void add_sorted_ability(int *table, int *max, int ab) */ void dump_abilities(FILE *fff) { - int i, j; - int *table; - int max = 0; - - C_MAKE(table, max_ab_idx, int); + int i; - /* Initialise the abilities list */ + // Find all abilities that the player has. + std::vector<int> table; for (i = 0; i < max_ab_idx; i++) { if (ab_info[i].name && has_ability(i)) - add_sorted_ability(table, &max, i); + { + table.push_back(i); + } } - if (max) + // Sort + std::sort(std::begin(table), + std::end(table), + compare_abilities); + + // Show + if (!table.empty()) { fprintf(fff, "\nAbilities"); - for (j = 0; j < max; j++) + for (int i : table) { - i = table[j]; - fprintf(fff, "\n * %s", ab_info[i].name + ab_name); } @@ -1648,7 +1609,7 @@ void dump_abilities(FILE *fff) /* * Draw the abilities list */ -void print_abilities(int table[], int max, int sel, int start) +static void print_abilities(const std::vector<int> &table, int sel, int start) { int i, j; int wid, hgt; @@ -1670,7 +1631,10 @@ void print_abilities(int table[], int max, int sel, int start) byte color = TERM_WHITE; char deb = ' ', end = ' '; - if (j >= max) break; + if (j >= table.size()) + { + break; + } i = table[j]; @@ -1708,14 +1672,11 @@ void print_abilities(int table[], int max, int sel, int start) */ void do_cmd_ability() { - int sel = 0, start = 0, max = 0; + int sel = 0, start = 0; char c; - int *table; int i; int wid, hgt; - C_MAKE(table, max_ab_idx, int); - /* Save the screen */ screen_save(); @@ -1723,30 +1684,43 @@ void do_cmd_ability() Term_clear(); /* Initialise the abilities list */ + std::vector<int> table; for (i = 0; i < max_ab_idx; i++) { -if (ab_info[i].name) - add_sorted_ability(table, &max, i); + if (ab_info[i].name) + { + table.push_back(i); + } } + std::sort(std::begin(table), + std::end(table), + compare_abilities); + while (TRUE) { Term_get_size(&wid, &hgt); /* Display list of skills */ - print_abilities(table, max, sel, start); + print_abilities(table, sel, start); /* Wait for user input */ c = inkey(); /* Leave the skill screen */ - if (c == ESCAPE) break; + if (c == ESCAPE) + { + break; + } /* Next page */ else if (c == 'n') { sel += (hgt - 7); - if (sel >= max) sel = max - 1; + if (sel >= table.size()) + { + sel = table.size() - 1; + } } /* Previous page */ @@ -1785,8 +1759,8 @@ if (ab_info[i].name) } /* Handle boundaries and scrolling */ - if (sel < 0) sel = max - 1; - if (sel >= max) sel = 0; + if (sel < 0) sel = table.size() - 1; + if (sel >= table.size()) sel = 0; if (sel < start) start = sel; if (sel >= start + (hgt - 7)) start = sel - (hgt - 7) + 1; } @@ -1795,8 +1769,6 @@ if (ab_info[i].name) /* Load the screen */ screen_load(); - C_FREE(table, max_ab_idx, int); - /* Update stuffs */ p_ptr->update |= (PU_BONUS | PU_HP | PU_MANA | PU_SPELLS | PU_POWERS | PU_SANITY | PU_BODY); diff --git a/src/spell_type.c b/src/spell_type.cc index 9948ba8e..33de3d7a 100644 --- a/src/spell_type.c +++ b/src/spell_type.cc @@ -6,6 +6,8 @@ #include "angband.h" +#include <type_traits> + #define SCHOOL_IDXS_MAX 3 /** @@ -24,14 +26,14 @@ struct spell_type s16b minimum_pval; /* Minimum required pval for item-based spells */ - casting_type casting_type; /* Type of casting required */ + enum casting_type casting_type; /* Type of casting required */ s16b casting_stat; /* Stat used for casting */ bool_ castable_while_blind; bool_ castable_while_confused; dice_type device_charges; /* Number of charges for devices */ - device_allocation *device_allocation; /* Allocation table for devices */ + struct device_allocation *device_allocation; /* Allocation table for devices */ s16b random_type; /* Type of random items in which skill may appear */ @@ -61,6 +63,7 @@ void spell_type_init(spell_type *spell, cptr name) { assert(spell != NULL); + static_assert(std::is_pod<spell_type>::value, "Cannot memset non-POD type"); memset(spell, 0, sizeof(spell_type)); spell->name = name; @@ -275,7 +278,7 @@ void spell_type_add_device_allocation(spell_type *spell, struct device_allocatio spell_type *spell_type_new(cptr name) { - spell_type *spell = malloc(sizeof(spell_type)); + spell_type *spell = new spell_type; assert(spell != NULL); spell_type_init(spell, name); return spell; diff --git a/src/spell_type.h b/src/spell_type.h index 956f35a5..eb96c0b9 100644 --- a/src/spell_type.h +++ b/src/spell_type.h @@ -14,12 +14,12 @@ extern "C" { /* * Casting type */ -typedef enum { USE_SPELL_POINTS, USE_PIETY } casting_type; +enum casting_type { USE_SPELL_POINTS, USE_PIETY }; /* * Does the spell appear on spell random books? */ -typedef enum { RANDOM, NO_RANDOM } random_type; +enum random_type { RANDOM, NO_RANDOM }; /* * Spell functions diff --git a/src/spells1.c b/src/spells1.cc index 45bea042..45bea042 100644 --- a/src/spells1.c +++ b/src/spells1.cc diff --git a/src/spells2.c b/src/spells2.cc index 3e3ac568..6b8688b8 100644 --- a/src/spells2.c +++ b/src/spells2.cc @@ -13,6 +13,8 @@ #include "angband.h" #include "hooks.h" +#include <vector> + #define WEIRD_LUCK 12 #define BIAS_LUCK 20 /* @@ -7841,7 +7843,9 @@ bool_ passwall(int dir, bool_ safe) /* * Print a batch of dungeons. */ -static void print_dungeon_batch(int *p, int start, int max, bool_ mode) +static void print_dungeon_batch(std::vector<int> const &dungeon_idxs, + int start, + bool_ mode) { char buf[80]; int i, j; @@ -7850,9 +7854,9 @@ static void print_dungeon_batch(int *p, int start, int max, bool_ mode) if (mode) prt(format(" %-31s", "Name"), 1, 20); - for (i = 0, j = start; i < 20 && j < max; i++, j++) + for (i = 0, j = start; i < 20 && j < dungeon_idxs.size(); i++, j++) { - dungeon_info_type *d_ptr = &d_info[p[j]]; + dungeon_info_type *d_ptr = &d_info[dungeon_idxs[j]]; strnfmt(buf, 80, " %c) %-30s", I2A(i), d_name + d_ptr->name); if (mode) @@ -7876,15 +7880,12 @@ static void print_dungeon_batch(int *p, int start, int max, bool_ mode) int reset_recall_aux() { char which; - int *p; - int max = 0, i, start = 0; + int i, start = 0; int ret; bool_ mode = FALSE; - - C_MAKE(p, max_d_idx, int); - - /* Count the max */ + // Dungeons available for recall + std::vector<int> dungeons; for (i = 1; i < max_d_idx; i++) { /* skip "blocked" dungeons */ @@ -7892,7 +7893,7 @@ int reset_recall_aux() if (max_dlv[i]) { - p[max++] = i; + dungeons.push_back(i); } } @@ -7901,7 +7902,7 @@ int reset_recall_aux() while (1) { - print_dungeon_batch(p, start, max, mode); + print_dungeon_batch(dungeons, start, mode); which = inkey(); if (which == ESCAPE) @@ -7920,7 +7921,7 @@ int reset_recall_aux() else if (which == '+') { start += 20; - if (start >= max) start -= 20; + if (start >= dungeons.size()) start -= 20; Term_load(); character_icky = FALSE; } @@ -7980,7 +7981,7 @@ int reset_recall_aux() else { which = tolower(which); - if (start + A2I(which) >= max) + if (start + A2I(which) >= dungeons.size()) { bell(); continue; @@ -7990,7 +7991,7 @@ int reset_recall_aux() bell(); continue; } - ret = p[start + A2I(which)]; + ret = dungeons[start + A2I(which)]; break; } } @@ -7998,8 +7999,6 @@ int reset_recall_aux() Term_load(); character_icky = FALSE; - C_FREE(p, max_d_idx, int); - return ret; } diff --git a/src/spells3.c b/src/spells3.cc index 84993bc2..96f103c0 100644 --- a/src/spells3.c +++ b/src/spells3.cc @@ -4,6 +4,8 @@ #include "spell_type.h" +#include <vector> + s32b NOXIOUSCLOUD = -1; /* Identifier */ s32b AIRWINGS = -1; /* Identifier */ s32b INVISIBILITY; @@ -1476,7 +1478,7 @@ struct eff_type { s16b feat; s16b low_effect; s16b high_effect; - s16b damage; + int damage; }; static eff_type *geomancy_find_effect(eff_type effs[], int feat) @@ -4606,15 +4608,14 @@ int call_to_the_halls_mlev() casting_result mandos_call_to_the_halls_spell(int item) { -#define N_SUMMONS 2 int y, x; - s16b m_idx, r_idx; - s16b summons[N_SUMMONS] = { + s16b m_idx; + std::vector<int> summons { test_monster_name("Experienced spirit"), - test_monster_name("Wise spirit"), + test_monster_name("Wise spirit") }; - r_idx = summons[rand_int(N_SUMMONS)]; + int r_idx = summons[rand_int(summons.size())]; assert(r_idx >= 0); find_position(p_ptr->py, p_ptr->px, &y, &x); @@ -4625,7 +4626,6 @@ casting_result mandos_call_to_the_halls_spell(int item) return CAST_OBVIOUS; } return NO_CAST; -#undef N_SUMMONS } const char *mandos_call_to_the_halls_info() @@ -4721,15 +4721,14 @@ static int call_of_the_ulumuri_mlev() casting_result ulmo_call_of_the_ulumuri_spell(int item) { -#define N_SUMMONS 2 int x,y; - s16b m_idx, r_idx; - s16b summons[N_SUMMONS] = { + s16b m_idx; + std::vector<int> summons { test_monster_name("Water spirit"), test_monster_name("Water elemental") }; - r_idx = summons[rand_int(N_SUMMONS)]; + int r_idx = summons[rand_int(summons.size())]; assert(r_idx >= 0); find_position(p_ptr->py, p_ptr->px, &y, &x); @@ -4742,7 +4741,6 @@ casting_result ulmo_call_of_the_ulumuri_spell(int item) } return NO_CAST; -#undef N_SUMMONS } const char *ulmo_call_of_the_ulumuri_info() diff --git a/src/spells4.c b/src/spells4.cc index 5a099483..9f5193ef 100644 --- a/src/spells4.c +++ b/src/spells4.cc @@ -127,7 +127,7 @@ static void spell_idx_init(spell_idx_list *p, s32b spell_idx) static spell_idx_list *new_spell_idx(void *ctx, s32b spell_idx) { - spell_idx_list *e = malloc(sizeof(spell_idx_list)); + spell_idx_list *e = new spell_idx_list; spell_idx_init(e, spell_idx); return e; } diff --git a/src/spells5.c b/src/spells5.cc index 72eddb76..72eddb76 100644 --- a/src/spells5.c +++ b/src/spells5.cc diff --git a/src/spells6.c b/src/spells6.cc index 5db4e18e..7bd0d911 100644 --- a/src/spells6.c +++ b/src/spells6.cc @@ -4,31 +4,40 @@ #include "spell_type.h" + +typedef struct school_provider school_provider; +struct school_provider +{ + byte deity_idx; /* Deity which provides school levels */ + + s16b skill_idx; /* Skill used for determining the boost */ + + long mul; /* Multiplier */ + + long div; /* Divisor */ + + school_provider *next; /* Next provider in list */ +}; + static int compare_school_provider(school_provider *a, school_provider *b) { return SGLIB_NUMERIC_COMPARATOR(a->deity_idx, b->deity_idx); } -static void school_provider_init(school_provider *p, byte deity_idx, long mul, long div) -{ - assert(p != NULL); +SGLIB_DEFINE_LIST_PROTOTYPES(school_provider, compare_school_provider, next); +SGLIB_DEFINE_LIST_FUNCTIONS(school_provider, compare_school_provider, next); +static school_provider *school_provider_new(byte deity_idx, long mul, long div) +{ + school_provider *p = new school_provider; p->deity_idx = deity_idx; p->skill_idx = SKILL_PRAY; p->mul = mul; p->div = div; p->next = NULL; -} - -static school_provider *school_provider_new(byte deity_idx, long mul, long div) -{ - school_provider *p = malloc(sizeof(school_provider)); - school_provider_init(p, deity_idx, mul, div); return p; } -SGLIB_DEFINE_LIST_FUNCTIONS(school_provider, compare_school_provider, next); - school_type *school_at(int index) { assert(index >= 0); @@ -162,7 +171,7 @@ struct get_level_school_callback_data { static bool_ get_level_school_callback(void *data_, int school_idx) { - get_level_school_callback_data *data = data_; + get_level_school_callback_data *data = static_cast<get_level_school_callback_data *>(data_); school_type *school = school_at(school_idx); long r = 0, s = 0, p = 0, ok = 0; diff --git a/src/status.c b/src/status.cc index 650a4ace..650a4ace 100644 --- a/src/status.c +++ b/src/status.cc diff --git a/src/store.c b/src/store.cc index 99d8dfa6..5f97c5be 100644 --- a/src/store.c +++ b/src/store.cc @@ -1514,7 +1514,7 @@ static void display_entry(int pos) if (x > p_ptr->au) color = TERM_L_DARK; /* Actually draw the price */ - strnfmt(out_val, 160, "%9ld ", (long)x); + strnfmt(out_val, 160, "%9ld ", static_cast<long>(x)); c_put_str(color, out_val, i + 6, 68); } } @@ -1565,7 +1565,7 @@ void store_prt_gold(void) prt("Gold Remaining: ", 19, 53); - strnfmt(out_val, 64, "%9ld", (long)p_ptr->au); + strnfmt(out_val, 64, "%9ld", static_cast<long>(p_ptr->au)); prt(out_val, 19, 68); } diff --git a/src/string_list.c b/src/string_list.cc index 2d658e1c..03080e46 100644 --- a/src/string_list.c +++ b/src/string_list.cc @@ -45,7 +45,7 @@ void string_list_destroy(string_list *sl) */ void string_list_append(string_list **slist, cptr s) { - string_list *e = malloc(sizeof(string_list)); + string_list *e = new string_list; string_list_init(e, s); sglib_string_list_add(slist, e); diff --git a/src/tables.c b/src/tables.cc index 20d24966..20d24966 100644 --- a/src/tables.c +++ b/src/tables.cc diff --git a/src/traps.c b/src/traps.cc index bb56bb20..bb56bb20 100644 --- a/src/traps.c +++ b/src/traps.cc diff --git a/src/types.h b/src/types.h index 8a035da6..b07e6954 100644 --- a/src/types.h +++ b/src/types.h @@ -2475,20 +2475,6 @@ struct skill_type u32b flags1; /* Skill flags */ }; -typedef struct school_provider school_provider; -struct school_provider -{ - byte deity_idx; /* Deity which provides school levels */ - - s16b skill_idx; /* Skill used for determining the boost */ - - long mul; /* Multiplier */ - - long div; /* Divisor */ - - school_provider *next; /* Next provider in list */ -}; - typedef struct school_type school_type; struct school_type { @@ -2504,7 +2490,7 @@ struct school_type bool_ (*depends_satisfied)(); /* Are dependendies satisfied? */ - school_provider *providers; /* List of secondary providers of this school */ + struct school_provider *providers; /* List of secondary providers of this school */ }; /* diff --git a/src/util.c b/src/util.cc index 52184f0b..ccb0eea7 100644 --- a/src/util.c +++ b/src/util.cc @@ -2,14 +2,11 @@ /* Purpose: Angband utilities -BEN- */ - +#include "util.hpp" #include "angband.h" - #include "messages.h" #include "quark.h" - - #ifndef HAS_MEMSET /* @@ -3638,8 +3635,6 @@ int test_monster_name(cptr name) { monster_race *r_ptr = &r_info[i]; cptr mon_name = r_name + r_ptr->name; - - /* If name matches, give us the number */ if (stricmp(name, mon_name) == 0) return (i); } return (0); @@ -3653,8 +3648,6 @@ int test_mego_name(cptr name) { monster_ego *re_ptr = &re_info[i]; cptr mon_name = re_name + re_ptr->name; - - /* If name matches, give us the number */ if (stricmp(name, mon_name) == 0) return (i); } return (0); @@ -3675,7 +3668,6 @@ int test_item_name(cptr name) { object_kind *k_ptr = &k_info[i]; cptr obj_name = k_name + k_ptr->name; - /* If name matches, give us the number */ if (stricmp(name, obj_name) == 0) return (i); } @@ -3781,7 +3773,7 @@ cptr get_player_race_name(int pr, int ps) /* * Ask to select an item in a list */ -int ask_menu(cptr ask, char **items, int max) +int ask_menu(cptr ask, const std::vector<std::string> &items) { int ret = -1, i, start = 0; char c; @@ -3798,9 +3790,9 @@ int ask_menu(cptr ask, char **items, int max) Term_load(); Term_save(); prt(ask, 0, 0); - for (i = start; (i < max) && (i < start + 20); i++) + for (i = start; (i < items.size()) && (i < start + 20); i++) { - prt(format("%c) %s", I2A(i - start), items[i]), i - start + 1, 0); + prt(format("%c) %s", I2A(i - start), items[i].c_str()), i - start + 1, 0); } /* Wait for user input */ @@ -3812,7 +3804,7 @@ int ask_menu(cptr ask, char **items, int max) /* Scroll */ else if (c == '+') { - if (start + 20 < max) + if (start + 20 < items.size()) start += 20; continue; } @@ -3829,7 +3821,7 @@ int ask_menu(cptr ask, char **items, int max) else { c = tolower(c); - if (A2I(c) + start >= max) + if (A2I(c) + start >= items.size()) { bell(); continue; @@ -3982,9 +3974,11 @@ void scansubdir(cptr dir) */ timer_type *new_timer(void (*callback)(), s32b delay) { - timer_type *t_ptr = NULL; + timer_type *t_ptr = new timer_type(); + + static_assert(std::is_pod<timer_type>::value, "Cannot memset a non-POD type"); + memset(t_ptr, 0, sizeof(timer_type)); - MAKE(t_ptr, timer_type); t_ptr->next = gl_timers; gl_timers = t_ptr; diff --git a/src/util.hpp b/src/util.hpp new file mode 100644 index 00000000..a8f892db --- /dev/null +++ b/src/util.hpp @@ -0,0 +1,8 @@ +#pragma once + +#include "h-basic.h" + +#include <vector> +#include <string> + +int ask_menu(cptr ask, const std::vector<std::string> &items); diff --git a/src/variable.c b/src/variable.cc index 4a07180d..adab0232 100644 --- a/src/variable.c +++ b/src/variable.cc @@ -572,10 +572,8 @@ char angband_sound_name[SOUND_MAX][16] = /* * The array of "cave grids" [MAX_WID][MAX_HGT]. - * Not completely allocated, that would be inefficient - * Not completely hardcoded, that would overflow memory */ -cave_type *cave[MAX_HGT]; +cave_type **cave = nullptr; /* * The array of dungeon items [max_o_idx] diff --git a/src/wild.c b/src/wild.cc index 13a221d0..8719a53a 100644 --- a/src/wild.c +++ b/src/wild.cc @@ -13,6 +13,7 @@ #include "angband.h" #include "hooks.h" +#include <memory> /* @@ -50,7 +51,7 @@ static void perturb_point_mid(int x1, int x2, int x3, int x4, if (avg > depth_max) avg = depth_max; /* Set the new value. */ - cave[ymid][xmid].feat = (byte)avg; + cave[ymid][xmid].feat = static_cast<byte>(avg); } @@ -74,7 +75,7 @@ static void perturb_point_end(int x1, int x2, int x3, if (avg > depth_max) avg = depth_max; /* Set the new value. */ - cave[ymid][xmid].feat = (byte)avg; + cave[ymid][xmid].feat = static_cast<byte>(avg); } @@ -957,8 +958,6 @@ static void town_gen_hack(int t_idx, int qy, int qx) int y, x, floor, num = 0; bool_ (*old_get_mon_num_hook)(int r_idx); - int *rooms; - /* Do we use dungeon floor or normal one */ if (magik(TOWN_NORMAL_FLOOR)) floor = FEAT_FLOOR; else floor = 0; @@ -975,8 +974,8 @@ static void town_gen_hack(int t_idx, int qy, int qx) } /* Prepare an Array of "remaining stores", and count them */ - C_MAKE(rooms, max_st_idx, int); - num = get_shops(rooms); + std::unique_ptr<int[]> rooms(new int[max_st_idx]); + num = get_shops(rooms.get()); /* Place two rows of stores */ for (y = 0; y < 2; y++) @@ -991,7 +990,6 @@ static void town_gen_hack(int t_idx, int qy, int qx) } } } - C_FREE(rooms, max_st_idx, int); /* Generates the town's borders */ if (magik(TOWN_NORMAL_FLOOR)) town_borders(t_idx, qy, qx); @@ -1048,8 +1046,6 @@ static void town_gen_circle(int t_idx, int qy, int qx) int y, x, cy, cx, rad, floor, num = 0; bool_ (*old_get_mon_num_hook)(int r_idx); - int *rooms; - /* Do we use dungeon floor or normal one */ if (magik(TOWN_NORMAL_FLOOR)) floor = FEAT_FLOOR; else floor = 0; @@ -1111,8 +1107,8 @@ static void town_gen_circle(int t_idx, int qy, int qx) } /* Prepare an Array of "remaining stores", and count them */ - C_MAKE(rooms, max_st_idx, int); - num = get_shops(rooms); + std::unique_ptr<int[]> rooms(new int[max_st_idx]); + num = get_shops(rooms.get()); /* Place two rows of stores */ for (y = 0; y < 2; y++) @@ -1127,7 +1123,6 @@ static void town_gen_circle(int t_idx, int qy, int qx) } } } - C_FREE(rooms, max_st_idx, int); /* Some inhabitants(leveled .. hehe :) */ @@ -1179,11 +1174,9 @@ static void town_gen_hidden(int t_idx, int qy, int qx) { int y, x, n, num = 0, i; - int *rooms; - /* Prepare an Array of "remaining stores", and count them */ - C_MAKE(rooms, max_st_idx, int); - num = get_shops(rooms); + std::unique_ptr<int[]> rooms(new int[max_st_idx]); + num = get_shops(rooms.get()); /* Get a number of stores to place */ n = rand_int(num / 2) + (num / 2); @@ -1206,7 +1199,6 @@ static void town_gen_hidden(int t_idx, int qy, int qx) build_store_hidden(rooms[num], y, x); } } - C_FREE(rooms, max_st_idx, int); } diff --git a/src/wizard1.c b/src/wizard1.cc index 11f96f61..90ee5d27 100644 --- a/src/wizard1.c +++ b/src/wizard1.cc @@ -4,6 +4,7 @@ #include "angband.h" +#include <vector> /* * The spoiler file being created @@ -1311,9 +1312,7 @@ static void spoil_artifact(cptr fname) */ static void spoil_mon_desc(cptr fname) { - int i, n = 0; - - s16b *who; + int i = 0; char buf[1024]; @@ -1342,7 +1341,7 @@ static void spoil_mon_desc(cptr fname) } /* Allocate the "who" array */ - C_MAKE(who, max_r_idx, s16b); + std::vector<s16b> who; /* Dump the header */ sprintf(buf, "Monster Spoilers for %s", get_version_string()); @@ -1362,12 +1361,14 @@ static void spoil_mon_desc(cptr fname) monster_race *r_ptr = &r_info[i]; /* Use that monster */ - if (r_ptr->name) who[n++] = i; + if (r_ptr->name) { + who.push_back(i); + } } /* Scan again */ - for (i = 0; i < n; i++) + for (i = 0; i < who.size(); i++) { monster_race *r_ptr = &r_info[who[i]]; @@ -1428,9 +1429,6 @@ static void spoil_mon_desc(cptr fname) /* End it */ fprintf(fff, "\n"); - /* Free the "who" array */ - C_KILL(who, max_r_idx, s16b); - /* Check for errors */ if (ferror(fff) || my_fclose(fff)) { diff --git a/src/wizard2.c b/src/wizard2.cc index e336f068..e336f068 100644 --- a/src/wizard2.c +++ b/src/wizard2.cc diff --git a/src/xtra1.c b/src/xtra1.cc index 821fe508..821fe508 100644 --- a/src/xtra1.c +++ b/src/xtra1.cc diff --git a/src/xtra2.c b/src/xtra2.cc index 53abc149..53abc149 100644 --- a/src/xtra2.c +++ b/src/xtra2.cc diff --git a/src/z-virt.h b/src/z-virt.h index 58aed382..952e2d86 100644 --- a/src/z-virt.h +++ b/src/z-virt.h @@ -68,10 +68,6 @@ extern "C" { (memset((char*)(P),0,SIZE(T))) -/* Load an array of type T[N], at location P1, from another, at location P2 */ -#define C_COPY(P1,P2,N,T) \ - (memcpy((char*)(P1),(char*)(P2),C_SIZE(N,T))) - /* Load a thing of type T, at location P1, from another, at location P2 */ #define COPY(P1,P2,T) \ (memcpy((char*)(P1),(char*)(P2),SIZE(T))) |