OSDN Git Service

[Refactor] #2807 Renamed monster-race-definition.h to monster-race-info.h
[hengbandforosx/hengbandosx.git] / src / spell-kind / spells-world.cpp
1 /*
2  * @brief 帰還やテレポート・レベル等、フロアを跨ぐ魔法効果の処理
3  * @author Hourier
4  * @date 2022/10/10
5  */
6
7 #include "spell-kind/spells-world.h"
8 #include "cmd-io/cmd-save.h"
9 #include "core/asking-player.h"
10 #include "core/player-redraw-types.h"
11 #include "dungeon/quest-completion-checker.h"
12 #include "floor/cave.h"
13 #include "floor/floor-mode-changer.h"
14 #include "floor/floor-town.h"
15 #include "floor/geometry.h"
16 #include "floor/wild.h"
17 #include "game-option/birth-options.h"
18 #include "game-option/play-record-options.h"
19 #include "game-option/special-options.h"
20 #include "io/input-key-acceptor.h"
21 #include "io/write-diary.h"
22 #include "main/sound-definitions-table.h"
23 #include "main/sound-of-music.h"
24 #include "market/building-util.h"
25 #include "monster-floor/monster-remover.h"
26 #include "monster-race/monster-race.h"
27 #include "monster-race/race-flags1.h"
28 #include "monster/monster-describer.h"
29 #include "monster/monster-description-types.h"
30 #include "system/dungeon-info.h"
31 #include "system/floor-type-definition.h"
32 #include "system/grid-type-definition.h"
33 #include "system/monster-race-info.h"
34 #include "system/monster-type-definition.h"
35 #include "system/player-type-definition.h"
36 #include "target/projection-path-calculator.h"
37 #include "target/target-checker.h"
38 #include "target/target-setter.h"
39 #include "target/target-types.h"
40 #include "term/screen-processor.h"
41 #include "util/int-char-converter.h"
42 #include "view/display-messages.h"
43 #include "world/world.h"
44
45 /*!
46  * @brief テレポート・レベルが効かないモンスターであるかどうかを判定する
47  * @param player_ptr プレイヤーへの参照ポインタ
48  * @param idx テレポート・レベル対象のモンスター
49  * @todo 変数名が実態と合っているかどうかは要確認
50  */
51 bool is_teleport_level_ineffective(PlayerType *player_ptr, MONSTER_IDX idx)
52 {
53     auto *floor_ptr = player_ptr->current_floor_ptr;
54     bool is_special_floor = floor_ptr->inside_arena || player_ptr->phase_out || (inside_quest(floor_ptr->quest_number) && !inside_quest(random_quest_number(player_ptr, floor_ptr->dun_level)));
55     bool is_invalid_floor = idx <= 0;
56     is_invalid_floor &= inside_quest(quest_number(player_ptr, floor_ptr->dun_level)) || (floor_ptr->dun_level >= dungeons_info[player_ptr->dungeon_idx].maxdepth);
57     is_invalid_floor &= player_ptr->current_floor_ptr->dun_level >= 1;
58     is_invalid_floor &= ironman_downward;
59     return is_special_floor || is_invalid_floor;
60 }
61
62 /*!
63  * @brief プレイヤー及びモンスターをレベルテレポートさせる /
64  * Teleport the player one level up or down (random when legal)
65  * @param player_ptr プレイヤーへの参照ポインタ
66  * @param m_idx テレポートの対象となるモンスターID(0ならばプレイヤー) / If m_idx <= 0, target is player.
67  * @todo cmd-save.h への依存あり。コールバックで何とかしたい
68  */
69 void teleport_level(PlayerType *player_ptr, MONSTER_IDX m_idx)
70 {
71     GAME_TEXT m_name[160];
72     auto see_m = true;
73     auto &floor_ref = *player_ptr->current_floor_ptr;
74     if (m_idx <= 0) {
75         strcpy(m_name, _("あなた", "you"));
76     } else {
77         auto *m_ptr = &floor_ref.m_list[m_idx];
78         monster_desc(player_ptr, m_name, m_ptr, 0);
79         see_m = is_seen(player_ptr, m_ptr);
80     }
81
82     if (is_teleport_level_ineffective(player_ptr, m_idx)) {
83         if (see_m) {
84             msg_print(_("効果がなかった。", "There is no effect."));
85         }
86         return;
87     }
88
89     if ((m_idx <= 0) && player_ptr->anti_tele) {
90         msg_print(_("不思議な力がテレポートを防いだ!", "A mysterious force prevents you from teleporting!"));
91         return;
92     }
93
94     bool go_up;
95     if (randint0(100) < 50) {
96         go_up = true;
97     } else {
98         go_up = false;
99     }
100
101     if ((m_idx <= 0) && w_ptr->wizard) {
102         if (get_check("Force to go up? ")) {
103             go_up = true;
104         } else if (get_check("Force to go down? ")) {
105             go_up = false;
106         }
107     }
108
109     if ((ironman_downward && (m_idx <= 0)) || (floor_ref.dun_level <= dungeons_info[player_ptr->dungeon_idx].mindepth)) {
110 #ifdef JP
111         if (see_m) {
112             msg_format("%^sは床を突き破って沈んでいく。", m_name);
113         }
114 #else
115         if (see_m) {
116             msg_format("%^s sink%s through the floor.", m_name, (m_idx <= 0) ? "" : "s");
117         }
118 #endif
119         if (m_idx <= 0) {
120             if (!floor_ref.is_in_dungeon()) {
121                 player_ptr->dungeon_idx = ironman_downward ? DUNGEON_ANGBAND : player_ptr->recall_dungeon;
122                 player_ptr->oldpy = player_ptr->y;
123                 player_ptr->oldpx = player_ptr->x;
124             }
125
126             if (record_stair) {
127                 exe_write_diary(player_ptr, DIARY_TELEPORT_LEVEL, 1, nullptr);
128             }
129
130             if (autosave_l) {
131                 do_cmd_save_game(player_ptr, true);
132             }
133
134             if (!floor_ref.is_in_dungeon()) {
135                 floor_ref.dun_level = dungeons_info[player_ptr->dungeon_idx].mindepth;
136                 prepare_change_floor_mode(player_ptr, CFM_RAND_PLACE);
137             } else {
138                 prepare_change_floor_mode(player_ptr, CFM_SAVE_FLOORS | CFM_DOWN | CFM_RAND_PLACE | CFM_RAND_CONNECT);
139             }
140
141             player_ptr->leaving = true;
142         }
143     } else if (inside_quest(quest_number(player_ptr, floor_ref.dun_level)) || (floor_ref.dun_level >= dungeons_info[player_ptr->dungeon_idx].maxdepth)) {
144 #ifdef JP
145         if (see_m) {
146             msg_format("%^sは天井を突き破って宙へ浮いていく。", m_name);
147         }
148 #else
149         if (see_m) {
150             msg_format("%^s rise%s up through the ceiling.", m_name, (m_idx <= 0) ? "" : "s");
151         }
152 #endif
153
154         if (m_idx <= 0) {
155             if (record_stair) {
156                 exe_write_diary(player_ptr, DIARY_TELEPORT_LEVEL, -1, nullptr);
157             }
158
159             if (autosave_l) {
160                 do_cmd_save_game(player_ptr, true);
161             }
162
163             prepare_change_floor_mode(player_ptr, CFM_SAVE_FLOORS | CFM_UP | CFM_RAND_PLACE | CFM_RAND_CONNECT);
164
165             leave_quest_check(player_ptr);
166             floor_ref.quest_number = QuestId::NONE;
167             player_ptr->leaving = true;
168         }
169     } else if (go_up) {
170 #ifdef JP
171         if (see_m) {
172             msg_format("%^sは天井を突き破って宙へ浮いていく。", m_name);
173         }
174 #else
175         if (see_m) {
176             msg_format("%^s rise%s up through the ceiling.", m_name, (m_idx <= 0) ? "" : "s");
177         }
178 #endif
179
180         if (m_idx <= 0) {
181             if (record_stair) {
182                 exe_write_diary(player_ptr, DIARY_TELEPORT_LEVEL, -1, nullptr);
183             }
184
185             if (autosave_l) {
186                 do_cmd_save_game(player_ptr, true);
187             }
188
189             prepare_change_floor_mode(player_ptr, CFM_SAVE_FLOORS | CFM_UP | CFM_RAND_PLACE | CFM_RAND_CONNECT);
190             player_ptr->leaving = true;
191         }
192     } else {
193 #ifdef JP
194         if (see_m) {
195             msg_format("%^sは床を突き破って沈んでいく。", m_name);
196         }
197 #else
198         if (see_m) {
199             msg_format("%^s sink%s through the floor.", m_name, (m_idx <= 0) ? "" : "s");
200         }
201 #endif
202
203         if (m_idx <= 0) {
204             if (record_stair) {
205                 exe_write_diary(player_ptr, DIARY_TELEPORT_LEVEL, 1, nullptr);
206             }
207             if (autosave_l) {
208                 do_cmd_save_game(player_ptr, true);
209             }
210
211             prepare_change_floor_mode(player_ptr, CFM_SAVE_FLOORS | CFM_DOWN | CFM_RAND_PLACE | CFM_RAND_CONNECT);
212             player_ptr->leaving = true;
213         }
214     }
215
216     if (m_idx <= 0) {
217         sound(SOUND_TPLEVEL);
218         return;
219     }
220
221     auto *m_ptr = &floor_ref.m_list[m_idx];
222     QuestCompletionChecker(player_ptr, m_ptr).complete();
223     if (record_named_pet && m_ptr->is_pet() && m_ptr->nickname) {
224         char m2_name[MAX_NLEN];
225
226         monster_desc(player_ptr, m2_name, m_ptr, MD_INDEF_VISIBLE);
227         exe_write_diary(player_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_TELE_LEVEL, m2_name);
228     }
229
230     delete_monster_idx(player_ptr, m_idx);
231     if (see_m) {
232         sound(SOUND_TPLEVEL);
233     }
234 }
235
236 bool teleport_level_other(PlayerType *player_ptr)
237 {
238     if (!target_set(player_ptr, TARGET_KILL)) {
239         return false;
240     }
241     MONSTER_IDX target_m_idx = player_ptr->current_floor_ptr->grid_array[target_row][target_col].m_idx;
242     if (!target_m_idx) {
243         return true;
244     }
245     if (!player_has_los_bold(player_ptr, target_row, target_col)) {
246         return true;
247     }
248     if (!projectable(player_ptr, player_ptr->y, player_ptr->x, target_row, target_col)) {
249         return true;
250     }
251
252     MonsterEntity *m_ptr;
253     MonsterRaceInfo *r_ptr;
254     m_ptr = &player_ptr->current_floor_ptr->m_list[target_m_idx];
255     r_ptr = &monraces_info[m_ptr->r_idx];
256     GAME_TEXT m_name[MAX_NLEN];
257     monster_desc(player_ptr, m_name, m_ptr, 0);
258     msg_format(_("%^sの足を指さした。", "You gesture at %^s's feet."), m_name);
259
260     auto has_immune = r_ptr->resistance_flags.has_any_of(RFR_EFF_RESIST_NEXUS_MASK) || r_ptr->resistance_flags.has(MonsterResistanceType::RESIST_TELEPORT);
261
262     if (has_immune || (r_ptr->flags1 & RF1_QUESTOR) || (r_ptr->level + randint1(50) > player_ptr->lev + randint1(60))) {
263         msg_format(_("しかし効果がなかった!", "%^s is unaffected!"), m_name);
264     } else {
265         teleport_level(player_ptr, target_m_idx);
266     }
267
268     return true;
269 }
270
271 /*!
272  * @brief 町間のテレポートを行うメインルーチン
273  * @param player_ptr プレイヤーへの参照ポインタ
274  * @return テレポート処理を決定したか否か
275  */
276 bool tele_town(PlayerType *player_ptr)
277 {
278     if (player_ptr->current_floor_ptr->dun_level) {
279         msg_print(_("この魔法は地上でしか使えない!", "This spell can only be used on the surface!"));
280         return false;
281     }
282
283     if (player_ptr->current_floor_ptr->inside_arena || player_ptr->phase_out) {
284         msg_print(_("この魔法は外でしか使えない!", "This spell can only be used outside!"));
285         return false;
286     }
287
288     screen_save();
289     clear_bldg(4, 10);
290
291     int i;
292     int num = 0;
293     for (i = 1; i < max_towns; i++) {
294         char buf[80];
295
296         if ((i == VALID_TOWNS) || (i == SECRET_TOWN) || (i == player_ptr->town_num) || !(player_ptr->visit & (1UL << (i - 1)))) {
297             continue;
298         }
299
300         sprintf(buf, "%c) %-20s", I2A(i - 1), town_info[i].name);
301         prt(buf, 5 + i, 5);
302         num++;
303     }
304
305     if (num == 0) {
306         msg_print(_("まだ行けるところがない。", "You have not yet visited any town."));
307         msg_print(nullptr);
308         screen_load();
309         return false;
310     }
311
312     prt(_("どこに行きますか:", "Where do you want to go: "), 0, 0);
313     while (true) {
314         i = inkey();
315
316         if (i == ESCAPE) {
317             screen_load();
318             return false;
319         }
320
321         else if ((i < 'a') || (i > ('a' + max_towns - 2))) {
322             continue;
323         } else if (((i - 'a' + 1) == player_ptr->town_num) || ((i - 'a' + 1) == VALID_TOWNS) || ((i - 'a' + 1) == SECRET_TOWN) || !(player_ptr->visit & (1UL << (i - 'a')))) {
324             continue;
325         }
326         break;
327     }
328
329     for (POSITION y = 0; y < w_ptr->max_wild_y; y++) {
330         for (POSITION x = 0; x < w_ptr->max_wild_x; x++) {
331             if (wilderness[y][x].town == (i - 'a' + 1)) {
332                 player_ptr->wilderness_y = y;
333                 player_ptr->wilderness_x = x;
334             }
335         }
336     }
337
338     player_ptr->leaving = true;
339     player_ptr->leave_bldg = true;
340     player_ptr->teleport_town = true;
341     screen_load();
342     return true;
343 }
344
345 /*!
346  * @brief 現実変容処理
347  * @param player_ptr プレイヤーへの参照ポインタ
348  */
349 void reserve_alter_reality(PlayerType *player_ptr, TIME_EFFECT turns)
350 {
351     if (player_ptr->current_floor_ptr->inside_arena || ironman_downward) {
352         msg_print(_("何も起こらなかった。", "Nothing happens."));
353         return;
354     }
355
356     if (player_ptr->alter_reality || turns == 0) {
357         player_ptr->alter_reality = 0;
358         msg_print(_("景色が元に戻った...", "The view around you returns to normal..."));
359         player_ptr->redraw |= PR_STATUS;
360         return;
361     }
362
363     player_ptr->alter_reality = turns;
364     msg_print(_("回りの景色が変わり始めた...", "The view around you begins to change..."));
365     player_ptr->redraw |= PR_STATUS;
366 }
367
368 /*!
369  * @brief これまでに入ったダンジョンの一覧を表示し、選択させる。
370  * @param note ダンジョンに施す処理記述
371  * @param y コンソールY座標
372  * @param x コンソールX座標
373  * @return 選択されたダンジョンID
374  */
375 static DUNGEON_IDX choose_dungeon(concptr note, POSITION y, POSITION x)
376 {
377     DUNGEON_IDX select_dungeon;
378     if (lite_town || vanilla_town || ironman_downward) {
379         if (max_dlv[DUNGEON_ANGBAND]) {
380             return DUNGEON_ANGBAND;
381         } else {
382             msg_format(_("まだ%sに入ったことはない。", "You haven't entered %s yet."), dungeons_info[DUNGEON_ANGBAND].name.data());
383             msg_print(nullptr);
384             return 0;
385         }
386     }
387
388     std::vector<DUNGEON_IDX> dun;
389
390     screen_save();
391     for (const auto &d_ref : dungeons_info) {
392         char buf[80];
393         bool seiha = false;
394
395         if (d_ref.idx == 0 || !d_ref.maxdepth) {
396             continue;
397         }
398         if (!max_dlv[d_ref.idx]) {
399             continue;
400         }
401         if (MonsterRace(d_ref.final_guardian).is_valid()) {
402             if (!monraces_info[d_ref.final_guardian].max_num) {
403                 seiha = true;
404             }
405         } else if (max_dlv[d_ref.idx] == d_ref.maxdepth) {
406             seiha = true;
407         }
408
409         sprintf(buf, _("      %c) %c%-12s : 最大 %d 階", "      %c) %c%-16s : Max level %d"),
410             static_cast<char>('a' + dun.size()), seiha ? '!' : ' ', d_ref.name.data(), (int)max_dlv[d_ref.idx]);
411         prt(buf, y + dun.size(), x);
412         dun.push_back(d_ref.idx);
413     }
414
415     if (dun.empty()) {
416         prt(_("      選べるダンジョンがない。", "      No dungeon is available."), y, x);
417     }
418
419     prt(format(_("どのダンジョン%sしますか:", "Which dungeon do you %s?: "), note), 0, 0);
420     while (true) {
421         auto i = inkey();
422         if ((i == ESCAPE) || dun.empty()) {
423             screen_load();
424             return 0;
425         }
426         if (i >= 'a' && i < static_cast<char>('a' + dun.size())) {
427             select_dungeon = dun[i - 'a'];
428             break;
429         } else {
430             bell();
431         }
432     }
433     screen_load();
434
435     return select_dungeon;
436 }
437
438 /*!
439  * @brief プレイヤーの帰還発動及び中止処理 /
440  * Recall the player to town or dungeon
441  * @param player_ptr プレイヤーへの参照ポインタ
442  * @param turns 発動までのターン数
443  * @return 常にTRUEを返す
444  * @todo Recall the player to the last visited town when in the wilderness
445  */
446 bool recall_player(PlayerType *player_ptr, TIME_EFFECT turns)
447 {
448     const auto &floor_ref = *player_ptr->current_floor_ptr;
449     if (floor_ref.inside_arena || ironman_downward) {
450         msg_print(_("何も起こらなかった。", "Nothing happens."));
451         return true;
452     }
453
454     bool is_special_floor = floor_ref.is_in_dungeon();
455     is_special_floor &= max_dlv[player_ptr->dungeon_idx] > floor_ref.dun_level;
456     is_special_floor &= !inside_quest(floor_ref.quest_number);
457     is_special_floor &= !player_ptr->word_recall;
458     if (is_special_floor) {
459         if (get_check(_("ここは最深到達階より浅い階です。この階に戻って来ますか? ", "Reset recall depth? "))) {
460             max_dlv[player_ptr->dungeon_idx] = floor_ref.dun_level;
461             if (record_maxdepth) {
462                 exe_write_diary(player_ptr, DIARY_TRUMP, player_ptr->dungeon_idx, _("帰還のときに", "when recalled from dungeon"));
463             }
464         }
465     }
466
467     if (player_ptr->word_recall || turns == 0) {
468         player_ptr->word_recall = 0;
469         msg_print(_("張りつめた大気が流れ去った...", "A tension leaves the air around you..."));
470         player_ptr->redraw |= (PR_STATUS);
471         return true;
472     }
473
474     if (!floor_ref.is_in_dungeon()) {
475         DUNGEON_IDX select_dungeon;
476         select_dungeon = choose_dungeon(_("に帰還", "recall"), 2, 14);
477         if (!select_dungeon) {
478             return false;
479         }
480         player_ptr->recall_dungeon = select_dungeon;
481     }
482
483     player_ptr->word_recall = turns;
484     msg_print(_("回りの大気が張りつめてきた...", "The air about you becomes charged..."));
485     player_ptr->redraw |= (PR_STATUS);
486     return true;
487 }
488
489 bool free_level_recall(PlayerType *player_ptr)
490 {
491     DUNGEON_IDX select_dungeon = choose_dungeon(_("にテレポート", "teleport"), 4, 0);
492     if (!select_dungeon) {
493         return false;
494     }
495
496     const auto &dungeon = dungeons_info[select_dungeon];
497     auto max_depth = dungeon.maxdepth;
498     if (select_dungeon == DUNGEON_ANGBAND) {
499         const auto &quest_list = QuestList::get_instance();
500         if (quest_list[QuestId::OBERON].status != QuestStatusType::FINISHED) {
501             max_depth = 98;
502         } else if (quest_list[QuestId::SERPENT].status != QuestStatusType::FINISHED) {
503             max_depth = 99;
504         }
505     }
506
507     const auto mes = _("%sの何階にテレポートしますか?", "Teleport to which level of %s? ");
508     QUANTITY amt = get_quantity(format(mes, dungeon.name.data()), (QUANTITY)max_depth);
509     if (amt <= 0) {
510         return false;
511     }
512
513     player_ptr->word_recall = 1;
514     player_ptr->recall_dungeon = select_dungeon;
515     max_dlv[player_ptr->recall_dungeon] = ((amt > dungeon.maxdepth) ? dungeon.maxdepth
516                                                                     : ((amt < dungeon.mindepth) ? dungeon.mindepth : amt));
517     if (record_maxdepth) {
518         exe_write_diary(player_ptr, DIARY_TRUMP, select_dungeon, _("トランプタワーで", "at Trump Tower"));
519     }
520
521     msg_print(_("回りの大気が張りつめてきた...", "The air about you becomes charged..."));
522
523     player_ptr->redraw |= PR_STATUS;
524     return true;
525 }
526
527 /*!
528  * @brief フロア・リセット処理
529  * @param player_ptr プレイヤーへの参照ポインタ
530  * @return リセット処理が実際に行われたらTRUEを返す
531  */
532 bool reset_recall(PlayerType *player_ptr)
533 {
534     int select_dungeon, dummy = 0;
535     char ppp[80];
536     char tmp_val[160];
537
538     select_dungeon = choose_dungeon(_("をセット", "reset"), 2, 14);
539     if (ironman_downward) {
540         msg_print(_("何も起こらなかった。", "Nothing happens."));
541         return true;
542     }
543
544     if (!select_dungeon) {
545         return false;
546     }
547     sprintf(ppp, _("何階にセットしますか (%d-%d):", "Reset to which level (%d-%d): "), (int)dungeons_info[select_dungeon].mindepth, (int)max_dlv[select_dungeon]);
548     sprintf(tmp_val, "%d", (int)std::max(player_ptr->current_floor_ptr->dun_level, 1));
549
550     if (!get_string(ppp, tmp_val, 10)) {
551         return false;
552     }
553
554     dummy = atoi(tmp_val);
555     if (dummy < 1) {
556         dummy = 1;
557     }
558     if (dummy > max_dlv[select_dungeon]) {
559         dummy = max_dlv[select_dungeon];
560     }
561     if (dummy < dungeons_info[select_dungeon].mindepth) {
562         dummy = dungeons_info[select_dungeon].mindepth;
563     }
564
565     max_dlv[select_dungeon] = dummy;
566
567     if (record_maxdepth) {
568         exe_write_diary(player_ptr, DIARY_TRUMP, select_dungeon, _("フロア・リセットで", "using a scroll of reset recall"));
569     }
570 #ifdef JP
571     msg_format("%sの帰還レベルを %d 階にセット。", dungeons_info[select_dungeon].name.data(), dummy, dummy * 50);
572 #else
573     msg_format("Recall depth set to level %d (%d').", dummy, dummy * 50);
574 #endif
575     return true;
576 }