From dd15ebb045ce25c7477a7d55d543a96c1168289a Mon Sep 17 00:00:00 2001 From: Hourier Date: Thu, 29 Apr 2021 16:32:43 +0900 Subject: [PATCH] [Refactor] #963 Moved update_alignment() from player-status.cpp to alignment.cpp/h --- src/player-info/alignment.cpp | 156 ++++++++++++++++++++++++++++++++++++++---- src/player-info/alignment.h | 5 +- src/player/player-status.cpp | 96 +------------------------- 3 files changed, 149 insertions(+), 108 deletions(-) diff --git a/src/player-info/alignment.cpp b/src/player-info/alignment.cpp index b81378ede..c36b16c56 100644 --- a/src/player-info/alignment.cpp +++ b/src/player-info/alignment.cpp @@ -1,6 +1,20 @@ #include "player-info/alignment.h" +#include "artifact/fixed-art-types.h" #include "game-option/text-display-options.h" +#include "inventory/inventory-slot-types.h" +#include "monster/monster-info.h" +#include "monster-race/monster-race.h" +#include "monster-race/race-flags3.h" +#include "monster/monster-status.h" +#include "player-info/avatar.h" +#include "player/player-race.h" +#include "player/player-status.h" // todo has_melee_weapon()が相互依存している。後で消す +#include "system/floor-type-definition.h" +#include "system/monster-race-definition.h" +#include "system/monster-type-definition.h" +#include "system/object-type-definition.h" #include "system/player-type-definition.h" +#include "util/bit-flags-calculator.h" PlayerAlignment::PlayerAlignment(player_type *creature_ptr) { @@ -8,6 +22,134 @@ PlayerAlignment::PlayerAlignment(player_type *creature_ptr) } /*! + * @brief クリーチャーの抽象的善悪アライメントの表記を返す。 / Return alignment title + * @param with_value 徳の情報と一緒に表示する時だけtrue + * @return アライメントの表記を返す。 + */ +concptr PlayerAlignment::get_alignment_description(bool with_value) +{ + auto s = alignment_label(); + if (with_value || show_actual_value) + return format(_("%s(%ld)", "%s (%ld)"), s, static_cast(this->creature_ptr->alignment)); + + return s; +} + +/* + * @brief アライメント情報の更新 + * @param なし + * @return なし + */ +void PlayerAlignment::update_alignment() +{ + this->reset_alignment(); + floor_type *floor_ptr = creature_ptr->current_floor_ptr; + for (MONSTER_IDX m_idx = floor_ptr->m_max - 1; m_idx >= 1; m_idx--) { + auto *m_ptr = &floor_ptr->m_list[m_idx]; + if (!monster_is_valid(m_ptr)) + continue; + auto *r_ptr = &r_info[m_ptr->r_idx]; + + if (!is_pet(m_ptr)) + continue; + + if (any_bits(r_ptr->flags3, RF3_GOOD)) { + this->set_alignment(r_ptr->level, update_alignment_type::ALIGNMENT_ADDITION); + } + + if (any_bits(r_ptr->flags3, RF3_EVIL)) { + this->set_alignment(r_ptr->level, update_alignment_type::ALIGNMENT_SUBTRACTION); + } + } + + if (creature_ptr->mimic_form) { + switch (creature_ptr->mimic_form) { + case MIMIC_DEMON: + this->set_alignment(200, update_alignment_type::ALIGNMENT_SUBTRACTION); + break; + case MIMIC_DEMON_LORD: + this->set_alignment(200, update_alignment_type::ALIGNMENT_SUBTRACTION); + break; + } + } else { + switch (creature_ptr->prace) { + case RACE_ARCHON: + this->set_alignment(200, update_alignment_type::ALIGNMENT_ADDITION); + break; + case RACE_BALROG: + this->set_alignment(200, update_alignment_type::ALIGNMENT_SUBTRACTION); + break; + + default: + break; + } + } + + for (int i = 0; i < 2; i++) { + if (!has_melee_weapon(creature_ptr, INVEN_MAIN_HAND + i) || (creature_ptr->inventory_list[INVEN_MAIN_HAND + i].name1 != ART_IRON_BALL)) + continue; + + this->set_alignment(1000, update_alignment_type::ALIGNMENT_SUBTRACTION); + } + + int j = 0; + int neutral[2]; + for (int i = 0; i < 8; i++) { + switch (creature_ptr->vir_types[i]) { + case V_JUSTICE: + this->set_alignment(creature_ptr->virtues[i] * 2, update_alignment_type::ALIGNMENT_ADDITION); + break; + case V_CHANCE: + break; + case V_NATURE: + case V_HARMONY: + neutral[j++] = i; + break; + case V_UNLIFE: + this->set_alignment(creature_ptr->virtues[i], update_alignment_type::ALIGNMENT_SUBTRACTION); + break; + default: + this->set_alignment(creature_ptr->virtues[i], update_alignment_type::ALIGNMENT_ADDITION); + break; + } + } + + for (int i = 0; i < j; i++) { + if (creature_ptr->alignment > 0) { + this->set_alignment(creature_ptr->virtues[neutral[i]] / 2, update_alignment_type::ALIGNMENT_SUBTRACTION); + if (creature_ptr->alignment < 0) + this->reset_alignment(); + } else if (creature_ptr->alignment < 0) { + this->set_alignment(creature_ptr->virtues[neutral[i]] / 2, update_alignment_type::ALIGNMENT_ADDITION); + if (creature_ptr->alignment > 0) + this->reset_alignment(); + } + } +} + +void PlayerAlignment::set_alignment(int value, update_alignment_type ua_type) +{ + switch (ua_type) { + case update_alignment_type::ALIGNMENT_SUBSTITUTION: + this->creature_ptr->alignment = value; + return; + case update_alignment_type::ALIGNMENT_ADDITION: + this->creature_ptr->alignment += value; + return; + case update_alignment_type::ALIGNMENT_SUBTRACTION: + this->creature_ptr->alignment -= value; + return; + default: + return; + } +} + +void PlayerAlignment::reset_alignment() +{ + this->set_alignment(0, update_alignment_type::ALIGNMENT_SUBSTITUTION); +} + +/*! * @brief クリーチャーの抽象的善悪アライメントの表記名のみを返す。 / Return only alignment title * @param creature_ptr 算出するクリーチャーの参照ポインタ。 * @return アライメントの表記名 @@ -29,17 +171,3 @@ concptr PlayerAlignment::alignment_label() else return _("大悪", "Chaotic"); } - -/*! - * @brief クリーチャーの抽象的善悪アライメントの表記を返す。 / Return alignment title - * @param creature_ptr 算出するクリーチャーの参照ポインタ。 - * @return アライメントの表記を返す。 - */ -concptr PlayerAlignment::get_alignment_description(bool with_value) -{ - auto s = alignment_label(); - if (with_value || show_actual_value) - return format(_("%s(%ld)", "%s (%ld)"), s, static_cast(this->creature_ptr->alignment)); - - return s; -} diff --git a/src/player-info/alignment.h b/src/player-info/alignment.h index f15068af6..16257c120 100644 --- a/src/player-info/alignment.h +++ b/src/player-info/alignment.h @@ -4,7 +4,7 @@ enum class update_alignment_type { ALIGNMENT_SUBSTITUTION, - ALIGNMENT_ADDTION, + ALIGNMENT_ADDITION, ALIGNMENT_SUBTRACTION, }; @@ -15,7 +15,10 @@ public: PlayerAlignment() = delete; virtual ~PlayerAlignment() = default; concptr get_alignment_description(bool with_value = false); + void update_alignment(); private: player_type *creature_ptr; concptr alignment_label(); + void set_alignment(int value, update_alignment_type ua_type); + void reset_alignment(); }; diff --git a/src/player/player-status.cpp b/src/player/player-status.cpp index c5eacad52..ddaa55d85 100644 --- a/src/player/player-status.cpp +++ b/src/player/player-status.cpp @@ -21,11 +21,9 @@ #include "floor/floor-save.h" #include "floor/floor-util.h" #include "game-option/birth-options.h" -#include "game-option/text-display-options.h" #include "grid/feature.h" #include "grid/grid.h" #include "inventory/inventory-object.h" -#include "inventory/inventory-slot-types.h" #include "io/input-key-acceptor.h" #include "io/write-diary.h" #include "main/sound-definitions-table.h" @@ -39,10 +37,7 @@ #include "monster-race/monster-race.h" #include "monster-race/race-flags1.h" #include "monster-race/race-flags2.h" -#include "monster-race/race-flags3.h" #include "monster-race/race-flags7.h" -#include "monster/monster-info.h" -#include "monster/monster-status.h" #include "monster/monster-update.h" #include "monster/smart-learn-types.h" #include "mutation/mutation-calculator.h" @@ -60,6 +55,7 @@ #include "object/object-mark-types.h" #include "perception/object-perception.h" #include "pet/pet-util.h" +#include "player-info/alignment.h" #include "player-info/avatar.h" #include "player-status/player-basic-statistics.h" #include "player-status/player-hand-types.h" @@ -459,93 +455,6 @@ static void update_bonuses(player_type *creature_ptr) check_no_flowed(creature_ptr); } -static void update_alignment(player_type *creature_ptr) -{ - creature_ptr->alignment = 0; - floor_type *floor_ptr = creature_ptr->current_floor_ptr; - for (MONSTER_IDX m_idx = floor_ptr->m_max - 1; m_idx >= 1; m_idx--) { - monster_type *m_ptr; - monster_race *r_ptr; - m_ptr = &floor_ptr->m_list[m_idx]; - if (!monster_is_valid(m_ptr)) - continue; - r_ptr = &r_info[m_ptr->r_idx]; - - if (!is_pet(m_ptr)) - continue; - - if (any_bits(r_ptr->flags3, RF3_GOOD)) - creature_ptr->alignment += r_ptr->level; - if (any_bits(r_ptr->flags3, RF3_EVIL)) - creature_ptr->alignment -= r_ptr->level; - } - - if (creature_ptr->mimic_form) { - switch (creature_ptr->mimic_form) { - case MIMIC_DEMON: - creature_ptr->alignment -= 200; - break; - case MIMIC_DEMON_LORD: - creature_ptr->alignment -= 200; - break; - } - } else { - switch (creature_ptr->prace) { - case RACE_ARCHON: - creature_ptr->alignment += 200; - break; - case RACE_BALROG: - creature_ptr->alignment -= 200; - break; - - default: - break; - } - } - - for (int i = 0; i < 2; i++) { - if (!has_melee_weapon(creature_ptr, INVEN_MAIN_HAND + i)) - continue; - if (creature_ptr->inventory_list[INVEN_MAIN_HAND + i].name1 != ART_IRON_BALL) - continue; - creature_ptr->alignment -= 1000; - } - - int j = 0; - int neutral[2]; - for (int i = 0; i < 8; i++) { - switch (creature_ptr->vir_types[i]) { - case V_JUSTICE: - creature_ptr->alignment += creature_ptr->virtues[i] * 2; - break; - case V_CHANCE: - break; - case V_NATURE: - case V_HARMONY: - neutral[j++] = i; - break; - case V_UNLIFE: - creature_ptr->alignment -= creature_ptr->virtues[i]; - break; - default: - creature_ptr->alignment += creature_ptr->virtues[i]; - break; - } - } - - for (int i = 0; i < j; i++) { - if (creature_ptr->alignment > 0) { - creature_ptr->alignment -= creature_ptr->virtues[neutral[i]] / 2; - if (creature_ptr->alignment < 0) - creature_ptr->alignment = 0; - } else if (creature_ptr->alignment < 0) { - creature_ptr->alignment += creature_ptr->virtues[neutral[i]] / 2; - if (creature_ptr->alignment > 0) - creature_ptr->alignment = 0; - } - } -} - /*! * @brief プレイヤーの最大HPを更新する / * Update the players maximal hit points @@ -2710,7 +2619,8 @@ void update_creature(player_type *creature_ptr) if (any_bits(creature_ptr->update, (PU_BONUS))) { reset_bits(creature_ptr->update, PU_BONUS); - update_alignment(creature_ptr); + std::unique_ptr alignment(new PlayerAlignment(creature_ptr)); + alignment->update_alignment(); update_bonuses(creature_ptr); } -- 2.11.0