From 44ad3f0ef09b9e77159ad04e5f1bcf8b6c69bedf Mon Sep 17 00:00:00 2001 From: Deskull <61610939+sikabane-works@users.noreply.github.com> Date: Thu, 23 Sep 2021 16:06:11 +0900 Subject: [PATCH] =?utf8?q?[Implement]=20get=5Fvalue()=20=E9=96=A2=E6=95=B0?= =?utf8?q?=E3=82=92=E5=AE=9F=E8=A3=85=E3=81=97=E3=81=A6=E3=80=81=E3=82=A6?= =?utf8?q?=E3=82=A3=E3=82=B6=E3=83=BC=E3=83=89=E3=83=A2=E3=83=BC=E3=83=89?= =?utf8?q?=E3=81=AE=E4=B8=80=E9=83=A8=E6=95=B0=E5=80=A4=E5=85=A5=E5=8A=9B?= =?utf8?q?=E3=81=AE=E3=81=BF=E6=B1=8E=E5=8C=96=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/core/asking-player.cpp | 25 +++++++++++++++++++++++++ src/core/asking-player.h | 1 + src/wizard/wizard-special-process.cpp | 14 ++++---------- src/wizard/wizard-spells.cpp | 32 +++++++++----------------------- 4 files changed, 39 insertions(+), 33 deletions(-) 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); } -- 2.11.0