OSDN Git Service

[Refactor] #3772 DragonBreaths クラスを定義してdragonbreath_info 改め dragon_breaths_info へのアクセ...
authorHourier <66951241+Hourier@users.noreply.github.com>
Sat, 2 Dec 2023 11:51:58 +0000 (20:51 +0900)
committerHourier <66951241+Hourier@users.noreply.github.com>
Mon, 4 Dec 2023 10:46:58 +0000 (19:46 +0900)
src/object-activation/activation-breath.cpp
src/object-enchant/dragon-breaths-table.cpp
src/object-enchant/dragon-breaths-table.h
src/system/item-entity.cpp

index b3bd750..d6b06b4 100644 (file)
@@ -31,13 +31,7 @@ bool activate_dragon_breath(PlayerType *player_ptr, ItemEntity *o_ptr)
     }
 
     const auto flags = o_ptr->get_flags();
-    std::vector<std::pair<AttributeType, std::string>> breaths;
-    for (const auto &dragon_breath : dragonbreath_info) {
-        if (flags.has(dragon_breath.flag)) {
-            breaths.push_back({ dragon_breath.type, dragon_breath.name });
-        }
-    }
-
+    const auto breaths = DragonBreaths::get_breaths(flags);
     if (breaths.empty()) {
         return false;
     }
@@ -50,9 +44,9 @@ bool activate_dragon_breath(PlayerType *player_ptr, ItemEntity *o_ptr)
         (void)SpellHex(player_ptr).stop_all_spells();
     }
 
-    const auto t = randint0(breaths.size());
-    msg_format(_("あなたは%sのブレスを吐いた。", "You breathe %s."), breaths[t].second.data());
-    fire_breath(player_ptr, breaths[t].first, dir, 250, 4);
+    const auto &breath = rand_choice(breaths);
+    msg_format(_("あなたは%sのブレスを吐いた。", "You breathe %s."), breath.second.data());
+    fire_breath(player_ptr, breath.first, dir, 250, 4);
     return true;
 }
 
index 2b47584..9b4f809 100644 (file)
@@ -2,12 +2,14 @@
 #include "effect/attribute-types.h"
 #include "locale/language-switcher.h"
 #include "object-enchant/tr-types.h"
+#include <sstream>
 
 /*!
  * @brief 装備耐性に準じたブレス効果の選択テーブル /
  * Define flags, effect type, name for dragon breath activation
  */
-const std::vector<dragonbreath_type> dragonbreath_info = {
+namespace {
+const std::vector<DragonBreathType> dragon_breaths_info = {
     { TR_RES_ACID, AttributeType::ACID, _("酸", "acid") },
     { TR_RES_ELEC, AttributeType::ELEC, _("電撃", "lightning") },
     { TR_RES_FIRE, AttributeType::FIRE, _("火炎", "fire") },
@@ -23,3 +25,38 @@ const std::vector<dragonbreath_type> dragonbreath_info = {
     { TR_RES_CHAOS, AttributeType::CHAOS, _("カオス", "chaos") },
     { TR_RES_DISEN, AttributeType::DISENCHANT, _("劣化", "disenchantment") },
 };
+}
+
+std::vector<std::pair<AttributeType, std::string>> DragonBreaths::get_breaths(const TrFlags &flags)
+{
+    std::vector<std::pair<AttributeType, std::string>> breaths;
+    for (const auto &dragon_breath : dragon_breaths_info) {
+        if (flags.has(dragon_breath.flag)) {
+            breaths.push_back({ dragon_breath.type, dragon_breath.name });
+        }
+    }
+
+    return breaths;
+}
+
+std::string DragonBreaths::build_description(const TrFlags &flags)
+{
+    std::stringstream ss;
+    ss << _("", "breathe ");
+    auto has_multi_breaths = false;
+    for (const auto &dragon_breath : dragon_breaths_info) {
+        if (flags.has_not(dragon_breath.flag)) {
+            continue;
+        }
+
+        if (has_multi_breaths) {
+            ss << _("、", ", ");
+        }
+
+        ss << dragon_breath.name;
+        has_multi_breaths = true;
+    }
+
+    ss << _("のブレス(250)", " (250)");
+    return ss.str();
+}
index c13e3ee..5b07bdb 100644 (file)
@@ -1,13 +1,21 @@
 #pragma once
 
+#include "object-enchant/tr-flags.h"
+#include <string>
+#include <utility>
 #include <vector>
 
 enum tr_type : int;
 enum class AttributeType;
-struct dragonbreath_type {
+class DragonBreathType {
+public:
     tr_type flag;
     AttributeType type;
     std::string name;
 };
 
-extern const std::vector<dragonbreath_type> dragonbreath_info;
+class DragonBreaths {
+public:
+    static std::vector<std::pair<AttributeType, std::string>> get_breaths(const TrFlags &flags);
+    static std::string build_description(const TrFlags &flags);
+};
index e8e684a..9311dc3 100644 (file)
@@ -1067,22 +1067,5 @@ std::string ItemEntity::build_activation_description(const ActivationType &act)
 std::string ItemEntity::build_activation_description_dragon_breath() const
 {
     const auto flags = this->get_flags();
-    std::stringstream ss;
-    ss << _("", "breathe ");
-    auto has_multi_breaths = false;
-    for (const auto &dragon_breath : dragonbreath_info) {
-        if (flags.has_not(dragon_breath.flag)) {
-            continue;
-        }
-
-        if (has_multi_breaths) {
-            ss << _("、", ", ");
-        }
-
-        ss << dragon_breath.name;
-        has_multi_breaths = true;
-    }
-
-    ss << _("のブレス(250)", " (250)");
-    return ss.str();
+    return DragonBreaths::build_description(flags);
 }