diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lua_bind.cc | 25 | ||||
-rw-r--r-- | src/lua_bind.hpp | 3 | ||||
-rw-r--r-- | src/skills.cc | 24 | ||||
-rw-r--r-- | src/squelch/condition_metadata.cc | 84 | ||||
-rw-r--r-- | src/squeltch.cc | 38 | ||||
-rw-r--r-- | src/util.cc | 59 | ||||
-rw-r--r-- | src/util.hpp | 5 |
7 files changed, 86 insertions, 152 deletions
diff --git a/src/lua_bind.cc b/src/lua_bind.cc index bb305aa3..c2f924ad 100644 --- a/src/lua_bind.cc +++ b/src/lua_bind.cc @@ -230,31 +230,6 @@ void load_map(const char *name, int *y, int *x) process_dungeon_file(name, y, x, cur_hgt, cur_wid, TRUE, TRUE); } -/* - * Some misc functions - */ -char *lua_input_box(cptr title, int max) -{ - static char buf[80]; - int wid, hgt; - - strcpy(buf, ""); - Term_get_size(&wid, &hgt); - if (!input_box(title, hgt / 2, wid / 2, buf, (max > 79) ? 79 : max)) - return buf; - return buf; -} - -char lua_msg_box(cptr title) -{ - int wid, hgt; - - Term_get_size(&wid, &hgt); - return msg_box(title, hgt / 2, wid / 2); -} - - - void increase_mana(int delta) { p_ptr->csp += delta; diff --git a/src/lua_bind.hpp b/src/lua_bind.hpp index 09338949..9882edad 100644 --- a/src/lua_bind.hpp +++ b/src/lua_bind.hpp @@ -24,9 +24,6 @@ extern s32b get_level_use_stick; extern void get_map_size(const char *name, int *ysize, int *xsize); extern void load_map(const char *name, int *y, int *x); -extern char *lua_input_box(cptr title, int max); -extern char lua_msg_box(cptr title); - extern void increase_mana(int delta); extern timer_type *TIMER_AGGRAVATE_EVIL; diff --git a/src/skills.cc b/src/skills.cc index 8572d07f..af5e46c7 100644 --- a/src/skills.cc +++ b/src/skills.cc @@ -44,6 +44,7 @@ #include <boost/algorithm/string/predicate.hpp> #include <cassert> #include <cmath> +#include <fmt/format.h> #include <memory> #include <vector> #include <tuple> @@ -73,15 +74,11 @@ static void increase_skill(int i, s16b *invest) max_skill_overage = modules[game_module_idx].skills.max_skill_overage; if (((s_info[i].value + s_info[i].mod) / SKILL_STEP) >= (p_ptr->lev + max_skill_overage + 1)) { - int hgt, wid; - char buf[256]; - - sprintf(buf, - "Cannot raise a skill value above " FMTs32b " + player level.", - max_skill_overage); - - Term_get_size(&wid, &hgt); - msg_box(buf, hgt / 2, wid / 2); + msg_box_auto( + fmt::format( + "Cannot raise a skill value above {} + player level.", + max_skill_overage + )); return; } @@ -693,7 +690,7 @@ void do_cmd_skill() flush(); /* Ask we can commit the change */ - if (msg_box("Save and use these skill values? (y/n)", hgt / 2, wid / 2) != 'y') + if (msg_box_auto("Save and use these skill values? (y/n)") != 'y') { /* User declines -- restore the skill values before exiting */ @@ -1629,12 +1626,9 @@ static void gain_ability(int ab) { auto const &ab_info = game->edit_data.ab_info; - int wid, hgt; - Term_get_size(&wid, &hgt); - if (!can_learn_ability(ab)) { - msg_box("You cannot learn this ability.", hgt / 2, wid / 2); + msg_box_auto("You cannot learn this ability."); return; } @@ -1642,7 +1636,7 @@ static void gain_ability(int ab) flush(); /* Ask we can commit the change */ - if (msg_box("Learn this ability (this is permanent)? (y/n)", hgt / 2, wid / 2) != 'y') + if (msg_box_auto("Learn this ability (this is permanent)? (y/n)") != 'y') { return; } diff --git a/src/squelch/condition_metadata.cc b/src/squelch/condition_metadata.cc index 62a90e58..5f91bbfb 100644 --- a/src/squelch/condition_metadata.cc +++ b/src/squelch/condition_metadata.cc @@ -14,8 +14,8 @@ namespace squelch { static std::shared_ptr<Condition> create_condition_name() { - cptr s = lua_input_box("Object name to match?", 79); - if (strlen(s) == 0) + auto s = input_box_auto("Object name to match?", 79); + if (s.empty()) { return nullptr; } @@ -25,8 +25,8 @@ static std::shared_ptr<Condition> create_condition_name() static std::shared_ptr<Condition> create_condition_contain() { - cptr s = lua_input_box("Word to find in object name?", 79); - if (strlen(s) == 0) + auto s = input_box_auto("Word to find in object name?", 79); + if (s.empty()) { return nullptr; } @@ -36,8 +36,8 @@ static std::shared_ptr<Condition> create_condition_contain() static std::shared_ptr<Condition> create_condition_inscribed() { - cptr s = lua_input_box("Word to find in object inscription?", 79); - if (strlen(s) == 0) + auto s = input_box_auto("Word to find in object inscription?", 79); + if (s.empty() == 0) { return nullptr; } @@ -50,16 +50,16 @@ static std::shared_ptr<Condition> create_condition_discount() int min, max; { - cptr s = lua_input_box("Min discount?", 79); - if (sscanf(s, "%d", &min) < 1) + auto s = input_box_auto("Min discount?", 79); + if (sscanf(s.c_str(), "%d", &min) < 1) { return nullptr; } } { - cptr s = lua_input_box("Max discount?", 79); - if (sscanf(s, "%d", &max) < 1) + auto s = input_box_auto("Max discount?", 79); + if (sscanf(s.c_str(), "%d", &max) < 1) { return nullptr; } @@ -70,22 +70,20 @@ static std::shared_ptr<Condition> create_condition_discount() static std::shared_ptr<Condition> create_condition_symbol() { - char c; - cptr s = lua_input_box("Symbol to match?", 1); - if (sscanf(s, "%c", &c) < 1) + auto s = input_box_auto("Symbol to match?", 1); + if (s.empty()) { return nullptr; } - return std::make_shared<SymbolCondition>(c); + return std::make_shared<SymbolCondition>(s[0]); } static std::shared_ptr<Condition> create_condition_status() { status_type status; - char c; - c = lua_msg_box("[t]errible, [v]ery bad, [b]ad, " + auto c = msg_box_auto("[t]errible, [v]ery bad, [b]ad, " "[a]verage, [G]ood, [V]ery good, [S]pecial?"); switch (c) @@ -105,7 +103,7 @@ static std::shared_ptr<Condition> create_condition_status() static std::shared_ptr<Condition> create_condition_state() { - char c = lua_msg_box("[i]dentified, [n]on identified?"); + char c = msg_box_auto("[i]dentified, [n]on identified?"); identification_state s; switch (c) @@ -125,9 +123,9 @@ static bool in_byte_range(int x) static std::shared_ptr<Condition> create_condition_tval() { - cptr s = lua_input_box("Tval to match?", 79); + auto s = input_box_auto("Tval to match?", 79); int tval; - if (sscanf(s, "%d", &tval) < 1) + if (sscanf(s.c_str(), "%d", &tval) < 1) { return nullptr; } @@ -145,8 +143,8 @@ static std::shared_ptr<Condition> create_condition_sval() int sval_min, sval_max; { - cptr s = lua_input_box("Min sval?", 79); - if ((sscanf(s, "%d", &sval_min) < 1) || + auto s = input_box_auto("Min sval?", 79); + if ((sscanf(s.c_str(), "%d", &sval_min) < 1) || (!in_byte_range(sval_min))) { return nullptr; @@ -154,8 +152,8 @@ static std::shared_ptr<Condition> create_condition_sval() } { - cptr s = lua_input_box("Max sval?", 79); - if ((sscanf(s, "%d", &sval_max) < 1) || + auto s = input_box_auto("Max sval?", 79); + if ((sscanf(s.c_str(), "%d", &sval_max) < 1) || (!in_byte_range(sval_max))) { return nullptr; @@ -167,8 +165,8 @@ static std::shared_ptr<Condition> create_condition_sval() static std::shared_ptr<Condition> create_condition_race() { - cptr s = lua_input_box("Player race to match?", 79); - if (strlen(s) == 0) + auto s = input_box_auto("Player race to match?", 79); + if (s.empty()) { return nullptr; } @@ -178,8 +176,8 @@ static std::shared_ptr<Condition> create_condition_race() static std::shared_ptr<Condition> create_condition_subrace() { - cptr s = lua_input_box("Player subrace to match?", 79); - if (strlen(s) == 0) + auto s = input_box_auto("Player subrace to match?", 79); + if (s.empty()) { return nullptr; } @@ -189,8 +187,8 @@ static std::shared_ptr<Condition> create_condition_subrace() static std::shared_ptr<Condition> create_condition_class() { - cptr s = lua_input_box("Player class to match?", 79); - if (strlen(s) == 0) + auto s = input_box_auto("Player class to match?", 79); + if (s.empty()) { return nullptr; } @@ -203,16 +201,16 @@ static std::shared_ptr<Condition> create_condition_level() int min, max; { - cptr s = lua_input_box("Min player level?", 79); - if (sscanf(s, "%d", &min) < 1) + auto s = input_box_auto("Min player level?", 79); + if (sscanf(s.c_str(), "%d", &min) < 1) { return nullptr; } } { - cptr s = lua_input_box("Max player level?", 79); - if (sscanf(s, "%d", &max) < 1) + auto s = input_box_auto("Max player level?", 79); + if (sscanf(s.c_str(), "%d", &max) < 1) { return nullptr; } @@ -226,16 +224,16 @@ static std::shared_ptr<Condition> create_condition_skill() int min, max; { - cptr s = lua_input_box("Min skill level?", 79); - if (sscanf(s, "%d", &min) < 1) + auto s = input_box_auto("Min skill level?", 79); + if (sscanf(s.c_str(), "%d", &min) < 1) { return nullptr; } } { - cptr s = lua_input_box("Max skill level?", 79); - if (sscanf(s, "%d", &max) < 1) + auto s = input_box_auto("Max skill level?", 79); + if (sscanf(s.c_str(), "%d", &max) < 1) { return nullptr; } @@ -243,13 +241,13 @@ static std::shared_ptr<Condition> create_condition_skill() s16b skill_idx; { - cptr s = lua_input_box("Skill name?", 79); - if (strlen(s) == 0) + auto s = input_box_auto("Skill name?", 79); + if (s.empty() == 0) { return nullptr; } - skill_idx = find_skill_i(s); + skill_idx = find_skill_i(s.c_str()); if (skill_idx < 0) { return nullptr; @@ -261,13 +259,13 @@ static std::shared_ptr<Condition> create_condition_skill() static std::shared_ptr<Condition> create_condition_ability() { - cptr s = lua_input_box("Ability name?", 79); - if (strlen(s) == 0) + auto s = input_box_auto("Ability name?", 79); + if (s.empty() == 0) { return nullptr; } - s16b ai = find_ability(s); + s16b ai = find_ability(s.c_str()); if (ai < 0) { return nullptr; diff --git a/src/squeltch.cc b/src/squeltch.cc index 8d2db53a..293eab69 100644 --- a/src/squeltch.cc +++ b/src/squeltch.cc @@ -121,18 +121,14 @@ void squeltch_inventory(void) static int create_new_rule() { - char name[20] = { '\0' }; - int wid = 0, hgt = 0; + std::string name = "No name"; - Term_get_size(&wid, &hgt); - - sprintf(name, "%s", "No name"); - if (!input_box("Name?", hgt / 2, wid / 2, name, sizeof(name))) + if (!input_box_auto("Name?", &name, 20)) { return -1; } - char typ = lua_msg_box("[D]estroy, [P]ickup, [I]nscribe?"); + char typ = msg_box_auto("[D]estroy, [P]ickup, [I]nscribe?"); std::shared_ptr<Rule> rule; switch (typ) @@ -150,15 +146,13 @@ static int create_new_rule() case 'i': case 'I': { - cptr i = lua_input_box("Inscription?", 79); - if ((i == nullptr) || (strlen(i) == 0)) + auto s = input_box_auto("Inscription?", 79); + if (s.empty()) { return -1; } - rule = std::make_shared<InscribeRule>( - name, game_module_idx, nullptr, std::string(i)); - + rule = std::make_shared<InscribeRule>(name, game_module_idx, nullptr, s); break; } @@ -179,7 +173,7 @@ static void automatizer_save_rules() std::string name = fmt::format("{}.atm", game->player_name); - if (!input_box("Save name?", hgt / 2, wid / 2, &name, 30)) + if (!input_box_auto("Save name?", &name, 30)) { return; } @@ -243,17 +237,12 @@ static void automatizer_save_rules() static void rename_rule(Rule *rule) { - char name[16]; - int wid, hgt; - assert(rule != nullptr); - Term_get_size(&wid, &hgt); - - sprintf(name, "%s", rule->get_name()); - if (input_box("New name?", hgt / 2, wid / 2, name, sizeof(name) - 1)) + std::string name = rule->get_name(); + if (input_box_auto("New name?", &name, 16)) { - rule->set_name(name); + rule->set_name(name.c_str()); } } @@ -261,18 +250,15 @@ static void rename_rule(Rule *rule) #define ACTIVE_RULE 1 void do_cmd_automatizer() { - int wid = 0, hgt = 0; int active = ACTIVE_LIST; cptr keys; cptr keys2; cptr keys3; std::vector<cptr> rule_names; - Term_get_size(&wid, &hgt); - if (!automatizer_enabled) { - if (msg_box("Automatizer is currently disabled, enable it? (y/n)", hgt / 2, wid / 2) == 'y') + if (msg_box_auto("Automatizer is currently disabled, enable it? (y/n)") == 'y') { automatizer_enabled = TRUE; } @@ -287,6 +273,8 @@ void do_cmd_automatizer() while (1) { Term_clear(); + + int wid, hgt; Term_get_size(&wid, &hgt); automatizer->get_rule_names(&rule_names); diff --git a/src/util.cc b/src/util.cc index 604c8793..1927d050 100644 --- a/src/util.cc +++ b/src/util.cc @@ -3598,59 +3598,40 @@ void display_list(int y, int x, int h, int w, cptr title, cptr *list, int max, i } } -/* - * Creates an input box - */ -bool input_box(cptr text, int y, int x, char *buf, int max) +bool input_box_auto(std::string const &prompt, std::string *buf, std::size_t max) { - int smax = strlen(text); + int wid, hgt; + Term_get_size(&wid, &hgt); + + auto const y = hgt / 2; + auto const x = wid / 2; - if (max > smax) smax = max; - smax++; + auto const smax = std::max(prompt.size(), max) + 1; draw_box(y - 1, x - (smax / 2), 3, smax); - c_put_str(TERM_WHITE, text, y, x - (strlen(text) / 2)); + c_put_str(TERM_WHITE, prompt.c_str(), y, x - (prompt.size() / 2)); Term_gotoxy(x - (smax / 2) + 1, y + 1); return askfor_aux(buf, max); } -/* - * Creates an input box - */ -bool input_box(std::string const &text, int y, int x, std::string *buf, std::size_t max) +std::string input_box_auto(std::string const &title, std::size_t max) { - std::size_t smax = text.size(); - - if (max > smax) - { - smax = max; - } - smax++; - - draw_box(y - 1, x - (smax / 2), 3, smax); - c_put_str(TERM_WHITE, text.c_str(), y, x - (text.size() / 2)); - - Term_gotoxy(x - (smax / 2) + 1, y + 1); - return askfor_aux(buf, max); + std::string buf; + input_box_auto(title, &buf, max); + return buf; } - -/* - * Creates a msg bbox and ask a question - */ -char msg_box(cptr text, int y, int x) +char msg_box_auto(std::string const &text) { - if (x == -1) - { - int wid = 0, hgt = 0; - Term_get_size(&wid, &hgt); - x = wid / 2; - y = hgt / 2; - } + int wid, hgt; + Term_get_size(&wid, &hgt); + + auto const y = hgt / 2; + auto const x = wid / 2; - draw_box(y - 1, x - ((strlen(text) + 1) / 2), 2, strlen(text) + 1); - c_put_str(TERM_WHITE, text, y, x - ((strlen(text) + 1) / 2) + 1); + draw_box(y - 1, x - ((text.size() + 1) / 2), 2, text.size() + 1); + c_put_str(TERM_WHITE, text.c_str(), y, x - ((text.size() + 1) / 2) + 1); return inkey(); } diff --git a/src/util.hpp b/src/util.hpp index 3c489120..90999567 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -8,8 +8,9 @@ #define MAX_IGNORE_KEYMAPS 12 -extern bool input_box(cptr text, int y, int x, char *buf, int max); extern bool input_box(std::string const &text, int y, int x, std::string *buf, std::size_t max); +extern std::string input_box_auto(std::string const &title, std::size_t max); +extern bool input_box_auto(std::string const &prompt, std::string *buf, std::size_t 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 std::string get_player_race_name(int pr, int ps); @@ -67,7 +68,7 @@ extern void strlower(char *buf); extern int test_monster_name(cptr name); extern int test_mego_name(cptr name); extern int test_item_name(cptr name); -extern char msg_box(cptr text, int y, int x); +extern char msg_box_auto(std::string const &title); extern timer_type *new_timer(void (*callback)(), s32b delay); extern int get_keymap_mode(); extern void repeat_push(int what); |