1 #include "spell-realm/spells-hex.h"
2 #include "core/asking-player.h"
3 #include "core/player-redraw-types.h"
4 #include "core/player-update-types.h"
5 #include "core/window-redrawer.h"
6 #include "effect/effect-characteristics.h"
7 #include "effect/effect-processor.h"
8 #include "monster-attack/monster-attack-util.h"
9 #include "monster-race/monster-race.h"
10 #include "player/attack-defense-types.h"
11 #include "player/player-skill.h"
12 #include "realm/realm-hex-numbers.h"
13 #include "spell-kind/spells-teleport.h"
14 #include "spell-realm/spells-crusade.h"
15 #include "spell-realm/spells-song.h"
16 #include "spell/spell-info.h"
17 #include "spell/spell-types.h"
18 #include "spell/spells-execution.h"
19 #include "spell/technic-info-table.h"
20 #include "status/action-setter.h"
21 #include "system/floor-type-definition.h"
22 #include "system/monster-race-definition.h"
23 #include "system/monster-type-definition.h"
24 #include "system/player-type-definition.h"
25 #include "term/screen-processor.h"
26 #include "util/bit-flags-calculator.h"
27 #include "util/int-char-converter.h"
28 #include "view/display-messages.h"
32 #include "monster/monster-describer.h"
33 #include "monster/monster-description-types.h"
37 constexpr int MAX_KEEP = 4;
39 SpellHex::SpellHex(player_type *player_ptr)
40 : player_ptr(player_ptr)
42 constexpr int max_realm_spells = 32;
43 for (auto spell = 0; spell < max_realm_spells; spell++) {
44 if (this->is_spelling_specific(spell)) {
45 this->casting_spells.push_back(spell);
49 if (this->casting_spells.size() > MAX_KEEP) {
50 throw("Invalid numbers of hex magics keep!");
54 SpellHex::SpellHex(player_type *player_ptr, monap_type *monap_ptr)
55 : player_ptr(player_ptr)
56 , monap_ptr(monap_ptr)
61 * @brief プレイヤーが詠唱中の全呪術を停止する
63 bool SpellHex::stop_all_spells()
65 for (auto spell : this->casting_spells) {
66 exe_spell(this->player_ptr, REALM_HEX, spell, SPELL_STOP);
69 this->player_ptr->magic_num1[0] = 0;
70 this->player_ptr->magic_num2[0] = 0;
71 if (this->player_ptr->action == ACTION_SPELL) {
72 set_action(this->player_ptr, ACTION_NONE);
75 this->player_ptr->update |= PU_BONUS | PU_HP | PU_MANA | PU_SPELLS;
76 this->player_ptr->redraw |= PR_EXTRA | PR_HP | PR_MANA;
81 * @brief プレイヤーが詠唱中の呪術から一つを選んで停止する
83 bool SpellHex::stop_one_spell()
85 if (!this->is_spelling_any()) {
86 msg_print(_("呪文を詠唱していません。", "You are not casting a spell."));
90 auto casting_num = this->player_ptr->magic_num2[0];
91 if ((casting_num == 1) || (this->player_ptr->lev < 35)) {
92 return this->stop_all_spells();
96 strnfmt(out_val, 78, _("どの呪文の詠唱を中断しますか?(呪文 %c-%c, 'l'全て, ESC)", "Which spell do you stop casting? (Spell %c-%c, 'l' to all, ESC)"),
97 I2A(0), I2A(casting_num - 1));
100 auto is_selected = select_spell_stopping(out_val, choice);
103 auto n = this->casting_spells[A2I(choice)];
104 exe_spell(this->player_ptr, REALM_HEX, n, SPELL_STOP);
105 this->reset_casting_flag(static_cast<spell_hex_type>(n));
106 this->add_casting_num(false);
109 this->player_ptr->update |= PU_BONUS | PU_HP | PU_MANA | PU_SPELLS;
110 this->player_ptr->redraw |= PR_EXTRA | PR_HP | PR_MANA;
116 * @param spells 詠唱中の呪術リスト
118 * @param choice 選択した呪文
119 * @return 選択が完了したらtrue、キャンセルならばfalse
121 bool SpellHex::select_spell_stopping(char *out_val, char &choice)
124 this->display_casting_spells_list();
125 if (!get_com(out_val, &choice, true)) {
129 if (isupper(choice)) {
130 choice = static_cast<char>(tolower(choice));
136 return this->stop_all_spells();
139 if ((choice < I2A(0)) || (choice > I2A(this->player_ptr->magic_num2[0] - 1))) {
147 void SpellHex::display_casting_spells_list()
149 constexpr auto y = 1;
150 constexpr auto x = 20;
152 term_erase(x, y, 255);
153 prt(_(" 名前", " Name"), y, x + 5);
154 for (auto spell : this->casting_spells) {
155 term_erase(x, y + n + 1, 255);
156 auto spell_result = exe_spell(this->player_ptr, REALM_HEX, spell, SPELL_NAME);
157 put_str(format("%c) %s", I2A(n), spell_result), y + n + 1, x + 2);
163 * @brief 一定時間毎に呪術で消費するMPを処理する
165 void SpellHex::decrease_mana()
167 if (this->player_ptr->realm1 != REALM_HEX) {
171 if (!this->is_spelling_any() && !this->player_ptr->magic_num1[1]) {
175 auto need_restart = this->check_restart();
176 if (this->player_ptr->anti_magic) {
177 this->stop_all_spells();
181 if (!this->process_mana_cost(need_restart)) {
185 this->gain_exp_from_hex();
186 for (auto spell : this->casting_spells) {
187 exe_spell(this->player_ptr, REALM_HEX, spell, SPELL_CONT);
192 * @brief 継続的な呪文の詠唱が可能な程度にMPが残っているか確認し、残量に応じて継続・中断を行う
193 * @param need_restart 詠唱を再開するか否か
194 * @return MPが足りているか否か
195 * @todo 64ビットの割り算をしなければいけない箇所には見えない. 調査の後不要ならば消すこと.
197 bool SpellHex::process_mana_cost(const bool need_restart)
199 auto need_mana = this->calc_need_mana();
200 uint need_mana_frac = 0;
201 s64b_div(&need_mana, &need_mana_frac, 0, 3); /* Divide by 3 */
202 need_mana += this->player_ptr->magic_num2[0] - 1;
204 auto enough_mana = s64b_cmp(this->player_ptr->csp, this->player_ptr->csp_frac, need_mana, need_mana_frac) >= 0;
206 this->stop_all_spells();
210 s64b_sub(&(this->player_ptr->csp), &(this->player_ptr->csp_frac), need_mana, need_mana_frac);
211 this->player_ptr->redraw |= PR_MANA;
216 msg_print(_("詠唱を再開した。", "You restart casting."));
217 this->player_ptr->action = ACTION_SPELL;
218 this->player_ptr->update |= PU_BONUS | PU_HP;
219 this->player_ptr->redraw |= PR_MAP | PR_STATUS | PR_STATE;
220 this->player_ptr->update |= PU_MONSTERS;
221 this->player_ptr->window_flags |= PW_OVERHEAD | PW_DUNGEON;
225 bool SpellHex::check_restart()
227 if (this->player_ptr->magic_num1[1] == 0) {
231 this->player_ptr->magic_num1[0] = this->player_ptr->magic_num1[1];
232 this->player_ptr->magic_num1[1] = 0;
236 int SpellHex::calc_need_mana()
239 for (auto spell : this->casting_spells) {
240 const auto *s_ptr = &technic_info[REALM_HEX - MIN_TECHNIC][spell];
241 need_mana += mod_need_mana(this->player_ptr, s_ptr->smana, spell, REALM_HEX);
247 void SpellHex::gain_exp_from_hex()
249 for (auto spell : this->casting_spells) {
250 if (!this->is_spelling_specific(spell)) {
254 if (this->player_ptr->spell_exp[spell] < SPELL_EXP_BEGINNER) {
255 this->player_ptr->spell_exp[spell] += 5;
259 if (this->gain_exp_skilled(spell)) {
263 if (this->gain_exp_expert(spell)) {
267 this->gain_exp_master(spell);
271 bool SpellHex::gain_exp_skilled(const int spell)
273 if (this->player_ptr->spell_exp[spell] >= SPELL_EXP_SKILLED) {
277 auto *floor_ptr = this->player_ptr->current_floor_ptr;
278 auto gain_condition = one_in_(2);
279 gain_condition &= floor_ptr->dun_level > 4;
280 gain_condition &= (floor_ptr->dun_level + 10) > this->player_ptr->lev;
281 if (gain_condition) {
282 this->player_ptr->spell_exp[spell]++;
288 bool SpellHex::gain_exp_expert(const int spell)
290 if (this->player_ptr->spell_exp[spell] >= SPELL_EXP_EXPERT) {
294 const auto *s_ptr = &technic_info[REALM_HEX - MIN_TECHNIC][spell];
295 auto *floor_ptr = this->player_ptr->current_floor_ptr;
296 auto gain_condition = one_in_(5);
297 gain_condition &= (floor_ptr->dun_level + 5) > this->player_ptr->lev;
298 gain_condition &= (floor_ptr->dun_level + 5) > s_ptr->slevel;
299 if (gain_condition) {
300 this->player_ptr->spell_exp[spell]++;
306 void SpellHex::gain_exp_master(const int spell)
308 if (this->player_ptr->spell_exp[spell] >= SPELL_EXP_MASTER) {
312 const auto *s_ptr = &technic_info[REALM_HEX - MIN_TECHNIC][spell];
313 auto *floor_ptr = this->player_ptr->current_floor_ptr;
314 auto gain_condition = one_in_(5);
315 gain_condition &= (floor_ptr->dun_level + 5) > this->player_ptr->lev;
316 gain_condition &= floor_ptr->dun_level > s_ptr->slevel;
317 if (gain_condition) {
318 this->player_ptr->spell_exp[spell]++;
323 * @brief プレイヤーの呪術詠唱枠がすでに最大かどうかを返す
324 * @return すでに全枠を利用しているならTRUEを返す
326 bool SpellHex::is_casting_full_capacity() const
328 auto k_max = (this->player_ptr->lev / 15) + 1;
329 k_max = MIN(k_max, MAX_KEEP);
330 return this->player_ptr->magic_num2[0] >= k_max;
334 * @brief 一定ゲームターン毎に復讐処理の残り期間の判定を行う
336 void SpellHex::continue_revenge()
338 if ((this->player_ptr->realm1 != REALM_HEX) || (this->get_revenge_turn() == 0)) {
342 switch (hex_revenge_type(this->player_ptr)) {
344 exe_spell(this->player_ptr, REALM_HEX, HEX_PATIENCE, SPELL_CONT);
347 exe_spell(this->player_ptr, REALM_HEX, HEX_REVENGE, SPELL_CONT);
355 * @brief 復讐ダメージの追加を行う
356 * @param dam 蓄積されるダメージ量
358 void SpellHex::store_vengeful_damage(HIT_POINT dam)
360 if ((this->player_ptr->realm1 != REALM_HEX) || (this->get_revenge_turn() == 0)) {
364 this->set_revenge_power(dam, false);
369 * @param m_idx 判定の対象となるモンスターID
370 * @return 呪術の効果が適用されるならTRUEを返す
371 * @details v3.0.0現在は反テレポート・反魔法・反増殖の3種類
373 bool SpellHex::check_hex_barrier(MONSTER_IDX m_idx, spell_hex_type type) const
375 const auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[m_idx];
376 const auto *r_ptr = &r_info[m_ptr->r_idx];
377 return this->is_spelling_specific(type) && ((this->player_ptr->lev * 3 / 2) >= randint1(r_ptr->level));
380 bool SpellHex::is_spelling_specific(int hex) const
382 auto check = static_cast<uint32_t>(this->player_ptr->magic_num1[0]);
383 return (this->player_ptr->realm1 == REALM_HEX) && any_bits(check, 1U << hex);
386 bool SpellHex::is_spelling_any() const
388 return (this->player_ptr->realm1 == REALM_HEX) && (this->player_ptr->magic_num1[0] != 0);
392 * @brief 呪術「目には目を」の効果処理
393 * @param this->player_ptr プレイヤーへの参照ポインタ
394 * @param monap_ptr モンスターからプレイヤーへの直接攻撃構造体への参照ポインタ
396 void SpellHex::eyes_on_eyes()
398 if (this->monap_ptr == nullptr) {
399 throw("Invalid constructor was used!");
402 const auto is_eyeeye_finished = (this->player_ptr->tim_eyeeye == 0) && !this->is_spelling_specific(HEX_EYE_FOR_EYE);
403 if (is_eyeeye_finished || (this->monap_ptr->get_damage == 0) || this->player_ptr->is_dead) {
408 msg_format("攻撃が%s自身を傷つけた!", this->monap_ptr->m_name);
410 GAME_TEXT m_name_self[MAX_MONSTER_NAME];
411 monster_desc(this->player_ptr, m_name_self, this->monap_ptr->m_ptr, MD_PRON_VISIBLE | MD_POSSESSIVE | MD_OBJECTIVE);
412 msg_format("The attack of %s has wounded %s!", this->monap_ptr->m_name, m_name_self);
414 const auto y = this->monap_ptr->m_ptr->fy;
415 const auto x = this->monap_ptr->m_ptr->fx;
416 project(this->player_ptr, 0, 0, y, x, this->monap_ptr->get_damage, GF_MISSILE, PROJECT_KILL);
417 if (this->player_ptr->tim_eyeeye) {
418 set_tim_eyeeye(this->player_ptr, this->player_ptr->tim_eyeeye - 5, true);
422 void SpellHex::thief_teleport()
424 if (this->monap_ptr == nullptr) {
425 throw("Invalid constructor was used!");
428 if (!this->monap_ptr->blinked || !this->monap_ptr->alive || this->player_ptr->is_dead) {
432 if (this->check_hex_barrier(this->monap_ptr->m_idx, HEX_ANTI_TELE)) {
433 msg_print(_("泥棒は笑って逃げ...ようとしたがバリアに防がれた。", "The thief flees laughing...? But a magic barrier obstructs it."));
435 msg_print(_("泥棒は笑って逃げた!", "The thief flees laughing!"));
436 teleport_away(this->player_ptr, this->monap_ptr->m_idx, MAX_SIGHT * 2 + 5, TELEPORT_SPONTANEOUS);
440 void SpellHex::set_casting_flag(spell_hex_type type)
442 auto value = static_cast<uint>(this->player_ptr->magic_num1[0]);
443 set_bits(value, 1U << type);
444 this->player_ptr->magic_num1[0] = value;
447 void SpellHex::reset_casting_flag(spell_hex_type type)
449 auto value = static_cast<uint>(this->player_ptr->magic_num1[0]);
450 reset_bits(value, 1U << type);
451 this->player_ptr->magic_num1[0] = value;
454 int32_t SpellHex::get_casting_num() const
456 return this->player_ptr->magic_num2[0];
461 * @param is_incremental 加算ならtrue、減算ならfalse
463 void SpellHex::add_casting_num(bool is_incremental)
465 if (is_incremental) {
466 this->player_ptr->magic_num2[0]++;
468 this->player_ptr->magic_num2[0]--;
472 int32_t SpellHex::get_revenge_power() const
474 return this->player_ptr->magic_num1[2];
477 void SpellHex::set_revenge_power(int32_t power, bool substitution)
480 this->player_ptr->magic_num1[2] = power;
482 this->player_ptr->magic_num1[2] += power;
486 byte SpellHex::get_revenge_turn() const
488 return this->player_ptr->magic_num2[2];
492 * @brief 復讐の残りターンをセットするか、残りターン数を減らす
493 * @param turn 残りターン (非負整数であること)
494 * @param substitution セットならtrue、ターン減少ならfalse
496 void SpellHex::set_revenge_turn(byte turn, bool substitution)
499 this->player_ptr->magic_num2[2] = turn;
501 this->player_ptr->magic_num2[2] -= turn;