From: Deskull <61610939+sikabane-works@users.noreply.github.com> Date: Thu, 23 Sep 2021 07:06:11 +0000 (+0900) Subject: [Implement] get_value() 関数を実装して、ウィザードモードの一部数値入力のみ汎化。 X-Git-Tag: vmacos3.0.0-alpha52~88^2^2~1 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=44ad3f0ef09b9e77159ad04e5f1bcf8b6c69bedf;p=hengbandforosx%2Fhengbandosx.git [Implement] get_value() 関数を実装して、ウィザードモードの一部数値入力のみ汎化。 --- diff --git a/src/core/asking-player.cpp b/src/core/asking-player.cpp index e30fc604e..4c489ce7d 100644 --- a/src/core/asking-player.cpp +++ b/src/core/asking-player.cpp @@ -16,6 +16,9 @@ #include #include +#include +#include +#include /* * Get some string input at the cursor location. @@ -414,3 +417,25 @@ void pause_line(int row) (void)inkey(); prt("", row, 0); } + +bool get_value(const char *text, int min, int max, int *value) +{ + std::stringstream st; + int val; + char tmp_val[10] = ""; + st << text << "(" << min << "-" << max << "): "; + int digit = std::max(std::to_string(min).length(), std::to_string(max).length()); + while (true) { + if (!get_string(st.str().c_str(), tmp_val, digit)) + return false; + + val = atoi(tmp_val); + + if (min <= val && max >= val) { + break; + } + msg_format(_("%dから%dの間で指定して下さい。", "It must be between %d to %d."), min, max); + } + *value = val; + return true; +} diff --git a/src/core/asking-player.h b/src/core/asking-player.h index 36645520b..577d8095a 100644 --- a/src/core/asking-player.h +++ b/src/core/asking-player.h @@ -19,3 +19,4 @@ bool get_check_strict(player_type *player_ptr, concptr prompt, BIT_FLAGS mode); bool get_com(concptr prompt, char *command, bool z_escape); QUANTITY get_quantity(concptr prompt, QUANTITY max); void pause_line(int row); +bool get_value(const char *text, int min, int max, int *value); diff --git a/src/wizard/wizard-special-process.cpp b/src/wizard/wizard-special-process.cpp index adfceb4fd..216ab8cde 100644 --- a/src/wizard/wizard-special-process.cpp +++ b/src/wizard/wizard-special-process.cpp @@ -219,19 +219,13 @@ void wiz_create_item(player_type *player_ptr) void wiz_create_named_art(player_type *player_ptr, ARTIFACT_IDX a_idx) { if (a_idx <= 0) { - char tmp[80] = ""; - sprintf(tmp, "Artifact ID (1-%d): ", max_a_idx - 1); - char tmp_val[10] = ""; - if (!get_string(tmp, tmp_val, 3)) + int val; + if (!get_value("ArtifactID", 1, a_info.size() - 1, &val)) { return; - - a_idx = (ARTIFACT_IDX)atoi(tmp_val); + } + a_idx = static_cast(val); } - if (a_idx <= 0 || a_idx >= max_a_idx) { - msg_format(_("番号は1から%dの間で指定して下さい。", "ID must be between 1 to %d."), max_a_idx - 1); - return; - } (void)create_named_art(player_ptr, a_idx, player_ptr->y, player_ptr->x); msg_print("Allocated."); diff --git a/src/wizard/wizard-spells.cpp b/src/wizard/wizard-spells.cpp index cb70c0de9..69ccf3fb6 100644 --- a/src/wizard/wizard-spells.cpp +++ b/src/wizard/wizard-spells.cpp @@ -30,6 +30,7 @@ #include "spell/summon-types.h" #include "system/floor-type-definition.h" #include "system/player-type-definition.h" +#include "system/monster-race-definition.h" #include "target/grid-selector.h" #include "target/target-checker.h" #include "target/target-getter.h" @@ -183,18 +184,11 @@ void wiz_summon_random_enemy(player_type *player_ptr, int num) void wiz_summon_specific_enemy(player_type *player_ptr, MONRACE_IDX r_idx) { if (r_idx <= 0) { - char tmp[80] = ""; - sprintf(tmp, "Monster ID (1-%d): ", max_r_idx - 1); - char tmp_val[10] = ""; - if (!get_string(tmp, tmp_val, 4)) + int val; + if(!get_value("MonsterID", 1, r_info.size() - 1, &val)) { return; - - r_idx = (MONRACE_IDX)atoi(tmp_val); - } - - if (r_idx <= 0 || r_idx >= max_r_idx) { - msg_format(_("番号は1から%dの間で指定して下さい。", "ID must be between 1 to %d."), max_r_idx - 1); - return; + } + r_idx = static_cast(val); } (void)summon_named_creature(player_ptr, 0, player_ptr->y, player_ptr->x, r_idx, PM_ALLOW_SLEEP | PM_ALLOW_GROUP); } @@ -209,20 +203,12 @@ void wiz_summon_specific_enemy(player_type *player_ptr, MONRACE_IDX r_idx) void wiz_summon_pet(player_type *player_ptr, MONRACE_IDX r_idx) { if (r_idx <= 0) { - char tmp[80] = ""; - sprintf(tmp, "Monster ID (1-%d): ", max_r_idx - 1); - char tmp_val[10] = ""; - if (!get_string(tmp, tmp_val, 4)) + int val; + if (!get_value("MonsterID", 1, r_info.size() - 1, &val)) { return; - - r_idx = (MONRACE_IDX)atoi(tmp_val); - } - - if (r_idx <= 0 || r_idx >= max_r_idx) { - msg_format(_("番号は1から%dの間で指定して下さい。", "ID must be between 1 to %d."), max_r_idx - 1); - return; + } + r_idx = static_cast(val); } - (void)summon_named_creature(player_ptr, 0, player_ptr->y, player_ptr->x, r_idx, PM_ALLOW_SLEEP | PM_ALLOW_GROUP | PM_FORCE_PET); }