OSDN Git Service

Merge pull request #3388 from Hourier/Fix-Compilation-Warnings-3.0.0Beta-3
[hengbandforosx/hengbandosx.git] / src / spell-realm / spells-hex.cpp
1 #include "spell-realm/spells-hex.h"
2 #include "core/asking-player.h"
3 #include "core/window-redrawer.h"
4 #include "effect/effect-characteristics.h"
5 #include "effect/effect-processor.h"
6 #include "monster-attack/monster-attack-player.h"
7 #include "monster-race/monster-race.h"
8 #include "player-base/player-class.h"
9 #include "player-info/spell-hex-data-type.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/spells-execution.h"
18 #include "spell/technic-info-table.h"
19 #include "status/action-setter.h"
20 #include "system/angband-exceptions.h"
21 #include "system/floor-type-definition.h"
22 #include "system/monster-entity.h"
23 #include "system/monster-race-info.h"
24 #include "system/player-type-definition.h"
25 #include "system/redrawing-flags-updater.h"
26 #include "term/screen-processor.h"
27 #include "util/bit-flags-calculator.h"
28 #include "util/int-char-converter.h"
29 #include "view/display-messages.h"
30
31 #ifdef JP
32 #else
33 #include "monster/monster-describer.h"
34 #include "monster/monster-description-types.h"
35 #endif
36
37 #include <iterator>
38
39 /*!< 呪術の最大詠唱数 */
40 constexpr int MAX_KEEP = 4;
41
42 SpellHex::SpellHex(PlayerType *player_ptr)
43     : player_ptr(player_ptr)
44     , spell_hex_data(PlayerClass(player_ptr).get_specific_data<spell_hex_data_type>())
45 {
46     if (!this->spell_hex_data) {
47         return;
48     }
49
50     HexSpellFlagGroup::get_flags(this->spell_hex_data->casting_spells, std::back_inserter(this->casting_spells));
51
52     if (this->casting_spells.size() > MAX_KEEP) {
53         THROW_EXCEPTION(std::logic_error, "Invalid numbers of hex magics keep!");
54     }
55 }
56
57 SpellHex::SpellHex(PlayerType *player_ptr, MonsterAttackPlayer *monap_ptr)
58     : player_ptr(player_ptr)
59     , monap_ptr(monap_ptr)
60 {
61 }
62
63 /*!
64  * @brief プレイヤーが詠唱中の全呪術を停止する
65  */
66 void SpellHex::stop_all_spells()
67 {
68     for (auto spell : this->casting_spells) {
69         exe_spell(this->player_ptr, REALM_HEX, spell, SpellProcessType::STOP);
70     }
71
72     this->spell_hex_data->casting_spells.clear();
73     if (this->player_ptr->action == ACTION_SPELL) {
74         set_action(this->player_ptr, ACTION_NONE);
75     }
76
77     auto &rfu = RedrawingFlagsUpdater::get_instance();
78     static constexpr auto flags_srf = {
79         StatusRecalculatingFlag::BONUS,
80         StatusRecalculatingFlag::HP,
81         StatusRecalculatingFlag::MP,
82         StatusRecalculatingFlag::SPELLS,
83     };
84     rfu.set_flags(flags_srf);
85     static constexpr auto flags_mwrf = {
86         MainWindowRedrawingFlag::EXTRA,
87         MainWindowRedrawingFlag::HP,
88         MainWindowRedrawingFlag::MP,
89     };
90     rfu.set_flags(flags_mwrf);
91 }
92
93 /*!
94  * @brief プレイヤーが詠唱中の呪術から選択式で一つまたは全てを停止する
95  * @return 停止したらtrue、停止をキャンセルしたらfalse
96  */
97 bool SpellHex::stop_spells_with_selection()
98 {
99     if (!this->is_spelling_any()) {
100         msg_print(_("呪文を詠唱していません。", "You are not casting a spell."));
101         return false;
102     }
103
104     auto casting_num = this->get_casting_num();
105     if ((casting_num == 1) || (this->player_ptr->lev < 35)) {
106         this->stop_all_spells();
107         return true;
108     }
109
110     char out_val[160];
111     strnfmt(out_val, 78, _("どの呪文の詠唱を中断しますか?(呪文 %c-%c, 'l'全て, ESC)", "Which spell do you stop casting? (Spell %c-%c, 'l' to all, ESC)"),
112         I2A(0), I2A(casting_num - 1));
113     screen_save();
114     auto [is_all, is_selected, choice] = select_spell_stopping(out_val);
115     if (is_all) {
116         return true;
117     }
118
119     screen_load();
120     if (is_selected) {
121         auto n = this->casting_spells[A2I(choice)];
122         exe_spell(this->player_ptr, REALM_HEX, n, SpellProcessType::STOP);
123         this->reset_casting_flag(i2enum<spell_hex_type>(n));
124     }
125
126     auto &rfu = RedrawingFlagsUpdater::get_instance();
127     static constexpr auto flags_srf = {
128         StatusRecalculatingFlag::BONUS,
129         StatusRecalculatingFlag::HP,
130         StatusRecalculatingFlag::MP,
131         StatusRecalculatingFlag::SPELLS,
132     };
133     rfu.set_flags(flags_srf);
134     static constexpr auto flags_mwrf = {
135         MainWindowRedrawingFlag::EXTRA,
136         MainWindowRedrawingFlag::HP,
137         MainWindowRedrawingFlag::MP,
138     };
139     rfu.set_flags(flags_mwrf);
140     return is_selected;
141 }
142
143 /*!
144  * @brief 中断する呪術を選択する
145  * @param out_val 呪文名
146  * @return
147  * Item1: 全ての呪文を中断するならばtrue、1つの呪文を中断するならばfalse
148  * Item2: 選択が完了したらtrue、キャンセルならばfalse
149  * Item3: 選択した呪文番号 (a~d、lの5択)
150  */
151 std::tuple<bool, bool, char> SpellHex::select_spell_stopping(char *out_val)
152 {
153     while (true) {
154         char choice = 0;
155         this->display_casting_spells_list();
156         if (!get_com(out_val, &choice, true)) {
157             return std::make_tuple(false, false, choice);
158         }
159
160         if (isupper(choice)) {
161             choice = static_cast<char>(tolower(choice));
162         }
163
164         if (choice == 'l') {
165             screen_load();
166             this->stop_all_spells();
167             return std::make_tuple(true, true, choice);
168         }
169
170         if ((choice < I2A(0)) || (choice > I2A(this->get_casting_num() - 1))) {
171             continue;
172         }
173
174         return std::make_tuple(false, true, choice);
175     }
176 }
177
178 void SpellHex::display_casting_spells_list()
179 {
180     constexpr auto y = 1;
181     constexpr auto x = 20;
182     auto n = 0;
183     term_erase(x, y, 255);
184     prt(_("     名前", "     Name"), y, x + 5);
185     for (auto spell : this->casting_spells) {
186         term_erase(x, y + n + 1, 255);
187         const auto spell_name = exe_spell(this->player_ptr, REALM_HEX, spell, SpellProcessType::NAME);
188         put_str(format("%c)  %s", I2A(n), spell_name->data()), y + n + 1, x + 2);
189         n++;
190     }
191 }
192
193 /*!
194  * @brief 一定時間毎に呪術で消費するMPを処理する
195  */
196 void SpellHex::decrease_mana()
197 {
198     if (!this->spell_hex_data) {
199         return;
200     }
201
202     if (this->spell_hex_data->casting_spells.none() && this->spell_hex_data->interrupting_spells.none()) {
203         return;
204     }
205
206     auto need_restart = this->check_restart();
207     if (this->player_ptr->anti_magic) {
208         this->stop_all_spells();
209         return;
210     }
211
212     if (!this->process_mana_cost(need_restart)) {
213         return;
214     }
215
216     this->gain_exp();
217     for (auto spell : this->casting_spells) {
218         exe_spell(this->player_ptr, REALM_HEX, spell, SpellProcessType::CONTNUATION);
219     }
220 }
221
222 /*!
223  * @brief 継続的な呪文の詠唱が可能な程度にMPが残っているか確認し、残量に応じて継続・中断を行う
224  * @param need_restart 詠唱を再開するか否か
225  * @return MPが足りているか否か
226  * @todo 64ビットの割り算をしなければいけない箇所には見えない. 調査の後不要ならば消すこと.
227  */
228 bool SpellHex::process_mana_cost(const bool need_restart)
229 {
230     auto need_mana = this->calc_need_mana();
231     uint need_mana_frac = 0;
232     s64b_div(&need_mana, &need_mana_frac, 0, 3); /* Divide by 3 */
233     need_mana += this->get_casting_num() - 1;
234
235     auto enough_mana = s64b_cmp(this->player_ptr->csp, this->player_ptr->csp_frac, need_mana, need_mana_frac) >= 0;
236     if (!enough_mana) {
237         this->stop_all_spells();
238         return false;
239     }
240
241     s64b_sub(&(this->player_ptr->csp), &(this->player_ptr->csp_frac), need_mana, need_mana_frac);
242     auto &rfu = RedrawingFlagsUpdater::get_instance();
243     rfu.set_flag(MainWindowRedrawingFlag::MP);
244     if (!need_restart) {
245         return true;
246     }
247
248     msg_print(_("詠唱を再開した。", "You restart casting."));
249     this->player_ptr->action = ACTION_SPELL;
250     static constexpr auto flags_srf = {
251         StatusRecalculatingFlag::BONUS,
252         StatusRecalculatingFlag::HP,
253         StatusRecalculatingFlag::MONSTER_STATUSES,
254     };
255     rfu.set_flags(flags_srf);
256     static constexpr auto flags_mwrf = {
257         MainWindowRedrawingFlag::MAP,
258         MainWindowRedrawingFlag::TIMED_EFFECT,
259         MainWindowRedrawingFlag::ACTION,
260     };
261     rfu.set_flags(flags_mwrf);
262     static constexpr auto flags_swrf = {
263         SubWindowRedrawingFlag::OVERHEAD,
264         SubWindowRedrawingFlag::DUNGEON,
265     };
266     rfu.set_flags(flags_swrf);
267     return true;
268 }
269
270 bool SpellHex::check_restart()
271 {
272     if (this->spell_hex_data->interrupting_spells.none()) {
273         return false;
274     }
275
276     this->spell_hex_data->casting_spells = this->spell_hex_data->interrupting_spells;
277     this->spell_hex_data->interrupting_spells.clear();
278     return true;
279 }
280
281 int SpellHex::calc_need_mana()
282 {
283     auto need_mana = 0;
284     for (auto spell : this->casting_spells) {
285         const auto *s_ptr = &technic_info[REALM_HEX - MIN_TECHNIC][spell];
286         need_mana += mod_need_mana(this->player_ptr, s_ptr->smana, spell, REALM_HEX);
287     }
288
289     return need_mana;
290 }
291
292 void SpellHex::gain_exp()
293 {
294     PlayerSkill ps(player_ptr);
295     for (auto spell : this->casting_spells) {
296         if (!this->is_spelling_specific(spell)) {
297             continue;
298         }
299
300         ps.gain_continuous_spell_skill_exp(REALM_HEX, spell);
301     }
302 }
303
304 /*!
305  * @brief プレイヤーの呪術詠唱枠がすでに最大かどうかを返す
306  * @return すでに全枠を利用しているならTRUEを返す
307  */
308 bool SpellHex::is_casting_full_capacity() const
309 {
310     auto k_max = (this->player_ptr->lev / 15) + 1;
311     k_max = std::min(k_max, MAX_KEEP);
312     return this->get_casting_num() >= k_max;
313 }
314
315 /*!
316  * @brief 一定ゲームターン毎に復讐処理の残り期間の判定を行う
317  */
318 void SpellHex::continue_revenge()
319 {
320     if (!this->spell_hex_data || (this->get_revenge_turn() == 0)) {
321         return;
322     }
323
324     switch (this->get_revenge_type()) {
325     case SpellHexRevengeType::PATIENCE:
326         exe_spell(this->player_ptr, REALM_HEX, HEX_PATIENCE, SpellProcessType::CONTNUATION);
327         return;
328     case SpellHexRevengeType::REVENGE:
329         exe_spell(this->player_ptr, REALM_HEX, HEX_REVENGE, SpellProcessType::CONTNUATION);
330         return;
331     default:
332         return;
333     }
334 }
335
336 /*!
337  * @brief 復讐ダメージの追加を行う
338  * @param dam 蓄積されるダメージ量
339  */
340 void SpellHex::store_vengeful_damage(int dam)
341 {
342     if (!this->spell_hex_data || (this->get_revenge_turn() == 0)) {
343         return;
344     }
345
346     this->set_revenge_power(dam, false);
347 }
348
349 /*!
350  * @brief 呪術結界の判定
351  * @param m_idx 判定の対象となるモンスターID
352  * @return 呪術の効果が適用されるならTRUEを返す
353  * @details v3.0.0現在は反テレポート・反魔法・反増殖の3種類
354  */
355 bool SpellHex::check_hex_barrier(MONSTER_IDX m_idx, spell_hex_type type) const
356 {
357     const auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[m_idx];
358     const auto *r_ptr = &monraces_info[m_ptr->r_idx];
359     return this->is_spelling_specific(type) && ((this->player_ptr->lev * 3 / 2) >= randint1(r_ptr->level));
360 }
361
362 bool SpellHex::is_spelling_specific(int hex) const
363 {
364     return this->spell_hex_data && this->spell_hex_data->casting_spells.has(i2enum<spell_hex_type>(hex));
365 }
366
367 bool SpellHex::is_spelling_any() const
368 {
369     return this->spell_hex_data && (this->get_casting_num() > 0);
370 }
371
372 void SpellHex::interrupt_spelling()
373 {
374     this->spell_hex_data->interrupting_spells = this->spell_hex_data->casting_spells;
375     this->spell_hex_data->casting_spells.clear();
376 }
377
378 /*!
379  * @brief 呪術「目には目を」の効果処理
380  * @param this->player_ptr プレイヤーへの参照ポインタ
381  * @param monap_ptr モンスターからプレイヤーへの直接攻撃構造体への参照ポインタ
382  */
383 void SpellHex::eyes_on_eyes()
384 {
385     if (this->monap_ptr == nullptr) {
386         THROW_EXCEPTION(std::logic_error, "Invalid constructor was used!");
387     }
388
389     const auto is_eyeeye_finished = (this->player_ptr->tim_eyeeye == 0) && !this->is_spelling_specific(HEX_EYE_FOR_EYE);
390     if (is_eyeeye_finished || (this->monap_ptr->get_damage == 0) || this->player_ptr->is_dead) {
391         return;
392     }
393
394 #ifdef JP
395     msg_format("攻撃が%s自身を傷つけた!", this->monap_ptr->m_name);
396 #else
397     const auto m_name_self = monster_desc(this->player_ptr, this->monap_ptr->m_ptr, MD_PRON_VISIBLE | MD_POSSESSIVE | MD_OBJECTIVE);
398     msg_format("The attack of %s has wounded %s!", this->monap_ptr->m_name, m_name_self.data());
399 #endif
400     const auto y = this->monap_ptr->m_ptr->fy;
401     const auto x = this->monap_ptr->m_ptr->fx;
402     project(this->player_ptr, 0, 0, y, x, this->monap_ptr->get_damage, AttributeType::MISSILE, PROJECT_KILL);
403     if (this->player_ptr->tim_eyeeye) {
404         set_tim_eyeeye(this->player_ptr, this->player_ptr->tim_eyeeye - 5, true);
405     }
406 }
407
408 void SpellHex::thief_teleport()
409 {
410     if (this->monap_ptr == nullptr) {
411         THROW_EXCEPTION(std::logic_error, "Invalid constructor was used!");
412     }
413
414     if (!this->monap_ptr->blinked || !this->monap_ptr->alive || this->player_ptr->is_dead) {
415         return;
416     }
417
418     if (this->check_hex_barrier(this->monap_ptr->m_idx, HEX_ANTI_TELE)) {
419         msg_print(_("泥棒は笑って逃げ...ようとしたがバリアに防がれた。", "The thief flees laughing...? But a magic barrier obstructs it."));
420     } else {
421         msg_print(_("泥棒は笑って逃げた!", "The thief flees laughing!"));
422         teleport_away(this->player_ptr, this->monap_ptr->m_idx, MAX_PLAYER_SIGHT * 2 + 5, TELEPORT_SPONTANEOUS);
423     }
424 }
425
426 void SpellHex::set_casting_flag(spell_hex_type type)
427 {
428     this->spell_hex_data->casting_spells.set(type);
429 }
430
431 void SpellHex::reset_casting_flag(spell_hex_type type)
432 {
433     this->spell_hex_data->casting_spells.reset(type);
434 }
435
436 int32_t SpellHex::get_casting_num() const
437 {
438     return this->spell_hex_data->casting_spells.count();
439 }
440
441 int32_t SpellHex::get_revenge_power() const
442 {
443     return this->spell_hex_data->revenge_power;
444 }
445
446 void SpellHex::set_revenge_power(int32_t power, bool substitution)
447 {
448     if (substitution) {
449         this->spell_hex_data->revenge_power = power;
450     } else {
451         this->spell_hex_data->revenge_power += power;
452     }
453 }
454
455 byte SpellHex::get_revenge_turn() const
456 {
457     return this->spell_hex_data->revenge_turn;
458 }
459
460 /*!
461  * @brief 復讐の残りターンをセットするか、残りターン数を減らす
462  * @param turn 残りターン (非負整数であること)
463  * @param substitution セットならtrue、ターン減少ならfalse
464  */
465 void SpellHex::set_revenge_turn(byte turn, bool substitution)
466 {
467     if (substitution) {
468         this->spell_hex_data->revenge_turn = turn;
469     } else {
470         this->spell_hex_data->revenge_turn -= turn;
471     }
472 }
473
474 SpellHexRevengeType SpellHex::get_revenge_type() const
475 {
476     return this->spell_hex_data->revenge_type;
477 }
478
479 void SpellHex::set_revenge_type(SpellHexRevengeType type)
480 {
481     this->spell_hex_data->revenge_type = type;
482 }