OSDN Git Service

[Refactor] struct player_type を class PlayerType に置換。
[hengbandforosx/hengbandosx.git] / src / mind / mind-power-getter.cpp
index 0e17c44..fdab613 100644 (file)
@@ -1,7 +1,7 @@
 #include "mind/mind-power-getter.h"
 #include "core/asking-player.h"
-#include "core/window-redrawer.h"
 #include "core/stuff-handler.h"
+#include "core/window-redrawer.h"
 #include "game-option/text-display-options.h"
 #include "inventory/inventory-slot-types.h"
 #include "io/command-repeater.h"
 #include "mind/mind-force-trainer.h"
 #include "mind/mind-info.h"
 #include "mind/mind-types.h"
-#include "player/player-class.h"
+#include "player-info/class-info.h"
+#include "player-info/equipment-info.h"
+#include "player/player-status-table.h"
 #include "system/player-type-definition.h"
 #include "term/screen-processor.h"
+#include "timed-effect/player-stun.h"
+#include "timed-effect/timed-effects.h"
+#include "util/enum-converter.h"
 #include "util/int-char-converter.h"
-#include "player/player-status-table.h"
+
+MindPowerGetter::MindPowerGetter(PlayerType *player_ptr)
+    : player_ptr(player_ptr)
+    , menu_line(use_menu ? 1 : 0)
+{
+}
 
 /*!
  * @brief 使用可能な特殊技能を選択する /
  * Allow user to choose a mindcrafter power.
  * @param sn 選択した特殊技能ID、キャンセルの場合-1、不正な選択の場合-2を返す
- * @param only_browse 一覧を見るだけの場合TRUEを返す
- * @return 発動可能な魔法を選択した場合TRUE、キャンセル処理か不正な選択が行われた場合FALSEを返す。
+ * @param only_browse 一覧を見るだけの場合trueを返す
+ * @return 発動可能な魔法を選択した場合true、キャンセル処理か不正な選択が行われた場合falseを返す。
  * @details
- * If a valid spell is chosen, saves it in '*sn' and returns TRUE\n
- * If the user hits escape, returns FALSE, and set '*sn' to -1\n
- * If there are no legal choices, returns FALSE, and sets '*sn' to -2\n
+ * If a valid spell is chosen, saves it in '*sn' and returns true\n
+ * If the user hits escape, returns false, and set '*sn' to -1\n
+ * If there are no legal choices, returns false, and sets '*sn' to -2\n
  *\n
  * The "prompt" should be "cast", "recite", or "study"\n
- * The "known" should be TRUE for cast/pray, FALSE for study\n
+ * The "known" should be true for cast/pray, false for study\n
  *\n
  * nb: This function has a (trivial) display bug which will be obvious\n
  * when you run it. It's probably easy to fix but I haven't tried,\n
  * sorry.\n
  */
