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-player.h"
9 #include "monster-race/monster-race.h"
10 #include "player-base/player-class.h"
11 #include "player-info/spell-hex-data-type.h"
12 #include "player/attack-defense-types.h"
13 #include "player/player-skill.h"
14 #include "realm/realm-hex-numbers.h"
15 #include "spell-kind/spells-teleport.h"
16 #include "spell-realm/spells-crusade.h"
17 #include "spell-realm/spells-song.h"
18 #include "spell/spell-info.h"
19 #include "spell/spells-execution.h"
20 #include "spell/technic-info-table.h"
21 #include "status/action-setter.h"
22 #include "system/floor-type-definition.h"
23 #include "system/monster-entity.h"
24 #include "system/monster-race-info.h"
25 #include "system/player-type-definition.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"
33 #include "monster/monster-describer.h"
34 #include "monster/monster-description-types.h"
40 constexpr int MAX_KEEP = 4;
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>())
46 if (!this->spell_hex_data) {
50 HexSpellFlagGroup::get_flags(this->spell_hex_data->casting_spells, std::back_inserter(this->casting_spells));
52 if (this->casting_spells.size() > MAX_KEEP) {
53 throw("Invalid numbers of hex magics keep!");
57 SpellHex::SpellHex(PlayerType *player_ptr, MonsterAttackPlayer *monap_ptr)
58 : player_ptr(player_ptr)
59 , monap_ptr(monap_ptr)
64 * @brief プレイヤーが詠唱中の全呪術を停止する
66 void SpellHex::stop_all_spells()
68 for (auto spell : this->casting_spells) {
69 exe_spell(this->player_ptr, REALM_HEX, spell, SpellProcessType::STOP);
72 this->spell_hex_data->casting_spells.clear();
73 if (this->player_ptr->action == ACTION_SPELL) {
74 set_action(this->player_ptr, ACTION_NONE);
77 this->player_ptr->update |= PU_BONUS | PU_HP | PU_MP | PU_SPELLS;
78 this->player_ptr->redraw |= PR_EXTRA | PR_HP | PR_MP;
82 * @brief プレイヤーが詠唱中の呪術から選択式で一つまたは全てを停止する
83 * @return 停止したらtrue、停止をキャンセルしたらfalse
85 bool SpellHex::stop_spells_with_selection()
87 if (!this->is_spelling_any()) {
88 msg_print(_("呪文を詠唱していません。", "You are not casting a spell."));
92 auto casting_num = this->get_casting_num();
93 if ((casting_num == 1) || (this->player_ptr->lev < 35)) {
94 this->stop_all_spells();
99 strnfmt(out_val, 78, _("どの呪文の詠唱を中断しますか?(呪文 %c-%c, 'l'全て, ESC)", "Which spell do you stop casting? (Spell %c-%c, 'l' to all, ESC)"),
100 I2A(0), I2A(casting_num - 1));
102 auto [is_all, is_selected, choice] = select_spell_stopping(out_val);
109 auto n = this->casting_spells[A2I(choice)];
110 exe_spell(this->player_ptr, REALM_HEX, n, SpellProcessType::STOP);
111 this->reset_casting_flag(i2enum<spell_hex_type>(n));
114 this->player_ptr->update |= PU_BONUS | PU_HP | PU_MP | PU_SPELLS;
115 this->player_ptr->redraw |= PR_EXTRA | PR_HP | PR_MP;
123 * Item1: 全ての呪文を中断するならばtrue、1つの呪文を中断するならばfalse
124 * Item2: 選択が完了したらtrue、キャンセルならばfalse
125 * Item3: 選択した呪文番号 (a~d、lの5択)
127 std::tuple<bool, bool, char> SpellHex::select_spell_stopping(char *out_val)
131 this->display_casting_spells_list();
132 if (!get_com(out_val, &choice, true)) {
133 return std::make_tuple(false, false, choice);
136 if (isupper(choice)) {
137 choice = static_cast<char>(tolower(choice));
142 this->stop_all_spells();
143 return std::make_tuple(true, true, choice);
146 if ((choice < I2A(0)) || (choice > I2A(this->get_casting_num() - 1))) {
150 return std::make_tuple(false, true, choice);
154 void SpellHex::display_casting_spells_list()
156 constexpr auto y = 1;
157 constexpr auto x = 20;
159 term_erase(x, y, 255);
160 prt(_(" 名前", " Name"), y, x + 5);
161 for (auto spell : this->casting_spells) {
162 term_erase(x, y + n + 1, 255);
163 const auto spell_name = exe_spell(this->player_ptr, REALM_HEX, spell, SpellProcessType::NAME);
164 put_str(format("%c) %s", I2A(n), spell_name->data()), y + n + 1, x + 2);
170 * @brief 一定時間毎に呪術で消費するMPを処理する
172 void SpellHex::decrease_mana()
174 if (!this->spell_hex_data) {
178 if (this->spell_hex_data->casting_spells.none() && this->spell_hex_data->interrupting_spells.none()) {
182 auto need_restart = this->check_restart();
183 if (this->player_ptr->anti_magic) {
184 this->stop_all_spells();
188 if (!this->process_mana_cost(need_restart)) {
193 for (auto spell : this->casting_spells) {
194 exe_spell(this->player_ptr, REALM_HEX, spell, SpellProcessType::CONTNUATION);
199 * @brief 継続的な呪文の詠唱が可能な程度にMPが残っているか確認し、残量に応じて継続・中断を行う
200 * @param need_restart 詠唱を再開するか否か
201 * @return MPが足りているか否か
202 * @todo 64ビットの割り算をしなければいけない箇所には見えない. 調査の後不要ならば消すこと.
204 bool SpellHex::process_mana_cost(const bool need_restart)
206 auto need_mana = this->calc_need_mana();
207 uint need_mana_frac = 0;
208 s64b_div(&need_mana, &need_mana_frac, 0, 3); /* Divide by 3 */
209 need_mana += this->get_casting_num() - 1;
211 auto enough_mana = s64b_cmp(this->player_ptr->csp, this->player_ptr->csp_frac, need_mana, need_mana_frac) >= 0;
213 this->stop_all_spells();
217 s64b_sub(&(this->player_ptr->csp), &(this->player_ptr->csp_frac), need_mana, need_mana_frac);
218 this->player_ptr->redraw |= PR_MP;
223 msg_print(_("詠唱を再開した。", "You restart casting."));
224 this->player_ptr->action = ACTION_SPELL;
225 this->player_ptr->update |= PU_BONUS | PU_HP;
226 this->player_ptr->redraw |= PR_MAP | PR_TIMED_EFFECT | PR_ACTION;
227 this->player_ptr->update |= PU_MONSTER_STATUSES;
228 this->player_ptr->window_flags |= PW_OVERHEAD | PW_DUNGEON;
232 bool SpellHex::check_restart()
234 if (this->spell_hex_data->interrupting_spells.none()) {
238 this->spell_hex_data->casting_spells = this->spell_hex_data->interrupting_spells;
239 this->spell_hex_data->interrupting_spells.clear();
243 int SpellHex::calc_need_mana()
246 for (auto spell : this->casting_spells) {
247 const auto *s_ptr = &technic_info[REALM_HEX - MIN_TECHNIC][spell];
248 need_mana += mod_need_mana(this->player_ptr, s_ptr->smana, spell, REALM_HEX);
254 void SpellHex::gain_exp()
256 PlayerSkill ps(player_ptr);
257 for (auto spell : this->casting_spells) {
258 if (!this->is_spelling_specific(spell)) {
262 ps.gain_continuous_spell_skill_exp(REALM_HEX, spell);
267 * @brief プレイヤーの呪術詠唱枠がすでに最大かどうかを返す
268 * @return すでに全枠を利用しているならTRUEを返す
270 bool SpellHex::is_casting_full_capacity() const
272 auto k_max = (this->player_ptr->lev / 15) + 1;
273 k_max = std::min(k_max, MAX_KEEP);
274 return this->get_casting_num() >= k_max;
278 * @brief 一定ゲームターン毎に復讐処理の残り期間の判定を行う
280 void SpellHex::continue_revenge()
282 if (!this->spell_hex_data || (this->get_revenge_turn() == 0)) {
286 switch (this->get_revenge_type()) {
287 case SpellHexRevengeType::PATIENCE:
288 exe_spell(this->player_ptr, REALM_HEX, HEX_PATIENCE, SpellProcessType::CONTNUATION);
290 case SpellHexRevengeType::REVENGE:
291 exe_spell(this->player_ptr, REALM_HEX, HEX_REVENGE, SpellProcessType::CONTNUATION);
299 * @brief 復讐ダメージの追加を行う
300 * @param dam 蓄積されるダメージ量
302 void SpellHex::store_vengeful_damage(int dam)
304 if (!this->spell_hex_data || (this->get_revenge_turn() == 0)) {
308 this->set_revenge_power(dam, false);
313 * @param m_idx 判定の対象となるモンスターID
314 * @return 呪術の効果が適用されるならTRUEを返す
315 * @details v3.0.0現在は反テレポート・反魔法・反増殖の3種類
317 bool SpellHex::check_hex_barrier(MONSTER_IDX m_idx, spell_hex_type type) const
319 const auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[m_idx];
320 const auto *r_ptr = &monraces_info[m_ptr->r_idx];
321 return this->is_spelling_specific(type) && ((this->player_ptr->lev * 3 / 2) >= randint1(r_ptr->level));
324 bool SpellHex::is_spelling_specific(int hex) const
326 return this->spell_hex_data && this->spell_hex_data->casting_spells.has(i2enum<spell_hex_type>(hex));
329 bool SpellHex::is_spelling_any() const
331 return this->spell_hex_data && (this->get_casting_num() > 0);
334 void SpellHex::interrupt_spelling()
336 this->spell_hex_data->interrupting_spells = this->spell_hex_data->casting_spells;
337 this->spell_hex_data->casting_spells.clear();
341 * @brief 呪術「目には目を」の効果処理
342 * @param this->player_ptr プレイヤーへの参照ポインタ
343 * @param monap_ptr モンスターからプレイヤーへの直接攻撃構造体への参照ポインタ
345 void SpellHex::eyes_on_eyes()
347 if (this->monap_ptr == nullptr) {
348 throw("Invalid constructor was used!");
351 const auto is_eyeeye_finished = (this->player_ptr->tim_eyeeye == 0) && !this->is_spelling_specific(HEX_EYE_FOR_EYE);
352 if (is_eyeeye_finished || (this->monap_ptr->get_damage == 0) || this->player_ptr->is_dead) {
357 msg_format("攻撃が%s自身を傷つけた!", this->monap_ptr->m_name);
359 const auto m_name_self = monster_desc(this->player_ptr, this->monap_ptr->m_ptr, MD_PRON_VISIBLE | MD_POSSESSIVE | MD_OBJECTIVE);
360 msg_format("The attack of %s has wounded %s!", this->monap_ptr->m_name, m_name_self.data());
362 const auto y = this->monap_ptr->m_ptr->fy;
363 const auto x = this->monap_ptr->m_ptr->fx;
364 project(this->player_ptr, 0, 0, y, x, this->monap_ptr->get_damage, AttributeType::MISSILE, PROJECT_KILL);
365 if (this->player_ptr->tim_eyeeye) {
366 set_tim_eyeeye(this->player_ptr, this->player_ptr->tim_eyeeye - 5, true);
370 void SpellHex::thief_teleport()
372 if (this->monap_ptr == nullptr) {
373 throw("Invalid constructor was used!");
376 if (!this->monap_ptr->blinked || !this->monap_ptr->alive || this->player_ptr->is_dead) {
380 if (this->check_hex_barrier(this->monap_ptr->m_idx, HEX_ANTI_TELE)) {
381 msg_print(_("泥棒は笑って逃げ...ようとしたがバリアに防がれた。", "The thief flees laughing...? But a magic barrier obstructs it."));
383 msg_print(_("泥棒は笑って逃げた!", "The thief flees laughing!"));
384 teleport_away(this->player_ptr, this->monap_ptr->m_idx, MAX_PLAYER_SIGHT * 2 + 5, TELEPORT_SPONTANEOUS);
388 void SpellHex::set_casting_flag(spell_hex_type type)
390 this->spell_hex_data->casting_spells.set(type);
393 void SpellHex::reset_casting_flag(spell_hex_type type)
395 this->spell_hex_data->casting_spells.reset(type);
398 int32_t SpellHex::get_casting_num() const
400 return this->spell_hex_data->casting_spells.count();
403 int32_t SpellHex::get_revenge_power() const
405 return this->spell_hex_data->revenge_power;
408 void SpellHex::set_revenge_power(int32_t power, bool substitution)
411 this->spell_hex_data->revenge_power = power;
413 this->spell_hex_data->revenge_power += power;
417 byte SpellHex::get_revenge_turn() const
419 return this->spell_hex_data->revenge_turn;
423 * @brief 復讐の残りターンをセットするか、残りターン数を減らす
424 * @param turn 残りターン (非負整数であること)
425 * @param substitution セットならtrue、ターン減少ならfalse
427 void SpellHex::set_revenge_turn(byte turn, bool substitution)
430 this->spell_hex_data->revenge_turn = turn;
432 this->spell_hex_data->revenge_turn -= turn;
436 SpellHexRevengeType SpellHex::get_revenge_type() const
438 return this->spell_hex_data->revenge_type;
441 void SpellHex::set_revenge_type(SpellHexRevengeType type)
443 this->spell_hex_data->revenge_type = type;