OSDN Git Service

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