-bool get_mind_power(player_type *caster_ptr, SPELL_IDX *sn, bool only_browse)
+bool MindPowerGetter::get_mind_power(SPELL_IDX *sn, bool only_browse)
 {
-    SPELL_IDX i;
-    int num = 0;
-    TERM_LEN y = 1;
-    TERM_LEN x = 10;
-    PERCENTAGE minfail = 0;
-    PLAYER_LEVEL plev = caster_ptr->lev;
-    PERCENTAGE chance = 0;
-    int ask = TRUE;
-    char choice;
+    select_mind_description();
+    if (select_spell_index(sn)) {
+        return true;
+    }
+
+    for (this->index = 0; this->index < MAX_MIND_POWERS; this->index++) {
+        if (mind_ptr->info[this->index].min_lev <= this->player_ptr->lev) {
+            this->num++;
+        }
+    }
+
     char out_val[160];
-    char comment[80];
-    concptr p;
-    COMMAND_CODE code;
-    mind_type spell;
-    const mind_power *mind_ptr;
-    bool flag, redraw;
-    int use_mind;
-    int menu_line = (use_menu ? 1 : 0);
-
-    switch (caster_ptr->pclass) {
-    case CLASS_MINDCRAFTER: {
-        use_mind = MIND_MINDCRAFTER;
-        p = _("超能力", "mindcraft");
-        break;
+    if (only_browse) {
+        (void)strnfmt(out_val, 78, _("(%^s %c-%c, '*'で一覧, ESC) どの%sについて知りますか?", "(%^ss %c-%c, *=List, ESC=exit) Use which %s? "),
+            this->mind_description, I2A(0), I2A(this->num - 1), this->mind_description);
+    } else {
+        (void)strnfmt(out_val, 78, _("(%^s %c-%c, '*'で一覧, ESC) どの%sを使いますか?", "(%^ss %c-%c, *=List, ESC=exit) Use which %s? "),
+            this->mind_description, I2A(0), I2A(this->num - 1), this->mind_description);
     }
-    case CLASS_FORCETRAINER: {
-        use_mind = MIND_KI;
-        p = _("練気術", "Force");
-        break;
+
+    if (use_menu && !only_browse) {
+        screen_save();
     }
-    case CLASS_BERSERKER: {
-        use_mind = MIND_BERSERKER;
-        p = _("技", "brutal power");
-        break;
+
+    this->choice = (always_show_list || use_menu) ? ESCAPE : 1;
+    decide_mind_choice(out_val, only_browse);
+    if (this->redraw && !only_browse) {
+        screen_load();
     }
-    case CLASS_MIRROR_MASTER: {
-        use_mind = MIND_MIRROR_MASTER;
-        p = _("鏡魔法", "magic");
-        break;
+
+    this->player_ptr->window_flags |= PW_SPELL;
+    handle_stuff(this->player_ptr);
+    if (!this->flag) {
+        return false;
     }
-    case CLASS_NINJA: {
-        use_mind = MIND_NINJUTSU;
-        p = _("忍術", "ninjutsu");
+
+    *sn = this->index;
+    repeat_push((COMMAND_CODE)this->index);
+    return true;
+}
+
+/*
+ * @brief 職業ごとの特殊技能表記を取得する
+ */
+void MindPowerGetter::select_mind_description()
+{
+    switch (this->player_ptr->pclass)
+    case PlayerClassType::MINDCRAFTER: {
+        this->use_mind = MindKindType::MINDCRAFTER;
+        this->mind_description = _("超能力", "mindcraft");
         break;
-    }
-    default: {
-        use_mind = 0;
-        p = _("超能力", "mindcraft");
+    case PlayerClassType::FORCETRAINER:
+        this->use_mind = MindKindType::KI;
+        this->mind_description = _("練気術", "Force");
+        break;
+    case PlayerClassType::BERSERKER:
+        this->use_mind = MindKindType::BERSERKER;
+        this->mind_description = _("技", "brutal power");
+        break;
+    case PlayerClassType::MIRROR_MASTER:
+        this->use_mind = MindKindType::MIRROR_MASTER;
+        this->mind_description = _("鏡魔法", "magic");
+        break;
+    case PlayerClassType::NINJA:
+        this->use_mind = MindKindType::NINJUTSU;
+        this->mind_description = _("忍術", "ninjutsu");
+        break;
+    default:
+        this->use_mind = MindKindType::MINDCRAFTER;
+        this->mind_description = _("超能力", "mindcraft");
         break;
     }
-    }
+}
 
-    mind_ptr = &mind_powers[use_mind];
+bool MindPowerGetter::select_spell_index(SPELL_IDX *sn)
+{
+    COMMAND_CODE code;
+    this->mind_ptr = &mind_powers[enum2i(this->use_mind)];
     *sn = -1;
-    if (repeat_pull(&code)) {
-        *sn = (SPELL_IDX)code;
-        if (*sn == INVEN_FORCE)
-            repeat_pull(&code);
-
-        *sn = (SPELL_IDX)code;
-        if (mind_ptr->info[*sn].min_lev <= plev)
-            return TRUE;
+    if (!repeat_pull(&code)) {
+        return false;
     }
 
-    flag = FALSE;
-    redraw = FALSE;
+    *sn = (SPELL_IDX)code;
+    if (*sn == INVEN_FORCE) {
+        (void)repeat_pull(&code);
+    }
 
-    for (i = 0; i < MAX_MIND_POWERS; i++)
-        if (mind_ptr->info[i].min_lev <= plev)
-            num++;
+    *sn = (SPELL_IDX)code;
+    return mind_ptr->info[*sn].min_lev <= this->player_ptr->lev;
+}
 
-    if (only_browse)
-        (void)strnfmt(out_val, 78, _("(%^s %c-%c, '*'で一覧, ESC) どの%sについて知りますか?", "(%^ss %c-%c, *=List, ESC=exit) Use which %s? "), p, I2A(0),
-            I2A(num - 1), p);
-    else
-        (void)strnfmt(
-            out_val, 78, _("(%^s %c-%c, '*'で一覧, ESC) どの%sを使いますか?", "(%^ss %c-%c, *=List, ESC=exit) Use which %s? "), p, I2A(0), I2A(num - 1), p);
+bool MindPowerGetter::decide_mind_choice(char *out_val, const bool only_browse)
+{
+    while (!this->flag) {
+        if (this->choice == ESCAPE) {
+            this->choice = ' ';
+        } else if (!get_com(out_val, &this->choice, true)) {
+            break;
+        }
 
-    if (use_menu && !only_browse)
-        screen_save();
+        if (!interpret_mind_key_input(only_browse)) {
+            return false;
+        }
 
-    choice = (always_show_list || use_menu) ? ESCAPE : 1;
-    while (!flag) {
-        if (choice == ESCAPE)
-            choice = ' ';
-        else if (!get_com(out_val, &choice, TRUE))
-            break;
+        if (display_minds_chance(only_browse)) {
+            continue;
+        }
 
-        if (use_menu && choice != ' ') {
-            switch (choice) {
-            case '0': {
-                if (!only_browse)
-                    screen_load();
+        make_choice_lower();
+        if ((this->index < 0) || (this->index >= this->num)) {
+            bell();
+            continue;
+        }
 
-                return FALSE;
-            }
-            case '8':
-            case 'k':
-            case 'K': {
-                menu_line += (num - 1);
-                break;
-            }
-            case '2':
-            case 'j':
-            case 'J': {
-                menu_line++;
-                break;
-            }
-            case 'x':
-            case 'X':
-            case '\r':
-            case '\n': {
-                i = menu_line - 1;
-                ask = FALSE;
-                break;
-            }
+        this->spell = &mind_ptr->info[this->index];
+        if (this->ask) {
+            char tmp_val[160];
+            (void)strnfmt(tmp_val, 78, _("%sを使いますか?", "Use %s? "), this->spell->name);
+            if (!get_check(tmp_val)) {
+                continue;
             }
+        }
+
+        this->flag = true;
+    }
+
+    return true;
+}
+
+bool MindPowerGetter::interpret_mind_key_input(const bool only_browse)
+{
+    if (!use_menu || this->choice == ' ') {
+        return true;
+    }
 
-            if (menu_line > num)
-                menu_line -= num;
+    switch (this->choice) {
+    case '0':
+        if (!only_browse) {
+            screen_load();
         }
 
-        if ((choice == ' ') || (choice == '*') || (choice == '?') || (use_menu && ask)) {
-            if (!redraw || use_menu) {
-                char psi_desc[80];
-                bool has_weapon[2];
-                redraw = TRUE;
-                if (!only_browse && !use_menu)
-                    screen_save();
-
-                prt("", y, x);
-                put_str(_("名前", "Name"), y, x + 5);
-                put_str(format(_("Lv   %s   失率 効果", "Lv   %s   Fail Info"), ((use_mind == MIND_BERSERKER) || (use_mind == MIND_NINJUTSU)) ? "HP" : "MP"), y,
-                    x + 35);
-                has_weapon[0] = has_melee_weapon(caster_ptr, INVEN_MAIN_HAND);
-                has_weapon[1] = has_melee_weapon(caster_ptr, INVEN_SUB_HAND);
-                for (i = 0; i < MAX_MIND_POWERS; i++) {
-                    int mana_cost;
-                    spell = mind_ptr->info[i];
-                    if (spell.min_lev > plev)
-                        break;
-
-                    chance = spell.fail;
-                    mana_cost = spell.mana_cost;
-                    if (chance) {
-                        chance -= 3 * (plev - spell.min_lev);
-                        chance -= 3 * (adj_mag_stat[caster_ptr->stat_index[mp_ptr->spell_stat]] - 1);
-                        if (use_mind == MIND_KI) {
-                            if (heavy_armor(caster_ptr))
-                                chance += 20;
-
-                            if (caster_ptr->icky_wield[0])
-                                chance += 20;
-                            else if (has_weapon[0])
-
-                                chance += 10;
-                            if (caster_ptr->icky_wield[1])
-                                chance += 20;
-                            else if (has_weapon[1])
-                                chance += 10;
-
-                            if (i == 5) {
-                                int j;
-                                for (j = 0; j < get_current_ki(caster_ptr) / 50; j++)
-                                    mana_cost += (j + 1) * 3 / 2;
-                            }
-                        }
-
-                        if ((use_mind != MIND_BERSERKER) && (use_mind != MIND_NINJUTSU) && (mana_cost > caster_ptr->csp))
-                            chance += 5 * (mana_cost - caster_ptr->csp);
-
-                        chance += caster_ptr->to_m_chance;
-                        minfail = adj_mag_fail[caster_ptr->stat_index[mp_ptr->spell_stat]];
-                        if (chance < minfail)
-                            chance = minfail;
-
-                        if (caster_ptr->stun > 50)
-                            chance += 25;
-                        else if (caster_ptr->stun)
-                            chance += 15;
-
-                        if (use_mind == MIND_KI) {
-                            if (heavy_armor(caster_ptr))
-                                chance += 5;
-                            if (caster_ptr->icky_wield[0])
-                                chance += 5;
-                            if (caster_ptr->icky_wield[1])
-                                chance += 5;
-                        }
-
-                        if (chance > 95)
-                            chance = 95;
-                    }
-
-                    mindcraft_info(caster_ptr, comment, use_mind, i);
-                    if (use_menu) {
-                        if (i == (menu_line - 1))
-                            strcpy(psi_desc, _("  》 ", "  >  "));
-                        else
-                            strcpy(psi_desc, "     ");
-                    } else
-                        sprintf(psi_desc, "  %c) ", I2A(i));
-
-                    strcat(psi_desc,
-                        format("%-30s%2d %4d%s %3d%%%s", spell.name, spell.min_lev, mana_cost,
-                            (((use_mind == MIND_MINDCRAFTER) && (i == 13)) ? _("~", "~ ") : "  "), chance, comment));
-                    prt(psi_desc, y + i + 1, x);
-                }
-
-                prt("", y + i + 1, x);
-            } else if (!only_browse) {
-                redraw = FALSE;
-                screen_load();
-            }
+        return false;
+    case '8':
+    case 'k':
+    case 'K':
+        this->menu_line += (this->num - 1);
+        break;
+    case '2':
+    case 'j':
+    case 'J':
+        this->menu_line++;
+        break;
+    case 'x':
+    case 'X':
+    case '\r':
+    case '\n':
+        this->index = this->menu_line - 1;
+        this->ask = false;
+        break;
+    default:
+        break;
+    }
 
-            continue;
+    if (this->menu_line > this->num) {
+        this->menu_line -= this->num;
+    }
+
+    return true;
+}
+
+bool MindPowerGetter::display_minds_chance(const bool only_browse)
+{
+    if ((this->choice != ' ') && (this->choice != '*') && (this->choice != '?') && (!use_menu || !this->ask)) {
+        return false;
+    }
+
+    if (!this->redraw || use_menu) {
+        this->redraw = true;
+        if (!only_browse && !use_menu) {
+            screen_save();
         }
 
-        if (!use_menu) {
-            ask = isupper(choice);
-            if (ask)
-                choice = (char)tolower(choice);
+        prt("", y, x);
+        put_str(_("名前", "Name"), y, x + 5);
+        put_str(format(_("Lv   %s   失率 効果", "Lv   %s   Fail Info"),
+                    ((this->use_mind == MindKindType::BERSERKER) || (this->use_mind == MindKindType::NINJUTSU)) ? "HP" : "MP"),
+            y, x + 35);
+        display_each_mind_chance();
+        prt("", y + this->index + 1, x);
+        return true;
+    }
 
-            i = (islower(choice) ? A2I(choice) : -1);
+    if (only_browse) {
+        return true;
+    }
+
+    this->redraw = false;
+    screen_load();
+    return true;
+}
+
+void MindPowerGetter::display_each_mind_chance()
+{
+    bool has_weapon[2];
+    has_weapon[0] = has_melee_weapon(this->player_ptr, INVEN_MAIN_HAND);
+    has_weapon[1] = has_melee_weapon(this->player_ptr, INVEN_SUB_HAND);
+    for (this->index = 0; this->index < MAX_MIND_POWERS; this->index++) {
+        this->spell = &mind_ptr->info[this->index];
+        if (this->spell->min_lev > this->player_ptr->lev) {
+            break;
         }
 
-        if ((i < 0) || (i >= num)) {
-            bell();
-            continue;
+        calculate_mind_chance(has_weapon);
+        char comment[80];
+        mindcraft_info(this->player_ptr, comment, this->use_mind, this->index);
+        char psi_desc[80];
+        if (use_menu) {
+            if (this->index == (this->menu_line - 1)) {
+                strcpy(psi_desc, _("  》 ", "  >  "));
+            } else {
+                strcpy(psi_desc, "     ");
+            }
+        } else {
+            sprintf(psi_desc, "  %c) ", I2A(this->index));
         }
 
-        spell = mind_ptr->info[i];
-        if (ask) {
-            char tmp_val[160];
-            (void)strnfmt(tmp_val, 78, _("%sを使いますか?", "Use %s? "), spell.name);
-            if (!get_check(tmp_val))
-                continue;
+        strcat(psi_desc,
+            format("%-30s%2d %4d%s %3d%%%s", this->spell->name, this->spell->min_lev, mana_cost,
+                (((this->use_mind == MindKindType::MINDCRAFTER) && (this->index == 13)) ? _("~", "~ ") : "  "), chance, comment));
+        prt(psi_desc, y + this->index + 1, x);
+    }
+}
+
+void MindPowerGetter::calculate_mind_chance(bool *has_weapon)
+{
+    this->chance = this->spell->fail;
+    this->mana_cost = this->spell->mana_cost;
+    if (this->chance == 0) {
+        return;
+    }
+
+    this->chance -= 3 * (this->player_ptr->lev - this->spell->min_lev);
+    this->chance -= 3 * (adj_mag_stat[this->player_ptr->stat_index[mp_ptr->spell_stat]] - 1);
+    calculate_ki_chance(has_weapon);
+    if ((this->use_mind != MindKindType::BERSERKER) && (this->use_mind != MindKindType::NINJUTSU) && (this->mana_cost > this->player_ptr->csp)) {
+        this->chance += 5 * (this->mana_cost - this->player_ptr->csp);
+    }
+
+    this->chance += this->player_ptr->to_m_chance;
+    PERCENTAGE minfail = adj_mag_fail[this->player_ptr->stat_index[mp_ptr->spell_stat]];
+    if (this->chance < minfail) {
+        this->chance = minfail;
+    }
+
+    auto player_stun = this->player_ptr->effects()->stun();
+    this->chance += player_stun->get_magic_chance_penalty();
+    add_ki_chance();
+    if (this->chance > 95) {
+        this->chance = 95;
+    }
+}
+
+void MindPowerGetter::calculate_ki_chance(bool *has_weapon)
+{
+    if (this->use_mind != MindKindType::KI) {
+        return;
+    }
+
+    if (heavy_armor(this->player_ptr))
+        this->chance += 20;
+
+    if (this->player_ptr->is_icky_wield[0]) {
+        this->chance += 20;
+    } else if (has_weapon[0]) {
+        this->chance += 10;
+    }
+
+    if (this->player_ptr->is_icky_wield[1]) {
+        chance += 20;
+    } else if (has_weapon[1]) {
+        this->chance += 10;
+    }
+
+    if (this->index == 5) {
+        for (auto j = 0; j < get_current_ki(this->player_ptr) / 50; j++) {
+            this->mana_cost += (j + 1) * 3 / 2;
         }
+    }
+}
+
+void MindPowerGetter::add_ki_chance()
+{
+    if (this->use_mind != MindKindType::KI) {
+        return;
+    }
 
-        flag = TRUE;
+    if (heavy_armor(this->player_ptr)) {
+        this->chance += 5;
     }
 
-    if (redraw && !only_browse)
-        screen_load();
+    if (this->player_ptr->is_icky_wield[0]) {
+        this->chance += 5;
+    }
 
-    caster_ptr->window_flags |= PW_SPELL;
-    handle_stuff(caster_ptr);
-    if (!flag)
-        return FALSE;
+    if (this->player_ptr->is_icky_wield[1]) {
+        this->chance += 5;
+    }
+}
+
+void MindPowerGetter::make_choice_lower()
+{
+    if (use_menu) {
+        return;
+    }
+
+    this->ask = (bool)isupper(this->choice);
+    if (this->ask) {
+        this->choice = (char)tolower(this->choice);
+    }
 
-    *sn = i;
-    repeat_push((COMMAND_CODE)i);
-    return TRUE;
+    this->index = (islower(this->choice) ? A2I(this->choice) : -1);
 }