OSDN Git Service

[Refactor] #3264 Habu氏のコメントに従い、テーブルをコンパイル時定数へ変えると共に、変数の上下限が決まっている処理をclamp に差し替えた
authorHourier <66951241+Hourier@users.noreply.github.com>
Tue, 16 May 2023 11:47:04 +0000 (20:47 +0900)
committerHourier <66951241+Hourier@users.noreply.github.com>
Tue, 16 May 2023 12:16:06 +0000 (21:16 +0900)
src/birth/birth-stat.cpp
src/wizard/wizard-special-process.cpp

index 80c4e21..fe8f56a 100644 (file)
 #include "spell/spells-status.h"
 #include "sv-definition/sv-weapon-types.h"
 #include "system/player-type-definition.h"
+#include <array>
 
-/*! オートロール能力値の乱数分布 / emulate 5 + 1d3 + 1d4 + 1d5 by randint0(60) */
-short rand3_4_5[60] = {
-    8, 9, 9, 9, 10, 10, 10, 10, 10, 10, /*00-09*/
-    11, 11, 11, 11, 11, 11, 11, 11, 11, 12, /*10-19*/
-    12, 12, 12, 12, 12, 12, 12, 12, 12, 12, /*20-29*/
-    13, 13, 13, 13, 13, 13, 13, 13, 13, 13, /*30-49*/
-    13, 14, 14, 14, 14, 14, 14, 14, 14, 14, /*40-49*/
-    15, 15, 15, 15, 15, 15, 16, 16, 16, 17 /*50-59*/
+namespace {
+
+constexpr auto random_distribution = 60;
+
+/*! オートロール能力値の乱数分布 (1d3, 1d4, 1d5 を3 * 4 * 5 = 60個で表現) */
+constexpr std::array<short, random_distribution> auto_roller_distribution = {
+    {
+        8, 9, 9, 9, 10, 10, 10, 10, 10, 10, /*00-09*/
+        11, 11, 11, 11, 11, 11, 11, 11, 11, 12, /*10-19*/
+        12, 12, 12, 12, 12, 12, 12, 12, 12, 12, /*20-29*/
+        13, 13, 13, 13, 13, 13, 13, 13, 13, 13, /*30-49*/
+        13, 14, 14, 14, 14, 14, 14, 14, 14, 14, /*40-49*/
+        15, 15, 15, 15, 15, 15, 16, 16, 16, 17 /*50-59*/
+    }
 };
+}
 
 /*!
  * @brief プレイヤーの能力値表現に基づいて加減算を行う。
@@ -64,19 +72,15 @@ int adjust_stat(int value, int amount)
 void get_stats(PlayerType *player_ptr)
 {
     while (true) {
-        int sum = 0;
-        for (int i = 0; i < 2; i++) {
-            auto tmp = randint0(60 * 60 * 60);
-            for (int j = 0; j < 3; j++) {
-                int stat = i * 3 + j;
-
-                /* Extract 5 + 1d3 + 1d4 + 1d5 */
-                auto val = rand3_4_5[tmp % 60];
-
+        auto sum = 0;
+        for (auto i = 0; i < 2; i++) {
+            auto tmp = randint0(random_distribution * random_distribution * random_distribution);
+            for (auto j = 0; j < 3; j++) {
+                auto stat = i * 3 + j;
+                auto val = auto_roller_distribution[tmp % random_distribution];
                 sum += val;
                 player_ptr->stat_cur[stat] = player_ptr->stat_max[stat] = val;
-
-                tmp /= 60;
+                tmp /= random_distribution;
             }
         }
 
index cfd5f96..9bf249d 100644 (file)
@@ -402,7 +402,6 @@ void wiz_create_named_art(PlayerType *player_ptr)
  */
 void wiz_change_status(PlayerType *player_ptr)
 {
-    short stat;
     char tmp_val[160];
     char ppp[80];
     for (int i = 0; i < A_MAX; i++) {
@@ -412,14 +411,9 @@ void wiz_change_status(PlayerType *player_ptr)
             return;
         }
 
-        stat = static_cast<short>(atoi(tmp_val));
-        if (stat > player_ptr->stat_max_max[i]) {
-            stat = player_ptr->stat_max_max[i];
-        } else if (stat < 3) {
-            stat = 3;
-        }
-
-        player_ptr->stat_cur[i] = player_ptr->stat_max[i] = stat;
+        auto stat = std::clamp<short>(static_cast<short>(atoi(tmp_val)), 3, player_ptr->stat_max_max[i]);
+        player_ptr->stat_cur[i] = stat;
+        player_ptr->stat_max[i] = stat;
     }
 
     strnfmt(tmp_val, sizeof(tmp_val), "%d", PlayerSkill::weapon_exp_at(PlayerSkillRank::MASTER));