OSDN Git Service

[Implement] get_value() 関数を実装して、ウィザードモードの一部数値入力のみ汎化。
authorDeskull <61610939+sikabane-works@users.noreply.github.com>
Thu, 23 Sep 2021 07:06:11 +0000 (16:06 +0900)
committerDeskull <61610939+sikabane-works@users.noreply.github.com>
Thu, 23 Sep 2021 07:39:46 +0000 (16:39 +0900)
src/core/asking-player.cpp
src/core/asking-player.h
src/wizard/wizard-special-process.cpp
src/wizard/wizard-spells.cpp

index e30fc60..4c489ce 100644 (file)
@@ -16,6 +16,9 @@
 
 #include <climits>
 #include <algorithm>
+#include <iostream>
+#include <string>
+#include <sstream>
 
 /*
  * 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;
+}
index 3664552..577d809 100644 (file)
@@ -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);
index adfceb4..216ab8c 100644 (file)
@@ -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<ARTIFACT_IDX>(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.");
index cb70c0d..69ccf3f 100644 (file)
@@ -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<MONRACE_IDX>(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<MONRACE_IDX>(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);
 }