OSDN Git Service

[Refactor] #2816 魔法書の判定をis_spell_book() にまとめた
authorHourier <66951241+Hourier@users.noreply.github.com>
Sat, 19 Nov 2022 06:29:19 +0000 (15:29 +0900)
committerHourier <66951241+Hourier@users.noreply.github.com>
Sat, 19 Nov 2022 09:47:38 +0000 (18:47 +0900)
13 files changed:
src/autopick/autopick-entry.cpp
src/autopick/autopick-matcher.cpp
src/cmd-item/cmd-destroy.cpp
src/flavor/named-item-describer.cpp
src/knowledge/knowledge-items.cpp
src/object-hook/hook-magic.cpp
src/object-hook/hook-magic.h
src/object/object-info.cpp
src/object/object-kind-hook.cpp
src/system/baseitem-info.cpp
src/system/baseitem-info.h
src/system/item-entity.cpp
src/system/item-entity.h

index ab7f374..1585f78 100644 (file)
@@ -433,7 +433,8 @@ void autopick_entry_from_object(PlayerType *player_ptr, autopick_type *entry, It
         ADD_FLG(FLG_HUMAN);
     }
 
-    if (o_ptr->tval >= ItemKindType::LIFE_BOOK && !check_book_realm(player_ptr, BaseitemKey(o_ptr->tval, o_ptr->sval))) {
+    const BaseitemKey bi_key(o_ptr->tval, o_ptr->sval);
+    if (o_ptr->is_spell_book() && !check_book_realm(player_ptr, bi_key)) {
         ADD_FLG(FLG_UNREADABLE);
         if (o_ptr->tval != ItemKindType::ARCANE_BOOK) {
             name = false;
@@ -453,16 +454,16 @@ void autopick_entry_from_object(PlayerType *player_ptr, autopick_type *entry, It
         name = false;
     }
 
-    if (o_ptr->tval >= ItemKindType::LIFE_BOOK && 0 == o_ptr->sval) {
+    if (o_ptr->is_spell_book() && (o_ptr->sval == 0)) {
         ADD_FLG(FLG_FIRST);
     }
-    if (o_ptr->tval >= ItemKindType::LIFE_BOOK && 1 == o_ptr->sval) {
+    if (o_ptr->is_spell_book() && (o_ptr->sval == 1)) {
         ADD_FLG(FLG_SECOND);
     }
-    if (o_ptr->tval >= ItemKindType::LIFE_BOOK && 2 == o_ptr->sval) {
+    if (o_ptr->is_spell_book() && (o_ptr->sval == 2)) {
         ADD_FLG(FLG_THIRD);
     }
-    if (o_ptr->tval >= ItemKindType::LIFE_BOOK && 3 == o_ptr->sval) {
+    if (o_ptr->is_spell_book() && (o_ptr->sval == 3)) {
         ADD_FLG(FLG_FOURTH);
     }
 
@@ -476,7 +477,7 @@ void autopick_entry_from_object(PlayerType *player_ptr, autopick_type *entry, It
         ADD_FLG(FLG_JUNKS);
     } else if (o_ptr->tval == ItemKindType::CORPSE) {
         ADD_FLG(FLG_CORPSES);
-    } else if (o_ptr->tval >= ItemKindType::LIFE_BOOK) {
+    } else if (o_ptr->is_spell_book()) {
         ADD_FLG(FLG_SPELLBOOKS);
     } else if (o_ptr->tval == ItemKindType::POLEARM || o_ptr->tval == ItemKindType::SWORD || o_ptr->tval == ItemKindType::DIGGING || o_ptr->tval == ItemKindType::HAFTED) {
         ADD_FLG(FLG_WEAPONS);
index 4512782..39fd8a1 100644 (file)
@@ -207,7 +207,8 @@ bool is_autopick_match(PlayerType *player_ptr, ItemEntity *o_ptr, autopick_type
         return false;
     }
 
-    if (IS_FLG(FLG_UNREADABLE) && (o_ptr->tval < ItemKindType::LIFE_BOOK || check_book_realm(player_ptr, { o_ptr->tval, o_ptr->sval }))) {
+    const BaseitemKey bi_key(o_ptr->tval, o_ptr->sval);
+    if (IS_FLG(FLG_UNREADABLE) && check_book_realm(player_ptr, bi_key)) {
         return false;
     }
 
@@ -222,19 +223,19 @@ bool is_autopick_match(PlayerType *player_ptr, ItemEntity *o_ptr, autopick_type
         return false;
     }
 
-    if (IS_FLG(FLG_FIRST) && ((o_ptr->tval < ItemKindType::LIFE_BOOK) || (o_ptr->sval) != 0)) {
+    if (IS_FLG(FLG_FIRST) && (!o_ptr->is_spell_book() || (o_ptr->sval != 0))) {
         return false;
     }
 
-    if (IS_FLG(FLG_SECOND) && ((o_ptr->tval < ItemKindType::LIFE_BOOK) || (o_ptr->sval) != 1)) {
+    if (IS_FLG(FLG_SECOND) && (!o_ptr->is_spell_book() || (o_ptr->sval != 1))) {
         return false;
     }
 
-    if (IS_FLG(FLG_THIRD) && ((o_ptr->tval < ItemKindType::LIFE_BOOK) || (o_ptr->sval) != 2)) {
+    if (IS_FLG(FLG_THIRD) && (!o_ptr->is_spell_book() || (o_ptr->sval != 2))) {
         return false;
     }
 
-    if (IS_FLG(FLG_FOURTH) && ((o_ptr->tval < ItemKindType::LIFE_BOOK) || (o_ptr->sval) != 3)) {
+    if (IS_FLG(FLG_FOURTH) && (!o_ptr->is_spell_book() || (o_ptr->sval != 3))) {
         return false;
     }
 
@@ -283,7 +284,7 @@ bool is_autopick_match(PlayerType *player_ptr, ItemEntity *o_ptr, autopick_type
             return false;
         }
     } else if (IS_FLG(FLG_SPELLBOOKS)) {
-        if (!(o_ptr->tval >= ItemKindType::LIFE_BOOK)) {
+        if (!o_ptr->is_spell_book()) {
             return false;
         }
     } else if (IS_FLG(FLG_HAFTED)) {
index 3176781..eaaaad2 100644 (file)
@@ -30,6 +30,7 @@
 #include "realm/realm-names-table.h"
 #include "status/action-setter.h"
 #include "status/experience.h"
+#include "system/baseitem-info.h"
 #include "system/item-entity.h"
 #include "system/player-type-definition.h"
 #include "term/screen-processor.h"
@@ -126,37 +127,30 @@ static bool decide_magic_book_exp(PlayerType *player_ptr, destroy_type *destroy_
 
     PlayerClass pc(player_ptr);
     if (pc.equals(PlayerClassType::WARRIOR) || pc.equals(PlayerClassType::BERSERKER)) {
-        return true;
+        return destroy_ptr->q_ptr->tval != ItemKindType::HISSATSU_BOOK;
     }
 
     if (!pc.equals(PlayerClassType::PALADIN)) {
         return false;
     }
 
-    auto gain_expr = false;
     const auto tval = destroy_ptr->o_ptr->tval;
     auto is_good_magic_realm = (tval == ItemKindType::LIFE_BOOK) || (tval == ItemKindType::CRUSADE_BOOK);
     if (is_good_realm(player_ptr->realm1)) {
-        if (!is_good_magic_realm) {
-            gain_expr = true;
-        }
+        return !is_good_magic_realm;
     } else {
-        if (is_good_magic_realm) {
-            gain_expr = true;
-        }
+        return is_good_magic_realm;
     }
-
-    return gain_expr;
 }
 
 static void gain_exp_by_destroying_magic_book(PlayerType *player_ptr, destroy_type *destroy_ptr)
 {
-    bool gain_expr = decide_magic_book_exp(player_ptr, destroy_ptr);
+    const auto gain_expr = decide_magic_book_exp(player_ptr, destroy_ptr);
     if (!gain_expr || (player_ptr->exp >= PY_MAX_EXP)) {
         return;
     }
 
-    int32_t tester_exp = player_ptr->max_exp / 20;
+    auto tester_exp = player_ptr->max_exp / 20;
     if (tester_exp > 10000) {
         tester_exp = 10000;
     }
@@ -175,15 +169,18 @@ static void gain_exp_by_destroying_magic_book(PlayerType *player_ptr, destroy_ty
 
 static void process_destroy_magic_book(PlayerType *player_ptr, destroy_type *destroy_ptr)
 {
-    if (!item_tester_high_level_book(destroy_ptr->q_ptr)) {
+    const auto *q_ptr = destroy_ptr->q_ptr;
+    const BaseitemKey bi_key(q_ptr->tval, q_ptr->sval);
+    if (!bi_key.is_high_level_book()) {
         return;
     }
 
+    const auto tval = bi_key.tval();
     gain_exp_by_destroying_magic_book(player_ptr, destroy_ptr);
-    if (item_tester_high_level_book(destroy_ptr->q_ptr) && destroy_ptr->q_ptr->tval == ItemKindType::LIFE_BOOK) {
+    if (tval == ItemKindType::LIFE_BOOK) {
         chg_virtue(player_ptr, V_UNLIFE, 1);
         chg_virtue(player_ptr, V_VITALITY, -1);
-    } else if (item_tester_high_level_book(destroy_ptr->q_ptr) && destroy_ptr->q_ptr->tval == ItemKindType::DEATH_BOOK) {
+    } else if (tval == ItemKindType::DEATH_BOOK) {
         chg_virtue(player_ptr, V_UNLIFE, -1);
         chg_virtue(player_ptr, V_VITALITY, 1);
     }
index 6575133..e36c1fb 100644 (file)
@@ -410,7 +410,7 @@ void describe_named_item(PlayerType *player_ptr, flavor_type *flavor_ptr)
 
     describe_artifact_ja(flavor_ptr);
 #endif
-    if (flavor_ptr->o_ptr->is_book()) {
+    if (flavor_ptr->o_ptr->is_spell_book()) {
         // svalは0から数えているので表示用に+1している
         flavor_ptr->t = object_desc_str(flavor_ptr->t, format("Lv%d ", flavor_ptr->o_ptr->sval + 1));
     }
index 0b50939..a233d43 100644 (file)
@@ -156,7 +156,7 @@ static short collect_objects(int grp_cur, short object_idx[], BIT_FLAGS8 mode)
 
         const auto tval = k_ref.bi_key.tval();
         if (group_tval == ItemKindType::LIFE_BOOK) {
-            if (ItemKindType::LIFE_BOOK <= tval && tval <= ItemKindType::HEX_BOOK) {
+            if (k_ref.bi_key.is_spell_book()) {
                 object_idx[object_cnt++] = k_ref.idx;
             } else {
                 continue;
index 5b8bd9f..9b4967f 100644 (file)
@@ -7,6 +7,7 @@
 #include "player-info/class-info.h"
 #include "player/player-realm.h"
 #include "realm/realm-names-table.h"
+#include "system/baseitem-info.h"
 #include "system/item-entity.h"
 #include "system/player-type-definition.h"
 #include "util/bit-flags-calculator.h"
@@ -58,6 +59,10 @@ bool item_tester_hook_use(PlayerType *player_ptr, const ItemEntity *o_ptr)
  */
 bool item_tester_learn_spell(PlayerType *player_ptr, const ItemEntity *o_ptr)
 {
+    if (!o_ptr->is_spell_book()) {
+        return false;
+    }
+
     int32_t choices = realm_choices2[enum2i(player_ptr->pclass)];
     PlayerClass pc(player_ptr);
     if (pc.equals(PlayerClassType::PRIEST)) {
@@ -68,33 +73,13 @@ bool item_tester_learn_spell(PlayerType *player_ptr, const ItemEntity *o_ptr)
         }
     }
 
-    if ((o_ptr->tval < ItemKindType::LIFE_BOOK) || (o_ptr->tval > ItemKindType::HEX_BOOK)) {
-        return false;
-    }
-
     if ((o_ptr->tval == ItemKindType::MUSIC_BOOK) && pc.equals(PlayerClassType::BARD)) {
         return true;
-    } else if (!is_magic(tval2realm(o_ptr->tval))) {
-        return false;
     }
 
-    return (get_realm1_book(player_ptr) == o_ptr->tval) || (get_realm2_book(player_ptr) == o_ptr->tval) || (choices & (0x0001U << (tval2realm(o_ptr->tval) - 1)));
-}
-
-/*!
- * @brief オブジェクトが高位の魔法書かどうかを判定する
- * @param o_ptr 判定したいオブジェクトの構造体参照ポインタ
- * @return オブジェクトが高位の魔法書ならばTRUEを返す
- */
-bool item_tester_high_level_book(const ItemEntity *o_ptr)
-{
-    if ((o_ptr->tval == ItemKindType::LIFE_BOOK) || (o_ptr->tval == ItemKindType::SORCERY_BOOK) || (o_ptr->tval == ItemKindType::NATURE_BOOK) || (o_ptr->tval == ItemKindType::CHAOS_BOOK) || (o_ptr->tval == ItemKindType::DEATH_BOOK) || (o_ptr->tval == ItemKindType::TRUMP_BOOK) || (o_ptr->tval == ItemKindType::CRAFT_BOOK) || (o_ptr->tval == ItemKindType::DEMON_BOOK) || (o_ptr->tval == ItemKindType::CRUSADE_BOOK) || (o_ptr->tval == ItemKindType::MUSIC_BOOK) || (o_ptr->tval == ItemKindType::HEX_BOOK)) {
-        if (o_ptr->sval > 1) {
-            return true;
-        } else {
-            return false;
-        }
+    if (!is_magic(tval2realm(o_ptr->tval))) {
+        return false;
     }
 
-    return false;
+    return (get_realm1_book(player_ptr) == o_ptr->tval) || (get_realm2_book(player_ptr) == o_ptr->tval) || (choices & (0x0001U << (tval2realm(o_ptr->tval) - 1)));
 }
index a6c7326..c29e4de 100644 (file)
@@ -4,4 +4,3 @@ class ItemEntity;
 class PlayerType;
 bool item_tester_hook_use(PlayerType *player_ptr, const ItemEntity *o_ptr);
 bool item_tester_learn_spell(PlayerType *player_ptr, const ItemEntity *o_ptr);
-bool item_tester_high_level_book(const ItemEntity *o_ptr);
index 3235eb6..1474908 100644 (file)
@@ -293,11 +293,11 @@ int16_t wield_slot(PlayerType *player_ptr, const ItemEntity *o_ptr)
  */
 bool check_book_realm(PlayerType *player_ptr, const BaseitemKey &bi_key)
 {
-    const auto tval = bi_key.tval();
-    if (tval < ItemKindType::LIFE_BOOK) {
+    if (!bi_key.is_spell_book()) {
         return false;
     }
 
+    const auto tval = bi_key.tval();
     PlayerClass pc(player_ptr);
     if (pc.equals(PlayerClassType::SORCERER)) {
         return is_magic(tval2realm(tval));
index 13e483d..eae6518 100644 (file)
@@ -50,25 +50,25 @@ bool kind_is_sword(short bi_id)
 }
 
 /*!
- * @brief オブジェクトが魔法書かどうかを判定する /
+ * @brief オブジェクトが魔法書かどうかを判定する
  * @param bi_id 判定したいオブジェクトのベースアイテムID
  * @return オブジェクトが魔法書ならばTRUEを返す
  */
 bool kind_is_book(short bi_id)
 {
     const auto &k_ref = baseitems_info[bi_id];
-    return (k_ref.bi_key.tval() >= ItemKindType::LIFE_BOOK) && (k_ref.bi_key.tval() <= ItemKindType::CRUSADE_BOOK);
+    return k_ref.bi_key.is_spell_book();
 }
 
 /*!
- * @brief オブジェクトがベースアイテム時点でGOODかどうかを判定する /
+ * @brief オブジェクトがベースアイテム時点でGOODかどうかを判定する
  * @param bi_id 判定したいオブジェクトのベースアイテムID
  * @return オブジェクトがベースアイテム時点でGOODなアイテムならばTRUEを返す
  */
 bool kind_is_good_book(short bi_id)
 {
     const auto &k_ref = baseitems_info[bi_id];
-    return (k_ref.bi_key.tval() >= ItemKindType::LIFE_BOOK) && (k_ref.bi_key.tval() <= ItemKindType::CRUSADE_BOOK) && (k_ref.bi_key.tval() != ItemKindType::ARCANE_BOOK) && (k_ref.bi_key.sval() > 1);
+    return k_ref.bi_key.is_high_level_book();
 }
 
 /*!
index f14ca04..8dacbec 100644 (file)
@@ -78,6 +78,41 @@ ItemKindType BaseitemKey::get_arrow_kind() const
     }
 }
 
+bool BaseitemKey::is_spell_book() const
+{
+    switch (this->type_value) {
+    case ItemKindType::LIFE_BOOK:
+    case ItemKindType::SORCERY_BOOK:
+    case ItemKindType::NATURE_BOOK:
+    case ItemKindType::CHAOS_BOOK:
+    case ItemKindType::DEATH_BOOK:
+    case ItemKindType::TRUMP_BOOK:
+    case ItemKindType::ARCANE_BOOK:
+    case ItemKindType::CRAFT_BOOK:
+    case ItemKindType::DEMON_BOOK:
+    case ItemKindType::CRUSADE_BOOK:
+    case ItemKindType::MUSIC_BOOK:
+    case ItemKindType::HISSATSU_BOOK:
+    case ItemKindType::HEX_BOOK:
+        return true;
+    default:
+        return false;
+    }
+}
+
+bool BaseitemKey::is_high_level_book() const
+{
+    if (!this->is_spell_book()) {
+        return false;
+    }
+
+    if (this->type_value == ItemKindType::ARCANE_BOOK) {
+        return false;
+    }
+
+    return this->subtype_value >= 2;
+}
+
 BaseitemInfo::BaseitemInfo()
     : bi_key(ItemKindType::NONE)
 {
index 238c993..104158b 100644 (file)
@@ -38,6 +38,8 @@ public:
     std::optional<int> sval() const;
 
     ItemKindType get_arrow_kind() const;
+    bool is_spell_book() const;
+    bool is_high_level_book() const;
 
 private:
     ItemKindType type_value;
index ec54143..e338300 100644 (file)
@@ -568,7 +568,7 @@ bool ItemEntity::is_activatable() const
 bool ItemEntity::is_fuel() const
 {
     auto is_fuel = (this->tval == ItemKindType::LITE) && ((this->sval == SV_LITE_TORCH) || (this->sval == SV_LITE_LANTERN));
-    is_fuel |= (this->tval == ItemKindType::FLASK) && (this->sval == SV_FLASK_OIL);
+    is_fuel |= BaseitemKey(this->tval, this->sval) == BaseitemKey(ItemKindType::FLASK, SV_FLASK_OIL);
     return is_fuel;
 }
 
@@ -576,26 +576,9 @@ bool ItemEntity::is_fuel() const
  * @brief オブジェクトが魔法書かどうかを判定する
  * @return 魔法書か否か
  */
-bool ItemEntity::is_book() const
+bool ItemEntity::is_spell_book() const
 {
-    switch (this->tval) {
-    case ItemKindType::LIFE_BOOK:
-    case ItemKindType::SORCERY_BOOK:
-    case ItemKindType::NATURE_BOOK:
-    case ItemKindType::CHAOS_BOOK:
-    case ItemKindType::DEATH_BOOK:
-    case ItemKindType::TRUMP_BOOK:
-    case ItemKindType::ARCANE_BOOK:
-    case ItemKindType::CRAFT_BOOK:
-    case ItemKindType::DEMON_BOOK:
-    case ItemKindType::CRUSADE_BOOK:
-    case ItemKindType::MUSIC_BOOK:
-    case ItemKindType::HISSATSU_BOOK:
-    case ItemKindType::HEX_BOOK:
-        return true;
-    default:
-        return false;
-    }
+    return BaseitemKey(this->tval).is_spell_book();
 }
 
 /*!
index c351aaf..1b44dd8 100644 (file)
@@ -113,7 +113,7 @@ public:
     bool is_offerable() const;
     bool is_activatable() const;
     bool is_fuel() const;
-    bool is_book() const;
+    bool is_spell_book() const;
     bool is_glove_same_temper(const ItemEntity *j_ptr) const;
     bool can_pile(const ItemEntity *j_ptr) const;
     TERM_COLOR get_color() const;