diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/cmd3.cc | 31 | ||||
-rw-r--r-- | src/cmd4.cc | 346 | ||||
-rw-r--r-- | src/cmd6.cc | 4 | ||||
-rw-r--r-- | src/corrupt.cc | 23 | ||||
-rw-r--r-- | src/corrupt.hpp | 4 | ||||
-rw-r--r-- | src/files.cc | 25 | ||||
-rw-r--r-- | src/files.hpp | 4 | ||||
-rw-r--r-- | src/q_bounty.cc | 14 | ||||
-rw-r--r-- | src/q_bounty.hpp | 4 | ||||
-rw-r--r-- | src/q_fireprof.cc | 36 | ||||
-rw-r--r-- | src/q_fireprof.hpp | 4 | ||||
-rw-r--r-- | src/q_god.cc | 15 | ||||
-rw-r--r-- | src/q_god.hpp | 4 | ||||
-rw-r--r-- | src/q_library.cc | 23 | ||||
-rw-r--r-- | src/q_library.hpp | 4 | ||||
-rw-r--r-- | src/q_rand.cc | 41 | ||||
-rw-r--r-- | src/q_rand.hpp | 4 | ||||
-rw-r--r-- | src/quest_type.hpp | 4 | ||||
-rw-r--r-- | src/status.cc | 50 | ||||
-rw-r--r-- | src/xtra1.cc | 48 | ||||
-rw-r--r-- | src/xtra1.hpp | 6 |
21 files changed, 276 insertions, 418 deletions
diff --git a/src/cmd3.cc b/src/cmd3.cc index d6ede5a8..ef0dfa28 100644 --- a/src/cmd3.cc +++ b/src/cmd3.cc @@ -40,7 +40,7 @@ #include <cassert> #include <algorithm> -#include <boost/filesystem.hpp> +#include <format.h> #include <memory> #include <utility> @@ -2010,29 +2010,23 @@ void do_cmd_cli(void) */ void do_cmd_cli_help() { - int i, j; - - /* Temporary file */ - auto const file_name_p = boost::filesystem::unique_path(); - auto const file_name = file_name_p.c_str(); + fmt::MemoryWriter w; + for (int i = 0, j = -1; i < cli_total; i++) + { + if (j < i - 1) + { + w << "/"; + } - /* Open a new file */ - FILE *fff = my_fopen(file_name, "w"); + w.write("[[[[[G{}]", cli_info[i].comm); - for (i = 0, j = -1; i < cli_total; i++) - { - if (j < i - 1) fprintf(fff, "/"); - fprintf(fff, "[[[[[G%s]", cli_info[i].comm); if (cli_info[i].descrip != cli_info[i + 1].descrip) { - fprintf(fff, " %s\n", cli_info[i].descrip); + w.write(" {}\n", cli_info[i].descrip); j = i; } } - /* Close the file */ - my_fclose(fff); - /* Enter "icky" mode */ character_icky = TRUE; @@ -2040,16 +2034,13 @@ void do_cmd_cli_help() Term_save(); /* Display the file contents */ - show_file(file_name, "Command line help"); + show_string(w.c_str(), "Command line help"); /* Restore the screen */ Term_load(); /* Leave "icky" mode */ character_icky = FALSE; - - /* Remove the file */ - fd_kill(file_name); } diff --git a/src/cmd4.cc b/src/cmd4.cc index 664645d8..9a8a3a65 100644 --- a/src/cmd4.cc +++ b/src/cmd4.cc @@ -38,8 +38,8 @@ #include "z-rand.hpp" #include <algorithm> -#include <boost/filesystem.hpp> #include <cassert> +#include <format.h> #include <memory> #include <string> #include <vector> @@ -3016,13 +3016,6 @@ void do_cmd_knowledge_artifacts(void) char base_name[80]; - /* Temporary file */ - auto const file_name_p = boost::filesystem::unique_path(); - auto const file_name = file_name_p.c_str(); - - /* Open a new file */ - FILE *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++) @@ -3155,6 +3148,9 @@ void do_cmd_knowledge_artifacts(void) } } + /* Output buffer */ + fmt::MemoryWriter w; + /* Scan the artifacts */ for (k = 0; k < max_a_idx; k++) { @@ -3195,7 +3191,7 @@ void do_cmd_knowledge_artifacts(void) } /* Hack -- Build the artifact name */ - fprintf(fff, " The %s\n", base_name); + w.write(" The {}\n", base_name); } for (k = 0; k < max_k_idx; k++) @@ -3223,17 +3219,11 @@ void do_cmd_knowledge_artifacts(void) } /* Hack -- Build the artifact name */ - fprintf(fff, " The %s\n", base_name); + w.write(" The {}\n", base_name); } - /* Close the file */ - my_fclose(fff); - - /* Display the file contents */ - show_file(file_name, "Artifacts Seen"); - - /* Remove the file */ - fd_kill(file_name); + /* Display */ + show_string(w.c_str(), "Artifacts Seen"); } @@ -3242,13 +3232,7 @@ void do_cmd_knowledge_artifacts(void) */ void do_cmd_knowledge_traps(void) { - /* Temporary file */ - auto const file_name_p = boost::filesystem::unique_path(); - auto const file_name = file_name_p.c_str(); - - /* Open a new file */ - FILE *fff = my_fopen(file_name, "w"); - + fmt::MemoryWriter w; /* Scan the traps */ for (int k = 0; k < max_t_idx; k++) { @@ -3262,17 +3246,11 @@ void do_cmd_knowledge_traps(void) if (!t_ptr->ident) continue; /* Hack -- Build the trap name */ - fprintf(fff, " %s\n", t_ptr->name); + w.write(" {}\n", t_ptr->name); } - /* Close the file */ - my_fclose(fff); - /* Display the file contents */ - show_file(file_name, "Traps known"); - - /* Remove the file */ - fd_kill(file_name); + show_string(w.c_str(), "Traps known"); } @@ -3287,18 +3265,9 @@ static int monster_get_race_level(int r_idx) { /* * Display known uniques - * - * Note that the player ghosts are ignored. XXX XXX XXX */ static void do_cmd_knowledge_uniques(void) { - /* Temporary file */ - auto const file_name_p = boost::filesystem::unique_path(); - auto const file_name = file_name_p.c_str(); - - /* Open a new file */ - FILE *fff = my_fopen(file_name, "w"); - // Extract the unique race indexes. std::vector<int> unique_r_idxs; for (int k = 1; k < max_r_idx; k++) @@ -3321,7 +3290,8 @@ static void do_cmd_knowledge_uniques(void) return monster_get_race_level(r_idx1) < monster_get_race_level(r_idx2); }); - /* Scan the monster races */ + // Scan the monster races + fmt::MemoryWriter w; for (int r_idx : unique_r_idxs) { monster_race *r_ptr = &r_info[r_idx]; @@ -3337,30 +3307,24 @@ static void do_cmd_knowledge_uniques(void) /* Print a message */ if (dead) { - fprintf(fff, "[[[[[%c%c] [[[[[R%-68s is dead]\n", - conv_color[r_ptr->d_attr], - r_ptr->d_char, + w.write("[[[[[{}{}] [[[[[R{:<68} is dead]\n", + static_cast<char>(conv_color[r_ptr->d_attr]), + static_cast<char>(r_ptr->d_char), r_ptr->name); } else { - fprintf(fff, "[[[[[%c%c] [[[[[w%-68s is alive]\n", - conv_color[r_ptr->d_attr], - r_ptr->d_char, + w.write("[[[[[{}{}] [[[[[w{:<68} is alive]\n", + static_cast<char>(conv_color[r_ptr->d_attr]), + static_cast<char>(r_ptr->d_char), r_ptr->name); } } } } - /* Close the file */ - my_fclose(fff); - - /* Display the file contents */ - show_file(file_name, "Known Uniques"); - - /* Remove the file */ - fd_kill(file_name); + // Display + show_string(w.c_str(), "Known Uniques"); } @@ -3458,32 +3422,17 @@ static void plural_aux(char *name) */ static void do_cmd_knowledge_pets(void) { - int i; - - monster_type *m_ptr; - int t_friends = 0; - int t_levels = 0; - int show_upkeep = 0; - - int upkeep_divider = 20; - - /* Temporary file */ - auto const file_name_p = boost::filesystem::unique_path(); - auto const file_name = file_name_p.c_str(); - - /* Open a new file */ - FILE *fff = my_fopen(file_name, "w"); - - if (has_ability(AB_PERFECT_CASTING)) upkeep_divider = 15; + // Buffer + fmt::MemoryWriter w; /* Process the monsters (backwards) */ - for (i = m_max - 1; i >= 1; i--) + for (int i = m_max - 1; i >= 1; i--) { /* Access the monster */ - m_ptr = &m_list[i]; + monster_type *m_ptr = &m_list[i]; /* Ignore "dead" monsters */ if (!m_ptr->r_idx) continue; @@ -3499,13 +3448,17 @@ static void do_cmd_knowledge_pets(void) char pet_name[80]; monster_desc(pet_name, m_ptr, 0x88); - fprintf(fff, "%s%s (%s)\n", - (r_ptr->flags1 & RF1_UNIQUE) ? "#####G" : "", - pet_name, - (m_ptr->status < MSTATUS_COMPANION) ? "pet" : "companion"); + w.write("{}{} ({})\n", + (r_ptr->flags1 & RF1_UNIQUE) ? "#####G" : "", + pet_name, + (m_ptr->status < MSTATUS_COMPANION) ? "pet" : "companion"); } } + // Calculate upkeep + int show_upkeep = 0; + int upkeep_divider = has_ability(AB_PERFECT_CASTING) ? 15 : 20; + if (t_friends > 1 + (p_ptr->lev / (upkeep_divider))) { show_upkeep = (t_levels); @@ -3514,80 +3467,58 @@ static void do_cmd_knowledge_pets(void) else if (show_upkeep < 10) show_upkeep = 10; } + // Summary + w.write("----------------------------------------------\n"); + w.write(" Total: {} pet{}.\n", t_friends, (t_friends == 1 ? "" : "s")); + w.write(" Upkeep: {}% mana.\n", show_upkeep); - fprintf(fff, "----------------------------------------------\n"); - fprintf(fff, " Total: %d pet%s.\n", t_friends, (t_friends == 1 ? "" : "s")); - fprintf(fff, " Upkeep: %d%% mana.\n", show_upkeep); - - - /* Close the file */ - my_fclose(fff); - - /* Display the file contents */ - show_file(file_name, "Current Pets"); - - /* Remove the file */ - fd_kill(file_name); + // Display + show_string(w.c_str(), "Current Pets"); } /* * Total kill count - * - * Note that the player ghosts are ignored. XXX XXX XXX */ static void do_cmd_knowledge_kill_count(void) { s32b Total = 0; - /* Temporary file */ - auto const file_name_p = boost::filesystem::unique_path(); - auto const file_name = file_name_p.c_str(); - - /* Open a new file */ - FILE *fff = my_fopen(file_name, "w"); + // Buffer + fmt::MemoryWriter w; + // Summary of monsters slain { - /* Monsters slain */ - int kk; - /* For all monsters */ - for (kk = 1; kk < max_r_idx; kk++) + for (int kk = 1; kk < max_r_idx; kk++) { monster_race *r_ptr = &r_info[kk]; if (r_ptr->flags1 & (RF1_UNIQUE)) { - bool_ dead = (r_ptr->max_num == 0); - - if (dead) + if (r_ptr->max_num == 0) { Total++; } } else { - s16b This = r_ptr->r_pkills; - - if (This > 0) - { - Total += This; - } + Total += std::max<s16b>(r_ptr->r_pkills, 0); } } if (Total < 1) { - fprintf(fff, "You have defeated no enemies yet.\n\n"); + w.write("You have defeated no enemies yet.\n\n"); } else if (Total == 1) { - fprintf(fff, "You have defeated one enemy.\n\n"); + w.write("You have defeated one enemy.\n\n"); } else { - fprintf(fff, "You have defeated " FMTs32b " enemies.\n\n", Total); + w.write("You have defeated {} enemies.\n\n", Total); } } @@ -3605,7 +3536,7 @@ static void do_cmd_knowledge_kill_count(void) if (dead) { /* Print a message */ - fprintf(fff, " %s\n", r_ptr->name); + w.write(" {}\n", r_ptr->name); Total++; } } @@ -3619,11 +3550,11 @@ static void do_cmd_knowledge_kill_count(void) { if (strstr(r_ptr->name, "coins")) { - fprintf(fff, " 1 pile of %s\n", r_ptr->name); + w.write(" 1 pile of {}\n", r_ptr->name); } else { - fprintf(fff, " 1 %s\n", r_ptr->name); + w.write(" 1 {}\n", r_ptr->name); } } else @@ -3631,7 +3562,7 @@ static void do_cmd_knowledge_kill_count(void) char to_plural[80]; strcpy(to_plural, r_ptr->name); plural_aux(to_plural); - fprintf(fff, " %d %s\n", This, to_plural); + w.write(" {} {}\n", This, to_plural); } Total += This; @@ -3639,17 +3570,11 @@ static void do_cmd_knowledge_kill_count(void) } } - fprintf(fff, "----------------------------------------------\n"); - fprintf(fff, " Total: " FMTs32b " creature%s killed.\n", Total, (Total == 1 ? "" : "s")); - - /* Close the file */ - my_fclose(fff); + w.write("----------------------------------------------\n"); + w.write(" Total: {} creature{} killed.\n", Total, (Total == 1 ? "" : "s")); /* Display the file contents */ - show_file(file_name, "Kill Count"); - - /* Remove the file */ - fd_kill(file_name); + show_string(w.c_str(), "Kill Count"); } @@ -3658,12 +3583,7 @@ static void do_cmd_knowledge_kill_count(void) */ static void do_cmd_knowledge_objects(void) { - /* Temporary file */ - auto const file_name_p = boost::filesystem::unique_path(); - auto const file_name = file_name_p.c_str(); - - /* Open a new file */ - FILE *fff = my_fopen(file_name, "w"); + fmt::MemoryWriter w; /* Scan the object kinds */ for (int k = 1; k < max_k_idx; k++) @@ -3676,32 +3596,25 @@ static void do_cmd_knowledge_objects(void) /* List known flavored objects */ if (k_ptr->flavor && k_ptr->aware) { - char o_name[80]; - object_type *i_ptr; object_type object_type_body; /* Get local object */ - i_ptr = &object_type_body; + object_type *i_ptr = &object_type_body; /* Create fake object */ object_prep(i_ptr, k); /* Describe the object */ + char o_name[80]; object_desc_store(o_name, i_ptr, FALSE, 0); /* Print a message */ - fprintf(fff, " %s\n", o_name); + w.write(" {}\n", o_name); } } - /* Close the file */ - my_fclose(fff); - - /* Display the file contents */ - show_file(file_name, "Known Objects"); - - /* Remove the file */ - fd_kill(file_name); + // Display + show_string(w.c_str(), "Known Objects"); } @@ -3710,15 +3623,7 @@ static void do_cmd_knowledge_objects(void) */ static void do_cmd_knowledge_dungeons(void) { - /* Temporary file */ - auto const file_name_p = boost::filesystem::unique_path(); - auto const file_name = file_name_p.c_str(); - - /* Open a new file */ - FILE *fff = my_fopen(file_name, "w"); - - /* Oops */ - if (fff == NULL) return; + fmt::MemoryWriter w; /* Scan all dungeons */ for (int y = 1; y < max_d_idx; y++) @@ -3727,21 +3632,15 @@ static void do_cmd_knowledge_dungeons(void) if (max_dlv[y]) { /* Describe the recall depth */ - fprintf(fff, " %c%s: Level %d (%d')\n", - (p_ptr->recall_dungeon == y) ? '*' : ' ', + w.write(" {}{}: Level {} ({}')\n", + (p_ptr->recall_dungeon == y) ? '*' : ' ', d_info[y].name, - max_dlv[y], 50 * (max_dlv[y])); + max_dlv[y], 50 * (max_dlv[y])); } } - /* Close the file */ - my_fclose(fff); - - /* Display the file contents */ - show_file(file_name, "Recall Depths"); - - /* Remove the file */ - fd_kill(file_name); + // Display + show_string(w.c_str(), "Recall Depths"); } @@ -3750,15 +3649,7 @@ static void do_cmd_knowledge_dungeons(void) */ void do_cmd_knowledge_towns(void) { - /* Temporary file */ - auto const file_name_p = boost::filesystem::unique_path(); - auto const file_name = file_name_p.c_str(); - - /* Open a new file */ - FILE *fff = my_fopen(file_name, "w"); - - /* Oops */ - if (fff == NULL) return; + fmt::MemoryWriter w; /* Scan all dungeons */ for (int i = 0; i < max_d_idx; i++) @@ -3777,21 +3668,15 @@ void do_cmd_knowledge_towns(void) if (!(town_info[town_idx].flags & (TOWN_KNOWN))) continue; /* Describe the dungeon town */ - fprintf(fff, " %s: Level %d (%d')\n", + w.write(" {}: Level {} ({}')\n", d_ptr->name, - d_ptr->t_level[j], - 50 * d_ptr->t_level[j]); + d_ptr->t_level[j], + 50 * d_ptr->t_level[j]); } } - /* Close the file */ - my_fclose(fff); - /* Display the file contents */ - show_file(file_name, "Dungeon Towns"); - - /* Remove the file */ - fd_kill(file_name); + show_string(w.c_str(), "Dungeon Towns"); } @@ -3800,27 +3685,7 @@ void do_cmd_knowledge_towns(void) */ static void do_cmd_knowledge_corruptions(void) { - /* Temporary file */ - auto const file_name_p = boost::filesystem::unique_path(); - auto const file_name = file_name_p.c_str(); - - /* Open a new file */ - FILE *fff = my_fopen(file_name, "w"); - - /* Dump the corruptions to file */ - if (fff) - { - dump_corruptions(fff, TRUE, FALSE); - } - - /* Close the file */ - my_fclose(fff); - - /* Display the file contents */ - show_file(file_name, "Corruptions"); - - /* Remove the file */ - fd_kill(file_name); + show_string(dump_corruptions(true, false).c_str(), "Corruptions"); } @@ -3862,35 +3727,28 @@ static void insert_sort_quest(int *order, int *num, int q_idx) */ static void do_cmd_knowledge_quests(void) { + /* Figure out display order of quests */ int order[MAX_Q_IDX] = { }; int num = 0; - - int i, j, z; - - - /* Temporary file */ - auto const file_name_p = boost::filesystem::unique_path(); - auto const file_name = file_name_p.c_str(); - - /* Open a new file */ - FILE *fff = my_fopen(file_name, "w"); - - for (i = 0; i < MAX_Q_IDX; i++) + for (int i = 0; i < MAX_Q_IDX; i++) { insert_sort_quest(order, &num, i); } - for (z = 0; z < MAX_Q_IDX; z++) + /* Write */ + fmt::MemoryWriter w; + for (int z = 0; z < MAX_Q_IDX; z++) { - i = order[z]; + int const i = order[z]; /* Dynamic descriptions */ if (quest[i].gen_desc != NULL) { - if (!quest[i].gen_desc(fff)) + auto s = quest[i].gen_desc(); + if (!s.empty()) { - continue; + w.write("{}\n\n", s); } } @@ -3900,32 +3758,26 @@ static void do_cmd_knowledge_quests(void) if (quest[i].status == QUEST_STATUS_TAKEN) { /* Print the quest info */ - fprintf(fff, "#####y%s (Danger level: %d)\n", + w.write("#####y{} (Danger level: {})\n", quest[i].name, quest[i].level); - j = 0; + int j = 0; while ((j < 10) && (quest[i].desc[j][0] != '\0')) { - fprintf(fff, "%s\n", quest[i].desc[j++]); + w.write("{}\n", quest[i].desc[j++]); } - fprintf(fff, "\n"); + w.write("\n"); } else if (quest[i].status == QUEST_STATUS_COMPLETED) { - fprintf(fff , "#####G%s Completed - Unrewarded\n", quest[i].name); - fprintf(fff, "\n"); + w.write("#####G{} Completed - Unrewarded\n", quest[i].name); + w.write("\n"); } } } - /* Close the file */ - my_fclose(fff); - - /* Display the file contents */ - show_file(file_name, "Quest status"); - - /* Remove the file */ - fd_kill(file_name); + /* Display */ + show_string(w.c_str(), "Quest status"); } @@ -3934,23 +3786,7 @@ static void do_cmd_knowledge_quests(void) */ static void do_cmd_knowledge_fates(void) { - /* Temporary file */ - auto const file_name_p = boost::filesystem::unique_path(); - auto const file_name = file_name_p.c_str(); - - /* Open a new file */ - FILE *fff = my_fopen(file_name, "w"); - - dump_fates(fff); - - /* Close the file */ - my_fclose(fff); - - /* Display the file contents */ - show_file(file_name, "Fate status"); - - /* Remove the file */ - fd_kill(file_name); + show_string(dump_fates().c_str(), "Fate status"); } diff --git a/src/cmd6.cc b/src/cmd6.cc index 1752d9a8..fce71215 100644 --- a/src/cmd6.cc +++ b/src/cmd6.cc @@ -2995,7 +2995,6 @@ void do_cmd_read_scroll(void) case SV_SCROLL_DIVINATION: { int i, count = 0; - char buf[120]; while (count < 1000) { @@ -3007,8 +3006,7 @@ void do_cmd_read_scroll(void) msg_print("A message appears on the scroll. It says:"); msg_print(NULL); - fate_desc(buf, i); - msg_format("%s", buf); + msg_format("%s", fate_desc(i).c_str()); msg_print(NULL); msg_print("The scroll disappears in a puff of smoke!"); diff --git a/src/corrupt.cc b/src/corrupt.cc index f182a9e2..47d79d53 100644 --- a/src/corrupt.cc +++ b/src/corrupt.cc @@ -12,6 +12,7 @@ #include "z-rand.hpp" #include <cassert> +#include <format.h> /** * Corruptions @@ -944,20 +945,18 @@ void lose_corruption() /* * Dump the corruption list */ -void dump_corruptions(FILE *fff, bool_ color, bool_ header) +std::string dump_corruptions(bool color, bool header) { - int i; - - assert(fff != NULL); + fmt::MemoryWriter w; - for (i = 0; i < CORRUPTIONS_MAX; i++) + for (int i = 0; i < CORRUPTIONS_MAX; i++) { - corruption_type *c_ptr = &corruptions[i]; + corruption_type const *c_ptr = &corruptions[i]; if (header) { - fprintf(fff, "\n Corruption list:\n"); - header = FALSE; + w.write("\nCorruption list:\n\n"); + header = false; } if (p_ptr->corruptions[i]) @@ -966,16 +965,18 @@ void dump_corruptions(FILE *fff, bool_ color, bool_ header) if (color) { - fprintf(fff, "#####%c%s:\n", conv_color[c], c_ptr->name); + w.write("#####{}{}:\n", static_cast<char>(conv_color[c]), c_ptr->name); } else { - fprintf(fff, "%s:\n", c_ptr->name); + w.write("{}:\n", c_ptr->name); } - fprintf(fff, "%s\n", c_ptr->desc); + w.write("{}\n\n", c_ptr->desc); } } + + return w.str(); } /* diff --git a/src/corrupt.hpp b/src/corrupt.hpp index c200762e..d7ab2e97 100644 --- a/src/corrupt.hpp +++ b/src/corrupt.hpp @@ -1,7 +1,9 @@ #include "h-basic.h" +#include <string> + extern void gain_random_corruption(); -extern void dump_corruptions(FILE *OutFile, bool_ color, bool_ header); +extern std::string dump_corruptions(bool color, bool header); extern void lose_corruption(); extern bool_ player_has_corruption(int corruption_idx); extern void player_gain_corruption(int corruption_idx); diff --git a/src/files.cc b/src/files.cc index ab5421b5..be1e208a 100644 --- a/src/files.cc +++ b/src/files.cc @@ -54,7 +54,10 @@ #include "xtra1.hpp" #include "z-rand.hpp" +#include <boost/filesystem.hpp> #include <memory> +#include <iostream> +#include <fstream> #include <unordered_set> /* @@ -2784,7 +2787,7 @@ errr file_character(cptr name, bool_ full) file_character_print_grid(fff, FALSE, FALSE); /* Dump corruptions */ - dump_corruptions(fff, FALSE, TRUE); + fprintf(fff, "\n%s\n", dump_corruptions(false, true).c_str()); /* Dump skills */ dump_skills(fff); @@ -2806,7 +2809,7 @@ errr file_character(cptr name, bool_ full) if ((fates[i].fate) && (fates[i].know)) { fprintf(fff, "\n\n [Fates]\n\n"); - dump_fates(fff); + fprintf(fff, "%s", dump_fates().c_str()); break; } } @@ -3501,6 +3504,24 @@ static bool_ show_file_aux(cptr name, cptr what, int line) return (TRUE); } +void show_string(const char *lines, const char *title, int line) +{ + // Temporary file + auto const file_name = boost::filesystem::unique_path().string(); + + // Open a new file + std::ofstream ofs(file_name); + ofs.exceptions(std::ofstream::failbit); + ofs << lines; + ofs.close(); + + // Display the file contents + show_file_aux(file_name.c_str(), title, line); + + // Remove the file + fd_kill(file_name.c_str()); +} + void show_file(cptr name, cptr what, int line) { show_file_aux(name, what, line); diff --git a/src/files.hpp b/src/files.hpp index d61415f6..d90f6660 100644 --- a/src/files.hpp +++ b/src/files.hpp @@ -3,6 +3,9 @@ #include "h-basic.h" #include "monster_type_fwd.hpp" +#include <string> +#include <vector> + extern void html_screenshot(cptr name); extern void help_file_screenshot(cptr name); extern void player_flags(u32b* f1, u32b* f2, u32b* f3, u32b* f4, u32b* f5, u32b* esp); @@ -13,6 +16,7 @@ extern cptr describe_player_location(void); extern errr file_character(cptr name, bool_ full); extern errr process_pref_file_aux(char *buf); extern errr process_pref_file(cptr name); +extern void show_string(const char *lines, const char *title, int line = 0); extern void show_file(cptr name, cptr what, int line = 0); extern void do_cmd_help(void); extern void process_player_base(void); diff --git a/src/q_bounty.cc b/src/q_bounty.cc index bb84d48d..7af282bf 100644 --- a/src/q_bounty.cc +++ b/src/q_bounty.cc @@ -11,6 +11,8 @@ #include "util.hpp" #include "variable.hpp" +#include <format.h> + #define cquest (quest[QUEST_BOUNTY]) #define bounty_quest_monster (cquest.data[0]) @@ -151,20 +153,18 @@ bool_ quest_bounty_get_item() return FALSE; } -bool_ quest_bounty_describe(FILE *fff) +std::string quest_bounty_describe() { char mdesc[512]; + fmt::MemoryWriter w; if (cquest.status == QUEST_STATUS_TAKEN) { monster_race_desc(mdesc, bounty_quest_monster, 0); - fprintf(fff, "#####yBounty quest!\n"); - fprintf(fff, "You must bring back %s corpse to the beastmaster.\n", mdesc); - fprintf(fff, "\n"); - - return TRUE; + w.write("#####yBounty quest!\n"); + w.write("You must bring back {} corpse to the beastmaster.", mdesc); } - return FALSE; + return w.str(); } diff --git a/src/q_bounty.hpp b/src/q_bounty.hpp index 234c036d..450ca8bd 100644 --- a/src/q_bounty.hpp +++ b/src/q_bounty.hpp @@ -2,7 +2,9 @@ #include "h-basic.h" +#include <string> + extern bool_ quest_bounty_init_hook(int q_idx); extern bool_ quest_bounty_drop_item(); extern bool_ quest_bounty_get_item(); -extern bool_ quest_bounty_describe(FILE *fff); +extern std::string quest_bounty_describe(); diff --git a/src/q_fireprof.cc b/src/q_fireprof.cc index 021cf2fb..0942a71c 100644 --- a/src/q_fireprof.cc +++ b/src/q_fireprof.cc @@ -17,11 +17,10 @@ #include "z-rand.hpp" #include <cassert> +#include <format.h> #define cquest (quest[QUEST_FIREPROOF]) -#define print_hook(fmt,...) do { fprintf(hook_file, fmt, ##__VA_ARGS__); } while (0) - /* * Per-module "settings" */ @@ -427,7 +426,7 @@ static bool_ fireproof_stair_hook(void *, void *, void *) } } -bool_ quest_fireproof_describe(FILE *hook_file) +std::string quest_fireproof_describe() { fireproof_settings const *settings = fireproof_get_settings(); int num_books, num_staff, num_scroll; @@ -437,37 +436,34 @@ bool_ quest_fireproof_describe(FILE *hook_file) num_staff = get_item_points_remaining() / FIREPROOF_STAFF_POINTS; num_scroll = get_item_points_remaining() / FIREPROOF_SCROLL_POINTS; + fmt::MemoryWriter w; + if (status == QUEST_STATUS_TAKEN) { /* Quest taken */ - print_hook("#####yAn Old Mages Quest!\n"); - print_hook("Retrieve the strange %s for the old mage " - "in Lothlorien.\n", settings->tval_name); - print_hook("\n"); + w.write("#####yAn Old Mages Quest!\n"); + w.write("Retrieve the strange {} for the old mage in Lothlorien.", settings->tval_name); } else if (status == QUEST_STATUS_COMPLETED) { /* essence retrieved, not taken to mage */ - print_hook("#####yAn Old Mages Quest!\n"); - print_hook("You have retrieved the %s for the old " - "mage in Lothlorien. Perhaps you \n", settings->tval_name); - print_hook("should see about a reward.\n"); - print_hook("\n"); + w.write("#####yAn Old Mages Quest!\n"); + w.write("You have retrieved the {} for the old mage in Lothlorien.\n", settings->tval_name); + w.write("Perhaps you should see about a reward."); } else if ((status == QUEST_STATUS_FINISHED) && (get_item_points_remaining() > 0)) { /* essence returned, not all books fireproofed */ - print_hook("#####yAn Old Mages Quest!\n"); - print_hook("You have retrieved the %s for the old " - "mage in Lothlorien. He will still \n", settings->tval_name); - print_hook("fireproof %d book(s) or %d staff/staves " - "or %d scroll(s) for you.\n", - num_books, num_staff, num_scroll); - print_hook("\n"); + w.write("#####yAn Old Mages Quest!\n"); + w.write("You have retrieved the {} for the old " + "mage in Lothlorien. He will still\n", settings->tval_name); + w.write("fireproof {} book(s) or {} staff/staves " + "or {} scroll(s) for you.", + num_books, num_staff, num_scroll); } - return TRUE; + return w.str(); } static bool_ fireproof_gen_hook(void *, void *, void *) diff --git a/src/q_fireprof.hpp b/src/q_fireprof.hpp index 53d368b0..48175ce9 100644 --- a/src/q_fireprof.hpp +++ b/src/q_fireprof.hpp @@ -2,6 +2,8 @@ #include "h-basic.h" +#include <string> + extern void quest_fireproof_building(bool_ *paid, bool_ *recreate); extern bool_ quest_fireproof_init_hook(int q); -extern bool_ quest_fireproof_describe(FILE *fff); +extern std::string quest_fireproof_describe(); diff --git a/src/q_god.cc b/src/q_god.cc index de58bfe7..79213146 100644 --- a/src/q_god.cc +++ b/src/q_god.cc @@ -271,19 +271,20 @@ static std::string make_directions(bool feel_it) } } -bool_ quest_god_describe(FILE *fff) +std::string quest_god_describe() { + fmt::MemoryWriter w; + if (cquest.status == QUEST_STATUS_TAKEN) { auto directions = make_directions(false); - fprintf(fff, "#####yGod quest " FMTs32b "!\n", cquest_quests_given); - fprintf(fff, "Thou art to find the lost temple of thy God and\n"); - fprintf(fff, "to retrieve the lost part of the relic for thy God! \n"); - fprintf(fff, "%s\n", directions.c_str()); - fprintf(fff, "\n"); + w.write("#####yGod quest {}!\n", cquest_quests_given); + w.write("Thou art to find the lost temple of thy God and\n"); + w.write("to retrieve the lost part of the relic for thy God!\n"); + w.write("{}", directions.c_str()); } - return TRUE; + return w.str(); } static void quest_god_place_rand_dung() diff --git a/src/q_god.hpp b/src/q_god.hpp index d9513bdb..d5a993fa 100644 --- a/src/q_god.hpp +++ b/src/q_god.hpp @@ -2,5 +2,7 @@ #include "h-basic.h" -bool_ quest_god_describe(FILE *); +#include <string> + +std::string quest_god_describe(); bool_ quest_god_init_hook(int q); diff --git a/src/q_library.cc b/src/q_library.cc index 52a67291..48c9d0cf 100644 --- a/src/q_library.cc +++ b/src/q_library.cc @@ -18,11 +18,10 @@ #include "z-rand.hpp" #include <cassert> +#include <format.h> #define cquest (quest[QUEST_LIBRARY]) -#define print_hook(fmt,...) do { fprintf(hook_file, fmt, ##__VA_ARGS__); } while (0) - #define MONSTER_LICH 518 #define MONSTER_MONASTIC_LICH 611 #define MONSTER_FLESH_GOLEM 256 @@ -482,25 +481,23 @@ void quest_library_building(bool_ *paid, bool_ *recreate) } } -bool_ quest_library_describe(FILE *hook_file) +std::string quest_library_describe() { + fmt::MemoryWriter w; + if (cquest.status == QUEST_STATUS_TAKEN) { - print_hook("#####yAn Old Mages Quest! (Danger Level: 35)\n"); - print_hook("Make the library safe for the old mage in Minas Anor.\n"); - print_hook("\n"); + w.write("#####yAn Old Mages Quest! (Danger Level: 35)\n"); + w.write("Make the library safe for the old mage in Minas Anor."); } else if (cquest.status == QUEST_STATUS_COMPLETED) { - /* Quest done, book not gotten yet */ - print_hook("#####yAn Old Mages Quest!\n"); - print_hook("You have made the library safe for the old mage in Minas Anor.\n"); - print_hook("Perhaps you should see about a reward.\n"); - print_hook("\n"); + w.write("#####yAn Old Mages Quest!\n"); + w.write("You have made the library safe for the old mage in Minas Anor.\n"); + w.write("Perhaps you should see about a reward."); } - /* Normal processing */ - return TRUE; + return w.str(); } bool_ quest_library_init_hook(int q) diff --git a/src/q_library.hpp b/src/q_library.hpp index 8150893e..4c085472 100644 --- a/src/q_library.hpp +++ b/src/q_library.hpp @@ -2,7 +2,9 @@ #include "h-basic.h" +#include <string> + bool_ quest_library_init_hook(int q); -bool_ quest_library_describe(FILE *fff); +std::string quest_library_describe(); void quest_library_building(bool_ *paid, bool_ *recreate); void initialize_bookable_spells(); diff --git a/src/q_rand.cc b/src/q_rand.cc index 4ef79928..8fb49b34 100644 --- a/src/q_rand.cc +++ b/src/q_rand.cc @@ -28,6 +28,7 @@ #include "z-rand.hpp" #include <cassert> +#include <format.h> static int randquest_hero[] = { 20, 13, 15, 16, 9, 17, 18, 8, -1 }; @@ -615,31 +616,37 @@ static bool_ quest_random_dump_hook(void *, void *in_, void *) return (FALSE); } -bool_ quest_random_describe(FILE *fff) +std::string quest_random_describe() { - if (!(dungeon_flags1 & DF1_PRINCIPAL)) return FALSE; - if ((dun_level < 1) || (dun_level >= MAX_RANDOM_QUEST)) return FALSE; - if (!random_quests[dun_level].type) return FALSE; - if (random_quests[dun_level].done) return FALSE; - if (p_ptr->inside_quest) return FALSE; - if (!dun_level) return FALSE; + // Only emit description if we're actually on a + // random quest level. + if (!(dungeon_flags1 & DF1_PRINCIPAL)) return ""; + if ((dun_level < 1) || (dun_level >= MAX_RANDOM_QUEST)) return ""; + if (!random_quests[dun_level].type) return ""; + if (random_quests[dun_level].done) return ""; + if (p_ptr->inside_quest) return ""; + if (!dun_level) return ""; + + fmt::MemoryWriter w; if (!is_randhero(dun_level)) { - fprintf(fff, "#####yCaptured princess!\n"); - fprintf(fff, "A princess is being held prisoner and tortured here!\n"); - fprintf(fff, "Save her from the horrible %s.\n", r_info[random_quests[dun_level].r_idx].name); + w.write("#####yCaptured princess!\n"); + w.write("A princess is being held prisoner and tortured here!\n"); + w.write("Save her from the horrible {}.\n", r_info[random_quests[dun_level].r_idx].name); } else { - fprintf(fff, "#####yLost sword!\n"); - fprintf(fff, "An adventurer lost his sword to a bunch of %s!\n", r_info[random_quests[dun_level].r_idx].name); - fprintf(fff, "Kill them all to get it back.\n"); + w.write("#####yLost sword!\n"); + w.write("An adventurer lost his sword to a bunch of {}!\n", r_info[random_quests[dun_level].r_idx].name); + w.write("Kill them all to get it back.\n"); } - fprintf(fff, "Number: %d, Killed: %ld.\n", - random_quests[dun_level].type, (long int) quest[QUEST_RANDOM].data[0]); - fprintf(fff, "\n"); - return TRUE; + + w.write("Number: {}, Killed: {}.", + random_quests[dun_level].type, + quest[QUEST_RANDOM].data[0]); + + return w.str(); } bool_ quest_random_init_hook(int q_idx) diff --git a/src/q_rand.hpp b/src/q_rand.hpp index fe87289b..0aa9ccc8 100644 --- a/src/q_rand.hpp +++ b/src/q_rand.hpp @@ -2,7 +2,9 @@ #include "h-basic.h" +#include <string> + void initialize_random_quests(int n); bool_ is_randhero(int level); bool_ quest_random_init_hook(int q_idx); -bool_ quest_random_describe(FILE *fff); +std::string quest_random_describe(); diff --git a/src/quest_type.hpp b/src/quest_type.hpp index aa99f40a..19d9cc87 100644 --- a/src/quest_type.hpp +++ b/src/quest_type.hpp @@ -2,6 +2,8 @@ #include "h-basic.h" +#include <string> + /** * Quest descriptor and runtime data. */ @@ -23,5 +25,5 @@ struct quest_type s32b data[9]; /* Various datas used by the quests */ - bool_ (*gen_desc)(FILE *fff); /* Function for generating description. */ + std::string (*gen_desc)(); /* Function for generating description. */ }; diff --git a/src/status.cc b/src/status.cc index 1886e5a6..c175fd7d 100644 --- a/src/status.cc +++ b/src/status.cc @@ -28,6 +28,7 @@ #include "xtra1.hpp" #include <boost/filesystem.hpp> +#include <format.h> #include <string> static void row_trival(const char*, s16b, u32b, s16b, u32b, int, u32b[INVEN_TOTAL - INVEN_WIELD + 2][7]); @@ -723,20 +724,12 @@ static void row_hd_bon(int which, int yo, u32b flag_arr[INVEN_TOTAL - INVEN_WIEL static void status_companion(void) { - int i; - Term_clear(); - /* Temporary file */ - auto const file_name_p = boost::filesystem::unique_path(); - auto const file_name = file_name_p.c_str(); + fmt::MemoryWriter w; - /* Open a new file */ - FILE *fff = my_fopen(file_name, "w"); - - /* Calculate companions */ /* Process the monsters (backwards) */ - for (i = m_max - 1; i >= 1; i--) + for (int i = m_max - 1; i >= 1; i--) { /* Access the monster */ monster_type *m_ptr = &m_list[i]; @@ -744,40 +737,35 @@ static void status_companion(void) if (m_ptr->status == MSTATUS_COMPANION) { char m_name[80]; - int b, y = 0; - - /* Extract monster name */ monster_desc(m_name, m_ptr, 0x80); - fprintf(fff, "#####BCompanion: %s\n", m_name); + std::string exp_for_next_level = (m_ptr->level < MONSTER_LEVEL_MAX) + ? std::to_string(monster_exp(m_ptr->level + 1)) + : "****"; - fprintf(fff, " Lev/Exp : [[[[[G%d / %ld]\n", m_ptr->level, (long int) m_ptr->exp); - if (m_ptr->level < MONSTER_LEVEL_MAX) fprintf(fff, " Next lvl: [[[[[G%ld]\n", (long int) monster_exp(m_ptr->level + 1)); - else fprintf(fff, " Next lvl: [[[[[G****]\n"); + w.write("#####BCompanion: {}\n", m_name); - fprintf(fff, " HP : [[[[[G%ld / %ld]\n", (long int) m_ptr->hp, (long int) m_ptr->maxhp); - fprintf(fff, " AC : [[[[[G%d]\n", m_ptr->ac); - fprintf(fff, " Speed : [[[[[G%d]\n", m_ptr->mspeed - 110); + w.write(" Lev/Exp : [[[[[G{} / {}]\n", m_ptr->level, m_ptr->exp); + w.write(" Next lvl: [[[[[G{}]\n", exp_for_next_level); - for (b = 0; b < 4; b++) + w.write(" HP : [[[[[G{} / {}]\n", m_ptr->hp, m_ptr->maxhp); + w.write(" AC : [[[[[G{}]\n", m_ptr->ac); + w.write(" Speed : [[[[[G{}]\n", m_ptr->mspeed - 110); + + int y = 0; + for (int b = 0; b < 4; b++) { if (!m_ptr->blow[b].d_dice) continue; if (!m_ptr->blow[b].d_side) continue; - fprintf(fff, " Blow %1d : [[[[[G%dd%d]\n", y + 1, m_ptr->blow[b].d_dice, m_ptr->blow[b].d_side); + w.write(" Blow {} : [[[[[G{}d{}]\n", y + 1, m_ptr->blow[b].d_dice, m_ptr->blow[b].d_side); y++; } - fprintf(fff, "\n"); + w.write("\n"); } } - /* Close the file */ - my_fclose(fff); - - /* Display the file contents */ - show_file(file_name, "Companion List"); - - /* Remove the file */ - fd_kill(file_name); + /* Display */ + show_string(w.c_str(), "Companion List"); } diff --git a/src/xtra1.cc b/src/xtra1.cc index fd4a11fd..ab521c61 100644 --- a/src/xtra1.cc +++ b/src/xtra1.cc @@ -50,6 +50,7 @@ #include "z-rand.hpp" #include <cassert> +#include <format.h> /* * Converts stat num into a six-char (right justified) string @@ -4529,18 +4530,19 @@ void gain_fate(byte fate) } } -void fate_desc(char *desc, int fate) +std::string fate_desc(int fate) { - char buf[120]; + fmt::MemoryWriter w; if (fates[fate].serious) { - strcpy(desc, "You are fated to "); + w.write("You are fated to "); } else { - strcpy(desc, "You may "); + w.write("You may "); } + switch (fates[fate].fate) { case FATE_FIND_O: @@ -4552,8 +4554,7 @@ void fate_desc(char *desc, int fate) object_prep(o_ptr, fates[fate].o_idx); object_desc_store(o_name, o_ptr, 1, 0); - sprintf(buf, "find %s on level %d.", o_name, fates[fate].level); - strcat(desc, buf); + w.write("find {} on level {}.", o_name, fates[fate].level); break; } case FATE_FIND_A: @@ -4605,54 +4606,55 @@ void fate_desc(char *desc, int fate) object_desc_store(o_name, q_ptr, 1, 0); } - sprintf(buf, "find %s on level %d.", o_name, fates[fate].level); - strcat(desc, buf); + w.write("find {} on level {}.", o_name, fates[fate].level); break; } case FATE_FIND_R: { char m_name[80]; - monster_race_desc(m_name, fates[fate].r_idx, 0); - sprintf(buf, "meet %s on level %d.", m_name, fates[fate].level); - strcat(desc, buf); + + w.write("meet {} on level {}.", m_name, fates[fate].level); break; } case FATE_DIE: { - sprintf(buf, "die on level %d.", fates[fate].level); - strcat(desc, buf); + w.write("die on level {}.", fates[fate].level); break; } case FATE_NO_DIE_MORTAL: { - strcat(desc, "never to die by the hand of a mortal being."); + w.write("never to die by the hand of a mortal being."); break; } } + + return w.str(); } -void dump_fates(FILE *outfile) +std::string dump_fates() { - int i; - char buf[120]; bool_ pending = FALSE; - if (!outfile) return; + fmt::MemoryWriter w; - for (i = 0; i < MAX_FATES; i++) + for (int i = 0; i < MAX_FATES; i++) { if ((fates[i].fate) && (fates[i].know)) { - fate_desc(buf, i); - fprintf(outfile, "%s\n", buf); + w.write("{}\n", fate_desc(i)); } - if ((fates[i].fate) && !(fates[i].know)) pending = TRUE; + + // Pending gets set if there's at least one fate we don't know + pending |= ((fates[i].fate) && !(fates[i].know)); } + if (pending) { - fprintf(outfile, "You do not know all of your fate.\n"); + w.write("You do not know all of your fate.\n"); } + + return w.str(); } /* diff --git a/src/xtra1.hpp b/src/xtra1.hpp index df2592ac..e2a41b13 100644 --- a/src/xtra1.hpp +++ b/src/xtra1.hpp @@ -2,6 +2,8 @@ #include "h-basic.h" +#include <string> + extern void fix_message(void); extern void apply_flags(u32b f1, u32b f2, u32b f3, u32b f4, u32b f5, u32b esp, s16b pval, s16b tval, s16b to_h, s16b to_d, s16b to_a); extern int luck(int min, int max); @@ -18,7 +20,7 @@ extern void handle_stuff(void); extern bool_ monk_heavy_armor(void); extern void calc_bonuses(bool_ silent); extern void gain_fate(byte fate); -extern void fate_desc(char *desc, int fate); -extern void dump_fates(FILE *OutFile); +extern std::string fate_desc(int fate); +extern std::string dump_fates(); extern bool race_flags1_p(u32b flags1_mask); extern bool race_flags2_p(u32b flags2_mask); |